Ticket #3834: smallerOOSlog.2.diff

File smallerOOSlog.2.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        for (std::pair<entity_id_t, std::string> templateEnt : m_LatestTemplates)
    6465        {
    65             if (ENTITY_IS_LOCAL(it->first))
     66            if (ENTITY_IS_LOCAL(templateEnt.first))
    6667                continue;
    67             ++count;
     68            templateMap[templateEnt.second].push_back(templateEnt.first);
    6869        }
    69         serialize.NumberU32_Unbounded("num entities", (u32)count);
    7070
    71         for (std::map<entity_id_t, std::string>::const_iterator it = m_LatestTemplates.begin(); it != m_LatestTemplates.end(); ++it)
     71        serialize.NumberU32_Unbounded("num templates", (u32)templateMap.size());
     72        for (std::pair<CStr, std::vector<entity_id_t>> list : templateMap)
    7273        {
    73             if (ENTITY_IS_LOCAL(it->first))
    74                 continue;
    75             serialize.NumberU32_Unbounded("id", it->first);
    76             serialize.StringASCII("template", it->second, 0, 256);
     74            serialize.StringASCII("template", list.first, 0, 256);
     75            SerializeVector<SerializeU32_Unbounded>()(serialize, "ids", list.second);
    7776        }
    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
    8277    }
    8378
    8479    virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize)
     
    8580    {
    8681        Init(paramNode);
    8782
    88         u32 numEntities;
    89         deserialize.NumberU32_Unbounded("num entities", numEntities);
    90         for (u32 i = 0; i < numEntities; ++i)
     83        u32 numTemplates;
     84        deserialize.NumberU32_Unbounded("num entities", numTemplates);
     85        for (u32 i = 0; i < numTemplates; ++i)
    9186        {
    92             entity_id_t ent;
    9387            std::string templateName;
    94             deserialize.NumberU32_Unbounded("id", ent);
     88            std::vector<entity_id_t> ids;
    9589            deserialize.StringASCII("template", templateName, 0, 256);
    96             m_LatestTemplates[ent] = templateName;
     90            SerializeVector<SerializeU32_Unbounded>()(deserialize, "ids", ids);
     91            for (entity_id_t id : ids)
     92                m_LatestTemplates[id] = templateName;   
    9793        }
    9894    }
    9995
  • source/simulation2/serialization/SerializeTemplates.h

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