mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 05:13:58 -07:00
Delete GetCivData from MapGeneratorWorker.cpp which is redundant with the ReadJSONFile from 871ed04521.
Unify civ file loading from gui/common/functions_civinfo.js and
rmgen/library.js in globalscripts/Templates.js.
Delete the two forgotton headers in 4275a8a33c.
Refs #4868, #4804, D900.
Differential Revision: https://code.wildfiregames.com/D1062
Discussed with: leper
This was SVN commit r20528.
This commit is contained in:
parent
ba8d1c6166
commit
89055ef858
14 changed files with 47 additions and 91 deletions
|
|
@ -1,3 +1,33 @@
|
|||
/**
|
||||
* Loads history and gameplay data of all civs.
|
||||
* Can be used from GUI and rmgen (because the simulation functions differ currently).
|
||||
*
|
||||
* @param selectableOnly {boolean} - Only load civs that can be selected
|
||||
* in the gamesetup. Scenario maps might set non-selectable civs.
|
||||
*/
|
||||
function loadCivFiles(selectableOnly)
|
||||
{
|
||||
let propertyNames = [
|
||||
"Code", "Culture", "Name", "Emblem", "History", "Music", "Factions", "CivBonuses", "TeamBonuses",
|
||||
"Structures", "StartEntities", "Formations", "AINames", "SkirmishReplacements", "SelectableInGameSetup"];
|
||||
|
||||
let civData = {};
|
||||
|
||||
for (let filename of Engine.BuildDirEntList("simulation/data/civs/", "*.json", false))
|
||||
{
|
||||
let data = Engine.ReadJSONFile(filename);
|
||||
|
||||
for (let prop of propertyNames)
|
||||
if (data[prop] === undefined)
|
||||
throw new Error(filename + " doesn't contain " + prop);
|
||||
|
||||
if (!selectableOnly || data.SelectableInGameSetup)
|
||||
civData[data.Code] = data;
|
||||
}
|
||||
|
||||
return civData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of all classes for this identity template
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
<objects>
|
||||
|
||||
<script file="gui/common/functions_civinfo.js"/>
|
||||
<script file="gui/common/functions_global_object.js"/>
|
||||
<script file="gui/common/functions_utility.js"/>
|
||||
<script file="gui/civinfo/civinfo.js"/>
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
/**
|
||||
* Loads history and some gameplay data for civs.
|
||||
*
|
||||
* @param selectableOnly {boolean} - Only load those which can be selected
|
||||
* in the gamesetup. Scenario maps might set non-selectable civs.
|
||||
* @param gaia {boolean} - Whether to include gaia as a mock civ.
|
||||
*/
|
||||
function loadCivData(selectableOnly, gaia)
|
||||
{
|
||||
let civData = {};
|
||||
let civFiles = Engine.BuildDirEntList("simulation/data/civs/", "*.json", false);
|
||||
|
||||
for (let filename of civFiles)
|
||||
{
|
||||
let data = Engine.ReadJSONFile(filename);
|
||||
if (!data)
|
||||
continue;
|
||||
|
||||
translateObjectKeys(data, ["Name", "Description", "History", "Special"]);
|
||||
if (!selectableOnly || data.SelectableInGameSetup)
|
||||
civData[data.Code] = data;
|
||||
|
||||
// Sanity check
|
||||
for (let prop of ["Code", "Culture", "Name", "Emblem", "History", "Music", "Factions", "CivBonuses",
|
||||
"TeamBonuses", "Structures", "StartEntities", "Formations", "AINames", "SelectableInGameSetup"])
|
||||
if (data[prop] == undefined)
|
||||
error(filename + " doesn't contain " + prop);
|
||||
}
|
||||
|
||||
if (gaia)
|
||||
civData.gaia = { "Code": "gaia", "Name": translate("Gaia") };
|
||||
|
||||
return deepfreeze(civData);
|
||||
}
|
||||
|
|
@ -37,6 +37,21 @@ function getJSONFileList(pathname)
|
|||
filename => filename.substring(pathname.length, filename.length - 5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns translated history and gameplay data of all civs, optionally including a mock gaia civ.
|
||||
*/
|
||||
function loadCivData(selectableOnly, gaia)
|
||||
{
|
||||
let civData = loadCivFiles(selectableOnly);
|
||||
|
||||
translateObjectKeys(civData, ["Name", "Description", "History", "Special"]);
|
||||
|
||||
if (gaia)
|
||||
civData.gaia = { "Code": "gaia", "Name": translate("Gaia") };
|
||||
|
||||
return deepfreeze(civData);
|
||||
}
|
||||
|
||||
// A sorting function for arrays of objects with 'name' properties, ignoring case
|
||||
function sortNameIgnoreCase(x, y)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
<objects>
|
||||
|
||||
<script file="gui/common/color.js"/>
|
||||
<script file="gui/common/functions_civinfo.js"/>
|
||||
<script file="gui/common/functions_global_object.js"/>
|
||||
<script file="gui/common/functions_utility.js"/>
|
||||
<script file="gui/common/gamedescription.js"/>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<objects>
|
||||
<script file="gui/common/functions_civinfo.js"/>
|
||||
<script file="gui/common/functions_global_object.js"/>
|
||||
<script file="gui/common/functions_utility.js"/>
|
||||
<script file="gui/common/tooltips.js"/>
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
<!-- Used to display game info. -->
|
||||
<script file="gui/common/color.js" />
|
||||
<script file="gui/common/functions_civinfo.js" />
|
||||
<script file="gui/common/functions_utility.js" />
|
||||
<script file="gui/common/gamedescription.js"/>
|
||||
<script file="gui/common/settings.js" />
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
<objects>
|
||||
|
||||
<script file="gui/common/color.js" />
|
||||
<script file="gui/common/functions_civinfo.js"/>
|
||||
<script file="gui/common/functions_global_object.js" />
|
||||
<script file="gui/common/functions_utility.js" />
|
||||
<script file="gui/common/functions_utility_loadsave.js" />
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
<script file="gui/common/color.js"/>
|
||||
<script file="gui/common/colorFades.js"/>
|
||||
<script file="gui/common/functions_civinfo.js"/>
|
||||
<script file="gui/common/functions_global_object.js"/>
|
||||
<script file="gui/common/functions_utility.js"/>
|
||||
<script file="gui/common/gamedescription.js"/>
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
<objects>
|
||||
<script file="gui/common/functions_global_object.js"/>
|
||||
<script file="gui/common/functions_civinfo.js"/>
|
||||
<script file="gui/common/functions_utility.js"/>
|
||||
<script file="gui/common/gamedescription.js"/>
|
||||
<script file="gui/common/settings.js"/>
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ const MAX_HEIGHT = MAX_HEIGHT_RANGE - SEA_LEVEL;
|
|||
// Default angle for buildings
|
||||
const BUILDING_ORIENTATION = - PI / 4;
|
||||
|
||||
const g_CivData = deepfreeze(loadCivFiles(false));
|
||||
|
||||
function fractionToTiles(f)
|
||||
{
|
||||
return g_Map.size * f;
|
||||
|
|
|
|||
|
|
@ -12,21 +12,8 @@ var g_Camera = {
|
|||
"Declination": 0.523599
|
||||
};
|
||||
|
||||
var g_CivData = {};
|
||||
|
||||
function InitMap()
|
||||
{
|
||||
// Get civ data as array of JSON strings
|
||||
var data = Engine.GetCivData();
|
||||
if (!data || !data.length)
|
||||
throw new Error("InitMapGen: error reading civ data");
|
||||
|
||||
for (var i = 0; i < data.length; ++i)
|
||||
{
|
||||
var civData = JSON.parse(data[i]);
|
||||
g_CivData[civData.Code] = civData;
|
||||
}
|
||||
|
||||
log("Creating new map...");
|
||||
g_Map = new Map(g_MapSettings.Size, g_MapSettings.BaseHeight);
|
||||
initTerrain(g_MapSettings.BaseTerrain);
|
||||
|
|
|
|||
|
|
@ -105,7 +105,6 @@ bool CMapGeneratorWorker::Run()
|
|||
m_ScriptInterface->RegisterFunction<void, JS::HandleValue, CMapGeneratorWorker::ExportMap>("ExportMap");
|
||||
m_ScriptInterface->RegisterFunction<void, int, CMapGeneratorWorker::SetProgress>("SetProgress");
|
||||
m_ScriptInterface->RegisterFunction<void, CMapGeneratorWorker::MaybeGC>("MaybeGC");
|
||||
m_ScriptInterface->RegisterFunction<std::vector<std::string>, CMapGeneratorWorker::GetCivData>("GetCivData");
|
||||
m_ScriptInterface->RegisterFunction<CParamNode, std::string, CMapGeneratorWorker::GetTemplate>("GetTemplate");
|
||||
m_ScriptInterface->RegisterFunction<bool, std::string, CMapGeneratorWorker::TemplateExists>("TemplateExists");
|
||||
m_ScriptInterface->RegisterFunction<std::vector<std::string>, std::string, bool, CMapGeneratorWorker::FindTemplates>("FindTemplates");
|
||||
|
|
@ -197,39 +196,6 @@ void CMapGeneratorWorker::MaybeGC(ScriptInterface::CxPrivate* pCxPrivate)
|
|||
self->m_ScriptInterface->MaybeGC();
|
||||
}
|
||||
|
||||
std::vector<std::string> CMapGeneratorWorker::GetCivData(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
||||
{
|
||||
VfsPath path(L"simulation/data/civs/");
|
||||
VfsPaths pathnames;
|
||||
|
||||
std::vector<std::string> data;
|
||||
|
||||
// Load all JSON files in civs directory
|
||||
Status ret = vfs::GetPathnames(g_VFS, path, L"*.json", pathnames);
|
||||
if (ret == INFO::OK)
|
||||
{
|
||||
for (const VfsPath& p : pathnames)
|
||||
{
|
||||
// Load JSON file
|
||||
CVFSFile file;
|
||||
PSRETURN ret = file.Load(g_VFS, p);
|
||||
if (ret != PSRETURN_OK)
|
||||
LOGERROR("CMapGeneratorWorker::GetCivData: Failed to load file '%s': %s", p.string8(), GetErrorString(ret));
|
||||
else
|
||||
data.push_back(file.DecodeUTF8()); // assume it's UTF-8
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Some error reading directory
|
||||
wchar_t error[200];
|
||||
LOGERROR("CMapGeneratorWorker::GetCivData: Error reading directory '%s': %s", path.string8(), utf8_from_wstring(StatusDescription(ret, error, ARRAY_SIZE(error))));
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
CParamNode CMapGeneratorWorker::GetTemplate(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName)
|
||||
{
|
||||
CMapGeneratorWorker* self = static_cast<CMapGeneratorWorker*>(pCxPrivate->pCBData);
|
||||
|
|
|
|||
|
|
@ -125,9 +125,6 @@ private:
|
|||
static void ExportMap(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue data);
|
||||
static void SetProgress(ScriptInterface::CxPrivate* pCxPrivate, int progress);
|
||||
static void MaybeGC(ScriptInterface::CxPrivate* pCxPrivate);
|
||||
static bool FileExists(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath);
|
||||
static JS::Value ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath);
|
||||
static std::vector<std::string> GetCivData(ScriptInterface::CxPrivate* pCxPrivate);
|
||||
static CParamNode GetTemplate(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName);
|
||||
static bool TemplateExists(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName);
|
||||
static std::vector<std::string> FindTemplates(ScriptInterface::CxPrivate* pCxPrivate, const std::string& path, bool includeSubdirectories);
|
||||
|
|
|
|||
Loading…
Reference in a new issue