From 3c91e526bb5576727e53b98fb850570074f9d036 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Fri, 22 Jun 2012 01:04:55 +0100 Subject: [PATCH] Client now provides methods for reading and saving preferences - less powerful than raw access to Cajun, (no mixed type arrays, for example) but allows other save formats to be used, such as property lists on OS X --- src/PowderToySDL.cpp | 3 + src/client/Client.cpp | 212 ++++++++++++++++++++++++++++++++++++++++- src/client/Client.h | 25 ++++- src/game/GameModel.cpp | 53 +++-------- 4 files changed, 248 insertions(+), 45 deletions(-) diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 53f902756..a8bb9a0d4 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -20,6 +20,8 @@ #include "interface/ControlFactory.h" #include "interface/Point.h" #include "interface/Label.h" +#include "simulation/SaveRenderer.h" +#include "client/Client.h" #include "game/GameController.h" #include "game/GameView.h" @@ -237,6 +239,7 @@ int main(int argc, char * argv[]) ui::Engine::Ref().CloseWindow(); delete gameController; delete ui::Engine::Ref().g; + Client::Ref().Shutdown(); return 0; } diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 285a9ad64..41c1faffb 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #ifdef WIN32 #include @@ -194,7 +195,7 @@ void Client::AddListener(ClientListener * listener) listeners.push_back(listener); } -Client::~Client() +void Client::Shutdown() { ClearThumbnailRequests(); http_done(); @@ -226,6 +227,10 @@ Client::~Client() } } +Client::~Client() +{ +} + void Client::SetAuthUser(User user) { @@ -1277,3 +1282,208 @@ std::vector * Client::AddTag(int saveID, string tag) free(data); return tags; } + +vector Client::explodePropertyString(std::string property) +{ + vector stringArray; + string current = ""; + for (string::iterator iter = property.begin(); iter != property.end(); ++iter) { + if (*iter == '.') { + if (current.length() > 0) { + stringArray.push_back(current); + current = ""; + } + } else { + current += *iter; + } + } + if(current.length() > 0) + stringArray.push_back(current); + return stringArray; +} + +std::string Client::GetPrefString(std::string property, std::string defaultValue) +{ + try + { + json::String value = GetPref(property); + return value.Value(); + } + catch (json::Exception & e) + { + + } + return defaultValue; +} + +double Client::GetPrefNumber(std::string property, double defaultValue) +{ + try + { + json::Number value = GetPref(property); + return value.Value(); + } + catch (json::Exception & e) + { + + } + return defaultValue; +} + +vector Client::GetPrefStringArray(std::string property) +{ + try + { + json::Array value = GetPref(property); + vector strArray; + for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter) + { + json::String cValue = *iter; + strArray.push_back(cValue.Value()); + } + return strArray; + } + catch (json::Exception & e) + { + + } + return vector(); +} + +vector Client::GetPrefNumberArray(std::string property) +{ + try + { + json::Array value = GetPref(property); + vector strArray; + for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter) + { + json::Number cValue = *iter; + strArray.push_back(cValue.Value()); + } + return strArray; + } + catch (json::Exception & e) + { + + } + return vector(); +} + +vector Client::GetPrefBoolArray(std::string property) +{ + try + { + json::Array value = GetPref(property); + vector strArray; + for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter) + { + json::Boolean cValue = *iter; + strArray.push_back(cValue.Value()); + } + return strArray; + } + catch (json::Exception & e) + { + + } + return vector(); +} + +bool Client::GetPrefBool(std::string property, bool defaultValue) +{ + try + { + json::Boolean value = GetPref(property); + return value.Value(); + } + catch (json::Exception & e) + { + + } + return defaultValue; +} + +void Client::SetPref(std::string property, std::string value) +{ + json::UnknownElement stringValue = json::String(value); + SetPref(property, stringValue); +} + +void Client::SetPref(std::string property, double value) +{ + json::UnknownElement numberValue = json::Number(value); + SetPref(property, numberValue); +} + +void Client::SetPref(std::string property, vector value) +{ + json::Array newArray; + for(vector::iterator iter = value.begin(); iter != value.end(); ++iter) + { + newArray.Insert(json::String(*iter)); + } + json::UnknownElement newArrayValue = newArray; + SetPref(property, newArrayValue); +} + +void Client::SetPref(std::string property, vector value) +{ + json::Array newArray; + for(vector::iterator iter = value.begin(); iter != value.end(); ++iter) + { + newArray.Insert(json::Number(*iter)); + } + json::UnknownElement newArrayValue = newArray; + SetPref(property, newArrayValue); +} + +void Client::SetPref(std::string property, vector value) +{ + json::Array newArray; + for(vector::iterator iter = value.begin(); iter != value.end(); ++iter) + { + newArray.Insert(json::Boolean(*iter)); + } + json::UnknownElement newArrayValue = newArray; + SetPref(property, newArrayValue); +} + +void Client::SetPref(std::string property, bool value) +{ + json::UnknownElement boolValue = json::Boolean(value); + SetPref(property, boolValue); +} + +json::UnknownElement Client::GetPref(std::string property) +{ + vector pTokens = Client::explodePropertyString(property); + const json::UnknownElement & configDocumentCopy = configDocument; + json::UnknownElement currentRef = configDocumentCopy; + for(vector::iterator iter = pTokens.begin(); iter != pTokens.end(); ++iter) + { + currentRef = currentRef[*iter]; + } + return currentRef; +} + +void Client::setPrefR(std::deque tokens, json::UnknownElement & element, json::UnknownElement & value) +{ + if(tokens.size()) + { + std::string token = tokens.front(); + tokens.pop_front(); + setPrefR(tokens, element[token], value); + } + else + element = value; +} + +void Client::SetPref(std::string property, json::UnknownElement & value) +{ + vector pTokens = Client::explodePropertyString(property); + deque dTokens(pTokens.begin(), pTokens.end()); + string token = dTokens.front(); + dTokens.pop_front(); + setPrefR(dTokens, configDocument[token], value); +} diff --git a/src/client/Client.h b/src/client/Client.h index 6cdedd4aa..8c1fae7b5 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -51,13 +51,13 @@ private: int activeThumbRequestCompleteTimes[IMGCONNS]; std::string activeThumbRequestIDs[IMGCONNS]; void updateStamps(); - + static vector explodePropertyString(std::string property); void notifyUpdateAvailable(); -public: - vector listeners; //Config file handle json::Object configDocument; +public: + vector listeners; Client(); ~Client(); @@ -94,6 +94,25 @@ public: return lastError; } void Tick(); + void Shutdown(); + + std::string GetPrefString(std::string property, std::string defaultValue); + double GetPrefNumber(std::string property, double defaultValue); + vector GetPrefStringArray(std::string property); + vector GetPrefNumberArray(std::string property); + vector GetPrefBoolArray(std::string property); + bool GetPrefBool(std::string property, bool defaultValue); + + void SetPref(std::string property, std::string value); + void SetPref(std::string property, double value); + void SetPref(std::string property, vector value); + void SetPref(std::string property, vector value); + void SetPref(std::string property, vector value); + void SetPref(std::string property, bool value); + + json::UnknownElement GetPref(std::string property); + void setPrefR(std::deque tokens, json::UnknownElement & element, json::UnknownElement & value); + void SetPref(std::string property, json::UnknownElement & value); }; #endif // CLIENT_H diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 7a1d2311b..eb0161574 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -30,33 +30,19 @@ GameModel::GameModel(): //Load config into renderer try { - json::Number tempNumber = Client::Ref().configDocument["Renderer"]["ColourMode"]; - if(tempNumber.Value()) - ren->SetColourMode(tempNumber.Value()); + ren->SetColourMode(Client::Ref().GetPrefNumber("Renderer.ColourMode", 0)); - json::Array tempArray = Client::Ref().configDocument["Renderer"]["DisplayModes"]; - if(tempArray.Size()) + vector tempArray = Client::Ref().GetPrefNumberArray("Renderer.DisplayModes"); + if(tempArray.size()) { - std::vector displayModes; - json::Array::const_iterator itDisplayModes(tempArray.Begin()), itDisplayModesEnd(tempArray.End()); - for (; itDisplayModes != itDisplayModesEnd; ++itDisplayModes) - { - json::Number tempNumberI = *itDisplayModes; - displayModes.push_back(tempNumberI.Value()); - } + std::vector displayModes(tempArray.begin(), tempArray.end()); ren->SetDisplayMode(displayModes); } - tempArray = Client::Ref().configDocument["Renderer"]["RenderModes"]; - if(tempArray.Size()) + tempArray = Client::Ref().GetPrefNumberArray("Renderer.RenderModes"); + if(tempArray.size()) { - std::vector renderModes; - json::Array::const_iterator itRenderModes(tempArray.Begin()), itRenderModesEnd(tempArray.End()); - for (; itRenderModes != itRenderModesEnd; ++itRenderModes) - { - json::Number tempNumberI = *itRenderModes; - renderModes.push_back(tempNumberI.Value()); - } + std::vector renderModes(tempArray.begin(), tempArray.end()); ren->SetRenderMode(renderModes); } } @@ -145,28 +131,13 @@ GameModel::GameModel(): GameModel::~GameModel() { //Save to config: - try - { - Client::Ref().configDocument["Renderer"]["ColourMode"] = json::Number(ren->GetColourMode()); + Client::Ref().SetPref("Renderer.ColourMode", (double)ren->GetColourMode()); - Client::Ref().configDocument["Renderer"]["DisplayModes"] = json::Array(); - std::vector displayModes = ren->GetDisplayMode(); - for (int i = 0; i < displayModes.size(); i++) - { - Client::Ref().configDocument["Renderer"]["DisplayModes"][i] = json::Number(displayModes[i]); - } + std::vector displayModes = ren->GetDisplayMode(); + Client::Ref().SetPref("Renderer.DisplayModes", std::vector(displayModes.begin(), displayModes.end())); - Client::Ref().configDocument["Renderer"]["RenderModes"] = json::Array(); - std::vector renderModes = ren->GetRenderMode(); - for (int i = 0; i < renderModes.size(); i++) - { - Client::Ref().configDocument["Renderer"]["RenderModes"][i] = json::Number(renderModes[i]); - } - } - catch(json::Exception & e) - { - - } + std::vector renderModes = ren->GetRenderMode(); + Client::Ref().SetPref("Renderer.RenderModes", std::vector(renderModes.begin(), renderModes.end())); for(int i = 0; i < menuList.size(); i++) {