Added JSON serialisation code for frames and sfx.

master
nick-the-ninja 2015-02-15 14:27:57 +00:00
parent 76452d79ea
commit ea07cbd107
4 changed files with 137 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#include "galaxy/StarSystem.h"
#include "Pi.h"
#include "Game.h"
#include "json/JsonUtils.h"
#include <algorithm>
Frame::Frame()
@ -42,6 +43,33 @@ void Frame::Serialize(Serializer::Writer &wr, Frame *f, Space *space)
Sfx::Serialize(wr, f);
}
void Frame::ToJson(Json::Value &jsonObj, Frame *f, Space *space)
{
Json::Value frameObj(Json::objectValue); // Create JSON object to contain frame data.
frameObj["flags"] = f->m_flags;
frameObj["radius"] = DoubleToStr(f->m_radius);
frameObj["label"] = f->m_label;
VectorToJson(frameObj, f->m_pos, "pos");
frameObj["ang_speed"] = DoubleToStr(f->m_angSpeed);
MatrixToJson(frameObj, f->m_initialOrient, "init_orient");
frameObj["index_for_system_body"] = space->GetIndexForSystemBody(f->m_sbody);
frameObj["index_for_astro_body"] = space->GetIndexForBody(f->m_astroBody);
Json::Value childFrameArray(Json::arrayValue); // Create JSON array to contain child frame data.
for (Frame* kid : f->GetChildren())
{
Json::Value childFrameArrayEl(Json::objectValue); // Create JSON object to contain child frame.
Frame::ToJson(childFrameArrayEl, kid, space);
childFrameArray.append(childFrameArrayEl); // Append child frame object to array.
}
frameObj["child_frames"] = childFrameArray; // Add child frame array to frame object.
Sfx::ToJson(frameObj, f);
jsonObj["frame"] = frameObj; // Add frame object to supplied object.
}
Frame *Frame::Unserialize(Serializer::Reader &rd, Space *space, Frame *parent, double at_time)
{
Frame *f = new Frame();
@ -66,6 +94,47 @@ Frame *Frame::Unserialize(Serializer::Reader &rd, Space *space, Frame *parent, d
return f;
}
Frame *Frame::FromJson(const Json::Value &jsonObj, Space *space, Frame *parent, double at_time)
{
Frame *f = new Frame();
f->m_parent = parent;
if (!jsonObj.isMember("frame")) throw SavedGameCorruptException();
Json::Value frameObj = jsonObj["frame"];
if (!frameObj.isMember("flags")) throw SavedGameCorruptException();
if (!frameObj.isMember("radius")) throw SavedGameCorruptException();
if (!frameObj.isMember("label")) throw SavedGameCorruptException();
if (!frameObj.isMember("pos")) throw SavedGameCorruptException();
if (!frameObj.isMember("ang_speed")) throw SavedGameCorruptException();
if (!frameObj.isMember("init_orient")) throw SavedGameCorruptException();
if (!frameObj.isMember("index_for_system_body")) throw SavedGameCorruptException();
if (!frameObj.isMember("index_for_astro_body")) throw SavedGameCorruptException();
f->m_flags = frameObj["flags"].asInt();
f->m_radius = StrToDouble(frameObj["radius"].asString());
f->m_label = frameObj["label"].asString();
JsonToVector(&(f->m_pos), frameObj, "pos");
f->m_angSpeed = StrToDouble(frameObj["ang_speed"].asString());
matrix3x3d orient;
JsonToMatrix(&orient, frameObj, "init_orient");
f->SetInitialOrient(orient, at_time);
f->m_sbody = space->GetSystemBodyByIndex(frameObj["index_for_system_body"].asUInt());
f->m_astroBodyIndex = frameObj["index_for_astro_body"].asUInt();
f->m_vel = vector3d(0.0); // m_vel is set to zero.
if (!frameObj.isMember("child_frames")) throw SavedGameCorruptException();
Json::Value childFrameArray = frameObj["child_frames"];
if (!childFrameArray.isArray()) throw SavedGameCorruptException();
for (unsigned int i = 0; i < childFrameArray.size(); ++i) {
f->m_children.push_back(FromJson(childFrameArray[i], space, f, at_time));
}
Sfx::FromJson(frameObj, f);
f->ClearMovement();
return f;
}
void Frame::PostUnserializeFixup(Frame *f, Space *space)
{
f->UpdateRootRelativeVars();

View File

@ -7,6 +7,7 @@
#include "libs.h"
#include "Serializer.h"
#include "IterationProxy.h"
#include "json/json.h"
#include <string>
#include <list>
@ -28,8 +29,10 @@ public:
Frame(Frame *parent, const char *label, unsigned int flags);
~Frame();
static void Serialize(Serializer::Writer &wr, Frame *f, Space *space);
static void ToJson(Json::Value &jsonObj, Frame *f, Space *space);
static void PostUnserializeFixup(Frame *f, Space *space);
static Frame *Unserialize(Serializer::Reader &rd, Space *space, Frame *parent, double at_time);
static Frame *FromJson(const Json::Value &jsonObj, Space *space, Frame *parent, double at_time);
const std::string &GetLabel() const { return m_label; }
void SetLabel(const char *label) { m_label = label; }

View File

@ -14,6 +14,7 @@
#include "graphics/Material.h"
#include "graphics/Renderer.h"
#include "graphics/TextureBuilder.h"
#include "json/JsonUtils.h"
using namespace Graphics;
@ -42,6 +43,18 @@ void Sfx::Save(Serializer::Writer &wr)
wr.Int32(m_type);
}
void Sfx::SaveToJson(Json::Value &jsonObj)
{
Json::Value sfxObj(Json::objectValue); // Create JSON object to contain sfx data.
VectorToJson(sfxObj, m_pos, "pos");
VectorToJson(sfxObj, m_vel, "vel");
sfxObj["age"] = FloatToStr(m_age);
sfxObj["type"] = m_type;
jsonObj["sfx"] = sfxObj; // Add sfx object to supplied object.
}
void Sfx::Load(Serializer::Reader &rd)
{
m_pos = rd.Vector3d();
@ -50,6 +63,21 @@ void Sfx::Load(Serializer::Reader &rd)
m_type = static_cast<Sfx::TYPE>(rd.Int32());
}
void Sfx::LoadFromJson(const Json::Value &jsonObj)
{
if (!jsonObj.isMember("sfx")) throw SavedGameCorruptException();
Json::Value sfxObj = jsonObj["sfx"];
if (!sfxObj.isMember("pos")) throw SavedGameCorruptException();
if (!sfxObj.isMember("vel")) throw SavedGameCorruptException();
if (!sfxObj.isMember("age")) throw SavedGameCorruptException();
if (!sfxObj.isMember("type")) throw SavedGameCorruptException();
JsonToVector(&m_pos, sfxObj, "pos");
JsonToVector(&m_vel, sfxObj, "vel");
m_age = StrToFloat(sfxObj["age"].asString());
m_type = static_cast<Sfx::TYPE>(sfxObj["type"].asInt());
}
void Sfx::Serialize(Serializer::Writer &wr, const Frame *f)
{
// how many sfx turds are active in frame?
@ -68,6 +96,26 @@ void Sfx::Serialize(Serializer::Writer &wr, const Frame *f)
}
}
void Sfx::ToJson(Json::Value &jsonObj, const Frame *f)
{
Json::Value sfxArray(Json::arrayValue); // Create JSON array to contain sfx data.
if (f->m_sfx)
{
for (int i = 0; i < MAX_SFX_PER_FRAME; i++)
{
if (f->m_sfx[i].m_type != TYPE_NONE)
{
Json::Value sfxArrayEl(Json::objectValue); // Create JSON object to contain sfx element.
f->m_sfx[i].SaveToJson(sfxArrayEl);
sfxArray.append(sfxArrayEl); // Append sfx object to array.
}
}
}
jsonObj["sfx_array"] = sfxArray; // Add sfx array to supplied object.
}
void Sfx::Unserialize(Serializer::Reader &rd, Frame *f)
{
int numActive = rd.Int32();
@ -79,6 +127,19 @@ void Sfx::Unserialize(Serializer::Reader &rd, Frame *f)
}
}
void Sfx::FromJson(const Json::Value &jsonObj, Frame *f)
{
if (!jsonObj.isMember("sfx_array")) throw SavedGameCorruptException();
Json::Value sfxArray = jsonObj["sfx_array"];
if (!sfxArray.isArray()) throw SavedGameCorruptException();
if (sfxArray.size()) f->m_sfx = new Sfx[MAX_SFX_PER_FRAME];
for (unsigned int i = 0; i < sfxArray.size(); ++i)
{
f->m_sfx[i].LoadFromJson(sfxArray[i]);
}
}
void Sfx::SetPosition(const vector3d &p)
{
m_pos = p;

View File

@ -27,7 +27,9 @@ public:
static void TimeStepAll(const float timeStep, Frame *f);
static void RenderAll(Graphics::Renderer *r, Frame *f, const Frame *camFrame);
static void Serialize(Serializer::Writer &wr, const Frame *f);
static void ToJson(Json::Value &jsonObj, const Frame *f);
static void Unserialize(Serializer::Reader &rd, Frame *f);
static void FromJson(const Json::Value &jsonObj, Frame *f);
Sfx();
void SetPosition(const vector3d &p);
@ -52,7 +54,9 @@ private:
void Render(Graphics::Renderer *r, const matrix4x4d &transform);
void TimeStepUpdate(const float timeStep);
void Save(Serializer::Writer &wr);
void SaveToJson(Json::Value &jsonObj);
void Load(Serializer::Reader &rd);
void LoadFromJson(const Json::Value &jsonObj);
vector3d m_pos;
vector3d m_vel;