1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

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

This commit is contained in:
Ivan Savenko
2025-03-30 17:18:47 +03:00
parent 4dc82284b4
commit 58a6aabd4c
2 changed files with 5 additions and 5 deletions

View File

@@ -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

View File

@@ -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;
}
};