Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/GameSetup/GameSetup.cpp
Show First 20 Lines • Show All 178 Lines • ▼ Show 20 Lines | |||||
//---------------------------------------------------------------------------- | //---------------------------------------------------------------------------- | ||||
// GUI integration | // GUI integration | ||||
//---------------------------------------------------------------------------- | //---------------------------------------------------------------------------- | ||||
// display progress / description in loading screen | // display progress / description in loading screen | ||||
void GUI_DisplayLoadProgress(int percent, const wchar_t* pending_task) | void GUI_DisplayLoadProgress(int percent, const wchar_t* pending_task) | ||||
{ | { | ||||
const ScriptInterface& scriptInterface = *(g_GUI->GetActiveGUI()->GetScriptInterface()); | const ScriptInterface& scriptInterface = *(g_GUI->GetActiveGUI()->GetScriptInterface()); | ||||
JSContext* cx = scriptInterface.GetContext(); | ScriptInterface::Request rq(scriptInterface); | ||||
JSAutoRequest rq(cx); | |||||
JS::AutoValueVector paramData(cx); | JS::AutoValueVector paramData(rq.cx); | ||||
paramData.append(JS::NumberValue(percent)); | paramData.append(JS::NumberValue(percent)); | ||||
JS::RootedValue valPendingTask(cx); | JS::RootedValue valPendingTask(rq.cx); | ||||
scriptInterface.ToJSVal(cx, &valPendingTask, pending_task); | scriptInterface.ToJSVal(rq, &valPendingTask, pending_task); | ||||
paramData.append(valPendingTask); | paramData.append(valPendingTask); | ||||
g_GUI->SendEventToAll(g_EventNameGameLoadProgress, paramData); | g_GUI->SendEventToAll(g_EventNameGameLoadProgress, paramData); | ||||
} | } | ||||
bool ShouldRender() | bool ShouldRender() | ||||
{ | { | ||||
return !g_app_minimized && (g_app_has_focus || !g_VideoMode.IsInFullscreen()); | return !g_app_minimized && (g_app_has_focus || !g_VideoMode.IsInFullscreen()); | ||||
▲ Show 20 Lines • Show All 308 Lines • ▼ Show 20 Lines | static void InitPs(bool setup_gui, const CStrW& gui_page, ScriptInterface* srcScriptInterface, JS::HandleValue initData) | ||||
// GUI uses VFS, so this must come after VFS init. | // GUI uses VFS, so this must come after VFS init. | ||||
g_GUI->SwitchPage(gui_page, srcScriptInterface, initData); | g_GUI->SwitchPage(gui_page, srcScriptInterface, initData); | ||||
} | } | ||||
void InitPsAutostart(bool networked, JS::HandleValue attrs) | void InitPsAutostart(bool networked, JS::HandleValue attrs) | ||||
{ | { | ||||
// The GUI has not been initialized yet, so use the simulation scriptinterface for this variable | // The GUI has not been initialized yet, so use the simulation scriptinterface for this variable | ||||
ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | ScriptInterface::Request rq(scriptInterface); | ||||
JSAutoRequest rq(cx); | |||||
JS::RootedValue playerAssignments(cx); | JS::RootedValue playerAssignments(rq.cx); | ||||
ScriptInterface::CreateObject(cx, &playerAssignments); | ScriptInterface::CreateObject(rq, &playerAssignments); | ||||
if (!networked) | if (!networked) | ||||
{ | { | ||||
JS::RootedValue localPlayer(cx); | JS::RootedValue localPlayer(rq.cx); | ||||
ScriptInterface::CreateObject(cx, &localPlayer, "player", g_Game->GetPlayerID()); | ScriptInterface::CreateObject(rq, &localPlayer, "player", g_Game->GetPlayerID()); | ||||
scriptInterface.SetProperty(playerAssignments, "local", localPlayer); | scriptInterface.SetProperty(playerAssignments, "local", localPlayer); | ||||
} | } | ||||
JS::RootedValue sessionInitData(cx); | JS::RootedValue sessionInitData(rq.cx); | ||||
ScriptInterface::CreateObject( | ScriptInterface::CreateObject( | ||||
cx, | rq, | ||||
&sessionInitData, | &sessionInitData, | ||||
"attribs", attrs, | "attribs", attrs, | ||||
"playerAssignments", playerAssignments); | "playerAssignments", playerAssignments); | ||||
InitPs(true, L"page_loading.xml", &scriptInterface, sessionInitData); | InitPs(true, L"page_loading.xml", &scriptInterface, sessionInitData); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 511 Lines • ▼ Show 20 Lines | void InitGraphics(const CmdLineArgs& args, int flags, const std::vector<CStr>& installedMods) | ||||
try | try | ||||
{ | { | ||||
if (!AutostartVisualReplay(args.Get("replay-visual")) && !Autostart(args)) | if (!AutostartVisualReplay(args.Get("replay-visual")) && !Autostart(args)) | ||||
{ | { | ||||
const bool setup_gui = ((flags & INIT_NO_GUI) == 0); | const bool setup_gui = ((flags & INIT_NO_GUI) == 0); | ||||
// We only want to display the splash screen at startup | // We only want to display the splash screen at startup | ||||
shared_ptr<ScriptInterface> scriptInterface = g_GUI->GetScriptInterface(); | shared_ptr<ScriptInterface> scriptInterface = g_GUI->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface->GetContext(); | ScriptInterface::Request rq(scriptInterface); | ||||
JSAutoRequest rq(cx); | JS::RootedValue data(rq.cx); | ||||
JS::RootedValue data(cx); | |||||
if (g_GUI) | if (g_GUI) | ||||
{ | { | ||||
ScriptInterface::CreateObject(cx, &data, "isStartup", true); | ScriptInterface::CreateObject(rq, &data, "isStartup", true); | ||||
if (!installedMods.empty()) | if (!installedMods.empty()) | ||||
scriptInterface->SetProperty(data, "installedMods", installedMods); | scriptInterface->SetProperty(data, "installedMods", installedMods); | ||||
} | } | ||||
InitPs(setup_gui, installedMods.empty() ? L"page_pregame.xml" : L"page_modmod.xml", g_GUI->GetScriptInterface().get(), data); | InitPs(setup_gui, installedMods.empty() ? L"page_pregame.xml" : L"page_modmod.xml", g_GUI->GetScriptInterface().get(), data); | ||||
} | } | ||||
} | } | ||||
catch (PSERROR_Game_World_MapLoadFailed& e) | catch (PSERROR_Game_World_MapLoadFailed& e) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 138 Lines • ▼ Show 20 Lines | bool Autostart(const CmdLineArgs& args) | ||||
CStr autoStartName = args.Get("autostart"); | CStr autoStartName = args.Get("autostart"); | ||||
if (autoStartName.empty()) | if (autoStartName.empty()) | ||||
return false; | return false; | ||||
g_Game = new CGame(!args.Has("autostart-disable-replay")); | g_Game = new CGame(!args.Has("autostart-disable-replay")); | ||||
ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | ScriptInterface::Request rq(scriptInterface); | ||||
JSAutoRequest rq(cx); | |||||
JS::RootedValue attrs(cx); | JS::RootedValue attrs(rq.cx); | ||||
JS::RootedValue settings(cx); | JS::RootedValue settings(rq.cx); | ||||
JS::RootedValue playerData(cx); | JS::RootedValue playerData(rq.cx); | ||||
ScriptInterface::CreateObject(cx, &attrs); | ScriptInterface::CreateObject(rq, &attrs); | ||||
ScriptInterface::CreateObject(cx, &settings); | ScriptInterface::CreateObject(rq, &settings); | ||||
ScriptInterface::CreateArray(cx, &playerData); | ScriptInterface::CreateArray(rq, &playerData); | ||||
// The directory in front of the actual map name indicates which type | // The directory in front of the actual map name indicates which type | ||||
// of map is being loaded. Drawback of this approach is the association | // of map is being loaded. Drawback of this approach is the association | ||||
// of map types and folders is hard-coded, but benefits are: | // of map types and folders is hard-coded, but benefits are: | ||||
// - No need to pass the map type via command line separately | // - No need to pass the map type via command line separately | ||||
// - Prevents mixing up of scenarios and skirmish maps to some degree | // - Prevents mixing up of scenarios and skirmish maps to some degree | ||||
Path mapPath = Path(autoStartName); | Path mapPath = Path(autoStartName); | ||||
std::wstring mapDirectory = mapPath.Parent().Filename().string(); | std::wstring mapDirectory = mapPath.Parent().Filename().string(); | ||||
std::string mapType; | std::string mapType; | ||||
if (mapDirectory == L"random") | if (mapDirectory == L"random") | ||||
{ | { | ||||
// Random map definition will be loaded from JSON file, so we need to parse it | // Random map definition will be loaded from JSON file, so we need to parse it | ||||
std::wstring scriptPath = L"maps/" + autoStartName.FromUTF8() + L".json"; | std::wstring scriptPath = L"maps/" + autoStartName.FromUTF8() + L".json"; | ||||
JS::RootedValue scriptData(cx); | JS::RootedValue scriptData(rq.cx); | ||||
scriptInterface.ReadJSONFile(scriptPath, &scriptData); | scriptInterface.ReadJSONFile(scriptPath, &scriptData); | ||||
if (!scriptData.isUndefined() && scriptInterface.GetProperty(scriptData, "settings", &settings)) | if (!scriptData.isUndefined() && scriptInterface.GetProperty(scriptData, "settings", &settings)) | ||||
{ | { | ||||
// JSON loaded ok - copy script name over to game attributes | // JSON loaded ok - copy script name over to game attributes | ||||
std::wstring scriptFile; | std::wstring scriptFile; | ||||
scriptInterface.GetProperty(settings, "Script", scriptFile); | scriptInterface.GetProperty(settings, "Script", scriptFile); | ||||
scriptInterface.SetProperty(attrs, "script", scriptFile); // RMS filename | scriptInterface.SetProperty(attrs, "script", scriptFile); // RMS filename | ||||
} | } | ||||
Show All 20 Lines | if (mapDirectory == L"random") | ||||
{ | { | ||||
CStr num = args.Get("autostart-players"); | CStr num = args.Get("autostart-players"); | ||||
numPlayers = num.ToUInt(); | numPlayers = num.ToUInt(); | ||||
} | } | ||||
// Set up player data | // Set up player data | ||||
for (size_t i = 0; i < numPlayers; ++i) | for (size_t i = 0; i < numPlayers; ++i) | ||||
{ | { | ||||
JS::RootedValue player(cx); | JS::RootedValue player(rq.cx); | ||||
// We could load player_defaults.json here, but that would complicate the logic | // We could load player_defaults.json here, but that would complicate the logic | ||||
// even more and autostart is only intended for developers anyway | // even more and autostart is only intended for developers anyway | ||||
ScriptInterface::CreateObject(cx, &player, "Civ", "athen"); | ScriptInterface::CreateObject(rq, &player, "Civ", "athen"); | ||||
scriptInterface.SetPropertyInt(playerData, i, player); | scriptInterface.SetPropertyInt(playerData, i, player); | ||||
} | } | ||||
mapType = "random"; | mapType = "random"; | ||||
} | } | ||||
else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// Initialize general settings from the map data so some values | // Initialize general settings from the map data so some values | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | else | ||||
aiseed = seedArg.ToULong(); | aiseed = seedArg.ToULong(); | ||||
} | } | ||||
scriptInterface.SetProperty(settings, "AISeed", aiseed); | scriptInterface.SetProperty(settings, "AISeed", aiseed); | ||||
// Set player data for AIs | // Set player data for AIs | ||||
// attrs.settings = { PlayerData: [ { AI: ... }, ... ] } | // attrs.settings = { PlayerData: [ { AI: ... }, ... ] } | ||||
// or = { PlayerData: [ null, { AI: ... }, ... ] } when gaia set | // or = { PlayerData: [ null, { AI: ... }, ... ] } when gaia set | ||||
int offset = 1; | int offset = 1; | ||||
JS::RootedValue player(cx); | JS::RootedValue player(rq.cx); | ||||
if (scriptInterface.GetPropertyInt(playerData, 0, &player) && player.isNull()) | if (scriptInterface.GetPropertyInt(playerData, 0, &player) && player.isNull()) | ||||
offset = 0; | offset = 0; | ||||
// Set teams | // Set teams | ||||
if (args.Has("autostart-team")) | if (args.Has("autostart-team")) | ||||
{ | { | ||||
std::vector<CStr> civArgs = args.GetMultiple("autostart-team"); | std::vector<CStr> civArgs = args.GetMultiple("autostart-team"); | ||||
for (size_t i = 0; i < civArgs.size(); ++i) | for (size_t i = 0; i < civArgs.size(); ++i) | ||||
{ | { | ||||
int playerID = civArgs[i].BeforeFirst(":").ToInt(); | int playerID = civArgs[i].BeforeFirst(":").ToInt(); | ||||
// Instead of overwriting existing player data, modify the array | // Instead of overwriting existing player data, modify the array | ||||
JS::RootedValue player(cx); | JS::RootedValue player(rq.cx); | ||||
if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | ||||
{ | { | ||||
if (mapDirectory == L"skirmishes") | if (mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// playerID is certainly bigger than this map player number | // playerID is certainly bigger than this map player number | ||||
LOGWARNING("Autostart: Invalid player %d in autostart-team option", playerID); | LOGWARNING("Autostart: Invalid player %d in autostart-team option", playerID); | ||||
continue; | continue; | ||||
} | } | ||||
ScriptInterface::CreateObject(cx, &player); | ScriptInterface::CreateObject(rq, &player); | ||||
} | } | ||||
int teamID = civArgs[i].AfterFirst(":").ToInt() - 1; | int teamID = civArgs[i].AfterFirst(":").ToInt() - 1; | ||||
scriptInterface.SetProperty(player, "Team", teamID); | scriptInterface.SetProperty(player, "Team", teamID); | ||||
scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | ||||
} | } | ||||
} | } | ||||
int ceasefire = 0; | int ceasefire = 0; | ||||
if (args.Has("autostart-ceasefire")) | if (args.Has("autostart-ceasefire")) | ||||
ceasefire = args.Get("autostart-ceasefire").ToInt(); | ceasefire = args.Get("autostart-ceasefire").ToInt(); | ||||
scriptInterface.SetProperty(settings, "Ceasefire", ceasefire); | scriptInterface.SetProperty(settings, "Ceasefire", ceasefire); | ||||
if (args.Has("autostart-ai")) | if (args.Has("autostart-ai")) | ||||
{ | { | ||||
std::vector<CStr> aiArgs = args.GetMultiple("autostart-ai"); | std::vector<CStr> aiArgs = args.GetMultiple("autostart-ai"); | ||||
for (size_t i = 0; i < aiArgs.size(); ++i) | for (size_t i = 0; i < aiArgs.size(); ++i) | ||||
{ | { | ||||
int playerID = aiArgs[i].BeforeFirst(":").ToInt(); | int playerID = aiArgs[i].BeforeFirst(":").ToInt(); | ||||
// Instead of overwriting existing player data, modify the array | // Instead of overwriting existing player data, modify the array | ||||
JS::RootedValue player(cx); | JS::RootedValue player(rq.cx); | ||||
if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | ||||
{ | { | ||||
if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// playerID is certainly bigger than this map player number | // playerID is certainly bigger than this map player number | ||||
LOGWARNING("Autostart: Invalid player %d in autostart-ai option", playerID); | LOGWARNING("Autostart: Invalid player %d in autostart-ai option", playerID); | ||||
continue; | continue; | ||||
} | } | ||||
ScriptInterface::CreateObject(cx, &player); | ScriptInterface::CreateObject(rq, &player); | ||||
} | } | ||||
scriptInterface.SetProperty(player, "AI", aiArgs[i].AfterFirst(":")); | scriptInterface.SetProperty(player, "AI", aiArgs[i].AfterFirst(":")); | ||||
scriptInterface.SetProperty(player, "AIDiff", 3); | scriptInterface.SetProperty(player, "AIDiff", 3); | ||||
scriptInterface.SetProperty(player, "AIBehavior", "balanced"); | scriptInterface.SetProperty(player, "AIBehavior", "balanced"); | ||||
scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | ||||
} | } | ||||
} | } | ||||
// Set AI difficulty | // Set AI difficulty | ||||
if (args.Has("autostart-aidiff")) | if (args.Has("autostart-aidiff")) | ||||
{ | { | ||||
std::vector<CStr> civArgs = args.GetMultiple("autostart-aidiff"); | std::vector<CStr> civArgs = args.GetMultiple("autostart-aidiff"); | ||||
for (size_t i = 0; i < civArgs.size(); ++i) | for (size_t i = 0; i < civArgs.size(); ++i) | ||||
{ | { | ||||
int playerID = civArgs[i].BeforeFirst(":").ToInt(); | int playerID = civArgs[i].BeforeFirst(":").ToInt(); | ||||
// Instead of overwriting existing player data, modify the array | // Instead of overwriting existing player data, modify the array | ||||
JS::RootedValue player(cx); | JS::RootedValue player(rq.cx); | ||||
if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | ||||
{ | { | ||||
if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// playerID is certainly bigger than this map player number | // playerID is certainly bigger than this map player number | ||||
LOGWARNING("Autostart: Invalid player %d in autostart-aidiff option", playerID); | LOGWARNING("Autostart: Invalid player %d in autostart-aidiff option", playerID); | ||||
continue; | continue; | ||||
} | } | ||||
ScriptInterface::CreateObject(cx, &player); | ScriptInterface::CreateObject(rq, &player); | ||||
} | } | ||||
scriptInterface.SetProperty(player, "AIDiff", civArgs[i].AfterFirst(":").ToInt()); | scriptInterface.SetProperty(player, "AIDiff", civArgs[i].AfterFirst(":").ToInt()); | ||||
scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | ||||
} | } | ||||
} | } | ||||
// Set player data for Civs | // Set player data for Civs | ||||
if (args.Has("autostart-civ")) | if (args.Has("autostart-civ")) | ||||
{ | { | ||||
if (mapDirectory != L"scenarios") | if (mapDirectory != L"scenarios") | ||||
{ | { | ||||
std::vector<CStr> civArgs = args.GetMultiple("autostart-civ"); | std::vector<CStr> civArgs = args.GetMultiple("autostart-civ"); | ||||
for (size_t i = 0; i < civArgs.size(); ++i) | for (size_t i = 0; i < civArgs.size(); ++i) | ||||
{ | { | ||||
int playerID = civArgs[i].BeforeFirst(":").ToInt(); | int playerID = civArgs[i].BeforeFirst(":").ToInt(); | ||||
// Instead of overwriting existing player data, modify the array | // Instead of overwriting existing player data, modify the array | ||||
JS::RootedValue player(cx); | JS::RootedValue player(rq.cx); | ||||
if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | ||||
{ | { | ||||
if (mapDirectory == L"skirmishes") | if (mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// playerID is certainly bigger than this map player number | // playerID is certainly bigger than this map player number | ||||
LOGWARNING("Autostart: Invalid player %d in autostart-civ option", playerID); | LOGWARNING("Autostart: Invalid player %d in autostart-civ option", playerID); | ||||
continue; | continue; | ||||
} | } | ||||
ScriptInterface::CreateObject(cx, &player); | ScriptInterface::CreateObject(rq, &player); | ||||
} | } | ||||
scriptInterface.SetProperty(player, "Civ", civArgs[i].AfterFirst(":")); | scriptInterface.SetProperty(player, "Civ", civArgs[i].AfterFirst(":")); | ||||
scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | ||||
} | } | ||||
} | } | ||||
else | else | ||||
LOGWARNING("Autostart: Option 'autostart-civ' is invalid for scenarios"); | LOGWARNING("Autostart: Option 'autostart-civ' is invalid for scenarios"); | ||||
} | } | ||||
// Add player data to map settings | // Add player data to map settings | ||||
scriptInterface.SetProperty(settings, "PlayerData", playerData); | scriptInterface.SetProperty(settings, "PlayerData", playerData); | ||||
// Add map settings to game attributes | // Add map settings to game attributes | ||||
scriptInterface.SetProperty(attrs, "settings", settings); | scriptInterface.SetProperty(attrs, "settings", settings); | ||||
// Get optional playername | // Get optional playername | ||||
CStrW userName = L"anonymous"; | CStrW userName = L"anonymous"; | ||||
if (args.Has("autostart-playername")) | if (args.Has("autostart-playername")) | ||||
userName = args.Get("autostart-playername").FromUTF8(); | userName = args.Get("autostart-playername").FromUTF8(); | ||||
// Add additional scripts to the TriggerScripts property | // Add additional scripts to the TriggerScripts property | ||||
std::vector<CStrW> triggerScriptsVector; | std::vector<CStrW> triggerScriptsVector; | ||||
JS::RootedValue triggerScripts(cx); | JS::RootedValue triggerScripts(rq.cx); | ||||
if (scriptInterface.HasProperty(settings, "TriggerScripts")) | if (scriptInterface.HasProperty(settings, "TriggerScripts")) | ||||
{ | { | ||||
scriptInterface.GetProperty(settings, "TriggerScripts", &triggerScripts); | scriptInterface.GetProperty(settings, "TriggerScripts", &triggerScripts); | ||||
FromJSVal_vector(cx, triggerScripts, triggerScriptsVector); | FromJSVal_vector(rq, triggerScripts, triggerScriptsVector); | ||||
} | } | ||||
if (!CRenderer::IsInitialised()) | if (!CRenderer::IsInitialised()) | ||||
{ | { | ||||
CStr nonVisualScript = "scripts/NonVisualTrigger.js"; | CStr nonVisualScript = "scripts/NonVisualTrigger.js"; | ||||
triggerScriptsVector.push_back(nonVisualScript.FromUTF8()); | triggerScriptsVector.push_back(nonVisualScript.FromUTF8()); | ||||
} | } | ||||
std::vector<CStr> victoryConditions(1, "conquest"); | std::vector<CStr> victoryConditions(1, "conquest"); | ||||
if (args.Has("autostart-victory")) | if (args.Has("autostart-victory")) | ||||
victoryConditions = args.GetMultiple("autostart-victory"); | victoryConditions = args.GetMultiple("autostart-victory"); | ||||
if (victoryConditions.size() == 1 && victoryConditions[0] == "endless") | if (victoryConditions.size() == 1 && victoryConditions[0] == "endless") | ||||
victoryConditions.clear(); | victoryConditions.clear(); | ||||
scriptInterface.SetProperty(settings, "VictoryConditions", victoryConditions); | scriptInterface.SetProperty(settings, "VictoryConditions", victoryConditions); | ||||
for (const CStr& victory : victoryConditions) | for (const CStr& victory : victoryConditions) | ||||
{ | { | ||||
JS::RootedValue scriptData(cx); | JS::RootedValue scriptData(rq.cx); | ||||
JS::RootedValue data(cx); | JS::RootedValue data(rq.cx); | ||||
JS::RootedValue victoryScripts(cx); | JS::RootedValue victoryScripts(rq.cx); | ||||
CStrW scriptPath = L"simulation/data/settings/victory_conditions/" + victory.FromUTF8() + L".json"; | CStrW scriptPath = L"simulation/data/settings/victory_conditions/" + victory.FromUTF8() + L".json"; | ||||
scriptInterface.ReadJSONFile(scriptPath, &scriptData); | scriptInterface.ReadJSONFile(scriptPath, &scriptData); | ||||
if (!scriptData.isUndefined() && scriptInterface.GetProperty(scriptData, "Data", &data) && !data.isUndefined() | if (!scriptData.isUndefined() && scriptInterface.GetProperty(scriptData, "Data", &data) && !data.isUndefined() | ||||
&& scriptInterface.GetProperty(data, "Scripts", &victoryScripts) && !victoryScripts.isUndefined()) | && scriptInterface.GetProperty(data, "Scripts", &victoryScripts) && !victoryScripts.isUndefined()) | ||||
{ | { | ||||
std::vector<CStrW> victoryScriptsVector; | std::vector<CStrW> victoryScriptsVector; | ||||
FromJSVal_vector(cx, victoryScripts, victoryScriptsVector); | FromJSVal_vector(rq, victoryScripts, victoryScriptsVector); | ||||
triggerScriptsVector.insert(triggerScriptsVector.end(), victoryScriptsVector.begin(), victoryScriptsVector.end()); | triggerScriptsVector.insert(triggerScriptsVector.end(), victoryScriptsVector.begin(), victoryScriptsVector.end()); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
LOGERROR("Autostart: Error reading victory script '%s'", utf8_from_wstring(scriptPath)); | LOGERROR("Autostart: Error reading victory script '%s'", utf8_from_wstring(scriptPath)); | ||||
throw PSERROR_Game_World_MapLoadFailed("Error reading victory script.\nCheck application log for details."); | throw PSERROR_Game_World_MapLoadFailed("Error reading victory script.\nCheck application log for details."); | ||||
} | } | ||||
} | } | ||||
ToJSVal_vector(cx, &triggerScripts, triggerScriptsVector); | ToJSVal_vector(rq, &triggerScripts, triggerScriptsVector); | ||||
scriptInterface.SetProperty(settings, "TriggerScripts", triggerScripts); | scriptInterface.SetProperty(settings, "TriggerScripts", triggerScripts); | ||||
int wonderDuration = 10; | int wonderDuration = 10; | ||||
if (args.Has("autostart-wonderduration")) | if (args.Has("autostart-wonderduration")) | ||||
wonderDuration = args.Get("autostart-wonderduration").ToInt(); | wonderDuration = args.Get("autostart-wonderduration").ToInt(); | ||||
scriptInterface.SetProperty(settings, "WonderDuration", wonderDuration); | scriptInterface.SetProperty(settings, "WonderDuration", wonderDuration); | ||||
int relicDuration = 10; | int relicDuration = 10; | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | bool AutostartVisualReplay(const std::string& replayFile) | ||||
if (!FileExists(OsPath(replayFile))) | if (!FileExists(OsPath(replayFile))) | ||||
return false; | return false; | ||||
g_Game = new CGame(false); | g_Game = new CGame(false); | ||||
g_Game->SetPlayerID(-1); | g_Game->SetPlayerID(-1); | ||||
g_Game->StartVisualReplay(replayFile); | g_Game->StartVisualReplay(replayFile); | ||||
ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | ScriptInterface::Request rq(scriptInterface); | ||||
JSAutoRequest rq(cx); | JS::RootedValue attrs(rq.cx, g_Game->GetSimulation2()->GetInitAttributes()); | ||||
JS::RootedValue attrs(cx, g_Game->GetSimulation2()->GetInitAttributes()); | |||||
InitPsAutostart(false, attrs); | InitPsAutostart(false, attrs); | ||||
return true; | return true; | ||||
} | } | ||||
void CancelLoad(const CStrW& message) | void CancelLoad(const CStrW& message) | ||||
{ | { | ||||
shared_ptr<ScriptInterface> pScriptInterface = g_GUI->GetActiveGUI()->GetScriptInterface(); | shared_ptr<ScriptInterface> pScriptInterface = g_GUI->GetActiveGUI()->GetScriptInterface(); | ||||
JSContext* cx = pScriptInterface->GetContext(); | ScriptInterface::Request rq(pScriptInterface); | ||||
JSAutoRequest rq(cx); | |||||
JS::RootedValue global(cx, pScriptInterface->GetGlobalObject()); | JS::RootedValue global(rq.cx, pScriptInterface->GetGlobalObject()); | ||||
LDR_Cancel(); | LDR_Cancel(); | ||||
if (g_GUI && | if (g_GUI && | ||||
g_GUI->GetPageCount() && | g_GUI->GetPageCount() && | ||||
pScriptInterface->HasProperty(global, "cancelOnLoadGameError")) | pScriptInterface->HasProperty(global, "cancelOnLoadGameError")) | ||||
pScriptInterface->CallFunctionVoid(global, "cancelOnLoadGameError", message); | pScriptInterface->CallFunctionVoid(global, "cancelOnLoadGameError", message); | ||||
} | } | ||||
Show All 10 Lines |
Wildfire Games · Phabricator