print ABR file structure recursively

This commit is contained in:
2019-02-07 22:05:30 +01:00
parent 17d5320ecb
commit f76a5c2fc3
4 changed files with 100 additions and 54 deletions

113
src/abr.h
View File

@@ -1,6 +1,8 @@
#pragma once
#include "asset.h"
#include <codecvt>
#include <fmt/core.h>
#include "util.h"
class BinaryStream
{
@@ -31,9 +33,9 @@ public:
m_byte_order = byte_order;
m_swap = byte_order == ByteOrder::Host ? false : byte_order != sys_order();
}
inline void skip(size_t bytes) { m_cur += bytes; }
inline bool eof() { return std::distance(m_ptr, m_cur) >= m_size; }
inline bool has_data(size_t sz) { return std::distance(m_ptr, m_cur + sz) < m_size; }
void skip(size_t bytes) { m_cur += bytes; }
bool eof() { return std::distance(m_ptr, m_cur) >= m_size; }
bool has_data(size_t sz) { return std::distance(m_ptr, m_cur + sz) < m_size; }
uint8_t ru8() { return read<uint8_t>(); }
uint16_t ru16() { return m_swap ? swap(read<uint16_t>()) : read<uint16_t>(); }
uint32_t ru32() { return m_swap ? swap(read<uint32_t>()) : read<uint32_t>(); }
@@ -45,21 +47,23 @@ public:
float rflt() { return read<float>(); }
double rdbl() { return read<double>(); }
std::string pick(size_t chars) { return { (char*)m_cur, chars }; }
std::string rstring() { auto len = ru32(); return { advance<char>(len), len }; }
std::string rstring()
{
auto len = ru32();
return { advance<char>(len), len };
}
std::string rstring(size_t len) { return { advance<char>(len), len }; }
std::wstring rwstring() { auto len = ru32(); return rwstring(len); }
std::wstring rwstring(size_t len) {
//std::wstring_convert<std::codecvt_utf16<char>> conv;
char* ptr = advance<char>(len * 2);
std::wstring rwstring()
{
auto len = ru32();
return rwstring(len);
}
std::wstring rwstring(size_t len)
{
auto ptr = advance<char>(len * 2);
for (int i = 0; i < len; i++)
std::swap(ptr[i * 2], ptr[i * 2 + 1]);
//auto s = conv.from_bytes(ptr, ptr + len);
wchar_t* wptr = (wchar_t*)ptr;
//std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
//std::string utf8 = convert.to_bytes(wptr, wptr + len);
//std::wstring ws(len, 0);
//mbsrtowcs((wchar_t*)ws.data(), (const char**)&ptr, len, nullptr);
return std::wstring(wptr, len);
return std::wstring((wchar_t*)ptr, len);
}
protected:
template<typename T> inline T align4(T x) { return ((x - T{1}) & (~(T{3}))) + T{4}; }
@@ -127,43 +131,56 @@ private:
ByteOrder m_byte_order = ByteOrder::Host;
};
static constexpr const uint32_t abr_s2sig(const char s[4])
{
return (uint32_t)(s[0]) << 0
| (uint32_t)(s[1]) << 8
| (uint32_t)(s[2]) << 16
| (uint32_t)(s[3]) << 24;
}
static constexpr const std::array<char, 5> abr_sig2sz(uint32_t x)
{
return {
(char)(x >> 0 ),
(char)(x >> 8 ),
(char)(x >> 16),
(char)(x >> 24),
0
};
}
class ABR : public BinaryStream
{
struct Type {
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() const { return "type"; }
virtual std::string str(int indent, const std::string& prefix) const
{
return "type";
}
};
struct Class : public Type { };
struct Property : public Type { };
struct Reference : public Type { };
struct List : public Type{
Type::Vec children;
struct List : public Type
{
Type::Vec items;
virtual std::string str(int indent, const std::string& prefix) const override
{
//return std::string(indent, '-') + prefix + fmt::format("list: {} items", items.size());
auto ret = std::string(indent, '-') + fmt::format("list: {} props:\n", items.size());
for (int i = 0; i < items.size(); i++)
ret += items[i]->str(indent + 1, fmt::format("{}) ", i) + "\n");
return ret;
}
};
struct Double : public Type
{
double value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("double: {}", value); }
};
struct UnitFloat : public Type
{
std::string unit;
double value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("float: {} ({})", value, unit); }
};
struct String : public Type
{
std::wstring value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("string: {}", wstr2str(value)); }
};
struct Enum : public Type
{
};
struct Double : public Type { double value; };
struct UnitFloat : public Type { std::string unit; double value; };
struct String : public Type { std::wstring value; };
struct Enum : public Type { };
struct EnumRef : public Type { };
struct Offset : public Type { };
struct Identifier : public Type { };
@@ -172,12 +189,15 @@ class ABR : public BinaryStream
struct Integer : public Type
{
int32_t value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("int: {}", value); }
};
struct LargeInteger : public Type { };
struct Boolean : public Type
{
bool value;
virtual std::string str() const override { return "Boolean: " + ; }
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("bool: {}", value); }
};
struct Alias : public Type { };
struct Descriptor : public Type
@@ -185,6 +205,13 @@ class ABR : public BinaryStream
std::wstring name;
std::string class_id;
Type::Map props;
virtual std::string str(int indent, const std::string& prefix) const override
{
auto ret = std::string(indent, '-') + fmt::format("objc: {} props:", props.size());
for (const auto& p : props)
ret += "\n" + p.second->str(indent + 1, fmt::format("'{}' ", p.first));
return ret;
}
};
public: