Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ jobs:
uses: actions/setup-python@v7
with:
python-version: ${{ matrix.python-version }}
- name: Use GCC 14
if: runner.os == 'Linux'
run: |
echo "CC=gcc-14" >> "$GITHUB_ENV"
echo "CXX=g++-14" >> "$GITHUB_ENV"
- name: Install
run: |
python -m pip install --upgrade pip
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.20)
project(oead CXX)

include(CMakeRC.cmake)
Expand Down Expand Up @@ -52,7 +52,7 @@ add_library(oead
src/yaz0.cpp
)

target_compile_features(oead PUBLIC cxx_std_20)
target_compile_features(oead PUBLIC cxx_std_23)
set_target_properties(oead PROPERTIES CXX_EXTENSIONS OFF)
target_include_directories(oead PUBLIC src/include)
target_include_directories(oead PRIVATE src/)
Expand Down
2 changes: 1 addition & 1 deletion lib/nonstd/nonstd/visit.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ namespace rollbear
return f(get<Is>(std::forward<Vs>(vs))...);
} else {
if (std::tuple(detail::index(vs)...) == std::tuple(Is...)) {
return f(forward_like<Vs>(*get_if<Is>(&vs))...);
return f(detail::forward_like<Vs>(*get_if<Is>(&vs))...);
}
return visit(n, m, std::forward<F>(f), std::forward<Vs>(vs)...);
}
Expand Down
4 changes: 2 additions & 2 deletions py/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.20)
project(oead CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
Expand Down
40 changes: 25 additions & 15 deletions py/py_byml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,37 @@ struct type_caster<oead::Byml> {
} // namespace pybind11::detail

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

/// Wraps a callable to avoid expensive conversions to Byml; instead, we detect
/// whether the first parameter is a Byml::Array or a Byml::Hash and if so we
/// borrow the value and move-construct a temporary Byml in order to avoid copies.
template <typename... Ts, typename Callable>
static auto BorrowByml(Callable&& callable) {
return [&, callable](py::handle handle, Ts&&... args) {
Byml obj;
const auto invoke = [&, callable](auto&& value) {
obj = std::move(value);
return std::invoke(callable, obj, std::forward<Ts>(args)...);
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();
};
if (py::isinstance<Byml::Array>(handle)) {
auto& ref = handle.cast<Byml::Array&>();
util::ScopeGuard guard{[&] { ref = std::move(obj.Get<Byml::Type::Array>()); }};
return invoke(std::move(ref));
}
if (py::isinstance<Byml::Hash>(handle)) {
auto& ref = handle.cast<Byml::Hash&>();
util::ScopeGuard guard{[&] { ref = std::move(obj.Get<Byml::Type::Hash>()); }};
return invoke(std::move(ref));
}
return invoke(handle.cast<Byml>());

return borrow_and_invoke(borrow_and_invoke, Borrowable<Byml::Array, Byml::Type::Array>{},
Borrowable<Byml::Hash, Byml::Type::Hash>{});
};
}

Expand All @@ -97,7 +106,8 @@ void BindByml(py::module& parent) {
m.def("get_float", BorrowByml(&Byml::GetFloat), "data"_a);
m.def("get_int", BorrowByml(&Byml::GetInt), "data"_a);
m.def("get_int64", BorrowByml(&Byml::GetInt64), "data"_a);
m.def("get_string", BorrowByml(py::overload_cast<>(&Byml::GetString, py::const_)), "data"_a);
m.def("get_string", BorrowByml([](const Byml& byml) -> std::string { return byml.GetString(); }),
"data"_a);
m.def("get_uint", BorrowByml(&Byml::GetUInt), "data"_a);
m.def("get_uint64", BorrowByml(&Byml::GetUInt64), "data"_a);

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Issues = "https://github.com/zeldamods/oead/issues"

[tool.scikit-build]
minimum-version = "build-system.requires"
cmake.version = ">=3.15"
cmake.version = ">=3.20"
cmake.source-dir = "py"
install.components = ["python"]
wheel.packages = ["py/oead-stubs"]
Expand Down
4 changes: 2 additions & 2 deletions readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ Building from source

Building oead from source requires:

* CMake 3.15+
* A compiler that supports C++20
* CMake 3.20+
* A compiler that supports C++23
* Everything needed to build libyaml

First, clone the repository then enter the oead directory and run ``git submodule update --init --recursive``.
Expand Down
37 changes: 3 additions & 34 deletions src/byml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <absl/hash/hash.h>
#include <algorithm>
#include <array>
#include <bit>
#include <cstring>
#include <map>
#include <stdexcept>
Expand Down Expand Up @@ -202,15 +203,15 @@ class Parser {
case NodeType::Int:
return S32(*raw);
case NodeType::Float:
return F32(util::BitCast<f32>(*raw));
return F32(std::bit_cast<f32>(*raw));
case NodeType::UInt:
return U32(*raw);
case NodeType::Int64:
return S64(read_long_value());
case NodeType::UInt64:
return U64(read_long_value());
case NodeType::Double:
return F64(util::BitCast<f64>(read_long_value()));
return F64(std::bit_cast<f64>(read_long_value()));
case NodeType::Null:
return Byml::Null();
default:
Expand Down Expand Up @@ -484,38 +485,6 @@ std::vector<u8> Byml::ToBinary(bool big_endian, int version) const {
return ctx.writer.Finalize();
}

Byml::Hash& Byml::GetHash() {
return Get<Type::Hash>();
}

Byml::Array& Byml::GetArray() {
return Get<Type::Array>();
}

Byml::String& Byml::GetString() {
return Get<Type::String>();
}

std::vector<u8>& Byml::GetBinary() {
return Get<Type::Binary>();
}

const Byml::Hash& Byml::GetHash() const {
return Get<Type::Hash>();
}

const Byml::Array& Byml::GetArray() const {
return Get<Type::Array>();
}

const Byml::String& Byml::GetString() const {
return Get<Type::String>();
}

const std::vector<u8>& Byml::GetBinary() const {
return Get<Type::Binary>();
}

bool Byml::GetBool() const {
return Get<Type::Bool>();
}
Expand Down
11 changes: 3 additions & 8 deletions src/include/oead/aamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,12 @@ class Parameter {

Type GetType() const { return m_value.GetType(); }
template <Type type>
const auto& Get() const {
return m_value.Get<type>();
}
template <Type type>
auto& Get() {
return m_value.Get<type>();
auto& Get(this auto&& self) {
return self.m_value.template Get<type>();
}
/// Get the value as a string view. Throws a TypeError if the parameter is not a string.
std::string_view GetStringView() const;
auto& GetVariant() { return m_value; }
const auto& GetVariant() const { return m_value; }
auto& GetVariant(this auto&& self) { return self.m_value; }

private:
Value m_value;
Expand Down
42 changes: 23 additions & 19 deletions src/include/oead/audio/element_type.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <utility>

namespace oead::audio {
struct ElementTypeCategory {
enum {
Expand Down Expand Up @@ -77,20 +79,20 @@ enum class ElementType {
General_ByteStream = ElementTypeCategory::General,
General_String,

SoundArchiveFile_StringBlock = static_cast<int>(ElementTypeCategory::SoundArchiveFile::Blocks),
SoundArchiveFile_StringBlock = std::to_underlying(ElementTypeCategory::SoundArchiveFile::Blocks),
SoundArchiveFile_InfoBlock,
SoundArchiveFile_FileBlock,

SoundArchiveFile_SoundInfoSection =
static_cast<int>(ElementTypeCategory::SoundArchiveFile::InfoSections),
std::to_underlying(ElementTypeCategory::SoundArchiveFile::InfoSections),
SoundArchiveFile_BankInfoSection,
SoundArchiveFile_PlayerInfoSection,
SoundArchiveFile_WaveArchiveInfoSection,
SoundArchiveFile_SoundGroupInfoSection,
SoundArchiveFile_GroupInfoSection,
SoundArchiveFile_FileInfoSection,

SoundArchiveFile_SoundInfo = static_cast<int>(ElementTypeCategory::SoundArchiveFile::ItemInfos),
SoundArchiveFile_SoundInfo = std::to_underlying(ElementTypeCategory::SoundArchiveFile::ItemInfos),
SoundArchiveFile_StreamSoundInfo,
SoundArchiveFile_WaveSoundInfo,
SoundArchiveFile_SequenceSoundInfo,
Expand All @@ -108,58 +110,60 @@ enum class ElementType {
SoundArchiveFile_SendInfo,
SoundArchiveFile_StreamSoundExtensionInfo,

SoundArchiveFile_StringTable = static_cast<int>(ElementTypeCategory::SoundArchiveFile::General),
SoundArchiveFile_StringTable = std::to_underlying(ElementTypeCategory::SoundArchiveFile::General),
SoundArchiveFile_PatriciaTree,

StreamSoundFile_InfoBlock = static_cast<int>(ElementTypeCategory::StreamSoundFile::Blocks),
StreamSoundFile_InfoBlock = std::to_underlying(ElementTypeCategory::StreamSoundFile::Blocks),
StreamSoundFile_SeekBlock,
StreamSoundFile_DataBlock,
StreamSoundFile_RegionBlock,
StreamSoundFile_PrefetchDataBlock,

StreamSoundFile_StreamSoundInfo =
static_cast<int>(ElementTypeCategory::StreamSoundFile::ItemInfos),
std::to_underlying(ElementTypeCategory::StreamSoundFile::ItemInfos),
StreamSoundFile_TrackInfo,
StreamSoundFile_ChannelInfo,

WaveSoundFile_WaveSoundMetaData = static_cast<int>(ElementTypeCategory::WaveSoundFile::ItemInfos),
WaveSoundFile_WaveSoundMetaData =
std::to_underlying(ElementTypeCategory::WaveSoundFile::ItemInfos),
WaveSoundFile_WaveSoundInfo,
WaveSoundFile_NoteInfo,
WaveSoundFile_TrackInfo,
WaveSoundFile_NoteEvent,

SequenceSoundFile_DataBlock = static_cast<int>(ElementTypeCategory::SequenceSoundFile::Blocks),
SequenceSoundFile_DataBlock = std::to_underlying(ElementTypeCategory::SequenceSoundFile::Blocks),
SequenceSoundFile_LabelBlock,

SequenceSoundFile_LabelInfo = static_cast<int>(ElementTypeCategory::SequenceSoundFile::ItemInfos),
SequenceSoundFile_LabelInfo =
std::to_underlying(ElementTypeCategory::SequenceSoundFile::ItemInfos),

BankFile_InfoBlock = static_cast<int>(ElementTypeCategory::BankFile::Blocks),
BankFile_InfoBlock = std::to_underlying(ElementTypeCategory::BankFile::Blocks),

BankFile_InstrumentInfo = static_cast<int>(ElementTypeCategory::BankFile::Items),
BankFile_InstrumentInfo = std::to_underlying(ElementTypeCategory::BankFile::Items),
BankFile_KeyRegionInfo,
BankFile_VelocityRegionInfo,
BankFile_NullInfo,

BankFile_DirectReferenceTable = static_cast<int>(ElementTypeCategory::BankFile::ItemTables),
BankFile_DirectReferenceTable = std::to_underlying(ElementTypeCategory::BankFile::ItemTables),
BankFile_RangeReferenceTable,
BankFile_IndexReferenceTable,

WaveArchiveFile_InfoBlock = static_cast<int>(ElementTypeCategory::WaveArchiveFile::Blocks),
WaveArchiveFile_InfoBlock = std::to_underlying(ElementTypeCategory::WaveArchiveFile::Blocks),

WaveFile_InfoBlock = static_cast<int>(ElementTypeCategory::WaveFile::Blocks),
WaveFile_InfoBlock = std::to_underlying(ElementTypeCategory::WaveFile::Blocks),
WaveFile_DataBlock,

WaveFile_ChannelInfo = static_cast<int>(ElementTypeCategory::WaveFile::ItemInfos),
WaveFile_ChannelInfo = std::to_underlying(ElementTypeCategory::WaveFile::ItemInfos),

GroupFile_InfoBlock = static_cast<int>(ElementTypeCategory::GroupFile::Blocks),
GroupFile_InfoBlock = std::to_underlying(ElementTypeCategory::GroupFile::Blocks),
GroupFile_FileBlock,
GroupFile_InfoExBlock,

GroupFile_GroupItemInfo = static_cast<int>(ElementTypeCategory::GroupFile::ItemInfos),
GroupFile_GroupItemInfo = std::to_underlying(ElementTypeCategory::GroupFile::ItemInfos),
GroupFile_GroupItemInfoEx,

AnimSoundFile_DataBlock = static_cast<int>(ElementTypeCategory::AnimSoundFile::Blocks),
AnimSoundFile_DataBlock = std::to_underlying(ElementTypeCategory::AnimSoundFile::Blocks),

AnimSoundFile_EventInfo = static_cast<int>(ElementTypeCategory::AnimSoundFile::Items),
AnimSoundFile_EventInfo = std::to_underlying(ElementTypeCategory::AnimSoundFile::Items),
};
} // namespace oead::audio
45 changes: 16 additions & 29 deletions src/include/oead/byml.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,10 @@ class Byml {

Type GetType() const { return m_value.GetType(); }
template <Type type>
const auto& Get() const {
return m_value.Get<type>();
auto& Get(this auto&& self) {
return self.m_value.template Get<type>();
}
template <Type type>
auto& Get() {
return m_value.Get<type>();
}
auto& GetVariant() { return m_value; }
const auto& GetVariant() const { return m_value; }
auto& GetVariant(this auto&& self) { return self.m_value; }

/// Get an item from a map/hash.
template <typename T>
Expand All @@ -107,19 +102,15 @@ class Byml {
Storage m_storage;
};

template <Type Type, typename T>
Reference<const T> Get(std::string_view key, T default_) const {
auto it = GetHash().find(key);
if (it == GetHash().end())
return default_;
return std::cref(it->second.Get<Type>());
}
template <Type Type, typename T>
Reference<T> Get(std::string_view key, T default_) {
auto it = GetHash().find(key);
if (it == GetHash().end())
return default_;
return std::ref(it->second.Get<Type>());
template <Type type, typename T>
auto Get(this auto&& self, std::string_view key, T default_) {
using Value = std::remove_reference_t<decltype(self.template Get<type>())>;

auto& hash = self.GetHash();
const auto it = hash.find(key);
if (it == hash.end())
return Reference<Value>(std::move(default_));
return Reference<Value>(std::ref(it->second.template Get<type>()));
}

/// Load a document from binary data.
Expand All @@ -138,14 +129,10 @@ class Byml {
// Some of them will perform type conversions automatically.
// If value types are incorrect, an exception is thrown.

Hash& GetHash();
Array& GetArray();
String& GetString();
std::vector<u8>& GetBinary();
const Hash& GetHash() const;
const Array& GetArray() const;
const String& GetString() const;
const std::vector<u8>& GetBinary() const;
auto& GetHash(this auto&& self) { return self.template Get<Type::Hash>(); }
auto& GetArray(this auto&& self) { return self.template Get<Type::Array>(); }
auto& GetString(this auto&& self) { return self.template Get<Type::String>(); }
auto& GetBinary(this auto&& self) { return self.template Get<Type::Binary>(); }
bool GetBool() const;
s32 GetInt() const;
u32 GetUInt() const;
Expand Down
Loading
Loading