Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/serialization/BinarySerializer.h
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
class CBinarySerializerScriptImpl | class CBinarySerializerScriptImpl | ||||
{ | { | ||||
public: | public: | ||||
CBinarySerializerScriptImpl(const ScriptInterface& scriptInterface, ISerializer& serializer); | CBinarySerializerScriptImpl(const ScriptInterface& scriptInterface, ISerializer& serializer); | ||||
void ScriptString(const char* name, JS::HandleString string); | void ScriptString(const char* name, JS::HandleString string); | ||||
void HandleScriptVal(JS::HandleValue val); | void HandleScriptVal(JS::HandleValue val); | ||||
void SetSerializablePrototypes(shared_ptr<ObjectIdCache<std::wstring> > prototypes); | |||||
private: | private: | ||||
const ScriptInterface& m_ScriptInterface; | const ScriptInterface& m_ScriptInterface; | ||||
ISerializer& m_Serializer; | ISerializer& m_Serializer; | ||||
ObjectIdCache<u32> m_ScriptBackrefs; | ObjectIdCache<u32> m_ScriptBackrefs; | ||||
u32 m_ScriptBackrefsNext; | u32 m_ScriptBackrefsNext; | ||||
u32 GetScriptBackrefTag(JS::HandleObject obj); | u32 GetScriptBackrefTag(JS::HandleObject obj); | ||||
shared_ptr<ObjectIdCache<std::wstring> > m_SerializablePrototypes; | |||||
bool IsSerializablePrototype(JS::HandleObject prototype); | |||||
std::wstring GetPrototypeName(JS::HandleObject prototype); | |||||
}; | }; | ||||
/** | /** | ||||
* Serialize to a binary stream. T must just implement the Put() method. | * Serialize to a binary stream. T must just implement the Put() method. | ||||
* (We use this templated approach to allow compiler inlining.) | * (We use this templated approach to allow compiler inlining.) | ||||
*/ | */ | ||||
template <typename T> | template <typename T> | ||||
class CBinarySerializer : public ISerializer | class CBinarySerializer : public ISerializer | ||||
Show All 11 Lines | public: | ||||
CBinarySerializer(const ScriptInterface& scriptInterface, A& a) : | CBinarySerializer(const ScriptInterface& scriptInterface, A& a) : | ||||
m_ScriptImpl(new CBinarySerializerScriptImpl(scriptInterface, *this)), | m_ScriptImpl(new CBinarySerializerScriptImpl(scriptInterface, *this)), | ||||
m_Impl(a), | m_Impl(a), | ||||
m_RawStreamBuf(m_Impl), | m_RawStreamBuf(m_Impl), | ||||
m_RawStream(&m_RawStreamBuf) | m_RawStream(&m_RawStreamBuf) | ||||
{ | { | ||||
} | } | ||||
virtual void SetSerializablePrototypes(shared_ptr<ObjectIdCache<std::wstring> >& prototypes) | |||||
{ | |||||
m_ScriptImpl->SetSerializablePrototypes(prototypes); | |||||
} | |||||
protected: | protected: | ||||
/* | /* | ||||
The Put* implementations here are designed for subclasses | The Put* implementations here are designed for subclasses | ||||
that want an efficient, portable, deserializable representation. | that want an efficient, portable, deserializable representation. | ||||
(Subclasses with different requirements should override these methods.) | (Subclasses with different requirements should override these methods.) | ||||
Numbers are converted to little-endian byte strings, for portability | Numbers are converted to little-endian byte strings, for portability | ||||
and efficiency. | and efficiency. | ||||
▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator