mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-11-23 13:12:06 +00:00
WIP: Converted custom map linker into different asset files
This commit is contained in:
128
src/ObjLoading/Game/T6/BSP/Linker/MapEntsLinker.cpp
Normal file
128
src/ObjLoading/Game/T6/BSP/Linker/MapEntsLinker.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include "MapEntsLinker.h"
|
||||
#include "../BSPUtil.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
using namespace nlohmann;
|
||||
|
||||
namespace
|
||||
{
|
||||
bool parseMapEntsJSON(json& entArrayJs, std::string& entityString)
|
||||
{
|
||||
int entityCount = entArrayJs.size();
|
||||
for (int i = 0; i < entityCount; i++)
|
||||
{
|
||||
auto currEntity = entArrayJs[i];
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
std::string className = currEntity["classname"];
|
||||
if (className.compare("worldspawn") != 0)
|
||||
{
|
||||
con::error("ERROR: first entity in the map entity string must be the worldspawn class!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
entityString.append("{\n");
|
||||
|
||||
for (auto& element : currEntity.items())
|
||||
{
|
||||
std::string key = element.key();
|
||||
std::string value = element.value();
|
||||
entityString.append(std::format("\"{}\" \"{}\"\n", key, value));
|
||||
}
|
||||
|
||||
entityString.append("}\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void parseSpawnpointJSON(json& entArrayJs, std::string& entityString, const char* spawnpointNames[], int nameCount)
|
||||
{
|
||||
int entityCount = entArrayJs.size();
|
||||
for (int i = 0; i < entityCount; i++)
|
||||
{
|
||||
auto currEntity = entArrayJs[i];
|
||||
|
||||
std::string origin = currEntity["origin"];
|
||||
std::string angles = currEntity["angles"];
|
||||
|
||||
for (int k = 0; k < nameCount; k++)
|
||||
{
|
||||
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("}\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace BSP
|
||||
{
|
||||
MapEntsLinker::MapEntsLinker(MemoryManager& memory, ISearchPath& searchPath, AssetCreationContext& context)
|
||||
: m_memory(memory),
|
||||
m_search_path(searchPath),
|
||||
m_context(context)
|
||||
{
|
||||
}
|
||||
|
||||
AssetCreationResult MapEntsLinker::linkMapEnts(BSPData* bsp)
|
||||
{
|
||||
std::string entityString;
|
||||
|
||||
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);
|
||||
}
|
||||
if (!parseMapEntsJSON(entJs["entities"], entityString))
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user