Torque3D/Engine/source/console/simFieldDictionary.cpp

438 lines
12 KiB
C++
Raw Normal View History

2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
// Copyright (C) 2015 Faust Logic, Inc.
//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
2012-09-19 15:15:01 +00:00
#include "platform/platform.h"
#include "console/simFieldDictionary.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "core/frameAllocator.h"
2017-11-06 04:33:32 +00:00
U32 SimFieldDictionary::getHashValue(StringTableEntry slotName)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
return HashPointer(slotName) % HashTableSize;
2012-09-19 15:15:01 +00:00
}
2017-11-06 04:33:32 +00:00
U32 SimFieldDictionary::getHashValue(const String& fieldName)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
return getHashValue(StringTable->insert(fieldName));
2012-09-19 15:15:01 +00:00
}
2017-11-06 04:33:32 +00:00
SimFieldDictionary::Entry *SimFieldDictionary::addEntry(U32 bucket, StringTableEntry slotName, ConsoleBaseType* type, char* value)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
Entry ret;
ret.slotName = slotName;
ret.type = type;
ret.value = value;
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
mNumFields++;
mVersion++;
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
mHashTable[bucket].push_back(std::move(ret));
return &mHashTable[bucket].back();
2012-09-19 15:15:01 +00:00
}
void SimFieldDictionary::freeEntry(SimFieldDictionary::Entry *ent)
{
2017-11-06 04:33:32 +00:00
auto &vec = mHashTable[getHashValue(ent->slotName)];
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
// Find the slot.
auto iter = std::find_if(vec.begin(), vec.end(), [&](const Entry &ref) -> bool {
return ref.slotName == ent->slotName;
});
if (iter != vec.end())
{
vec.erase(iter);
mNumFields--;
}
2012-09-19 15:15:01 +00:00
}
SimFieldDictionary::SimFieldDictionary()
2017-11-06 04:33:32 +00:00
: mNumFields(0),
mVersion(0)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
2012-09-19 15:15:01 +00:00
}
SimFieldDictionary::~SimFieldDictionary()
{
}
void SimFieldDictionary::setFieldType(StringTableEntry slotName, const char *typeString)
{
2017-11-06 04:33:32 +00:00
ConsoleBaseType *cbt = ConsoleBaseType::getTypeByName(typeString);
2012-09-19 15:15:01 +00:00
setFieldType(slotName, cbt);
}
void SimFieldDictionary::setFieldType(StringTableEntry slotName, const U32 typeId)
{
ConsoleBaseType *cbt = ConsoleBaseType::getType(typeId);
setFieldType(slotName, cbt);
}
void SimFieldDictionary::setFieldType(StringTableEntry slotName, ConsoleBaseType *type)
{
// If the field exists on the object, set the type
2017-11-06 04:33:32 +00:00
U32 bucket = getHashValue(slotName);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
for (Entry &ref : mHashTable[bucket])
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
if (ref.slotName == slotName)
2012-09-19 15:15:01 +00:00
{
// Found and type assigned, let's bail
2017-11-06 04:33:32 +00:00
ref.type = type;
2012-09-19 15:15:01 +00:00
return;
}
}
// Otherwise create the field, and set the type. Assign a null value.
2017-11-06 04:33:32 +00:00
addEntry(bucket, slotName, type);
2012-09-19 15:15:01 +00:00
}
U32 SimFieldDictionary::getFieldType(StringTableEntry slotName) const
{
2017-11-06 04:33:32 +00:00
U32 bucket = getHashValue(slotName);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
const std::vector<Entry> &vec = mHashTable[bucket];
size_t size = vec.size();
for (size_t i = 0; i < size; ++i)
{
const Entry &ref = vec[i];
if (ref.slotName == slotName)
return ref.type ? ref.type->getTypeID() : TypeString;
}
2012-09-19 15:15:01 +00:00
return TypeString;
}
SimFieldDictionary::Entry *SimFieldDictionary::findDynamicField(const String &fieldName) const
{
2017-11-06 04:33:32 +00:00
U32 bucket = getHashValue(fieldName);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
const std::vector<Entry> &vec = mHashTable[bucket];
size_t size = vec.size();
for (size_t i = 0; i < size; ++i)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
const Entry &ref = vec[i];
if (fieldName.equal(ref.slotName, String::NoCase))
return const_cast<Entry*>(&ref);
2012-09-19 15:15:01 +00:00
}
return NULL;
}
2017-11-06 04:33:32 +00:00
SimFieldDictionary::Entry *SimFieldDictionary::findDynamicField(StringTableEntry fieldName) const
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
U32 bucket = getHashValue(fieldName);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
const std::vector<Entry> &vec = mHashTable[bucket];
size_t size = vec.size();
for (size_t i = 0; i < size; ++i)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
if (vec[i].slotName == fieldName)
{
return const_cast<Entry*>(&vec[i]);
}
2012-09-19 15:15:01 +00:00
}
return NULL;
}
void SimFieldDictionary::setFieldValue(StringTableEntry slotName, const char *value)
{
U32 bucket = getHashValue(slotName);
2017-11-06 04:33:32 +00:00
for (Entry &ref : mHashTable[bucket])
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
if (ref.slotName == slotName)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
if (!value || !*value)
{
mVersion++;
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
if (ref.value)
dFree(ref.value);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
freeEntry(&ref);
}
else
{
if (ref.value)
dFree(ref.value);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
ref.value = dStrdup(value);
}
return;
2012-09-19 15:15:01 +00:00
}
}
2017-11-06 04:33:32 +00:00
// no field, add entry.
addEntry(bucket, slotName, 0, dStrdup(value));
2012-09-19 15:15:01 +00:00
}
const char *SimFieldDictionary::getFieldValue(StringTableEntry slotName)
{
U32 bucket = getHashValue(slotName);
2017-11-06 04:33:32 +00:00
for (const Entry &ref : mHashTable[bucket])
if (ref.slotName == slotName)
return ref.value;
2012-09-19 15:15:01 +00:00
return NULL;
}
void SimFieldDictionary::assignFrom(SimFieldDictionary *dict)
{
mVersion++;
2017-11-06 04:33:32 +00:00
for (U32 i = 0; i < HashTableSize; i++)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
for (const Entry &ref : mHashTable[i])
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
setFieldValue(ref.slotName, ref.value);
setFieldType(ref.slotName, ref.type);
2012-09-19 15:15:01 +00:00
}
}
}
2017-11-06 04:33:32 +00:00
static S32 QSORT_CALLBACK compareEntries(const void* a, const void* b)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
const SimFieldDictionary::Entry *fa = reinterpret_cast<const SimFieldDictionary::Entry*>(a);
const SimFieldDictionary::Entry *fb = reinterpret_cast<const SimFieldDictionary::Entry*>(b);
2012-09-19 15:15:01 +00:00
return dStricmp(fa->slotName, fb->slotName);
}
void SimFieldDictionary::writeFields(SimObject *obj, Stream &stream, U32 tabStop)
{
const AbstractClassRep::FieldList &list = obj->getFieldList();
2017-11-06 04:33:32 +00:00
Vector<Entry> flist(__FILE__, __LINE__);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
for (U32 i = 0; i < HashTableSize; i++)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
for (const Entry &walk : mHashTable[i])
2012-09-19 15:15:01 +00:00
{
// make sure we haven't written this out yet:
2017-11-06 04:33:32 +00:00
U32 j;
for (j = 0; j < list.size(); j++)
if (list[j].pFieldname == walk.slotName)
2012-09-19 15:15:01 +00:00
break;
2017-11-06 04:33:32 +00:00
if (j != list.size())
2012-09-19 15:15:01 +00:00
continue;
2017-11-06 04:33:32 +00:00
if (!obj->writeField(walk.slotName, walk.value))
2012-09-19 15:15:01 +00:00
continue;
flist.push_back(walk);
}
}
// Sort Entries to prevent version control conflicts
2017-11-06 04:33:32 +00:00
dQsort(flist.address(), flist.size(), sizeof(Entry), compareEntries);
2012-09-19 15:15:01 +00:00
// Save them out
2017-11-06 04:33:32 +00:00
for (const Entry &ref : flist)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
U32 nBufferSize = (dStrlen(ref.value) * 2) + dStrlen(ref.slotName) + 16;
FrameTemp<char> expandedBuffer(nBufferSize);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
stream.writeTabs(tabStop + 1);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
const char *typeName = ref.type && ref.type->getTypeID() != TypeString ? ref.type->getTypeName() : "";
dSprintf(expandedBuffer, nBufferSize, "%s%s%s = \"", typeName, *typeName ? " " : "", ref.slotName);
if (ref.value)
expandEscape((char*)expandedBuffer + dStrlen(expandedBuffer), ref.value);
2012-09-19 15:15:01 +00:00
dStrcat(expandedBuffer, "\";\r\n");
2017-11-06 04:33:32 +00:00
stream.write(dStrlen(expandedBuffer), expandedBuffer);
2012-09-19 15:15:01 +00:00
}
}
void SimFieldDictionary::printFields(SimObject *obj)
{
const AbstractClassRep::FieldList &list = obj->getFieldList();
char expandedBuffer[4096];
2017-11-06 04:33:32 +00:00
Vector<Entry> flist(__FILE__, __LINE__);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
for (U32 i = 0; i < HashTableSize; i++)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
for (const Entry &walk : mHashTable[i])
2012-09-19 15:15:01 +00:00
{
// make sure we haven't written this out yet:
2017-11-06 04:33:32 +00:00
U32 j;
for (j = 0; j < list.size(); j++)
if (list[i].pFieldname == walk.slotName)
2012-09-19 15:15:01 +00:00
break;
2017-11-06 04:33:32 +00:00
if (j != list.size())
2012-09-19 15:15:01 +00:00
continue;
flist.push_back(walk);
}
}
2017-11-06 04:33:32 +00:00
dQsort(flist.address(), flist.size(), sizeof(Entry), compareEntries);
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
for (const Entry &ref : flist)
2012-09-19 15:15:01 +00:00
{
const char* type = "string";
2017-11-06 04:33:32 +00:00
if (ref.type)
type = ref.type->getTypeClassName();
dSprintf(expandedBuffer, sizeof(expandedBuffer), " %s %s = \"", type, ref.slotName);
if (ref.value)
expandEscape(expandedBuffer + dStrlen(expandedBuffer), ref.value);
2012-09-19 15:15:01 +00:00
Con::printf("%s\"", expandedBuffer);
}
}
SimFieldDictionary::Entry *SimFieldDictionary::operator[](U32 index)
{
2017-11-06 04:33:32 +00:00
AssertFatal(index < mNumFields, "out of range");
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
if (index > mNumFields)
2012-09-19 15:15:01 +00:00
return NULL;
SimFieldDictionaryIterator itr(this);
for (S32 i = 0; i < index && *itr; i++)
++itr;
return (*itr);
}
//------------------------------------------------------------------------------
SimFieldDictionaryIterator::SimFieldDictionaryIterator(SimFieldDictionary * dictionary)
{
mDictionary = dictionary;
2017-11-06 04:33:32 +00:00
mHashIndex = 0;
mVecIndex = -1; // -1 since we immediately call operator++
mEntry = NULL;
2012-09-19 15:15:01 +00:00
operator++();
}
SimFieldDictionary::Entry* SimFieldDictionaryIterator::operator++()
{
2017-11-06 04:33:32 +00:00
if (!mDictionary || mHashIndex >= SimFieldDictionary::HashTableSize)
{
mEntry = NULL;
return NULL;
}
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
std::vector<SimFieldDictionary::Entry> &vec = mDictionary->mHashTable[mHashIndex];
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
while (vec.size() == 0 && mHashIndex < (SimFieldDictionary::HashTableSize - 1))
{
vec = mDictionary->mHashTable[++mHashIndex];
mVecIndex = 0;
}
if (mVecIndex >= vec.size() || mHashIndex >= SimFieldDictionary::HashTableSize)
{
mEntry = NULL;
return NULL;
}
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
mEntry = &vec[mVecIndex];
++mVecIndex;
return mEntry;
2012-09-19 15:15:01 +00:00
}
SimFieldDictionary::Entry* SimFieldDictionaryIterator::operator*()
{
2017-11-06 04:33:32 +00:00
return mEntry;
}
2017-11-06 04:33:32 +00:00
// A variation of the stock SimFieldDictionary::setFieldValue(), this method adds the
// <no_replace> argument which, when true, prohibits the replacement of fields that
// already have a value.
//
// AFX uses this when an effects-choreographer (afxMagicSpell, afxEffectron) is created
// using the new operator. It prevents any in-line effect parameters from being overwritten
// by default parameters that are copied over later.
void SimFieldDictionary::setFieldValue(StringTableEntry slotName, const char *value, ConsoleBaseType *type, bool no_replace)
{
if (!no_replace)
{
setFieldValue(slotName, value);
return;
}
if (!value || !*value)
return;
U32 bucket = getHashValue(slotName);
2017-11-06 04:33:32 +00:00
for (const Entry &walk : mHashTable[bucket])
{
if (walk.slotName == slotName)
{
return;
}
}
2017-11-06 04:33:32 +00:00
addEntry(bucket, slotName, type, dStrdup(value));
}
// A variation of the stock SimFieldDictionary::assignFrom(), this method adds <no_replace>
// and <filter> arguments. When true, <no_replace> prohibits the replacement of fields that already
// have a value. When <filter> is specified, only fields with leading characters that exactly match
// the characters in <filter> are copied.
void SimFieldDictionary::assignFrom(SimFieldDictionary *dict, const char* filter, bool no_replace)
{
dsize_t filter_len = (filter) ? dStrlen(filter) : 0;
if (filter_len == 0 && !no_replace)
{
assignFrom(dict);
return;
}
mVersion++;
if (filter_len == 0)
{
2017-11-06 04:33:32 +00:00
for (U32 i = 0; i < HashTableSize; i++)
{
for (const Entry &walk : dict->mHashTable[i])
{
setFieldValue(walk.slotName, walk.value, walk.type, no_replace);
}
}
}
else
{
2017-11-06 04:33:32 +00:00
for (U32 i = 0; i < HashTableSize; i++)
{
for (const Entry &walk : dict->mHashTable[i])
{
if (dStrncmp(walk.slotName, filter, filter_len) == 0)
setFieldValue(walk.slotName, walk.value, walk.type, no_replace);
}
}
}
2017-11-06 04:33:32 +00:00
}