Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/gamesettings/GameSettings.js
- This file was added.
/** | |||||
* Data store for game settings. | |||||
* Serializes to/from JSON. | |||||
*/ | |||||
class GameSettings | |||||
{ | |||||
Init() | |||||
{ | |||||
for (let comp in GameSettings.prototype.Attributes) | |||||
{ | |||||
let name = comp[0].toLowerCase() + comp.substr(1); | |||||
this[name] = new GameSettings.prototype.Attributes[comp](this); | |||||
} | |||||
for (let comp in this) | |||||
this[comp].Init(); | |||||
return this; | |||||
} | |||||
/** | |||||
* Serialize the settings into a g_GameAttributes-compatible format. | |||||
* This is currently also used for hotloading and MP sync, | |||||
* so it should be equivalent to the non-serialized state. | |||||
* TODO: does it make sense to serialize differently? | |||||
*/ | |||||
LegacySerialize() | |||||
{ | |||||
let attribs = { | |||||
"settings": {} | |||||
}; | |||||
for (let comp in this) | |||||
if (this[comp].LegacySerialize) | |||||
this[comp].LegacySerialize(attribs); | |||||
return attribs; | |||||
} | |||||
/** | |||||
* Deserialize from a g_GameAttributes-compatible format. | |||||
* TODO: this could/should maybe support partial deserialization, | |||||
* which means MP might actually send only the bits that change. | |||||
*/ | |||||
LegacyDeserialize(attribs) | |||||
{ | |||||
// Settings depend on the map, but some settings | |||||
// may also be illegal for a given map. | |||||
// It would be good to validate, but just bulk-accept at the moment. | |||||
// There is some light order-dependency between settings. | |||||
// First deserialize the map, then the player #, then victory conditions, then the rest. | |||||
// TODO: there's a DAG in there. | |||||
this.map.LegacyDeserialize(attribs); | |||||
this.playerCount.LegacyDeserialize(attribs); | |||||
this.victoryConditions.LegacyDeserialize(attribs); | |||||
for (let comp in this) | |||||
if (this[comp].LegacyDeserialize && | |||||
comp !== "map" && comp !== "playerCount" && comp !== "victoryConditions") | |||||
this[comp].LegacyDeserialize(attribs); | |||||
} | |||||
/** | |||||
* Call this before starting the game. | |||||
*/ | |||||
prepareForLaunch() | |||||
{ | |||||
this.pickRandomItems(); | |||||
} | |||||
/** | |||||
* Change "random" settings into their proper settings. | |||||
*/ | |||||
pickRandomItems() | |||||
{ | |||||
let components = Object.keys(this); | |||||
let i = 0; | |||||
while (components.length && i < 100) | |||||
{ | |||||
// Re-pick if any random setting was unrandomised, | |||||
// to make sure dependencies are cleared. | |||||
// TODO: there's probably a better way to handle this. | |||||
components = components.filter(comp => this[comp].pickRandomItems ? | |||||
!!this[comp].pickRandomItems() : false); | |||||
++i; | |||||
} | |||||
if (i === 100) | |||||
{ | |||||
throw new Error("Infinite loop picking random items, remains : " + uneval(components)); | |||||
} | |||||
} | |||||
} | |||||
Object.defineProperty(GameSettings.prototype, "Attributes", { | |||||
"value": {}, | |||||
"enumerable": false, | |||||
"writable": true, | |||||
}); |
Wildfire Games · Phabricator