Commit 12a5e5ab authored by twanvl's avatar twanvl

added support for includes

parent a2db142d
......@@ -10,6 +10,26 @@
#include <util/error.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
String program_dir() {
......@@ -50,10 +70,10 @@ PackagedP PackageManager::openAny(const String& name) {
if (fn.GetExt() == _("mse-game")) p = new_shared<Game>();
// 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-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 {
throw PackageError(_("Unrecognized package type: ") + fn.GetExt());
throw PackageError(_("Unrecognized package type: '") + fn.GetExt() + _("'\nwhile trying to open: ") + name);
}
p->open(filename);
return p;
......@@ -64,6 +84,19 @@ String PackageManager::findFirst(const String& pattern) {
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() {
loaded_packages.clear();
}
\ No newline at end of file
......@@ -53,6 +53,9 @@ class PackageManager {
*/
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.
/** This function MUST be called before the program terminates, otherwise
* we could get into fights with pool allocators used by ScriptValues */
......
......@@ -9,10 +9,11 @@
#include "reader.hpp"
#include <util/vector2d.hpp>
#include <util/error.hpp>
#include <util/io/package_manager.hpp>
// ----------------------------------------------------------------------------- : Reader
Reader::Reader(const InputStreamP& input, String filename)
Reader::Reader(const InputStreamP& input, const String& filename)
: input(input), filename(filename), line_number(0)
, indent(0), expected_indent(0), just_opened(false)
, stream(*input)
......@@ -20,6 +21,16 @@ Reader::Reader(const InputStreamP& input, String filename)
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) {
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));
}
void Reader::unknownKey() {
warning(_("Unexpected key: '") + key + _("'"));
do {
moveNext();
} while (indent > expected_indent);
}
// ----------------------------------------------------------------------------- : Handling basic types
template <> void Reader::handle(String& s) {
......
......@@ -33,10 +33,11 @@ class Reader {
/// Construct a reader that reads from the given input stream
/** 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
Reader(String filename);
/** Used for "include file" keys. */
Reader(const String& filename);
/// Tell the reflection code we are reading
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
void readLine();
/// Issue a warning: "Unexpected key: $key"
void unexpected();
/// No line was read, because nothing mathes the current key
/** 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
......@@ -150,13 +161,7 @@ void Reader::handle(map<K,V>& map) {
while (indent >= expected_indent) { \
UInt l = line_number; \
object.reflect(*this); \
if (l == line_number) { \
/* warning: unexpected key */ \
warning(_("Unexpected key: '") + key + _("'")); \
do { \
moveNext(); \
} while (indent > expected_indent); \
} \
if (l == line_number) unknownKey(object); \
} \
} \
void Cls::reflect(Reader& reader) { \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment