Implemented the context management part of the ScriptManager

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@61 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-10-30 21:39:58 +00:00
parent 0a3a089c98
commit 0caaf01a78
22 changed files with 288 additions and 58 deletions
+3
View File
@@ -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,
+39
View File
@@ -0,0 +1,39 @@
//+----------------------------------------------------------------------------+
//| 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
+94 -1
View File
@@ -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
}
+15 -26
View 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);
+6
View File
@@ -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
+8 -1
View File
@@ -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
+3 -3
View File
@@ -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