Skip to content

BYML v10 Support - #74

Open
dt-12345 wants to merge 13 commits into
zeldamods:masterfrom
dt-12345:v10
Open

dt-12345 wants to merge 13 commits into
zeldamods:masterfrom
dt-12345:v10

Conversation

@dt-12345

@dt-12345 dt-12345 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Adds support for:

  • Hash32 and Hash64 containers
    • Rename Hash to Dictionary to avoid confusion
  • Relocated string table (needs some testing, I don't know of any official files that use these)
  • Aligned binary values
  • Non-container root nodes (needs some testing, I don't know of any official files that use these)

TODO:

  • Ordered container variants (Hash32, Hash64, Dictionary)
    • This can probably just use tsl::ordered_map
  • Mono-typed array
    • This can probably be a wrapper around std::vector<Byml> (though maybe without exposing references to the underlying elements to enforce the mono-typing?)

Also as side note - the current abseil version fails to build on Windows with compilers that don't define _MSC_VER (MinGW, Windows clang not in MSVC-compatibility mode) because O_NONBLOCK is not defined. This is fixed in upstream by abseil/abseil-cpp@ddbdebc.


This change is Reviewable

@dt-12345

Copy link
Copy Markdown
Contributor Author

I can squash all the commits at the end. I'm not super sure about how to handle the mono-typed arrays. Currently, I'm not enforcing strict typing except when reading/writing. I suppose you could have a wrapper around std::vector that enforces typing when modifying the array, but it's pretty easy to just get a reference to an element and modify it that way (and the wrapper is kind of messy anyways). Another option would be to have a variant of vectors of all the different possible types, but that would make a lot of the code way more complicated and I'm not sure that's desirable. For now, this is the simplest solution and it seems to work. Afaik, no games actually use mono-typed arrays so it's not super important at the end of the day.

@dt-12345
dt-12345 marked this pull request as ready for review November 11, 2026 03:56
@leoetlino

Copy link
Copy Markdown
Member

yeah, I agree there's probably no point in enforcing strict typing other than at read/write time

@leoetlino
leoetlino self-requested a review November 13, 2026 01:09

@leoetlino leoetlino left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leoetlino reviewed 10 files and all commit messages, and made 11 comments.
Reviewable status: all files reviewed, 11 unresolved discussions (waiting on dt-12345).


py/py_byml.cpp line 77 at r1 (raw file):

      return std::invoke(callable, obj, std::forward<Ts>(args)...);
    };
    if (py::isinstance<Byml::Array>(handle)) {

let's not copy and paste the same block too many times. can we try something like

template <typename T, Byml::Type type>
struct Borrowable {};

template <typename... Ts, typename Callable>
static auto BorrowByml(Callable&& callable) {
  return [&, callable](py::handle handle, Ts&&... args) {
    Byml obj;
    const auto invoke = [&] { return std::invoke(callable, obj, std::forward<Ts>(args)...); };

    const auto borrow_and_invoke = [&]<typename T, Byml::Type type, typename... Rest>(
                                       const auto& self, Borrowable<T, type>, Rest... rest) {
      if (!py::isinstance<T>(handle)) {
        if constexpr (sizeof...(Rest) == 0) {
          obj = handle.cast<Byml>();
          return invoke();
        } else {
          return self(self, rest...);
        }
      }

      auto& ref = handle.cast<T&>();
      obj = std::move(ref);
      util::ScopeGuard guard{[&] { ref = std::move(obj.Get<type>()); }};
      return invoke();
    };

    return borrow_and_invoke(borrow_and_invoke,
                             Borrowable<Byml::Array, Byml::Type::Array>{},
                             Borrowable<Byml::MonoTypedArray, Byml::Type::MonoTypedArray>{},
                             Borrowable<Byml::Dictionary, Byml::Type::Dictionary>{},
                             Borrowable<Byml::OrderedDictionary, Byml::Type::OrderedDictionary>{},
                             Borrowable<Byml::Hash32, Byml::Type::Hash32>{},
                             Borrowable<Byml::Hash64, Byml::Type::Hash64>{},
                             Borrowable<Byml::OrderedHash32, Byml::Type::OrderedHash32>{},
                             Borrowable<Byml::OrderedHash64, Byml::Type::OrderedHash64>{});
  };
}

src/byml.cpp line 60 at r1 (raw file):

  Hash32 = 0x20,
  Hash64 = 0x21,
  OrderedHash32 = 0x30,  // Unsupported

these are no longer unsupported are they?


src/byml.cpp line 343 at r1 (raw file):

      const std::vector<u32> indices = ParseIndexOrder(index_offset, size);
      result.reserve(size);
      for (u32 i = 0; i < size; ++i) {

for (auto index : indices)? doesn't look like we're using i other than to get the i-th index


src/byml.cpp line 397 at r1 (raw file):

      const std::vector<u32> indices = ParseIndexOrder(index_offset, size);
      result.reserve(size);
      for (u32 i = 0; i < size; ++i) {

for (auto index : indices)?


src/byml.cpp line 593 at r1 (raw file):

    switch (data.GetType()) {
    case Byml::Type::Hash32: {
      const auto& hash = data.GetHash32();

these are all looking very similar - can we refactor these new cases into a (likely templated) function to deduplicate them?


src/include/oead/byml.h line 56 at r1 (raw file):

    Array,
    MonoTypedArray,
    Dictionary,

afaik Hash is the official name for this data type - or do you know if they've renamed it since?


src/include/oead/byml.h line 72 at r1 (raw file):

  using OrderedHash32 =
      tsl::ordered_map<u32, Byml, absl::Hash<u32>, std::equal_to<u32>,
                       std::allocator<std::pair<u32, Byml>>, std::vector<std::pair<u32, Byml>>>;

any reason we're specifying all these template arguments? do the defaults not work?


src/include/oead/byml.h line 85 at r1 (raw file):

  struct MonoTypedArray {
    MonoTypedArray() : elements(), type(Type::Null) {}

Suggestion:

    MonoTypedArray() = default;

src/include/oead/byml.h line 87 at r1 (raw file):

    MonoTypedArray() : elements(), type(Type::Null) {}

    explicit MonoTypedArray(Type type_) : elements(), type(type_) {}

Suggestion:

    explicit MonoTypedArray(Type type_) : type(type_) {}

src/include/oead/byml.h line 98 at r1 (raw file):

    std::vector<Byml> elements;
    Type type;

Suggestion:

    Type type = Type::Null;

test/test_map_get.py line 64 at r1 (raw file):

        2 : oead.U32(2),
        3 : oead.U32(3),
        4 : oead.U32(4),

can we try adding 10: oead.U32(10) somewhere in the middle to make sure that the items don't get reordered?

@dt-12345 dt-12345 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dt-12345 made 11 comments.
Reviewable status: 2 of 11 files reviewed, 11 unresolved discussions (waiting on leoetlino).


py/py_byml.cpp line 77 at r1 (raw file):

Previously, leoetlino (Léo Lam) wrote…

let's not copy and paste the same block too many times. can we try something like

template <typename T, Byml::Type type>
struct Borrowable {};

template <typename... Ts, typename Callable>
static auto BorrowByml(Callable&& callable) {
  return [&, callable](py::handle handle, Ts&&... args) {
    Byml obj;
    const auto invoke = [&] { return std::invoke(callable, obj, std::forward<Ts>(args)...); };

    const auto borrow_and_invoke = [&]<typename T, Byml::Type type, typename... Rest>(
                                       const auto& self, Borrowable<T, type>, Rest... rest) {
      if (!py::isinstance<T>(handle)) {
        if constexpr (sizeof...(Rest) == 0) {
          obj = handle.cast<Byml>();
          return invoke();
        } else {
          return self(self, rest...);
        }
      }

      auto& ref = handle.cast<T&>();
      obj = std::move(ref);
      util::ScopeGuard guard{[&] { ref = std::move(obj.Get<type>()); }};
      return invoke();
    };

    return borrow_and_invoke(borrow_and_invoke,
                             Borrowable<Byml::Array, Byml::Type::Array>{},
                             Borrowable<Byml::MonoTypedArray, Byml::Type::MonoTypedArray>{},
                             Borrowable<Byml::Dictionary, Byml::Type::Dictionary>{},
                             Borrowable<Byml::OrderedDictionary, Byml::Type::OrderedDictionary>{},
                             Borrowable<Byml::Hash32, Byml::Type::Hash32>{},
                             Borrowable<Byml::Hash64, Byml::Type::Hash64>{},
                             Borrowable<Byml::OrderedHash32, Byml::Type::OrderedHash32>{},
                             Borrowable<Byml::OrderedHash64, Byml::Type::OrderedHash64>{});
  };
}

Done.


src/byml.cpp line 60 at r1 (raw file):

Previously, leoetlino (Léo Lam) wrote…

these are no longer unsupported are they?

Done.


src/byml.cpp line 343 at r1 (raw file):

Previously, leoetlino (Léo Lam) wrote…

for (auto index : indices)? doesn't look like we're using i other than to get the i-th index

Done.


src/byml.cpp line 397 at r1 (raw file):

Previously, leoetlino (Léo Lam) wrote…

for (auto index : indices)?

Done.


src/byml.cpp line 593 at r1 (raw file):

Previously, leoetlino (Léo Lam) wrote…

these are all looking very similar - can we refactor these new cases into a (likely templated) function to deduplicate them?

Done.


src/include/oead/byml.h line 56 at r1 (raw file):

Previously, leoetlino (Léo Lam) wrote…

afaik Hash is the official name for this data type - or do you know if they've renamed it since?

I don't know if we have any official confirmation of them renaming it. I thought it might be a bit clearer to distinguish between the versions with integer keys and string keys, but I can revert it if we prefer to stick to the official naming as much as possible.


src/include/oead/byml.h line 72 at r1 (raw file):

Previously, leoetlino (Léo Lam) wrote…

any reason we're specifying all these template arguments? do the defaults not work?

They do, but I saw we were using std::vector as the underlying container in the aamp classes so I thought I should do the same here.


test/test_map_get.py line 64 at r1 (raw file):

Previously, leoetlino (Léo Lam) wrote…

can we try adding 10: oead.U32(10) somewhere in the middle to make sure that the items don't get reordered?

Done.


src/include/oead/byml.h line 87 at r1 (raw file):

    MonoTypedArray() : elements(), type(Type::Null) {}

    explicit MonoTypedArray(Type type_) : elements(), type(type_) {}

Done.


src/include/oead/byml.h line 85 at r1 (raw file):

  struct MonoTypedArray {
    MonoTypedArray() : elements(), type(Type::Null) {}

Done.


src/include/oead/byml.h line 98 at r1 (raw file):

    std::vector<Byml> elements;
    Type type;

Done.

@leoetlino leoetlino left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leoetlino reviewed 9 files and all commit messages, made 1 comment, and resolved 11 discussions.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on dt-12345).


src/byml.cpp line 629 at r2 (raw file):

        }
        writer.AlignUp(4);
        WriteIndices(indices);

I think I'm a bit confused - why do we need indices? in your old version, we're writing sorted but now we're writing indices? was that a bug?

@leoetlino

Copy link
Copy Markdown
Member

I ran this through AI and it looks like a few bugs sneaked past my review :^)

  1. Writing an ordered hash inside an array or hash crashes
  2. Integer hash keys read back from text all become 0.
  3. An empty Python {} now becomes an integer-keyed hash
  4. A file whose root is a single 64-bit number, double or binary value doesn't read back.
  5. An alignment of 0 kills the process when writing. That includes the Python default AlignedBinary(), which starts at 0.
  6. Ordered containers switch to 4-byte indexes at 4096 entries, but three format write-ups say 2-byte indexes go up to 65535. The fix is 0x1000 → 0x10000.
  7. The PR breaks the C++ Get by key, which already existed in master. Only code that calls it fails to compile.
  8. Tagged YAML maps such as !!map that master loaded are now rejected.
  9. A truncated file can trigger an unchecked read in the new string table code.

a quick verification suggests these are real problems

@dt-12345

Copy link
Copy Markdown
Contributor Author

Thanks, I'll get to fixing these. I am realizing now that the RelocatedStringTable implementation is incorrect as well since the offset only gets added to the offsets in the table, not the offset of the table of offsets. It'd be so much nicer to have actual files to test these features with.

A truncated file can trigger an unchecked read in the new string table code.

Like a crash? Doesn't the reader class check all the reads already? Though related to that, I did notice that the existing Binary node parsing doesn't do any checks on the data size and will happily read oob. I can fix that alongside the AlignedBinary thing.

@dt-12345

Copy link
Copy Markdown
Contributor Author

src/byml.cpp line 629 at r2 (raw file):

Previously, leoetlino (Léo Lam) wrote…

I think I'm a bit confused - why do we need indices? in your old version, we're writing sorted but now we're writing indices? was that a bug?

Yeah, it was wrong before. I think it's good now (third time's the charm). To get index n out of the container, they take the nth index from the array of indices to index the array of container entries.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants