Explorar el Código

Switch list patching to more user-friendly 1-based indexing

Ivan Savenko hace 6 meses
padre
commit
58a6aabd4c
Se han modificado 2 ficheros con 5 adiciones y 5 borrados
  1. 2 2
      docs/modders/Readme.md
  2. 3 3
      lib/json/JsonUtils.cpp

+ 2 - 2
docs/modders/Readme.md

@@ -180,13 +180,13 @@ More complete description of such syntax:
 		"appendItems" : [ "crossbowman", "arbalist" ]
 		
 		// insert new item before specified position
-		// NOTE: VCMI assume 0-based indexation, the very first item has index '0'
+		// NOTE: VCMI assume 1-based indexation, the very first item has index '1'
 		// Following example will insert new item before 0th item - at the very beginning of the list
 		// Item with provided index must exist in the list
 		"insert@0" : "crossbowman"
 		
 		// modify existing item at specified position
-		// NOTE: VCMI assume 0-based indexation, the very first item has index '0'
+		// NOTE: VCMI assume 1-based indexation, the very first item has index '1'
 		// Following example will modify 0th item
 		// If item is a json object with multiple properties, e.g. { "key" : "value" } 
 		// you only need to provide changed properites, as usually

+ 3 - 3
lib/json/JsonUtils.cpp

@@ -222,9 +222,9 @@ void JsonUtils::merge(JsonNode & dest, JsonNode & source, bool ignoreOverride, b
 					{
 						try {
 							int index = std::stoi(keyName);
-							if (index < 0 || index > dest.Vector().size())
+							if (index <= 0 || index > dest.Vector().size())
 								throw std::out_of_range("dummy");
-							return index;
+							return index - 1; // 1-based index -> 0-based index
 						}
 						catch(const std::invalid_argument &)
 						{
@@ -233,7 +233,7 @@ void JsonUtils::merge(JsonNode & dest, JsonNode & source, bool ignoreOverride, b
 						}
 						catch(const std::out_of_range & )
 						{
-							logMod->warn("Failed to replace index when replacing individual items in array. Value '%s' does not exists in targeted array", keyName);
+							logMod->warn("Failed to replace index when replacing individual items in array. Value '%s' does not exists in targeted array of %d items", keyName, dest.Vector().size());
 							return std::nullopt;
 						}
 					};