Skip to content

Commit

Permalink
update readme, rename File to BinaryWithAlignment, convert to string …
Browse files Browse the repository at this point in the history
…before stoul/stoull
  • Loading branch information
dt-12345 committed Jul 7, 2024
1 parent 9b09f82 commit 04a5cd2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
10 changes: 5 additions & 5 deletions py/py_byml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ void BindByml(py::module& parent) {
BindMap<Byml::Hash32>(m, "Hash32");
BindMap<Byml::Hash64>(m, "Hash64");

py::class_<Byml::File> clas(m, "File");
py::class_<Byml::BinaryWithAlignment> clas(m, "BinaryWithAlignment");
clas
.def(py::init<>())
.def(py::init<std::vector<u8>, U32>())
.def(py::self == py::self)
.def_readwrite("data", &Byml::File::data)
.def_readwrite("alignment", &Byml::File::align)
.def("__copy__", [](const Byml::File& f) { return Byml::File(f); })
.def("__deepcopy__", [](const Byml::File& f, py::dict) { return Byml::File(f); });
.def_readwrite("data", &Byml::BinaryWithAlignment::data)
.def_readwrite("alignment", &Byml::BinaryWithAlignment::align)
.def("__copy__", [](const Byml::BinaryWithAlignment& f) { return Byml::BinaryWithAlignment(f); })
.def("__deepcopy__", [](const Byml::BinaryWithAlignment& f, py::dict) { return Byml::BinaryWithAlignment(f); });
}
} // namespace oead::bind
2 changes: 1 addition & 1 deletion readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Features
Currently, oead only handles very common formats that are extensively used in recent games such as *Breath of the Wild* and *Super Mario Odyssey*.

* `AAMP <https://zeldamods.org/wiki/AAMP>`_ (binary parameter archive): Only version 2 is supported.
* `BYML <https://zeldamods.org/wiki/BYML>`_ (binary YAML): Versions 1-4 are fully supported, version 5-10 have partial support.
* `BYML <https://zeldamods.org/wiki/BYML>`_ (binary YAL): Versions 1-10 are supported (only 32-bit and 64-bit hash nodes), remapped container nodes, monotyped arrays, and non-container root nodes are not supported.
* `SARC <https://zeldamods.org/wiki/SARC>`_ (archive)
* `Yaz0 <https://zeldamods.org/wiki/Yaz0>`_ (compression algorithm)

