Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/gamesetup/Controls/MapCache.js
- This file was added.
Property | Old Value | New Value |
---|---|---|
svn:eol-style | null | native \ No newline at end of property |
/** | |||||
* This class obtains, caches and provides the gamesettings from map XML and JSON files. | |||||
*/ | |||||
class MapCache | |||||
{ | |||||
constructor() | |||||
{ | |||||
this.mapFilters = Engine.ReadJSONFile(this.FiltersFile); | |||||
translateObjectKeys(this.mapFilters, ["Name", "Tooltip"]); | |||||
this.cache = {}; | |||||
} | |||||
getMapTypePath(mapTypeName) | |||||
{ | |||||
if (!this.MapTypes[mapTypeName]) | |||||
{ | |||||
error("Can't get filtered maps for invalid maptype: " + mapTypeName); | |||||
return undefined; | |||||
} | |||||
return this.MapTypes[mapTypeName].path; | |||||
} | |||||
getMapData(mapTypeName, mapPath) | |||||
{ | |||||
if (!mapPath || mapPath == "random") | |||||
return undefined; | |||||
if (!this.cache[mapPath]) | |||||
{ | |||||
Engine.ProfileStart("getMapData"); | |||||
let mapData = mapTypeName == "random" ? | |||||
Engine.ReadJSONFile(mapPath + ".json") : | |||||
Engine.LoadMapSettings(mapPath); | |||||
// Remove gaia, TODO: Maps should be consistent | |||||
if (mapData && | |||||
mapData.settings && | |||||
mapData.settings.PlayerData && | |||||
mapData.settings.PlayerData.length && | |||||
!mapData.settings.PlayerData[0]) | |||||
{ | |||||
mapData.settings.PlayerData.shift(); | |||||
} | |||||
this.cache[mapPath] = mapData; | |||||
Engine.ProfileStop(); | |||||
} | |||||
return this.cache[mapPath]; | |||||
} | |||||
/** | |||||
* Doesn't translate, so that networked and lobby page viewers can do that locally. | |||||
* The result is to be used with translateMapTitle. | |||||
*/ | |||||
getTranslatableMapName(mapTypeName, mapPath) | |||||
{ | |||||
if (mapPath == "random") | |||||
return "random"; | |||||
let mapData = this.getMapData(mapTypeName, mapPath); | |||||
if (!mapData || !mapData.settings || !mapData.settings.Name) | |||||
return undefined; | |||||
return mapData.settings.Name; | |||||
} | |||||
/** | |||||
* Some map filters may reject every map of a particular mapType. | |||||
* This function allows identifying which map filters have any matches for that maptype. | |||||
*/ | |||||
getAvailableMapFilters(mapTypeName) | |||||
{ | |||||
return this.mapFilters.filter(filter => | |||||
this.getFilteredMaps(mapTypeName, filter.Name, true)); | |||||
} | |||||
/** | |||||
* This function identifies all maps matching the given mapType and mapFilter. | |||||
* If existence is true, it will only test if there is at least one file for that mapType and mapFilter. | |||||
* Otherwise it returns an array with filename, translated map title and map description. | |||||
*/ | |||||
getFilteredMaps(mapTypeName, filterName, existence) | |||||
{ | |||||
let mapType = this.MapTypes[mapTypeName] || undefined; | |||||
if (!mapType) | |||||
{ | |||||
error("Can't get filtered maps for invalid maptype: " + mapTypeName); | |||||
return undefined; | |||||
} | |||||
let mapFilter = this.mapFilters.find(filter => filter.Name == filterName); | |||||
if (!mapFilter) | |||||
{ | |||||
error("Invalid mapfilter name: " + filterName); | |||||
return undefined; | |||||
} | |||||
let maps = []; | |||||
for (let filename of listFiles(mapType.path, mapType.suffix, false)) | |||||
{ | |||||
if (filename.startsWith(this.HiddenFilesPrefix)) | |||||
continue; | |||||
let mapPath = mapType.path + filename; | |||||
let mapData = this.getMapData(mapTypeName, mapPath); | |||||
// Map files may come with custom json files | |||||
if (!mapData || !mapData.settings) | |||||
continue; | |||||
if (MatchesClassList(mapData.settings.Keywords || [], mapFilter.Match)) | |||||
{ | |||||
if (existence) | |||||
return true; | |||||
maps.push({ | |||||
"file": mapPath, | |||||
"name": translate(mapData.settings.Name), | |||||
"description": translate(mapData.settings.Description) | |||||
}); | |||||
} | |||||
} | |||||
return existence ? false : maps; | |||||
} | |||||
} | |||||
/** | |||||
* Directory containing all maps of the given type. | |||||
*/ | |||||
MapCache.prototype.MapTypes = { | |||||
"scenario": { | |||||
Stan: Shouldn't it look for XMB as well? | |||||
Done Inline ActionsThat would be new. elexis: That would be new. | |||||
Not Done Inline ActionsAnd nice, since we bother caching those files while they are never read... :/ Stan: And nice, since we bother caching those files while they are never read... :/ | |||||
"path":"maps/scenarios/", | |||||
"suffix": ".xml", | |||||
}, | |||||
"skirmish": { | |||||
"path": "maps/skirmishes/", | |||||
"suffix": ".xml" | |||||
}, | |||||
"random": { | |||||
"path": "maps/random/", | |||||
"suffix": ".json" | |||||
} | |||||
}; | |||||
MapCache.prototype.FiltersFile = | |||||
"gui/gamesetup/Controls/MapFilters.json"; | |||||
/** | |||||
* When maps start with this prefix, they will not appear in the maplist. | |||||
* Used for the Atlas _default.xml for instance. | |||||
*/ | |||||
MapCache.prototype.HiddenFilesPrefix = | |||||
"_"; |
Wildfire Games · Phabricator
Shouldn't it look for XMB as well?