Skip to content

Commit

Permalink
Various changes
Browse files Browse the repository at this point in the history
Add conversion functions for Vector2 and Vector3
Prevent if_handler from dying
Remove fmt library, use std instead
Do some sophisticated brackets handling in text processors
  • Loading branch information
MunWolf committed Nov 24, 2023
1 parent d192e4c commit 8fc7320
Show file tree
Hide file tree
Showing 57 changed files with 334 additions and 219 deletions.
1 change: 0 additions & 1 deletion conan_include.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_configure(
REQUIRES
zlib/1.2.13
fmt/9.1.0
magic_enum/0.8.1
nlohmann_json/3.11.2
inih/56
Expand Down
4 changes: 2 additions & 2 deletions projects/Common/csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <iostream>
#include <sstream>

#include <fmt/format.h>
#include <format>

namespace csv {
namespace {
Expand Down Expand Up @@ -128,7 +128,7 @@ namespace csv {
}

str_replace(str, "\"", "\"\"");
return fmt::format("\"{}\"", str);
return std::format("\"{}\"", str);
}

void csv_save_file(CSV& csv) {
Expand Down
36 changes: 34 additions & 2 deletions projects/Common/ext.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#pragma once

#include <fmt/format.h>
#include <algorithm>
#include <array>
#include <cctype>
#include <optional>
#include <span>
#include <sstream>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>

enum class BooleanOperator {
GreaterOrEquals,
Expand Down Expand Up @@ -147,6 +147,38 @@ void split_str(std::string_view str, Container& cont, char delim = ' ') {
}
}

template <class Container>
void split_str(const std::string_view str, Container& cont, const std::span<std::pair<char, char>> brackets, const char delim = ' ') {
std::string part;
int inside_brackets = 0;
std::pair<char, char> last_bracket;
for (const auto c : str) {
if (inside_brackets > 0) {
if (c == last_bracket.first) {
++inside_brackets;
} else if (c == last_bracket.second) {
--inside_brackets;
}
} else {
for (const auto bracket : brackets) {
if (c == bracket.first) {
++inside_brackets;
last_bracket = bracket;
}
}
}

if (inside_brackets == 0 && c == delim) {
cont.push_back(part);
part = "";
} else {
cont.push_back(part);
}
}

cont.push_back(part);
}

std::string_view find_next_unbalanced(std::string_view text, std::string_view start, std::string_view end);

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion projects/Core/animation/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace core::animation {
frame_definition.params = std::optional(mat_params);
}
} catch (std::exception& ex) {
trace(MessageType::Warning, 3, "anim_renderer", fmt::format("failed to read '{}{}' error '{}'", base_path().string(), path, ex.what()));
trace(MessageType::Warning, 3, "anim_renderer", std::format("failed to read '{}{}' error '{}'", base_path().string(), path, ex.what()));
}

return anim;
Expand Down
4 changes: 2 additions & 2 deletions projects/Core/animation/timeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace core::animation {
entry->parse(state, jentry);
frames.push_back(entry);
} else
trace(MessageType::Warning, 3, "timeline", fmt::format("unknown timeline entry in '{}{}'", base_path().string(), path));
trace(MessageType::Warning, 3, "timeline", std::format("unknown timeline entry in '{}{}'", base_path().string(), path));
}
}
}
Expand All @@ -68,7 +68,7 @@ namespace core::animation {
try {
parse_entries(path, state, frames, j);
} catch (...) {
trace(MessageType::Warning, 3, "timeline", fmt::format("failed to parse timeline '{}{}'", base_path().string(), path));
trace(MessageType::Warning, 3, "timeline", std::format("failed to parse timeline '{}{}'", base_path().string(), path));
return nullptr;
}
}
Expand Down
2 changes: 1 addition & 1 deletion projects/Core/api/game/loading_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <Modloader/app/types/UI.h>
#include <Modloader/modloader.h>

#include <fmt/format.h>
#include <format>

using namespace modloader;
using namespace app::classes;
Expand Down
6 changes: 3 additions & 3 deletions projects/Core/api/graphics/textures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ namespace core::api::graphics::textures {
auto shard_icons = types::SpiritShardSettings::get_class()->static_fields->Instance->fields.Icons;
auto icons = il2cpp::invoke<app::SpiritShardIconsCollection_Icons__Boxed>(shard_icons, "GetValue", &icon);
texture = il2cpp::gchandle_new_weak(icons->fields.InventoryIcon, true);
modloader::warn("textures", fmt::format("failed to load texture {} ({}).", texture_path, stbi_failure_reason()));
modloader::warn("textures", std::format("failed to load texture {} ({}).", texture_path, stbi_failure_reason()));
return;
}

