Browse Source

properly initialize settings when configuration was not found

Ivan Savenko 12 years ago
parent
commit
0307639c32
4 changed files with 34 additions and 9 deletions
  1. 5 0
      config/schemas/settings.json
  2. 7 2
      lib/CConfigHandler.cpp
  3. 11 1
      lib/CModHandler.cpp
  4. 11 6
      lib/JsonNode.cpp

+ 5 - 0
config/schemas/settings.json

@@ -8,6 +8,7 @@
 	{
 	{
 		"general" : {
 		"general" : {
 			"type" : "object",
 			"type" : "object",
+			"default": {},
 			"required" : [ "classicCreatureWindow", "playerName", "showfps", "music", "sound" ],
 			"required" : [ "classicCreatureWindow", "playerName", "showfps", "music", "sound" ],
 			"properties" : {
 			"properties" : {
 				"classicCreatureWindow" : {
 				"classicCreatureWindow" : {
@@ -34,6 +35,7 @@
 		},
 		},
 		"video" : {
 		"video" : {
 			"type" : "object",
 			"type" : "object",
+			"default": {},
 			"required" : [ "screenRes", "bitsPerPixel", "fullscreen" ],
 			"required" : [ "screenRes", "bitsPerPixel", "fullscreen" ],
 			"properties" : {
 			"properties" : {
 				"screenRes" : {
 				"screenRes" : {
@@ -57,6 +59,7 @@
 		},
 		},
 		"adventure" : {
 		"adventure" : {
 			"type" : "object",
 			"type" : "object",
+			"default": {},
 			"required" : [ "heroSpeed", "enemySpeed", "scrollSpeed", "heroReminder" ],
 			"required" : [ "heroSpeed", "enemySpeed", "scrollSpeed", "heroReminder" ],
 			"properties" : {
 			"properties" : {
 				"heroSpeed" : {
 				"heroSpeed" : {
@@ -79,6 +82,7 @@
 		},
 		},
 		"battle" : {
 		"battle" : {
 			"type" : "object",
 			"type" : "object",
+			"default": {},
 			"required" : [ "animationSpeed", "mouseShadow", "cellBorders", "stackRange", "showQueue" ],
 			"required" : [ "animationSpeed", "mouseShadow", "cellBorders", "stackRange", "showQueue" ],
 			"properties" : {
 			"properties" : {
 				"animationSpeed" : {
 				"animationSpeed" : {
@@ -105,6 +109,7 @@
 		},
 		},
 		"server" : {
 		"server" : {
 			"type" : "object",
 			"type" : "object",
+			"default": {},
 			"required" : [ "server", "port", "localInformation", "playerAI", "neutralAI" ],
 			"required" : [ "server", "port", "localInformation", "playerAI", "neutralAI" ],
 			"properties" : {
 			"properties" : {
 				"server" : {
 				"server" : {

+ 7 - 2
lib/CConfigHandler.cpp

@@ -57,8 +57,13 @@ SettingsStorage::SettingsStorage():
 
 
 void SettingsStorage::init()
 void SettingsStorage::init()
 {
 {
-	CResourceHandler::get()->createResource("config/settings.json");
-	JsonNode(ResourceID("config/settings.json")).swap(config);
+	std::string confName = "config/settings.json";
+
+	// Porbably new install. Create initial configuration
+	if (!CResourceHandler::get()->existsResource(ResourceID(confName)))
+		CResourceHandler::get()->createResource(confName);
+	else
+		JsonNode(ResourceID("config/settings.json")).swap(config);
 
 
 	JsonUtils::maximize(config, "vcmi:settings");
 	JsonUtils::maximize(config, "vcmi:settings");
 	JsonUtils::validate(config, "vcmi:settings", "settings");
 	JsonUtils::validate(config, "vcmi:settings", "settings");

+ 11 - 1
lib/CModHandler.cpp

@@ -229,7 +229,17 @@ std::vector <TModID> CModHandler::resolveDependencies(std::vector <TModID> input
 
 
 void CModHandler::initialize(std::vector<std::string> availableMods)
 void CModHandler::initialize(std::vector<std::string> availableMods)
 {
 {
-	JsonNode modConfig(ResourceID("config/modSettings.json"));
+	std::string confName = "config/modSettings.json";
+	JsonNode modConfig;
+
+	// Porbably new install. Create initial configuration
+	if (!CResourceHandler::get()->existsResource(ResourceID(confName)))
+		CResourceHandler::get()->createResource(confName);
+	else
+		modConfig = JsonNode(ResourceID(confName));
+
+	CResourceHandler::get()->createResource("config/modSettings.json");
+
 	const JsonNode & modList = modConfig["activeMods"];
 	const JsonNode & modList = modConfig["activeMods"];
 	JsonNode resultingList;
 	JsonNode resultingList;
 
 

+ 11 - 6
lib/JsonNode.cpp

@@ -1080,14 +1080,19 @@ std::string JsonValidator::fail(const std::string &message)
 {
 {
 	std::string errors;
 	std::string errors;
 	errors += "At ";
 	errors += "At ";
-	BOOST_FOREACH(const JsonNode &path, currentPath)
+	if (!currentPath.empty())
 	{
 	{
-		errors += "/";
-		if (path.getType() == JsonNode::DATA_STRING)
-			errors += path.String();
-		else
-			errors += boost::lexical_cast<std::string>(static_cast<unsigned>(path.Float()));
+		BOOST_FOREACH(const JsonNode &path, currentPath)
+		{
+			errors += "/";
+			if (path.getType() == JsonNode::DATA_STRING)
+				errors += path.String();
+			else
+				errors += boost::lexical_cast<std::string>(static_cast<unsigned>(path.Float()));
+		}
 	}
 	}
+	else
+		errors += "<root>";
 	errors += "\n\t Error: " + message + "\n";
 	errors += "\n\t Error: " + message + "\n";
 	return errors;
 	return errors;
 }
 }