Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/Simulation2.cpp
Show First 20 Lines • Show All 988 Lines • ▼ Show 20 Lines | std::string CSimulation2::GetAIData() | ||||
// Build single JSON string with array of AI data | // Build single JSON string with array of AI data | ||||
JS::RootedValue ais(rq.cx); | JS::RootedValue ais(rq.cx); | ||||
if (!Script::CreateObject(rq, &ais, "AIData", aiData)) | if (!Script::CreateObject(rq, &ais, "AIData", aiData)) | ||||
return std::string(); | return std::string(); | ||||
return Script::StringifyJSON(rq, &ais); | return Script::StringifyJSON(rq, &ais); | ||||
} | } | ||||
std::vector<std::string> CSimulation2::GetBiomes(const std::wstring& mapName) | |||||
{ | |||||
const ScriptInterface& scriptInterface = GetScriptInterface(); | |||||
ScriptRequest rq(scriptInterface); | |||||
std::vector<std::string> biomesNormalized; | |||||
phosit: I would declare this right bevore the for loop and `return {}` in the other cases. | |||||
VfsPath mapFile = L"maps/random/" + mapName + L".json"; | |||||
JS::RootedValue mapData(rq.cx); | |||||
JS::RootedValue mapSettings(rq.cx); | |||||
JS::RootedValue supportedBiomes(rq.cx); | |||||
Script::ReadJSONFile(rq, mapFile, &mapData); | |||||
if (!Script::GetProperty(rq, mapData, "settings", &mapSettings)) | |||||
{ | |||||
LOGERROR("Error parsing map data for %s", utf8_from_wstring(mapName)); | |||||
return biomesNormalized; | |||||
} | |||||
// SupportedBiomes may or may not be defined | |||||
if (!Script::GetProperty(rq, mapSettings, "SupportedBiomes", &supportedBiomes) || supportedBiomes.isUndefined()) | |||||
return biomesNormalized; | |||||
if (supportedBiomes.isString()) | |||||
{ | |||||
std::wstring supportedBiomesDirectory; | |||||
Script::FromJSVal(rq, supportedBiomes, supportedBiomesDirectory); | |||||
VfsPath biomesPath = L"maps/random/rmbiome/" + supportedBiomesDirectory; | |||||
VfsPaths biomesList; | |||||
if (vfs::GetPathnames(g_VFS, biomesPath, L"*.json", biomesList) == INFO::OK) | |||||
for (const VfsPath& biome : biomesList) | |||||
biomesNormalized.push_back(utf8_from_wstring(supportedBiomesDirectory + biome.Basename().string())); | |||||
else | |||||
LOGERROR("Error reading biome files in %s", biomesPath.string8()); | |||||
return biomesNormalized; | |||||
} | |||||
if (!Script::FromJSVal_vector(rq, supportedBiomes, biomesNormalized)) | |||||
LOGERROR("Error converting SupportedBiomes array"); | |||||
return biomesNormalized; | |||||
} |
Wildfire Games · Phabricator
I would declare this right bevore the for loop and return {} in the other cases.