Expand Down
36 changes: 18 additions & 18 deletions src/byml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum class NodeType : u8 {
// 0x30 to 0x3f are also hash nodes but with index remapping (unsupported)
String = 0xa0,
Binary = 0xa1,
File = 0xa2,
BinaryWithAlignment = 0xa2,
Array = 0xc0,
Dictionary = 0xc1,
StringTable = 0xc2,
Expand All @@ -81,7 +81,7 @@ enum class NodeType : u8 {
constexpr NodeType GetNodeType(Byml::Type type) {
constexpr std::array map{
NodeType::Null, NodeType::Hash32, NodeType::Hash64, NodeType::String, NodeType::Binary,
NodeType::File, NodeType::Array, NodeType::Dictionary, NodeType::Bool, NodeType::Int,
NodeType::BinaryWithAlignment, NodeType::Array, NodeType::Dictionary, NodeType::Bool, NodeType::Int,
NodeType::Float, NodeType::UInt, NodeType::Int64, NodeType::UInt64, NodeType::Double,
};
return map[u8(type)];
Expand All @@ -99,7 +99,7 @@ constexpr bool IsLongType(T type) {

template <typename T = NodeType>
constexpr bool IsNonInlineType(T type) {
return IsContainerType(type) || IsLongType(type) || type == T::Binary || type == T::File;
return IsContainerType(type) || IsLongType(type) || type == T::Binary || type == T::BinaryWithAlignment;
}

constexpr bool IsValidVersion(int version) {
Expand Down Expand Up @@ -211,11 +211,11 @@ class Parser {
return Byml{std::vector<u8>(m_reader.span().begin() + data_offset + 4,
m_reader.span().begin() + data_offset + 4 + size)};
}
case NodeType::File: {
case NodeType::BinaryWithAlignment: {
const u32 data_offset = *raw;
const u32 size = m_reader.Read<u32>(data_offset).value();
const u32 align = m_reader.Read<u32>().value();
return Byml{Byml::File{{m_reader.span().begin() + data_offset + 8,
return Byml{Byml::BinaryWithAlignment{{m_reader.span().begin() + data_offset + 8,
m_reader.span().begin() + data_offset + 8 + size}, align}};
}
case NodeType::Bool:
Expand Down Expand Up @@ -250,8 +250,8 @@ class Parser {
const u32 types_offset = offset + 4 + 8 * size;
for (u32 i = 0; i< size; ++i) {
const auto type = m_reader.Read<NodeType>(types_offset + i);
const auto hash = m_reader.Read<u32>(offset + 4 + 8 * i);
result.emplace(*hash, ParseContainerChildNode(offset + 8 + 8 * i, type.value()));
const auto hash = m_reader.Read<u32>(offset + 4 + 8 * i).value();
result.emplace(hash, ParseContainerChildNode(offset + 8 + 8 * i, type.value()));
}
return Byml{std::move(result)};
}
Expand Down Expand Up @@ -377,11 +377,11 @@ struct WriteContext {
writer.Write(static_cast<u32>(data.GetBinary().size()));
writer.WriteBytes(data.GetBinary());
return;
case Byml::Type::File:
writer.Seek(util::AlignUp(data.GetFile().align, writer.Tell() + 8) - 8);
writer.Write(static_cast<u32>(data.GetFile().data.size()));
writer.Write(data.GetFile().align);
writer.WriteBytes(data.GetFile().data);
case Byml::Type::BinaryWithAlignment:
writer.Seek(util::AlignUp(data.GetBinaryWithAlignment().align, writer.Tell() + 8) - 8);
writer.Write(static_cast<u32>(data.GetBinaryWithAlignment().data.size()));
writer.Write(data.GetBinaryWithAlignment().align);
writer.WriteBytes(data.GetBinaryWithAlignment().data);
return;
case Byml::Type::Bool:
return writer.Write<u32>(data.GetBool());
Expand Down Expand Up @@ -485,8 +485,8 @@ struct WriteContext {
if (IsContainerType(type)) {
writer.AlignUp(4); // unsure if necessary but a lot of other tools will break from unaligned reads so compatiblity I guess
}
const size_t offset = type != Byml::Type::File ? writer.Tell()
: util::AlignUp(node.data->GetFile().align, writer.Tell() + 8) - 8;
const size_t offset = type != Byml::Type::BinaryWithAlignment ? writer.Tell()
: util::AlignUp(node.data->GetBinaryWithAlignment().align, writer.Tell() + 8) - 8;
writer.RunAt(node.offset_in_container, [&](size_t) { writer.Write<u32>(offset); });
non_inline_node_data.emplace(*node.data, offset);
if (IsContainerType(type))
Expand Down Expand Up @@ -604,8 +604,8 @@ std::vector<u8>& Byml::GetBinary() {
return Get<Type::Binary>();
}

Byml::File& Byml::GetFile() {
return Get<Type::File>();
Byml::BinaryWithAlignment& Byml::GetBinaryWithAlignment() {
return Get<Type::BinaryWithAlignment>();
}

const Byml::Hash32& Byml::GetHash32() const {
Expand All @@ -632,8 +632,8 @@ const std::vector<u8>& Byml::GetBinary() const {
return Get<Type::Binary>();
}

const Byml::File& Byml::GetFile() const {
return Get<Type::File>();
const Byml::BinaryWithAlignment& Byml::GetBinaryWithAlignment() const {
return Get<Type::BinaryWithAlignment>();
}

bool Byml::GetBool() const {
Expand Down
12 changes: 6 additions & 6 deletions src/byml_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,23 @@ Byml ParseYamlNode(const c4::yml::NodeRef& node) {
} else if (yml::RymlGetValTag(node) == "!h32") {
auto hash = Byml::Hash32{};
for (const auto& child : node) {
u32 key = std::stoul(child.key().data(), nullptr, 16);
u32 key = std::stoul(std::string(child.key().data(), child.key().size()), nullptr, 16);
Byml value = ParseYamlNode(child);
hash.emplace(key, std::move(value));
}
return Byml{std::move(hash)};
} else if (yml::RymlGetValTag(node) == "!h64") {
auto hash = Byml::Hash64{};
for (const auto& child : node) {
u64 key = std::stoull(child.key().data(), nullptr, 16);
u64 key = std::stoull(std::string(child.key().data(), child.key().size()), nullptr, 16);
Byml value = ParseYamlNode(child);
hash.emplace(key, std::move(value));
}
return Byml{std::move(hash)};
} else if (yml::RymlGetValTag(node) == "!file") {
const auto& align = node["Alignment"];
const auto& data = node["Data"];
return Byml::File{
return Byml::BinaryWithAlignment{
byml::ScalarToValue(yml::RymlGetValTag(data),
yml::ParseScalar(data, byml::RecognizeTag)).GetBinary(),
byml::ScalarToValue(yml::RymlGetValTag(align),
Expand Down Expand Up @@ -181,13 +181,13 @@ std::string Byml::ToText() const {
[&](const String& v) { emitter.EmitString(v); },
[&](const std::vector<u8>& v) {
const std::string encoded =
absl::Base64Escape(absl::string_view((const char*)v.data(), v.size()));
absl::Base64Escape(absl::string_view(reinterpret_cast<const char*>(v.data()), v.size()));
emitter.EmitString(encoded, "tag:yaml.org,2002:binary");
},
[&](const File& v) {
[&](const BinaryWithAlignment& v) {
yml::LibyamlEmitter::MappingScope scope{emitter, "!file", YAML_BLOCK_MAPPING_STYLE};
const std::string encoded =
absl::Base64Escape(absl::string_view((const char*)v.data.data(), v.data.size()));
absl::Base64Escape(absl::string_view(reinterpret_cast<const char*>(v.data.data()), v.data.size()));
emitter.EmitString("Alignment");
emitter.EmitScalar(absl::StrFormat("0x%08x", v.align), false, false, "!u");
emitter.EmitString("Data");
Expand Down
10 changes: 5 additions & 5 deletions src/include/oead/byml.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Byml {
Hash64,
String,
Binary,
File,
BinaryWithAlignment,
Array,
Dictionary,
Bool,
Expand All @@ -72,10 +72,10 @@ class Byml {
using Array = std::vector<Byml>;
using Dictionary = absl::btree_map<std::string, Byml>;

using File = BinaryAligned;
using BinaryWithAlignment = BinaryAligned;

using Value = util::Variant<Type, Null, std::unique_ptr<Hash32>, std::unique_ptr<Hash64>, std::unique_ptr<String>,
std::unique_ptr<std::vector<u8>>, std::unique_ptr<File>, std::unique_ptr<Array>,
std::unique_ptr<std::vector<u8>>, std::unique_ptr<BinaryWithAlignment>, std::unique_ptr<Array>,
std::unique_ptr<Dictionary>, bool, S32, F32, U32, S64, U64, F64>;

Byml() = default;
Expand Down Expand Up @@ -184,14 +184,14 @@ class Byml {
Array& GetArray();
String& GetString();
std::vector<u8>& GetBinary();
File& GetFile();
BinaryWithAlignment& GetBinaryWithAlignment();
const Hash32& GetHash32() const;
const Hash64& GetHash64() const;
const Dictionary& GetDictionary() const;
const Array& GetArray() const;
const String& GetString() const;
const std::vector<u8>& GetBinary() const;
const File& GetFile() const;
const BinaryWithAlignment& GetBinaryWithAlignment() const;
bool GetBool() const;
s32 GetInt() const;
u32 GetUInt() const;
Expand Down

0 comments on commit 04a5cd2

Please sign in to comment.