Ticket #4239: serialization-fix.patch

File serialization-fix.patch, 5.6 KB (added by Itms, 8 years ago)

This new one should do the job, and uses an array instead of changing to a boolean

  • binaries/data/mods/public/simulation/components/Auras.js

     
    66    "</attribute>" +
    77    "<text a:help='A whitespace-separated list of aura files, placed under simulation/data/auras/'/>";
    88
     9Auras.prototype.Serialize = function()
     10{
     11    let ret = {};
     12    for (let key in this)
     13    {
     14        if (!this.hasOwnProperty(key))
     15            continue;
     16
     17        // Don't serialize the templates themselves
     18        if (key == "auras")
     19            continue;
     20
     21        ret[key] = this[key];
     22    }
     23
     24    return ret;
     25};
     26
     27Auras.prototype.Deserialize = function(data)
     28{
     29    for (let key in data)
     30        if (data.hasOwnProperty(key))
     31            this[key] = data[key];
     32
     33    // Reload aura templates from the disk
     34    this.auras = {};
     35    let cmpDataTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager);
     36    for (let name of this.GetAuraNames())
     37        this.auras[name] = cmpDataTemplateManager.GetAuraTemplate(name);
     38};
     39
    940Auras.prototype.Init = function()
    1041{
    1142    let cmpDataTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager);
    1243    this.auras = {};
    1344    this.affectedPlayers = {};
    14     let auraNames = this.GetAuraNames();
    15     for (let name of auraNames)
     45    for (let name of this.GetAuraNames())
    1646    {
    1747        this.affectedPlayers[name] = [];
    1848        this.auras[name] = cmpDataTemplateManager.GetAuraTemplate(name);
  • binaries/data/mods/public/simulation/components/DataTemplateManager.js

     
    1515        this.GetTechnologyTemplate(techNames[i]);
    1616};
    1717
     18DataTemplateManager.prototype.Serialize = null; // we have no dynamic state to save
     19
     20DataTemplateManager.prototype.Deserialize = function()
     21{
     22    this.Init();
     23};
     24
    1825DataTemplateManager.prototype.GetTechnologyTemplate = function(template)
    1926{
    2027    if (!this.allTechs[template])
  • binaries/data/mods/public/simulation/components/TechnologyManager.js

     
    55
    66TechnologyManager.prototype.Serialize = function()
    77{
    8     // The modifications cache will be affected by property reads from the GUI and other places so we shouldn't
    9     // serialize it.
     8    let ret = {};
    109
    11     var ret = {};
    12     for (var i in this)
     10    for (let key in this)
    1311    {
    14         if (this.hasOwnProperty(i))
    15             ret[i] = this[i];
     12        if (!this.hasOwnProperty(key))
     13            continue;
     14
     15        // The modifications cache will be affected by property reads from the GUI and
     16        // other places so we shouldn't serialize it.
     17        if (key == "modificationCache")
     18            continue;
     19
     20        // Only serialize the template name of researched technologies, to avoid serialization issues.
     21        if (key == "researchedTechs")
     22        {
     23            ret[key] = [];
     24            for (let templateName in this[key])
     25                ret[key].push(templateName);
     26            continue;
     27        }
     28
     29        ret[key] = this[key];
    1630    }
    17     ret.modificationCache = {};
     31
    1832    return ret;
    1933};
    2034
     35TechnologyManager.prototype.Deserialize = function(data)
     36{
     37    for (let key in data)
     38    {
     39        if (!data.hasOwnProperty(key))
     40            continue;
     41
     42        if (key == "researchedTechs")
     43        {
     44            this[key] = {};
     45            for (let templateName of data[key])
     46                this[key][templateName] = this.GetTechnologyTemplate(templateName);
     47            continue;
     48        }
     49
     50        this[key] = data[key];
     51    }
     52
     53    this.modificationCache = {};
     54};
     55
    2156TechnologyManager.prototype.Init = function()
    2257{
    2358    this.researchedTechs = {}; // technologies which have been researched
     
    4075
    4176    // Some technologies are automatically researched when their conditions are met.  They have no cost and are
    4277    // researched instantly.  This allows civ bonuses and more complicated technologies.
    43     this.autoResearchTech = {};
    44     var allTechs = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager).GetAllTechs();
    45     for (var key in allTechs)
     78    this.autoResearchTech = [];
     79    let allTechs = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager).GetAllTechs();
     80    for (let key in allTechs)
    4681    {
    4782        if (allTechs[key].autoResearch || allTechs[key].top)
    48             this.autoResearchTech[key] = allTechs[key];
     83            this.autoResearchTech.push(key);
    4984    }
    5085};
    5186
     
    5893// This function checks if the requirements of any autoresearch techs are met and if they are it researches them
    5994TechnologyManager.prototype.UpdateAutoResearch = function()
    6095{
    61     var cmpDataTempMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager);
    62     for (var key in this.autoResearchTech)
     96    let cmpDataTempMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_DataTemplateManager);
     97    for (let i in this.autoResearchTech)
    6398    {
    64         var tech = cmpDataTempMan.GetTechnologyTemplate(key);
    65         if ((tech.autoResearch && this.CanResearch(key))
     99        let template = this.autoResearchTech[i];
     100        let tech = cmpDataTempMan.GetTechnologyTemplate(template);
     101        if ((tech.autoResearch && this.CanResearch(template))
    66102            || (tech.top && (this.IsTechnologyResearched(tech.top) || this.IsTechnologyResearched(tech.bottom))))
    67103        {
    68             delete this.autoResearchTech[key];
    69             this.ResearchTechnology(key);
     104            // Remove the technology from the autoresearch list and research it
     105            this.autoResearchTech.splice(i, 1);
     106            this.ResearchTechnology(template);
    70107            return; // We will have recursively handled any knock-on effects so can just return
    71108        }
    72109    }