huge abr refactoring

This commit is contained in:
2019-03-07 18:22:34 +01:00
parent 1b56dff7d0
commit 8a581ed59e
2 changed files with 319 additions and 255 deletions

361
src/abr.h
View File

@@ -176,6 +176,13 @@ public:
}
return data;
}
std::string rkey_or_string()
{
auto len = ru32();
if (len == 0)
len = 4;
return rstring(len);
}
protected:
template<typename T> inline T align4(T x) { return ((x - T{1}) & (~(T{3}))) + T{4}; }
template<typename T> T read()
@@ -240,12 +247,21 @@ public:
wu32(s.size());
write(s.data(), s.size());
}
void wstring_raw(std::string s)
{
write(s.data(), s.size());
}
void wwstring(std::wstring s)
{
wu32(s.size());
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter;
wstring(converter.to_bytes(s));
}
void wwstring_raw(std::wstring s)
{
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter;
wstring(converter.to_bytes(s));
}
void wpascal(std::string s)
{
wu8(s.size());
@@ -293,6 +309,10 @@ public:
idx = seq.first + seq.second;
}
}
void wkey_or_string(std::string s)
{
wstring(s);
}
protected:
template<typename T> void write(T x)
{
@@ -318,13 +338,24 @@ public:
{
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());
@@ -332,68 +363,169 @@ public:
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); }
{
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); }
{
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)); }
{
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); }
{
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 EnumRef : public Type { };
struct Offset : public Type { };
struct Identifier : public Type { };
struct Index : public Type { };
struct Name : public Type { };
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); }
{
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 LargeInteger : public Type { };
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); }
{
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 Alias : public Type { };
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()); }
{
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 +
@@ -427,6 +559,37 @@ public:
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
{
@@ -438,14 +601,44 @@ public:
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); }
{
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); }
{
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
{
@@ -454,8 +647,54 @@ public:
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)) { }
//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
{
@@ -463,7 +702,7 @@ public:
Rectangle rect;
std::vector<Channel> channels;
VMArray() = default;
VMArray(uint32_t version, const Rectangle& rect) : version(version), rect(rect) { }
//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();
@@ -518,62 +757,42 @@ public:
}
return nullptr;
}
};
};
class SerializedStreamReader : public SerializedStream, public BinaryStreamReader
{
public:
std::shared_ptr<VMArray> parse_vmem(); // Parse Virtual Memory Array List
std::shared_ptr<List> parse_vlls();
std::shared_ptr<String> parse_text();
std::shared_ptr<Descriptor> parse_objc();
std::shared_ptr<UnitFloat> parse_untf();
std::shared_ptr<Boolean> parse_bool();
std::shared_ptr<Integer> parse_long();
std::shared_ptr<Double> parse_doub();
std::shared_ptr<Enum> parse_enum();
std::shared_ptr<RawData> parse_tdta();
std::map<std::string /*key*/, std::function<Type::Ref()>> m_parser_table;
SerializedStreamReader();
std::string rkey_or_string()
{
auto len = ru32();
if (len == 0)
len = 4;
return rstring(len);
}
Rectangle rrect()
{
Rectangle ret;
ret.top = ru32();
ret.left = ru32();
ret.bottom = ru32();
ret.right = ru32();
return ret;
}
Point rpoint()
{
Point ret;
ret.x = ru16();
ret.y = ru16();
return ret;
}
Type::Ref call(std::string t)
{
if (m_parser_table.find(t) != m_parser_table.end())
virtual bool read(BinaryStreamReader& r) override
{
auto& method = m_parser_table[t];
return method();
// 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;
}
return nullptr;
}
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;
};
class ABR : private SerializedStreamReader
class ABR : public SerializedStream, public BinaryStreamReader
{
bool section_desc();
bool section_samp();