|
@@ -20,6 +20,10 @@
|
|
|
// IN THE SOFTWARE.
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
+//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
|
|
|
+// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
|
|
|
+// Copyright (C) 2015 Faust Logic, Inc.
|
|
|
+//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
|
|
|
#include "platform/platform.h"
|
|
|
#include "console/simDatablock.h"
|
|
|
|
|
@@ -29,6 +33,8 @@
|
|
|
#include "T3D/gameBase/gameConnectionEvents.h"
|
|
|
#include "T3D/gameBase/gameConnection.h"
|
|
|
|
|
|
+#include "core/stream/bitStream.h"
|
|
|
+#include "console/compiler.h"
|
|
|
|
|
|
IMPLEMENT_CO_DATABLOCK_V1(SimDataBlock);
|
|
|
SimObjectId SimDataBlock::sNextObjectId = DataBlockObjectIdFirst;
|
|
@@ -52,6 +58,260 @@ SimDataBlock::SimDataBlock()
|
|
|
setModDynamicFields(true);
|
|
|
setModStaticFields(true);
|
|
|
}
|
|
|
+// this implements a simple structure for managing substitution statements.
|
|
|
+
|
|
|
+SimDataBlock::SubstitutionStatement::SubstitutionStatement(StringTableEntry slot, S32 idx, const char* value)
|
|
|
+{
|
|
|
+ this->slot = slot;
|
|
|
+ this->idx = idx;
|
|
|
+ this->value = dStrdup(value);
|
|
|
+}
|
|
|
+
|
|
|
+SimDataBlock::SubstitutionStatement::~SubstitutionStatement()
|
|
|
+{
|
|
|
+ dFree(value);
|
|
|
+}
|
|
|
+
|
|
|
+void SimDataBlock::SubstitutionStatement::replaceValue(const char* value)
|
|
|
+{
|
|
|
+ dFree(this->value);
|
|
|
+ this->value = dStrdup(value);
|
|
|
+}
|
|
|
+
|
|
|
+// this is the copy-constructor for creating temp-clones.
|
|
|
+SimDataBlock::SimDataBlock(const SimDataBlock& other, bool temp_clone) : SimObject(other, temp_clone)
|
|
|
+{
|
|
|
+ modifiedKey = other.modifiedKey;
|
|
|
+}
|
|
|
+
|
|
|
+// a destructor is added to SimDataBlock so that we can delete any substitutions.
|
|
|
+SimDataBlock::~SimDataBlock()
|
|
|
+{
|
|
|
+ clear_substitutions();
|
|
|
+}
|
|
|
+
|
|
|
+void SimDataBlock::clear_substitutions()
|
|
|
+{
|
|
|
+ for (S32 i = 0; i < substitutions.size(); i++)
|
|
|
+ delete substitutions[i];
|
|
|
+ substitutions.clear();
|
|
|
+}
|
|
|
+
|
|
|
+void SimDataBlock::addSubstitution(StringTableEntry slot, S32 idx, const char* subst)
|
|
|
+{
|
|
|
+ AssertFatal(subst != 0 && subst[0] == '$' && subst[1] == '$', "Bad substition statement string added");
|
|
|
+
|
|
|
+ subst += 2;
|
|
|
+ while (dIsspace(*subst))
|
|
|
+ subst++;
|
|
|
+
|
|
|
+ bool empty_subs = (*subst == '\0');
|
|
|
+
|
|
|
+ for (S32 i = 0; i < substitutions.size(); i++)
|
|
|
+ {
|
|
|
+ if (substitutions[i] && substitutions[i]->slot == slot && substitutions[i]->idx == idx)
|
|
|
+ {
|
|
|
+ if (empty_subs)
|
|
|
+ {
|
|
|
+ delete substitutions[i];
|
|
|
+ substitutions[i] = 0;
|
|
|
+ onRemoveSubstitution(slot, idx);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ substitutions[i]->replaceValue(subst);
|
|
|
+ onAddSubstitution(slot, idx, subst);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty_subs)
|
|
|
+ {
|
|
|
+ substitutions.push_back(new SubstitutionStatement(slot, idx, subst));
|
|
|
+ onAddSubstitution(slot, idx, subst);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const char* SimDataBlock::getSubstitution(StringTableEntry slot, S32 idx)
|
|
|
+{
|
|
|
+ for (S32 i = 0; i < substitutions.size(); i++)
|
|
|
+ {
|
|
|
+ if (substitutions[i] && substitutions[i]->slot == slot && substitutions[i]->idx == idx)
|
|
|
+ return substitutions[i]->value;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+bool SimDataBlock::fieldHasSubstitution(StringTableEntry slot)
|
|
|
+{
|
|
|
+ for (S32 i = 0; i < substitutions.size(); i++)
|
|
|
+ if (substitutions[i] && substitutions[i]->slot == slot)
|
|
|
+ return true;
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+void SimDataBlock::printSubstitutions()
|
|
|
+{
|
|
|
+ for (S32 i = 0; i < substitutions.size(); i++)
|
|
|
+ if (substitutions[i])
|
|
|
+ Con::errorf("SubstitutionStatement[%s] = \"%s\" -- %d", substitutions[i]->slot, substitutions[i]->value, i);
|
|
|
+}
|
|
|
+
|
|
|
+void SimDataBlock::copySubstitutionsFrom(SimDataBlock* other)
|
|
|
+{
|
|
|
+ clear_substitutions();
|
|
|
+ if (!other)
|
|
|
+ return;
|
|
|
+
|
|
|
+ for (S32 i = 0; i < other->substitutions.size(); i++)
|
|
|
+ {
|
|
|
+ if (other->substitutions[i])
|
|
|
+ {
|
|
|
+ SubstitutionStatement* subs = other->substitutions[i];
|
|
|
+ substitutions.push_back(new SubstitutionStatement(subs->slot, subs->idx, subs->value));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// This is the method that evaluates any substitution statements on a datablock and does the
|
|
|
+// actual replacement of substituted datablock fields.
|
|
|
+//
|
|
|
+// Much of the work is done by passing the statement to Con::evaluate() but first there are
|
|
|
+// some key operations performed on the statement.
|
|
|
+// -- Instances of "%%" in the statement are replaced with the id of the <obj> object.
|
|
|
+// -- Instances of "##" are replaced with the value of <index>.
|
|
|
+//
|
|
|
+// There are also some return values that get special treatment.
|
|
|
+// -- An empty result will produce a realtime error message.
|
|
|
+// -- A result of "~~" will leave the original field value unchanged.
|
|
|
+// -- A result of "~0" will clear the original field to "" without producing an error message.
|
|
|
+//
|
|
|
+void SimDataBlock::performSubstitutions(SimDataBlock* dblock, const SimObject* obj, S32 index)
|
|
|
+{
|
|
|
+ if (!dblock || !dblock->getClassRep())
|
|
|
+ {
|
|
|
+ // error message
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ char obj_str[32];
|
|
|
+ dStrcpy(obj_str, Con::getIntArg(obj->getId()));
|
|
|
+
|
|
|
+ char index_str[32];
|
|
|
+ dStrcpy(index_str, Con::getIntArg(index));
|
|
|
+
|
|
|
+ for (S32 i = 0; i < substitutions.size(); i++)
|
|
|
+ {
|
|
|
+ if (substitutions[i])
|
|
|
+ {
|
|
|
+ static char buffer[1024];
|
|
|
+ static char* b_oob = &buffer[1024];
|
|
|
+ char* b = buffer;
|
|
|
+
|
|
|
+ // perform special token expansion (%% and ##)
|
|
|
+ const char* v = substitutions[i]->value;
|
|
|
+ while (*v != '\0')
|
|
|
+ {
|
|
|
+ // identify "%%" tokens and replace with <obj> id
|
|
|
+ if (v[0] == '%' && v[1] == '%')
|
|
|
+ {
|
|
|
+ const char* s = obj_str;
|
|
|
+ while (*s != '\0')
|
|
|
+ {
|
|
|
+ b[0] = s[0];
|
|
|
+ b++;
|
|
|
+ s++;
|
|
|
+ }
|
|
|
+ v += 2;
|
|
|
+ }
|
|
|
+ // identify "##" tokens and replace with <index> value
|
|
|
+ else if (v[0] == '#' && v[1] == '#')
|
|
|
+ {
|
|
|
+ const char* s = index_str;
|
|
|
+ while (*s != '\0')
|
|
|
+ {
|
|
|
+ b[0] = s[0];
|
|
|
+ b++;
|
|
|
+ s++;
|
|
|
+ }
|
|
|
+ v += 2;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ b[0] = v[0];
|
|
|
+ b++;
|
|
|
+ v++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ AssertFatal((uintptr_t)b < (uintptr_t)b_oob, "Substitution buffer overflowed");
|
|
|
+
|
|
|
+ b[0] = '\0';
|
|
|
+
|
|
|
+ // perform the statement evaluation
|
|
|
+ Compiler::gSyntaxError = false;
|
|
|
+ //Con::errorf("EVAL [%s]", avar("return %s;", buffer));
|
|
|
+ const char *result = Con::evaluate(avar("return %s;", buffer), false, 0);
|
|
|
+ if (Compiler::gSyntaxError)
|
|
|
+ {
|
|
|
+ Con::errorf("Field Substitution Failed: field=\"%s\" substitution=\"%s\" -- syntax error",
|
|
|
+ substitutions[i]->slot, substitutions[i]->value);
|
|
|
+ Compiler::gSyntaxError = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // output a runtime console error when a substitution produces and empty result.
|
|
|
+ if (result == 0 || result[0] == '\0')
|
|
|
+ {
|
|
|
+ Con::errorf("Field Substitution Failed: field=\"%s\" substitution=\"%s\" -- empty result",
|
|
|
+ substitutions[i]->slot, substitutions[i]->value);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // handle special return values
|
|
|
+ if (result[0] == '~')
|
|
|
+ {
|
|
|
+ // if value is "~~" then keep the existing value
|
|
|
+ if (result[1] == '~' && result[2] == '\0')
|
|
|
+ continue;
|
|
|
+ // if "~0" then clear it
|
|
|
+ if (result[1] == '0' && result[2] == '\0')
|
|
|
+ result = "";
|
|
|
+ }
|
|
|
+
|
|
|
+ const AbstractClassRep::Field* field = dblock->getClassRep()->findField(substitutions[i]->slot);
|
|
|
+ if (!field)
|
|
|
+ {
|
|
|
+ // this should be very unlikely...
|
|
|
+ Con::errorf("Field Substitution Failed: unknown field, \"%s\".", substitutions[i]->slot);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (field->keepClearSubsOnly && result[0] != '\0')
|
|
|
+ {
|
|
|
+ Con::errorf("Field Substitution Failed: field \"%s\" of datablock %s only allows \"$$ ~~\" (keep) and \"$$ ~0\" (clear) field substitutions. [%s]",
|
|
|
+ substitutions[i]->slot, this->getClassName(), this->getName());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // substitute the field value with its replacement
|
|
|
+ Con::setData(field->type, (void*)(((const char*)(dblock)) + field->offset), substitutions[i]->idx, 1, &result, field->table, field->flag);
|
|
|
+
|
|
|
+ //dStrncpy(buffer, result, 255);
|
|
|
+ //Con::errorf("SUBSTITUTION %s.%s[%d] = %s idx=%s", Con::getIntArg(getId()), substitutions[i]->slot, substitutions[i]->idx, buffer, index_str);
|
|
|
+
|
|
|
+ // notify subclasses of a field modification
|
|
|
+ dblock->onStaticModified(substitutions[i]->slot);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // notify subclasses of substitution operation
|
|
|
+ if (substitutions.size() > 0)
|
|
|
+ dblock->onPerformSubstitutions();
|
|
|
+}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
@@ -96,14 +356,37 @@ void SimDataBlock::onStaticModified(const char* slotName, const char* newValue)
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
-void SimDataBlock::packData(BitStream*)
|
|
|
+// packData() and unpackData() do nothing in the stock implementation, but here
|
|
|
+// they've been modified to pack and unpack any substitution statements.
|
|
|
+//
|
|
|
+void SimDataBlock::packData(BitStream* stream)
|
|
|
{
|
|
|
+ for (S32 i = 0; i < substitutions.size(); i++)
|
|
|
+ {
|
|
|
+ if (substitutions[i])
|
|
|
+ {
|
|
|
+ stream->writeFlag(true);
|
|
|
+ stream->writeString(substitutions[i]->slot);
|
|
|
+ stream->write(substitutions[i]->idx);
|
|
|
+ stream->writeString(substitutions[i]->value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ stream->writeFlag(false);
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-
|
|
|
-void SimDataBlock::unpackData(BitStream*)
|
|
|
+void SimDataBlock::unpackData(BitStream* stream)
|
|
|
{
|
|
|
+ clear_substitutions();
|
|
|
+ while(stream->readFlag())
|
|
|
+ {
|
|
|
+ char slotName[256];
|
|
|
+ S32 idx;
|
|
|
+ char value[256];
|
|
|
+ stream->readString(slotName);
|
|
|
+ stream->read(&idx);
|
|
|
+ stream->readString(value);
|
|
|
+ substitutions.push_back(new SubstitutionStatement(StringTable->insert(slotName), idx, value));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|