Merge pull request #300 from Areloch/sortedSettings

Adds sorting to the settings class so when it saves out to file
This commit is contained in:
Brian Roberts 2020-08-23 16:26:59 -05:00 committed by GitHub
commit 20479cd818
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 20 deletions

View file

@ -142,28 +142,32 @@ void Settings::buildGroupString(String &name, const UTF8 *settingName)
if(mGroupStack.size() > 0) if(mGroupStack.size() > 0)
{ {
for(S32 i=0; i < mGroupStack.size(); i++) for(S32 i=0; i < mGroupStack.size(); i++)
{ {
S32 pos = 0; S32 pos = 0;
if(name.size() > 0) if(name.size() > 0)
pos = name.size()-1; pos = name.size()-1;
// tack on the "/" in front if this isn't the first // tack on the "/" in front if this isn't the first
if(i == 0) if(i == 0)
{ {
name.insert(pos, mGroupStack[i]); name.insert(pos, mGroupStack[i]);
} else }
{ else
name.insert(pos, "/"); {
name.insert(pos, "/");
name.insert(pos+1, mGroupStack[i]); name.insert(pos+1, mGroupStack[i]);
} }
} }
// tack on a final "/" // tack on a final "/"
name.insert(name.size()-1, "/"); name.insert(name.size()-1, "/");
if(dStrlen(settingName) > 0) if(dStrlen(settingName) > 0)
name.insert(name.size()-1, settingName); name.insert(name.size()-1, settingName);
} else }
name = settingName; else
{
name = settingName;
}
} }
void Settings::clearAllFields() void Settings::clearAllFields()
@ -238,8 +242,8 @@ bool Settings::write()
SimFieldDictionary::Entry* fieldEntry = *itr; SimFieldDictionary::Entry* fieldEntry = *itr;
String check(fieldEntry->slotName); String check(fieldEntry->slotName);
if(check.find("_default") != String::NPos || check.find("_type") != String::NPos) if(check.find("_default") != String::NPos || check.find("_type") != String::NPos)
continue; continue;
node->addValue(fieldEntry->slotName, fieldEntry->value); node->addValue(fieldEntry->slotName, fieldEntry->value);
} }
@ -622,11 +626,15 @@ void SettingSaveNode::buildDocument(SimXMLDocument *document, bool skipWrite)
if(!mIsGroup && !skipWrite) if(!mIsGroup && !skipWrite)
{ {
document->pushNewElement("Setting"); document->pushNewElement("Setting");
document->setAttribute("name", mName); document->setAttribute("name", mName);
document->addText(mValue); document->addText(mValue);
} else }
else
{ {
mSettingNodes.sort(_NodeCompare);
mGroupNodes.sort(_NodeCompare);
for(S32 i=0; i<mSettingNodes.size(); i++) for(S32 i=0; i<mSettingNodes.size(); i++)
{ {
SettingSaveNode *node = mSettingNodes[i]; SettingSaveNode *node = mSettingNodes[i];
@ -644,6 +652,12 @@ void SettingSaveNode::buildDocument(SimXMLDocument *document, bool skipWrite)
document->popElement(); document->popElement();
} }
S32 QSORT_CALLBACK SettingSaveNode::_NodeCompare(SettingSaveNode* const* a, SettingSaveNode* const* b)
{
S32 result = dStrnatcasecmp((*a)->mName.c_str(), (*b)->mName.c_str());
return result;
}
DefineEngineMethod(Settings, setValue, void, (const char * settingName, const char * value), (""), "settingObj.setValue(settingName, value);") DefineEngineMethod(Settings, setValue, void, (const char * settingName, const char * value), (""), "settingObj.setValue(settingName, value);")
{ {
StringTableEntry fieldName = StringTable->insert( settingName ); StringTableEntry fieldName = StringTable->insert( settingName );

View file

@ -66,6 +66,8 @@ public:
//S32 buildSearchList(const char* pattern, bool deepSearch = false, bool defaultsSearch = false); //S32 buildSearchList(const char* pattern, bool deepSearch = false, bool defaultsSearch = false);
const char* findFirstValue(const char* pattern, bool deepSearch = false, bool includeDefaults = false); const char* findFirstValue(const char* pattern, bool deepSearch = false, bool includeDefaults = false);
const char* findNextValue(); const char* findNextValue();
}; };
class SettingSaveNode class SettingSaveNode
@ -105,6 +107,8 @@ public:
void buildDocument(SimXMLDocument *document, bool skipWrite = false); void buildDocument(SimXMLDocument *document, bool skipWrite = false);
void clear(); void clear();
static S32 _NodeCompare(SettingSaveNode* const* a, SettingSaveNode* const* b);
}; };
#endif #endif