mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 05:13:58 -07:00
Up to now `eslint-plugin-brace-rules` was used to enforce a common brace style for JavaScript code. This plugin was however updated the last time over 9 years ago and will be incompatible with ESLint v10, as that [removes `context.getSourceCode()`][1], the plugin relies on. To keep the eslint config working with ESLint v10, this replaces `eslint-plugin-brace-rules` with the [`@stylistic/brace-style`][2] rule from `@stylistic/eslint-plugin`, a package we already use. While `@stylistic/brace-style` doesn't offer an option to format braces in exactly the same way as before, the "allman" style seems to be the one closest to the existing code. [1]: https://eslint.org/blog/2025/11/eslint-v10.0.0-alpha.0-released/#removed-deprecated-rule-context-members [2]: https://eslint.style/rules/brace-style
142 lines
3.6 KiB
JavaScript
142 lines
3.6 KiB
JavaScript
/**
|
|
* Mimics CampaignRun's necessary interface, but for a broken run
|
|
* (i.e. a run that can't be loaded), thus allowing to delete it.
|
|
*/
|
|
class BrokenRun
|
|
{
|
|
constructor(file)
|
|
{
|
|
this.filename = file;
|
|
}
|
|
|
|
getLabel(forList)
|
|
{
|
|
if (!forList)
|
|
return this.filename + ".0adcampaign";
|
|
return coloredText(sprintf("%(filename)s (%(error)s)", {
|
|
"filename": this.filename + ".0adcampaign",
|
|
"error": translate("file cannot be loaded")
|
|
}), "red");
|
|
}
|
|
|
|
destroy()
|
|
{
|
|
Engine.DeleteCampaignSave("saves/campaigns/" + this.filename + ".0adcampaign");
|
|
if (CampaignRun.getCurrentRunFilename() === this.filename)
|
|
CampaignRun.clearCurrentRun();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Lets you load/delete/look at existing campaign runs in your user folder.
|
|
*/
|
|
class LoadModal extends AutoWatcher
|
|
{
|
|
constructor(campaignTemplate)
|
|
{
|
|
super("render");
|
|
|
|
// _watch so render() is called anytime currentRuns are modified.
|
|
this.currentRuns = _watch(this.getRuns(), () => this.render());
|
|
|
|
Engine.GetGUIObjectByName('cancelButton').onPress = () => Engine.SwitchGuiPage("page_pregame.xml", {});
|
|
Engine.GetGUIObjectByName('deleteGameButton').onPress = () => this.deleteSelectedRun();
|
|
Engine.GetGUIObjectByName('startButton').onPress = () => this.startSelectedRun();
|
|
|
|
this.noCampaignsText = Engine.GetGUIObjectByName("noCampaignsText");
|
|
|
|
this.selectedRun = -1;
|
|
this.runSelection = Engine.GetGUIObjectByName("runSelection");
|
|
this.runSelection.onSelectionChange = () =>
|
|
{
|
|
this.selectedRun = this.runSelection.selected;
|
|
if (this.selectedRun === -1)
|
|
Engine.GetGUIObjectByName('runDescription').caption = "";
|
|
else
|
|
Engine.GetGUIObjectByName('runDescription').caption = this.currentRuns[this.selectedRun].getLabel();
|
|
};
|
|
|
|
this.runSelection.onMouseLeftDoubleClickItem = () => this.startSelectedRun();
|
|
|
|
this._ready = true;
|
|
}
|
|
|
|
getRuns()
|
|
{
|
|
const out = [];
|
|
const files = Engine.ListDirectoryFiles("saves/campaigns/", "*.0adcampaign", false);
|
|
for (const file of files)
|
|
{
|
|
const name = file.replace("saves/campaigns/", "").replace(".0adcampaign", "");
|
|
try
|
|
{
|
|
out.push(new CampaignRun(name).load());
|
|
}
|
|
catch(err)
|
|
{
|
|
warn(err.toString());
|
|
out.push(new BrokenRun(name));
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
loadCampaign()
|
|
{
|
|
const filename = this.currentRuns[this.selectedRun].filename;
|
|
const run = new CampaignRun(filename)
|
|
.load()
|
|
.setCurrent();
|
|
|
|
Engine.SwitchGuiPage(run.getMenuPath(), {
|
|
"filename": run.filename
|
|
});
|
|
}
|
|
|
|
async deleteSelectedRun()
|
|
{
|
|
if (this.selectedRun === -1)
|
|
return;
|
|
|
|
const run = this.currentRuns[this.selectedRun];
|
|
|
|
const buttonIndex = await messageBox(
|
|
400, 200,
|
|
sprintf(translate("Are you sure you want to delete run %s? This cannot be undone."), run.getLabel()),
|
|
translate("Confirmation"),
|
|
[translate("No"), translate("Yes")]);
|
|
if (buttonIndex === 0)
|
|
return;
|
|
run.destroy();
|
|
this.currentRuns.splice(this.selectedRun, 1);
|
|
this.selectedRun = -1;
|
|
}
|
|
|
|
startSelectedRun()
|
|
{
|
|
if (this.currentRuns[this.selectedRun] instanceof CampaignRun)
|
|
this.loadCampaign();
|
|
}
|
|
|
|
displayCurrentRuns()
|
|
{
|
|
this.runSelection.list = this.currentRuns.map(run => run.getLabel(true));
|
|
this.runSelection.list_data = this.currentRuns.map(run => run.filename);
|
|
}
|
|
|
|
render()
|
|
{
|
|
this.noCampaignsText.hidden = !!this.currentRuns.length;
|
|
Engine.GetGUIObjectByName('deleteGameButton').enabled = this.selectedRun !== -1;
|
|
Engine.GetGUIObjectByName('startButton').enabled = this.selectedRun !== -1 && this.currentRuns[this.selectedRun] instanceof CampaignRun;
|
|
this.displayCurrentRuns();
|
|
}
|
|
}
|
|
|
|
|
|
var g_LoadModal;
|
|
|
|
function init()
|
|
{
|
|
g_LoadModal = new LoadModal();
|
|
}
|