1
1
forked from 0ad/0ad

mod.io support.

mod.io is a new platform for sharing mods, that 0 A.D. can make use of
in order to download mods and install them.

Based on patch by leper, numerous changes from s0600204, vladislavbelov,
Imarok, elexis, temple and myself.
Differential Revision: https://code.wildfiregames.com/D1029
This was SVN commit r21759.
This commit is contained in:
Nicolas Auvray 2018-04-22 18:14:45 +00:00
parent fe177fb5bf
commit 833c9f108c
24 changed files with 2036 additions and 18 deletions

View File

@ -433,6 +433,14 @@ delay = 200 ; Duration in milliseconds that is waited b
[mod]
enabledmods = "mod public"
[modio]
public_key = "RWQBhIRg+dOifTWlwgYHe8RfD8bqoDh1cCvygboAl3GOUKiCo0NlF4fw" ; Public key corresponding to the private key valid mods are signed with
[modio.v1]
baseurl = "https://api.mod.io/v1"
api_key = "23df258a71711ea6e4b50893acc1ba55"
name_id = "0ad"
[network]
duplicateplayernames = false ; Rename joining player to "User (2)" if "User" is already connected, otherwise prohibit join.
lateobservers = everyone ; Allow observers to join the game after it started. Possible values: everyone, buddies, disabled.

View File

@ -1,3 +1,38 @@
/**
* @param filesize - In bytes.
* @return Object with quantized filesize and suitable unit of size.
*/
function filesizeToObj(filesize)
{
// We are unlikely to download files measured in units greater than GiB.
let units = [
translateWithContext("filesize unit", "B"),
translateWithContext("filesize unit", "KiB"),
translateWithContext("filesize unit", "MiB"),
translateWithContext("filesize unit", "GiB")
];
let i = 0;
while (i < units.length - 1)
{
if (filesize < 1024)
break;
filesize /= 1024;
++i;
}
return {
"filesize": filesize.toFixed(i == 0 ? 0 : 1),
"unit": units[i]
};
}
function filesizeToString(filesize)
{
// Translation: For example: 123.4 KiB
return sprintf(translate("%(filesize)s %(unit)s"), filesizeToObj(filesize));
}
/**
* Convert time in milliseconds to [HH:]mm:ss string representation.
*

View File

@ -0,0 +1,344 @@
var g_ModsAvailableOnline = [];
/**
* Indicates if we have encountered an error in one of the network-interaction attempts.
*
* We use a global so we don't get multiple messageBoxes appearing (one for each "tick").
*
* Set to `true` by showErrorMessageBox
* Set to `false` by init, updateModList, downloadFile, and cancelRequest
*/
var g_Failure;
/**
* Indicates if the user has cancelled a request.
*
* Primarily used so the user can cancel the mod list fetch, as whenever that get cancelled,
* the modio state reverts to "ready", even if we've successfully listed mods before.
*
* Set to `true` by cancelRequest
* Set to `false` by updateModList, and downloadFile
*/
var g_RequestCancelled;
var g_RequestStartTime;
/**
* Returns true if ModIoAdvanceRequest should be called.
*/
var g_ModIOState = {
/**
* Finished status indicators
*/
"ready": progressData => {
// GameID acquired, ready to fetch mod list
if (!g_RequestCancelled)
updateModList();
return true;
},
"listed": progressData => {
// List of available mods acquired
// Only run this once (for each update).
if (Engine.GetGUIObjectByName("modsAvailableList").list.length)
return true;
hideDialog();
Engine.GetGUIObjectByName("refreshButton").enabled = true;
g_ModsAvailableOnline = Engine.ModIoGetMods();
displayMods();
return true;
},
"success": progressData => {
// Successfully acquired a mod file
hideDialog();
Engine.GetGUIObjectByName("downloadButton").enabled = true;
return true;
},
/**
* In-progress status indicators.
*/
"gameid": progressData => {
// Acquiring GameID from mod.io
return true;
},
"listing": progressData => {
// Acquiring list of available mods from mod.io
return true;
},
"downloading": progressData => {
// Downloading a mod file
updateProgressBar(progressData.progress, g_ModsAvailableOnline[selectedModIndex()].filesize);
return true;
},
/**
* Error/Failure status indicators.
*/
"failed_gameid": progressData => {
// Game ID couldn't be retrieved
showErrorMessageBox(
sprintf(translateWithContext("mod.io error message", "Game ID could not be retrieved.\n\n%(technicalDetails)s"), {
"technicalDetails": progressData.error
}),
translateWithContext("mod.io error message", "Initialization Error"),
[translate("Abort"), translate("Retry")],
[closePage, init]);
return false;
},
"failed_listing": progressData => {
// Mod list couldn't be retrieved
showErrorMessageBox(
sprintf(translateWithContext("mod.io error message", "Mod List could not be retrieved.\n\n%(technicalDetails)s"), {
"technicalDetails": progressData.error
}),
translateWithContext("mod.io error message", "Fetch Error"),
[translate("Abort"), translate("Retry")],
[cancelModListUpdate, updateModList]);
return false;
},
"failed_downloading": progressData => {
// File couldn't be retrieved
showErrorMessageBox(
sprintf(translateWithContext("mod.io error message", "File download failed.\n\n%(technicalDetails)s"), {
"technicalDetails": progressData.error
}),
translateWithContext("mod.io error message", "Download Error"),
[translate("Abort"), translate("Retry")],
[cancelRequest, downloadMod]);
return false;
},
"failed_filecheck": progressData => {
// The file is corrupted
showErrorMessageBox(
sprintf(translateWithContext("mod.io error message", "File verification error.\n\n%(technicalDetails)s"), {
"technicalDetails": progressData.error
}),
translateWithContext("mod.io error message", "Verification Error"),
[translate("Abort")],
[cancelRequest]);
return false;
},
/**
* Default
*/
"none": progressData => {
// Nothing has happened yet.
return true;
}
};
function init(data)
{
progressDialog(
translate("Initializing mod.io interface."),
translate("Initializing"),
false,
translate("Cancel"),
closePage);
g_Failure = false;
Engine.ModIoStartGetGameId();
}
function onTick()
{
let progressData = Engine.ModIoGetDownloadProgress();
let handler = g_ModIOState[progressData.status];
if (!handler)
{
warn("Unrecognized progress status: " + progressData.status);
return;
}
if (handler(progressData))
Engine.ModIoAdvanceRequest();
}
function displayMods()
{
let modsAvailableList = Engine.GetGUIObjectByName("modsAvailableList");
let selectedMod = modsAvailableList.list[modsAvailableList.selected];
modsAvailableList.selected = -1;
let displayedMods = clone(g_ModsAvailableOnline);
for (let i = 0; i < displayedMods.length; ++i)
displayedMods[i].i = i;
let filterColumns = ["name", "name_id", "summary"];
let filterText = Engine.GetGUIObjectByName("modFilter").caption.toLowerCase();
displayedMods = displayedMods.filter(mod => filterColumns.some(column => mod[column].toLowerCase().indexOf(filterText) != -1));
displayedMods.sort((mod1, mod2) =>
modsAvailableList.selected_column_order *
(modsAvailableList.selected_column == "filesize" ?
mod1.filesize - mod2.filesize :
String(mod1[modsAvailableList.selected_column]).localeCompare(String(mod2[modsAvailableList.selected_column]))));
modsAvailableList.list_name = displayedMods.map(mod => mod.name);
modsAvailableList.list_name_id = displayedMods.map(mod => mod.name_id);
modsAvailableList.list_version = displayedMods.map(mod => mod.version);
modsAvailableList.list_filesize = displayedMods.map(mod => filesizeToString(mod.filesize));
modsAvailableList.list_dependencies = displayedMods.map(mod => (mod.dependencies || []).join(" "));
modsAvailableList.list = displayedMods.map(mod => mod.i);
modsAvailableList.selected = modsAvailableList.list.indexOf(selectedMod);
}
function clearModList()
{
let modsAvailableList = Engine.GetGUIObjectByName("modsAvailableList");
modsAvailableList.selected = -1;
for (let listIdx of Object.keys(modsAvailableList).filter(key => key.startsWith("list")))
modsAvailableList[listIdx] = [];
}
function selectedModIndex()
{
let modsAvailableList = Engine.GetGUIObjectByName("modsAvailableList");
if (modsAvailableList.selected == -1)
return undefined;
return +modsAvailableList.list[modsAvailableList.selected];
}
function showModDescription()
{
let selected = selectedModIndex();
Engine.GetGUIObjectByName("downloadButton").enabled = selected !== undefined;
Engine.GetGUIObjectByName("modDescription").caption = selected !== undefined ? g_ModsAvailableOnline[selected].summary : "";
}
function cancelModListUpdate()
{
cancelRequest();
if (!g_ModsAvailableOnline.length)
{
closePage();
return;
}
displayMods();
Engine.GetGUIObjectByName('refreshButton').enabled = true;
}
function updateModList()
{
clearModList();
Engine.GetGUIObjectByName("refreshButton").enabled = false;
progressDialog(
translate("Fetching and updating list of available mods."),
translate("Updating"),
false,
translate("Cancel Update"),
cancelModListUpdate);
g_Failure = false;
g_RequestCancelled = false;
Engine.ModIoStartListMods();
}
function downloadMod()
{
let selected = selectedModIndex();
progressDialog(
sprintf(translate("Downloading “%(modname)s”"), {
"modname": g_ModsAvailableOnline[selected].name
}),
translate("Downloading"),
true,
translate("Cancel Download"),
() => { Engine.GetGUIObjectByName("downloadButton").enabled = true; });
Engine.GetGUIObjectByName("downloadButton").enabled = false;
g_Failure = false;
g_RequestCancelled = false;
Engine.ModIoStartDownloadMod(selected);
}
function cancelRequest()
{
g_Failure = false;
g_RequestCancelled = true;
Engine.ModIoCancelRequest();
hideDialog();
}
function closePage(data)
{
Engine.PopGuiPageCB(undefined);
}
function showErrorMessageBox(caption, title, buttonCaptions, buttonActions)
{
if (g_Failure)
return;
messageBox(500, 250, caption, title, buttonCaptions, buttonActions);
g_Failure = true;
}
function progressDialog(dialogCaption, dialogTitle, showProgressBar, buttonCaption, buttonAction)
{
Engine.GetGUIObjectByName("downloadDialog_title").caption = dialogTitle;
let downloadDialog_caption = Engine.GetGUIObjectByName("downloadDialog_caption");
downloadDialog_caption.caption = dialogCaption;
let size = downloadDialog_caption.size;
size.rbottom = showProgressBar ? 40 : 80;
downloadDialog_caption.size = size;
Engine.GetGUIObjectByName("downloadDialog_progress").hidden = !showProgressBar;
Engine.GetGUIObjectByName("downloadDialog_status").hidden = !showProgressBar;
let downloadDialog_button = Engine.GetGUIObjectByName("downloadDialog_button");
downloadDialog_button.caption = buttonCaption;
downloadDialog_button.onPress = () => { cancelRequest(); buttonAction(); };
Engine.GetGUIObjectByName("downloadDialog").hidden = false;
g_RequestStartTime = Date.now();
}
/*
* The "remaining time" and "average speed" texts both naively assume that
* the connection remains relatively stable throughout the download.
*/
function updateProgressBar(progress, totalSize)
{
let progressPercent = Math.ceil(progress * 100);
Engine.GetGUIObjectByName("downloadDialog_progressBar").caption = progressPercent;
let transferredSize = progress * totalSize;
let transferredSizeObj = filesizeToObj(transferredSize);
// Translation: Mod file download indicator. Current size over expected final size, with percentage complete.
Engine.GetGUIObjectByName("downloadDialog_progressText").caption = sprintf(translate("%(current)s / %(total)s (%(percent)s%%)"), {
"current": filesizeToObj(totalSize).unit == transferredSizeObj.unit ? transferredSizeObj.filesize : filesizeToString(transferredSize),
"total": filesizeToString(totalSize),
"percent": progressPercent
});
let elapsedTime = Date.now() - g_RequestStartTime;
let remainingTime = progressPercent ? (100 - progressPercent) * elapsedTime / progressPercent : 0;
let avgSpeed = filesizeToObj(transferredSize / (elapsedTime / 1000));
// Translation: Mod file download status message.
Engine.GetGUIObjectByName("downloadDialog_status").caption = sprintf(translate("Time Elapsed: %(elapsed)s\nEstimated Time Remaining: %(remaining)s\nAverage Speed: %(avgSpeed)s"), {
"elapsed": timeToString(elapsedTime),
"remaining": remainingTime ? timeToString(remainingTime) : translate("∞"),
// Translation: Average download speed, used to give the user a very rough and naive idea of the download time. For example: 123.4 KiB/s
"avgSpeed": sprintf(translate("%(number)s %(unit)s/s"), {
"number": avgSpeed.filesize,
"unit": avgSpeed.unit
})
});
}
function hideDialog()
{
Engine.GetGUIObjectByName("downloadDialog").hidden = true;
}

