Ticket #3834: smallerOOSlog.3.diff

File smallerOOSlog.3.diff, 6.3 KB (added by sanderd17, 8 years ago)
  • binaries/data/mods/public/simulation/components/AIInterface.js

     
    4848            continue;
    4949        if (typeof this[key] == "function")
    5050            continue;
     51        if (key == "templates")
     52            continue;
    5153        state[key] = this[key];
    5254    }
    5355    return state;
  • source/simulation2/components/CCmpRangeManager.cpp

     
    415415
    416416        serialize.Bool("global visibility update", m_GlobalVisibilityUpdate);
    417417        SerializeVector<SerializeU8_Unbounded>()(serialize, "global player visibility update", m_GlobalPlayerVisibilityUpdate);
    418         SerializeVector<SerializeU16_Unbounded>()(serialize, "dirty visibility", m_DirtyVisibility);
     418        SerializeRepetitiveVector<SerializeU16_Unbounded>()(serialize, "dirty visibility", m_DirtyVisibility);
    419419        SerializeVector<SerializeU32_Unbounded>()(serialize, "modified entities", m_ModifiedEntities);
    420420
    421421        // We don't serialize m_Subdivision, m_LosPlayerCounts or m_LosTiles
     
    422422        // since they can be recomputed from the entity data when deserializing;
    423423        // m_LosState must be serialized since it depends on the history of exploration
    424424
    425         SerializeVector<SerializeU32_Unbounded>()(serialize, "los state", m_LosState);
     425        SerializeRepetitiveVector<SerializeU32_Unbounded>()(serialize, "los state", m_LosState);
    426426        SerializeVector<SerializeU32_Unbounded>()(serialize, "shared los masks", m_SharedLosMasks);
    427427        SerializeVector<SerializeU16_Unbounded>()(serialize, "shared dirty visibility masks", m_SharedDirtyVisibilityMasks);
    428428    }
  • source/simulation2/components/CCmpTemplateManager.cpp

     
    1 /* Copyright (C) 2015 Wildfire Games.
     1/* Copyright (C) 2016 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    2121#include "ICmpTemplateManager.h"
    2222
    2323#include "simulation2/MessageTypes.h"
     24#include "simulation2/serialization/SerializeTemplates.h"
    2425
    2526#include "ps/TemplateLoader.h"
    2627
     
    5859
    5960    virtual void Serialize(ISerializer& serialize)
    6061    {
    61         size_t count = 0;
     62        std::map<CStr, std::vector<entity_id_t>> templateMap;
    6263
    63         for (std::map<entity_id_t, std::string>::const_iterator it = m_LatestTemplates.begin(); it != m_LatestTemplates.end(); ++it)
     64       
     65        for (std::pair<entity_id_t, std::string> templateEnt : m_LatestTemplates)
    6466        {
    65             if (ENTITY_IS_LOCAL(it->first))
     67            if (ENTITY_IS_LOCAL(templateEnt.first))
    6668                continue;
    67             ++count;
     69            // subtract the vecor length to get a more repetitive vector
     70            size_t s = templateMap[templateEnt.second].size();
     71            templateMap[templateEnt.second].push_back(templateEnt.first - s);
    6872        }
    69         serialize.NumberU32_Unbounded("num entities", (u32)count);
    70 
    71         for (std::map<entity_id_t, std::string>::const_iterator it = m_LatestTemplates.begin(); it != m_LatestTemplates.end(); ++it)
    72         {
    73             if (ENTITY_IS_LOCAL(it->first))
    74                 continue;
    75             serialize.NumberU32_Unbounded("id", it->first);
    76             serialize.StringASCII("template", it->second, 0, 256);
    77         }
    78         // TODO: maybe we should do some kind of interning thing instead of printing so many strings?
    79 
    80         // TODO: will need to serialize techs too, because we need to be giving out
    81         // template data before other components (like the tech components) have been deserialized
     73        SerializeMap<SerializeString, SerializeRepetitiveVector<SerializeU32_Unbounded>>()(serialize, "templates", templateMap);
    8274    }
    8375
    8476    virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize)
     
    8577    {
    8678        Init(paramNode);
    8779
    88         u32 numEntities;
    89         deserialize.NumberU32_Unbounded("num entities", numEntities);
    90         for (u32 i = 0; i < numEntities; ++i)
    91         {
    92             entity_id_t ent;
    93             std::string templateName;
    94             deserialize.NumberU32_Unbounded("id", ent);
    95             deserialize.StringASCII("template", templateName, 0, 256);
    96             m_LatestTemplates[ent] = templateName;
    97         }
     80        std::map<CStr, std::vector<entity_id_t>> templateMap;
     81        SerializeMap<SerializeString, SerializeRepetitiveVector<SerializeU32_Unbounded>>()(deserialize, "templates", templateMap);
     82        for (std::pair<CStr, std::vector<entity_id_t>> list : templateMap)
     83            for (u32 i = 0; i < list.second.size(); i++)
     84                m_LatestTemplates[list.second[i] + i] = list.first;
    9885    }
    9986
    10087    virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
  • source/simulation2/serialization/SerializeTemplates.h

     
    5353    }
    5454};
    5555
     56template<typename ELEM>
     57struct SerializeRepetitiveVector
     58{
     59    template<typename T>
     60    void operator()(ISerializer& serialize, const char* name, std::vector<T>& value)
     61    {
     62        size_t len = value.size();
     63        serialize.NumberU32_Unbounded("length", (u32)len);
     64        if (len == 0)
     65            return;
     66        u32 count = 1;
     67        T prevVal = value[0];
     68        for (size_t i = 1; i < len; ++i)
     69        {
     70            if (prevVal == value[i])
     71            {
     72                count++;
     73                continue;
     74            }
     75            serialize.NumberU32_Unbounded("#", count);
     76            ELEM()(serialize, name, prevVal);
     77            count = 1;
     78            prevVal = value[i];
     79        }
     80        serialize.NumberU32_Unbounded("#", count);
     81        ELEM()(serialize, name, prevVal);
     82    }
     83
     84    template<typename T>
     85    void operator()(IDeserializer& deserialize, const char* name, std::vector<T>& value)
     86    {
     87        value.clear();
     88        u32 len;
     89        deserialize.NumberU32_Unbounded("length", len);
     90        value.reserve(len); // TODO: watch out for out-of-memory
     91        for (size_t i = 0; i < len;)
     92        {
     93            u32 count;
     94            deserialize.NumberU32_Unbounded("#", count);
     95            T el;
     96            ELEM()(deserialize, name, el);
     97            i += count;
     98            value.insert(value.end(), count, el);
     99        }
     100    }
     101};
     102
    56103template<typename KS, typename VS>
    57104struct SerializeMap
    58105{