Index: ps/trunk/binaries/data/mods/mod/gui/modmod/modmod.js =================================================================== --- ps/trunk/binaries/data/mods/mod/gui/modmod/modmod.js (revision 25473) +++ ps/trunk/binaries/data/mods/mod/gui/modmod/modmod.js (revision 25474) @@ -1,495 +1,495 @@ /** * @file This GUI page displays all available mods and allows the player to enabled and launch a set of compatible mods. */ /** * A mod is defined by a mod.json file, for example * { * "name": "0ad", * "version": "0.0.24", * "label": "0 A.D. - Empires Ascendant", * "url": "https://wildfiregames.com/", * "description": "A free, open-source, historical RTS game.", * "dependencies": [] * } * * Or: * { * "name": "mod2", * "label": "Mod 2", * "version": "1.1", * "description": "", * "dependencies": ["0ad<=0.0.24", "rote"] * } * * A mod is identified by the directory name. * A mod must define the "name", "version", "label", "description" and "dependencies" property. * The "url" property is optional. * * The property "name" can consist alphanumeric characters, underscore and dash. * The name is used for version comparison of mod dependencies. * The property "version" may only contain numbers and up to two periods. * The property "label" is a human-readable name of the mod. * The property "description" is a human-readable summary of the features of the mod. * The property "url" is reference to a website about the mod. * The property "dependencies" is an array of strings. Each string is either a modname or a mod version comparison. * A mod version comparison is a modname, followed by an operator (=, <, >, <= or >=), followed by a mod version. * This allows mods to express upwards and downwards compatibility. */ /** * Mod definitions loaded from the files, including invalid mods. */ var g_Mods = {}; /** * Folder names of all mods that are or can be launched. */ var g_ModsEnabled = []; var g_ModsDisabled = []; var g_ModsEnabledFiltered = []; var g_ModsDisabledFiltered = []; /** * Cache mod compatibility recomputed when some mod is enbaled/disabled. */ var g_ModsCompatibility = []; /** * Name of the mods installed by the ModInstaller. */ var g_InstalledMods; -var g_HasFailedMods; +var g_HasIncompatibleMods; var g_FakeMod = { "name": translate("This mod does not exist"), "version": "", "label": "", "url": "", "description": "", "dependencies": [] }; var g_ColorNoModSelected = "255 255 100"; var g_ColorDependenciesMet = "100 255 100"; var g_ColorDependenciesNotMet = "255 100 100"; function init(data, hotloadData) { g_InstalledMods = data && data.installedMods || hotloadData && hotloadData.installedMods || []; - g_HasFailedMods = Engine.HasFailedMods(); + g_HasIncompatibleMods = Engine.HasIncompatibleMods(); initMods(); initGUIButtons(data); - if (g_HasFailedMods) + if (g_HasIncompatibleMods) Engine.PushGuiPage("page_incompatible_mods.xml", {}); } function initMods() { loadMods(); loadEnabledMods(); recomputeCompatibility(); validateMods(); initGUIFilters(); } function getHotloadData() { return { "installedMods": g_InstalledMods }; } function loadMods() { g_Mods = Engine.GetAvailableMods(); deepfreeze(g_Mods); } /** * Return fake mod for mods which do not exist */ function getMod(folder) { return !!g_Mods[folder] ? g_Mods[folder] : g_FakeMod; } function loadEnabledMods() { - if (g_HasFailedMods) + if (g_HasIncompatibleMods) g_ModsEnabled = Engine.GetFailedMods().filter(folder => folder != "mod"); else g_ModsEnabled = Engine.GetEnabledMods().filter(folder => !!g_Mods[folder]); g_ModsDisabled = Object.keys(g_Mods).filter(folder => g_ModsEnabled.indexOf(folder) == -1); g_ModsEnabledFiltered = g_ModsEnabled; g_ModsDisabledFiltered = g_ModsDisabled; } function validateMods() { for (let folder in g_Mods) validateMod(folder, g_Mods[folder], true); } function initGUIFilters() { Engine.GetGUIObjectByName("negateFilter").checked = false; Engine.GetGUIObjectByName("modCompatibleFilter").checked = true; displayModLists(); } function initGUIButtons(data) { // Either get back to the previous page or quit if there is no previous page let hasPreviousPage = !data || data.cancelbutton; Engine.GetGUIObjectByName("cancelButton").hidden = !hasPreviousPage; Engine.GetGUIObjectByName("quitButton").hidden = hasPreviousPage; Engine.GetGUIObjectByName("startModsButton").hidden = !hasPreviousPage; Engine.GetGUIObjectByName("startButton").hidden = hasPreviousPage; Engine.GetGUIObjectByName("toggleModButton").caption = translateWithContext("mod activation", "Enable"); } function saveMods() { sortEnabledMods(); Engine.ConfigDB_CreateValue("user", "mod.enabledmods", ["mod"].concat(g_ModsEnabled).join(" ")); Engine.ConfigDB_WriteFile("user", "config/user.cfg"); } function startMods() { sortEnabledMods(); if (!Engine.SetModsAndRestartEngine(["mod"].concat(g_ModsEnabled))) Engine.GetGUIObjectByName("message").caption = coloredText(translate('Dependencies not met'), g_ColorDependenciesNotMet); } function displayModLists() { g_ModsEnabledFiltered = displayModList("modsEnabledList", g_ModsEnabled, true); g_ModsDisabledFiltered = displayModList("modsDisabledList", g_ModsDisabled, false); } function displayModList(listObjectName, folders, enabled) { let listObject = Engine.GetGUIObjectByName(listObjectName); if (listObjectName == "modsDisabledList") { let sortFolder = folder => String(getMod(folder)[listObject.selected_column] || folder); folders.sort((folder1, folder2) => listObject.selected_column_order * sortFolder(folder1).localeCompare(sortFolder(folder2))); } folders = folders.filter(filterMod); if (!enabled && Engine.GetGUIObjectByName("modCompatibleFilter").checked) folders = folders.filter(folder => g_ModsCompatibility[folder]); let selected = listObject.selected !== -1 ? listObject.list_name[listObject.selected] : null; listObject.list_name = folders.map(folder => colorMod(folder, getMod(folder).name, enabled)); listObject.list_folder = folders.map(folder => colorMod(folder, folder, enabled)); listObject.list_label = folders.map(folder => colorMod(folder, getMod(folder).label, enabled)); listObject.list_url = folders.map(folder => colorMod(folder, getMod(folder).url || "", enabled)); listObject.list_version = folders.map(folder => colorMod(folder, getMod(folder).version, enabled)); listObject.list_dependencies = folders.map(folder => colorMod(folder, getMod(folder).dependencies.join(" "), enabled)); listObject.list = folders; listObject.selected = selected ? listObject.list_name.indexOf(selected) : -1; return folders; } function getModColor(folder, enabled) { if (!g_ModsCompatibility[folder]) return enabled ? g_ColorDependenciesNotMet : "gray"; if (g_InstalledMods.indexOf(getMod(folder).name) != -1) return "green"; return false; } function colorMod(folder, text, enabled) { let color = getModColor(folder, enabled); return color ? coloredText(text, color) : text; } function reloadDisabledMods() { g_ModsDisabled = Object.keys(g_Mods).filter(folder => g_ModsEnabled.indexOf(folder) == -1); } function enableMod() { let modsDisabledList = Engine.GetGUIObjectByName("modsDisabledList"); let pos = modsDisabledList.selected; if (pos == -1 || !g_ModsCompatibility[g_ModsDisabledFiltered[pos]]) return; g_ModsEnabled.push(g_ModsDisabledFiltered.splice(pos, 1)[0]); reloadDisabledMods(); recomputeCompatibility(); if (pos >= g_ModsDisabledFiltered.length) --pos; displayModLists(); Engine.GetGUIObjectByName("message").caption = ""; modsDisabledList.selected = pos; } function disableMod() { let modsEnabledList = Engine.GetGUIObjectByName("modsEnabledList"); let pos = modsEnabledList.selected; if (pos == -1) return; // Find true position of disabled mod and remove it let disabledMod = g_ModsEnabledFiltered[pos]; for (let i = 0; i < g_ModsEnabled.length; ++i) if (g_ModsEnabled[i] == disabledMod) { g_ModsEnabled.splice(i, 1); break; } if (!!g_Mods[disabledMod]) g_ModsDisabled.push(disabledMod); // Remove mods that required the removed mod and cascade // Sort them, so we know which ones can depend on the removed mod // TODO: Find position where the removed mod would have fit (for now assume idx 0) sortEnabledMods(); for (let i = 0; i < g_ModsEnabled.length; ++i) if (!areDependenciesMet(g_ModsEnabled[i], true)) { g_ModsDisabled.push(g_ModsEnabled.splice(i, 1)[0]); --i; } recomputeCompatibility(true); displayModLists(); Engine.GetGUIObjectByName("message").caption = ""; modsEnabledList.selected = Math.min(pos, g_ModsEnabledFiltered.length - 1); } function filterMod(folder) { let mod = getMod(folder); let negateFilter = Engine.GetGUIObjectByName("negateFilter").checked; let searchText = Engine.GetGUIObjectByName("modGenericFilter").caption; if (searchText && folder.indexOf(searchText) == -1 && mod.name.indexOf(searchText) == -1 && mod.label.indexOf(searchText) == -1 && (mod.url || "").indexOf(searchText) == -1 && mod.version.indexOf(searchText) == -1 && mod.description.indexOf(searchText) == -1 && mod.dependencies.indexOf(searchText) == -1) return negateFilter; return !negateFilter; } function closePage() { Engine.SwitchGuiPage("page_pregame.xml", {}); } function areFilters() { let searchText = Engine.GetGUIObjectByName("modGenericFilter").caption; return searchText && searchText != translate("Filter"); } /** * Moves an item in the list up or down. */ function moveCurrItem(objectName, up) { // Prevent moving while filters are applied // because we would need to map filtered positions // to not filtered positions so changes will persist. if (areFilters()) return; let obj = Engine.GetGUIObjectByName(objectName); let idx = obj.selected; if (idx == -1) return; let num = obj.list.length; let idx2 = idx + (up ? -1 : 1); if (idx2 < 0 || idx2 >= num) return; let tmp = g_ModsEnabled[idx]; g_ModsEnabled[idx] = g_ModsEnabled[idx2]; g_ModsEnabled[idx2] = tmp; g_ModsEnabledFiltered = displayModList("modsEnabledList", g_ModsEnabled, true); obj.selected = idx2; } function areDependenciesMet(folder, disabledAction = false) { // If we disabled mod it will not change satus of incompatible mods if (disabledAction && !g_ModsCompatibility[folder]) return g_ModsCompatibility[folder]; if (!g_Mods[folder]) return false; for (let dependency of getMod(folder).dependencies) { if (!isDependencyMet(dependency)) return false; } return true; } function recomputeCompatibility(disabledAction = false) { for (let mod in g_Mods) g_ModsCompatibility[mod] = areDependenciesMet(mod, disabledAction); } /** * @param dependency is a mod name or a mod version comparison. */ function isDependencyMet(dependency) { let operator = dependency.match(g_RegExpComparisonOperator); let [name, version] = operator ? dependency.split(operator[0]) : [dependency, undefined]; return g_ModsEnabled.some(folder => getMod(folder).name == name && (!operator || versionSatisfied(getMod(folder).version, operator[0], version))); } /** * Compares the given versions using the given operator. * '-' or '_' is ignored. Only numbers are supported. * @note "5.3" < "5.3.0" */ function versionSatisfied(version1, operator, version2) { let versionList1 = version1.split(/[-_]/)[0].split(/\./g); let versionList2 = version2.split(/[-_]/)[0].split(/\./g); let eq = operator.indexOf("=") != -1; let lt = operator.indexOf("<") != -1; let gt = operator.indexOf(">") != -1; for (let i = 0; i < Math.min(versionList1.length, versionList2.length); ++i) { let diff = +versionList1[i] - +versionList2[i]; if (gt && diff > 0 || lt && diff < 0) return true; if (gt && diff < 0 || lt && diff > 0 || eq && diff) return false; } // common prefix matches let ldiff = versionList1.length - versionList2.length; if (!ldiff) return eq; // NB: 2.3 != 2.3.0 if (ldiff < 0) return lt; return gt; } function sortEnabledMods() { let dependencies = {}; for (let folder of g_ModsEnabled) dependencies[folder] = getMod(folder).dependencies.map(d => d.split(g_RegExpComparisonOperator)[0]); g_ModsEnabled.sort((folder1, folder2) => dependencies[folder1].indexOf(getMod(folder2).name) != -1 ? 1 : dependencies[folder2].indexOf(getMod(folder1).name) != -1 ? -1 : 0); g_ModsEnabledFiltered = displayModList("modsEnabledList", g_ModsEnabled, true); } function selectedMod(listObjectName) { let listObject = Engine.GetGUIObjectByName(listObjectName); let isPickedDisabledList = listObjectName == "modsDisabledList"; let otherListObject = Engine.GetGUIObjectByName(isPickedDisabledList ? "modsEnabledList" : "modsDisabledList"); let toggleModButton = Engine.GetGUIObjectByName("toggleModButton"); let isModSelected = listObject.selected != -1; if (isModSelected) { otherListObject.selected = -1; toggleModButton.onPress = isPickedDisabledList ? enableMod : disableMod; } Engine.GetGUIObjectByName("visitWebButton").enabled = isModSelected && !!getSelectedModUrl(); toggleModButton.caption = isPickedDisabledList ? translateWithContext("mod activation", "Enable") : translateWithContext("mod activation", "Disable"); toggleModButton.enabled = isPickedDisabledList ? isModSelected && g_ModsCompatibility[listObject.list[listObject.selected]] : isModSelected; Engine.GetGUIObjectByName("enabledModUp").enabled = isModSelected && listObjectName == "modsEnabledList" && !areFilters(); Engine.GetGUIObjectByName("enabledModDown").enabled = isModSelected && listObjectName == "modsEnabledList" && !areFilters(); Engine.GetGUIObjectByName("globalModDescription").caption = listObject.list[listObject.selected] ? getMod(listObject.list[listObject.selected]).description : '[color="' + g_ColorNoModSelected + '"]' + translate("No mod has been selected.") + '[/color]'; if (!g_ModsEnabled.length) { if (!Engine.GetGUIObjectByName("startButton").hidden) Engine.GetGUIObjectByName("message").caption = coloredText(translate('Enable at least 0ad mod and save configuration'), g_ColorDependenciesNotMet); else Engine.GetGUIObjectByName("message").caption = coloredText(translate('Enable at least 0ad mod'), g_ColorDependenciesNotMet); } if (!Engine.GetGUIObjectByName("startButton").hidden) Engine.GetGUIObjectByName("startButton").enabled = g_ModsEnabled.length > 0; if (!Engine.GetGUIObjectByName("startModsButton").hidden) Engine.GetGUIObjectByName("startModsButton").enabled = g_ModsEnabled.length > 0; } /** * @returns {string} The url of the currently selected mod. */ function getSelectedModUrl() { let modsEnabledList = Engine.GetGUIObjectByName("modsEnabledList"); let modsDisabledList = Engine.GetGUIObjectByName("modsDisabledList"); let list = modsEnabledList.selected == -1 ? modsDisabledList : modsEnabledList; let folder = list.list[list.selected]; return folder && getMod(folder) && getMod(folder).url || undefined; } function visitModWebsite() { let url = getSelectedModUrl(); if (!url) return; if (!url.startsWith("http://") && !url.startsWith("https://")) url = "http://" + url; openURL(url); } Index: ps/trunk/source/main.cpp =================================================================== --- ps/trunk/source/main.cpp (revision 25473) +++ ps/trunk/source/main.cpp (revision 25474) @@ -1,754 +1,744 @@ /* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ /* This module drives the game when running without Atlas (our integrated map editor). It receives input and OS messages via SDL and feeds them into the input dispatcher, where they are passed on to the game GUI and simulation. It also contains main(), which either runs the above controller or that of Atlas depending on commandline parameters. */ // not for any PCH effort, but instead for the (common) definitions // included there. #define MINIMAL_PCH 2 #include "lib/precompiled.h" #include "lib/debug.h" #include "lib/status.h" #include "lib/secure_crt.h" #include "lib/frequency_filter.h" #include "lib/input.h" #include "lib/ogl.h" #include "lib/timer.h" #include "lib/external_libraries/libsdl.h" #include "ps/ArchiveBuilder.h" #include "ps/CConsole.h" #include "ps/CLogger.h" #include "ps/ConfigDB.h" #include "ps/Filesystem.h" #include "ps/Game.h" #include "ps/Globals.h" #include "ps/Hotkey.h" #include "ps/Loader.h" #include "ps/Mod.h" #include "ps/ModInstaller.h" #include "ps/Profile.h" #include "ps/Profiler2.h" #include "ps/Pyrogenesis.h" #include "ps/Replay.h" #include "ps/TouchInput.h" #include "ps/UserReport.h" #include "ps/Util.h" #include "ps/VideoMode.h" #include "ps/World.h" #include "ps/GameSetup/GameSetup.h" #include "ps/GameSetup/Atlas.h" #include "ps/GameSetup/Config.h" #include "ps/GameSetup/CmdLineArgs.h" #include "ps/GameSetup/Paths.h" #include "ps/XML/Xeromyces.h" #include "network/NetClient.h" #include "network/NetServer.h" #include "network/NetSession.h" #include "lobby/IXmppClient.h" #include "graphics/Camera.h" #include "graphics/GameView.h" #include "graphics/TextureManager.h" #include "gui/GUIManager.h" #include "renderer/Renderer.h" #include "rlinterface/RLInterface.h" #include "scriptinterface/ScriptContext.h" #include "scriptinterface/ScriptEngine.h" #include "scriptinterface/JSON.h" #include "simulation2/Simulation2.h" #include "simulation2/system/TurnManager.h" #include "soundmanager/ISoundManager.h" #if OS_UNIX #include #include // geteuid #endif // OS_UNIX #if OS_MACOSX #include "lib/sysdep/os/osx/osx_atlas.h" #endif #if MSC_VERSION #include #define getpid _getpid // Use the non-deprecated function name #endif #include -extern CmdLineArgs g_CmdLineArgs; extern CStrW g_UniqueLogPostfix; // Marks terrain as modified so the minimap can repaint (is there a cleaner way of handling this?) bool g_GameRestarted = false; // Determines the lifetime of the mainloop enum ShutdownType { // The application shall continue the main loop. None, // The process shall terminate as soon as possible. Quit, // The engine should be restarted in the same process, for instance to activate different mods. Restart, // Atlas should be started in the same process. RestartAsAtlas }; static ShutdownType g_Shutdown = ShutdownType::None; // to avoid redundant and/or recursive resizing, we save the new // size after VIDEORESIZE messages and only update the video mode // once per frame. // these values are the latest resize message, and reset to 0 once we've // updated the video mode static int g_ResizedW; static int g_ResizedH; static std::chrono::high_resolution_clock::time_point lastFrameTime; bool IsQuitRequested() { return g_Shutdown == ShutdownType::Quit; } void QuitEngine() { g_Shutdown = ShutdownType::Quit; } void RestartEngine() { g_Shutdown = ShutdownType::Restart; } void StartAtlas() { g_Shutdown = ShutdownType::RestartAsAtlas; } // main app message handler static InReaction MainInputHandler(const SDL_Event_* ev) { switch(ev->ev.type) { case SDL_WINDOWEVENT: switch(ev->ev.window.event) { case SDL_WINDOWEVENT_ENTER: RenderCursor(true); break; case SDL_WINDOWEVENT_LEAVE: RenderCursor(false); break; case SDL_WINDOWEVENT_RESIZED: g_ResizedW = ev->ev.window.data1; g_ResizedH = ev->ev.window.data2; break; case SDL_WINDOWEVENT_MOVED: g_VideoMode.UpdatePosition(ev->ev.window.data1, ev->ev.window.data2); } break; case SDL_QUIT: QuitEngine(); break; case SDL_DROPFILE: { char* dropped_filedir = ev->ev.drop.file; const Paths paths(g_CmdLineArgs); CModInstaller installer(paths.UserData() / "mods", paths.Cache()); installer.Install(std::string(dropped_filedir), g_ScriptContext, true); SDL_free(dropped_filedir); if (installer.GetInstalledMods().empty()) LOGERROR("Failed to install mod %s", dropped_filedir); else { LOGMESSAGE("Installed mod %s", installer.GetInstalledMods().front()); RestartEngine(); } break; } case SDL_HOTKEYPRESS: std::string hotkey = static_cast(ev->ev.user.data1); if (hotkey == "exit") { QuitEngine(); return IN_HANDLED; } else if (hotkey == "screenshot") { WriteScreenshot(L".png"); return IN_HANDLED; } else if (hotkey == "bigscreenshot") { int tiles = 4, tileWidth = 256, tileHeight = 256; CFG_GET_VAL("screenshot.tiles", tiles); CFG_GET_VAL("screenshot.tilewidth", tileWidth); CFG_GET_VAL("screenshot.tileheight", tileHeight); if (tiles > 0 && tileWidth > 0 && tileHeight > 0) WriteBigScreenshot(L".bmp", tiles, tileWidth, tileHeight); else LOGWARNING("Invalid big screenshot size: tiles=%d tileWidth=%d tileHeight=%d", tiles, tileWidth, tileHeight); return IN_HANDLED; } else if (hotkey == "togglefullscreen") { g_VideoMode.ToggleFullscreen(); return IN_HANDLED; } else if (hotkey == "profile2.toggle") { g_Profiler2.Toggle(); return IN_HANDLED; } break; } return IN_PASS; } // dispatch all pending events to the various receivers. static void PumpEvents() { ScriptRequest rq(g_GUI->GetScriptInterface()); PROFILE3("dispatch events"); SDL_Event_ ev; while (in_poll_event(&ev)) { PROFILE2("event"); if (g_GUI) { JS::RootedValue tmpVal(rq.cx); Script::ToJSVal(rq, &tmpVal, ev); std::string data = Script::StringifyJSON(rq, &tmpVal); PROFILE2_ATTR("%s", data.c_str()); } in_dispatch_event(&ev); } g_TouchInput.Frame(); } /** * Optionally throttle the render frequency in order to * prevent 100% workload of the currently used CPU core. */ inline static void LimitFPS() { if (g_VSync) return; double fpsLimit = 0.0; CFG_GET_VAL(g_Game && g_Game->IsGameStarted() ? "adaptivefps.session" : "adaptivefps.menu", fpsLimit); // Keep in sync with options.json if (fpsLimit < 20.0 || fpsLimit >= 100.0) return; double wait = 1000.0 / fpsLimit - std::chrono::duration_cast( std::chrono::high_resolution_clock::now() - lastFrameTime).count() / 1000.0; if (wait > 0.0) SDL_Delay(wait); lastFrameTime = std::chrono::high_resolution_clock::now(); } static int ProgressiveLoad() { PROFILE3("progressive load"); wchar_t description[100]; int progress_percent; try { Status ret = LDR_ProgressiveLoad(10e-3, description, ARRAY_SIZE(description), &progress_percent); switch(ret) { // no load active => no-op (skip code below) case INFO::OK: return 0; // current task didn't complete. we only care about this insofar as the // load process is therefore not yet finished. case ERR::TIMED_OUT: break; // just finished loading case INFO::ALL_COMPLETE: g_Game->ReallyStartGame(); wcscpy_s(description, ARRAY_SIZE(description), L"Game is starting.."); // LDR_ProgressiveLoad returns L""; set to valid text to // avoid problems in converting to JSString break; // error! default: WARN_RETURN_STATUS_IF_ERR(ret); // can't do this above due to legit ERR::TIMED_OUT break; } } catch (PSERROR_Game_World_MapLoadFailed& e) { // Map loading failed // Call script function to do the actual work // (delete game data, switch GUI page, show error, etc.) CancelLoad(CStr(e.what()).FromUTF8()); } GUI_DisplayLoadProgress(progress_percent, description); return 0; } static void RendererIncrementalLoad() { PROFILE3("renderer incremental load"); const double maxTime = 0.1f; double startTime = timer_Time(); bool more; do { more = g_Renderer.GetTextureManager().MakeProgress(); } while (more && timer_Time() - startTime < maxTime); } static void Frame() { g_Profiler2.RecordFrameStart(); PROFILE2("frame"); g_Profiler2.IncrementFrameNumber(); PROFILE2_ATTR("%d", g_Profiler2.GetFrameNumber()); ogl_WarnIfError(); // get elapsed time const double time = timer_Time(); g_frequencyFilter->Update(time); // .. old method - "exact" but contains jumps #if 0 static double last_time; const double time = timer_Time(); const float TimeSinceLastFrame = (float)(time-last_time); last_time = time; ONCE(return); // first call: set last_time and return // .. new method - filtered and more smooth, but errors may accumulate #else const float realTimeSinceLastFrame = 1.0 / g_frequencyFilter->SmoothedFrequency(); #endif ENSURE(realTimeSinceLastFrame > 0.0f); // Decide if update is necessary bool need_update = true; // If we are not running a multiplayer game, disable updates when the game is // minimized or out of focus and relinquish the CPU a bit, in order to make // debugging easier. if (g_PauseOnFocusLoss && !g_NetClient && !g_app_has_focus) { PROFILE3("non-focus delay"); need_update = false; // don't use SDL_WaitEvent: don't want the main loop to freeze until app focus is restored SDL_Delay(10); } // this scans for changed files/directories and reloads them, thus // allowing hotloading (changes are immediately assimilated in-game). ReloadChangedFiles(); ProgressiveLoad(); RendererIncrementalLoad(); PumpEvents(); // if the user quit by closing the window, the GL context will be broken and // may crash when we call Render() on some drivers, so leave this loop // before rendering if (g_Shutdown != ShutdownType::None) return; // respond to pumped resize events if (g_ResizedW || g_ResizedH) { g_VideoMode.ResizeWindow(g_ResizedW, g_ResizedH); g_ResizedW = g_ResizedH = 0; } if (g_NetClient) g_NetClient->Poll(); ogl_WarnIfError(); g_GUI->TickObjects(); ogl_WarnIfError(); if (g_RLInterface) g_RLInterface->TryApplyMessage(); if (g_Game && g_Game->IsGameStarted() && need_update) { if (!g_RLInterface) g_Game->Update(realTimeSinceLastFrame); g_Game->GetView()->Update(float(realTimeSinceLastFrame)); } // Keep us connected to any XMPP servers if (g_XmppClient) g_XmppClient->recv(); g_UserReporter.Update(); g_Console->Update(realTimeSinceLastFrame); ogl_WarnIfError(); if (g_SoundManager) g_SoundManager->IdleTask(); if (ShouldRender()) { Render(); { PROFILE3("swap buffers"); SDL_GL_SwapWindow(g_VideoMode.GetWindow()); ogl_WarnIfError(); } g_Renderer.OnSwapBuffers(); } g_Profiler.Frame(); g_GameRestarted = false; LimitFPS(); } static void NonVisualFrame() { g_Profiler2.RecordFrameStart(); PROFILE2("frame"); g_Profiler2.IncrementFrameNumber(); PROFILE2_ATTR("%d", g_Profiler2.GetFrameNumber()); static u32 turn = 0; debug_printf("Turn %u (%u)...\n", turn++, DEFAULT_TURN_LENGTH); g_Game->GetSimulation2()->Update(DEFAULT_TURN_LENGTH); g_Profiler.Frame(); if (g_Game->IsGameFinished()) QuitEngine(); } static void MainControllerInit() { // add additional input handlers only needed by this controller: // must be registered after gui_handler. Should mayhap even be last. in_add_handler(MainInputHandler); } static void MainControllerShutdown() { in_reset_handlers(); } static void StartRLInterface(CmdLineArgs args) { std::string server_address; CFG_GET_VAL("rlinterface.address", server_address); if (!args.Get("rl-interface").empty()) server_address = args.Get("rl-interface"); g_RLInterface = std::make_unique(server_address.c_str()); debug_printf("RL interface listening on %s\n", server_address.c_str()); } // moved into a helper function to ensure args is destroyed before // exit(), which may result in a memory leak. static void RunGameOrAtlas(int argc, const char* argv[]) { CmdLineArgs args(argc, argv); g_CmdLineArgs = args; if (args.Has("version")) { debug_printf("Pyrogenesis %s\n", engine_version); return; } if (args.Has("autostart-nonvisual") && args.Get("autostart").empty() && !args.Has("rl-interface")) { LOGERROR("-autostart-nonvisual cant be used alone. A map with -autostart=\"TYPEDIR/MAPNAME\" is needed."); return; } if (args.Has("unique-logs")) g_UniqueLogPostfix = L"_" + std::to_wstring(std::time(nullptr)) + L"_" + std::to_wstring(getpid()); const bool isVisualReplay = args.Has("replay-visual"); const bool isNonVisualReplay = args.Has("replay"); const bool isNonVisual = args.Has("autostart-nonvisual"); const bool isUsingRLInterface = args.Has("rl-interface"); const OsPath replayFile( isVisualReplay ? args.Get("replay-visual") : isNonVisualReplay ? args.Get("replay") : ""); if (isVisualReplay || isNonVisualReplay) { if (!FileExists(replayFile)) { debug_printf("ERROR: The requested replay file '%s' does not exist!\n", replayFile.string8().c_str()); return; } if (DirectoryExists(replayFile)) { debug_printf("ERROR: The requested replay file '%s' is a directory!\n", replayFile.string8().c_str()); return; } } std::vector modsToInstall; for (const CStr& arg : args.GetArgsWithoutName()) { const OsPath modPath(arg); if (!CModInstaller::IsDefaultModExtension(modPath.Extension())) { debug_printf("Skipping file '%s' which does not have a mod file extension.\n", modPath.string8().c_str()); continue; } if (!FileExists(modPath)) { debug_printf("ERROR: The mod file '%s' does not exist!\n", modPath.string8().c_str()); continue; } if (DirectoryExists(modPath)) { debug_printf("ERROR: The mod file '%s' is a directory!\n", modPath.string8().c_str()); continue; } modsToInstall.emplace_back(std::move(modPath)); } // We need to initialize SpiderMonkey and libxml2 in the main thread before // any thread uses them. So initialize them here before we might run Atlas. ScriptEngine scriptEngine; CXeromyces::Startup(); if (ATLAS_RunIfOnCmdLine(args, false)) { CXeromyces::Terminate(); return; } if (isNonVisualReplay) { - if (!args.Has("mod")) - { - LOGERROR("At least one mod should be specified! Did you mean to add the argument '-mod=public'?"); - CXeromyces::Terminate(); - return; - } - Paths paths(args); g_VFS = CreateVfs(); // Mount with highest priority, we don't want mods overwriting this. g_VFS->Mount(L"cache/", paths.Cache(), VFS_MOUNT_ARCHIVABLE, VFS_MAX_PRIORITY); - MountMods(paths, g_Mods.GetModsFromArguments(args, INIT_MODS)); { CReplayPlayer replay; replay.Load(replayFile); replay.Replay( args.Has("serializationtest"), args.Has("rejointest") ? args.Get("rejointest").ToInt() : -1, args.Has("ooslog"), !args.Has("hashtest-full") || args.Get("hashtest-full") == "true", args.Has("hashtest-quick") && args.Get("hashtest-quick") == "true"); } g_VFS.reset(); CXeromyces::Terminate(); return; } // run in archive-building mode if requested if (args.Has("archivebuild")) { Paths paths(args); OsPath mod(args.Get("archivebuild")); OsPath zip; if (args.Has("archivebuild-output")) zip = args.Get("archivebuild-output"); else zip = mod.Filename().ChangeExtension(L".zip"); CArchiveBuilder builder(mod, paths.Cache()); // Add mods provided on the command line // NOTE: We do not handle mods in the user mod path here std::vector mods = args.GetMultiple("mod"); for (size_t i = 0; i < mods.size(); ++i) builder.AddBaseMod(paths.RData()/"mods"/mods[i]); builder.Build(zip, args.Has("archivebuild-compress")); CXeromyces::Terminate(); return; } const double res = timer_Resolution(); g_frequencyFilter = CreateFrequencyFilter(res, 30.0); // run the game int flags = INIT_MODS; do { g_Shutdown = ShutdownType::None; if (!Init(args, flags)) { flags &= ~INIT_MODS; Shutdown(SHUTDOWN_FROM_CONFIG); continue; } std::vector installedMods; if (!modsToInstall.empty()) { Paths paths(args); CModInstaller installer(paths.UserData() / "mods", paths.Cache()); // Install the mods without deleting the pyromod files for (const OsPath& modPath : modsToInstall) installer.Install(modPath, g_ScriptContext, true); installedMods = installer.GetInstalledMods(); } if (isNonVisual) { InitNonVisual(args); if (isUsingRLInterface) StartRLInterface(args); while (g_Shutdown == ShutdownType::None) { if (isUsingRLInterface) g_RLInterface->TryApplyMessage(); else NonVisualFrame(); } } else { InitGraphics(args, 0, installedMods); MainControllerInit(); if (isUsingRLInterface) StartRLInterface(args); while (g_Shutdown == ShutdownType::None) Frame(); } // Do not install mods again in case of restart (typically from the mod selector) modsToInstall.clear(); - g_Mods.ClearIncompatibleMods(); Shutdown(0); MainControllerShutdown(); flags &= ~INIT_MODS; } while (g_Shutdown == ShutdownType::Restart); #if OS_MACOSX if (g_Shutdown == ShutdownType::RestartAsAtlas) startNewAtlasProcess(g_Mods.GetEnabledMods()); #else if (g_Shutdown == ShutdownType::RestartAsAtlas) ATLAS_RunIfOnCmdLine(args, true); #endif CXeromyces::Terminate(); } #if OS_ANDROID // In Android we compile the engine as a shared library, not an executable, // so rename main() to a different symbol that the wrapper library can load #undef main #define main pyrogenesis_main extern "C" __attribute__((visibility ("default"))) int main(int argc, char* argv[]); #endif extern "C" int main(int argc, char* argv[]) { #if OS_UNIX // Don't allow people to run the game with root permissions, // because bad things can happen, check before we do anything if (geteuid() == 0) { std::cerr << "********************************************************\n" << "WARNING: Attempted to run the game with root permission!\n" << "This is not allowed because it can alter home directory \n" << "permissions and opens your system to vulnerabilities. \n" << "(You received this message because you were either \n" <<" logged in as root or used e.g. the 'sudo' command.) \n" << "********************************************************\n\n"; return EXIT_FAILURE; } #endif // OS_UNIX EarlyInit(); // must come at beginning of main RunGameOrAtlas(argc, const_cast(argv)); // Shut down profiler initialised by EarlyInit g_Profiler2.Shutdown(); return EXIT_SUCCESS; } Index: ps/trunk/source/ps/GameSetup/CmdLineArgs.h =================================================================== --- ps/trunk/source/ps/GameSetup/CmdLineArgs.h (revision 25473) +++ ps/trunk/source/ps/GameSetup/CmdLineArgs.h (revision 25474) @@ -1,78 +1,80 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #ifndef INCLUDED_CMDLINEARGS #define INCLUDED_CMDLINEARGS #include "ps/CStr.h" #include "lib/os_path.h" #include class CmdLineArgs { public: CmdLineArgs() {} /** * Parse the command-line options, for future processing. * All arguments are required to be of the form -name or * -name=value - anything else is ignored. * * @param argc size of argv array * @param argv array of arguments; argv[0] should be the program's name */ CmdLineArgs(int argc, const char* argv[]); /** * Test whether the given name was specified, as either -name or * -name=value */ bool Has(const CStr& name) const; /** * Get the value of the named parameter. If it was not specified, returns * the empty string. If it was specified multiple times, returns the value * from the first occurrence. */ CStr Get(const CStr& name) const; /** * Get all the values given to the named parameter. Returns values in the * same order as they were given in argv. */ std::vector GetMultiple(const CStr& name) const; /** * Get the value of argv[0], which is typically meant to be the name/path of * the program (but the actual value is up to whoever executed the program). */ OsPath GetArg0() const; /** * Returns all arguments that don't have a name (string started with '-'). */ std::vector GetArgsWithoutName() const; private: typedef std::vector > ArgsT; ArgsT m_Args; OsPath m_Arg0; std::vector m_ArgsWithoutName; }; +extern CmdLineArgs g_CmdLineArgs; + #endif // INCLUDED_CMDLINEARGS Index: ps/trunk/source/ps/GameSetup/GameSetup.cpp =================================================================== --- ps/trunk/source/ps/GameSetup/GameSetup.cpp (revision 25473) +++ ps/trunk/source/ps/GameSetup/GameSetup.cpp (revision 25474) @@ -1,1655 +1,1644 @@ /* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "lib/app_hooks.h" #include "lib/config2.h" #include "lib/input.h" #include "lib/ogl.h" #include "lib/timer.h" #include "lib/external_libraries/libsdl.h" #include "lib/file/common/file_stats.h" #include "lib/res/h_mgr.h" #include "lib/res/graphics/cursor.h" #include "graphics/CinemaManager.h" #include "graphics/Color.h" #include "graphics/FontMetrics.h" #include "graphics/GameView.h" #include "graphics/LightEnv.h" #include "graphics/MapReader.h" #include "graphics/ModelDef.h" #include "graphics/MaterialManager.h" #include "graphics/TerrainTextureManager.h" #include "gui/CGUI.h" #include "gui/GUIManager.h" #include "i18n/L10n.h" #include "maths/MathUtil.h" #include "network/NetServer.h" #include "network/NetClient.h" #include "network/NetMessage.h" #include "network/NetMessages.h" #include "ps/CConsole.h" #include "ps/CLogger.h" #include "ps/ConfigDB.h" #include "ps/Filesystem.h" #include "ps/Game.h" #include "ps/GameSetup/Atlas.h" #include "ps/GameSetup/GameSetup.h" #include "ps/GameSetup/Paths.h" #include "ps/GameSetup/Config.h" #include "ps/GameSetup/CmdLineArgs.h" #include "ps/GameSetup/HWDetect.h" #include "ps/Globals.h" #include "ps/GUID.h" #include "ps/Hotkey.h" #include "ps/Joystick.h" #include "ps/Loader.h" #include "ps/Mod.h" #include "ps/ModIo.h" #include "ps/Profile.h" #include "ps/ProfileViewer.h" #include "ps/Profiler2.h" #include "ps/Pyrogenesis.h" // psSetLogDir #include "ps/scripting/JSInterface_Console.h" #include "ps/TouchInput.h" #include "ps/UserReport.h" #include "ps/Util.h" #include "ps/VideoMode.h" #include "ps/VisualReplay.h" #include "ps/World.h" #include "renderer/Renderer.h" #include "renderer/VertexBufferManager.h" #include "renderer/ModelRenderer.h" #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/ScriptStats.h" #include "scriptinterface/ScriptContext.h" #include "scriptinterface/ScriptConversions.h" #include "scriptinterface/JSON.h" #include "simulation2/Simulation2.h" #include "lobby/IXmppClient.h" #include "soundmanager/scripting/JSInterface_Sound.h" #include "soundmanager/ISoundManager.h" #include "tools/atlas/GameInterface/GameLoop.h" #include "tools/atlas/GameInterface/View.h" #if !(OS_WIN || OS_MACOSX || OS_ANDROID) // assume all other platforms use X11 for wxWidgets #define MUST_INIT_X11 1 #include #else #define MUST_INIT_X11 0 #endif extern void RestartEngine(); #include #include #include #include #include ERROR_GROUP(System); ERROR_TYPE(System, SDLInitFailed); ERROR_TYPE(System, VmodeFailed); ERROR_TYPE(System, RequiredExtensionsMissing); bool g_DoRenderGui = true; bool g_DoRenderLogger = true; bool g_DoRenderCursor = true; thread_local shared_ptr g_ScriptContext; static const int SANE_TEX_QUALITY_DEFAULT = 5; // keep in sync with code static const CStr g_EventNameGameLoadProgress = "GameLoadProgress"; bool g_InDevelopmentCopy; bool g_CheckedIfInDevelopmentCopy = false; static void SetTextureQuality(int quality) { int q_flags; GLint filter; retry: // keep this in sync with SANE_TEX_QUALITY_DEFAULT switch(quality) { // worst quality case 0: q_flags = OGL_TEX_HALF_RES|OGL_TEX_HALF_BPP; filter = GL_NEAREST; break; // [perf] add bilinear filtering case 1: q_flags = OGL_TEX_HALF_RES|OGL_TEX_HALF_BPP; filter = GL_LINEAR; break; // [vmem] no longer reduce resolution case 2: q_flags = OGL_TEX_HALF_BPP; filter = GL_LINEAR; break; // [vmem] add mipmaps case 3: q_flags = OGL_TEX_HALF_BPP; filter = GL_NEAREST_MIPMAP_LINEAR; break; // [perf] better filtering case 4: q_flags = OGL_TEX_HALF_BPP; filter = GL_LINEAR_MIPMAP_LINEAR; break; // [vmem] no longer reduce bpp case SANE_TEX_QUALITY_DEFAULT: q_flags = OGL_TEX_FULL_QUALITY; filter = GL_LINEAR_MIPMAP_LINEAR; break; // [perf] add anisotropy case 6: // TODO: add anisotropic filtering q_flags = OGL_TEX_FULL_QUALITY; filter = GL_LINEAR_MIPMAP_LINEAR; break; // invalid default: debug_warn(L"SetTextureQuality: invalid quality"); quality = SANE_TEX_QUALITY_DEFAULT; // careful: recursion doesn't work and we don't want to duplicate // the "sane" default values. goto retry; } ogl_tex_set_defaults(q_flags, filter); } //---------------------------------------------------------------------------- // GUI integration //---------------------------------------------------------------------------- // display progress / description in loading screen void GUI_DisplayLoadProgress(int percent, const wchar_t* pending_task) { const ScriptInterface& scriptInterface = *(g_GUI->GetActiveGUI()->GetScriptInterface()); ScriptRequest rq(scriptInterface); JS::RootedValueVector paramData(rq.cx); ignore_result(paramData.append(JS::NumberValue(percent))); JS::RootedValue valPendingTask(rq.cx); Script::ToJSVal(rq, &valPendingTask, pending_task); ignore_result(paramData.append(valPendingTask)); g_GUI->SendEventToAll(g_EventNameGameLoadProgress, paramData); } bool ShouldRender() { return !g_app_minimized && (g_app_has_focus || !g_VideoMode.IsInFullscreen()); } void Render() { // Do not render if not focused while in fullscreen or minimised, // as that triggers a difficult-to-reproduce crash on some graphic cards. if (!ShouldRender()) return; PROFILE3("render"); ogl_WarnIfError(); g_Profiler2.RecordGPUFrameStart(); ogl_WarnIfError(); // prepare before starting the renderer frame if (g_Game && g_Game->IsGameStarted()) g_Game->GetView()->BeginFrame(); if (g_Game) g_Renderer.SetSimulation(g_Game->GetSimulation2()); // start new frame g_Renderer.BeginFrame(); ogl_WarnIfError(); if (g_Game && g_Game->IsGameStarted()) g_Game->GetView()->Render(); ogl_WarnIfError(); g_Renderer.RenderTextOverlays(); // If we're in Atlas game view, render special tools if (g_AtlasGameLoop && g_AtlasGameLoop->view) { g_AtlasGameLoop->view->DrawCinemaPathTool(); ogl_WarnIfError(); } if (g_Game && g_Game->IsGameStarted()) g_Game->GetView()->GetCinema()->Render(); ogl_WarnIfError(); if (g_DoRenderGui) { // All GUI elements are drawn in Z order to render semi-transparent // objects correctly. glDisable(GL_DEPTH_TEST); g_GUI->Draw(); glEnable(GL_DEPTH_TEST); } ogl_WarnIfError(); // If we're in Atlas game view, render special overlays (e.g. editor bandbox) if (g_AtlasGameLoop && g_AtlasGameLoop->view) { g_AtlasGameLoop->view->DrawOverlays(); ogl_WarnIfError(); } // Text: glDisable(GL_DEPTH_TEST); g_Console->Render(); ogl_WarnIfError(); if (g_DoRenderLogger) g_Logger->Render(); ogl_WarnIfError(); // Profile information g_ProfileViewer.RenderProfile(); ogl_WarnIfError(); // Draw the cursor (or set the Windows cursor, on Windows) if (g_DoRenderCursor) { PROFILE3_GPU("cursor"); CStrW cursorName = g_CursorName; if (cursorName.empty()) { cursor_draw(g_VFS, NULL, g_mouse_x, g_yres-g_mouse_y, g_GuiScale, false); } else { bool forceGL = false; CFG_GET_VAL("nohwcursor", forceGL); #if CONFIG2_GLES #warning TODO: implement cursors for GLES #else // set up transform for GL cursor glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); CMatrix3D transform; transform.SetOrtho(0.f, (float)g_xres, 0.f, (float)g_yres, -1.f, 1000.f); glLoadMatrixf(&transform._11); #endif #if OS_ANDROID #warning TODO: cursors for Android #else if (cursor_draw(g_VFS, cursorName.c_str(), g_mouse_x, g_yres-g_mouse_y, g_GuiScale, forceGL) < 0) LOGWARNING("Failed to draw cursor '%s'", utf8_from_wstring(cursorName)); #endif #if CONFIG2_GLES #warning TODO: implement cursors for GLES #else // restore transform glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); #endif } } glEnable(GL_DEPTH_TEST); g_Renderer.EndFrame(); PROFILE2_ATTR("draw calls: %d", (int)g_Renderer.GetStats().m_DrawCalls); PROFILE2_ATTR("terrain tris: %d", (int)g_Renderer.GetStats().m_TerrainTris); PROFILE2_ATTR("water tris: %d", (int)g_Renderer.GetStats().m_WaterTris); PROFILE2_ATTR("model tris: %d", (int)g_Renderer.GetStats().m_ModelTris); PROFILE2_ATTR("overlay tris: %d", (int)g_Renderer.GetStats().m_OverlayTris); PROFILE2_ATTR("blend splats: %d", (int)g_Renderer.GetStats().m_BlendSplats); PROFILE2_ATTR("particles: %d", (int)g_Renderer.GetStats().m_Particles); ogl_WarnIfError(); g_Profiler2.RecordGPUFrameEnd(); ogl_WarnIfError(); } ErrorReactionInternal psDisplayError(const wchar_t* UNUSED(text), size_t UNUSED(flags)) { // If we're fullscreen, then sometimes (at least on some particular drivers on Linux) // displaying the error dialog hangs the desktop since the dialog box is behind the // fullscreen window. So we just force the game to windowed mode before displaying the dialog. // (But only if we're in the main thread, and not if we're being reentrant.) if (Threading::IsMainThread()) { static bool reentering = false; if (!reentering) { reentering = true; g_VideoMode.SetFullscreen(false); reentering = false; } } // We don't actually implement the error display here, so return appropriately return ERI_NOT_IMPLEMENTED; } void MountMods(const Paths& paths, const std::vector& mods) { OsPath modPath = paths.RData()/"mods"; OsPath modUserPath = paths.UserData()/"mods"; size_t userFlags = VFS_MOUNT_WATCH|VFS_MOUNT_ARCHIVABLE; size_t baseFlags = userFlags|VFS_MOUNT_MUST_EXIST; size_t priority = 0; for (size_t i = 0; i < mods.size(); ++i) { priority = i + 1; // Mods are higher priority than regular mountings, which default to priority 0 OsPath modName(mods[i]); // Only mount mods from the user path if they don't exist in the 'rdata' path. if (DirectoryExists(modPath / modName / "")) g_VFS->Mount(L"", modPath / modName / "", baseFlags, priority); else g_VFS->Mount(L"", modUserPath / modName / "", userFlags, priority); } // Mount the user mod last. In dev copy, mount it with a low priority. Otherwise, make it writable. g_VFS->Mount(L"", modUserPath / "user" / "", userFlags, InDevelopmentCopy() ? 0 : priority + 1); } static void InitVfs(const CmdLineArgs& args, int flags) { TIMER(L"InitVfs"); const bool setup_error = (flags & INIT_HAVE_DISPLAY_ERROR) == 0; const Paths paths(args); OsPath logs(paths.Logs()); CreateDirectories(logs, 0700); psSetLogDir(logs); // desired location for crashlog is now known. update AppHooks ASAP // (particularly before the following error-prone operations): AppHooks hooks = {0}; hooks.bundle_logs = psBundleLogs; hooks.get_log_dir = psLogDir; if (setup_error) hooks.display_error = psDisplayError; app_hooks_update(&hooks); g_VFS = CreateVfs(); const OsPath readonlyConfig = paths.RData()/"config"/""; // Mount these dirs with highest priority so that mods can't overwrite them. g_VFS->Mount(L"cache/", paths.Cache(), VFS_MOUNT_ARCHIVABLE, VFS_MAX_PRIORITY); // (adding XMBs to archive speeds up subsequent reads) if (readonlyConfig != paths.Config()) g_VFS->Mount(L"config/", readonlyConfig, 0, VFS_MAX_PRIORITY-1); g_VFS->Mount(L"config/", paths.Config(), 0, VFS_MAX_PRIORITY); g_VFS->Mount(L"screenshots/", paths.UserData()/"screenshots"/"", 0, VFS_MAX_PRIORITY); g_VFS->Mount(L"saves/", paths.UserData()/"saves"/"", VFS_MOUNT_WATCH, VFS_MAX_PRIORITY); // Engine localization files (regular priority, these can be overwritten). g_VFS->Mount(L"l10n/", paths.RData()/"l10n"/""); - MountMods(paths, g_Mods.GetModsFromArguments(args, flags)); + // Mods will be mounted later. // note: don't bother with g_VFS->TextRepresentation - directories // haven't yet been populated and are empty. } static void InitPs(bool setup_gui, const CStrW& gui_page, ScriptInterface* srcScriptInterface, JS::HandleValue initData) { { // console TIMER(L"ps_console"); g_Console->UpdateScreenSize(g_xres, g_yres); // Calculate and store the line spacing CFontMetrics font(CStrIntern(CONSOLE_FONT)); g_Console->m_iFontHeight = font.GetLineSpacing(); g_Console->m_iFontWidth = font.GetCharacterWidth(L'C'); g_Console->m_charsPerPage = (size_t)(g_xres / g_Console->m_iFontWidth); // Offset by an arbitrary amount, to make it fit more nicely g_Console->m_iFontOffset = 7; double blinkRate = 0.5; CFG_GET_VAL("gui.cursorblinkrate", blinkRate); g_Console->SetCursorBlinkRate(blinkRate); } // hotkeys { TIMER(L"ps_lang_hotkeys"); LoadHotkeys(g_ConfigDB); } if (!setup_gui) { // We do actually need *some* kind of GUI loaded, so use the // (currently empty) Atlas one g_GUI->SwitchPage(L"page_atlas.xml", srcScriptInterface, initData); return; } // GUI uses VFS, so this must come after VFS init. g_GUI->SwitchPage(gui_page, srcScriptInterface, initData); } void InitPsAutostart(bool networked, JS::HandleValue attrs) { // The GUI has not been initialized yet, so use the simulation scriptinterface for this variable ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue playerAssignments(rq.cx); Script::CreateObject(rq, &playerAssignments); if (!networked) { JS::RootedValue localPlayer(rq.cx); Script::CreateObject(rq, &localPlayer, "player", g_Game->GetPlayerID()); Script::SetProperty(rq, playerAssignments, "local", localPlayer); } JS::RootedValue sessionInitData(rq.cx); Script::CreateObject( rq, &sessionInitData, "attribs", attrs, "playerAssignments", playerAssignments); InitPs(true, L"page_loading.xml", &scriptInterface, sessionInitData); } void InitInput() { g_Joystick.Initialise(); // register input handlers // This stack is constructed so the first added, will be the last // one called. This is important, because each of the handlers // has the potential to block events to go further down // in the chain. I.e. the last one in the list added, is the // only handler that can block all messages before they are // processed. in_add_handler(game_view_handler); in_add_handler(CProfileViewer::InputThunk); in_add_handler(HotkeyInputActualHandler); // gui_handler needs to be registered after (i.e. called before!) the // hotkey handler so that input boxes can be typed in without // setting off hotkeys. in_add_handler(gui_handler); // Likewise for the console. in_add_handler(conInputHandler); in_add_handler(touch_input_handler); // Should be called after scancode map update (i.e. after the global input, but before UI). // This never blocks the event, but it does some processing necessary for hotkeys, // which are triggered later down the input chain. // (by calling this before the UI, we can use 'EventWouldTriggerHotkey' in the UI). in_add_handler(HotkeyInputPrepHandler); // These two must be called first (i.e. pushed last) // GlobalsInputHandler deals with some important global state, // such as which scancodes are being pressed, mouse buttons pressed, etc. // while HotkeyStateChange updates the map of active hotkeys. in_add_handler(GlobalsInputHandler); in_add_handler(HotkeyStateChange); } static void ShutdownPs() { SAFE_DELETE(g_GUI); UnloadHotkeys(); // disable the special Windows cursor, or free textures for OGL cursors cursor_draw(g_VFS, 0, g_mouse_x, g_yres-g_mouse_y, 1.0, false); } static void InitRenderer() { TIMER(L"InitRenderer"); // create renderer new CRenderer; // create terrain related stuff new CTerrainTextureManager; g_Renderer.Open(g_xres, g_yres); // Setup lighting environment. Since the Renderer accesses the // lighting environment through a pointer, this has to be done before // the first Frame. g_Renderer.SetLightEnv(&g_LightEnv); // I haven't seen the camera affecting GUI rendering and such, but the // viewport has to be updated according to the video mode SViewPort vp; vp.m_X = 0; vp.m_Y = 0; vp.m_Width = g_xres; vp.m_Height = g_yres; g_Renderer.SetViewport(vp); ModelDefActivateFastImpl(); ColorActivateFastImpl(); ModelRenderer::Init(); } static void InitSDL() { #if OS_LINUX // In fullscreen mode when SDL is compiled with DGA support, the mouse // sensitivity often appears to be unusably wrong (typically too low). // (This seems to be reported almost exclusively on Ubuntu, but can be // reproduced on Gentoo after explicitly enabling DGA.) // Disabling the DGA mouse appears to fix that problem, and doesn't // have any obvious negative effects. setenv("SDL_VIDEO_X11_DGAMOUSE", "0", 0); #endif if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE) < 0) { LOGERROR("SDL library initialization failed: %s", SDL_GetError()); throw PSERROR_System_SDLInitFailed(); } atexit(SDL_Quit); // Text input is active by default, disable it until it is actually needed. SDL_StopTextInput(); #if SDL_VERSION_ATLEAST(2, 0, 9) // SDL2 >= 2.0.9 defaults to 32 pixels (to support touch screens) but that can break our double-clicking. SDL_SetHint(SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS, "1"); #endif #if OS_MACOSX // Some Mac mice only have one button, so they can't right-click // but SDL2 can emulate that with Ctrl+Click bool macMouse = false; CFG_GET_VAL("macmouse", macMouse); SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, macMouse ? "1" : "0"); #endif } static void ShutdownSDL() { SDL_Quit(); } void EndGame() { SAFE_DELETE(g_NetClient); SAFE_DELETE(g_NetServer); SAFE_DELETE(g_Game); if (CRenderer::IsInitialised()) { ISoundManager::CloseGame(); g_Renderer.ResetState(); } } void Shutdown(int flags) { const bool hasRenderer = CRenderer::IsInitialised(); if ((flags & SHUTDOWN_FROM_CONFIG)) goto from_config; EndGame(); SAFE_DELETE(g_XmppClient); SAFE_DELETE(g_ModIo); ShutdownPs(); TIMER_BEGIN(L"shutdown TexMan"); delete &g_TexMan; TIMER_END(L"shutdown TexMan"); if (hasRenderer) { TIMER_BEGIN(L"shutdown Renderer"); g_Renderer.~CRenderer(); g_VBMan.Shutdown(); TIMER_END(L"shutdown Renderer"); } g_RenderingOptions.ClearHooks(); g_Profiler2.ShutdownGPU(); // Free cursors before shutting down SDL, as they may depend on SDL. cursor_shutdown(); TIMER_BEGIN(L"shutdown SDL"); ShutdownSDL(); TIMER_END(L"shutdown SDL"); if (hasRenderer) g_VideoMode.Shutdown(); TIMER_BEGIN(L"shutdown UserReporter"); g_UserReporter.Deinitialize(); TIMER_END(L"shutdown UserReporter"); // Cleanup curl now that g_ModIo and g_UserReporter have been shutdown. curl_global_cleanup(); delete &g_L10n; from_config: TIMER_BEGIN(L"shutdown ConfigDB"); CConfigDB::Shutdown(); TIMER_END(L"shutdown ConfigDB"); SAFE_DELETE(g_Console); // This is needed to ensure that no callbacks from the JSAPI try to use // the profiler when it's already destructed g_ScriptContext.reset(); // resource // first shut down all resource owners, and then the handle manager. TIMER_BEGIN(L"resource modules"); ISoundManager::SetEnabled(false); g_VFS.reset(); // this forcibly frees all open handles (thus preventing real leaks), // and makes further access to h_mgr impossible. h_mgr_shutdown(); file_stats_dump(); TIMER_END(L"resource modules"); TIMER_BEGIN(L"shutdown misc"); timer_DisplayClientTotals(); CNetHost::Deinitialize(); // should be last, since the above use them SAFE_DELETE(g_Logger); delete &g_Profiler; delete &g_ProfileViewer; SAFE_DELETE(g_ScriptStatsTable); TIMER_END(L"shutdown misc"); } #if OS_UNIX static void FixLocales() { #if OS_MACOSX || OS_BSD // OS X requires a UTF-8 locale in LC_CTYPE so that *wprintf can handle // wide characters. Peculiarly the string "UTF-8" seems to be acceptable // despite not being a real locale, and it's conveniently language-agnostic, // so use that. setlocale(LC_CTYPE, "UTF-8"); #endif // On misconfigured systems with incorrect locale settings, we'll die // with a C++ exception when some code (e.g. Boost) tries to use locales. // To avoid death, we'll detect the problem here and warn the user and // reset to the default C locale. // For informing the user of the problem, use the list of env vars that // glibc setlocale looks at. (LC_ALL is checked first, and LANG last.) const char* const LocaleEnvVars[] = { "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", "LANG" }; try { // this constructor is similar to setlocale(LC_ALL, ""), // but instead of returning NULL, it throws runtime_error // when the first locale env variable found contains an invalid value std::locale(""); } catch (std::runtime_error&) { LOGWARNING("Invalid locale settings"); for (size_t i = 0; i < ARRAY_SIZE(LocaleEnvVars); i++) { if (char* envval = getenv(LocaleEnvVars[i])) LOGWARNING(" %s=\"%s\"", LocaleEnvVars[i], envval); else LOGWARNING(" %s=\"(unset)\"", LocaleEnvVars[i]); } // We should set LC_ALL since it overrides LANG if (setenv("LC_ALL", std::locale::classic().name().c_str(), 1)) debug_warn(L"Invalid locale settings, and unable to set LC_ALL env variable."); else LOGWARNING("Setting LC_ALL env variable to: %s", getenv("LC_ALL")); } } #else static void FixLocales() { // Do nothing on Windows } #endif void EarlyInit() { // If you ever want to catch a particular allocation: //_CrtSetBreakAlloc(232647); Threading::SetMainThread(); debug_SetThreadName("main"); // add all debug_printf "tags" that we are interested in: debug_filter_add("TIMER"); timer_Init(); // initialise profiler early so it can profile startup, // but only after LatchStartTime g_Profiler2.Initialise(); FixLocales(); // Because we do GL calls from a secondary thread, Xlib needs to // be told to support multiple threads safely. // This is needed for Atlas, but we have to call it before any other // Xlib functions (e.g. the ones used when drawing the main menu // before launching Atlas) #if MUST_INIT_X11 int status = XInitThreads(); if (status == 0) debug_printf("Error enabling thread-safety via XInitThreads\n"); #endif // Initialise the low-quality rand function srand(time(NULL)); // NOTE: this rand should *not* be used for simulation! } bool Autostart(const CmdLineArgs& args); /** * Returns true if the user has intended to start a visual replay from command line. */ bool AutostartVisualReplay(const std::string& replayFile); -bool EnableModsOrSetDefault(const CmdLineArgs& args, const std::vector& mods, bool fromConfig) -{ - ScriptInterface scriptInterface("Engine", "CheckAndEnableMods", g_ScriptContext); - if (g_Mods.CheckAndEnableMods(scriptInterface, mods)) - return true; - // Here we refuse to start as there is no gui anyway - if (args.Has("autostart-nonvisual")) - { - if (fromConfig) - LOGERROR("Trying to start with incompatible mods from configuration file: %s.", boost::algorithm::join(g_Mods.GetIncompatibleMods(), ", ")); - else - LOGERROR("Trying to start with incompatible mods: %s.", boost::algorithm::join(g_Mods.GetIncompatibleMods(), ", ")); - return false; - } - g_Mods.SetDefaultMods(); - RestartEngine(); - return false; -} - - bool Init(const CmdLineArgs& args, int flags) { h_mgr_init(); // Do this as soon as possible, because it chdirs // and will mess up the error reporting if anything // crashes before the working directory is set. InitVfs(args, flags); // This must come after VFS init, which sets the current directory // (required for finding our output log files). g_Logger = new CLogger; new CProfileViewer; new CProfileManager; // before any script code g_ScriptStatsTable = new CScriptStatsTable; g_ProfileViewer.AddRootTable(g_ScriptStatsTable); // Set up the console early, so that debugging // messages can be logged to it. (The console's size // and fonts are set later in InitPs()) g_Console = new CConsole(); // g_ConfigDB, command line args, globals CONFIG_Init(args); // Using a global object for the context is a workaround until Simulation and AI use // their own threads and also their own contexts. const int contextSize = 384 * 1024 * 1024; const int heapGrowthBytesGCTrigger = 20 * 1024 * 1024; g_ScriptContext = ScriptContext::CreateContext(contextSize, heapGrowthBytesGCTrigger); - g_Mods.CacheEnabledModVersions(g_ScriptContext); + // On the first Init (INIT_MODS), check for command-line arguments + // or use the default mods from the config and enable those. + // On later engine restarts (e.g. the mod selector), we will skip this path, + // to avoid overwriting the newly selected mods. + if (flags & INIT_MODS) + { + ScriptInterface modInterface("Engine", "Mod", g_ScriptContext); + std::vector mods; + if (args.Has("mod")) + mods = args.GetMultiple("mod"); + else + { + CStr modsStr; + CFG_GET_VAL("mod.enabledmods", modsStr); + boost::split(mods, modsStr, boost::algorithm::is_space(), boost::token_compress_on); + } + + if (!g_Mods.EnableMods(modInterface, mods, flags & INIT_MODS_PUBLIC)) + { + // In non-visual mode, fail entirely. + if (args.Has("autostart-nonvisual")) + { + LOGERROR("Trying to start with incompatible mods: %s.", boost::algorithm::join(g_Mods.GetIncompatibleMods(), ", ")); + return false; + } + LOGWARNING("Invalid mods specified, starting with default mods."); + g_Mods.EnableDefaultMods(modInterface); + } + // Sanity check. + if (!g_Mods.GetIncompatibleMods().empty()) + { + LOGERROR("Trying to start with incompatible mods: %s.", boost::algorithm::join(g_Mods.GetIncompatibleMods(), ", ")); + return false; + } + } + MountMods(Paths(args), g_Mods.GetEnabledMods()); // Special command-line mode to dump the entity schemas instead of running the game. // (This must be done after loading VFS etc, but should be done before wasting time // on anything else.) if (args.Has("dumpSchema")) { CSimulation2 sim(NULL, g_ScriptContext, NULL); sim.LoadDefaultScripts(); std::ofstream f("entity.rng", std::ios_base::out | std::ios_base::trunc); f << sim.GenerateSchema(); std::cout << "Generated entity.rng\n"; exit(0); } CNetHost::Initialize(); #if CONFIG2_AUDIO if (!args.Has("autostart-nonvisual") && !g_DisableAudio) ISoundManager::CreateSoundManager(); #endif - // Check if there are mods specified on the command line, - // or if we already set the mods (~INIT_MODS), - // else check if there are mods that should be loaded specified - // in the config and load those (by aborting init and restarting - // the engine). - if ((flags & INIT_MODS) == INIT_MODS) - { - if (!args.Has("mod")) - { - CStr modstring; - CFG_GET_VAL("mod.enabledmods", modstring); - if (!modstring.empty()) - { - std::vector mods; - boost::split(mods, modstring, boost::is_any_of(" "), boost::token_compress_on); - if (!EnableModsOrSetDefault(args, mods, true)) - return false; - - RestartEngine(); - return false; - } - } - else if (!EnableModsOrSetDefault(args, g_Mods.GetEnabledMods(), false)) - return false; - } - new L10n; // Optionally start profiler HTTP output automatically // (By default it's only enabled by a hotkey, for security/performance) bool profilerHTTPEnable = false; CFG_GET_VAL("profiler2.autoenable", profilerHTTPEnable); if (profilerHTTPEnable) g_Profiler2.EnableHTTP(); // Initialise everything except Win32 sockets (because our networking // system already inits those) curl_global_init(CURL_GLOBAL_ALL & ~CURL_GLOBAL_WIN32); if (!g_Quickstart) g_UserReporter.Initialize(); // after config PROFILE2_EVENT("Init finished"); return true; } void InitGraphics(const CmdLineArgs& args, int flags, const std::vector& installedMods) { const bool setup_vmode = (flags & INIT_HAVE_VMODE) == 0; if(setup_vmode) { InitSDL(); if (!g_VideoMode.InitSDL()) throw PSERROR_System_VmodeFailed(); // abort startup } RunHardwareDetection(); const int quality = SANE_TEX_QUALITY_DEFAULT; // TODO: set value from config file SetTextureQuality(quality); ogl_WarnIfError(); // Optionally start profiler GPU timings automatically // (By default it's only enabled by a hotkey, for performance/compatibility) bool profilerGPUEnable = false; CFG_GET_VAL("profiler2.autoenable", profilerGPUEnable); if (profilerGPUEnable) g_Profiler2.EnableGPU(); if(!g_Quickstart) { WriteSystemInfo(); // note: no longer vfs_display here. it's dog-slow due to unbuffered // file output and very rarely needed. } if(g_DisableAudio) ISoundManager::SetEnabled(false); g_GUI = new CGUIManager(); // (must come after SetVideoMode, since it calls ogl_Init) CStr8 renderPath = "default"; CFG_GET_VAL("renderpath", renderPath); if ((ogl_HaveExtensions(0, "GL_ARB_vertex_program", "GL_ARB_fragment_program", NULL) != 0 // ARB && ogl_HaveExtensions(0, "GL_ARB_vertex_shader", "GL_ARB_fragment_shader", NULL) != 0) // GLSL || RenderPathEnum::FromString(renderPath) == FIXED) { // It doesn't make sense to continue working here, because we're not // able to display anything. DEBUG_DISPLAY_FATAL_ERROR( L"Your graphics card doesn't appear to be fully compatible with OpenGL shaders." L" The game does not support pre-shader graphics cards." L" You are advised to try installing newer drivers and/or upgrade your graphics card." L" For more information, please see http://www.wildfiregames.com/forum/index.php?showtopic=16734" ); } const char* missing = ogl_HaveExtensions(0, "GL_ARB_multitexture", "GL_EXT_draw_range_elements", "GL_ARB_texture_env_combine", "GL_ARB_texture_env_dot3", NULL); if(missing) { wchar_t buf[500]; swprintf_s(buf, ARRAY_SIZE(buf), L"The %hs extension doesn't appear to be available on your computer." L" The game may still work, though - you are welcome to try at your own risk." L" If not or it doesn't look right, upgrade your graphics card.", missing ); DEBUG_DISPLAY_ERROR(buf); // TODO: i18n } if (!ogl_HaveExtension("GL_ARB_texture_env_crossbar")) { DEBUG_DISPLAY_ERROR( L"The GL_ARB_texture_env_crossbar extension doesn't appear to be available on your computer." L" Shadows are not available and overall graphics quality might suffer." L" You are advised to try installing newer drivers and/or upgrade your graphics card."); g_ConfigDB.SetValueBool(CFG_HWDETECT, "shadows", false); } ogl_WarnIfError(); g_RenderingOptions.ReadConfigAndSetupHooks(); InitRenderer(); InitInput(); ogl_WarnIfError(); // TODO: Is this the best place for this? if (VfsDirectoryExists(L"maps/")) CXeromyces::AddValidator(g_VFS, "map", "maps/scenario.rng"); try { if (!AutostartVisualReplay(args.Get("replay-visual")) && !Autostart(args)) { const bool setup_gui = ((flags & INIT_NO_GUI) == 0); // We only want to display the splash screen at startup shared_ptr scriptInterface = g_GUI->GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue data(rq.cx); if (g_GUI) { Script::CreateObject(rq, &data, "isStartup", true); if (!installedMods.empty()) Script::SetProperty(rq, data, "installedMods", installedMods); } InitPs(setup_gui, installedMods.empty() ? L"page_pregame.xml" : L"page_modmod.xml", g_GUI->GetScriptInterface().get(), data); } } catch (PSERROR_Game_World_MapLoadFailed& e) { // Map Loading failed // Start the engine so we have a GUI InitPs(true, L"page_pregame.xml", NULL, JS::UndefinedHandleValue); // Call script function to do the actual work // (delete game data, switch GUI page, show error, etc.) CancelLoad(CStr(e.what()).FromUTF8()); } } void InitNonVisual(const CmdLineArgs& args) { // Need some stuff for terrain movement costs: // (TODO: this ought to be independent of any graphics code) new CTerrainTextureManager; g_TexMan.LoadTerrainTextures(); Autostart(args); } void RenderGui(bool RenderingState) { g_DoRenderGui = RenderingState; } void RenderLogger(bool RenderingState) { g_DoRenderLogger = RenderingState; } void RenderCursor(bool RenderingState) { g_DoRenderCursor = RenderingState; } /** * Temporarily loads a scenario map and retrieves the "ScriptSettings" JSON * data from it. * The scenario map format is used for scenario and skirmish map types (random * games do not use a "map" (format) but a small JavaScript program which * creates a map on the fly). It contains a section to initialize the game * setup screen. * @param mapPath Absolute path (from VFS root) to the map file to peek in. * @return ScriptSettings in JSON format extracted from the map. */ CStr8 LoadSettingsOfScenarioMap(const VfsPath &mapPath) { CXeromyces mapFile; const char *pathToSettings[] = { "Scenario", "ScriptSettings", "" // Path to JSON data in map }; Status loadResult = mapFile.Load(g_VFS, mapPath); if (INFO::OK != loadResult) { LOGERROR("LoadSettingsOfScenarioMap: Unable to load map file '%s'", mapPath.string8()); throw PSERROR_Game_World_MapLoadFailed("Unable to load map file, check the path for typos."); } XMBElement mapElement = mapFile.GetRoot(); // Select the ScriptSettings node in the map file... for (int i = 0; pathToSettings[i][0]; ++i) { int childId = mapFile.GetElementID(pathToSettings[i]); XMBElementList nodes = mapElement.GetChildNodes(); auto it = std::find_if(nodes.begin(), nodes.end(), [&childId](const XMBElement& child) { return child.GetNodeName() == childId; }); if (it != nodes.end()) mapElement = *it; } // ... they contain a JSON document to initialize the game setup // screen return mapElement.GetText(); } /* * Command line options for autostart * (keep synchronized with binaries/system/readme.txt): * * -autostart="TYPEDIR/MAPNAME" enables autostart and sets MAPNAME; * TYPEDIR is skirmishes, scenarios, or random * -autostart-seed=SEED sets randomization seed value (default 0, use -1 for random) * -autostart-ai=PLAYER:AI sets the AI for PLAYER (e.g. 2:petra) * -autostart-aidiff=PLAYER:DIFF sets the DIFFiculty of PLAYER's AI * (0: sandbox, 5: very hard) * -autostart-aiseed=AISEED sets the seed used for the AI random * generator (default 0, use -1 for random) * -autostart-player=NUMBER sets the playerID in non-networked games (default 1, use -1 for observer) * -autostart-civ=PLAYER:CIV sets PLAYER's civilisation to CIV * (skirmish and random maps only) * -autostart-team=PLAYER:TEAM sets the team for PLAYER (e.g. 2:2). * -autostart-ceasefire=NUM sets a ceasefire duration NUM * (default 0 minutes) * -autostart-nonvisual disable any graphics and sounds * -autostart-victory=SCRIPTNAME sets the victory conditions with SCRIPTNAME * located in simulation/data/settings/victory_conditions/ * (default conquest). When the first given SCRIPTNAME is * "endless", no victory conditions will apply. * -autostart-wonderduration=NUM sets the victory duration NUM for wonder victory condition * (default 10 minutes) * -autostart-relicduration=NUM sets the victory duration NUM for relic victory condition * (default 10 minutes) * -autostart-reliccount=NUM sets the number of relics for relic victory condition * (default 2 relics) * -autostart-disable-replay disable saving of replays * * Multiplayer: * -autostart-playername=NAME sets local player NAME (default 'anonymous') * -autostart-host sets multiplayer host mode * -autostart-host-players=NUMBER sets NUMBER of human players for multiplayer * game (default 2) * -autostart-client=IP sets multiplayer client to join host at * given IP address * Random maps only: * -autostart-size=TILES sets random map size in TILES (default 192) * -autostart-players=NUMBER sets NUMBER of players on random map * (default 2) * * Examples: * 1) "Bob" will host a 2 player game on the Arcadia map: * -autostart="scenarios/Arcadia" -autostart-host -autostart-host-players=2 -autostart-playername="Bob" * "Alice" joins the match as player 2: * -autostart="scenarios/Arcadia" -autostart-client=127.0.0.1 -autostart-playername="Alice" * The players use the developer overlay to control players. * * 2) Load Alpine Lakes random map with random seed, 2 players (Athens and Britons), and player 2 is PetraBot: * -autostart="random/alpine_lakes" -autostart-seed=-1 -autostart-players=2 -autostart-civ=1:athen -autostart-civ=2:brit -autostart-ai=2:petra * * 3) Observe the PetraBot on a triggerscript map: * -autostart="random/jebel_barkal" -autostart-seed=-1 -autostart-players=2 -autostart-civ=1:athen -autostart-civ=2:brit -autostart-ai=1:petra -autostart-ai=2:petra -autostart-player=-1 */ bool Autostart(const CmdLineArgs& args) { CStr autoStartName = args.Get("autostart"); if (autoStartName.empty()) return false; g_Game = new CGame(!args.Has("autostart-disable-replay")); ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue attrs(rq.cx); JS::RootedValue settings(rq.cx); JS::RootedValue playerData(rq.cx); Script::CreateObject(rq, &attrs); Script::CreateObject(rq, &settings); Script::CreateArray(rq, &playerData); // 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 types and folders is hard-coded, but benefits are: // - No need to pass the map type via command line separately // - Prevents mixing up of scenarios and skirmish maps to some degree Path mapPath = Path(autoStartName); std::wstring mapDirectory = mapPath.Parent().Filename().string(); std::string mapType; if (mapDirectory == L"random") { // Random map definition will be loaded from JSON file, so we need to parse it std::wstring scriptPath = L"maps/" + autoStartName.FromUTF8() + L".json"; JS::RootedValue scriptData(rq.cx); Script::ReadJSONFile(rq, scriptPath, &scriptData); if (!scriptData.isUndefined() && Script::GetProperty(rq, scriptData, "settings", &settings)) { // JSON loaded ok - copy script name over to game attributes std::wstring scriptFile; Script::GetProperty(rq, settings, "Script", scriptFile); Script::SetProperty(rq, attrs, "script", scriptFile); // RMS filename } else { // Problem with JSON file LOGERROR("Autostart: Error reading random map script '%s'", utf8_from_wstring(scriptPath)); throw PSERROR_Game_World_MapLoadFailed("Error reading random map script.\nCheck application log for details."); } // Get optional map size argument (default 192) uint mapSize = 192; if (args.Has("autostart-size")) { CStr size = args.Get("autostart-size"); mapSize = size.ToUInt(); } Script::SetProperty(rq, settings, "Size", mapSize); // Random map size (in patches) // Get optional number of players (default 2) size_t numPlayers = 2; if (args.Has("autostart-players")) { CStr num = args.Get("autostart-players"); numPlayers = num.ToUInt(); } // Set up player data for (size_t i = 0; i < numPlayers; ++i) { JS::RootedValue player(rq.cx); // We could load player_defaults.json here, but that would complicate the logic // even more and autostart is only intended for developers anyway Script::CreateObject(rq, &player, "Civ", "athen"); Script::SetPropertyInt(rq, playerData, i, player); } mapType = "random"; } else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") { // Initialize general settings from the map data so some values // (e.g. name of map) are always present, even when autostart is // partially configured CStr8 mapSettingsJSON = LoadSettingsOfScenarioMap("maps/" + autoStartName + ".xml"); Script::ParseJSON(rq, mapSettingsJSON, &settings); // Initialize the playerData array being modified by autostart // with the real map data, so sensible values are present: Script::GetProperty(rq, settings, "PlayerData", &playerData); if (mapDirectory == L"scenarios") mapType = "scenario"; else mapType = "skirmish"; } else { LOGERROR("Autostart: Unrecognized map type '%s'", utf8_from_wstring(mapDirectory)); throw PSERROR_Game_World_MapLoadFailed("Unrecognized map type.\nConsult readme.txt for the currently supported types."); } Script::SetProperty(rq, attrs, "mapType", mapType); Script::SetProperty(rq, attrs, "map", "maps/" + autoStartName); Script::SetProperty(rq, settings, "mapType", mapType); Script::SetProperty(rq, settings, "CheatsEnabled", true); // The seed is used for both random map generation and simulation u32 seed = 0; if (args.Has("autostart-seed")) { CStr seedArg = args.Get("autostart-seed"); if (seedArg == "-1") seed = rand(); else seed = seedArg.ToULong(); } Script::SetProperty(rq, settings, "Seed", seed); // Set seed for AIs u32 aiseed = 0; if (args.Has("autostart-aiseed")) { CStr seedArg = args.Get("autostart-aiseed"); if (seedArg == "-1") aiseed = rand(); else aiseed = seedArg.ToULong(); } Script::SetProperty(rq, settings, "AISeed", aiseed); // Set player data for AIs // attrs.settings = { PlayerData: [ { AI: ... }, ... ] } // or = { PlayerData: [ null, { AI: ... }, ... ] } when gaia set int offset = 1; JS::RootedValue player(rq.cx); if (Script::GetPropertyInt(rq, playerData, 0, &player) && player.isNull()) offset = 0; // Set teams if (args.Has("autostart-team")) { std::vector civArgs = args.GetMultiple("autostart-team"); for (size_t i = 0; i < civArgs.size(); ++i) { int playerID = civArgs[i].BeforeFirst(":").ToInt(); // Instead of overwriting existing player data, modify the array JS::RootedValue currentPlayer(rq.cx); if (!Script::GetPropertyInt(rq, playerData, playerID-offset, ¤tPlayer) || currentPlayer.isUndefined()) { if (mapDirectory == L"skirmishes") { // playerID is certainly bigger than this map player number LOGWARNING("Autostart: Invalid player %d in autostart-team option", playerID); continue; } Script::CreateObject(rq, ¤tPlayer); } int teamID = civArgs[i].AfterFirst(":").ToInt() - 1; Script::SetProperty(rq, currentPlayer, "Team", teamID); Script::SetPropertyInt(rq, playerData, playerID-offset, currentPlayer); } } int ceasefire = 0; if (args.Has("autostart-ceasefire")) ceasefire = args.Get("autostart-ceasefire").ToInt(); Script::SetProperty(rq, settings, "Ceasefire", ceasefire); if (args.Has("autostart-ai")) { std::vector aiArgs = args.GetMultiple("autostart-ai"); for (size_t i = 0; i < aiArgs.size(); ++i) { int playerID = aiArgs[i].BeforeFirst(":").ToInt(); // Instead of overwriting existing player data, modify the array JS::RootedValue currentPlayer(rq.cx); if (!Script::GetPropertyInt(rq, playerData, playerID-offset, ¤tPlayer) || currentPlayer.isUndefined()) { if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") { // playerID is certainly bigger than this map player number LOGWARNING("Autostart: Invalid player %d in autostart-ai option", playerID); continue; } Script::CreateObject(rq, ¤tPlayer); } Script::SetProperty(rq, currentPlayer, "AI", aiArgs[i].AfterFirst(":")); Script::SetProperty(rq, currentPlayer, "AIDiff", 3); Script::SetProperty(rq, currentPlayer, "AIBehavior", "balanced"); Script::SetPropertyInt(rq, playerData, playerID-offset, currentPlayer); } } // Set AI difficulty if (args.Has("autostart-aidiff")) { std::vector civArgs = args.GetMultiple("autostart-aidiff"); for (size_t i = 0; i < civArgs.size(); ++i) { int playerID = civArgs[i].BeforeFirst(":").ToInt(); // Instead of overwriting existing player data, modify the array JS::RootedValue currentPlayer(rq.cx); if (!Script::GetPropertyInt(rq, playerData, playerID-offset, ¤tPlayer) || currentPlayer.isUndefined()) { if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") { // playerID is certainly bigger than this map player number LOGWARNING("Autostart: Invalid player %d in autostart-aidiff option", playerID); continue; } Script::CreateObject(rq, ¤tPlayer); } Script::SetProperty(rq, currentPlayer, "AIDiff", civArgs[i].AfterFirst(":").ToInt()); Script::SetPropertyInt(rq, playerData, playerID-offset, currentPlayer); } } // Set player data for Civs if (args.Has("autostart-civ")) { if (mapDirectory != L"scenarios") { std::vector civArgs = args.GetMultiple("autostart-civ"); for (size_t i = 0; i < civArgs.size(); ++i) { int playerID = civArgs[i].BeforeFirst(":").ToInt(); // Instead of overwriting existing player data, modify the array JS::RootedValue currentPlayer(rq.cx); if (!Script::GetPropertyInt(rq, playerData, playerID-offset, ¤tPlayer) || currentPlayer.isUndefined()) { if (mapDirectory == L"skirmishes") { // playerID is certainly bigger than this map player number LOGWARNING("Autostart: Invalid player %d in autostart-civ option", playerID); continue; } Script::CreateObject(rq, ¤tPlayer); } Script::SetProperty(rq, currentPlayer, "Civ", civArgs[i].AfterFirst(":")); Script::SetPropertyInt(rq, playerData, playerID-offset, currentPlayer); } } else LOGWARNING("Autostart: Option 'autostart-civ' is invalid for scenarios"); } // Add player data to map settings Script::SetProperty(rq, settings, "PlayerData", playerData); // Add map settings to game attributes Script::SetProperty(rq, attrs, "settings", settings); // Get optional playername CStrW userName = L"anonymous"; if (args.Has("autostart-playername")) userName = args.Get("autostart-playername").FromUTF8(); // Add additional scripts to the TriggerScripts property std::vector triggerScriptsVector; JS::RootedValue triggerScripts(rq.cx); if (Script::HasProperty(rq, settings, "TriggerScripts")) { Script::GetProperty(rq, settings, "TriggerScripts", &triggerScripts); Script::FromJSVal(rq, triggerScripts, triggerScriptsVector); } if (!CRenderer::IsInitialised()) { CStr nonVisualScript = "scripts/NonVisualTrigger.js"; triggerScriptsVector.push_back(nonVisualScript.FromUTF8()); } std::vector victoryConditions(1, "conquest"); if (args.Has("autostart-victory")) victoryConditions = args.GetMultiple("autostart-victory"); if (victoryConditions.size() == 1 && victoryConditions[0] == "endless") victoryConditions.clear(); Script::SetProperty(rq, settings, "VictoryConditions", victoryConditions); for (const CStr& victory : victoryConditions) { JS::RootedValue scriptData(rq.cx); JS::RootedValue data(rq.cx); JS::RootedValue victoryScripts(rq.cx); CStrW scriptPath = L"simulation/data/settings/victory_conditions/" + victory.FromUTF8() + L".json"; Script::ReadJSONFile(rq, scriptPath, &scriptData); if (!scriptData.isUndefined() && Script::GetProperty(rq, scriptData, "Data", &data) && !data.isUndefined() && Script::GetProperty(rq, data, "Scripts", &victoryScripts) && !victoryScripts.isUndefined()) { std::vector victoryScriptsVector; Script::FromJSVal(rq, victoryScripts, victoryScriptsVector); triggerScriptsVector.insert(triggerScriptsVector.end(), victoryScriptsVector.begin(), victoryScriptsVector.end()); } else { 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."); } } Script::ToJSVal(rq, &triggerScripts, triggerScriptsVector); Script::SetProperty(rq, settings, "TriggerScripts", triggerScripts); int wonderDuration = 10; if (args.Has("autostart-wonderduration")) wonderDuration = args.Get("autostart-wonderduration").ToInt(); Script::SetProperty(rq, settings, "WonderDuration", wonderDuration); int relicDuration = 10; if (args.Has("autostart-relicduration")) relicDuration = args.Get("autostart-relicduration").ToInt(); Script::SetProperty(rq, settings, "RelicDuration", relicDuration); int relicCount = 2; if (args.Has("autostart-reliccount")) relicCount = args.Get("autostart-reliccount").ToInt(); Script::SetProperty(rq, settings, "RelicCount", relicCount); if (args.Has("autostart-host")) { InitPsAutostart(true, attrs); size_t maxPlayers = 2; if (args.Has("autostart-host-players")) maxPlayers = args.Get("autostart-host-players").ToUInt(); // Generate a secret to identify the host client. std::string secret = ps_generate_guid(); g_NetServer = new CNetServer(false, maxPlayers); g_NetServer->SetControllerSecret(secret); g_NetServer->UpdateInitAttributes(&attrs, scriptInterface); bool ok = g_NetServer->SetupConnection(PS_DEFAULT_PORT); ENSURE(ok); g_NetClient = new CNetClient(g_Game); g_NetClient->SetUserName(userName); g_NetClient->SetupServerData("127.0.0.1", PS_DEFAULT_PORT, false); g_NetClient->SetControllerSecret(secret); g_NetClient->SetupConnection(nullptr); } else if (args.Has("autostart-client")) { InitPsAutostart(true, attrs); g_NetClient = new CNetClient(g_Game); g_NetClient->SetUserName(userName); CStr ip = args.Get("autostart-client"); if (ip.empty()) ip = "127.0.0.1"; g_NetClient->SetupServerData(ip, PS_DEFAULT_PORT, false); ENSURE(g_NetClient->SetupConnection(nullptr)); } else { g_Game->SetPlayerID(args.Has("autostart-player") ? args.Get("autostart-player").ToInt() : 1); g_Game->StartGame(&attrs, ""); if (CRenderer::IsInitialised()) { InitPsAutostart(false, attrs); } else { // TODO: Non progressive load can fail - need a decent way to handle this LDR_NonprogressiveLoad(); ENSURE(g_Game->ReallyStartGame() == PSRETURN_OK); } } return true; } bool AutostartVisualReplay(const std::string& replayFile) { if (!FileExists(OsPath(replayFile))) return false; g_Game = new CGame(false); g_Game->SetPlayerID(-1); g_Game->StartVisualReplay(replayFile); ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue attrs(rq.cx, g_Game->GetSimulation2()->GetInitAttributes()); InitPsAutostart(false, attrs); return true; } void CancelLoad(const CStrW& message) { shared_ptr pScriptInterface = g_GUI->GetActiveGUI()->GetScriptInterface(); ScriptRequest rq(pScriptInterface); JS::RootedValue global(rq.cx, rq.globalValue()); LDR_Cancel(); if (g_GUI && g_GUI->GetPageCount() && Script::HasProperty(rq, global, "cancelOnLoadGameError")) ScriptFunction::CallVoid(rq, global, "cancelOnLoadGameError", message); } bool InDevelopmentCopy() { if (!g_CheckedIfInDevelopmentCopy) { g_InDevelopmentCopy = (g_VFS->GetFileInfo(L"config/dev.cfg", NULL) == INFO::OK); g_CheckedIfInDevelopmentCopy = true; } return g_InDevelopmentCopy; } Index: ps/trunk/source/ps/GameSetup/GameSetup.h =================================================================== --- ps/trunk/source/ps/GameSetup/GameSetup.h (revision 25473) +++ ps/trunk/source/ps/GameSetup/GameSetup.h (revision 25474) @@ -1,105 +1,106 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #ifndef INCLUDED_GAMESETUP #define INCLUDED_GAMESETUP // // GUI integration // // display progress / description in loading screen extern void GUI_DisplayLoadProgress(int percent, const wchar_t* pending_task); extern void Render(); extern bool ShouldRender(); /** * initialize global modules that are be needed before Init. * must be called from the very beginning of main. **/ extern void EarlyInit(); extern void EndGame(); enum InitFlags { // avoid setting a video mode / initializing OpenGL; assume that has // already been done and everything is ready for rendering. // needed by map editor because it creates its own window. INIT_HAVE_VMODE = 1, // skip initializing the in-game GUI. // needed by map editor because it uses its own GUI. INIT_NO_GUI = 2, // avoid setting display_error app hook // needed by map editor because it has its own wx error display INIT_HAVE_DISPLAY_ERROR = 4, // initialize the mod folders from command line parameters INIT_MODS = 8, // mount the public mod // needed by the map editor as "mod" does not provide everything it needs INIT_MODS_PUBLIC = 16 }; enum ShutdownFlags { // start shutdown from config down // needed for loading mods as specified in the config // without having to go through a full init-shutdown cycle SHUTDOWN_FROM_CONFIG = 1 }; /** * enable/disable rendering of the GUI (intended mainly for screenshots) */ extern void RenderGui(bool RenderingState); extern void RenderLogger(bool RenderingState); /** * enable/disable rendering of the cursor - this does not hide cursor, but reverts to OS style */ extern void RenderCursor(bool RenderingState); class CmdLineArgs; class Paths; extern const std::vector& GetMods(const CmdLineArgs& args, int flags); /** * Mounts all files of the given mods in the global VFS. * Make sure to call CacheEnabledModVersions after every call to this. */ extern void MountMods(const Paths& paths, const std::vector& mods); + /** * Returns true if successful, false if mods changed and restart_engine was called. * In the latter case the caller should call Shutdown() with SHUTDOWN_FROM_CONFIG. */ extern bool Init(const CmdLineArgs& args, int flags); extern void InitInput(); extern void InitGraphics(const CmdLineArgs& args, int flags, const std::vector& installedMods = std::vector()); extern void InitNonVisual(const CmdLineArgs& args); extern void Shutdown(int flags); extern void CancelLoad(const CStrW& message); extern bool InDevelopmentCopy(); #endif // INCLUDED_GAMESETUP Index: ps/trunk/source/ps/Mod.cpp =================================================================== --- ps/trunk/source/ps/Mod.cpp (revision 25473) +++ ps/trunk/source/ps/Mod.cpp (revision 25474) @@ -1,377 +1,367 @@ /* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "ps/Mod.h" #include "i18n/L10n.h" #include "lib/file/file_system.h" #include "lib/file/vfs/vfs.h" #include "lib/utf8.h" #include "ps/Filesystem.h" -#include "ps/GameSetup/CmdLineArgs.h" #include "ps/GameSetup/GameSetup.h" #include "ps/GameSetup/Paths.h" #include "ps/Profiler2.h" #include "ps/Pyrogenesis.h" #include "scriptinterface/Object.h" #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/JSON.h" #include #include #include #include #include #include namespace { /** * Global instance of Mod, always exists. */ Mod g_ModInstance; bool ParseModJSON(const ScriptRequest& rq, const PIVFS& vfs, OsPath modsPath, OsPath mod, JS::MutableHandleValue json) { // Attempt to open mod.json first. std::ifstream modjson; modjson.open((modsPath / mod / L"mod.json").string8()); if (!modjson.is_open()) { modjson.close(); // Fallback: open the archive and read mod.json there. // This can take in the hundreds of milliseconds with large mods. vfs->Clear(); if (vfs->Mount(L"", modsPath / mod / "", VFS_MOUNT_MUST_EXIST, VFS_MIN_PRIORITY) < 0) return false; CVFSFile modinfo; if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK) return false; if (!Script::ParseJSON(rq, modinfo.GetAsString(), json)) return false; // Attempt to write the mod.json file so we'll take the fast path next time. std::ofstream out_mod_json((modsPath / mod / L"mod.json").string8()); if (out_mod_json.good()) { out_mod_json << modinfo.GetAsString(); out_mod_json.close(); } else { // Print a warning - we'll keep trying, which could have adverse effects. if (L10n::IsInitialised()) LOGWARNING(g_L10n.Translate("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled."), mod.string8()); else LOGWARNING("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled.", mod.string8()); } return true; } else { std::stringstream buffer; buffer << modjson.rdbuf(); return Script::ParseJSON(rq, buffer.str(), json); } } } // anonymous namespace Mod& Mod::Instance() { return g_ModInstance; } JS::Value Mod::GetAvailableMods(const ScriptInterface& scriptInterface) const { PROFILE2("GetAvailableMods"); const Paths paths(g_CmdLineArgs); // loop over all possible paths OsPath modPath = paths.RData()/"mods"; OsPath modUserPath = paths.UserData()/"mods"; DirectoryNames modDirs; DirectoryNames modDirsUser; GetDirectoryEntries(modPath, NULL, &modDirs); // Sort modDirs so that we can do a fast lookup below std::sort(modDirs.begin(), modDirs.end()); PIVFS vfs = CreateVfs(); ScriptRequest rq(scriptInterface); JS::RootedValue value(rq.cx, Script::CreateObject(rq)); for (DirectoryNames::iterator iter = modDirs.begin(); iter != modDirs.end(); ++iter) { JS::RootedValue json(rq.cx); if (!ParseModJSON(rq, vfs, modPath, *iter, &json)) continue; // Valid mod data, add it to our structure Script::SetProperty(rq, value, utf8_from_wstring(iter->string()).c_str(), json); } GetDirectoryEntries(modUserPath, NULL, &modDirsUser); for (DirectoryNames::iterator iter = modDirsUser.begin(); iter != modDirsUser.end(); ++iter) { // Ignore mods in the user folder if we have already found them in modDirs. if (std::binary_search(modDirs.begin(), modDirs.end(), *iter)) continue; JS::RootedValue json(rq.cx); if (!ParseModJSON(rq, vfs, modUserPath, *iter, &json)) continue; // Valid mod data, add it to our structure Script::SetProperty(rq, value, utf8_from_wstring(iter->string()).c_str(), json); } return value.get(); } const std::vector& Mod::GetEnabledMods() const { return m_ModsLoaded; } const std::vector& Mod::GetIncompatibleMods() const { return m_IncompatibleMods; } -const std::vector& Mod::GetFailedMods() const +void Mod::EnableDefaultMods(const ScriptInterface& scriptInterface) { - return m_FailedMods; + m_IncompatibleMods.clear(); + m_ModsLoaded.clear(); + m_ModsLoaded.insert(m_ModsLoaded.begin(), "mod"); + CacheEnabledModVersions(scriptInterface); } -const std::vector& Mod::GetModsFromArguments(const CmdLineArgs& args, int flags) +bool Mod::EnableMods(const ScriptInterface& scriptInterface, const std::vector& mods, const bool addPublic) { - const bool initMods = (flags & INIT_MODS) == INIT_MODS; - const bool addPublic = (flags & INIT_MODS_PUBLIC) == INIT_MODS_PUBLIC; - - if (!initMods) - return m_ModsLoaded; + m_IncompatibleMods.clear(); + m_ModsLoaded.clear(); - m_ModsLoaded = args.GetMultiple("mod"); + std::unordered_map counts; + for (const CStr& mod : mods) + { + // Ignore duplicates. + if (counts.try_emplace(mod, 0).first->second++ > 0) + continue; + m_ModsLoaded.emplace_back(mod); + } - if (addPublic) + if (addPublic && counts["public"] == 0) m_ModsLoaded.insert(m_ModsLoaded.begin(), "public"); - m_ModsLoaded.insert(m_ModsLoaded.begin(), "mod"); + if (counts["mod"] == 0) + m_ModsLoaded.insert(m_ModsLoaded.begin(), "mod"); - return m_ModsLoaded; -} - -void Mod::SetDefaultMods() -{ - m_ModsLoaded.clear(); - m_ModsLoaded.insert(m_ModsLoaded.begin(), "mod"); -} + ScriptRequest rq(scriptInterface); + JS::RootedValue availableMods(rq.cx, GetAvailableMods(scriptInterface)); + m_IncompatibleMods = CheckForIncompatibleMods(scriptInterface, m_ModsLoaded, availableMods); -void Mod::ClearIncompatibleMods() -{ - m_IncompatibleMods.clear(); - m_FailedMods.clear(); -} + for (const CStr& mod : m_IncompatibleMods) + m_ModsLoaded.erase(std::find(m_ModsLoaded.begin(), m_ModsLoaded.end(), mod)); -bool Mod::CheckAndEnableMods(const ScriptInterface& scriptInterface, const std::vector& mods) -{ - ScriptRequest rq(scriptInterface); + CacheEnabledModVersions(scriptInterface); - JS::RootedValue availableMods(rq.cx, GetAvailableMods(scriptInterface)); - if (!AreModsCompatible(scriptInterface, mods, availableMods)) - { - m_FailedMods = mods; - return false; - } - m_ModsLoaded = mods; - return true; + return m_IncompatibleMods.empty(); } -bool Mod::AreModsCompatible(const ScriptInterface& scriptInterface, const std::vector& mods, const JS::RootedValue& availableMods) +std::vector Mod::CheckForIncompatibleMods(const ScriptInterface& scriptInterface, const std::vector& mods, const JS::RootedValue& availableMods) const { ScriptRequest rq(scriptInterface); + std::vector incompatibleMods; std::unordered_map> modDependencies; std::unordered_map modNameVersions; for (const CStr& mod : mods) { if (mod == "mod") continue; JS::RootedValue modData(rq.cx); // Requested mod is not available, fail if (!Script::HasProperty(rq, availableMods, mod.c_str())) { - m_IncompatibleMods.push_back(mod); + incompatibleMods.push_back(mod); continue; } if (!Script::GetProperty(rq, availableMods, mod.c_str(), &modData)) { - m_IncompatibleMods.push_back(mod); + incompatibleMods.push_back(mod); continue; } std::vector dependencies; CStr version; CStr name; Script::GetProperty(rq, modData, "dependencies", dependencies); Script::GetProperty(rq, modData, "version", version); Script::GetProperty(rq, modData, "name", name); modNameVersions.emplace(name, version); modDependencies.emplace(mod, dependencies); } static const std::vector toCheck = { "<=", ">=", "=", "<", ">" }; for (const CStr& mod : mods) { if (mod == "mod") continue; const std::unordered_map>::iterator res = modDependencies.find(mod); if (res == modDependencies.end()) continue; const std::vector deps = res->second; if (deps.empty()) continue; for (const CStr& dep : deps) { if (dep.empty()) continue; // 0ad<=0.0.24 for (const CStr& op : toCheck) { const int pos = dep.Find(op.c_str()); if (pos == -1) continue; //0ad const CStr modToCheck = dep.substr(0, pos); //0.0.24 const CStr versionToCheck = dep.substr(pos + op.size()); const std::unordered_map::iterator it = modNameVersions.find(modToCheck); if (it == modNameVersions.end()) { - m_IncompatibleMods.push_back(mod); + incompatibleMods.push_back(mod); continue; } // 0.0.25(0ad) , <=, 0.0.24(required version) if (!CompareVersionStrings(it->second, op, versionToCheck)) { - m_IncompatibleMods.push_back(mod); + incompatibleMods.push_back(mod); continue; } break; } } } - return m_IncompatibleMods.empty(); + return incompatibleMods; } bool Mod::CompareVersionStrings(const CStr& version, const CStr& op, const CStr& required) const { std::vector versionSplit; std::vector requiredSplit; static const std::string toIgnore = "-,_"; boost::split(versionSplit, version, boost::is_any_of(toIgnore), boost::token_compress_on); boost::split(requiredSplit, required, boost::is_any_of(toIgnore), boost::token_compress_on); boost::split(versionSplit, versionSplit[0], boost::is_any_of("."), boost::token_compress_on); boost::split(requiredSplit, requiredSplit[0], boost::is_any_of("."), boost::token_compress_on); const bool eq = op.Find("=") != -1; const bool lt = op.Find("<") != -1; const bool gt = op.Find(">") != -1; const size_t min = std::min(versionSplit.size(), requiredSplit.size()); for (size_t i = 0; i < min; ++i) { const int diff = versionSplit[i].ToInt() - requiredSplit[i].ToInt(); if ((gt && diff > 0) || (lt && diff < 0)) return true; if ((gt && diff < 0) || (lt && diff > 0) || (eq && diff)) return false; } const size_t versionSize = versionSplit.size(); const size_t requiredSize = requiredSplit.size(); if (versionSize == requiredSize) return eq; return versionSize < requiredSize ? lt : gt; } -void Mod::CacheEnabledModVersions(const shared_ptr& scriptContext) +void Mod::CacheEnabledModVersions(const ScriptInterface& scriptInterface) { - ScriptInterface scriptInterface("Engine", "CacheEnabledModVersions", scriptContext); ScriptRequest rq(scriptInterface); JS::RootedValue availableMods(rq.cx, GetAvailableMods(scriptInterface)); m_LoadedModVersions.clear(); for (const CStr& mod : m_ModsLoaded) { // Ignore mod mod as it is irrelevant for compatibility checks if (mod == "mod") continue; CStr version; JS::RootedValue modData(rq.cx); if (Script::GetProperty(rq, availableMods, mod.c_str(), &modData)) Script::GetProperty(rq, modData, "version", version); m_LoadedModVersions.push_back({mod, version}); } } JS::Value Mod::GetLoadedModsWithVersions(const ScriptInterface& scriptInterface) const { ScriptRequest rq(scriptInterface); JS::RootedValue returnValue(rq.cx); Script::ToJSVal(rq, &returnValue, m_LoadedModVersions); return returnValue; } JS::Value Mod::GetEngineInfo(const ScriptInterface& scriptInterface) const { ScriptRequest rq(scriptInterface); JS::RootedValue mods(rq.cx, GetLoadedModsWithVersions(scriptInterface)); JS::RootedValue metainfo(rq.cx); Script::CreateObject( rq, &metainfo, "engine_version", engine_version, "mods", mods); Script::FreezeObject(rq, metainfo, true); return metainfo; } Index: ps/trunk/source/ps/Mod.h =================================================================== --- ps/trunk/source/ps/Mod.h (revision 25473) +++ ps/trunk/source/ps/Mod.h (revision 25474) @@ -1,83 +1,91 @@ /* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #ifndef INCLUDED_MOD #define INCLUDED_MOD #include "ps/CStr.h" #include "scriptinterface/ScriptForward.h" #include -class CmdLineArgs; - -extern CmdLineArgs g_CmdLineArgs; - #define g_Mods (Mod::Instance()) class Mod { + friend class TestMod; public: // Singleton-like interface. static Mod& Instance(); JS::Value GetAvailableMods(const ScriptInterface& scriptInterface) const; const std::vector& GetEnabledMods() const; const std::vector& GetIncompatibleMods() const; - const std::vector& GetFailedMods() const; /** - * This reads the version numbers from the launched mods. - * It caches the result, since the reading of zip files is slow and - * JS pages can request the version numbers too often easily. - * Make sure this is called after each MountMods call. + * Enable the default mods. De-activates any non-default mod currently enabled. */ - void CacheEnabledModVersions(const shared_ptr& scriptContext); + void EnableDefaultMods(const ScriptInterface& scriptInterface); - const std::vector& GetModsFromArguments(const CmdLineArgs& args, int flags); - bool AreModsCompatible(const ScriptInterface& scriptInterface, const std::vector& mods, const JS::RootedValue& availableMods); - bool CheckAndEnableMods(const ScriptInterface& scriptInterface, const std::vector& mods); - bool CompareVersionStrings(const CStr& required, const CStr& op, const CStr& version) const; - void SetDefaultMods(); - void ClearIncompatibleMods(); + /** + * Enables specified mods (& mods required by the engine). + * @param addPublic - if true, enable the public mod. + * @return whether the mods were enabled successfully. This can fail if e.g. mods are incompatible. + * If true, GetEnabledMods() should be non-empty, GetIncompatibleMods() empty. Otherwise, GetIncompatibleMods() is non-empty. + */ + bool EnableMods(const ScriptInterface& scriptInterface, const std::vector& mods, const bool addPublic); /** * Get the loaded mods and their version. * "user" mod and "mod" mod are ignored as they are irrelevant for compatibility checks. * * @param scriptInterface the ScriptInterface in which to create the return data. * @return list of loaded mods with the format [[modA, versionA], [modB, versionB], ...] */ JS::Value GetLoadedModsWithVersions(const ScriptInterface& scriptInterface) const; /** * Gets info (version and mods loaded) on the running engine * * @param scriptInterface the ScriptInterface in which to create the return data. * @return list of objects containing data */ JS::Value GetEngineInfo(const ScriptInterface& scriptInterface) const; private: + /** + * This reads the version numbers from the launched mods. + * It caches the result, since the reading of zip files is slow and + * JS pages can request the version numbers too often easily. + * Make sure this is called after each MountMods call. + */ + void CacheEnabledModVersions(const ScriptInterface& scriptInterface); + + /** + * Checks a list of @a mods and returns the incompatible mods, if any. + */ + std::vector CheckForIncompatibleMods(const ScriptInterface& scriptInterface, const std::vector& mods, const JS::RootedValue& availableMods) const; + bool CompareVersionStrings(const CStr& required, const CStr& op, const CStr& version) const; + std::vector m_ModsLoaded; + // Of the currently loaded mods, these are the incompatible with the engine and cannot be loaded. std::vector m_IncompatibleMods; - std::vector m_FailedMods; std::vector> m_LoadedModVersions; }; #endif // INCLUDED_MOD Index: ps/trunk/source/ps/ModIo.cpp =================================================================== --- ps/trunk/source/ps/ModIo.cpp (revision 25473) +++ ps/trunk/source/ps/ModIo.cpp (revision 25474) @@ -1,856 +1,857 @@ /* Copyright (C) 2021 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "precompiled.h" #include "ModIo.h" #include "i18n/L10n.h" #include "lib/file/file_system.h" #include "lib/sysdep/filesystem.h" #include "lib/sysdep/sysdep.h" #include "maths/MD5.h" #include "ps/CLogger.h" #include "ps/ConfigDB.h" +#include "ps/GameSetup/CmdLineArgs.h" #include "ps/GameSetup/Paths.h" #include "ps/Mod.h" #include "ps/ModInstaller.h" #include "ps/Util.h" #include "scriptinterface/ScriptConversions.h" #include "scriptinterface/ScriptContext.h" #include "scriptinterface/ScriptRequest.h" #include "scriptinterface/JSON.h" #include #include ModIo* g_ModIo = nullptr; struct DownloadCallbackData { DownloadCallbackData() : fp(nullptr), md5(), hash_state(nullptr) { } DownloadCallbackData(FILE* _fp) : fp(_fp), md5() { hash_state = static_cast( sodium_malloc(crypto_generichash_statebytes())); ENSURE(hash_state); crypto_generichash_init(hash_state, nullptr, 0U, crypto_generichash_BYTES_MAX); } ~DownloadCallbackData() { if (hash_state) sodium_free(hash_state); } FILE* fp; MD5 md5; crypto_generichash_state* hash_state; }; ModIo::ModIo() : m_GamesRequest("/games"), m_CallbackData(nullptr) { // Get config values from the default namespace. // This can be overridden on the command line. // // We do this so a malicious mod cannot change the base url and // get the user to make connections to someone else's endpoint. // If another user of the engine wants to provide different values // here, while still using the same engine version, they can just // provide some shortcut/script that sets these using command line // parameters. std::string pk_str; g_ConfigDB.GetValue(CFG_DEFAULT, "modio.public_key", pk_str); g_ConfigDB.GetValue(CFG_DEFAULT, "modio.v1.baseurl", m_BaseUrl); { std::string api_key; g_ConfigDB.GetValue(CFG_DEFAULT, "modio.v1.api_key", api_key); m_ApiKey = "api_key=" + api_key; } { std::string nameid; g_ConfigDB.GetValue(CFG_DEFAULT, "modio.v1.name_id", nameid); m_IdQuery = "name_id="+nameid; } m_CurlMulti = curl_multi_init(); ENSURE(m_CurlMulti); m_Curl = curl_easy_init(); ENSURE(m_Curl); // Capture error messages curl_easy_setopt(m_Curl, CURLOPT_ERRORBUFFER, m_ErrorBuffer); // Fail if the server did curl_easy_setopt(m_Curl, CURLOPT_FAILONERROR, 1L); // Disable signal handlers (required for multithreaded applications) curl_easy_setopt(m_Curl, CURLOPT_NOSIGNAL, 1L); // To minimise security risks, don't support redirects (except for file // downloads, for which this setting will be enabled). curl_easy_setopt(m_Curl, CURLOPT_FOLLOWLOCATION, 0L); // For file downloads, one redirect seems plenty for a CDN serving the files. curl_easy_setopt(m_Curl, CURLOPT_MAXREDIRS, 1L); m_Headers = NULL; std::string ua = "User-Agent: pyrogenesis "; ua += curl_version(); ua += " (https://play0ad.com/)"; m_Headers = curl_slist_append(m_Headers, ua.c_str()); curl_easy_setopt(m_Curl, CURLOPT_HTTPHEADER, m_Headers); if (sodium_init() < 0) ENSURE(0 && "Failed to initialize libsodium."); size_t bin_len = 0; if (sodium_base642bin((unsigned char*)&m_pk, sizeof m_pk, pk_str.c_str(), pk_str.size(), NULL, &bin_len, NULL, sodium_base64_VARIANT_ORIGINAL) != 0 || bin_len != sizeof m_pk) ENSURE(0 && "Failed to decode base64 public key. Please fix your configuration or mod.io will be unusable."); } ModIo::~ModIo() { // Clean things up to avoid unpleasant surprises, // and delete the temporary file if any. TearDownRequest(); if (m_DownloadProgressData.status == DownloadProgressStatus::DOWNLOADING) DeleteDownloadedFile(); curl_slist_free_all(m_Headers); curl_easy_cleanup(m_Curl); curl_multi_cleanup(m_CurlMulti); delete m_CallbackData; } size_t ModIo::ReceiveCallback(void* buffer, size_t size, size_t nmemb, void* userp) { ModIo* self = static_cast(userp); self->m_ResponseData += std::string((char*)buffer, (char*)buffer+size*nmemb); return size*nmemb; } size_t ModIo::DownloadCallback(void* buffer, size_t size, size_t nmemb, void* userp) { DownloadCallbackData* data = static_cast(userp); if (!data->fp) return 0; size_t len = fwrite(buffer, size, nmemb, data->fp); // Only update the hash with data we actually managed to write. // In case we did not write all of it we will fail the download, // but we do not want to have a possibly valid hash in that case. size_t written = len*size; data->md5.Update(static_cast(buffer), written); ENSURE(data->hash_state); crypto_generichash_update(data->hash_state, static_cast(buffer), written); return written; } int ModIo::DownloadProgressCallback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t UNUSED(ultotal), curl_off_t UNUSED(ulnow)) { DownloadProgressData* data = static_cast(clientp); // If we got more data than curl expected, something is very wrong, abort. if (dltotal != 0 && dlnow > dltotal) return 1; data->progress = dltotal == 0 ? 0 : static_cast(dlnow) / static_cast(dltotal); return 0; } CURLMcode ModIo::SetupRequest(const std::string& url, bool fileDownload) { if (fileDownload) { // The download link will most likely redirect elsewhere, so allow that. // We verify the validity of the file later. curl_easy_setopt(m_Curl, CURLOPT_FOLLOWLOCATION, 1L); // Enable the progress meter curl_easy_setopt(m_Curl, CURLOPT_NOPROGRESS, 0L); // Set IO callbacks curl_easy_setopt(m_Curl, CURLOPT_WRITEFUNCTION, DownloadCallback); curl_easy_setopt(m_Curl, CURLOPT_WRITEDATA, static_cast(m_CallbackData)); curl_easy_setopt(m_Curl, CURLOPT_XFERINFOFUNCTION, DownloadProgressCallback); curl_easy_setopt(m_Curl, CURLOPT_XFERINFODATA, static_cast(&m_DownloadProgressData)); // Initialize the progress counter m_DownloadProgressData.progress = 0; } else { // To minimise security risks, don't support redirects curl_easy_setopt(m_Curl, CURLOPT_FOLLOWLOCATION, 0L); // Disable the progress meter curl_easy_setopt(m_Curl, CURLOPT_NOPROGRESS, 1L); // Set IO callbacks curl_easy_setopt(m_Curl, CURLOPT_WRITEFUNCTION, ReceiveCallback); curl_easy_setopt(m_Curl, CURLOPT_WRITEDATA, this); } m_ErrorBuffer[0] = '\0'; curl_easy_setopt(m_Curl, CURLOPT_URL, url.c_str()); return curl_multi_add_handle(m_CurlMulti, m_Curl); } void ModIo::TearDownRequest() { ENSURE(curl_multi_remove_handle(m_CurlMulti, m_Curl) == CURLM_OK); if (m_CallbackData) { if (m_CallbackData->fp) fclose(m_CallbackData->fp); m_CallbackData->fp = nullptr; } } void ModIo::StartGetGameId() { // Don't start such a request during active downloads. if (m_DownloadProgressData.status == DownloadProgressStatus::GAMEID || m_DownloadProgressData.status == DownloadProgressStatus::LISTING || m_DownloadProgressData.status == DownloadProgressStatus::DOWNLOADING) return; m_GameId.clear(); CURLMcode err = SetupRequest(m_BaseUrl+m_GamesRequest+"?"+m_ApiKey+"&"+m_IdQuery, false); if (err != CURLM_OK) { TearDownRequest(); m_DownloadProgressData.status = DownloadProgressStatus::FAILED_GAMEID; m_DownloadProgressData.error = fmt::sprintf( g_L10n.Translate("Failure while starting querying for game id. Error: %s; %s."), curl_multi_strerror(err), m_ErrorBuffer); return; } m_DownloadProgressData.status = DownloadProgressStatus::GAMEID; } void ModIo::StartListMods() { // Don't start such a request during active downloads. if (m_DownloadProgressData.status == DownloadProgressStatus::GAMEID || m_DownloadProgressData.status == DownloadProgressStatus::LISTING || m_DownloadProgressData.status == DownloadProgressStatus::DOWNLOADING) return; m_ModData.clear(); if (m_GameId.empty()) { LOGERROR("Game ID not fetched from mod.io. Call StartGetGameId first and wait for it to finish."); return; } CURLMcode err = SetupRequest(m_BaseUrl+m_GamesRequest+m_GameId+"/mods?"+m_ApiKey, false); if (err != CURLM_OK) { TearDownRequest(); m_DownloadProgressData.status = DownloadProgressStatus::FAILED_LISTING; m_DownloadProgressData.error = fmt::sprintf( g_L10n.Translate("Failure while starting querying for mods. Error: %s; %s."), curl_multi_strerror(err), m_ErrorBuffer); return; } m_DownloadProgressData.status = DownloadProgressStatus::LISTING; } void ModIo::StartDownloadMod(u32 idx) { // Don't start such a request during active downloads. if (m_DownloadProgressData.status == DownloadProgressStatus::GAMEID || m_DownloadProgressData.status == DownloadProgressStatus::LISTING || m_DownloadProgressData.status == DownloadProgressStatus::DOWNLOADING) return; if (idx >= m_ModData.size()) return; const Paths paths(g_CmdLineArgs); const OsPath modUserPath = paths.UserData()/"mods"; const OsPath modPath = modUserPath/m_ModData[idx].properties["name_id"]; if (!DirectoryExists(modPath) && INFO::OK != CreateDirectories(modPath, 0700, false)) { m_DownloadProgressData.status = DownloadProgressStatus::FAILED_DOWNLOADING; m_DownloadProgressData.error = fmt::sprintf( g_L10n.Translate("Could not create mod directory: %s."), modPath.string8()); return; } // Name the file after the name_id, since using the filename would mean that // we could end up with multiple zip files in the folder that might not work // as expected for a user (since a later version might remove some files // that aren't compatible anymore with the engine version). // So we ignore the filename provided by the API and assume that we do not // care about handling update.zip files. If that is the case we would need // a way to find out what files are required by the current one and which // should be removed for everything to work. This seems to be too complicated // so we just do not support that usage. // NOTE: We do save the file under a slightly different name from the final // one, to ensure that in case a download aborts and the file stays // around, the game will not attempt to open the file which has not // been verified. m_DownloadFilePath = modPath/(m_ModData[idx].properties["name_id"]+".zip.temp"); delete m_CallbackData; m_CallbackData = new DownloadCallbackData(sys_OpenFile(m_DownloadFilePath, "wb")); if (!m_CallbackData->fp) { m_DownloadProgressData.status = DownloadProgressStatus::FAILED_DOWNLOADING; m_DownloadProgressData.error = fmt::sprintf( g_L10n.Translate("Could not open temporary file for mod download: %s."), m_DownloadFilePath.string8()); return; } CURLMcode err = SetupRequest(m_ModData[idx].properties["binary_url"], true); if (err != CURLM_OK) { TearDownRequest(); m_DownloadProgressData.status = DownloadProgressStatus::FAILED_DOWNLOADING; m_DownloadProgressData.error = fmt::sprintf( g_L10n.Translate("Failed to start the download. Error: %s; %s."), curl_multi_strerror(err), m_ErrorBuffer); return; } m_DownloadModID = idx; m_DownloadProgressData.status = DownloadProgressStatus::DOWNLOADING; } void ModIo::CancelRequest() { TearDownRequest(); switch (m_DownloadProgressData.status) { case DownloadProgressStatus::GAMEID: case DownloadProgressStatus::FAILED_GAMEID: m_DownloadProgressData.status = DownloadProgressStatus::NONE; break; case DownloadProgressStatus::LISTING: case DownloadProgressStatus::FAILED_LISTING: m_DownloadProgressData.status = DownloadProgressStatus::READY; break; case DownloadProgressStatus::DOWNLOADING: case DownloadProgressStatus::FAILED_DOWNLOADING: m_DownloadProgressData.status = DownloadProgressStatus::LISTED; DeleteDownloadedFile(); break; default: break; } } bool ModIo::AdvanceRequest(const ScriptInterface& scriptInterface) { // If the request was cancelled, stop trying to advance it if (m_DownloadProgressData.status != DownloadProgressStatus::GAMEID && m_DownloadProgressData.status != DownloadProgressStatus::LISTING && m_DownloadProgressData.status != DownloadProgressStatus::DOWNLOADING) return true; int stillRunning; CURLMcode err = curl_multi_perform(m_CurlMulti, &stillRunning); if (err != CURLM_OK) { std::string error = fmt::sprintf( g_L10n.Translate("Asynchronous download failure: %s, %s."), curl_multi_strerror(err), m_ErrorBuffer); TearDownRequest(); if (m_DownloadProgressData.status == DownloadProgressStatus::GAMEID) m_DownloadProgressData.status = DownloadProgressStatus::FAILED_GAMEID; else if (m_DownloadProgressData.status == DownloadProgressStatus::LISTING) m_DownloadProgressData.status = DownloadProgressStatus::FAILED_LISTING; else if (m_DownloadProgressData.status == DownloadProgressStatus::DOWNLOADING) { m_DownloadProgressData.status = DownloadProgressStatus::FAILED_DOWNLOADING; DeleteDownloadedFile(); } m_DownloadProgressData.error = error; return true; } CURLMsg* message; do { int in_queue; message = curl_multi_info_read(m_CurlMulti, &in_queue); if (!message) continue; if (message->data.result == CURLE_OK) continue; std::string error = fmt::sprintf( g_L10n.Translate("Download failure. Server response: %s; %s."), curl_easy_strerror(message->data.result), m_ErrorBuffer); TearDownRequest(); if (m_DownloadProgressData.status == DownloadProgressStatus::GAMEID) m_DownloadProgressData.status = DownloadProgressStatus::FAILED_GAMEID; else if (m_DownloadProgressData.status == DownloadProgressStatus::LISTING) m_DownloadProgressData.status = DownloadProgressStatus::FAILED_LISTING; else if (m_DownloadProgressData.status == DownloadProgressStatus::DOWNLOADING) { m_DownloadProgressData.status = DownloadProgressStatus::FAILED_DOWNLOADING; DeleteDownloadedFile(); } m_DownloadProgressData.error = error; return true; } while (message); if (stillRunning) return false; // Download finished. TearDownRequest(); // Perform parsing and/or checks std::string error; switch (m_DownloadProgressData.status) { case DownloadProgressStatus::GAMEID: if (!ParseGameId(scriptInterface, error)) { m_DownloadProgressData.status = DownloadProgressStatus::FAILED_GAMEID; m_DownloadProgressData.error = error; break; } m_DownloadProgressData.status = DownloadProgressStatus::READY; break; case DownloadProgressStatus::LISTING: if (!ParseMods(scriptInterface, error)) { m_ModData.clear(); // Failed during parsing, make sure we don't provide partial data m_DownloadProgressData.status = DownloadProgressStatus::FAILED_LISTING; m_DownloadProgressData.error = error; break; } m_DownloadProgressData.status = DownloadProgressStatus::LISTED; break; case DownloadProgressStatus::DOWNLOADING: if (!VerifyDownloadedFile(error)) { m_DownloadProgressData.status = DownloadProgressStatus::FAILED_FILECHECK; m_DownloadProgressData.error = error; DeleteDownloadedFile(); break; } m_DownloadProgressData.status = DownloadProgressStatus::SUCCESS; { Paths paths(g_CmdLineArgs); CModInstaller installer(paths.UserData() / "mods", paths.Cache()); installer.Install(m_DownloadFilePath, g_ScriptContext, false); } break; default: break; } return true; } bool ModIo::ParseGameId(const ScriptInterface& scriptInterface, std::string& err) { int id = -1; bool ret = ParseGameIdResponse(scriptInterface, m_ResponseData, id, err); m_ResponseData.clear(); if (!ret) return false; m_GameId = "/" + std::to_string(id); return true; } bool ModIo::ParseMods(const ScriptInterface& scriptInterface, std::string& err) { bool ret = ParseModsResponse(scriptInterface, m_ResponseData, m_ModData, m_pk, err); m_ResponseData.clear(); return ret; } void ModIo::DeleteDownloadedFile() { if (wunlink(m_DownloadFilePath) != 0) LOGERROR("Failed to delete temporary file."); m_DownloadFilePath = OsPath(); } bool ModIo::VerifyDownloadedFile(std::string& err) { // Verify filesize, as a first basic download check. { u64 filesize = std::stoull(m_ModData[m_DownloadModID].properties.at("filesize")); if (filesize != FileSize(m_DownloadFilePath)) { err = g_L10n.Translate("Mismatched filesize."); return false; } } ENSURE(m_CallbackData); // MD5 (because upstream provides it) // Just used to make sure there was no obvious corruption during transfer. { u8 digest[MD5::DIGESTSIZE]; m_CallbackData->md5.Final(digest); std::string md5digest = Hexify(digest, MD5::DIGESTSIZE); if (m_ModData[m_DownloadModID].properties.at("filehash_md5") != md5digest) { err = fmt::sprintf( g_L10n.Translate("Invalid file. Expected md5 %s, got %s."), m_ModData[m_DownloadModID].properties.at("filehash_md5").c_str(), md5digest); return false; } } // Verify file signature. // Used to make sure that the downloaded file was actually checked and signed // by Wildfire Games. And has not been tampered with by the API provider, or the CDN. unsigned char hash_fin[crypto_generichash_BYTES_MAX] = {}; ENSURE(m_CallbackData->hash_state); if (crypto_generichash_final(m_CallbackData->hash_state, hash_fin, sizeof hash_fin) != 0) { err = g_L10n.Translate("Failed to compute final hash."); return false; } if (crypto_sign_verify_detached(m_ModData[m_DownloadModID].sig.sig, hash_fin, sizeof hash_fin, m_pk.pk) != 0) { err = g_L10n.Translate("Failed to verify signature."); return false; } return true; } #define FAIL(...) STMT(err = fmt::sprintf(__VA_ARGS__); CLEANUP(); return false;) /** * Parses the current content of m_ResponseData to extract m_GameId. * * The JSON data is expected to look like * { "data": [{"id": 42, ...}, ...], ... } * where we are only interested in the value of the id property. * * @returns true iff it successfully parsed the id. */ bool ModIo::ParseGameIdResponse(const ScriptInterface& scriptInterface, const std::string& responseData, int& id, std::string& err) { #define CLEANUP() id = -1; ScriptRequest rq(scriptInterface); JS::RootedValue gameResponse(rq.cx); if (!Script::ParseJSON(rq, responseData, &gameResponse)) FAIL("Failed to parse response as JSON."); if (!gameResponse.isObject()) FAIL("response not an object."); JS::RootedObject gameResponseObj(rq.cx, gameResponse.toObjectOrNull()); JS::RootedValue dataVal(rq.cx); if (!JS_GetProperty(rq.cx, gameResponseObj, "data", &dataVal)) FAIL("data property not in response."); // [{"id": 42, ...}, ...] if (!dataVal.isObject()) FAIL("data property not an object."); JS::RootedObject data(rq.cx, dataVal.toObjectOrNull()); u32 length; bool isArray; if (!JS::IsArrayObject(rq.cx, data, &isArray) || !isArray || !JS::GetArrayLength(rq.cx, data, &length) || !length) FAIL("data property not an array with at least one element."); // {"id": 42, ...} JS::RootedValue first(rq.cx); if (!JS_GetElement(rq.cx, data, 0, &first)) FAIL("Couldn't get first element."); if (!first.isObject()) FAIL("First element not an object."); JS::RootedObject firstObj(rq.cx, &first.toObject()); bool hasIdProperty; if (!JS_HasProperty(rq.cx, firstObj, "id", &hasIdProperty) || !hasIdProperty) FAIL("No id property in first element."); JS::RootedValue idProperty(rq.cx); ENSURE(JS_GetProperty(rq.cx, firstObj, "id", &idProperty)); // Make sure the property is not set to something that could be converted to a bogus value // TODO: We should be able to convert JS::Values to C++ variables in a way that actually // fails when types do not match (see https://trac.wildfiregames.com/ticket/5128). if (!idProperty.isNumber()) FAIL("id property not a number."); id = -1; if (!Script::FromJSVal(rq, idProperty, id) || id <= 0) FAIL("Invalid id."); return true; #undef CLEANUP } /** * Parses the current content of m_ResponseData into m_ModData. * * The JSON data is expected to look like * { data: [modobj1, modobj2, ...], ... (including result_count) } * where modobjN has the following structure * { homepage_url: "url", name: "displayname", nameid: "short-non-whitespace-name", * summary: "short desc.", modfile: { version: "1.2.4", filename: "asdf.zip", * filehash: { md5: "deadbeef" }, filesize: 1234, download: { binary_url: "someurl", ... } }, ... }. * Only the listed properties are of interest to consumers, and we flatten * the modfile structure as that simplifies handling and there are no conflicts. */ bool ModIo::ParseModsResponse(const ScriptInterface& scriptInterface, const std::string& responseData, std::vector& modData, const PKStruct& pk, std::string& err) { // Make sure we don't end up passing partial results back #define CLEANUP() modData.clear(); ScriptRequest rq(scriptInterface); JS::RootedValue modResponse(rq.cx); if (!Script::ParseJSON(rq, responseData, &modResponse)) FAIL("Failed to parse response as JSON."); if (!modResponse.isObject()) FAIL("response not an object."); JS::RootedObject modResponseObj(rq.cx, modResponse.toObjectOrNull()); JS::RootedValue dataVal(rq.cx); if (!JS_GetProperty(rq.cx, modResponseObj, "data", &dataVal)) FAIL("data property not in response."); // [modobj1, modobj2, ... ] if (!dataVal.isObject()) FAIL("data property not an object."); JS::RootedObject rData(rq.cx, dataVal.toObjectOrNull()); u32 length; bool isArray; if (!JS::IsArrayObject(rq.cx, rData, &isArray) || !isArray || !JS::GetArrayLength(rq.cx, rData, &length) || !length) FAIL("data property not an array with at least one element."); modData.clear(); modData.reserve(length); #define INVALIDATE_DATA_AND_CONTINUE(...) \ {\ data.properties.emplace("invalid", "true");\ data.properties.emplace("error", __VA_ARGS__);\ continue;\ } for (u32 i = 0; i < length; ++i) { modData.emplace_back(); ModIoModData& data = modData.back(); JS::RootedValue el(rq.cx); if (!JS_GetElement(rq.cx, rData, i, &el) || !el.isObject()) INVALIDATE_DATA_AND_CONTINUE("Failed to get array element object.") bool ok = true; std::string copyStringError; #define COPY_STRINGS_ELSE_CONTINUE(prefix, obj, ...) \ for (const std::string& prop : { __VA_ARGS__ }) \ { \ std::string val; \ if (!Script::FromJSProperty(rq, obj, prop.c_str(), val, true)) \ { \ ok = false; \ copyStringError = "Failed to get " + prop + " from " + #obj + "."; \ break; \ }\ data.properties.emplace(prefix+prop, val); \ } \ if (!ok) \ INVALIDATE_DATA_AND_CONTINUE(copyStringError); // TODO: Currently the homepage_url field does not contain a non-null value for any entry. COPY_STRINGS_ELSE_CONTINUE("", el, "name", "name_id", "summary") // Now copy over the modfile part, but without the pointless substructure JS::RootedObject elObj(rq.cx, el.toObjectOrNull()); JS::RootedValue modFile(rq.cx); if (!JS_GetProperty(rq.cx, elObj, "modfile", &modFile)) INVALIDATE_DATA_AND_CONTINUE("Failed to get modfile data."); if (!modFile.isObject()) INVALIDATE_DATA_AND_CONTINUE("modfile not an object."); COPY_STRINGS_ELSE_CONTINUE("", modFile, "version", "filesize"); JS::RootedObject modFileObj(rq.cx, modFile.toObjectOrNull()); JS::RootedValue filehash(rq.cx); if (!JS_GetProperty(rq.cx, modFileObj, "filehash", &filehash)) INVALIDATE_DATA_AND_CONTINUE("Failed to get filehash data."); COPY_STRINGS_ELSE_CONTINUE("filehash_", filehash, "md5"); JS::RootedValue download(rq.cx); if (!JS_GetProperty(rq.cx, modFileObj, "download", &download)) INVALIDATE_DATA_AND_CONTINUE("Failed to get download data."); COPY_STRINGS_ELSE_CONTINUE("", download, "binary_url"); // Parse metadata_blob (sig+deps) std::string metadata_blob; if (!Script::FromJSProperty(rq, modFile, "metadata_blob", metadata_blob, true)) INVALIDATE_DATA_AND_CONTINUE("Failed to get metadata_blob from modFile."); JS::RootedValue metadata(rq.cx); if (!Script::ParseJSON(rq, metadata_blob, &metadata)) INVALIDATE_DATA_AND_CONTINUE("Failed to parse metadata_blob as JSON."); if (!metadata.isObject()) INVALIDATE_DATA_AND_CONTINUE("metadata_blob is not decoded as an object."); if (!Script::FromJSProperty(rq, metadata, "dependencies", data.dependencies, true)) INVALIDATE_DATA_AND_CONTINUE("Failed to get dependencies from metadata_blob."); std::vector minisigs; if (!Script::FromJSProperty(rq, metadata, "minisigs", minisigs, true)) INVALIDATE_DATA_AND_CONTINUE("Failed to get minisigs from metadata_blob."); // Check we did find a valid matching signature. std::string signatureParsingErr; if (!ParseSignature(minisigs, data.sig, pk, signatureParsingErr)) INVALIDATE_DATA_AND_CONTINUE(signatureParsingErr); #undef COPY_STRINGS_ELSE_CONTINUE #undef INVALIDATE_DATA_AND_CONTINUE } return true; #undef CLEANUP } /** * Parse signatures to find one that matches the public key, and has a valid global signature. * Returns true and sets @param sig to the valid matching signature. */ bool ModIo::ParseSignature(const std::vector& minisigs, SigStruct& sig, const PKStruct& pk, std::string& err) { #define CLEANUP() sig = {}; for (const std::string& file_sig : minisigs) { // Format of a .minisig file (created using minisign(1) with -SHm file.zip) // untrusted comment: .*\nb64sign_of_file\ntrusted comment: .*\nb64sign_of_sign_of_file_and_trusted_comment std::vector sig_lines; boost::split(sig_lines, file_sig, boost::is_any_of("\n")); if (sig_lines.size() < 4) FAIL("Invalid (too short) sig."); // Verify that both the untrusted comment and the trusted comment start with the correct prefix // because that is easy. const std::string untrusted_comment_prefix = "untrusted comment: "; const std::string trusted_comment_prefix = "trusted comment: "; if (!boost::algorithm::starts_with(sig_lines[0], untrusted_comment_prefix)) FAIL("Malformed untrusted comment."); if (!boost::algorithm::starts_with(sig_lines[2], trusted_comment_prefix)) FAIL("Malformed trusted comment."); // We only _really_ care about the second line which is the signature of the file (b64-encoded) // Also handling the other signature is nice, but not really required. const std::string& msg_sig = sig_lines[1]; size_t bin_len = 0; if (sodium_base642bin((unsigned char*)&sig, sizeof sig, msg_sig.c_str(), msg_sig.size(), NULL, &bin_len, NULL, sodium_base64_VARIANT_ORIGINAL) != 0 || bin_len != sizeof sig) FAIL("Failed to decode base64 sig."); cassert(sizeof pk.keynum == sizeof sig.keynum); if (memcmp(&pk.keynum, &sig.keynum, sizeof sig.keynum) != 0) continue; // mismatched key, try another one if (memcmp(&sig.sig_alg, "ED", 2) != 0) FAIL("Only hashed minisign signatures are supported."); // Signature matches our public key // Now verify the global signature (sig || trusted_comment) unsigned char global_sig[crypto_sign_BYTES]; if (sodium_base642bin(global_sig, sizeof global_sig, sig_lines[3].c_str(), sig_lines[3].size(), NULL, &bin_len, NULL, sodium_base64_VARIANT_ORIGINAL) != 0 || bin_len != sizeof global_sig) FAIL("Failed to decode base64 global_sig."); const std::string trusted_comment = sig_lines[2].substr(trusted_comment_prefix.size()); unsigned char* sig_and_trusted_comment = (unsigned char*)sodium_malloc((sizeof sig.sig) + trusted_comment.size()); if (!sig_and_trusted_comment) FAIL("sodium_malloc failed."); memcpy(sig_and_trusted_comment, sig.sig, sizeof sig.sig); memcpy(sig_and_trusted_comment + sizeof sig.sig, trusted_comment.data(), trusted_comment.size()); if (crypto_sign_verify_detached(global_sig, sig_and_trusted_comment, (sizeof sig.sig) + trusted_comment.size(), pk.pk) != 0) { err = "Failed to verify global signature."; sodium_free(sig_and_trusted_comment); return false; } sodium_free(sig_and_trusted_comment); // Valid global sig, and the keynum matches the real one return true; } return false; #undef CLEANUP } #undef FAIL Index: ps/trunk/source/ps/Replay.cpp =================================================================== --- ps/trunk/source/ps/Replay.cpp (revision 25473) +++ ps/trunk/source/ps/Replay.cpp (revision 25474) @@ -1,340 +1,367 @@ /* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "Replay.h" #include "graphics/TerrainTextureManager.h" #include "lib/timer.h" #include "lib/file/file_system.h" #include "lib/res/h_mgr.h" #include "lib/tex/tex.h" -#include "ps/Game.h" #include "ps/CLogger.h" +#include "ps/Game.h" +#include "ps/GameSetup/GameSetup.h" +#include "ps/GameSetup/CmdLineArgs.h" +#include "ps/GameSetup/Paths.h" #include "ps/Loader.h" #include "ps/Mod.h" #include "ps/Profile.h" #include "ps/ProfileViewer.h" #include "ps/Pyrogenesis.h" #include "ps/Mod.h" #include "ps/Util.h" #include "ps/VisualReplay.h" #include "scriptinterface/Object.h" #include "scriptinterface/ScriptContext.h" +#include "scriptinterface/ScriptInterface.h" #include "scriptinterface/ScriptRequest.h" #include "scriptinterface/ScriptStats.h" #include "scriptinterface/JSON.h" #include "simulation2/components/ICmpGuiInterface.h" #include "simulation2/helpers/Player.h" #include "simulation2/helpers/SimulationCommand.h" #include "simulation2/Simulation2.h" #include "simulation2/system/CmpPtr.h" #include #include /** * Number of turns between two saved profiler snapshots. * Keep in sync with source/tools/replayprofile/graph.js */ static const int PROFILE_TURN_INTERVAL = 20; CReplayLogger::CReplayLogger(const ScriptInterface& scriptInterface) : m_ScriptInterface(scriptInterface), m_Stream(NULL) { } CReplayLogger::~CReplayLogger() { delete m_Stream; } void CReplayLogger::StartGame(JS::MutableHandleValue attribs) { ScriptRequest rq(m_ScriptInterface); // Add timestamp, since the file-modification-date can change Script::SetProperty(rq, attribs, "timestamp", (double)std::time(nullptr)); // Add engine version and currently loaded mods for sanity checks when replaying Script::SetProperty(rq, attribs, "engine_version", engine_version); JS::RootedValue mods(rq.cx, g_Mods.GetLoadedModsWithVersions(m_ScriptInterface)); Script::SetProperty(rq, attribs, "mods", mods); m_Directory = createDateIndexSubdirectory(VisualReplay::GetDirectoryPath()); debug_printf("Writing replay to %s\n", m_Directory.string8().c_str()); m_Stream = new std::ofstream(OsString(m_Directory / L"commands.txt").c_str(), std::ofstream::out | std::ofstream::trunc); *m_Stream << "start " << Script::StringifyJSON(rq, attribs, false) << "\n"; } void CReplayLogger::Turn(u32 n, u32 turnLength, std::vector& commands) { ScriptRequest rq(m_ScriptInterface); *m_Stream << "turn " << n << " " << turnLength << "\n"; for (SimulationCommand& command : commands) *m_Stream << "cmd " << command.player << " " << Script::StringifyJSON(rq, &command.data, false) << "\n"; *m_Stream << "end\n"; m_Stream->flush(); } void CReplayLogger::Hash(const std::string& hash, bool quick) { if (quick) *m_Stream << "hash-quick " << Hexify(hash) << "\n"; else *m_Stream << "hash " << Hexify(hash) << "\n"; } void CReplayLogger::SaveMetadata(const CSimulation2& simulation) { CmpPtr cmpGuiInterface(simulation, SYSTEM_ENTITY); if (!cmpGuiInterface) { LOGERROR("Could not save replay metadata!"); return; } ScriptInterface& scriptInterface = simulation.GetScriptInterface(); ScriptRequest rq(scriptInterface); JS::RootedValue arg(rq.cx); JS::RootedValue metadata(rq.cx); cmpGuiInterface->ScriptCall(INVALID_PLAYER, L"GetReplayMetadata", arg, &metadata); const OsPath fileName = g_Game->GetReplayLogger().GetDirectory() / L"metadata.json"; CreateDirectories(fileName.Parent(), 0700); std::ofstream stream (OsString(fileName).c_str(), std::ofstream::out | std::ofstream::trunc); stream << Script::StringifyJSON(rq, &metadata, false); stream.close(); debug_printf("Saved replay metadata to %s\n", fileName.string8().c_str()); } OsPath CReplayLogger::GetDirectory() const { return m_Directory; } //////////////////////////////////////////////////////////////// CReplayPlayer::CReplayPlayer() : m_Stream(NULL) { } CReplayPlayer::~CReplayPlayer() { delete m_Stream; } void CReplayPlayer::Load(const OsPath& path) { ENSURE(!m_Stream); m_Stream = new std::ifstream(OsString(path).c_str()); ENSURE(m_Stream->good()); } CStr CReplayPlayer::ModListToString(const std::vector>& list) const { CStr text; for (const std::vector& mod : list) text += mod[0] + " (" + mod[1] + ")\n"; return text; } void CReplayPlayer::CheckReplayMods(const ScriptInterface& scriptInterface, JS::HandleValue attribs) const { ScriptRequest rq(scriptInterface); std::vector> replayMods; Script::GetProperty(rq, attribs, "mods", replayMods); std::vector> enabledMods; JS::RootedValue enabledModsJS(rq.cx, g_Mods.GetLoadedModsWithVersions(scriptInterface)); Script::FromJSVal(rq, enabledModsJS, enabledMods); CStr warn; if (replayMods.size() != enabledMods.size()) warn = "The number of enabled mods does not match the mods of the replay."; else for (size_t i = 0; i < replayMods.size(); ++i) { if (replayMods[i][0] != enabledMods[i][0]) { warn = "The enabled mods don't match the mods of the replay."; break; } else if (replayMods[i][1] != enabledMods[i][1]) { warn = "The mod '" + replayMods[i][0] + "' with version '" + replayMods[i][1] + "' is required by the replay file, but version '" + enabledMods[i][1] + "' is present!"; break; } } if (!warn.empty()) LOGWARNING("%s\nThe mods of the replay are:\n%s\nThese mods are enabled:\n%s", warn, ModListToString(replayMods), ModListToString(enabledMods)); } void CReplayPlayer::Replay(const bool serializationtest, const int rejointestturn, const bool ooslog, const bool testHashFull, const bool testHashQuick) { ENSURE(m_Stream); new CProfileViewer; new CProfileManager; g_ScriptStatsTable = new CScriptStatsTable; g_ProfileViewer.AddRootTable(g_ScriptStatsTable); const int contextSize = 384 * 1024 * 1024; const int heapGrowthBytesGCTrigger = 20 * 1024 * 1024; g_ScriptContext = ScriptContext::CreateContext(contextSize, heapGrowthBytesGCTrigger); - g_Mods.CacheEnabledModVersions(g_ScriptContext); - - g_Game = new CGame(false); - if (serializationtest) - g_Game->GetSimulation2()->EnableSerializationTest(); - if (rejointestturn >= 0) - g_Game->GetSimulation2()->EnableRejoinTest(rejointestturn); - if (ooslog) - g_Game->GetSimulation2()->EnableOOSLog(); - - // Need some stuff for terrain movement costs: - // (TODO: this ought to be independent of any graphics code) - new CTerrainTextureManager; - g_TexMan.LoadTerrainTextures(); - - // Initialise h_mgr so it doesn't crash when emitting sounds - h_mgr_init(); - std::vector commands; u32 turn = 0; u32 turnLength = 0; { - ScriptRequest rq(g_Game->GetSimulation2()->GetScriptInterface()); std::string type; while ((*m_Stream >> type).good()) { if (type == "start") { - std::string line; - std::getline(*m_Stream, line); - JS::RootedValue attribs(rq.cx); - ENSURE(Script::ParseJSON(rq, line, &attribs)); + std::string attribsStr; + { + // TODO: it'd be nice to not create a scriptInterface to load JSON. + ScriptInterface scriptInterface("Engine", "Replay", g_ScriptContext); + ScriptRequest rq(scriptInterface); + std::getline(*m_Stream, attribsStr); + JS::RootedValue attribs(rq.cx); + if (!Script::ParseJSON(rq, attribsStr, &attribs)) + { + LOGERROR("Error parsing JSON attributes: %s", attribsStr); + // TODO: do something cleverer than crashing. + ENSURE(false); + } + + // Load the mods specified in the replay. + std::vector> replayMods; + Script::GetProperty(rq, attribs, "mods", replayMods); + std::vector mods; + for (const std::vector& ModAndVersion : replayMods) + if (!ModAndVersion.empty()) + mods.emplace_back(ModAndVersion[0]); + + // Ignore the return value, we check below. + g_Mods.EnableMods(scriptInterface, mods, false); + MountMods(Paths(g_CmdLineArgs), g_Mods.GetEnabledMods()); + + CheckReplayMods(scriptInterface, attribs); + } + + g_Game = new CGame(false); + if (serializationtest) + g_Game->GetSimulation2()->EnableSerializationTest(); + if (rejointestturn >= 0) + g_Game->GetSimulation2()->EnableRejoinTest(rejointestturn); + if (ooslog) + g_Game->GetSimulation2()->EnableOOSLog(); + + // Need some stuff for terrain movement costs: + // (TODO: this ought to be independent of any graphics code) + new CTerrainTextureManager; + g_TexMan.LoadTerrainTextures(); - CheckReplayMods(g_Game->GetSimulation2()->GetScriptInterface(), attribs); + // Initialise h_mgr so it doesn't crash when emitting sounds + h_mgr_init(); + ScriptRequest rq(g_Game->GetSimulation2()->GetScriptInterface()); + JS::RootedValue attribs(rq.cx); + ENSURE(Script::ParseJSON(rq, attribsStr, &attribs)); g_Game->StartGame(&attribs, ""); // TODO: Non progressive load can fail - need a decent way to handle this LDR_NonprogressiveLoad(); PSRETURN ret = g_Game->ReallyStartGame(); ENSURE(ret == PSRETURN_OK); } else if (type == "turn") { *m_Stream >> turn >> turnLength; debug_printf("Turn %u (%u)...\n", turn, turnLength); } else if (type == "cmd") { player_id_t player; *m_Stream >> player; std::string line; std::getline(*m_Stream, line); + ScriptRequest rq(g_Game->GetSimulation2()->GetScriptInterface()); JS::RootedValue data(rq.cx); Script::ParseJSON(rq, line, &data); Script::FreezeObject(rq, data, true); commands.emplace_back(SimulationCommand(player, rq.cx, data)); } else if (type == "hash" || type == "hash-quick") { std::string replayHash; *m_Stream >> replayHash; TestHash(type, replayHash, testHashFull, testHashQuick); } else if (type == "end") { { g_Profiler2.RecordFrameStart(); PROFILE2("frame"); g_Profiler2.IncrementFrameNumber(); PROFILE2_ATTR("%d", g_Profiler2.GetFrameNumber()); g_Game->GetSimulation2()->Update(turnLength, commands); commands.clear(); } g_Profiler.Frame(); if (turn % PROFILE_TURN_INTERVAL == 0) g_ProfileViewer.SaveToFile(); } else debug_printf("Unrecognised replay token %s\n", type.c_str()); } } SAFE_DELETE(m_Stream); g_Profiler2.SaveToFile(); std::string hash; bool ok = g_Game->GetSimulation2()->ComputeStateHash(hash, false); ENSURE(ok); debug_printf("# Final state: %s\n", Hexify(hash).c_str()); timer_DisplayClientTotals(); SAFE_DELETE(g_Game); // Must be explicitly destructed here to avoid callbacks from the JSAPI trying to use g_Profiler2 when // it's already destructed. g_ScriptContext.reset(); // Clean up delete &g_TexMan; delete &g_Profiler; delete &g_ProfileViewer; SAFE_DELETE(g_ScriptStatsTable); } void CReplayPlayer::TestHash(const std::string& hashType, const std::string& replayHash, const bool testHashFull, const bool testHashQuick) { bool quick = (hashType == "hash-quick"); if ((quick && !testHashQuick) || (!quick && !testHashFull)) return; std::string hash; ENSURE(g_Game->GetSimulation2()->ComputeStateHash(hash, quick)); std::string hexHash = Hexify(hash); if (hexHash == replayHash) debug_printf("%s ok (%s)\n", hashType.c_str(), hexHash.c_str()); else debug_printf("%s MISMATCH (%s != %s)\n", hashType.c_str(), hexHash.c_str(), replayHash.c_str()); } Index: ps/trunk/source/ps/Replay.h =================================================================== --- ps/trunk/source/ps/Replay.h (revision 25473) +++ ps/trunk/source/ps/Replay.h (revision 25474) @@ -1,119 +1,121 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #ifndef INCLUDED_REPLAY #define INCLUDED_REPLAY #include "lib/os_path.h" #include "ps/CStr.h" #include "scriptinterface/ScriptTypes.h" +#include + struct SimulationCommand; class CSimulation2; class ScriptInterface; /** * Replay log recorder interface. * Call its methods at appropriate times during the game. */ class IReplayLogger { public: IReplayLogger() { } virtual ~IReplayLogger() { } /** * Started the game with the given game attributes. */ virtual void StartGame(JS::MutableHandleValue attribs) = 0; /** * Run the given turn with the given collection of player commands. */ virtual void Turn(u32 n, u32 turnLength, std::vector& commands) = 0; /** * Optional hash of simulation state (for sync checking). */ virtual void Hash(const std::string& hash, bool quick) = 0; /** * Saves metadata.json containing part of the simulation state used for the summary screen. */ virtual void SaveMetadata(const CSimulation2& simulation) = 0; /** * Remember the directory containing the commands.txt file, so that we can save additional files to it. */ virtual OsPath GetDirectory() const = 0; }; /** * Implementation of IReplayLogger that simply throws away all data. */ class CDummyReplayLogger : public IReplayLogger { public: virtual void StartGame(JS::MutableHandleValue UNUSED(attribs)) { } virtual void Turn(u32 UNUSED(n), u32 UNUSED(turnLength), std::vector& UNUSED(commands)) { } virtual void Hash(const std::string& UNUSED(hash), bool UNUSED(quick)) { } virtual void SaveMetadata(const CSimulation2& UNUSED(simulation)) { }; virtual OsPath GetDirectory() const { return OsPath(); } }; /** * Implementation of IReplayLogger that saves data to a file in the logs directory. */ class CReplayLogger : public IReplayLogger { NONCOPYABLE(CReplayLogger); public: CReplayLogger(const ScriptInterface& scriptInterface); ~CReplayLogger(); virtual void StartGame(JS::MutableHandleValue attribs); virtual void Turn(u32 n, u32 turnLength, std::vector& commands); virtual void Hash(const std::string& hash, bool quick); virtual void SaveMetadata(const CSimulation2& simulation); virtual OsPath GetDirectory() const; private: const ScriptInterface& m_ScriptInterface; std::ostream* m_Stream; OsPath m_Directory; }; /** * Replay log replayer. Runs the log with no graphics and dumps some info to stdout. */ class CReplayPlayer { public: CReplayPlayer(); ~CReplayPlayer(); void Load(const OsPath& path); void Replay(const bool serializationtest, const int rejointestturn, const bool ooslog, const bool testHashFull, const bool testHashQuick); private: std::istream* m_Stream; CStr ModListToString(const std::vector>& list) const; void CheckReplayMods(const ScriptInterface& scriptInterface, JS::HandleValue attribs) const; void TestHash(const std::string& hashType, const std::string& replayHash, const bool testHashFull, const bool testHashQuick); }; #endif // INCLUDED_REPLAY Index: ps/trunk/source/ps/VisualReplay.cpp =================================================================== --- ps/trunk/source/ps/VisualReplay.cpp (revision 25473) +++ ps/trunk/source/ps/VisualReplay.cpp (revision 25474) @@ -1,507 +1,508 @@ /* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "VisualReplay.h" #include "graphics/GameView.h" #include "lib/timer.h" #include "lib/utf8.h" #include "lib/allocators/shared_ptr.h" #include "lib/external_libraries/libsdl.h" #include "network/NetClient.h" #include "network/NetServer.h" #include "ps/CLogger.h" #include "ps/Filesystem.h" #include "ps/Game.h" +#include "ps/GameSetup/CmdLineArgs.h" #include "ps/GameSetup/Paths.h" #include "ps/Mod.h" #include "ps/Pyrogenesis.h" #include "ps/Replay.h" #include "ps/Util.h" #include "scriptinterface/JSON.h" #include /** * Filter too short replays (value in seconds). */ const u8 minimumReplayDuration = 3; OsPath VisualReplay::GetDirectoryPath() { return Paths(g_CmdLineArgs).UserData() / "replays" / engine_version; } OsPath VisualReplay::GetCacheFilePath() { return GetDirectoryPath() / L"replayCache.json"; } OsPath VisualReplay::GetTempCacheFilePath() { return GetDirectoryPath() / L"replayCache_temp.json"; } bool VisualReplay::StartVisualReplay(const OsPath& directory) { ENSURE(!g_NetServer); ENSURE(!g_NetClient); ENSURE(!g_Game); const OsPath replayFile = VisualReplay::GetDirectoryPath() / directory / L"commands.txt"; if (!FileExists(replayFile)) return false; g_Game = new CGame(false); return g_Game->StartVisualReplay(replayFile); } bool VisualReplay::ReadCacheFile(const ScriptInterface& scriptInterface, JS::MutableHandleObject cachedReplaysObject) { if (!FileExists(GetCacheFilePath())) return false; std::ifstream cacheStream(OsString(GetCacheFilePath()).c_str()); CStr cacheStr((std::istreambuf_iterator(cacheStream)), std::istreambuf_iterator()); cacheStream.close(); ScriptRequest rq(scriptInterface); JS::RootedValue cachedReplays(rq.cx); if (Script::ParseJSON(rq, cacheStr, &cachedReplays)) { cachedReplaysObject.set(&cachedReplays.toObject()); bool isArray; if (JS::IsArrayObject(rq.cx, cachedReplaysObject, &isArray) && isArray) return true; } LOGWARNING("The replay cache file is corrupted, it will be deleted"); wunlink(GetCacheFilePath()); return false; } void VisualReplay::StoreCacheFile(const ScriptInterface& scriptInterface, JS::HandleObject replays) { ScriptRequest rq(scriptInterface); JS::RootedValue replaysRooted(rq.cx, JS::ObjectValue(*replays)); std::ofstream cacheStream(OsString(GetTempCacheFilePath()).c_str(), std::ofstream::out | std::ofstream::trunc); cacheStream << Script::StringifyJSON(rq, &replaysRooted); cacheStream.close(); wunlink(GetCacheFilePath()); if (wrename(GetTempCacheFilePath(), GetCacheFilePath())) LOGERROR("Could not store the replay cache"); } JS::HandleObject VisualReplay::ReloadReplayCache(const ScriptInterface& scriptInterface, bool compareFiles) { TIMER(L"ReloadReplayCache"); ScriptRequest rq(scriptInterface); // Maps the filename onto the index, mtime and size using replayCacheMap = std::map>; replayCacheMap fileList; JS::RootedObject cachedReplaysObject(rq.cx); if (ReadCacheFile(scriptInterface, &cachedReplaysObject)) { // Create list of files included in the cache u32 cacheLength = 0; JS::GetArrayLength(rq.cx, cachedReplaysObject, &cacheLength); for (u32 j = 0; j < cacheLength; ++j) { JS::RootedValue replay(rq.cx); JS_GetElement(rq.cx, cachedReplaysObject, j, &replay); JS::RootedValue file(rq.cx); OsPath fileName; double fileSize; double fileMtime; Script::GetProperty(rq, replay, "directory", fileName); Script::GetProperty(rq, replay, "fileSize", fileSize); Script::GetProperty(rq, replay, "fileMTime", fileMtime); fileList[fileName] = std::make_tuple(j, fileMtime, fileSize); } } JS::RootedObject replays(rq.cx, JS::NewArrayObject(rq.cx, 0)); DirectoryNames directories; if (GetDirectoryEntries(GetDirectoryPath(), nullptr, &directories) != INFO::OK) return replays; bool newReplays = false; std::vector copyFromOldCache; // Specifies where the next replay should be kept u32 i = 0; for (const OsPath& directory : directories) { // This cannot use IsQuitRequested(), because the current loop and that function both run in the main thread. // So SDL events are not processed unless called explicitly here. if (SDL_QuitRequested()) // Don't return, because we want to save our progress break; const OsPath replayFile = GetDirectoryPath() / directory / L"commands.txt"; bool isNew = true; replayCacheMap::iterator it = fileList.find(directory); if (it != fileList.end()) { if (compareFiles) { if (!FileExists(replayFile)) continue; CFileInfo fileInfo; GetFileInfo(replayFile, &fileInfo); if ((u64)fileInfo.MTime() == std::get<1>(it->second) && (off_t)fileInfo.Size() == std::get<2>(it->second)) isNew = false; } else isNew = false; } if (isNew) { JS::RootedValue replayData(rq.cx, LoadReplayData(scriptInterface, directory)); if (replayData.isNull()) { if (!FileExists(replayFile)) continue; CFileInfo fileInfo; GetFileInfo(replayFile, &fileInfo); Script::CreateObject( rq, &replayData, "directory", directory.string(), "fileMTime", static_cast(fileInfo.MTime()), "fileSize", static_cast(fileInfo.Size())); } JS_SetElement(rq.cx, replays, i++, replayData); newReplays = true; } else copyFromOldCache.push_back(std::get<0>(it->second)); } debug_printf( "Loading %lu cached replays, removed %lu outdated entries, loaded %i new entries\n", (unsigned long)fileList.size(), (unsigned long)(fileList.size() - copyFromOldCache.size()), i); if (!newReplays && fileList.empty()) return replays; // No replay was changed, so just return the cache if (!newReplays && fileList.size() == copyFromOldCache.size()) return cachedReplaysObject; { // Copy the replays from the old cache that are not deleted if (!copyFromOldCache.empty()) for (u32 j : copyFromOldCache) { JS::RootedValue replay(rq.cx); JS_GetElement(rq.cx, cachedReplaysObject, j, &replay); JS_SetElement(rq.cx, replays, i++, replay); } } StoreCacheFile(scriptInterface, replays); return replays; } JS::Value VisualReplay::GetReplays(const ScriptInterface& scriptInterface, bool compareFiles) { TIMER(L"GetReplays"); ScriptRequest rq(scriptInterface); JS::RootedObject replays(rq.cx, ReloadReplayCache(scriptInterface, compareFiles)); // Only take entries with data JS::RootedValue replaysWithoutNullEntries(rq.cx); Script::CreateArray(rq, &replaysWithoutNullEntries); u32 replaysLength = 0; JS::GetArrayLength(rq.cx, replays, &replaysLength); for (u32 j = 0, i = 0; j < replaysLength; ++j) { JS::RootedValue replay(rq.cx); JS_GetElement(rq.cx, replays, j, &replay); if (Script::HasProperty(rq, replay, "attribs")) Script::SetPropertyInt(rq, replaysWithoutNullEntries, i++, replay); } return replaysWithoutNullEntries; } /** * Move the cursor backwards until a newline was read or the beginning of the file was found. * Either way the cursor points to the beginning of a newline. * * @return The current cursor position or -1 on error. */ inline off_t goBackToLineBeginning(std::istream* replayStream, const OsPath& fileName, off_t fileSize) { int currentPos; char character; for (int characters = 0; characters < 10000; ++characters) { currentPos = (int) replayStream->tellg(); // Stop when reached the beginning of the file if (currentPos == 0) return currentPos; if (!replayStream->good()) { LOGERROR("Unknown error when returning to the last line (%i of %lu) of %s", currentPos, fileSize, fileName.string8().c_str()); return -1; } // Stop when reached newline replayStream->get(character); if (character == '\n') return currentPos; // Otherwise go back one character. // Notice: -1 will set the cursor back to the most recently read character. replayStream->seekg(-2, std::ios_base::cur); } LOGERROR("Infinite loop when going back to a line beginning in %s", fileName.string8().c_str()); return -1; } /** * Compute game duration in seconds. Assume constant turn length. * Find the last line that starts with "turn" by reading the file backwards. * * @return seconds or -1 on error */ inline int getReplayDuration(std::istream* replayStream, const OsPath& fileName, off_t fileSize) { CStr type; // Move one character before the file-end replayStream->seekg(-2, std::ios_base::end); // Infinite loop protection, should never occur. // There should be about 5 lines to read until a turn is found. for (int linesRead = 1; linesRead < 1000; ++linesRead) { off_t currentPosition = goBackToLineBeginning(replayStream, fileName, fileSize); // Read error or reached file beginning. No turns exist. if (currentPosition < 1) return -1; if (!replayStream->good()) { LOGERROR("Read error when determining replay duration at %i of %llu in %s", currentPosition - 2, fileSize, fileName.string8().c_str()); return -1; } // Found last turn, compute duration. if (currentPosition + 4 < fileSize && (*replayStream >> type).good() && type == "turn") { u32 turn = 0, turnLength = 0; *replayStream >> turn >> turnLength; return (turn+1) * turnLength / 1000; // add +1 as turn numbers starts with 0 } // Otherwise move cursor back to the character before the last newline replayStream->seekg(currentPosition - 2, std::ios_base::beg); } LOGERROR("Infinite loop when determining replay duration for %s", fileName.string8().c_str()); return -1; } JS::Value VisualReplay::LoadReplayData(const ScriptInterface& scriptInterface, const OsPath& directory) { // The directory argument must not be constant, otherwise concatenating will fail const OsPath replayFile = GetDirectoryPath() / directory / L"commands.txt"; if (!FileExists(replayFile)) return JS::NullValue(); // Get file size and modification date CFileInfo fileInfo; GetFileInfo(replayFile, &fileInfo); const off_t fileSize = fileInfo.Size(); if (fileSize == 0) return JS::NullValue(); std::ifstream* replayStream = new std::ifstream(OsString(replayFile).c_str()); CStr type; if (!(*replayStream >> type).good()) { LOGERROR("Couldn't open %s.", replayFile.string8().c_str()); SAFE_DELETE(replayStream); return JS::NullValue(); } if (type != "start") { LOGWARNING("The replay %s doesn't begin with 'start'!", replayFile.string8().c_str()); SAFE_DELETE(replayStream); return JS::NullValue(); } // Parse header / first line CStr header; std::getline(*replayStream, header); ScriptRequest rq(scriptInterface); JS::RootedValue attribs(rq.cx); if (!Script::ParseJSON(rq, header, &attribs)) { LOGERROR("Couldn't parse replay header of %s", replayFile.string8().c_str()); SAFE_DELETE(replayStream); return JS::NullValue(); } // Ensure "turn" after header if (!(*replayStream >> type).good() || type != "turn") { SAFE_DELETE(replayStream); return JS::NullValue(); // there are no turns at all } // Don't process files of rejoined clients u32 turn = 1; *replayStream >> turn; if (turn != 0) { SAFE_DELETE(replayStream); return JS::NullValue(); } int duration = getReplayDuration(replayStream, replayFile, fileSize); SAFE_DELETE(replayStream); // Ensure minimum duration if (duration < minimumReplayDuration) return JS::NullValue(); // Return the actual data JS::RootedValue replayData(rq.cx); Script::CreateObject( rq, &replayData, "directory", directory.string(), "fileSize", static_cast(fileSize), "fileMTime", static_cast(fileInfo.MTime()), "duration", duration); Script::SetProperty(rq, replayData, "attribs", attribs); return replayData; } bool VisualReplay::DeleteReplay(const OsPath& replayDirectory) { if (replayDirectory.empty()) return false; const OsPath directory = GetDirectoryPath() / replayDirectory; return DirectoryExists(directory) && DeleteDirectory(directory) == INFO::OK; } JS::Value VisualReplay::GetReplayAttributes(const ScriptInterface& scriptInterface, const OsPath& directoryName) { // Create empty JS object ScriptRequest rq(scriptInterface); JS::RootedValue attribs(rq.cx); Script::CreateObject(rq, &attribs); // Return empty object if file doesn't exist const OsPath replayFile = GetDirectoryPath() / directoryName / L"commands.txt"; if (!FileExists(replayFile)) return attribs; // Open file std::istream* replayStream = new std::ifstream(OsString(replayFile).c_str()); CStr type, line; ENSURE((*replayStream >> type).good() && type == "start"); // Read and return first line std::getline(*replayStream, line); Script::ParseJSON(rq, line, &attribs); SAFE_DELETE(replayStream);; return attribs; } void VisualReplay::AddReplayToCache(const ScriptInterface& scriptInterface, const CStrW& directoryName) { TIMER(L"AddReplayToCache"); ScriptRequest rq(scriptInterface); JS::RootedValue replayData(rq.cx, LoadReplayData(scriptInterface, OsPath(directoryName))); if (replayData.isNull()) return; JS::RootedObject cachedReplaysObject(rq.cx); if (!ReadCacheFile(scriptInterface, &cachedReplaysObject)) cachedReplaysObject = JS::NewArrayObject(rq.cx, 0); u32 cacheLength = 0; JS::GetArrayLength(rq.cx, cachedReplaysObject, &cacheLength); JS_SetElement(rq.cx, cachedReplaysObject, cacheLength, replayData); StoreCacheFile(scriptInterface, cachedReplaysObject); } bool VisualReplay::HasReplayMetadata(const OsPath& directoryName) { const OsPath filePath(GetDirectoryPath() / directoryName / L"metadata.json"); if (!FileExists(filePath)) return false; CFileInfo fileInfo; GetFileInfo(filePath, &fileInfo); return fileInfo.Size() > 0; } JS::Value VisualReplay::GetReplayMetadata(const ScriptInterface& scriptInterface, const OsPath& directoryName) { if (!HasReplayMetadata(directoryName)) return JS::NullValue(); ScriptRequest rq(scriptInterface); JS::RootedValue metadata(rq.cx); std::ifstream* stream = new std::ifstream(OsString(GetDirectoryPath() / directoryName / L"metadata.json").c_str()); ENSURE(stream->good()); CStr line; std::getline(*stream, line); stream->close(); SAFE_DELETE(stream); Script::ParseJSON(rq, line, &metadata); return metadata; } Index: ps/trunk/source/ps/scripting/JSInterface_Mod.cpp =================================================================== --- ps/trunk/source/ps/scripting/JSInterface_Mod.cpp (revision 25473) +++ ps/trunk/source/ps/scripting/JSInterface_Mod.cpp (revision 25474) @@ -1,58 +1,57 @@ /* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "JSInterface_Mod.h" #include "ps/Mod.h" #include "scriptinterface/FunctionWrapper.h" extern void RestartEngine(); namespace JSI_Mod { Mod* ModGetter(const ScriptRequest&, JS::CallArgs&) { return &g_Mods; } bool SetModsAndRestartEngine(const ScriptInterface& scriptInterface, const std::vector& mods) { - g_Mods.ClearIncompatibleMods(); - if (!g_Mods.CheckAndEnableMods(scriptInterface, mods)) + if (!g_Mods.EnableMods(scriptInterface, mods, false)) return false; RestartEngine(); return true; } -bool HasFailedMods() +bool HasIncompatibleMods() { - return g_Mods.GetFailedMods().size() > 0; + return g_Mods.GetIncompatibleMods().size() > 0; } void RegisterScriptFunctions(const ScriptRequest& rq) { ScriptFunction::Register<&Mod::GetEngineInfo, ModGetter>(rq, "GetEngineInfo"); ScriptFunction::Register<&Mod::GetAvailableMods, ModGetter>(rq, "GetAvailableMods"); ScriptFunction::Register<&Mod::GetEnabledMods, ModGetter>(rq, "GetEnabledMods"); - ScriptFunction::Register (rq, "HasFailedMods"); - ScriptFunction::Register<&Mod::GetFailedMods, ModGetter>(rq, "GetFailedMods"); + ScriptFunction::Register (rq, "HasIncompatibleMods"); + ScriptFunction::Register<&Mod::GetIncompatibleMods, ModGetter>(rq, "GetIncompatibleMods"); ScriptFunction::Register<&SetModsAndRestartEngine>(rq, "SetModsAndRestartEngine"); } } Index: ps/trunk/source/ps/tests/test_Mod.h =================================================================== --- ps/trunk/source/ps/tests/test_Mod.h (revision 25473) +++ ps/trunk/source/ps/tests/test_Mod.h (revision 25474) @@ -1,185 +1,180 @@ /* Copyright (C) 2021 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "lib/self_test.h" #include "ps/CLogger.h" #include "ps/Mod.h" #include "scriptinterface/JSON.h" #include "scriptinterface/ScriptInterface.h" class TestMod : public CxxTest::TestSuite { + Mod m_Mods; public: void test_version_check() { CStr eq = "="; CStr lt = "<"; CStr gt = ">"; CStr leq = "<="; CStr geq = ">="; CStr required = "0.0.24";// 0ad <= required CStr version = "0.0.24";// 0ad version // 0.0.24 = 0.0.24 - TS_ASSERT(g_Mods.CompareVersionStrings(version, eq, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, lt, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, gt, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, leq, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, geq, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, eq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, lt, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, gt, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, leq, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, geq, required)); // 0.0.23 <= 0.0.24 version = "0.0.23"; - TS_ASSERT(!g_Mods.CompareVersionStrings(version, eq, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, lt, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, gt, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, leq, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, geq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, eq, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, lt, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, gt, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, leq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, geq, required)); // 0.0.25 >= 0.0.24 version = "0.0.25"; - TS_ASSERT(!g_Mods.CompareVersionStrings(version, eq, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, lt, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, gt, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, leq, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, geq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, eq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, lt, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, gt, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, leq, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, geq, required)); // 0.0.9 <= 0.1.0 version = "0.0.9"; required = "0.1.0"; - TS_ASSERT(!g_Mods.CompareVersionStrings(version, eq, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, lt, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, gt, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, leq, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, geq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, eq, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, lt, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, gt, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, leq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, geq, required)); // 5.3 <= 5.3.0 version = "5.3"; required = "5.3.0"; - TS_ASSERT(!g_Mods.CompareVersionStrings(version, eq, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, lt, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, gt, required)); - TS_ASSERT(g_Mods.CompareVersionStrings(version, leq, required)); - TS_ASSERT(!g_Mods.CompareVersionStrings(version, geq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, eq, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, lt, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, gt, required)); + TS_ASSERT(m_Mods.CompareVersionStrings(version, leq, required)); + TS_ASSERT(!m_Mods.CompareVersionStrings(version, geq, required)); } void test_compatible() { ScriptInterface script("Test", "Test", g_ScriptContext); ScriptRequest rq(script); JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx)); CStr jsonString = "{\ \"name\": \"0ad\",\ \"version\" : \"0.0.25\",\ \"label\" : \"0 A.D. Empires Ascendant\",\ \"url\" : \"https://play0ad.com\",\ \"description\" : \"A free, open-source, historical RTS game.\",\ \"dependencies\" : []\ }\ "; JS::RootedValue json(rq.cx); TS_ASSERT(Script::ParseJSON(rq, jsonString, &json)); JS_SetProperty(rq.cx, obj, "public", json); JS::RootedValue jsonW(rq.cx); CStr jsonStringW = "{\ \"name\": \"wrong\",\ \"version\" : \"0.0.25\",\ \"label\" : \"wrong mod\",\ \"url\" : \"\",\ \"description\" : \"fail\",\ \"dependencies\" : [\"0ad=0.0.24\"]\ }\ "; TS_ASSERT(Script::ParseJSON(rq, jsonStringW, &jsonW)); JS_SetProperty(rq.cx, obj, "wrong", jsonW); JS::RootedValue jsonG(rq.cx); CStr jsonStringG = "{\ \"name\": \"good\",\ \"version\" : \"0.0.25\",\ \"label\" : \"good mod\",\ \"url\" : \"\",\ \"description\" : \"ok\",\ \"dependencies\" : [\"0ad=0.0.25\"]\ }\ "; TS_ASSERT(Script::ParseJSON(rq, jsonStringG, &jsonG)); JS_SetProperty(rq.cx, obj, "good", jsonG); JS::RootedValue jsonG2(rq.cx); CStr jsonStringG2 = "{\ \"name\": \"good\",\ \"version\" : \"0.0.25\",\ \"label\" : \"good mod\",\ \"url\" : \"\",\ \"description\" : \"ok\",\ \"dependencies\" : [\"0ad>=0.0.24\"]\ }\ "; TS_ASSERT(Script::ParseJSON(rq, jsonStringG2, &jsonG2)); JS_SetProperty(rq.cx, obj, "good2", jsonG2); JS::RootedValue availableMods(rq.cx, JS::ObjectValue(*obj)); std::vector mods; mods.clear(); mods.push_back("public"); - g_Mods.ClearIncompatibleMods(); - TS_ASSERT(g_Mods.AreModsCompatible(script, mods, availableMods)); + TS_ASSERT(m_Mods.CheckForIncompatibleMods(script, mods, availableMods).empty()); mods.clear(); mods.push_back("mod"); mods.push_back("public"); - g_Mods.ClearIncompatibleMods(); - TS_ASSERT(g_Mods.AreModsCompatible(script, mods, availableMods)); + TS_ASSERT(m_Mods.CheckForIncompatibleMods(script, mods, availableMods).empty()); mods.clear(); mods.push_back("public"); mods.push_back("good"); - g_Mods.ClearIncompatibleMods(); - TS_ASSERT(g_Mods.AreModsCompatible(script, mods, availableMods)); + TS_ASSERT(m_Mods.CheckForIncompatibleMods(script, mods, availableMods).empty()); mods.clear(); mods.push_back("public"); mods.push_back("good2"); - g_Mods.ClearIncompatibleMods(); - TS_ASSERT(g_Mods.AreModsCompatible(script, mods, availableMods)); + TS_ASSERT(m_Mods.CheckForIncompatibleMods(script, mods, availableMods).empty()); mods.clear(); mods.push_back("public"); mods.push_back("wrong"); - g_Mods.ClearIncompatibleMods(); - TS_ASSERT(!g_Mods.AreModsCompatible(script, mods, availableMods)); + TS_ASSERT(!m_Mods.CheckForIncompatibleMods(script, mods, availableMods).empty()); mods.clear(); mods.push_back("public"); mods.push_back("does_not_exist"); - g_Mods.ClearIncompatibleMods(); - TS_ASSERT(!g_Mods.AreModsCompatible(script, mods, availableMods)); + TS_ASSERT(!m_Mods.CheckForIncompatibleMods(script, mods, availableMods).empty()); } };