View File

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="utf-8"?>
<objects>
<script directory="gui/common/"/>
<script directory="gui/modio/"/>
<object type="image" sprite="ModernFade" z="0"/>
<object name="modio" type="image" style="ModernDialog" size="10% 10% 90% 90%">
<action on="Tick">
onTick();
</action>
<!-- Page Title -->
<object style="ModernLabelText" type="text" size="50%-128 -18 50%+128 14">
<translatableAttribute id="caption">mod.io Mods</translatableAttribute>
</object>
<!-- Available Mods Wrapper -->
<object name="modsAvailable" size="16 20 100%-16 100%-70" style="ModmodScrollbar">
<object style="ModernLabelText" type="text" size="0 5 100% 25">
<translatableAttribute id="caption">Available Mods</translatableAttribute>
</object>
<object name="modFilter"
type="input"
style="ModernInput"
size="16 0 200 24"
>
<action on="Press">displayMods();</action>
<action on="TextEdit">displayMods();</action>
</object>
<object name="modsAvailableList"
type="olist"
style="ModernSortedList"
size="0 30 100%-2 100%"
sortable="true"
selected_column="name"
selected_column_order="1"
font="sans-stroke-13"
>
<action on="SelectionChange">showModDescription();</action>
<action on="SelectionColumnChange">displayMods();</action>
<action on="MouseLeftDoubleClickItem">downloadMod();</action>
<!-- List headers -->
<!-- Keep in sync with mod property names -->
<column id="name_id" color="255 255 255" width="20%">
<translatableAttribute id="heading">Name</translatableAttribute>
</column>
<column id="version" color="255 255 255" width="15%">
<translatableAttribute id="heading">Version</translatableAttribute>
</column>
<column id="name" color="255 255 255" width="20%">
<translatableAttribute id="heading">Mod Label</translatableAttribute>
</column>
<column id="filesize" color="255 255 255" width="20%">
<translatableAttribute id="heading">File Size</translatableAttribute>
</column>
<column id="dependencies" color="255 255 255" width="25%">
<translatableAttribute id="heading">Dependencies</translatableAttribute>
</column>
</object>
<object name="modDescription" type="text" style="ModmodScrollbar" size="0 100% 100%-16 100%+28" />
</object>
<!-- Buttons -->
<object type="button" style="ModernButtonRed" size="100%-552 100%-44 100%-372 100%-16">
<translatableAttribute id="caption">Back</translatableAttribute>
<action on="Press">closePage();</action>
</object>
<object name="refreshButton" type="button" style="ModernButtonRed" size="100%-368 100%-44 100%-188 100%-16" enabled="false">
<translatableAttribute id="caption">Refresh List</translatableAttribute>
<action on="Press">updateModList();</action>
</object>
<object name="downloadButton" type="button" style="ModernButtonRed" size="100%-184 100%-44 100%-16 100%-16" enabled="false">
<translatableAttribute id="caption">Download</translatableAttribute>
<action on="Press">downloadMod();</action>
</object>
</object>
<!-- Download/Request-in-progress Dialog -->
<!-- Captions are supplied in modio.js -->
<!-- This must be after the buttons, and with "z" > 20, else it and/or other objects will not be drawn correctly -->
<object name="downloadDialog" type="image" sprite="ModernFade" z="30" size="0 0 100% 100%">
<object type="image" style="ModernDialog" size="50%-190 50%-96 50%+190 50%+96">
<object name="downloadDialog_title" type="text" style="ModernLabelText" size="50%-128 0%-16 50%+128 16"/>
<object name="downloadDialog_caption" type="text" style="ModernLabelText" size="0 0 100% 40%"/>
<object name="downloadDialog_progress" size="50%-160 35%-16 50%+160 35%+16" type="image" style="ModernProgressBarBackground">
<object name="downloadDialog_progressBar" type="progressbar" style="ModernProgressBar"/>
<object name="downloadDialog_progressText" type="text" style="ModernProgressBarText"/>
</object>
<object name="downloadDialog_status" type="text" style="ModernLabelText" size="0 35%+16 100% 35%+64"/>
<object name="downloadDialog_button" type="button" style="ModernButtonRed" size="50%-80 80%-14 50%+80 80%+14" hotkey="cancel"/>
</object>
</object>
</objects>

