1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/* Copyright (C) 2020 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "ComponentManager.h"
#include "IComponent.h"
#include "ParamNode.h"
#include "simulation2/MessageTypes.h"
#include "simulation2/serialization/DebugSerializer.h"
#include "simulation2/serialization/HashSerializer.h"
#include "simulation2/serialization/StdSerializer.h"
#include "simulation2/serialization/StdDeserializer.h"
#include "simulation2/components/ICmpTemplateManager.h"
#include "ps/CLogger.h"
std::string SerializeRNG(const boost::random::rand48& rng)
{
std::stringstream s;
s << rng;
return s.str();
}
void DeserializeRNG(const std::string& str, boost::random::rand48& rng)
{
std::stringstream s;
s << str;
s >> rng;
}
bool CComponentManager::DumpDebugState(std::ostream& stream, bool includeDebugInfo) const
{
CDebugSerializer serializer(m_ScriptInterface, stream, includeDebugInfo);
serializer.StringASCII("rng", SerializeRNG(m_RNG), 0, 32);
serializer.TextLine("entities:");
// We want the output to be grouped by entity ID, so invert the CComponentManager data structures
std::map<entity_id_t, std::map<ComponentTypeId, IComponent*> > components;
//std::map<ComponentTypeId, std::string> names;
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator ctit = m_ComponentsByTypeId.begin();
for (; ctit != m_ComponentsByTypeId.end(); ++ctit)
{
std::map<entity_id_t, IComponent*>::const_iterator eit = ctit->second.begin();
for (; eit != ctit->second.end(); ++eit)
{
components[eit->first][ctit->first] = eit->second;
}
}
std::map<entity_id_t, std::map<ComponentTypeId, IComponent*> >::const_iterator cit = components.begin();
for (; cit != components.end(); ++cit)
{
std::stringstream n;
n << "- id: " << cit->first;
serializer.TextLine(n.str());
if (ENTITY_IS_LOCAL(cit->first))
serializer.TextLine(" type: local");
std::map<ComponentTypeId, IComponent*>::const_iterator it = cit->second.begin();
for (; it != cit->second.end(); ++it)
{
std::stringstream st;
st << " " << LookupComponentTypeName(it->first) << ":";
serializer.TextLine(st.str());
serializer.Indent(4);
it->second->Serialize(serializer);
serializer.Dedent(4);
}
serializer.TextLine("");
}
// TODO: catch exceptions
return true;
}
bool CComponentManager::ComputeStateHash(std::string& outHash, bool quick) const
{
// Hash serialization: this includes the minimal data necessary to detect
// differences in the state, and ignores things like counts and names
// If 'quick' is set, this checks even fewer things, so that it will
// be fast enough to run every turn but will typically detect any
// out-of-syncs fairly soon
CHashSerializer serializer(m_ScriptInterface);
serializer.StringASCII("rng", SerializeRNG(m_RNG), 0, 32);
serializer.NumberU32_Unbounded("next entity id", m_NextEntityId);
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator cit = m_ComponentsByTypeId.begin();
for (; cit != m_ComponentsByTypeId.end(); ++cit)
{
// In quick mode, only check unit positions
if (quick && !(cit->first == CID_Position))
continue;
// Only emit component types if they have a component that will be serialized
bool needsSerialization = false;
for (std::map<entity_id_t, IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)
{
// Don't serialize local entities
if (ENTITY_IS_LOCAL(eit->first))
continue;
needsSerialization = true;
break;
}
if (!needsSerialization)
continue;
serializer.NumberI32_Unbounded("component type id", cit->first);
for (std::map<entity_id_t, IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)
{
// Don't serialize local entities
if (ENTITY_IS_LOCAL(eit->first))
continue;
serializer.NumberU32_Unbounded("entity id", eit->first);
eit->second->Serialize(serializer);
}
}
outHash = std::string((const char*)serializer.ComputeHash(), serializer.GetHashLength());
// TODO: catch exceptions
return true;
}
/*
* Simulation state serialization format:
*
* TODO: Global version number.
* Number of SYSTEM_ENTITY component types
* For each SYSTEM_ENTITY component type:
* Component type name
* TODO: Component type version number.
* Component state.
* Number of (non-empty, non-SYSTEM_ENTITY-only) component types.
* For each component type:
* Component type name.
* TODO: Component type version number.
* Number of entities.
* For each entity:
* Entity id.
* Component state.
*
* Rationale:
* Saved games should be valid across patches, which might change component
* type IDs. Thus the names are serialized, not the IDs.
* Version numbers are used so saved games from future versions can be rejected,
* and those from older versions can be fixed up to work with the latest version.
* (These aren't really needed for networked games (where everyone will have the same
* version), but it doesn't seem worth having a separate codepath for that.)
*/
bool CComponentManager::SerializeState(std::ostream& stream) const
{
CStdSerializer serializer(m_ScriptInterface, stream);
// We don't serialize the destruction queue, since we'd have to be careful to skip local entities etc.
// This means we cannot have non-local entities in the destruction queue at this point.
ENSURE(m_DestructionQueue.empty() || std::find_if(m_DestructionQueue.begin(), m_DestructionQueue.end(),
[](entity_id_t ent) { return !ENTITY_IS_LOCAL(ent); }) == m_DestructionQueue.end());
serializer.StringASCII("rng", SerializeRNG(m_RNG), 0, 32);
serializer.NumberU32_Unbounded("next entity id", m_NextEntityId);
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> >::const_iterator cit;
uint32_t numSystemComponentTypes = 0;
uint32_t numComponentTypes = 0;
std::set<ComponentTypeId> serializedSystemComponentTypes;
std::set<ComponentTypeId> serializedComponentTypes;
for (cit = m_ComponentsByTypeId.begin(); cit != m_ComponentsByTypeId.end(); ++cit)
{
// Only emit component types if they have a component that will be serialized
bool needsSerialization = false;
for (std::map<entity_id_t, IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)
{
// Don't serialize local entities, and handle SYSTEM_ENTITY separately
if (ENTITY_IS_LOCAL(eit->first) || eit->first == SYSTEM_ENTITY)
continue;
needsSerialization = true;
break;
}
if (needsSerialization)
{
numComponentTypes++;
serializedComponentTypes.insert(cit->first);
}
if (cit->second.find(SYSTEM_ENTITY) != cit->second.end())
{
numSystemComponentTypes++;
serializedSystemComponentTypes.insert(cit->first);
}
}
serializer.NumberU32_Unbounded("num system component types", numSystemComponentTypes);
for (cit = m_ComponentsByTypeId.begin(); cit != m_ComponentsByTypeId.end(); ++cit)
{
if (serializedSystemComponentTypes.find(cit->first) == serializedSystemComponentTypes.end())
continue;
std::map<ComponentTypeId, ComponentType>::const_iterator ctit = m_ComponentTypesById.find(cit->first);
if (ctit == m_ComponentTypesById.end())
{
debug_warn(L"Invalid ctit"); // this should never happen
return false;
}
serializer.StringASCII("name", ctit->second.name, 0, 255);
std::map<entity_id_t, IComponent*>::const_iterator eit = cit->second.find(SYSTEM_ENTITY);
if (eit == cit->second.end())
{
debug_warn(L"Invalid eit"); // this should never happen
return false;
}
eit->second->Serialize(serializer);
}
serializer.NumberU32_Unbounded("num component types", numComponentTypes);
for (cit = m_ComponentsByTypeId.begin(); cit != m_ComponentsByTypeId.end(); ++cit)
{
if (serializedComponentTypes.find(cit->first) == serializedComponentTypes.end())
continue;
std::map<ComponentTypeId, ComponentType>::const_iterator ctit = m_ComponentTypesById.find(cit->first);
if (ctit == m_ComponentTypesById.end())
{
debug_warn(L"Invalid ctit"); // this should never happen
return false;
}
serializer.StringASCII("name", ctit->second.name, 0, 255);
// Count the components before serializing any of them
uint32_t numComponents = 0;
for (std::map<entity_id_t, IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)
{
// Don't serialize local entities or SYSTEM_ENTITY
if (ENTITY_IS_LOCAL(eit->first) || eit->first == SYSTEM_ENTITY)
continue;
numComponents++;
}
// Emit the count
serializer.NumberU32_Unbounded("num components", numComponents);
// Serialize the components now
for (std::map<entity_id_t, IComponent*>::const_iterator eit = cit->second.begin(); eit != cit->second.end(); ++eit)
{
// Don't serialize local entities or SYSTEM_ENTITY
if (ENTITY_IS_LOCAL(eit->first) || eit->first == SYSTEM_ENTITY)
continue;
serializer.NumberU32_Unbounded("entity id", eit->first);
eit->second->Serialize(serializer);
}
}
// TODO: catch exceptions
return true;
}
bool CComponentManager::DeserializeState(std::istream& stream)
{
try
{
CStdDeserializer deserializer(m_ScriptInterface, stream);
ResetState();
InitSystemEntity();
std::string rng;
deserializer.StringASCII("rng", rng, 0, 32);
DeserializeRNG(rng, m_RNG);
deserializer.NumberU32_Unbounded("next entity id", m_NextEntityId); // TODO: use sensible bounds
uint32_t numSystemComponentTypes;
deserializer.NumberU32_Unbounded("num system component types", numSystemComponentTypes);
ICmpTemplateManager* templateManager = NULL;
CParamNode noParam;
for (size_t i = 0; i < numSystemComponentTypes; ++i)
{
std::string ctname;
deserializer.StringASCII("name", ctname, 0, 255);
ComponentTypeId ctid = LookupCID(ctname);
if (ctid == CID__Invalid)
{
LOGERROR("Deserialization saw unrecognised component type '%s'", ctname.c_str());
return false;
}
IComponent* component = ConstructComponent(m_SystemEntity, ctid);
if (!component)
return false;
component->Deserialize(noParam, deserializer);
// If this was the template manager, remember it so we can use it when
// deserializing any further non-system entities
if (ctid == CID_TemplateManager)
templateManager = static_cast<ICmpTemplateManager*> (component);
}
uint32_t numComponentTypes;
deserializer.NumberU32_Unbounded("num component types", numComponentTypes);
for (size_t i = 0; i < numComponentTypes; ++i)
{
std::string ctname;
deserializer.StringASCII("name", ctname, 0, 255);
ComponentTypeId ctid = LookupCID(ctname);
if (ctid == CID__Invalid)
{
LOGERROR("Deserialization saw unrecognised component type '%s'", ctname.c_str());
return false;
}
uint32_t numComponents;
deserializer.NumberU32_Unbounded("num components", numComponents);
for (size_t j = 0; j < numComponents; ++j)
{
entity_id_t ent;
deserializer.NumberU32_Unbounded("entity id", ent);
IComponent* component = ConstructComponent(LookupEntityHandle(ent, true), ctid);
if (!component)
return false;
// Try to find the template for this entity
const CParamNode* entTemplate = NULL;
if (templateManager)
entTemplate = templateManager->LoadLatestTemplate(ent);
// Deserialize, with the appropriate template for this component
if (entTemplate)
component->Deserialize(entTemplate->GetChild(ctname.c_str()), deserializer);
else
component->Deserialize(noParam, deserializer);
}
}
if (stream.peek() != EOF)
{
LOGERROR("Deserialization didn't reach EOF");
return false;
}
// Allow components to do some final reinitialisation after everything is loaded
CMessageDeserialized msg;
BroadcastMessage(msg);
return true;
}
catch (PSERROR_Deserialize& e)
{
LOGERROR("Deserialization failed: %s", e.what());
return false;
}
}
|