2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-11-23 13:12:06 +00:00

Refactor to improve C++ and safe code use

This commit is contained in:
LJW-Dev
2025-10-26 18:20:04 +08:00
parent 173565c7b3
commit 9d4c32b6b4
10 changed files with 285 additions and 261 deletions

View File

@@ -8,14 +8,14 @@ namespace
{
bool parseMapEntsJSON(json& entArrayJs, std::string& entityString)
{
int entityCount = entArrayJs.size();
for (int i = 0; i < entityCount; i++)
for (size_t entIdx = 0; entIdx < entArrayJs.size(); entIdx++)
{
auto currEntity = entArrayJs[i];
auto& entity = entArrayJs[entIdx];
if (i == 0)
if (entIdx == 0)
{
std::string className = currEntity["classname"];
std::string className;
entity.at("classname").get_to(className);
if (className.compare("worldspawn") != 0)
{
con::error("ERROR: first entity in the map entity string must be the worldspawn class!");
@@ -25,7 +25,7 @@ namespace
entityString.append("{\n");
for (auto& element : currEntity.items())
for (auto& element : entity.items())
{
std::string key = element.key();
std::string value = element.value();
@@ -38,26 +38,31 @@ namespace
return true;
}
void parseSpawnpointJSON(json& entArrayJs, std::string& entityString, const char* spawnpointNames[], int nameCount)
void parseSpawnpointJSON(json& entArrayJs, std::string& entityString, const char* spawnpointNames[], size_t nameCount)
{
int entityCount = entArrayJs.size();
for (int i = 0; i < entityCount; i++)
for (auto& element : entArrayJs.items())
{
auto currEntity = entArrayJs[i];
std::string origin;
std::string angles;
auto& entity = element.value();
entity.at("origin").get_to(origin);
entity.at("angles").get_to(angles);
std::string origin = currEntity["origin"];
std::string angles = currEntity["angles"];
for (int k = 0; k < nameCount; k++)
for (size_t nameIdx = 0; nameIdx < nameCount; nameIdx++)
{
entityString.append("{\n");
entityString.append(std::format("\"origin\" \"{}\"\n", origin));
entityString.append(std::format("\"angles\" \"{}\"\n", angles));
entityString.append(std::format("\"classname\" \"{}\"\n", spawnpointNames[k]));
entityString.append(std::format("\"classname\" \"{}\"\n", spawnpointNames[nameIdx]));
entityString.append("}\n");
}
}
}
std::string loadMapEnts()
{
}
}
namespace BSP
@@ -71,58 +76,65 @@ namespace BSP
AssetCreationResult MapEntsLinker::linkMapEnts(BSPData* bsp)
{
std::string entityString;
try
{
json entJs;
std::string entityFilePath = BSPUtil::getFileNameForBSPAsset("entities.json");
const auto entFile = m_search_path.Open(entityFilePath);
if (!entFile.IsOpen())
{
con::warn("Can't find entity file {}, using default entities instead", entityFilePath);
entJs = json::parse(BSPLinkingConstants::DEFAULT_MAP_ENTS_STRING);
}
else
{
entJs = json::parse(*entFile.m_stream);
}
std::string entityString;
if (!parseMapEntsJSON(entJs["entities"], entityString))
return AssetCreationResult::Failure();
json entJs;
std::string entityFilePath = BSPUtil::getFileNameForBSPAsset("entities.json");
const auto entFile = m_search_path.Open(entityFilePath);
if (!entFile.IsOpen())
{
con::warn("Can't find entity file {}, using default entities instead", entityFilePath);
entJs = json::parse(BSPLinkingConstants::DEFAULT_MAP_ENTS_STRING);
json spawnJs;
std::string spawnFilePath = BSPUtil::getFileNameForBSPAsset("spawns.json");
const auto spawnFile = m_search_path.Open(spawnFilePath);
if (!spawnFile.IsOpen())
{
con::warn("Cant find spawn file {}, setting spawns to 0 0 0", spawnFilePath);
spawnJs = json::parse(BSPLinkingConstants::DEFAULT_SPAWN_POINT_STRING);
}
else
{
spawnJs = json::parse(*spawnFile.m_stream);
}
size_t defenderNameCount = std::extent<decltype(BSPGameConstants::DEFENDER_SPAWN_POINT_NAMES)>::value;
size_t attackerNameCount = std::extent<decltype(BSPGameConstants::ATTACKER_SPAWN_POINT_NAMES)>::value;
size_t ffaNameCount = std::extent<decltype(BSPGameConstants::FFA_SPAWN_POINT_NAMES)>::value;
parseSpawnpointJSON(spawnJs["attackers"], entityString, BSPGameConstants::DEFENDER_SPAWN_POINT_NAMES, defenderNameCount);
parseSpawnpointJSON(spawnJs["defenders"], entityString, BSPGameConstants::ATTACKER_SPAWN_POINT_NAMES, attackerNameCount);
parseSpawnpointJSON(spawnJs["FFA"], entityString, BSPGameConstants::FFA_SPAWN_POINT_NAMES, ffaNameCount);
MapEnts* mapEnts = m_memory.Alloc<MapEnts>();
mapEnts->name = m_memory.Dup(bsp->bspName.c_str());
mapEnts->entityString = m_memory.Dup(entityString.c_str());
mapEnts->numEntityChars = entityString.length() + 1; // numEntityChars includes the null character
// don't need these
mapEnts->trigger.count = 0;
mapEnts->trigger.models = nullptr;
mapEnts->trigger.hullCount = 0;
mapEnts->trigger.hulls = nullptr;
mapEnts->trigger.slabCount = 0;
mapEnts->trigger.slabs = nullptr;
auto mapEntsAsset = m_context.AddAsset<AssetMapEnts>(mapEnts->name, mapEnts);
return AssetCreationResult::Success(mapEntsAsset);
}
else
catch (const json::exception& e)
{
entJs = json::parse(*entFile.m_stream);
}
if (!parseMapEntsJSON(entJs["entities"], entityString))
con::error("JSON error when parsing map ents and spawns: {}", e.what());
return AssetCreationResult::Failure();
json spawnJs;
std::string spawnFilePath = BSPUtil::getFileNameForBSPAsset("spawns.json");
const auto spawnFile = m_search_path.Open(spawnFilePath);
if (!spawnFile.IsOpen())
{
con::warn("Cant find spawn file {}, setting spawns to 0 0 0", spawnFilePath);
spawnJs = json::parse(BSPLinkingConstants::DEFAULT_SPAWN_POINT_STRING);
}
else
{
spawnJs = json::parse(*spawnFile.m_stream);
}
int defenderNameCount = std::extent<decltype(BSPGameConstants::DEFENDER_SPAWN_POINT_NAMES)>::value;
int attackerNameCount = std::extent<decltype(BSPGameConstants::ATTACKER_SPAWN_POINT_NAMES)>::value;
int ffaNameCount = std::extent<decltype(BSPGameConstants::FFA_SPAWN_POINT_NAMES)>::value;
parseSpawnpointJSON(spawnJs["attackers"], entityString, BSPGameConstants::DEFENDER_SPAWN_POINT_NAMES, defenderNameCount);
parseSpawnpointJSON(spawnJs["defenders"], entityString, BSPGameConstants::ATTACKER_SPAWN_POINT_NAMES, attackerNameCount);
parseSpawnpointJSON(spawnJs["FFA"], entityString, BSPGameConstants::FFA_SPAWN_POINT_NAMES, ffaNameCount);
MapEnts* mapEnts = m_memory.Alloc<MapEnts>();
mapEnts->name = m_memory.Dup(bsp->bspName.c_str());
mapEnts->entityString = m_memory.Dup(entityString.c_str());
mapEnts->numEntityChars = entityString.length() + 1; // numEntityChars includes the null character
// don't need these
mapEnts->trigger.count = 0;
mapEnts->trigger.models = nullptr;
mapEnts->trigger.hullCount = 0;
mapEnts->trigger.hulls = nullptr;
mapEnts->trigger.slabCount = 0;
mapEnts->trigger.slabs = nullptr;
auto mapEntsAsset = m_context.AddAsset<AssetMapEnts>(mapEnts->name, mapEnts);
return AssetCreationResult::Success(mapEntsAsset);
}
}