View File

@ -0,0 +1,4 @@
function init(data)
{
Engine.GetGUIObjectByName("mainText").caption = Engine.TranslateLines(Engine.ReadFile("gui/modmod/help/help.txt"));
}

View File

@ -0,0 +1,7 @@
0 A.D. is designed to be easily modded. Mods are distributed in the form of .pyromod files, which can be opened like .zip files.
In order to install a mod, just open the file with 0 A.D. (either double-click on the file and choose to open it with the game, or run "pyrogenesis file.pyromod" in a terminal). The mod will then be available in the mod selector. You can enable it and disable it at will. You can delete the mod manually using your file browser if needed (see https://trac.wildfiregames.com/wiki/GameDataPaths).
For more information about modding the game, see the Modding Guide online (click the Modding Guide button below).
The mod.io service is developed by DBolical, the company behind IndieDB and ModDB. Those websites have spread the word about 0 A.D. and other indie projects for a long time! Today, mod.io allows us to list and download all the mods that were verified by the team. Click "Download Mods" to try it out and install some!

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<objects>
<script directory="gui/common/"/>
<script directory="gui/modmod/help/"/>
<!-- Add a translucent black background to fade out the menu page -->
<object type="image" z="0" sprite="ModernFade"/>
<object type="image" style="ModernDialog" size="50%-466 50%-316 50%+466 50%+316">
<object name="title" style="ModernLabelText" type="text" size="50%-128 -18 50%+128 14">
<translatableAttribute id="caption">Pyrogenesis Mod Selector</translatableAttribute>
</object>
<object type="image" sprite="ModernFade" size="20 20 100%-20 100%-58">
<object name="mainText" type="text" style="ModernTextPanel"/>
</object>
<object type="button" style="ModernButtonRed" tooltip_style="snToolTip" size="100%-602 100%-52 100%-412 100%-24" hotkey="cancel">
<translatableAttribute id="caption">Close</translatableAttribute>
<action on="Press">Engine.PopGuiPage();</action>
</object>
<object type="button" style="ModernButtonRed" size="100%-408 100%-52 100%-218 100%-24">
<translatableAttribute id="caption">Modding Guide</translatableAttribute>
<action on="Press">Engine.OpenURL("https://trac.wildfiregames.com/wiki/Modding_Guide");</action>
</object>
<object type="button" style="ModernButtonRed" size="100%-214 100%-52 100%-24 100%-24">
<translatableAttribute id="caption">Visit mod.io</translatableAttribute>
<action on="Press">Engine.OpenURL("https://mod.io");</action>
</object>
</object>
</objects>

View File

@ -305,6 +305,23 @@ function isDependencyMet(dependency)
(!operator || versionSatisfied(g_Mods[folder].version, operator[0], version)));
}
function modIo()
{
messageBox(500, 250,
translate("You are about to connect to the mod.io online service. This provides easy access to community-made mods, but is not under the control of Wildfire Games.\n\nWhile we have taken care to make this secure, we cannot guarantee with absolute certainty that this is not a security risk.\n\nDo you really want to connect?"),
translate("Connect to mod.io?"),
[translate("Cancel"), translateWithContext("mod.io connection message box", "Connect")],
[
null,
() => {
Engine.PushGuiPage("page_modio.xml", {
"callback": "initMods"
});
}
]
);
}
/**
* Compares the given versions using the given operator.
* '-' or '_' is ignored. Only numbers are supported.

View File

@ -175,16 +175,26 @@
<object name="message" type="text" size="394 100%-78 100%-15 100%-52" text_align="left" textcolor="white"/>
<!-- BUTTONS -->
<object name="quitButton" type="button" style="ModernButtonRed" size="100%-576 100%-44 100%-392 100%-16">
<object name="quitButton" type="button" style="ModernButtonRed" size="100%-944 100%-44 100%-764 100%-16">
<translatableAttribute id="caption">Quit</translatableAttribute>
<action on="Press">Engine.Exit();</action>
</object>
<object name="cancelButton" type="button" style="ModernButtonRed" size="100%-576 100%-44 100%-392 100%-16" hotkey="cancel">
<object name="cancelButton" type="button" style="ModernButtonRed" size="100%-944 100%-44 100%-764 100%-16" hotkey="cancel">
<translatableAttribute id="caption">Cancel</translatableAttribute>
<action on="Press">closePage();</action>
</object>
<object type="button" style="ModernButtonRed" size="100%-760 100%-44 100%-580 100%-16">
<translatableAttribute id="caption">Help</translatableAttribute>
<action on="Press">Engine.PushGuiPage("page_modhelp.xml");</action>
</object>
<object type="button" style="ModernButtonRed" size="100%-576 100%-44 100%-392 100%-16">
<translatableAttribute id="caption">Download Mods</translatableAttribute>
<action on="Press">modIo();</action>
</object>
<object name="saveConfigurationButton" type="button" style="ModernButtonRed" size="100%-388 100%-44 100%-204 100%-16">
<translatableAttribute id="caption">Save Configuration</translatableAttribute>
<action on="Press">saveMods();</action>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<page>
<include>common/modern/setup.xml</include>
<include>common/modern/styles.xml</include>
<include>common/modern/sprites.xml</include>
<include>modmod/help/help.xml</include>
</page>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<page>
<include>common/modern/setup.xml</include>
<include>common/modern/styles.xml</include>
<include>common/modern/sprites.xml</include>
<include>modmod/styles.xml</include>
<include>modio/modio.xml</include>
</page>

View File

@ -39,6 +39,14 @@
"translate": {}
}
}
},
{
"extractor": "txt",
"filemasks": [
"gui/**.txt"
],
"options": {
}
}
]
}

View File

@ -394,7 +394,7 @@
tooltip_style="pgToolTip"
>
<translatableAttribute id="caption">Mod Selection</translatableAttribute>
<translatableAttribute id="tooltip">Select mods to use.</translatableAttribute>
<translatableAttribute id="tooltip">Select and download mods for the game.</translatableAttribute>
<action on="Press">
Engine.SwitchGuiPage("page_modmod.xml");
</action>

View File

@ -722,6 +722,7 @@ function setup_all_libs ()
"tinygettext",
"icu",
"iconv",
"libsodium",
}
if not _OPTIONS["without-audio"] then

View File

@ -608,7 +608,7 @@ static void RunGameOrAtlas(int argc, const char* argv[])
// Install the mods without deleting the pyromod files
for (const OsPath& modPath : modsToInstall)
installer.Install(modPath, g_ScriptRuntime, false);
installer.Install(modPath, g_ScriptRuntime, true);
installedMods = installer.GetInstalledMods();
}

View File

@ -61,6 +61,7 @@
#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"
@ -704,6 +705,8 @@ void Shutdown(int flags)
SAFE_DELETE(g_XmppClient);
SAFE_DELETE(g_ModIo);
ShutdownPs();
TIMER_BEGIN(L"shutdown TexMan");
@ -735,6 +738,9 @@ void Shutdown(int flags)
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:
@ -966,6 +972,10 @@ bool Init(const CmdLineArgs& args, int flags)
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

View File

@ -41,12 +41,15 @@ CModInstaller::~CModInstaller()
CModInstaller::ModInstallationResult CModInstaller::Install(
const OsPath& mod,
const std::shared_ptr<ScriptRuntime>& scriptRuntime,
bool deleteAfterInstall)
bool keepFile)
{
const OsPath modTemp = m_TempDir / mod.Basename() / mod.Filename().ChangeExtension(L".zip");
CreateDirectories(modTemp.Parent(), 0700);
if (keepFile)
CopyFile(mod, modTemp, true);
else
wrename(mod, modTemp);
// Load the mod to VFS
if (m_VFS->Mount(m_CacheDir, m_TempDir / "") != INFO::OK)
@ -96,10 +99,6 @@ CModInstaller::ModInstallationResult CModInstaller::Install(
}
#endif // OS_WIN
// Remove the original file if requested
if (deleteAfterInstall)
wunlink(mod);
m_InstalledMods.emplace_back(modName);
return SUCCESS;

View File

@ -53,11 +53,12 @@ public:
/**
* Process and unpack the mod.
* @param mod path of .pyromod/.zip file
* @param keepFile if true, copy the file, if false move it
*/
ModInstallationResult Install(
const OsPath& mod,
const std::shared_ptr<ScriptRuntime>& scriptRuntime,
bool deleteAfterInstall);
bool keepFile);
/**
* @return a list of all mods installed so far by this CModInstaller.

822
source/ps/ModIo.cpp Normal file
View File

@ -0,0 +1,822 @@
/* Copyright (C) 2018 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/Paths.h"
#include "ps/Mod.h"
#include "ps/ModInstaller.h"
#include "scriptinterface/ScriptConversions.h"
#include "scriptinterface/ScriptInterface.h"
#include <boost/algorithm/string.hpp>
#include <iomanip>
ModIo* g_ModIo = nullptr;
struct DownloadCallbackData
{
DownloadCallbackData()
: fp(nullptr), md5()
{
}
DownloadCallbackData(FILE* _fp)
: fp(_fp), md5()
{
crypto_generichash_init(&hash_state, NULL, 0U, crypto_generichash_BYTES_MAX);
}
FILE* fp;
MD5 md5;
crypto_generichash_state hash_state;
};
ModIo::ModIo()
: m_GamesRequest("/games"), m_CallbackData(nullptr)
{
// Get config values from the sytem namespace, or below (default).
// 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_SYSTEM, "modio.public_key", pk_str);
g_ConfigDB.GetValue(CFG_SYSTEM, "modio.v1.baseurl", m_BaseUrl);
{
std::string api_key;
g_ConfigDB.GetValue(CFG_SYSTEM, "modio.v1.api_key", api_key);
m_ApiKey = "api_key=" + api_key;
}
{
std::string nameid;
g_ConfigDB.GetValue(CFG_SYSTEM, "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<ModIo*>(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<DownloadCallbackData*>(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((const u8*)buffer, written);
crypto_generichash_update(&data->hash_state, (const u8*)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<DownloadProgressData*>(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<double>(dlnow) / static_cast<double>(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, (void*)m_CallbackData);
curl_easy_setopt(m_Curl, CURLOPT_XFERINFOFUNCTION, DownloadProgressCallback);
curl_easy_setopt(m_Curl, CURLOPT_XFERINFODATA, (void*)&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(size_t 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_args);
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 || message->msg == CURLMSG_DONE || message->easy_handle == m_Curl)
continue;
CURLcode err = message->data.result;
if (err == CURLE_OK)
continue;
std::string error = fmt::sprintf(
g_L10n.Translate("Download failure. Server response: %s; %s."), curl_easy_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;
} 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_args);
CModInstaller installer(paths.UserData() / "mods", paths.Cache());
installer.Install(m_DownloadFilePath, g_ScriptRuntime, 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::stringstream md5digest;
md5digest << std::hex << std::setfill('0');
for (size_t i = 0; i < MD5::DIGESTSIZE; ++i)
md5digest << std::setw(2) << (int)digest[i];
if (m_ModData[m_DownloadModID].properties.at("filehash_md5") != md5digest.str())
{
err = fmt::sprintf(
g_L10n.Translate("Invalid file. Expected md5 %s, got %s."),
m_ModData[m_DownloadModID].properties.at("filehash_md5").c_str(),
md5digest.str());
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] = {};
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;
JSContext* cx = scriptInterface.GetContext();
JSAutoRequest rq(cx);
JS::RootedValue gameResponse(cx);
if (!scriptInterface.ParseJSON(responseData, &gameResponse))
FAIL("Failed to parse response as JSON.");
if (!gameResponse.isObject())
FAIL("response not an object.");
JS::RootedObject gameResponseObj(cx, gameResponse.toObjectOrNull());
JS::RootedValue dataVal(cx);
if (!JS_GetProperty(cx, gameResponseObj, "data", &dataVal))
FAIL("data property not in response.");
// [{"id": 42, ...}, ...]
if (!dataVal.isObject())
FAIL("data property not an object.");
JS::RootedObject data(cx, dataVal.toObjectOrNull());
u32 length;
if (!JS_IsArrayObject(cx, data) || !JS_GetArrayLength(cx, data, &length) || !length)
FAIL("data property not an array with at least one element.");
// {"id": 42, ...}
JS::RootedValue first(cx);
if (!JS_GetElement(cx, data, 0, &first))
FAIL("Couldn't get first element.");
if (!first.isObject())
FAIL("First element not an object.");
JS::RootedObject firstObj(cx, &first.toObject());
bool hasIdProperty;
if (!JS_HasProperty(cx, firstObj, "id", &hasIdProperty) || !hasIdProperty)
FAIL("No id property in first element.");
JS::RootedValue idProperty(cx);
ENSURE(JS_GetProperty(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 (!ScriptInterface::FromJSVal(cx, 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", 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<ModIoModData>& modData, const PKStruct& pk, std::string& err)
{
// Make sure we don't end up passing partial results back
#define CLEANUP() modData.clear();
JSContext* cx = scriptInterface.GetContext();
JSAutoRequest rq(cx);
JS::RootedValue modResponse(cx);
if (!scriptInterface.ParseJSON(responseData, &modResponse))
FAIL("Failed to parse response as JSON.");
if (!modResponse.isObject())
FAIL("response not an object.");
JS::RootedObject modResponseObj(cx, modResponse.toObjectOrNull());
JS::RootedValue dataVal(cx);
if (!JS_GetProperty(cx, modResponseObj, "data", &dataVal))
FAIL("data property not in response.");
// [modobj1, modobj2, ... ]
if (!dataVal.isObject())
FAIL("data property not an object.");
JS::RootedObject data(cx, dataVal.toObjectOrNull());
u32 length;
if (!JS_IsArrayObject(cx, data) || !JS_GetArrayLength(cx, data, &length) || !length)
FAIL("data property not an array with at least one element.");
modData.clear();
modData.reserve(length);
for (u32 i = 0; i < length; ++i)
{
JS::RootedValue el(cx);
if (!JS_GetElement(cx, data, i, &el) || !el.isObject())
FAIL("Failed to get array element object.");
modData.emplace_back();
#define COPY_STRINGS(prefix, obj, ...) \
for (const std::string& prop : { __VA_ARGS__ }) \
{ \
std::string val; \
if (!ScriptInterface::FromJSProperty(cx, obj, prop.c_str(), val)) \
FAIL("Failed to get %s from %s.", prop, #obj);\
modData.back().properties.emplace(prefix+prop, val); \
}
// TODO: Currently the homepage field does not contain a non-null value for any entry.
COPY_STRINGS("", el, "name", "name_id", "summary");
// Now copy over the modfile part, but without the pointless substructure
JS::RootedObject elObj(cx, el.toObjectOrNull());
JS::RootedValue modFile(cx);
if (!JS_GetProperty(cx, elObj, "modfile", &modFile))
FAIL("Failed to get modfile data.");
if (!modFile.isObject())
FAIL("modfile not an object.");
COPY_STRINGS("", modFile, "version", "filesize");
JS::RootedObject modFileObj(cx, modFile.toObjectOrNull());
JS::RootedValue filehash(cx);
if (!JS_GetProperty(cx, modFileObj, "filehash", &filehash))
FAIL("Failed to get filehash data.");
COPY_STRINGS("filehash_", filehash, "md5");
JS::RootedValue download(cx);
if (!JS_GetProperty(cx, modFileObj, "download", &download))
FAIL("Failed to get download data.");
COPY_STRINGS("", download, "binary_url");
// Parse metadata_blob (sig+deps)
std::string metadata_blob;
if (!ScriptInterface::FromJSProperty(cx, modFile, "metadata_blob", metadata_blob))
FAIL("Failed to get metadata_blob from modFile.");
JS::RootedValue metadata(cx);
if (!scriptInterface.ParseJSON(metadata_blob, &metadata))
FAIL("Failed to parse metadata_blob as JSON.");
if (!metadata.isObject())
FAIL("metadata_blob not decoded as an object.");
if (!ScriptInterface::FromJSProperty(cx, metadata, "dependencies", modData.back().dependencies))
FAIL("Failed to get dependencies from metadata_blob.");
std::vector<std::string> minisigs;
if (!ScriptInterface::FromJSProperty(cx, metadata, "minisigs", minisigs))
FAIL("Failed to get minisigs from metadata_blob.");
// Remove this entry if we did not find a valid matching signature.
std::string signatureParsingErr;
if (!ParseSignature(minisigs, modData.back().sig, pk, signatureParsingErr))
modData.pop_back();
#undef COPY_STRINGS
}
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<std::string>& 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<std::string> 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

208
source/ps/ModIo.h Normal file
View File

@ -0,0 +1,208 @@
/* Copyright (C) 2018 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.
*/
#ifndef INCLUDED_MODIO
#define INCLUDED_MODIO
#include "lib/external_libraries/curl.h"
#include "scriptinterface/ScriptInterface.h"
#include <sodium.h>
#include <string>
// TODO: Allocate instance of the below two using sodium_malloc?
struct PKStruct
{
unsigned char sig_alg[2] = {}; // == "Ed"
unsigned char keynum[8] = {}; // should match the keynum in the sigstruct, else this is the wrong key
unsigned char pk[crypto_sign_PUBLICKEYBYTES] = {};
};
struct SigStruct
{
unsigned char sig_alg[2] = {}; // "ED" (since we only support the hashed mode)
unsigned char keynum[8] = {}; // should match the keynum in the PKStruct
unsigned char sig[crypto_sign_BYTES] = {};
};
struct ModIoModData
{
std::map<std::string, std::string> properties;
std::vector<std::string> dependencies;
SigStruct sig;
};
enum class DownloadProgressStatus {
NONE, // Default state
GAMEID, // The game ID is being downloaded
READY, // The game ID has been downloaded
LISTING, // The mod list is being downloaded
LISTED, // The mod list has been downloaded
DOWNLOADING, // A mod file is being downloaded
SUCCESS, // A mod file has been downloaded
FAILED_GAMEID, // Game ID couldn't be retrieved
FAILED_LISTING, // Mod list couldn't be retrieved
FAILED_DOWNLOADING, // File couldn't be retrieved
FAILED_FILECHECK // The file is corrupted
};
struct DownloadProgressData
{
DownloadProgressStatus status;
double progress;
std::string error;
};
struct DownloadCallbackData;
/**
* mod.io API interfacing code.
*
* Overview
*
* This class interfaces with a remote API provider that returns a list of mod files.
* These can then be downloaded after some cursory checking of well-formedness of the returned
* metadata.
* Downloaded files are checked for well formedness by validating that they fit the size and hash
* indicated by the API, then we check if the file is actually signed by a trusted key, and only
* if all of that is success the file is actually possible to be loaded as a mod.
*
* Security considerations
*
* This both distrusts the loaded JS mods, and the API as much as possible.
* We do not want a malicious mod to use this to download arbitrary files, nor do we want the API
* to make us download something we have not verified.
* Therefore we only allow mods to download one of the mods returned by this class (using indices).
*
* This (mostly) necessitates parsing the API responses here, as opposed to in JS.
* One could alternatively parse the responses in a locked down JS context, but that would require
* storing that code in here, or making sure nobody can overwrite it. Also this would possibly make
* some of the needed accesses for downloading and verifying files a bit more complicated.
*
* Everything downloaded from the API has its signature verified against our public key.
* This is a requirement, as otherwise a compromise of the API would result in users installing
* possibly malicious files.
* So a compromised API can just serve old files that we signed, so in that case there would need
* to be an issue in that old file that was missed.
*
* To limit the extend to how old those files could be the signing key should be rotated
* regularly (e.g. every release). To allow old versions of the engine to still use the API
* files can be signed by both the old and the new key for some amount of time, that however
* only makes sense in case a mod is compatible with both engine versions.
*
* Note that this does not prevent all possible attacks a package manager/update system should
* defend against. This is intentionally not an update system since proper package managers already
* exist. However there is some possible overlap in attack vectors and these should be evalutated
* whether they apply and to what extend we can fix that on our side (or how to get the API provider
* to help us do so). For a list of some possible issues see:
* https://github.com/theupdateframework/specification/blob/master/tuf-spec.md
*
* The mod.io settings are also locked down such that only mods that have been authorized by us
* show up in API queries. This is both done so that all required information (dependencies)
* are stored for the files, and that only mods that have been checked for being ok are actually
* shown to users.
*/
class ModIo
{
NONCOPYABLE(ModIo);
public:
ModIo();
~ModIo();
// Async requests
void StartGetGameId();
void StartListMods();
void StartDownloadMod(size_t idx);
/**
* Advance the current async request and perform final steps if the download is complete.
*
* @param scriptInterface used for parsing the data and possibly install the mod.
* @return true if the download is complete (successful or not), false otherwise.
*/
bool AdvanceRequest(const ScriptInterface& scriptInterface);
/**
* Cancel the current async request and clean things up
*/
void CancelRequest();
const std::vector<ModIoModData>& GetMods() const
{
return m_ModData;
}
const DownloadProgressData& GetDownloadProgress() const
{
return m_DownloadProgressData;
}
private:
static size_t ReceiveCallback(void* buffer, size_t size, size_t nmemb, void* userp);
static size_t DownloadCallback(void* buffer, size_t size, size_t nmemb, void* userp);
static int DownloadProgressCallback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
CURLMcode SetupRequest(const std::string& url, bool fileDownload);
void TearDownRequest();
bool ParseGameId(const ScriptInterface& scriptInterface, std::string& err);
bool ParseMods(const ScriptInterface& scriptInterface, std::string& err);
void DeleteDownloadedFile();
bool VerifyDownloadedFile(std::string& err);
// Utility methods for parsing mod.io responses and metadata
static bool ParseGameIdResponse(const ScriptInterface& scriptInterface, const std::string& responseData, int& id, std::string& err);
static bool ParseModsResponse(const ScriptInterface& scriptInterface, const std::string& responseData, std::vector<ModIoModData>& modData, const PKStruct& pk, std::string& err);
static bool ParseSignature(const std::vector<std::string>& minisigs, SigStruct& sig, const PKStruct& pk, std::string& err);
// Url parts
std::string m_BaseUrl;
std::string m_GamesRequest;
std::string m_GameId;
// Query parameters
std::string m_ApiKey;
std::string m_IdQuery;
CURL* m_Curl;
CURLM* m_CurlMulti;
curl_slist* m_Headers;
char m_ErrorBuffer[CURL_ERROR_SIZE];
std::string m_ResponseData;
// Current mod download
int m_DownloadModID;
OsPath m_DownloadFilePath;
DownloadCallbackData* m_CallbackData;
DownloadProgressData m_DownloadProgressData;
PKStruct m_pk;
std::vector<ModIoModData> m_ModData;
friend class TestModIo;
};
extern ModIo* g_ModIo;
#endif // INCLUDED_MODIO

