split classes into files

This commit is contained in:
2019-03-07 18:46:00 +01:00
parent 8a581ed59e
commit 3e16341cf2
10 changed files with 1048 additions and 813 deletions

473
src/serializer.h Normal file
View File

@@ -0,0 +1,473 @@
#pragma once
#include "binary_stream.h"
#include "image.h"
#include "util.h"
#include "log.h"
class Serializer
{
public:
struct Type
{
using Vec = std::vector<std::shared_ptr<Type>>;
using Map = std::map<std::string, std::shared_ptr<Type>>;
using Ref = std::shared_ptr<Type>;
virtual std::string str(int indent, const std::string& prefix) const
{
return "type";
}
virtual bool read(BinaryStreamReader& r) { return false; }
virtual void write(BinaryStreamWriter& w) const { }
virtual std::string type_key() const { return ""; }
};
struct Class : public Type { };
struct Property : public Type { };
struct Reference : public Type { };
struct EnumRef : public Type { };
struct Offset : public Type { };
struct Identifier : public Type { };
struct Index : public Type { };
struct Name : public Type { };
struct LargeInteger : public Type { };
struct Alias : public Type { };
struct List : public Type
{
Type::Vec items;
virtual std::string type_key() const override { return "VlLs"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
auto ret = std::string(indent, '-') + fmt::format("list: {} items:", items.size());
for (int i = 0; i < items.size(); i++)
ret += "\n" + items[i]->str(indent + 1, fmt::format("{}) ", i));
return ret;
}
virtual bool read(BinaryStreamReader& r) override
{
auto count = r.ru32();
for (int i = 0; i < count; i++)
{
auto type = r.rstring(4);
auto item = instanciate(type);
if (!item || !item->read(r))
return false;
items.push_back(item);
}
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wi32(items.size());
for (auto& i : items)
i->write(w);
}
};
struct Double : public Type
{
using native_type = double;
double value;
virtual std::string type_key() const override { return "doub"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("double: {}", value);
}
virtual bool read(BinaryStreamReader& r) override
{
value = r.rdbl();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wdbl(value);
}
};
struct UnitFloat : public Type
{
using native_type = double;
std::string unit;
double value;
virtual std::string type_key() const override { return "UntF"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("float: {} ({})", value, unit);
}
virtual bool read(BinaryStreamReader& r) override
{
unit = r.rstring(4);
value = r.rdbl();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wstring_raw(unit);
w.wdbl(value);
}
};
struct String : public Type
{
using native_type = std::wstring;
std::wstring value;
virtual std::string type_key() const override { return "TEXT"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("string: {}", wstr2str(value));
}
virtual bool read(BinaryStreamReader& r) override
{
value = r.rwstring();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wwstring(value);
}
};
struct Enum : public Type
{
std::string type;
std::string value;
virtual std::string type_key() const override { return "enum"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("enum {}: {}", type, value);
}
virtual bool read(BinaryStreamReader& r) override
{
type = r.rkey_or_string();
value = r.rkey_or_string();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wkey_or_string(type);
w.wkey_or_string(value);
}
};
struct Integer : public Type
{
using native_type = int32_t;
int32_t value;
virtual std::string type_key() const override { return "long"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("int: {}", value);
}
virtual bool read(BinaryStreamReader& r) override
{
value = r.ri32();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wi32(value);
}
};
struct Boolean : public Type
{
using native_type = bool;
bool value;
virtual std::string type_key() const override { return "bool"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("bool: {}", value);
}
virtual bool read(BinaryStreamReader& r) override
{
value = r.ru8();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wu8(value);
}
};
struct RawData : public Type
{
std::vector<uint8_t> data;
virtual std::string type_key() const override { return "tdta"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("raw: {} bytes", data.size());
}
virtual bool read(BinaryStreamReader& r) override
{
data = r.rraw();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wraw(data);
}
};
struct Descriptor : public Type
{
std::wstring name;
std::string class_id;
Type::Map props;
virtual std::string type_key() const override { return "Objc"; }
virtual std::string str(int indent, const std::string& prefix) const override
{
auto ret = std::string(indent, '-') + prefix +
fmt::format("objc {} ({}): {} props:", wstr2str(name), class_id, props.size());
for (const auto& p : props)
ret += "\n" + p.second->str(indent + 1, fmt::format("'{}' ", p.first));
return ret;
}
bool has(const std::string& key) const
{
return props.find(key) != props.end();
}
template<typename T> std::shared_ptr<T> get(const std::string& key) const
{
return has(key) ? std::dynamic_pointer_cast<T>(props.at(key)) : nullptr;
}
template<typename T> auto value(const std::string& key) const
{
if (auto v = get<T>(key))
return v->value;
return decltype(T::value){};
}
template<typename T, typename D> auto value_or(const std::string& key, const D val) const
{
if (auto v = get<T>(key))
return v->value;
return val;
}
template<typename T, typename D> void value(const std::string& key, D& dest) const
{
if (auto v = get<T>(key))
dest = static_cast<D>(v->value);
}
virtual bool read(BinaryStreamReader& r) override
{
name = r.rwstring();
class_id = r.rkey_or_string();
auto count = r.ru32();
for (int i = 0; i < count; i++)
{
auto key = r.rkey_or_string();
auto type = r.rstring(4);
//printf("prop %s\n", t.c_str());
auto p = instanciate(type);
if (!p || !p->read(r))
return false;
if (props.find(key) != props.end())
LOG("DUPLICATE prop %s\n", key.c_str());
props[key] = p;
}
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wwstring(name);
w.wkey_or_string(class_id);
w.wu32(props.size());
for (auto& p : props)
{
w.wkey_or_string(p.first);
w.wstring(p.second->type_key());
p.second->write(w);
}
}
};
struct Rectangle : public Type
{
uint32_t top;
uint32_t left;
uint32_t bottom;
uint32_t right;
uint32_t area() const { return (right - left) * (bottom - top); }
uint32_t width() const { return right - left; }
uint32_t height() const { return bottom - top; }
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("rect: [{}, {}, {}, {}]", top, left, bottom, right);
}
virtual bool read(BinaryStreamReader& r) override
{
top = r.ru32();
left = r.ru32();
bottom = r.ru32();
right = r.ru32();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wu32(top);
w.wu32(left);
w.wu32(bottom);
w.wu32(right);
}
};
struct Point : public Type
{
uint32_t x;
uint32_t y;
virtual std::string str(int indent, const std::string& prefix) const override
{
return std::string(indent, '-') + prefix + fmt::format("point: [{}, {}]", x, y);
}
virtual bool read(BinaryStreamReader& r) override
{
x = r.ru32();
y = r.ru32();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
w.wu32(x);
w.wu32(y);
}
};
struct Channel : public Type
{
uint32_t depth;
Rectangle rect;
uint8_t compression;
std::vector<uint8_t> data;
Channel() = default;
//Channel(uint32_t depth, Rectangle rect, uint8_t compression, std::vector<uint8_t> data) :
// depth(depth), rect(rect), compression(compression), data(std::move(data)) { }
virtual bool read(BinaryStreamReader& r) override
{
auto length = r.ru32(); // skip if 0, length is from the next field to the end
if (length == 0)
return true;
depth = r.ru32(); // Pixel depth: 1, 8, 16 or 32
rect.read(r);
auto depth2 = r.ru16(); // again?
compression = r.ru8(); // 1 = zip
if (depth != 8)
{
LOG("unsupported depth %d bits\n", depth);
r.skip(length - 23);
return true;
}
if (compression == 0)
{
int data_size = (depth >> 3) * rect.area();
data = r.rraw(data_size);
}
else if (compression == 1)
{
auto start = r.pos();
auto height = rect.height();
// contain the compressed length of each scanline
std::vector<uint16_t> scanlines;
scanlines.reserve(height);
for (int i = 0; i < height; i++)
scanlines.push_back(r.ru16());
for (auto sl : scanlines)
{
auto decoded = r.rrle(sl);
data.insert(data.end(), decoded.begin(), decoded.end());
}
auto len = r.pos() - start;
}
else
{
LOG("unsupported compression mode %d\n", compression);
r.skip(length - 23);
return true;
}
}
virtual void write(BinaryStreamWriter& w) const override
{
}
};
struct VMArray : public Type
{
uint32_t version; // = 3
Rectangle rect;
std::vector<Channel> channels;
VMArray() = default;
//VMArray(uint32_t version, const Rectangle& rect) : version(version), rect(rect) { }
std::shared_ptr<Image> image(bool grayscale, bool invert) const
{
int nc = channels.size();
auto pixels = (channels[0].depth >> 3) * rect.area();
if (nc == 1 || nc >= 3)
{
auto img = std::make_shared<Image>();
img->comp = 4;
img->width = rect.width();
img->height = rect.height();
img->m_data = std::make_unique<uint8_t[]>(pixels * 4);
auto out = reinterpret_cast<glm::u8vec4*>(img->m_data.get());
if (grayscale)
{
auto const& raw = channels[0].data;
if (invert)
{
for (int i = 0; i < raw.size(); i++)
out[i] = glm::u8vec4(glm::u8vec3(255 - raw[i]), 255);
}
else
{
for (int i = 0; i < raw.size(); i++)
out[i] = glm::u8vec4(glm::u8vec3(raw[i]), 255);
}
}
else
{
std::fill_n(out, pixels, glm::u8vec4(255));
for (int ch = 0; ch < std::min(nc, 3); ch++)
{
auto const& raw = channels[ch].data;
if (invert)
{
for (int i = 0; i < raw.size(); i++)
out[i][ch] = 255 - raw[i];
}
else
{
for (int i = 0; i < raw.size(); i++)
out[i][ch] = raw[i];
}
}
}
return img;
//stbi_write_png(fmt::format("x64/out/{}.png", uid).c_str(),
// image->rect.width(), image->rect.height(), 4, out.data(), 0);
}
else
{
LOG("Error image with %d channels\n", channels.size());
}
return nullptr;
}
virtual bool read(BinaryStreamReader& r) override
{
// Virtual Memory Array List
version = r.ru32(); // = 3
assert(version == 3);
auto vmem_length = r.ru32();
// TODO: check if at the end there's good data
// check if the bounds are within the parent's size
rect.read(r);
auto vmem_channels = r.ru32();
// The following is a virtual memory array,
// repeated for the number of channels
// + one for a user mask + one for a sheet mask.
vmem_channels += 2; // user and sheet mask
for (int ch = 0; ch < vmem_channels; ch++)
{
auto array_written = r.ru32(); // skip if 0
if (array_written == 0)
continue;
channels.emplace_back();
if (!channels.back().read(r))
return false;
}
r.snap();
return true;
}
virtual void write(BinaryStreamWriter& w) const override
{
}
};
protected:
static Type::Ref instanciate(const std::string& key);
static std::map<std::string /*key*/, std::function<Type::Ref()>> m_ctor_table;
};