1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +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 != "")
{
Settings entry = persistent.write["campaign"][ourCampaign->campaignSet][ourCampaign->getFilename()]["completed"];
Settings entry = persistentStorage.write["completedCampaigns"][ourCampaign->getFilename()];
entry->Bool() = true;
}

View File

@@ -107,7 +107,7 @@ CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode & config, const
auto header = CampaignHandler::getHeader(campFile);
hoverText = header->getName();
if(persistent["campaign"][campaignSet][header->getFilename()]["completed"].Bool())
if(persistentStorage["completedCampaigns"][header->getFilename()].Bool())
status = CCampaignScreen::COMPLETED;
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())
{
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;
}
}
if(persistentStorage["unlockAllCampaigns"].Bool())
status = CCampaignScreen::ENABLED;
if(status != CCampaignScreen::DISABLED)
{
addUsedEvents(LCLICK | HOVER);

View File

@@ -17,7 +17,7 @@
VCMI_LIB_NAMESPACE_BEGIN
SettingsStorage settings;
SettingsStorage persistent;
SettingsStorage persistentStorage;
template<typename Accessor>
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;
cfgName = "config/settings.json";
if(persistentStorage)
cfgName = "config/persistent.json";
this->dataFilename = dataFilename;
this->schema = schema;
JsonPath confName = JsonPath::builtin(cfgName);
JsonPath confName = JsonPath::builtin(dataFilename);
JsonUtils::assembleFromFiles(confName.getOriginalName()).swap(config);
// Probably new install. Create config file to save settings to
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::validate(config, "vcmi:settings", "settings");
JsonUtils::maximize(config, schema);
JsonUtils::validate(config, schema, "settings");
}
}
@@ -83,10 +81,10 @@ void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath
JsonNode savedConf = config;
savedConf.Struct().erase("session");
if(!persistentStorage)
JsonUtils::minimize(savedConf, "vcmi:settings");
if(schema != "")
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();
}

View File

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

View File

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

View File

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