mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-29 19:00:33 +02:00
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
This commit is contained in:
@@ -20,6 +20,8 @@
|
|||||||
#include "interface/ControlFactory.h"
|
#include "interface/ControlFactory.h"
|
||||||
#include "interface/Point.h"
|
#include "interface/Point.h"
|
||||||
#include "interface/Label.h"
|
#include "interface/Label.h"
|
||||||
|
#include "simulation/SaveRenderer.h"
|
||||||
|
#include "client/Client.h"
|
||||||
|
|
||||||
#include "game/GameController.h"
|
#include "game/GameController.h"
|
||||||
#include "game/GameView.h"
|
#include "game/GameView.h"
|
||||||
@@ -237,6 +239,7 @@ int main(int argc, char * argv[])
|
|||||||
ui::Engine::Ref().CloseWindow();
|
ui::Engine::Ref().CloseWindow();
|
||||||
delete gameController;
|
delete gameController;
|
||||||
delete ui::Engine::Ref().g;
|
delete ui::Engine::Ref().g;
|
||||||
|
Client::Ref().Shutdown();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
@@ -194,7 +195,7 @@ void Client::AddListener(ClientListener * listener)
|
|||||||
listeners.push_back(listener);
|
listeners.push_back(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::~Client()
|
void Client::Shutdown()
|
||||||
{
|
{
|
||||||
ClearThumbnailRequests();
|
ClearThumbnailRequests();
|
||||||
http_done();
|
http_done();
|
||||||
@@ -226,6 +227,10 @@ Client::~Client()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Client::~Client()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Client::SetAuthUser(User user)
|
void Client::SetAuthUser(User user)
|
||||||
{
|
{
|
||||||
@@ -1277,3 +1282,208 @@ std::vector<string> * Client::AddTag(int saveID, string tag)
|
|||||||
free(data);
|
free(data);
|
||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<std::string> Client::explodePropertyString(std::string property)
|
||||||
|
{
|
||||||
|
vector<string> 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<string> Client::GetPrefStringArray(std::string property)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
json::Array value = GetPref(property);
|
||||||
|
vector<string> 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<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<double> Client::GetPrefNumberArray(std::string property)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
json::Array value = GetPref(property);
|
||||||
|
vector<double> 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<double>();
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<bool> Client::GetPrefBoolArray(std::string property)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
json::Array value = GetPref(property);
|
||||||
|
vector<bool> 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>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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<string> value)
|
||||||
|
{
|
||||||
|
json::Array newArray;
|
||||||
|
for(vector<string>::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<double> value)
|
||||||
|
{
|
||||||
|
json::Array newArray;
|
||||||
|
for(vector<double>::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<bool> value)
|
||||||
|
{
|
||||||
|
json::Array newArray;
|
||||||
|
for(vector<bool>::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<string> pTokens = Client::explodePropertyString(property);
|
||||||
|
const json::UnknownElement & configDocumentCopy = configDocument;
|
||||||
|
json::UnknownElement currentRef = configDocumentCopy;
|
||||||
|
for(vector<string>::iterator iter = pTokens.begin(); iter != pTokens.end(); ++iter)
|
||||||
|
{
|
||||||
|
currentRef = currentRef[*iter];
|
||||||
|
}
|
||||||
|
return currentRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::setPrefR(std::deque<string> 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<string> pTokens = Client::explodePropertyString(property);
|
||||||
|
deque<string> dTokens(pTokens.begin(), pTokens.end());
|
||||||
|
string token = dTokens.front();
|
||||||
|
dTokens.pop_front();
|
||||||
|
setPrefR(dTokens, configDocument[token], value);
|
||||||
|
}
|
||||||
|
@@ -51,13 +51,13 @@ private:
|
|||||||
int activeThumbRequestCompleteTimes[IMGCONNS];
|
int activeThumbRequestCompleteTimes[IMGCONNS];
|
||||||
std::string activeThumbRequestIDs[IMGCONNS];
|
std::string activeThumbRequestIDs[IMGCONNS];
|
||||||
void updateStamps();
|
void updateStamps();
|
||||||
|
static vector<std::string> explodePropertyString(std::string property);
|
||||||
void notifyUpdateAvailable();
|
void notifyUpdateAvailable();
|
||||||
public:
|
|
||||||
vector<ClientListener*> listeners;
|
|
||||||
|
|
||||||
//Config file handle
|
//Config file handle
|
||||||
json::Object configDocument;
|
json::Object configDocument;
|
||||||
|
public:
|
||||||
|
vector<ClientListener*> listeners;
|
||||||
|
|
||||||
Client();
|
Client();
|
||||||
~Client();
|
~Client();
|
||||||
@@ -94,6 +94,25 @@ public:
|
|||||||
return lastError;
|
return lastError;
|
||||||
}
|
}
|
||||||
void Tick();
|
void Tick();
|
||||||
|
void Shutdown();
|
||||||
|
|
||||||
|
std::string GetPrefString(std::string property, std::string defaultValue);
|
||||||
|
double GetPrefNumber(std::string property, double defaultValue);
|
||||||
|
vector<string> GetPrefStringArray(std::string property);
|
||||||
|
vector<double> GetPrefNumberArray(std::string property);
|
||||||
|
vector<bool> 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<string> value);
|
||||||
|
void SetPref(std::string property, vector<double> value);
|
||||||
|
void SetPref(std::string property, vector<bool> value);
|
||||||
|
void SetPref(std::string property, bool value);
|
||||||
|
|
||||||
|
json::UnknownElement GetPref(std::string property);
|
||||||
|
void setPrefR(std::deque<string> tokens, json::UnknownElement & element, json::UnknownElement & value);
|
||||||
|
void SetPref(std::string property, json::UnknownElement & value);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CLIENT_H
|
#endif // CLIENT_H
|
||||||
|
@@ -30,33 +30,19 @@ GameModel::GameModel():
|
|||||||
//Load config into renderer
|
//Load config into renderer
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
json::Number tempNumber = Client::Ref().configDocument["Renderer"]["ColourMode"];
|
ren->SetColourMode(Client::Ref().GetPrefNumber("Renderer.ColourMode", 0));
|
||||||
if(tempNumber.Value())
|
|
||||||
ren->SetColourMode(tempNumber.Value());
|
|
||||||
|
|
||||||
json::Array tempArray = Client::Ref().configDocument["Renderer"]["DisplayModes"];
|
vector<double> tempArray = Client::Ref().GetPrefNumberArray("Renderer.DisplayModes");
|
||||||
if(tempArray.Size())
|
if(tempArray.size())
|
||||||
{
|
{
|
||||||
std::vector<unsigned int> displayModes;
|
std::vector<unsigned int> displayModes(tempArray.begin(), tempArray.end());
|
||||||
json::Array::const_iterator itDisplayModes(tempArray.Begin()), itDisplayModesEnd(tempArray.End());
|
|
||||||
for (; itDisplayModes != itDisplayModesEnd; ++itDisplayModes)
|
|
||||||
{
|
|
||||||
json::Number tempNumberI = *itDisplayModes;
|
|
||||||
displayModes.push_back(tempNumberI.Value());
|
|
||||||
}
|
|
||||||
ren->SetDisplayMode(displayModes);
|
ren->SetDisplayMode(displayModes);
|
||||||
}
|
}
|
||||||
|
|
||||||
tempArray = Client::Ref().configDocument["Renderer"]["RenderModes"];
|
tempArray = Client::Ref().GetPrefNumberArray("Renderer.RenderModes");
|
||||||
if(tempArray.Size())
|
if(tempArray.size())
|
||||||
{
|
{
|
||||||
std::vector<unsigned int> renderModes;
|
std::vector<unsigned int> renderModes(tempArray.begin(), tempArray.end());
|
||||||
json::Array::const_iterator itRenderModes(tempArray.Begin()), itRenderModesEnd(tempArray.End());
|
|
||||||
for (; itRenderModes != itRenderModesEnd; ++itRenderModes)
|
|
||||||
{
|
|
||||||
json::Number tempNumberI = *itRenderModes;
|
|
||||||
renderModes.push_back(tempNumberI.Value());
|
|
||||||
}
|
|
||||||
ren->SetRenderMode(renderModes);
|
ren->SetRenderMode(renderModes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,28 +131,13 @@ GameModel::GameModel():
|
|||||||
GameModel::~GameModel()
|
GameModel::~GameModel()
|
||||||
{
|
{
|
||||||
//Save to config:
|
//Save to config:
|
||||||
try
|
Client::Ref().SetPref("Renderer.ColourMode", (double)ren->GetColourMode());
|
||||||
{
|
|
||||||
Client::Ref().configDocument["Renderer"]["ColourMode"] = json::Number(ren->GetColourMode());
|
|
||||||
|
|
||||||
Client::Ref().configDocument["Renderer"]["DisplayModes"] = json::Array();
|
|
||||||
std::vector<unsigned int> displayModes = ren->GetDisplayMode();
|
std::vector<unsigned int> displayModes = ren->GetDisplayMode();
|
||||||
for (int i = 0; i < displayModes.size(); i++)
|
Client::Ref().SetPref("Renderer.DisplayModes", std::vector<double>(displayModes.begin(), displayModes.end()));
|
||||||
{
|
|
||||||
Client::Ref().configDocument["Renderer"]["DisplayModes"][i] = json::Number(displayModes[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Client::Ref().configDocument["Renderer"]["RenderModes"] = json::Array();
|
|
||||||
std::vector<unsigned int> renderModes = ren->GetRenderMode();
|
std::vector<unsigned int> renderModes = ren->GetRenderMode();
|
||||||
for (int i = 0; i < renderModes.size(); i++)
|
Client::Ref().SetPref("Renderer.RenderModes", std::vector<double>(renderModes.begin(), renderModes.end()));
|
||||||
{
|
|
||||||
Client::Ref().configDocument["Renderer"]["RenderModes"][i] = json::Number(renderModes[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(json::Exception & e)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i = 0; i < menuList.size(); i++)
|
for(int i = 0; i < menuList.size(); i++)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user