Skip to content

Commit b06d63b

Browse files
authored
custom levels: sort actors by aid and check for duplicates (#3315)
When giving entities custom actor IDs in your level JSON, it is possible to break entity lookups by actor ID if the actors are not sorted by ID because `entity-by-aid` expects them to be in order. This sorts the actor list by ID before generating the level file and also checks for any duplicates.
1 parent 13f1aa1 commit b06d63b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

goalc/build_level/jak1/build_level.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ bool run_build_level(const std::string& input_file,
4848
auto dts = decompiler::DecompilerTypeSystem(GameVersion::Jak1);
4949
dts.parse_enum_defs({"decompiler", "config", "jak1", "all-types.gc"});
5050
add_actors_from_json(level_json.at("actors"), actors, level_json.value("base_id", 1234), dts);
51+
std::sort(actors.begin(), actors.end(), [](auto& a, auto& b) { return a.aid < b.aid; });
52+
auto duplicates = std::adjacent_find(actors.begin(), actors.end(),
53+
[](auto& a, auto& b) { return a.aid == b.aid; });
54+
ASSERT_MSG(duplicates == actors.end(),
55+
fmt::format("Actor IDs must be unique. Found at least two actors with ID {}",
56+
duplicates->aid));
5157
file.actors = std::move(actors);
5258
// ambients
5359
std::vector<EntityAmbient> ambients;

goalc/build_level/jak2/build_level.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ bool run_build_level(const std::string& input_file,
4747
dts.parse_enum_defs({"decompiler", "config", "jak2", "all-types.gc"});
4848
std::vector<EntityActor> actors;
4949
add_actors_from_json(level_json.at("actors"), actors, level_json.value("base_id", 1234), dts);
50+
std::sort(actors.begin(), actors.end(), [](auto& a, auto& b) { return a.aid < b.aid; });
51+
auto duplicates = std::adjacent_find(actors.begin(), actors.end(),
52+
[](auto& a, auto& b) { return a.aid == b.aid; });
53+
ASSERT_MSG(duplicates == actors.end(),
54+
fmt::format("Actor IDs must be unique. Found at least two actors with ID {}",
55+
duplicates->aid));
5056
file.actors = std::move(actors);
5157
// cameras
5258
// nodes

0 commit comments

Comments
 (0)