View File

@ -578,10 +578,6 @@ void CUserReporter::Initialize()
std::string url;
CFG_GET_VAL("userreport.url", url);
// Initialise everything except Win32 sockets (because our networking
// system already inits those)
curl_global_init(CURL_GLOBAL_ALL & ~CURL_GLOBAL_WIN32);
m_Worker = new CUserReporterWorker(userID, url);
m_Worker->SetEnabled(IsReportingEnabled());
@ -595,9 +591,7 @@ void CUserReporter::Deinitialize()
if (m_Worker->Shutdown())
{
// Worker was shut down cleanly
SAFE_DELETE(m_Worker);
curl_global_cleanup();
}
else
{

View File

@ -17,9 +17,11 @@
#include "precompiled.h"
#include "ps/scripting/JSInterface_Mod.h"
#include "JSInterface_Mod.h"
#include "ps/CLogger.h"
#include "ps/Mod.h"
#include "ps/ModIo.h"
extern void restart_engine();
@ -53,10 +55,142 @@ void JSI_Mod::SetMods(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std:
g_modsLoaded = mods;
}
void JSI_Mod::ModIoStartGetGameId(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
if (!g_ModIo)
g_ModIo = new ModIo();
ENSURE(g_ModIo);
g_ModIo->StartGetGameId();
}
void JSI_Mod::ModIoStartListMods(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
if (!g_ModIo)
{
LOGERROR("ModIoStartListMods called before ModIoStartGetGameId");
return;
}
g_ModIo->StartListMods();
}
void JSI_Mod::ModIoStartDownloadMod(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), uint32_t idx)
{
if (!g_ModIo)
{
LOGERROR("ModIoStartDownloadMod called before ModIoStartGetGameId");
return;
}
g_ModIo->StartDownloadMod(idx);
}
bool JSI_Mod::ModIoAdvanceRequest(ScriptInterface::CxPrivate* pCxPrivate)
{
if (!g_ModIo)
{
LOGERROR("ModIoAdvanceRequest called before ModIoGetMods");
return false;
}
ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface;
return g_ModIo->AdvanceRequest(*scriptInterface);
}
void JSI_Mod::ModIoCancelRequest(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
if (!g_ModIo)
{
LOGERROR("ModIoCancelRequest called before ModIoGetMods");
return;
}
g_ModIo->CancelRequest();
}
JS::Value JSI_Mod::ModIoGetMods(ScriptInterface::CxPrivate* pCxPrivate)
{
if (!g_ModIo)
{
LOGERROR("ModIoGetMods called before ModIoStartGetGameId");
return JS::NullValue();
}
ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface;
JSContext* cx = scriptInterface->GetContext();
JSAutoRequest rq(cx);
const std::vector<ModIoModData>& availableMods = g_ModIo->GetMods();
JS::RootedObject mods(cx, JS_NewArrayObject(cx, availableMods.size()));
if (!mods)
return JS::NullValue();
u32 i = 0;
for (const ModIoModData& mod : availableMods)
{
JS::RootedValue m(cx, JS::ObjectValue(*JS_NewPlainObject(cx)));
for (const std::pair<std::string, std::string>& prop : mod.properties)
scriptInterface->SetProperty(m, prop.first.c_str(), prop.second, true);
scriptInterface->SetProperty(m, "dependencies", mod.dependencies, true);
JS_SetElement(cx, mods, i++, m);
}
return JS::ObjectValue(*mods);
}
const std::map<DownloadProgressStatus, std::string> statusStrings = {
{ DownloadProgressStatus::NONE, "none" },
{ DownloadProgressStatus::GAMEID, "gameid" },
{ DownloadProgressStatus::READY, "ready" },
{ DownloadProgressStatus::LISTING, "listing" },
{ DownloadProgressStatus::LISTED, "listed" },
{ DownloadProgressStatus::DOWNLOADING, "downloading" },
{ DownloadProgressStatus::SUCCESS, "success" },
{ DownloadProgressStatus::FAILED_GAMEID, "failed_gameid" },
{ DownloadProgressStatus::FAILED_LISTING, "failed_listing" },
{ DownloadProgressStatus::FAILED_DOWNLOADING, "failed_downloading" },
{ DownloadProgressStatus::FAILED_FILECHECK, "failed_filecheck" }
};
JS::Value JSI_Mod::ModIoGetDownloadProgress(ScriptInterface::CxPrivate* pCxPrivate)
{
if (!g_ModIo)
{
LOGERROR("ModIoGetDownloadProgress called before ModIoGetMods");
return JS::NullValue();
}
ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface;
JSContext* cx = scriptInterface->GetContext();
JSAutoRequest rq(cx);
JS::RootedValue progressData(cx, JS::ObjectValue(*JS_NewPlainObject(cx)));
const DownloadProgressData& progress = g_ModIo->GetDownloadProgress();
scriptInterface->SetProperty(progressData, "status", statusStrings.at(progress.status), true);
scriptInterface->SetProperty(progressData, "progress", progress.progress, true);
scriptInterface->SetProperty(progressData, "error", progress.error, true);
return progressData;
}
void JSI_Mod::RegisterScriptFunctions(const ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction<JS::Value, &GetEngineInfo>("GetEngineInfo");
scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::GetAvailableMods>("GetAvailableMods");
scriptInterface.RegisterFunction<void, &JSI_Mod::RestartEngine>("RestartEngine");
scriptInterface.RegisterFunction<void, std::vector<CStr>, &JSI_Mod::SetMods>("SetMods");
scriptInterface.RegisterFunction<void, &JSI_Mod::ModIoStartGetGameId>("ModIoStartGetGameId");
scriptInterface.RegisterFunction<void, &JSI_Mod::ModIoStartListMods>("ModIoStartListMods");
scriptInterface.RegisterFunction<void, uint32_t, &JSI_Mod::ModIoStartDownloadMod>("ModIoStartDownloadMod");
scriptInterface.RegisterFunction<bool, &JSI_Mod::ModIoAdvanceRequest>("ModIoAdvanceRequest");
scriptInterface.RegisterFunction<void, &JSI_Mod::ModIoCancelRequest>("ModIoCancelRequest");
scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::ModIoGetMods>("ModIoGetMods");
scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::ModIoGetDownloadProgress>("ModIoGetDownloadProgress");
}

