1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

code review

This commit is contained in:
Laserlicht
2023-09-21 21:27:06 +02:00
committed by GitHub
parent 4d8414bd3d
commit 5fd2eee3e8
6 changed files with 25 additions and 24 deletions

View File

@@ -685,7 +685,7 @@ void CServerHandler::startCampaignScenario(std::shared_ptr<CampaignState> cs)
{ {
if(ourCampaign->campaignSet != "") if(ourCampaign->campaignSet != "")
{ {
Settings entry = persistent.write["campaign"][ourCampaign->campaignSet][ourCampaign->getFilename()]["completed"]; Settings entry = persistentStorage.write["completedCampaigns"][ourCampaign->getFilename()];
entry->Bool() = true; entry->Bool() = true;
} }

View File

@@ -107,7 +107,7 @@ CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config, const
auto header = CampaignHandler::getHeader(campFile); auto header = CampaignHandler::getHeader(campFile);
hoverText = header->getName(); hoverText = header->getName();
if(persistent["campaign"][campaignSet][header->getFilename()]["completed"].Bool()) if(persistentStorage["completedCampaigns"][header->getFilename()].Bool())
status = CCampaignScreen::COMPLETED; status = CCampaignScreen::COMPLETED;
for(const JsonNode & node : parentConfig[campaignSet]["items"].Vector()) for(const JsonNode & node : parentConfig[campaignSet]["items"].Vector())
@@ -115,11 +115,14 @@ CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config, const
for(const JsonNode & requirement : config["requires"].Vector()) for(const JsonNode & requirement : config["requires"].Vector())
{ {
if(node["id"].Integer() == requirement.Integer()) if(node["id"].Integer() == requirement.Integer())
if(!persistent["campaign"][campaignSet][node["file"].String()]["completed"].Bool()) if(!persistentStorage["completedCampaigns"][node["file"].String()].Bool())
status = CCampaignScreen::DISABLED; status = CCampaignScreen::DISABLED;
} }
} }
if(persistentStorage["unlockAllCampaigns"].Bool())
status = CCampaignScreen::ENABLED;
if(status != CCampaignScreen::DISABLED) if(status != CCampaignScreen::DISABLED)
{ {
addUsedEvents(LCLICK | HOVER); addUsedEvents(LCLICK | HOVER);

View File

@@ -17,7 +17,7 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
SettingsStorage settings; SettingsStorage settings;
SettingsStorage persistent; SettingsStorage persistentStorage;
template<typename Accessor> template<typename Accessor>
SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path): SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path):
@@ -54,25 +54,23 @@ SettingsStorage::SettingsStorage():
{ {
} }
void SettingsStorage::init(bool p) void SettingsStorage::init(const std::string & dataFilename, const std::string & schema)
{ {
persistentStorage = p; this->dataFilename = dataFilename;
cfgName = "config/settings.json"; this->schema = schema;
if(persistentStorage)
cfgName = "config/persistent.json";
JsonPath confName = JsonPath::builtin(cfgName); JsonPath confName = JsonPath::builtin(dataFilename);
JsonUtils::assembleFromFiles(confName.getOriginalName()).swap(config); JsonUtils::assembleFromFiles(confName.getOriginalName()).swap(config);
// Probably new install. Create config file to save settings to // Probably new install. Create config file to save settings to
if (!CResourceHandler::get("local")->existsResource(confName)) if (!CResourceHandler::get("local")->existsResource(confName))
CResourceHandler::get("local")->createResource(cfgName); CResourceHandler::get("local")->createResource(dataFilename);
if(!persistentStorage) if(schema != "")
{ {
JsonUtils::maximize(config, "vcmi:settings"); JsonUtils::maximize(config, schema);
JsonUtils::validate(config, "vcmi:settings", "settings"); JsonUtils::validate(config, schema, "settings");
} }
} }
@@ -83,10 +81,10 @@ void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath
JsonNode savedConf = config; JsonNode savedConf = config;
savedConf.Struct().erase("session"); savedConf.Struct().erase("session");
if(!persistentStorage) if(schema != "")
JsonUtils::minimize(savedConf, "vcmi:settings"); JsonUtils::minimize(savedConf, schema);
std::fstream file(CResourceHandler::get()->getResourceName(JsonPath::builtin(cfgName))->c_str(), std::ofstream::out | std::ofstream::trunc); std::fstream file(CResourceHandler::get()->getResourceName(JsonPath::builtin(dataFilename))->c_str(), std::ofstream::out | std::ofstream::trunc);
file << savedConf.toJson(); file << savedConf.toJson();
} }

View File

@@ -35,8 +35,8 @@ class DLL_LINKAGE SettingsStorage
std::set<SettingsListener*> listeners; std::set<SettingsListener*> listeners;
JsonNode config; JsonNode config;
bool persistentStorage; std::string dataFilename;
std::string cfgName; std::string schema;
JsonNode & getNode(const std::vector<std::string> & path); JsonNode & getNode(const std::vector<std::string> & path);
@@ -48,7 +48,7 @@ class DLL_LINKAGE SettingsStorage
public: public:
// Initialize config structure // Initialize config structure
SettingsStorage(); SettingsStorage();
void init(bool persistent = false); void init(const std::string & dataFilename, const std::string & schema);
// Get write access to config node at path // Get write access to config node at path
const NodeAccessor<Settings> write; const NodeAccessor<Settings> write;
@@ -116,6 +116,6 @@ public:
}; };
extern DLL_LINKAGE SettingsStorage settings; extern DLL_LINKAGE SettingsStorage settings;
extern DLL_LINKAGE SettingsStorage persistent; extern DLL_LINKAGE SettingsStorage persistentStorage;
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@@ -52,8 +52,8 @@ DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool onlyEssential, bool
console = Console; console = Console;
VLC = new LibClasses(); VLC = new LibClasses();
VLC->loadFilesystem(extractArchives); VLC->loadFilesystem(extractArchives);
settings.init(); settings.init("config/settings.json", "vcmi:settings");
persistent.init(true); persistentStorage.init("config/persistentStorage.json", "");
VLC->loadModFilesystem(onlyEssential); VLC->loadModFilesystem(onlyEssential);
} }

View File

@@ -293,7 +293,7 @@ public:
static JsonNode crossoverSerialize(CGHeroInstance * hero); static JsonNode crossoverSerialize(CGHeroInstance * hero);
static CGHeroInstance * crossoverDeserialize(const JsonNode & node, CMap * map); static CGHeroInstance * crossoverDeserialize(const JsonNode & node, CMap * map);
std::string campaignSet = ""; std::string campaignSet;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {