Line data Source code
1 : /** 2 : * This class provides a cache for accessing damage types metadata stored in JSON files. 3 : * Note that damage types need not be defined in JSON files to be handled in-game. 4 : * (this is intended to simplify modding) 5 : * This class must be initialised before using, as initialising it directly in globalscripts would 6 : * introduce disk I/O every time e.g. a GUI page is loaded. 7 : */ 8 : class DamageTypesMetadata 9 : { 10 : constructor() 11 : { 12 1 : this.damageTypeData = {}; 13 : 14 1 : let files = Engine.ListDirectoryFiles("simulation/data/damage_types", "*.json", false); 15 1 : for (let filename of files) 16 : { 17 2 : let data = Engine.ReadJSONFile(filename); 18 2 : if (!data) 19 0 : continue; 20 : 21 2 : if (data.code in this.damageTypeData) 22 : { 23 0 : error("Encountered two damage types with the code " + data.name); 24 0 : continue; 25 : } 26 : 27 2 : this.damageTypeData[data.code] = data; 28 : } 29 : 30 2 : let hasMetadata = (a) => this.damageTypeData[a] ? -1 : 1; 31 1 : this._sort = (a, b) => { 32 2 : if (this.damageTypeData[a] && this.damageTypeData[b]) 33 1 : return this.damageTypeData[a].order - this.damageTypeData[b].order; 34 1 : return hasMetadata(a) - hasMetadata(b); 35 : }; 36 : } 37 : 38 : /** 39 : * @param {string[]} damageTypes - The damageTypes to sort. 40 : * @returns {string[]} - The damageTypes in sorted order; first the ones 41 : * where metadata is provided, then the rest. 42 : */ 43 : sort(damageTypes) 44 : { 45 1 : let sorted = damageTypes.slice(); 46 1 : sorted.sort(this._sort); 47 1 : return sorted; 48 : } 49 : 50 : /** 51 : * @returns the name of the @param code damage type, or @code if no metadata exists in JSON files. 52 : */ 53 : getName(code) 54 : { 55 3 : return this.damageTypeData[code] ? this.damageTypeData[code].name : code; 56 : } 57 : }