Commit f8142bdd authored by twanvl's avatar twanvl

Implemented the context management part of the ScriptManager

parent 771029fe
......@@ -71,7 +71,6 @@ shared_ptr<Field> read_new<Field>(Reader& reader) {
}
}
// ----------------------------------------------------------------------------- : Style
Style::Style(const FieldP& field)
......@@ -100,6 +99,14 @@ template <> StyleP read_new<Style>(Reader&) {
throw InternalError(_("IndexMap contains nullptr StyleP the application should have crashed already"));
}
void Style::initDependencies(Context& ctx, const Dependency& dep) const {
left .initDependencies(ctx,dep);
top .initDependencies(ctx,dep);
width .initDependencies(ctx,dep);
height .initDependencies(ctx,dep);
visible.initDependencies(ctx,dep);
}
// ----------------------------------------------------------------------------- : Value
Value::~Value() {}
......
......@@ -13,10 +13,13 @@
#include <util/reflect.hpp>
#include <util/alignment.hpp>
#include <script/scriptable.hpp>
#include <script/dependency.hpp>
DECLARE_POINTER_TYPE(Field);
DECLARE_POINTER_TYPE(Style);
DECLARE_POINTER_TYPE(Value);
class Context;
class Dependency;
// for DataViewer/editor
class DataViewer; class DataEditor;
......@@ -44,7 +47,7 @@ class Field {
String card_list_name; ///< Alternate name to use in card list.
Alignment card_list_align; ///< Alignment of the card list colummn.
int tab_index; ///< Tab index in editor
// vector<Dependency> dependentScripts; // scripts that depend on values of this field
vector<Dependency> dependent_scripts; ///< Scripts that depend on values of this field
/// Creates a new Value corresponding to this Field
/** thisP is a smart pointer to this */
......@@ -55,6 +58,9 @@ class Field {
/// Type of this field
virtual String typeName() const = 0;
/// Add the given dependency to the dependet_scripts list for the variables this field depends on
virtual void initDependencies(Context&, const Dependency&) const {}
private:
DECLARE_REFLECTION_VIRTUAL();
};
......@@ -90,6 +96,9 @@ class Style {
/** thisP is a smart pointer to this */
virtual ValueEditorP makeEditor(DataEditor& parent, const StyleP& thisP) = 0;
/// Add the given dependency to the dependet_scripts list for the variables this style depends on
virtual void initDependencies(Context&, const Dependency&) const;
private:
DECLARE_REFLECTION_VIRTUAL();
};
......
......@@ -16,6 +16,10 @@
IMPLEMENT_DYNAMIC_ARG(Game*, game_for_reading, nullptr);
Game::Game()
: dependencies_initialized(false)
{}
GameP Game::byName(const String& name) {
return packages.open<Game>(name + _(".mse-game"));
}
......@@ -48,7 +52,7 @@ IMPLEMENT_REFLECTION(Game) {
// REFLECT(word_lists);
}
void Game::validate() {
void Game::validate(Version) {
// a default for the full name
if (full_name.empty()) full_name = name();
}
......
......@@ -12,6 +12,7 @@
#include <util/prec.hpp>
#include <util/io/package.hpp>
#include <script/scriptable.hpp>
#include <script/dependency.hpp>
#include <util/dynamic_arg.hpp>
DECLARE_POINTER_TYPE(Field);
......@@ -25,12 +26,18 @@ DECLARE_DYNAMIC_ARG(Game*, game_for_reading);
/// A description of a card game
class Game : public Packaged {
public:
Game();
String full_name; ///< Name of this game, for menus etc.
String icon_filename; ///< Filename of icon to use in NewWindow
OptionalScript init_script; ///< Script of variables available to other scripts in this game
vector<FieldP> set_fields; ///< Fields for set information
vector<FieldP> card_fields; ///< Fields on each card
vector<Dependency> dependent_scripts_cards; ///< scripts that depend on the card list
vector<Dependency> dependent_scripts_keywords; ///< scripts that depend on the keywords
bool dependencies_initialized; ///< are the script dependencies comming from this game all initialized?
/// Loads the game with a particular name, for example "magic"
static GameP byName(const String& name);
......@@ -43,7 +50,7 @@ class Game : public Packaged {
virtual InputStreamP openIconFile();
protected:
void validate();
virtual void validate(Version);
DECLARE_REFLECTION();
};
......
......@@ -11,32 +11,78 @@
#include <data/stylesheet.hpp>
#include <data/card.hpp>
#include <data/field.hpp>
#include <data/field/text.hpp> // for 0.2.7 fix
#include <script/value.hpp>
#include <script/script_manager.hpp>
DECLARE_TYPEOF_COLLECTION(CardP);
typedef IndexMap<FieldP,ValueP> IndexMap_FieldP_ValueP;
DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_ValueP);
// ----------------------------------------------------------------------------- : Set
Set::Set() {}
Set::Set()
: script_manager(new ScriptManager(*this))
{}
Set::Set(const GameP& game)
: game(game)
, script_manager(new ScriptManager(*this))
{}
Set::Set(const StyleSheetP& stylesheet)
: stylesheet(stylesheet)
, game(stylesheet->game)
, script_manager(new ScriptManager(*this))
{}
Set::~Set() {}
Context& Set::getContext() {
throw "TODO";
return script_manager->getContext(stylesheet);
}
Context& Set::getContext(const Card& card) {
return script_manager->getContext(card.stylesheet ? card.stylesheet : stylesheet);
}
String Set::typeName() const { return _("set"); }
void Set::validate() {
// fix values for versions < 0.2.7
void fix_value_207(const ValueP& value) {
if (TextValue* v = dynamic_cast<TextValue*>(value.get())) {
// text value -> fix it
// v->value.assign( // don't change defaultness
// fix_old_tags(v->value); // remove tags
// );
}
}
void Set::validate(Version file_app_version) {
// are the
if (!game) {
throw Error(_("No game specified for the set"));
}
if (!stylesheet) {
// TODO : Allow user to select a different style
throw Error(_("No stylesheet specified for the set"));
}
if (stylesheet->game != game) {
throw Error(_("stylesheet and set don't refer to the same game, this is an error in the stylesheet file"));
}
// This is our chance to fix version incompatabilities
if (file_app_version < 207) {
// Since 0.2.7 we use </tag> style close tags, in older versions it was </>
// Walk over all fields and fix...
FOR_EACH(c, cards) {
FOR_EACH(v, c->data) fix_value_207(v);
}
FOR_EACH(v, data) fix_value_207(v);
/* FOR_EACH(s, styleData) {
FOR_EACH(v, s.second->data) fix_value_207(v);
}
*/ }
}
IMPLEMENT_REFLECTION(Set) {
......
......@@ -55,9 +55,13 @@ class Set : public Packaged {
/** Should only be used from the main thread! */
Context& getContext();
/// A context for performing scripts on a particular card
/** Should only be used from the main thread! */
Context& getContext(const Card& card);
protected:
virtual String typeName() const;
virtual void validate();
virtual void validate(Version);
DECLARE_REFLECTION();
private:
......
......@@ -11,11 +11,14 @@
#include <data/field.hpp>
#include <util/io/package_manager.hpp>
DECLARE_TYPEOF_COLLECTION(StyleSheet*);
// ----------------------------------------------------------------------------- : StyleSheet
StyleSheet::StyleSheet()
: card_width(100), card_height(100)
, card_dpi(96), card_background(*wxWHITE)
, dependencies_initialized(false)
{}
StyleSheetP StyleSheet::byGameAndName(const Game& game, const String& name) {
......@@ -63,6 +66,11 @@ IMPLEMENT_REFLECTION(StyleSheet) {
// io(_("extra style"), extraInfoStyle);
}
void StyleSheet::validate(Version) {
// a default for the full name
if (full_name.empty()) full_name = name();
}
// special behaviour of reading/writing StyleSheetPs: only read/write the name
......
......@@ -40,18 +40,21 @@ class StyleSheet : public Packaged {
/** The indices should correspond to the set_fields in the Game */
IndexMap<FieldP, StyleP> set_info_style;
static String typeNameStatic();
virtual String typeName() const;
virtual String fullName() const;
virtual InputStreamP openIconFile();
bool dependencies_initialized; ///< are the script dependencies comming from this stylesheet all initialized?
/// Load a StyleSheet, given a Game and the name of the StyleSheet
static StyleSheetP byGameAndName(const Game& game, const String& name);
/// name of the package without the game name
String styleName();
static String typeNameStatic();
virtual String typeName() const;
virtual String fullName() const;
virtual InputStreamP openIconFile();
protected:
virtual void validate(Version);
private:
DECLARE_REFLECTION();
};
......
......@@ -30,18 +30,18 @@ void resample_pass(const Image& img_in, Image& img_out, int offset_in, int offse
int lines, int line_delta_in, int line_delta_out)
{
bool alpha = img_in.HasAlpha();
if (alpha) img_out.InitAlpha();
if (alpha && !img_out.HasAlpha()) img_out.InitAlpha();
int out_fact = (length_out << shift) / length_in; // how much to output for 256 input = 1 pixel
int out_rest = (length_out << shift) % length_in;
// for each line
for (int l = 0 ; l < lines ; ++l) {
Byte* in = img_in .GetData() + 3 * (offset_in + line_delta_in);
Byte* out = img_out.GetData() + 3 * (offset_out + line_delta_out);
Byte* in = img_in .GetData() + 3 * (offset_in + l * line_delta_in);
Byte* out = img_out.GetData() + 3 * (offset_out + l * line_delta_out);
UInt in_rem = out_fact + out_rest; // remaining to input from the current input pixel
if (alpha) {
Byte* in_a = img_in .GetAlpha() + (offset_in + line_delta_in);
Byte* out_a = img_out.GetAlpha() + (offset_out + line_delta_out);
Byte* in_a = img_in .GetAlpha() + (offset_in + l * line_delta_in);
Byte* out_a = img_out.GetAlpha() + (offset_out + l * line_delta_out);
for (int x = 0 ; x < length_out ; ++x) {
UInt out_rem = 1 << shift;
......
......@@ -1128,6 +1128,9 @@
<File
RelativePath=".\script\dependency.cpp">
</File>
<File
RelativePath=".\script\dependency.hpp">
</File>
<File
RelativePath=".\script\image.cpp">
<FileConfiguration
......
......@@ -73,11 +73,12 @@ bool ValueViewer::nativeLook() const {
return ValueViewerP(new Type##ValueViewer(parent, static_pointer_cast<Type##Style>(thisP))); \
}
ValueViewerP ChoiceStyle ::makeViewer(DataViewer& parent, const StyleP& thisP) { return ValueViewerP(); }
//ValueViewerP ChoiceStyle ::makeViewer(DataViewer& parent, const StyleP& thisP) { return ValueViewerP(); }
ValueViewerP BooleanStyle ::makeViewer(DataViewer& parent, const StyleP& thisP) { return ValueViewerP(); }
ValueViewerP MultipleChoiceStyle::makeViewer(DataViewer& parent, const StyleP& thisP) { return ValueViewerP(); }
//ValueViewerP ColorStyle ::makeViewer(DataViewer& parent, const StyleP& thisP) { return ValueViewerP(); }
//ValueViewerP ImageStyle ::makeViewer(DataViewer& parent, const StyleP& thisP) { return ValueViewerP(); }
IMPLEMENT_MAKE_VIEWER(Choice);
IMPLEMENT_MAKE_VIEWER(Color);
IMPLEMENT_MAKE_VIEWER(Image);
ValueViewerP SymbolStyle ::makeViewer(DataViewer& parent, const StyleP& thisP) { return ValueViewerP(); }
......
......@@ -13,6 +13,9 @@
DECLARE_TYPEOF_COLLECTION(ScriptValueP);
DECLARE_TYPEOF_COLLECTION(Context::Binding);
// NOTE: dependency.cpp has nothing to do with dependency.hpp, the latter defines the dependency
// type, which is used here as an abstract type. The header for this source file is context.hpp
// ----------------------------------------------------------------------------- : Dummy values
// A dummy type used during dependency analysis,
......
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
#ifndef HEADER_SCRIPT_DEPENDENCY
#define HEADER_SCRIPT_DEPENDENCY
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
// ----------------------------------------------------------------------------- : Dependency
/// Types of dependencies
enum DependencyType
{ DEP_CARD_FIELD ///< dependency of a script in a "card" field
, DEP_CARDS_FIELD ///< dependency of a script in a "card" field for all cards
, DEP_SET_FIELD ///< dependency of a script in a "set" field
, DEP_STYLE ///< dependency of a script in a "style" property, data gives the stylesheet
, DEP_CARD_COPY_DEP ///< copy the dependencies from a card field
, DEP_SET_COPY_DEP ///< copy the dependencies from a set field
};
/// A 'pointer' to some script that depends on another script
class Dependency {
public:
inline Dependency(DependencyType type, size_t index, void* data = nullptr)
: type(type), index(index), data(data)
{}
DependencyType type : 5; ///< Type of the dependent script
size_t index : 27; ///< index into an IndexMap
void* data; ///< Extra pointer data
};
// ----------------------------------------------------------------------------- : EOF
#endif
......@@ -7,5 +7,98 @@
// ----------------------------------------------------------------------------- : Includes
#include <script/script_manager.hpp>
#include <data/set.hpp>
#include <data/stylesheet.hpp>
#include <data/game.hpp>
#include <data/field.hpp>
#include <util/error.hpp>
// ----------------------------------------------------------------------------- :
typedef map<const StyleSheet*,Context*> Contexts;
typedef IndexMap<FieldP,StyleP> IndexMap_FieldP_StyleP;
DECLARE_TYPEOF(Contexts);
DECLARE_TYPEOF_COLLECTION(FieldP);
DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_StyleP);
// ----------------------------------------------------------------------------- : ScriptManager : initialization
ScriptManager::ScriptManager(Set& set)
: set(set)
{}
ScriptManager::~ScriptManager() {
set.actions.removeListener(this);
// destroy context
FOR_EACH(sc, contexts) {
delete sc.second;
}
// add as an action listener for the set, so we receive actions
set.actions.addListener(this);
}
Context& ScriptManager::getContext(const StyleSheetP& stylesheet) {
assert(wxThread::IsMain()); // only use our contexts from the main thread
Contexts::iterator it = contexts.find(stylesheet.get());
if (it != contexts.end()) {
return *it->second; // we already have a context
} else {
// create a new context
Context* ctx = new Context();
contexts.insert(make_pair(stylesheet.get(), ctx));
// variables
// NOTE: do not use a smart pointer for the pointer to the set, because the set owns this
// which would lead to a reference cycle.
ctx->setVariable(_("set"), new_intrusive1<ScriptObject<Set*> >(&set));
ctx->setVariable(_("game"), toScript(set.game));
ctx->setVariable(_("stylesheet"), toScript(stylesheet));
//ctx->style->object = style;
//ctx->setVariable(_("styling"), toScript(set->extraStyleData(style)));
try {
// perform init scripts
set.game ->init_script.invoke(*ctx);
stylesheet->init_script.invoke(*ctx);
// find script dependencies
initDependencies(*ctx, *set.game);
initDependencies(*ctx, *stylesheet);
// apply scripts to everything
updateAll();
} catch (Error e) {
handle_error(e, false, false);
}
// initialize dependencies
return *ctx;
}
}
void ScriptManager::initDependencies(Context& ctx, Game& game) {
if (game.dependencies_initialized) return;
game.dependencies_initialized = true;
// find dependencies of card fields
FOR_EACH(f, game.card_fields) {
f->initDependencies(ctx, Dependency(DEP_CARD_FIELD, f->index));
}
// find dependencies of set fields
FOR_EACH(f, game.set_fields) {
f->initDependencies(ctx, Dependency(DEP_SET_FIELD, f->index));
}
}
void ScriptManager::initDependencies(Context& ctx, StyleSheet& stylesheet) {
if (stylesheet.dependencies_initialized) return;
stylesheet.dependencies_initialized = true;
// find dependencies of choice images and other style stuff
FOR_EACH(s, stylesheet.card_style) {
s->initDependencies(ctx, Dependency(DEP_STYLE, s->fieldP->index, &stylesheet));
}
}
// ----------------------------------------------------------------------------- : ScriptManager : dependency handling
void ScriptManager::onAction(const Action& action, bool undone) {
// TODO
}
void ScriptManager::updateAll() {
// TODO
}
\ No newline at end of file
......@@ -10,29 +10,15 @@
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/action_stack.hpp>
#include <script/context.hpp>
#include <data/set.hpp>
#include <script/dependency.hpp>
// ----------------------------------------------------------------------------- : Dependency
/// Types of dependencies
enum DependencyType
{ DEP_CARD_FIELD ///< dependency of a script in a "card" field
, DEP_CARDS_FIELD ///< dependency of a script in a "card" field for all cards
, DEP_SET_INFO_FIELD ///< dependency of a script in a "set" field
, DEP_STYLESHEET_FIELD ///< dependency of a script in a "style" property
, DEP_CARD_COPY_DEP ///< copy the dependencies from a card field
, DEP_SET_COPY_DEP ///< copy the dependencies from a set field
, DEP_CHOICE_IMAGE ///< dependency of a generated choice image, index2 gives the index of the choice image
};
/// A 'pointer' to some script that depends on another script
class Dependency {
public:
DependencyType type : 4; ///< Type of the dependent script
UInt index2 : 10; ///< A second index, used for some types
size_t index; ///< index into an IndexMap
};
class Set;
class Value;
DECLARE_POINTER_TYPE(Game);
DECLARE_POINTER_TYPE(StyleSheet);
DECLARE_POINTER_TYPE(Card);
// ----------------------------------------------------------------------------- : Dependencies of data type members
......@@ -50,14 +36,17 @@ class Dependency {
class ScriptManager : public ActionListener {
public:
ScriptManager(Set& set);
~ScriptManager();
/// Get a context to use for the set, for a given stylesheet
Context& getContext(const StyleSheetP& s);
private:
Context context; ///< Context for evaluating scripts
Set& set; ///< Set for which we are managing scripts
map<const StyleSheet*,Context*> contexts; ///< Context for evaluating scripts that use a given stylesheet
void initScriptStuff();
void initDependencies();
void initDependencies(const StyleSheetP&);
void initContext(const StyleSheetP&);
void initDependencies(Context&, Game&);
void initDependencies(Context&, StyleSheet&);
// Update all styles for a particular card
void updateStyles(const CardP& card);
......
......@@ -40,6 +40,12 @@ void OptionalScript::parse(Reader& reader) {
}
}
void OptionalScript::initDependencies(Context& ctx, const Dependency& dep) const {
if (script) {
ctx.dependencies(dep, *script);
}
}
// custom reflection, different for each type
......
......@@ -55,7 +55,10 @@ class OptionalScript {
}
return false;
}
/// Initialize things this script depends on by adding dep to their list of dependent scripts
void initDependencies(Context&, const Dependency& dep) const;
private:
ScriptP script; ///< The script, may be null if there is no script
String unparsed; ///< Unparsed script, for writing back to a file
......@@ -83,6 +86,10 @@ class Scriptable {
return script.invokeOn(ctx, value);
}
inline void initDependencies(Context& ctx, const Dependency& dep) const {
script.initDependencies(ctx, dep);
}
private:
T value; ///< The actual value
OptionalScript script; ///< The optional script
......
......@@ -214,7 +214,7 @@ class ScriptMap : public ScriptValue {
template <typename T>
class ScriptObject : public ScriptValue {
public:
inline ScriptObject(const shared_ptr<T>& v) : value(v) {}
inline ScriptObject(const T& v) : value(v) {}
virtual ScriptType type() const { return SCRIPT_OBJECT; }
virtual String typeName() const { return _("object"); }
virtual ScriptValueP getMember(const String& name) const {
......@@ -224,7 +224,7 @@ class ScriptObject : public ScriptValue {
else throw ScriptError(_("Object has no member '") + name + _("'"));
}
private:
shared_ptr<T> value; ///< The object
T value; ///< The object
};
// ----------------------------------------------------------------------------- : Creating
......@@ -242,7 +242,7 @@ inline ScriptValueP toScript(const map<K,V>* v) { return new_intrusive1<Scr
template <typename K, typename V>
inline ScriptValueP toScript(const IndexMap<K,V>* v) { return new_intrusive1<ScriptMap<IndexMap<K,V> > >(v); }
template <typename T>
inline ScriptValueP toScript(const shared_ptr<T>& v) { return new_intrusive1<ScriptObject<T> >(v); }
inline ScriptValueP toScript(const shared_ptr<T>& v) { return new_intrusive1<ScriptObject<shared_ptr<T> > >(v); }
// ----------------------------------------------------------------------------- : Buildin functions
......
......@@ -388,6 +388,7 @@ void Packaged::open(const String& package) {
try {
reader.handleAppVersion();
reader.handle(*this);
validate(reader.file_app_version);
} catch (const ParseError& err) {
throw FileParseError(err.what(), absoluteFilename() + _("/") + typeName()); // more detailed message
}
......
......@@ -179,7 +179,7 @@ class Packaged : public Package {
/// filename of the data file, and extension of the package file
virtual String typeName() const = 0;
/// Can be overloaded to do validation after loading
virtual void validate() {}
virtual void validate(Version file_app_version) {}
DECLARE_REFLECTION_VIRTUAL();
};
......
......@@ -31,18 +31,18 @@ Reader::Reader(const String& filename)
}
void Reader::addAlias(Version end_version, const Char* a, const Char* b) {
if (app_version < end_version) {
if (file_app_version < end_version) {
aliasses[a] = b;
}
}
void Reader::handleAppVersion() {
if (enterBlock(_("mse_version"))) {
handle(app_version);
if (::app_version < app_version) {
handle(file_app_version);
if (app_version < file_app_version) {
wxMessageBox(
filename + _("\n")
_("This file is made with a newer version of Magic Set Editor (")+ app_version.toString() +_(").\n")
_("This file is made with a newer version of Magic Set Editor (")+ file_app_version.toString() +_(").\n")
_("When you open it, some aspects of the file may be lost.\n")
_("It is recommended that you upgrade to the latest version.\n")
_("Visit http:://magicseteditor.sourceforge.net/"), _("Warning"), wxOK | wxICON_EXCLAMATION);
......
......@@ -48,7 +48,7 @@ class Reader {
inline bool reading() const { return true; }
/// Is the thing currently being read 'complex', i.e. does it have children
inline bool isComplex() const { return value.empty(); }
/// Add a as an alias for b, all keys a will be replaced with b, only if app_version < end_version
/// Add a as an alias for b, all keys a will be replaced with b, only if file_app_version < end_version
void addAlias(Version end_version, const Char* a, const Char* b);
/// Read and check the application version
......@@ -90,7 +90,7 @@ class Reader {
// --------------------------------------------------- : Data
/// App version this file was made with
Version app_version;
Version file_app_version;
private:
/// The line we read
String line;
......
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