Changeset View
Standalone View
binaries/data/mods/public/gui/campaign/campaign_io.js
- This file was added.
// This file provides helper functions related to loading/saving campaign save states and campaign templates | |||||
// Most functions rely on loading campaign.js, but not all (such as canLoadCurrentCampaign) | |||||
elexisUnsubmitted Done Inline Actionselexis: ```
/**
* @file
```
| |||||
/* | |||||
* TODOs in this file: | |||||
* - Provide a way to check if the campaign state changed? | |||||
* Other things | |||||
*/ | |||||
Not Done Inline Actionsgreat elexis: great | |||||
Not Done Inline Actions"loadAvailableCampaigns"? (A campaign template sounds like a template for a campaign, i.e. it wouldn't be a campaign) elexis: "loadAvailableCampaigns"? (A campaign template sounds like a template for a campaign, i.e. it… | |||||
function LoadAvailableCampaigns() | |||||
{ | |||||
let campaigns = Engine.BuildDirEntList("campaigns/", "*.json", false); | |||||
Not Done Inline ActionslistFiles elexis: `listFiles` | |||||
let ret = {}; | |||||
for (let filename of campaigns) | |||||
{ | |||||
let data = Engine.ReadJSONFile(filename); | |||||
if (!data) | |||||
continue; | |||||
Done Inline Actionslet path = "campaigns/"; elexis: let path = "campaigns/";
filename.slice(path.length, -".json".length)? or was it splice again? | |||||
// TODO: sanity checks, probably in their own function? | |||||
if (!data.Name) | |||||
continue; | |||||
ret[filename.replace("campaigns/","").replace(".json","")] = data; | |||||
} | |||||
return ret; | |||||
} | |||||
function canLoadCurrentCampaign(verbose = false) | |||||
{ | |||||
let campaign = Engine.ConfigDB_GetValue("user", "currentcampaign"); | |||||
if (!campaign) | |||||
{ | |||||
if (verbose) | |||||
warn("No campaign chosen, currentcampaign is not defined in user.cfg. Quitting campaign mode.") | |||||
return false; | |||||
} | |||||
Done Inline ActionsWarnings often should be either a throw new Error(, or a JS message box if its a user error and not a developer error. elexis: Warnings often should be either a `throw new Error(`, or a JS message box if its a user error… | |||||
Done Inline ActionsI guess it could happen in case of a mod, or to help the user developing his own campaign to notice his mistakes. My idea was just to validate the JSON file here. wraitii: I guess it could happen in case of a mod, or to help the user developing his own campaign to… | |||||
if (!Engine.FileExists("campaignsaves/" + campaign + ".0adcampaign")) | |||||
{ | |||||
if (verbose) | |||||
warn("Current campaign not found. Quitting campaign mode."); | |||||
return false; | |||||
} | |||||
Not Done Inline ActionsWhat is the use case of exiting because of errors and not showing the reason why it exits? elexis: What is the use case of exiting because of errors and not showing the reason why it exits? | |||||
Not Done Inline ActionsThis isn't really an "error". We haven't found the campaign save from the config file, maybe the user has deleted it. In that case, we just pretend we don't have any current campaign. The verbose warning were mostly there for debug during development, they can be removed. wraitii: This isn't really an "error". We haven't found the campaign save from the config file, maybe… | |||||
// TODO: load up the file and do some checks? | |||||
return true; | |||||
} | |||||
function loadCurrentCampaignSave() | |||||
{ | |||||
let campaign = Engine.ConfigDB_GetValue("user", "currentcampaign"); | |||||
if (!canLoadCurrentCampaign(true)) | |||||
return false; | |||||
if (g_CampaignSave) | |||||
Done Inline ActionsRemove debug code or make it production code elexis: Remove debug code or make it production code | |||||
{ | |||||
warn("Campaign already loaded"); | |||||
return false; | |||||
} | |||||
g_CampaignSave = campaign; | |||||
Not Done Inline Actions|| elexis: `||` | |||||
let campaignData = loadCampaignSave(g_CampaignSave); | |||||
if (!campaignData) | |||||
{ | |||||
warn("Campaign failed to load properly. Quitting campaign mode.") | |||||
return false; | |||||
} | |||||
g_CampaignData = campaignData; | |||||
g_CampaignID = g_CampaignData.campaign; | |||||
if (!loadCampaignTemplate(g_CampaignID)) | |||||
return false; | |||||
// actually fetch the menu | |||||
Engine.SwitchGuiPage(g_CampaignTemplate.Interface, {"ID" : g_CampaignID, "template" : g_CampaignTemplate, "save": g_CampaignSave, "data" : g_CampaignData}); | |||||
Not Done Inline ActionsOne line per property makes that thing easier to read and extend IMO elexis: One line per property makes that thing easier to read and extend IMO | |||||
return true; | |||||
} | |||||
function loadCampaignTemplate(name) | |||||
{ | |||||
let data = Engine.ReadJSONFile("campaigns/" + name + ".json"); | |||||
if (!data) | |||||
{ | |||||
warn("Could not parse campaign data."); | |||||
return false; | |||||
} | |||||
g_CampaignTemplate = data; | |||||
return true; | |||||
} | |||||
function saveCurrentCampaign() | |||||
{ | |||||
if (!g_CampaignSave) | |||||
Done Inline Actionsunneeded elexis: unneeded | |||||
{ | |||||
warn("Cannot save current campaign, no campaign is currently loaded."); | |||||
return false; | |||||
} | |||||
return saveCampaign(g_CampaignSave, g_CampaignData); | |||||
} | |||||
Done Inline Actions(See other calls for whitespace) elexis: (See other calls for whitespace) | |||||
function saveCampaign(filename, data) | |||||
{ | |||||
Engine.WriteJSONFile("campaignsaves/" + filename + ".0adcampaign", data); | |||||
return true; | |||||
Not Done Inline ActionsThat (and possibly related variables) are not a filenames elexis: That (and possibly related variables) are not a filenames | |||||
} | |||||
function loadCampaignSave(filename) | |||||
Done Inline ActionsIs that warning redundant with a technical warning we get when saving? The player can't act upon that warning, but developes can still act on a technical JS warning (callstack). elexis: Is that warning redundant with a technical warning we get when saving? The player can't act… | |||||
{ | |||||
let campaignData = Engine.ReadJSONFile("campaignsaves/" + filename + ".0adcampaign"); | |||||
if (!campaignData) | |||||
{ | |||||
Not Done Inline Actions;; elexis: ;; | |||||
error("Campaign " + filename + " failed to load properly."); | |||||
return undefined; | |||||
} | |||||
return campaignData; | |||||
} | |||||
Done Inline ActionsIs that error redundant with the engine failing to read valid JSON and could become a oneliner? elexis: Is that error redundant with the engine failing to read valid JSON and could become a oneliner? | |||||
Done Inline Actionsunused return value elexis: unused return value |