View File

@ -30,6 +30,14 @@ namespace JSI_Mod
JS::Value GetAvailableMods(ScriptInterface::CxPrivate* pCxPrivate);
void RestartEngine(ScriptInterface::CxPrivate* pCxPrivate);
void SetMods(ScriptInterface::CxPrivate* pCxPrivate, const std::vector<CStr>& mods);
void ModIoStartGetGameId(ScriptInterface::CxPrivate* pCxPrivate);
void ModIoStartListMods(ScriptInterface::CxPrivate* pCxPrivate);
void ModIoStartDownloadMod(ScriptInterface::CxPrivate* pCxPrivate, uint32_t idx);
bool ModIoAdvanceRequest(ScriptInterface::CxPrivate* pCxPrivate);
void ModIoCancelRequest(ScriptInterface::CxPrivate* pCxPrivate);
JS::Value ModIoGetMods(ScriptInterface::CxPrivate* pCxPrivate);
JS::Value ModIoGetDownloadProgress(ScriptInterface::CxPrivate* pCxPrivate);
}
#endif

View File

@ -0,0 +1,246 @@
/* Copyright (C) 2018 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/ModIo.h"
#include "scriptinterface/ScriptInterface.h"
#include <sodium.h>
class TestModIo : public CxxTest::TestSuite
{
public:
void setUp()
{
if (sodium_init() < 0)
LOGERROR("failed to initialize libsodium");
}
// TODO: One could probably fuzz these parsing functions to
// make sure they handle malformed input nicely.
void test_id_parsing()
{
ScriptInterface script("Test", "Test", g_ScriptRuntime);
#define TS_ASSERT_PARSE(input, expected_error, expected_id) \
{ \
TestLogger logger; \
int id = -1; \
std::string err; \
TS_ASSERT(!ModIo::ParseGameIdResponse(script, input, id, err)); \
TS_ASSERT_STR_EQUALS(err, expected_error); \
TS_ASSERT_EQUALS(id, expected_id); \
}
// Various malformed inputs
TS_ASSERT_PARSE("", "Failed to parse response as JSON.", -1);
TS_ASSERT_PARSE("()", "Failed to parse response as JSON.", -1);
TS_ASSERT_PARSE("[]", "data property not an object.", -1);
TS_ASSERT_PARSE("null", "response not an object.", -1);
TS_ASSERT_PARSE("{}", "data property not an object.", -1);
TS_ASSERT_PARSE("{\"data\": null}", "data property not an object.", -1);
TS_ASSERT_PARSE("{\"data\": {}}", "data property not an array with at least one element.", -1);
TS_ASSERT_PARSE("{\"data\": []}", "data property not an array with at least one element.", -1);
TS_ASSERT_PARSE("{\"data\": [null]}", "First element not an object.", -1);
TS_ASSERT_PARSE("{\"data\": [false]}", "First element not an object.", -1);
TS_ASSERT_PARSE("{\"data\": [{}]}", "No id property in first element.", -1);
TS_ASSERT_PARSE("{\"data\": [[]]}", "No id property in first element.", -1);
// Various invalid IDs
TS_ASSERT_PARSE("{\"data\": [{\"id\": null}]}", "id property not a number.", -1);
TS_ASSERT_PARSE("{\"data\": [{\"id\": {}}]}", "id property not a number.", -1);
TS_ASSERT_PARSE("{\"data\": [{\"id\": true}]}", "id property not a number.", -1);
TS_ASSERT_PARSE("{\"data\": [{\"id\": -12}]}", "Invalid id.", -1);
TS_ASSERT_PARSE("{\"data\": [{\"id\": 0}]}", "Invalid id.", -1);
#undef TS_ASSERT_PARSE
// Correctly formed input
{
TestLogger logger;
int id = -1;
std::string err;
TS_ASSERT(ModIo::ParseGameIdResponse(script, "{\"data\": [{\"id\": 42}]}", id, err));
TS_ASSERT(err.empty());
TS_ASSERT_EQUALS(id, 42);
}
}
void test_mods_parsing()
{
ScriptInterface script("Test", "Test", g_ScriptRuntime);
PKStruct pk;
const std::string pk_str = "RWTA6VIoth2Q1PFLsRILr3G7NB+mwwO8BSGoXs63X6TQgNGM4cE8Pvd6";
size_t bin_len = 0;
if (sodium_base642bin((unsigned char*)&pk, sizeof pk, pk_str.c_str(), pk_str.size(), NULL, &bin_len, NULL, sodium_base64_VARIANT_ORIGINAL) != 0 || bin_len != sizeof pk)
LOGERROR("failed to decode base64 public key");
#define TS_ASSERT_PARSE(input, expected_error) \
{ \
TestLogger logger; \
std::vector<ModIoModData> mods; \
std::string err; \
TS_ASSERT(!ModIo::ParseModsResponse(script, input, mods, pk, err)); \
TS_ASSERT_STR_EQUALS(err, expected_error); \
TS_ASSERT_EQUALS(mods.size(), 0); \
}
TS_ASSERT_PARSE("", "Failed to parse response as JSON.");
TS_ASSERT_PARSE("()", "Failed to parse response as JSON.");
TS_ASSERT_PARSE("null", "response not an object.");
TS_ASSERT_PARSE("[]", "data property not an object.");
TS_ASSERT_PARSE("{}", "data property not an object.");
TS_ASSERT_PARSE("{\"data\": null}", "data property not an object.");
TS_ASSERT_PARSE("{\"data\": {}}", "data property not an array with at least one element.");
TS_ASSERT_PARSE("{\"data\": []}", "data property not an array with at least one element.");
TS_ASSERT_PARSE("{\"data\": [null]}", "Failed to get array element object.");
TS_ASSERT_PARSE("{\"data\": [false]}", "Failed to get array element object.");
TS_ASSERT_PARSE("{\"data\": [true]}", "Failed to get array element object.");
TS_ASSERT_PARSE("{\"data\": [{}]}", "Failed to get name from el.");
TS_ASSERT_PARSE("{\"data\": [[]]}", "Failed to get name from el.");
TS_ASSERT_PARSE("{\"data\": [{\"foo\":\"bar\"}]}", "Failed to get name from el.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":null}]}", "Failed to get name_id from el."); // also some script value conversion check warning
TS_ASSERT_PARSE("{\"data\": [{\"name\":42}]}", "Failed to get name_id from el."); // no conversion warning, but converting numbers to strings and vice-versa seems ok
TS_ASSERT_PARSE("{\"data\": [{\"name\":false}]}", "Failed to get name_id from el."); // also some script value conversion check warning
TS_ASSERT_PARSE("{\"data\": [{\"name\":{}}]}", "Failed to get name_id from el."); // also some script value conversion check warning
TS_ASSERT_PARSE("{\"data\": [{\"name\":[]}]}", "Failed to get name_id from el."); // also some script value conversion check warning
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"foobar\"}]}", "Failed to get name_id from el.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\"}]}", "modfile not an object.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":null}]}", "modfile not an object.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":[]}]}", "Failed to get version from modFile.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{}}]}", "Failed to get version from modFile.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":null}}]}", "Failed to get filesize from modFile."); // also some script value conversion check warning
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234}}]}", "Failed to get md5 from filehash.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":null}}]}", "Failed to get md5 from filehash.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{}}}]}", "Failed to get md5 from filehash.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":null}}}]}", "Failed to get binary_url from download."); // also some script value conversion check warning
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}}}]}", "Failed to get binary_url from download.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":null}}]}", "Failed to get binary_url from download."); // also some script value conversion check warning
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":null}}}]}", "Failed to get metadata_blob from modFile."); // also some script value conversion check warning
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"}}}]}", "Failed to get metadata_blob from modFile.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"},\"metadata_blob\":null}}]}", "metadata_blob not decoded as an object.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"},\"metadata_blob\":\"\"}}]}", "Failed to parse metadata_blob as JSON.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"},\"metadata_blob\":\"{}\"}}]}", "Failed to get dependencies from metadata_blob.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"},\"metadata_blob\":\"{\\\"dependencies\\\":null}\"}}]}", "Failed to get dependencies from metadata_blob.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"},\"metadata_blob\":\"{\\\"dependencies\\\":[]}\"}}]}", "Failed to get minisigs from metadata_blob.");
TS_ASSERT_PARSE("{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"},\"metadata_blob\":\"{\\\"dependencies\\\":[],\\\"minisigs\\\":null}\"}}]}", "Failed to get minisigs from metadata_blob.");
#undef TS_ASSERT_PARSE
// Correctly formed input, but no signature matching the public key
// Thus all such mods/modfiles are not added, thus we get 0 parsed mods.
{
TestLogger logger;
std::vector<ModIoModData> mods;
std::string err;
TS_ASSERT(ModIo::ParseModsResponse(script, "{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"},\"metadata_blob\":\"{\\\"dependencies\\\":[],\\\"minisigs\\\":[]}\"}}]}", mods, pk, err));
TS_ASSERT(err.empty());
TS_ASSERT_EQUALS(mods.size(), 0);
}
// Correctly formed input (with a signature matching the public key above, and a valid global signature)
{
TestLogger logger;
std::vector<ModIoModData> mods;
std::string err;
TS_ASSERT(ModIo::ParseModsResponse(script, "{\"data\": [{\"name\":\"\",\"name_id\":\"\",\"summary\":\"\",\"modfile\":{\"version\":\"\",\"filesize\":1234, \"filehash\":{\"md5\":\"abc\"}, \"download\":{\"binary_url\":\"\"},\"metadata_blob\":\"{\\\"dependencies\\\":[],\\\"minisigs\\\":[\\\"untrusted comment: signature from minisign secret key\\\\nRUTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\\\\ntrusted comment: timestamp:1517285433\\\\tfile:tm.zip\\\\nTHwNMhK4Ogj6XA4305p1K9/ouP/DrxPcDFrPaiu+Ke6/WGlHIzBZHvmHWUedvsK6dzL31Gk8YNzscKWnZqWNCw==\\\"]}\"}}]}", mods, pk, err));
TS_ASSERT(err.empty());
TS_ASSERT_EQUALS(mods.size(), 1);
}
}
void test_signature_parsing()
{
PKStruct pk;
const std::string pk_str = "RWTA6VIoth2Q1PFLsRILr3G7NB+mwwO8BSGoXs63X6TQgNGM4cE8Pvd6";
size_t bin_len = 0;
if (sodium_base642bin((unsigned char*)&pk, sizeof pk, pk_str.c_str(), pk_str.size(), NULL, &bin_len, NULL, sodium_base64_VARIANT_ORIGINAL) != 0 || bin_len != sizeof pk)
LOGERROR("failed to decode base64 public key");
// No invalid signature at all (silent failure)
#define TS_ASSERT_PARSE_SILENT_FAILURE(input) \
{ \
TestLogger logger; \
SigStruct sig; \
std::string err; \
TS_ASSERT(!ModIo::ParseSignature(input, sig, pk, err)); \
TS_ASSERT(err.empty()); \
}
#define TS_ASSERT_PARSE(input, expected_error) \
{ \
TestLogger logger; \
SigStruct sig; \
std::string err; \
TS_ASSERT(!ModIo::ParseSignature({input}, sig, pk, err)); \
TS_ASSERT_STR_EQUALS(err, expected_error); \
}
TS_ASSERT_PARSE_SILENT_FAILURE({});
TS_ASSERT_PARSE("", "Invalid (too short) sig.");
TS_ASSERT_PARSE("\nRUTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\n\nTHwNMhK4Ogj6XA4305p1K9/ouP/DrxPcDFrPaiu+Ke6/WGlHIzBZHvmHWUedvsK6dzL31Gk8YNzscKWnZqWNCw==", "Malformed untrusted comment.");
TS_ASSERT_PARSE("unturusted comment: \nRUTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\n\nTHwNMhK4Ogj6XA4305p1K9/ouP/DrxPcDFrPaiu+Ke6/WGlHIzBZHvmHWUedvsK6dzL31Gk8YNzscKWnZqWNCw==", "Malformed untrusted comment.");
TS_ASSERT_PARSE("untrusted comment: \nRUTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\n\nTHwNMhK4Ogj6XA4305p1K9/ouP/DrxPcDFrPaiu+Ke6/WGlHIzBZHvmHWUedvsK6dzL31Gk8YNzscKWnZqWNCw==", "Malformed trusted comment.");
TS_ASSERT_PARSE("untrusted comment: \nRUTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\ntrusted comment:\nTHwNMhK4Ogj6XA4305p1K9/ouP/DrxPcDFrPaiu+Ke6/WGlHIzBZHvmHWUedvsK6dzL31Gk8YNzscKWnZqWNCw==", "Malformed trusted comment.");
TS_ASSERT_PARSE("untrusted comment: \n\ntrusted comment: \n", "Failed to decode base64 sig.");
TS_ASSERT_PARSE("untrusted comment: \nZm9vYmFyCg==\ntrusted comment: \n", "Failed to decode base64 sig.");
TS_ASSERT_PARSE("untrusted comment: \nRWTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\ntrusted comment: \n", "Only hashed minisign signatures are supported.");
// Silent failure again this one has the wrong keynum
TS_ASSERT_PARSE_SILENT_FAILURE({"untrusted comment: \nRUTA5VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\ntrusted comment: \n"});
TS_ASSERT_PARSE("untrusted comment: \nRUTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\ntrusted comment: \n", "Failed to decode base64 global_sig.");
TS_ASSERT_PARSE("untrusted comment: \nRUTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\ntrusted comment: timestamp:1517285433\tfile:tm.zip\nAHwNMhK4Ogj6XA4305p1K9/ouP/DrxPcDFrPaiu+Ke6/WGlHIzBZHvmHWUedvsK6dzL31Gk8YNzscKWnZqWNCw==", "Failed to verify global signature.");
// Valid signature
{
TestLogger logger;
SigStruct sig;
std::string err;
TS_ASSERT(ModIo::ParseSignature({"untrusted comment: \nRUTA6VIoth2Q1HUg5bwwbCUZPcqbQ/reLXqxiaWARH5PNcwxX5vBv/mLPLgdxGsIrOyK90763+rCVTmjeYx5BDz8C0CIbGZTNQs=\ntrusted comment: timestamp:1517285433\tfile:tm.zip\nTHwNMhK4Ogj6XA4305p1K9/ouP/DrxPcDFrPaiu+Ke6/WGlHIzBZHvmHWUedvsK6dzL31Gk8YNzscKWnZqWNCw=="}, sig, pk, err));
TS_ASSERT(err.empty());
}
#undef TS_ASSERT_PARSE_SILENT_FAILURE
#undef TS_ASSERT_PARSE
}
};