Expand All @@ -354,10 +354,10 @@ namespace core::api::graphics::textures {
files[path] = texture.value();
dont_unload_texture(reinterpret_cast<app::Texture*>(texture_ptr));
} else {
modloader::warn("textures", fmt::format("unknown texture protocol used when loading texture '{}'.", type));
modloader::warn("textures", std::format("unknown texture protocol used when loading texture '{}'.", type));
}
} catch (std::exception e) {
modloader::warn("textures", fmt::format("Fatal error fetching texture ({})", e.what()));
modloader::warn("textures", std::format("Fatal error fetching texture ({})", e.what()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions projects/Core/api/messages/text_style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace text_style {

std::unordered_set<std::string> created_styles;
app::TextStyle* create_color_style(std::unordered_set<std::string>& styles, std::string_view text) {
auto hex_style = fmt::format("hex_{}", text);
auto hex_style = std::format("hex_{}", text);
auto it = styles.find(hex_style);
if (it != styles.end() || text.size() != 8) {
return nullptr;
Expand All @@ -92,7 +92,7 @@ namespace text_style {
}

app::TextStyle* create_size_style(std::unordered_set<std::string>& styles, std::string_view text) {
auto size_style = fmt::format("s_{}", text);
auto size_style = std::format("s_{}", text);
auto it = styles.find(size_style);
if (it != styles.end()) {
return nullptr;
Expand Down
4 changes: 2 additions & 2 deletions projects/Core/api/scenes/scene_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace core::api::scenes {
auto scene_manager_scene = ScenesManager::GetFromCurrentScenes_1(scenes_manager, scene_meta);

if (scene_loader_debug_logging) {
console::console_send(fmt::format("{} -> {}", scene_name, static_cast<int>(state)));
console::console_send(std::format("{} -> {}", scene_name, static_cast<int>(state)));
}

SceneLoadEventMetadata event{
Expand Down Expand Up @@ -332,7 +332,7 @@ namespace core::api::scenes {
return;
}

console::console_send(fmt::format("Debug logging {}", scene_loader_debug_logging ? "enabled" : "disabled"));
console::console_send(std::format("Debug logging {}", scene_loader_debug_logging ? "enabled" : "disabled"));
}

auto on_game_ready = modloader::event_bus().register_handler(ModloaderEvent::GameReady, [](auto) {
Expand Down
4 changes: 2 additions & 2 deletions projects/Core/api/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ namespace core::api {
int artificial_host_id = 1;

auto on_game_ready = modloader::event_bus().register_handler(ModloaderEvent::GameReady, [](auto) {
modloader::info("sound", fmt::format("WWise version '{}.{}'", AkSoundEngine::GetMajorMinorVersion(), AkSoundEngine::GetSubminorBuildVersion()));
modloader::info("sound", fmt::format("WWise soundbank version '{}'", AkSoundEngine::get_AK_SOUNDBANK_VERSION()));
modloader::info("sound", std::format("WWise version '{}.{}'", AkSoundEngine::GetMajorMinorVersion(), AkSoundEngine::GetSubminorBuildVersion()));
modloader::info("sound", std::format("WWise soundbank version '{}'", AkSoundEngine::get_AK_SOUNDBANK_VERSION()));

load_bank("WotwRando", modloader::base_path() / "assets/sounds/WotwRando.bnk");
});
Expand Down
20 changes: 10 additions & 10 deletions projects/Core/api/uber_states/uber_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,28 +148,28 @@ namespace core::api::uber_states {
UberState::UberState(int group, int state)
: m_group(static_cast<UberStateGroup>(group)), m_state(state) {
if (VALIDATE_UBER_STATES_ON_CREATION && collection_initialized() && !valid()) {
trace(MessageType::Warning, 2, "uber_state", fmt::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber_state", std::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
}
}

UberState::UberState(app::IUberState* state)
: m_group(static_cast<UberStateGroup>(il2cpp::invoke<app::UberID>(state, "get_GroupID")->fields.m_id)), m_state(il2cpp::invoke<app::UberID>(state, "get_StateID")->fields.m_id) {
if (VALIDATE_UBER_STATES_ON_CREATION && collection_initialized() && !valid()) {
trace(MessageType::Warning, 2, "uber_state", fmt::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber_state", std::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
}
}

UberState::UberState(UberStateGroup group, int state)
: m_group(group), m_state(state) {
if (VALIDATE_UBER_STATES_ON_CREATION && collection_initialized() && !valid()) {
trace(MessageType::Warning, 2, "uber_state", fmt::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber_state", std::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
}
}

UberState::UberState(UberStateGroup group, app::AbilityType__Enum state)
: m_group(group), m_state(static_cast<int>(state)) {
if (VALIDATE_UBER_STATES_ON_CREATION && collection_initialized() && !valid()) {
trace(MessageType::Warning, 2, "uber _state", fmt::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber _state", std::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
}
}

Expand Down Expand Up @@ -209,12 +209,12 @@ namespace core::api::uber_states {
} else {
auto uber_state = ptr();
if (!il2cpp::unity::is_valid(uber_state)) {
trace(MessageType::Warning, 2, "uber_state", fmt::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber_state", std::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
return;
}

if (value != prev && settings::dev_mode()) {
const auto text = fmt::format("uber state ({}, {}) set to {} from {}", static_cast<int>(m_group), m_state, value, prev);
const auto text = std::format("uber state ({}, {}) set to {} from {}", static_cast<int>(m_group), m_state, value, prev);
modloader::trace(MessageType::Info, 3, "uber_state", text);
}

Expand All @@ -238,7 +238,7 @@ namespace core::api::uber_states {
} else if (il2cpp::is_assignable(uber_state, types::SavePedestalUberState::get_class())) {
next::Moon::uberSerializationWisp::SavePedestalUberState::set_IsTeleporterActive(reinterpret_cast<app::SavePedestalUberState*>(uber_state), value > 0.5);
} else {
trace(MessageType::Warning, 2, "uber_state", fmt::format("unable to get value of uber state ({}, {})", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber_state", std::format("unable to get value of uber state ({}, {})", static_cast<int>(m_group), m_state));
return;
}
}
Expand All @@ -262,7 +262,7 @@ namespace core::api::uber_states {
// TODO: Change this into something better then a series of if checks.
auto uber_state = ptr();
if (!il2cpp::unity::is_valid(uber_state)) {
trace(MessageType::Warning, 2, "uber_state", fmt::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber_state", std::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
return 0.0;
} else if (il2cpp::is_assignable(uber_state, types::SerializedBooleanUberState::get_class())) {
return static_cast<double>(SerializedBooleanUberState::get_Value(reinterpret_cast<app::SerializedBooleanUberState*>(uber_state)));
Expand All @@ -284,7 +284,7 @@ namespace core::api::uber_states {
return SavePedestalUberState::get_IsTeleporterActive(reinterpret_cast<app::SavePedestalUberState*>(uber_state)) ? 1.0 : 0.0;
}

trace(MessageType::Warning, 2, "uber_state", fmt::format("unable to get value of uber state ({}, {})", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber_state", std::format("unable to get value of uber state ({}, {})", static_cast<int>(m_group), m_state));
return 0.0;
}

Expand Down Expand Up @@ -334,7 +334,7 @@ namespace core::api::uber_states {
std::string UberState::string_value() const {
auto uber_state = ptr();
if (!il2cpp::unity::is_valid(uber_state)) {
trace(MessageType::Warning, 2, "uber_state", fmt::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
trace(MessageType::Warning, 2, "uber_state", std::format("uber state ({}, {}) doesn't exist", static_cast<int>(m_group), m_state));
return "Unknown";
} else if (
il2cpp::is_assignable(uber_state, types::SerializedBooleanUberState::get_class()) ||
Expand Down
4 changes: 2 additions & 2 deletions projects/Core/api/uber_states/uber_state_condition.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <api/uber_states/uber_state_condition.h>
#include <Modloader/modloader.h>

#include <fmt/format.h>
#include <format>
#include <Common/ext.h>

namespace core::api::uber_states {
Expand Down Expand Up @@ -65,7 +65,7 @@ namespace core::api::uber_states {
auto result = common::parse_operator(state_str);
if (result.has_value()) {
if (result->prefix.empty() || result->suffix.empty()) {
modloader::warn("uber_state_condition", fmt::format("Failed to parse {}: Invalid condition", state_str));
modloader::warn("uber_state_condition", std::format("Failed to parse {}: Invalid condition", state_str));
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions projects/Core/api/uber_states/uber_state_dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ namespace {
console::console_send("start visualizing.");
auto list = uber_state_controller->static_fields->AllStateAppliers;
for (auto i = 0; i < list->fields._size; ++i) {
console::console_send(fmt::format("visualizing applier ({} / {})", i + 1, list->fields._size));
console::console_send(std::format("visualizing applier ({} / {})", i + 1, list->fields._size));
console::console_flush();

auto item = list->fields._items->vector[i];
Expand All @@ -178,7 +178,7 @@ namespace {
auto count = il2cpp::unity::get_scene_count();
console::console_send("start visualizing.");
for (auto i = 0; i < count; ++i) {
console::console_send(fmt::format("visualizing scene ({} / {})", i + 1, count));
console::console_send(std::format("visualizing scene ({} / {})", i + 1, count));
console::console_flush();

auto scene = il2cpp::unity::get_scene_at(i);
Expand Down
6 changes: 3 additions & 3 deletions projects/Core/dev/object_visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ namespace dev::visualize {
if (std::string(klass->namespaze).empty())
return klass->name;
else
return fmt::format("{}.{}", klass->namespaze, klass->name);
return std::format("{}.{}", klass->namespaze, klass->name);
}

std::string get_full_name(void* obj) {
auto cast = reinterpret_cast<Il2CppObject*>(obj);
if (std::string(cast->Il2CppClass.klass->namespaze).empty())
return cast->Il2CppClass.klass->name;
else
return fmt::format("{}.{}", cast->Il2CppClass.klass->namespaze, cast->Il2CppClass.klass->name);
return std::format("{}.{}", cast->Il2CppClass.klass->namespaze, cast->Il2CppClass.klass->name);
}

void indent(Visualizer& visualizer, int pre = 0, int post = 0) {
Expand Down Expand Up @@ -697,7 +697,7 @@ namespace dev::visualize {
indent(visualizer, 0, 1);
visualizer.stream << "scene (" << roots.size() << "):" << visualizer.new_line;
for (auto i = 0; i < roots.size(); ++i) {
console::console_send(fmt::format("root_object ({} / {})", i + 1, roots.size()));
console::console_send(std::format("root_object ({} / {})", i + 1, roots.size()));
console::console_flush();

dev::visualize::visualize_object(visualizer, roots[i], indent_start + 1, depth_start - 1);
Expand Down
2 changes: 1 addition & 1 deletion projects/Core/dev/status_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace core::dev {
// Need to hold all the sync handles so we can check what the worst condition is.
const auto& [format, size, play_sound] = m_config.entries[type];
const messages::MessageInfo info{
.text = fmt::format("<s_{:.3}>{}</>", size, fmt::format(fmt::runtime(format), message)),
.text = std::format("<s_{:.3}>{}</>", size, message),
.duration = duration,
.show_box = false,
.play_sound = play_sound,
Expand Down
2 changes: 1 addition & 1 deletion projects/Core/dev/timing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace dev {
void print_time(std::chrono::time_point<std::chrono::steady_clock> start, std::string_view tag) {
auto now = std::chrono::high_resolution_clock::now();
auto time_span = duration_cast<std::chrono::microseconds>(now - start);
modloader::win::console::console_send(fmt::format("{:.2f} ms {}", time_span.count() / 1000.f, tag));
modloader::win::console::console_send(std::format("{:.2f} ms {}", time_span.count() / 1000.f, tag));
modloader::win::console::console_flush();
}
}
2 changes: 1 addition & 1 deletion projects/Core/dev/timing.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <Core/macros.h>
#include <Modloader/windows_api/console.h>
#include <fmt/format.h>
#include <format>
#include <chrono>

namespace dev {
Expand Down
6 changes: 3 additions & 3 deletions projects/Core/ipc/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace core::ipc {
try {
receive_result = socket->recv(msg);
} catch (const zmq::error_t& e) {
warn("IPC", fmt::format("ZeroMQ recv Error: {} {}", e.num(), e.what()));
warn("IPC", std::format("ZeroMQ recv Error: {} {}", e.num(), e.what()));
continue;
}

Expand Down Expand Up @@ -83,7 +83,7 @@ namespace core::ipc {
try {
socket->send(zmq_message, zmq::send_flags::none);
} catch (const zmq::error_t& e) {
warn("IPC", fmt::format("ZeroMQ send Error: {} {}", e.num(), e.what()));
warn("IPC", std::format("ZeroMQ send Error: {} {}", e.num(), e.what()));
continue;
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ namespace core::ipc {
if (it != handlers.end()) {
it->second(j);
} else {
info("ipc", fmt::format("Received unknown action request: {}", j.dump()));
info("ipc", std::format("Received unknown action request: {}", j.dump()));
}
}
}
Expand Down
Loading

0 comments on commit 8fc7320

Please sign in to comment.