mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
added support for includes
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@31 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -10,6 +10,26 @@
|
|||||||
#include <util/error.hpp>
|
#include <util/error.hpp>
|
||||||
#include <data/game.hpp>
|
#include <data/game.hpp>
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------- : IncludePackage
|
||||||
|
|
||||||
|
/// A package that just contains a bunch of files that are used from other packages
|
||||||
|
class IncludePackage : public Packaged {
|
||||||
|
protected:
|
||||||
|
String typeName() const;
|
||||||
|
DECLARE_REFLECTION();
|
||||||
|
};
|
||||||
|
|
||||||
|
String IncludePackage::typeName() const { return _("include"); }
|
||||||
|
|
||||||
|
IMPLEMENT_REFLECTION(IncludePackage) {
|
||||||
|
if (tag.reading()) {
|
||||||
|
// ingore
|
||||||
|
String full_name, version;
|
||||||
|
REFLECT(full_name);
|
||||||
|
REFLECT(version);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : PackageManager
|
// ----------------------------------------------------------------------------- : PackageManager
|
||||||
|
|
||||||
String program_dir() {
|
String program_dir() {
|
||||||
@@ -50,10 +70,10 @@ PackagedP PackageManager::openAny(const String& name) {
|
|||||||
if (fn.GetExt() == _("mse-game")) p = new_shared<Game>();
|
if (fn.GetExt() == _("mse-game")) p = new_shared<Game>();
|
||||||
// else if (fn.GetExt() == _("mse-style")) p = new_shared<CardStyle>();
|
// else if (fn.GetExt() == _("mse-style")) p = new_shared<CardStyle>();
|
||||||
// else if (fn.GetExt() == _("mse-locale")) p = new_shared<Locale>();
|
// else if (fn.GetExt() == _("mse-locale")) p = new_shared<Locale>();
|
||||||
// else if (fn.GetExt() == _("mse-include")) p = new_shared<IncludePackage>();
|
else if (fn.GetExt() == _("mse-include")) p = new_shared<IncludePackage>();
|
||||||
// else if (fn.GetExt() == _("mse-symbol-font")) p = new_shared<SymbolFont>();
|
// else if (fn.GetExt() == _("mse-symbol-font")) p = new_shared<SymbolFont>();
|
||||||
else {
|
else {
|
||||||
throw PackageError(_("Unrecognized package type: ") + fn.GetExt());
|
throw PackageError(_("Unrecognized package type: '") + fn.GetExt() + _("'\nwhile trying to open: ") + name);
|
||||||
}
|
}
|
||||||
p->open(filename);
|
p->open(filename);
|
||||||
return p;
|
return p;
|
||||||
@@ -64,6 +84,19 @@ String PackageManager::findFirst(const String& pattern) {
|
|||||||
return wxFindFirstFile(data_directory + _("/") + pattern, 0);
|
return wxFindFirstFile(data_directory + _("/") + pattern, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputStreamP PackageManager::openFileFromPackage(const String& name) {
|
||||||
|
// we don't want an absolute path (for security reasons)
|
||||||
|
String n;
|
||||||
|
if (!name.empty() && name.GetChar(0) == _('/')) n = name.substr(1);
|
||||||
|
else n = name;
|
||||||
|
// break
|
||||||
|
size_t pos = n.find_first_of(_("/\\"));
|
||||||
|
if (pos == String::npos) throw FileNotFoundError(n, _("No package name specified, use 'package/filename'"));
|
||||||
|
// open package and file
|
||||||
|
PackagedP p = openAny(n.substr(0, pos));
|
||||||
|
return p->openIn(n.substr(pos+1));
|
||||||
|
}
|
||||||
|
|
||||||
void PackageManager::destroy() {
|
void PackageManager::destroy() {
|
||||||
loaded_packages.clear();
|
loaded_packages.clear();
|
||||||
}
|
}
|
||||||
@@ -53,6 +53,9 @@ class PackageManager {
|
|||||||
*/
|
*/
|
||||||
String findFirst(const String& pattern);
|
String findFirst(const String& pattern);
|
||||||
|
|
||||||
|
// Open a file from a package, with a name encoded as "package/file"
|
||||||
|
InputStreamP openFileFromPackage(const String& name);
|
||||||
|
|
||||||
/// Empty the list of packages.
|
/// Empty the list of packages.
|
||||||
/** This function MUST be called before the program terminates, otherwise
|
/** This function MUST be called before the program terminates, otherwise
|
||||||
* we could get into fights with pool allocators used by ScriptValues */
|
* we could get into fights with pool allocators used by ScriptValues */
|
||||||
|
|||||||
+19
-1
@@ -9,10 +9,11 @@
|
|||||||
#include "reader.hpp"
|
#include "reader.hpp"
|
||||||
#include <util/vector2d.hpp>
|
#include <util/vector2d.hpp>
|
||||||
#include <util/error.hpp>
|
#include <util/error.hpp>
|
||||||
|
#include <util/io/package_manager.hpp>
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : Reader
|
// ----------------------------------------------------------------------------- : Reader
|
||||||
|
|
||||||
Reader::Reader(const InputStreamP& input, String filename)
|
Reader::Reader(const InputStreamP& input, const String& filename)
|
||||||
: input(input), filename(filename), line_number(0)
|
: input(input), filename(filename), line_number(0)
|
||||||
, indent(0), expected_indent(0), just_opened(false)
|
, indent(0), expected_indent(0), just_opened(false)
|
||||||
, stream(*input)
|
, stream(*input)
|
||||||
@@ -20,6 +21,16 @@ Reader::Reader(const InputStreamP& input, String filename)
|
|||||||
moveNext();
|
moveNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reader::Reader(const String& filename)
|
||||||
|
: input(packages.openFileFromPackage(filename))
|
||||||
|
, filename(filename), line_number(0)
|
||||||
|
, indent(0), expected_indent(0), just_opened(false)
|
||||||
|
, stream(*input)
|
||||||
|
{
|
||||||
|
moveNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Reader::warning(const String& msg) {
|
void Reader::warning(const String& msg) {
|
||||||
wxMessageBox((msg + _("\nOn line: ")) << line_number << _("\nIn file: ") << filename, _("Warning"), wxOK | wxICON_EXCLAMATION);
|
wxMessageBox((msg + _("\nOn line: ")) << line_number << _("\nIn file: ") << filename, _("Warning"), wxOK | wxICON_EXCLAMATION);
|
||||||
}
|
}
|
||||||
@@ -84,6 +95,13 @@ void Reader::readLine() {
|
|||||||
value = pos == String::npos ? _("") : trim_left(line.substr(pos+1));
|
value = pos == String::npos ? _("") : trim_left(line.substr(pos+1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Reader::unknownKey() {
|
||||||
|
warning(_("Unexpected key: '") + key + _("'"));
|
||||||
|
do {
|
||||||
|
moveNext();
|
||||||
|
} while (indent > expected_indent);
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : Handling basic types
|
// ----------------------------------------------------------------------------- : Handling basic types
|
||||||
|
|
||||||
template <> void Reader::handle(String& s) {
|
template <> void Reader::handle(String& s) {
|
||||||
|
|||||||
+16
-11
@@ -33,10 +33,11 @@ class Reader {
|
|||||||
/// Construct a reader that reads from the given input stream
|
/// Construct a reader that reads from the given input stream
|
||||||
/** filename is used only for error messages
|
/** filename is used only for error messages
|
||||||
*/
|
*/
|
||||||
Reader(const InputStreamP& input, String filename = _(""));
|
Reader(const InputStreamP& input, const String& filename = wxEmptyString);
|
||||||
|
|
||||||
/// Construct a reader that reads a file in a package
|
/// Construct a reader that reads a file in a package
|
||||||
Reader(String filename);
|
/** Used for "include file" keys. */
|
||||||
|
Reader(const String& filename);
|
||||||
|
|
||||||
/// Tell the reflection code we are reading
|
/// Tell the reflection code we are reading
|
||||||
inline bool reading() const { return true; }
|
inline bool reading() const { return true; }
|
||||||
@@ -106,8 +107,18 @@ class Reader {
|
|||||||
/// Reads the next line from the input, and stores it in line/key/value/indent
|
/// Reads the next line from the input, and stores it in line/key/value/indent
|
||||||
void readLine();
|
void readLine();
|
||||||
|
|
||||||
/// Issue a warning: "Unexpected key: $key"
|
/// No line was read, because nothing mathes the current key
|
||||||
void unexpected();
|
/** Maybe the key is "include file" */
|
||||||
|
template <typename T>
|
||||||
|
void unknownKey(T& v) {
|
||||||
|
if (key == _("include_file")) {
|
||||||
|
Reader reader(value);
|
||||||
|
reader.handle(v);
|
||||||
|
} else {
|
||||||
|
unknownKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void unknownKey();
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : Container types
|
// ----------------------------------------------------------------------------- : Container types
|
||||||
@@ -150,13 +161,7 @@ void Reader::handle(map<K,V>& map) {
|
|||||||
while (indent >= expected_indent) { \
|
while (indent >= expected_indent) { \
|
||||||
UInt l = line_number; \
|
UInt l = line_number; \
|
||||||
object.reflect(*this); \
|
object.reflect(*this); \
|
||||||
if (l == line_number) { \
|
if (l == line_number) unknownKey(object); \
|
||||||
/* warning: unexpected key */ \
|
|
||||||
warning(_("Unexpected key: '") + key + _("'")); \
|
|
||||||
do { \
|
|
||||||
moveNext(); \
|
|
||||||
} while (indent > expected_indent); \
|
|
||||||
} \
|
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
void Cls::reflect(Reader& reader) { \
|
void Cls::reflect(Reader& reader) { \
|
||||||
|
|||||||
Reference in New Issue
Block a user