-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9d6316
commit c82921d
Showing
13 changed files
with
403 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ set( | |
"math_utils.h" | ||
"settings.h" | ||
"variant_cast.h" | ||
"vx.h" | ||
) | ||
|
||
set( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
#pragma once | ||
|
||
#include <any> | ||
#include <iostream> | ||
#include <optional> | ||
#include <tuple> | ||
#include <type_traits> | ||
#include <variant> | ||
|
||
// https://github.com/AVasK/vx/blob/main/vx.hpp | ||
|
||
namespace vx { | ||
|
||
// ===== [ try_find ] ===== | ||
namespace detail { | ||
template<typename X, typename... Ts> | ||
struct try_find_impl {}; | ||
|
||
template<typename X, typename T, typename... Ts> | ||
struct try_find_impl<X, T, Ts...> { | ||
static constexpr std::optional<size_t> try_find(size_t index = 0) noexcept { return try_find_impl<X, Ts...>::try_find(index + 1); } | ||
}; | ||
|
||
template<typename T, typename... Ts> | ||
struct try_find_impl<T, T, Ts...> { | ||
static constexpr std::optional<size_t> try_find(size_t index = 0) noexcept { return {index}; } | ||
}; | ||
|
||
template<typename X> | ||
struct try_find_impl<X> { | ||
static constexpr std::optional<size_t> try_find(size_t = 0) noexcept { return {}; } | ||
}; | ||
} // namespace detail | ||
|
||
template<typename X, typename... Ts> | ||
constexpr std::optional<size_t> try_find(size_t index = 0) { | ||
return detail::try_find_impl<X, Ts...>::try_find(index); | ||
} | ||
|
||
// =====[ at ]===== | ||
template<size_t I> | ||
struct at_t : std::in_place_index_t<I> {}; | ||
template<size_t I> | ||
inline constexpr at_t<I> at; | ||
|
||
template<typename T, size_t I> | ||
#if defined __cpp_concepts && __cplusplus >= __cpp_concepts | ||
requires requires(T object) { | ||
{ std::get<I>(object) }; | ||
} | ||
#endif | ||
decltype(auto) operator|(T && v, at_t<I>) { | ||
return std::get<I>(std::forward<T>(v)); | ||
} | ||
|
||
|
||
// =====[ as ]===== | ||
template<typename T> | ||
struct as_t : std::in_place_type_t<T> {}; | ||
template<typename T> | ||
inline constexpr as_t<T> as; | ||
|
||
// generic case : as acts as a static_cast (for non-variant types) | ||
template<typename From, typename To> | ||
constexpr auto operator|(From const& value, as_t<To>) { | ||
return static_cast<To>(value); | ||
} | ||
|
||
// =====[ variant|as ]===== | ||
template<typename... Ts, typename Type> | ||
constexpr decltype(auto) operator|(std::variant<Ts...>& variant, as_t<Type>) { | ||
return std::get<Type>(variant); | ||
} | ||
|
||
template<typename... Ts, typename Type> | ||
constexpr decltype(auto) operator|(std::variant<Ts...> const& variant, as_t<Type>) { | ||
return std::get<Type>(variant); | ||
} | ||
|
||
template<typename... Ts, typename Type> | ||
constexpr decltype(auto) operator|(std::variant<Ts...>&& variant, as_t<Type>) { | ||
return std::get<Type>(std::move(variant)); | ||
} | ||
|
||
// =====[ any|as ]===== | ||
template<typename Type> | ||
constexpr decltype(auto) operator|(std::any & a, as_t<Type>) { | ||
return std::any_cast<Type>(a); | ||
} | ||
|
||
template<typename Type> | ||
constexpr decltype(auto) operator|(std::any const& a, as_t<Type>) { | ||
return std::any_cast<Type>(a); | ||
} | ||
|
||
template<typename Type> | ||
constexpr decltype(auto) operator|(std::any && a, as_t<Type>) { | ||
return std::any_cast<Type>(std::move(a)); | ||
} | ||
|
||
|
||
// =====[ is ]===== | ||
template<typename T> | ||
struct compare {}; | ||
template<typename T> | ||
inline constexpr compare<T> is{}; | ||
|
||
// =====[ variant|is ]===== | ||
template<typename... Ts, typename Type> | ||
#if defined __cpp_concepts && __cplusplus >= __cpp_concepts | ||
requires(try_find<Type, Ts...>() | as<bool>) | ||
#endif | ||
constexpr bool operator|(std::variant<Ts...> const& variant, compare<Type>) { | ||
return std::holds_alternative<Type>(variant); | ||
} | ||
|
||
// =====[ any|is ]===== | ||
//! constexpr just for being futureproof :) | ||
#if defined __cpp_concepts && __cplusplus >= __cpp_concepts | ||
template<typename Type, typename Any> | ||
requires std::same_as<Any, std::any> | ||
#else | ||
template<typename Type, typename Any, typename = std::enable_if_t<std::is_same_v<Any, std::any>>> | ||
#endif | ||
constexpr bool operator|(Any const& a, compare<Type>) { | ||
return a.type() == typeid(Type); | ||
} | ||
|
||
|
||
// =====[ match ]===== | ||
template<typename... Fs> | ||
struct match : Fs... { | ||
using Fs::operator()...; | ||
|
||
// constexpr match(Fs &&... fs) : Fs{fs}... {} | ||
}; | ||
template<class... Ts> | ||
match(Ts...) -> match<Ts...>; | ||
|
||
template<typename... Ts, typename... Fs> | ||
constexpr decltype(auto) operator|(std::variant<Ts...> const& v, match<Fs...> const& match) { | ||
return std::visit(match, v); | ||
} | ||
|
||
template<typename... Ts, typename... Fs> | ||
constexpr decltype(auto) operator|(std::variant<Ts...>& v, match<Fs...> const& match) { | ||
return std::visit(match, v); | ||
} | ||
|
||
// =====[ optional match ]===== | ||
template<typename T, typename... Fs> | ||
#if defined __cpp_concepts && __cplusplus >= __cpp_concepts | ||
requires(std::is_invocable_v<match<Fs...>, T> && std::is_invocable_v<match<Fs...>>) //&& requires(match<Fs...> const& match){ {match()}; }) | ||
#endif | ||
constexpr decltype(auto) operator|(std::optional<T> const& o, match<Fs...> const& match) { | ||
if (o.has_value()) | ||
return match(o.value()); | ||
else | ||
return match(); | ||
} | ||
|
||
} // namespace vx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ enum class SaveMetaSlot { | |
CheckpointGameStats, | ||
SaveFileGameStats, | ||
SeedMetaData, | ||
ArchipelagoData, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <Modloader/modloader.h> | ||
#include <Randomizer/archipelago/archipelago_protocol.h> | ||
#include <nlohmann/json.hpp> | ||
|
||
namespace randomizer::archipelago::messages { | ||
std::optional<ap_server_message_t> parse_server_message(const nlohmann::json& message) { | ||
const auto command = message.at("cmd").get<std::string>(); | ||
|
||
if (command == "Connected") { | ||
return message.get<Connected>(); | ||
} | ||
|
||
if (command == "ConnectionRefused") { | ||
return message.get<ConnectionRefused>(); | ||
} | ||
|
||
if (command == "RoomInfo") { | ||
return message.get<RoomInfo>(); | ||
} | ||
|
||
if (command == "ReceivedItem") { | ||
return message.get<ReceivedItem>(); | ||
} | ||
|
||
modloader::warn("archipelago", std::format("Failed to parse server message: Unknown command {}", command)); | ||
|
||
return std::nullopt; | ||
} | ||
} // namespace randomizer::archipelago::messages |
Oops, something went wrong.