mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
made dependency analysis work without errors for magic-new (except for a few script functions); implemented the rest of the ScriptManager
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@71 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -25,9 +25,11 @@ class DependencyDummy : public ScriptIterator {
|
||||
virtual ScriptType type() const { return SCRIPT_DUMMY; }
|
||||
virtual String typeName() const { return _("dummy"); }
|
||||
virtual ScriptValueP next() { return ScriptValueP(); }
|
||||
virtual ScriptValueP eval(Context&) const { return dependency_dummy; } // dummy() == dummy
|
||||
virtual ScriptValueP getMember(const String&) const { return dependency_dummy; } // dummy.* = dummy
|
||||
};
|
||||
|
||||
ScriptValueP dependencyDummy(new DependencyDummy);
|
||||
ScriptValueP dependency_dummy(new DependencyDummy);
|
||||
|
||||
ScriptValueP unified(const ScriptValueP& a, const ScriptValueP& b);
|
||||
|
||||
@@ -220,7 +222,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
assert(dynamic_pointer_cast<ScriptIterator>(it)); // top of stack must be an iterator
|
||||
ScriptValueP val = static_pointer_cast<ScriptIterator>(it)->next();
|
||||
if (val) {
|
||||
it = dependencyDummy; // invalidate iterator, so we loop only once
|
||||
it = dependency_dummy; // invalidate iterator, so we loop only once
|
||||
stack.push_back(val);
|
||||
} else {
|
||||
stack.erase(stack.end() - 2); // remove iterator
|
||||
@@ -259,7 +261,9 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
// Get a variable (almost as normal)
|
||||
case I_GET_VAR: {
|
||||
ScriptValueP value = variables[i.data].value;
|
||||
if (!value) value = new_intrusive1<ScriptMissingVariable>(variable_to_string(i.data)); // no errors here
|
||||
if (!value) {
|
||||
value = new_intrusive1<ScriptMissingVariable>(variable_to_string(i.data)); // no errors here
|
||||
}
|
||||
stack.push_back(value);
|
||||
break;
|
||||
}
|
||||
@@ -277,7 +281,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
a = a->makeIterator(); // as normal
|
||||
break;
|
||||
default:
|
||||
a = dependencyDummy;
|
||||
a = dependency_dummy;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -298,7 +302,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
unify(a, b); // may be function composition
|
||||
break;
|
||||
default:
|
||||
a = dependencyDummy;
|
||||
a = dependency_dummy;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -307,7 +311,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
ScriptValueP c = stack.back(); stack.pop_back();
|
||||
ScriptValueP b = stack.back(); stack.pop_back();
|
||||
ScriptValueP& a = stack.back();
|
||||
a = dependencyDummy;
|
||||
a = dependency_dummy;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <util/age.hpp>
|
||||
#include <util/dynamic_arg.hpp>
|
||||
#include <script/scriptable.hpp>
|
||||
#include <gfx/gfx.hpp>
|
||||
#include <util/dynamic_arg.hpp>
|
||||
|
||||
class Package;
|
||||
DECLARE_INTRUSIVE_POINTER_TYPE(ScriptImage);
|
||||
@@ -64,6 +64,10 @@ class ScriptableImage {
|
||||
/// Is the cached image up to date?
|
||||
bool upToDate(Context& ctx, Age age) const;
|
||||
|
||||
inline void initDependencies(Context& ctx, const Dependency& dep) const {
|
||||
script.initDependencies(ctx, dep);
|
||||
}
|
||||
|
||||
private:
|
||||
OptionalScript script; ///< The script, not really optional
|
||||
ScriptImageP cache; ///< The cached image
|
||||
|
||||
@@ -10,14 +10,19 @@
|
||||
#include <data/set.hpp>
|
||||
#include <data/stylesheet.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/card.hpp>
|
||||
#include <data/field.hpp>
|
||||
#include <util/error.hpp>
|
||||
|
||||
typedef map<const StyleSheet*,Context*> Contexts;
|
||||
typedef IndexMap<FieldP,StyleP> IndexMap_FieldP_StyleP;
|
||||
typedef IndexMap<FieldP,ValueP> IndexMap_FieldP_ValueP;
|
||||
DECLARE_TYPEOF(Contexts);
|
||||
DECLARE_TYPEOF_COLLECTION(CardP);
|
||||
DECLARE_TYPEOF_COLLECTION(FieldP);
|
||||
DECLARE_TYPEOF_COLLECTION(Dependency);
|
||||
DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_StyleP);
|
||||
DECLARE_TYPEOF_NO_REV(IndexMap_FieldP_ValueP);
|
||||
|
||||
// initialize functions, from functions.cpp
|
||||
void init_script_functions(Context& ctx);
|
||||
@@ -58,9 +63,9 @@ Context& ScriptManager::getContext(const StyleSheetP& stylesheet) {
|
||||
ctx->setVariable(_("card"), set.cards.empty() ? script_nil : toScript(set.cards.front())); // dummy value
|
||||
//ctx->setVariable(_("styling"), toScript(set->extraStyleData(style)));
|
||||
try {
|
||||
// perform init scripts
|
||||
set.game ->init_script.invoke(*ctx);
|
||||
stylesheet->init_script.invoke(*ctx);
|
||||
// perform init scripts, don't use a scope, variables stay bound in the context
|
||||
set.game ->init_script.invoke(*ctx, false);
|
||||
stylesheet->init_script.invoke(*ctx, false);
|
||||
// find script dependencies
|
||||
initDependencies(*ctx, *set.game);
|
||||
initDependencies(*ctx, *stylesheet);
|
||||
@@ -97,12 +102,140 @@ void ScriptManager::initDependencies(Context& ctx, StyleSheet& stylesheet) {
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : ScriptManager : dependency handling
|
||||
// ----------------------------------------------------------------------------- : ScriptManager : updating
|
||||
|
||||
void ScriptManager::onAction(const Action& action, bool undone) {
|
||||
// TODO
|
||||
// TYPE_CASE(action, ValueAction) {
|
||||
// }
|
||||
// TYPE_CASE(action, CardListAction) {
|
||||
// }
|
||||
}
|
||||
|
||||
void ScriptManager::updateStyles(const CardP& card) {
|
||||
// lastUpdatedCard = card;
|
||||
StyleSheetP stylesheet = set.stylesheetFor(card);
|
||||
Context& ctx = getContext(stylesheet);
|
||||
// update all styles
|
||||
FOR_EACH(s, stylesheet->card_style) {
|
||||
if (s->update(ctx)) {
|
||||
// style has changed, tell listeners
|
||||
// ScriptStyleEvent change(s.get());
|
||||
// set->actions.tellListeners(change);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptManager::updateValue(Value& value, const CardP& card) {
|
||||
Age starting_age; // the start of the update process
|
||||
deque<ToUpdate> to_update;
|
||||
// execute script for initial changed value
|
||||
value.update(getContext(set.stylesheetFor(card)));
|
||||
// update dependent scripts
|
||||
alsoUpdate(to_update, value.fieldP->dependent_scripts, card);
|
||||
updateRecursive(to_update, starting_age);
|
||||
}
|
||||
|
||||
void ScriptManager::updateAll() {
|
||||
// TODO
|
||||
// update set data
|
||||
Context& ctx = getContext(set.stylesheet);
|
||||
FOR_EACH(v, set.data) {
|
||||
v->update(ctx);
|
||||
}
|
||||
// update card data of all cards
|
||||
FOR_EACH(card, set.cards) {
|
||||
Context& ctx = getContext(set.stylesheetFor(card));
|
||||
FOR_EACH(v, card->data) {
|
||||
v->update(ctx);
|
||||
}
|
||||
}
|
||||
// update things that depend on the card list
|
||||
updateAllDependend(set.game->dependent_scripts_cards);
|
||||
}
|
||||
|
||||
void ScriptManager::updateAllDependend(const vector<Dependency>& dependent_scripts) {
|
||||
deque<ToUpdate> to_update;
|
||||
Age starting_age;
|
||||
alsoUpdate(to_update, dependent_scripts, CardP());
|
||||
updateRecursive(to_update, starting_age);
|
||||
}
|
||||
|
||||
void ScriptManager::updateRecursive(deque<ToUpdate>& to_update, Age starting_age) {
|
||||
// set->order_cache.clear(); // clear caches before evaluating a round of scripts
|
||||
while (!to_update.empty()) {
|
||||
updateToUpdate(to_update.front(), to_update, starting_age);
|
||||
to_update.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptManager::updateToUpdate(const ToUpdate& u, deque<ToUpdate>& to_update, Age starting_age) {
|
||||
Age age = u.value->last_script_update;
|
||||
if (starting_age < age) return; // this value was already updated
|
||||
Context& ctx = getContext(set.stylesheetFor(u.card));
|
||||
if (u.value->update(ctx)) {
|
||||
// changed, send event
|
||||
// ScriptValueEvent change(&*u.card, u.value);
|
||||
// set.actions.tellListeners(change);
|
||||
// u.value has changed, also update values with a dependency on u.value
|
||||
alsoUpdate(to_update, u.value->fieldP->dependent_scripts, u.card);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptManager::alsoUpdate(deque<ToUpdate>& to_update, const vector<Dependency>& deps, const CardP& card) {
|
||||
FOR_EACH_CONST(d, deps) {
|
||||
switch (d.type) {
|
||||
case DEP_SET_FIELD: {
|
||||
break;
|
||||
} case DEP_CARD_FIELD: {
|
||||
break;
|
||||
} case DEP_CARDS_FIELD: {
|
||||
break;
|
||||
} case DEP_STYLE: {
|
||||
break;
|
||||
} case DEP_CARD_COPY_DEP: {
|
||||
break;
|
||||
} case DEP_SET_COPY_DEP: {
|
||||
break;
|
||||
} default:
|
||||
assert(false);
|
||||
}
|
||||
/*
|
||||
if (d.type == DependendScript.setField) {
|
||||
// from set data
|
||||
ValueP value = set->data.at(ds.index);
|
||||
toUpdate.push_back(ToUpdate(&*value));
|
||||
} else if (ds.type == DependendScript.cardField) {
|
||||
// from the same card's data
|
||||
assert(card);
|
||||
ValueP value = card->data.at(ds.index);
|
||||
toUpdate.push_back(ToUpdate(&*value, card));
|
||||
} else if (ds.type == DependendScript.cardsField) {
|
||||
// something invalidates a card value for all cards, so all cards need updating
|
||||
FOR_EACH(card, set)->cards {
|
||||
ValueP value = card->data.at(ds.index);
|
||||
toUpdate.push_back(ToUpdate(&*value, card));
|
||||
}
|
||||
} else if (ds.type >= DependendScript.choiceImage) {
|
||||
// a generated image has become invalid, there is not much we can do
|
||||
// because the index is not exact enough, it only gives the field
|
||||
// TODO : Indicate what style
|
||||
//CardStyleP style = set->styleOf(card) // WRONG?
|
||||
CardStyle* style = CardStyle.getByIndex(ds.type - DependendScript.choiceImage);
|
||||
StyleP s = style->cardStyle.at(ds.index);
|
||||
s->invalidate();
|
||||
// something changed, send event
|
||||
ScriptStyleEvent change(&*s);
|
||||
set->actions.tellListeners(change);
|
||||
} else if (ds.type == DependendScript.cardCopyDep) {
|
||||
// propagate dependencies from another field
|
||||
FieldP f = game->cardFields#(ds.index);
|
||||
alsoUpdate(toUpdate, f->dependendScripts, card);
|
||||
} else if (ds.type == DependendScript.setCopyDep) {
|
||||
// propagate dependencies from another field
|
||||
FieldP f = game->setFields#(ds.index);
|
||||
alsoUpdate(toUpdate, f->dependendScripts, card);
|
||||
} else {
|
||||
assert(false); // only setField, cardField and cardsField should be possible
|
||||
}*/
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,10 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <util/action_stack.hpp>
|
||||
#include <util/age.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/dependency.hpp>
|
||||
#include <queue>
|
||||
|
||||
class Set;
|
||||
class Value;
|
||||
@@ -37,10 +39,13 @@ 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);
|
||||
|
||||
// Update all styles for a particular card
|
||||
void updateStyles(const CardP& card);
|
||||
|
||||
private:
|
||||
Set& set; ///< Set for which we are managing scripts
|
||||
map<const StyleSheet*,Context*> contexts; ///< Context for evaluating scripts that use a given stylesheet
|
||||
@@ -48,18 +53,29 @@ class ScriptManager : public ActionListener {
|
||||
void initDependencies(Context&, Game&);
|
||||
void initDependencies(Context&, StyleSheet&);
|
||||
|
||||
// Update all styles for a particular card
|
||||
void updateStyles(const CardP& card);
|
||||
/// Updates scripts, starting at some value
|
||||
/** if the value changes any dependend values are updated as well */
|
||||
void updateValue(Value* value, const CardP& card);
|
||||
void updateValue(Value& value, const CardP& card);
|
||||
/// Update all fields of all cards
|
||||
/** Update all set info fields
|
||||
* Doesn't update styles
|
||||
*/
|
||||
void updateAll();
|
||||
// Update all values with a specific dependency
|
||||
void updateAllDependend(const vector<Dependency>& dependendScripts);
|
||||
void updateAllDependend(const vector<Dependency>& dependent_scripts);
|
||||
|
||||
// Something that needs to be updated
|
||||
struct ToUpdate {
|
||||
Value* value; // value to update
|
||||
CardP card; // card the value is in, or 0 if it is not a card field
|
||||
};
|
||||
/// Update all things in to_update, and things that depent on them, etc.
|
||||
/** Only update things that are older than starting_age. */
|
||||
void updateRecursive(deque<ToUpdate>& to_update, Age starting_age);
|
||||
/// Update a value given by a ToUpdate object, and add things depending on it to to_update
|
||||
void updateToUpdate(const ToUpdate& u, deque<ToUpdate>& to_update, Age starting_age);
|
||||
/// Schedule all things in deps to be updated by adding them to to_update
|
||||
void alsoUpdate(deque<ToUpdate>& to_update, const vector<Dependency>& deps, const CardP& card);
|
||||
|
||||
protected:
|
||||
/// Respond to actions by updating scripts
|
||||
|
||||
@@ -24,9 +24,9 @@ void store(const ScriptValueP& val, Defaultable<String>& var) { var.assign(*val)
|
||||
|
||||
OptionalScript::~OptionalScript() {}
|
||||
|
||||
ScriptValueP OptionalScript::invoke(Context& ctx) const {
|
||||
ScriptValueP OptionalScript::invoke(Context& ctx, bool open_scope) const {
|
||||
if (script) {
|
||||
return ctx.eval(*script);
|
||||
return ctx.eval(*script, open_scope);
|
||||
} else {
|
||||
return script_nil;
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@
|
||||
#include <util/reflect.hpp>
|
||||
#include <util/defaultable.hpp>
|
||||
#include <script/script.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/parser.hpp>
|
||||
|
||||
DECLARE_INTRUSIVE_POINTER_TYPE(Script);
|
||||
class Context;
|
||||
|
||||
// ----------------------------------------------------------------------------- : Store
|
||||
|
||||
@@ -37,7 +37,7 @@ class OptionalScript {
|
||||
inline operator bool() const { return !!script; }
|
||||
|
||||
/// Invoke the script, return the result, or script_nil if there is no script
|
||||
ScriptValueP invoke(Context& ctx) const;
|
||||
ScriptValueP invoke(Context& ctx, bool open_scope = true) const;
|
||||
|
||||
/// Invoke the script on a value
|
||||
/** Assigns the result to value if it has changed.
|
||||
@@ -47,7 +47,8 @@ class OptionalScript {
|
||||
bool invokeOn(Context& ctx, T& value) const {
|
||||
if (script) {
|
||||
T new_value;
|
||||
store(new_value, script->invoke(ctx));
|
||||
ctx.setVariable(_("value"), toScript(value));
|
||||
store(ctx.eval(*script), new_value);
|
||||
if (value != new_value) {
|
||||
value = new_value;
|
||||
return true;
|
||||
@@ -55,6 +56,16 @@ class OptionalScript {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// Invoke the script on a value if it is in the default state
|
||||
template <typename T>
|
||||
bool invokeOnDefault(Context& ctx, Defaultable<T>& value) const {
|
||||
if (value.isDefault() && invokeOn(ctx, value)) {
|
||||
value.setDefault(); // restore defaultness
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize things this script depends on by adding dep to their list of dependent scripts
|
||||
void initDependencies(Context&, const Dependency& dep) const;
|
||||
@@ -68,6 +79,9 @@ class OptionalScript {
|
||||
template <typename T> friend class Scriptable;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline ScriptValueP toScript(const Defaultable<T>& v) { return toScript(v.get()); }
|
||||
|
||||
// ----------------------------------------------------------------------------- : Scriptable
|
||||
|
||||
/// A script that defines a calculation to find a value
|
||||
|
||||
+10
-12
@@ -13,20 +13,18 @@
|
||||
// ----------------------------------------------------------------------------- : ScriptValue
|
||||
// Base cases
|
||||
|
||||
ScriptValue::operator String() const { return _("[[") + typeName() + _("]]"); }
|
||||
ScriptValue::operator int() const { throw ScriptError(_("Can't convert from ")+typeName()+_(" to integer number")); }
|
||||
ScriptValue::operator double() const { throw ScriptError(_("Can't convert from ")+typeName()+_(" to real number" )); }
|
||||
ScriptValue::operator Color() const { throw ScriptError(_("Can't convert from ")+typeName()+_(" to color" )); }
|
||||
ScriptValueP ScriptValue::eval(Context&) const
|
||||
{ throw ScriptError(_("Can't convert from ")+typeName()+_(" to function" )); }
|
||||
ScriptValueP ScriptValue::getMember(const String& name) const
|
||||
{ throw ScriptError(typeName() + _(" has no member '") + name + _("'")); }
|
||||
ScriptValueP ScriptValue::next() { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
|
||||
ScriptValueP ScriptValue::makeIterator() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
int ScriptValue::itemCount() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
ScriptValue::operator String() const { return _("[[") + typeName() + _("]]"); }
|
||||
ScriptValue::operator int() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to integer number")); }
|
||||
ScriptValue::operator double() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to real number" )); }
|
||||
ScriptValue::operator Color() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to color" )); }
|
||||
ScriptValueP ScriptValue::eval(Context&) const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to function" )); }
|
||||
ScriptValueP ScriptValue::getMember(const String& name) const { throw ScriptError(typeName() + _(" has no member '") + name + _("'")); }
|
||||
ScriptValueP ScriptValue::next() { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
|
||||
ScriptValueP ScriptValue::makeIterator() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
int ScriptValue::itemCount() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
|
||||
void ScriptValue::signalDependent(Context&, const Dependency&, const String& name) {}
|
||||
ScriptValueP ScriptValue::dependencies( Context&, const Dependency&) const { return script_nil; }
|
||||
ScriptValueP ScriptValue::dependencies(Context&, const Dependency&) const { return dependency_dummy; }
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------- : Iterators
|
||||
|
||||
@@ -114,7 +114,7 @@ class ScriptValue {
|
||||
extern ScriptValueP script_nil; ///< The preallocated nil value
|
||||
extern ScriptValueP script_true; ///< The preallocated true value
|
||||
extern ScriptValueP script_false; ///< The preallocated false value
|
||||
|
||||
extern ScriptValueP dependency_dummy; ///< Dummy value used during dependency analysis
|
||||
|
||||
// ----------------------------------------------------------------------------- : Iterators
|
||||
|
||||
|
||||
Reference in New Issue
Block a user