mirror of
https://github.com/geode-sdk/geode.git
synced 2025-04-04 01:01:00 -04:00
remove try blocks
This commit is contained in:
parent
044007099f
commit
4cd08e1ca9
22 changed files with 272 additions and 269 deletions
CMakeLists.txt
loader
include/Geode
cocos/base_nodes
loader
utils
src
loader
platform/android
ui
internal
nodes
utils
test/dependency
|
@ -98,7 +98,7 @@ if (WIN32)
|
|||
endif()
|
||||
|
||||
set(MAT_JSON_AS_INTERFACE ON)
|
||||
CPMAddPackage("gh:geode-sdk/json#49bdff7")
|
||||
CPMAddPackage("gh:geode-sdk/json#51bb562")
|
||||
CPMAddPackage("gh:fmtlib/fmt#10.1.1")
|
||||
CPMAddPackage("gh:gulrak/filesystem#3e5b930")
|
||||
|
||||
|
|
|
@ -946,10 +946,8 @@ public:
|
|||
template<class T>
|
||||
std::optional<T> getAttribute(std::string const& attribute) {
|
||||
if (auto value = this->getAttributeInternal(attribute)) {
|
||||
try {
|
||||
if (value.value().template is<T>()) {
|
||||
return value.value().template as<T>();
|
||||
} catch(...) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
|
|
|
@ -176,11 +176,8 @@ namespace geode {
|
|||
T getSavedValue(std::string_view const key) {
|
||||
auto& saved = this->getSaveContainer();
|
||||
if (saved.contains(key)) {
|
||||
try {
|
||||
// json -> T may fail
|
||||
return saved.get<T>(key);
|
||||
}
|
||||
catch (...) {
|
||||
if (auto value = saved.try_get<T>(key)) {
|
||||
return *value;
|
||||
}
|
||||
}
|
||||
return T();
|
||||
|
@ -190,11 +187,8 @@ namespace geode {
|
|||
T getSavedValue(std::string_view const key, T const& defaultValue) {
|
||||
auto& saved = this->getSaveContainer();
|
||||
if (saved.contains(key)) {
|
||||
try {
|
||||
// json -> T may fail
|
||||
return saved.get<T>(key);
|
||||
}
|
||||
catch (...) {
|
||||
if (auto value = saved.try_get<T>(key)) {
|
||||
return *value;
|
||||
}
|
||||
}
|
||||
saved[key] = defaultValue;
|
||||
|
|
|
@ -4,13 +4,9 @@
|
|||
namespace geode {
|
||||
template<class T>
|
||||
bool GeodeSettingValue<T>::load(matjson::Value const& json) {
|
||||
try {
|
||||
m_value = json.as<ValueType>();
|
||||
return true;
|
||||
}
|
||||
catch (...) {
|
||||
return false;
|
||||
}
|
||||
if (!json.is<ValueType>()) return false;
|
||||
m_value = json.as<ValueType>();
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
|
|
@ -151,12 +151,12 @@ namespace geode {
|
|||
template <class T>
|
||||
JsonMaybeValue& validate(JsonValueValidator<T> validator) {
|
||||
if (this->isError()) return *this;
|
||||
try {
|
||||
if (self().m_json.template is<T>()) {
|
||||
if (!validator(self().m_json.template as<T>())) {
|
||||
this->setError(self().m_hierarchy + ": Invalid value format");
|
||||
}
|
||||
}
|
||||
catch (...) {
|
||||
else {
|
||||
this->setError(
|
||||
self().m_hierarchy + ": Invalid type \"" +
|
||||
std::string(jsonValueTypeToString(self().m_json.type())) + "\""
|
||||
|
@ -193,13 +193,13 @@ namespace geode {
|
|||
this->inferType<A>();
|
||||
if (this->isError()) return *this;
|
||||
|
||||
try {
|
||||
if (self().m_json.template is<A>()) {
|
||||
target = self().m_json.template as<A>();
|
||||
}
|
||||
catch (...) {
|
||||
else {
|
||||
this->setError(
|
||||
self().m_hierarchy + ": Invalid type \"" +
|
||||
jsonValueTypeToString(self().m_json.type()) + "\""
|
||||
std::string(jsonValueTypeToString(self().m_json.type())) + "\""
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -210,23 +210,8 @@ namespace geode {
|
|||
T get() {
|
||||
this->inferType<T>();
|
||||
if (this->isError()) return T();
|
||||
try {
|
||||
|
||||
constexpr auto type = getJsonType<T>();
|
||||
|
||||
if constexpr (type == value_t::Number) {
|
||||
return self().m_json.as_double();
|
||||
} else if constexpr (type == value_t::Bool) {
|
||||
return self().m_json.as_bool();
|
||||
} else if constexpr (type == value_t::String) {
|
||||
return self().m_json.as_string();
|
||||
}
|
||||
}
|
||||
catch (...) {
|
||||
this->setError(
|
||||
self().m_hierarchy + ": Invalid type to get \"" +
|
||||
std::string(jsonValueTypeToString(self().m_json.type())) + "\""
|
||||
);
|
||||
if (self().m_json.template is<T>()) {
|
||||
return self().m_json.template as<T>();
|
||||
}
|
||||
return T();
|
||||
}
|
||||
|
|
|
@ -237,6 +237,10 @@ struct matjson::Serialize<V> {
|
|||
return info.toString();
|
||||
}
|
||||
|
||||
static bool is_json(matjson::Value const& json) {
|
||||
return json.is_string();
|
||||
}
|
||||
|
||||
static V from_json(matjson::Value const& json) {
|
||||
auto ver = V::parse(json.as_string());
|
||||
if (!ver) {
|
||||
|
|
|
@ -16,12 +16,14 @@ template <>
|
|||
struct matjson::Serialize<cocos2d::ccColor3B> {
|
||||
static matjson::Value GEODE_DLL to_json(cocos2d::ccColor3B const& color);
|
||||
static cocos2d::ccColor3B GEODE_DLL from_json(matjson::Value const& color);
|
||||
static bool GEODE_DLL is_json(matjson::Value const& json);
|
||||
};
|
||||
|
||||
template <>
|
||||
struct matjson::Serialize<cocos2d::ccColor4B> {
|
||||
static matjson::Value GEODE_DLL to_json(cocos2d::ccColor4B const& color);
|
||||
static cocos2d::ccColor4B GEODE_DLL from_json(matjson::Value const& color);
|
||||
static bool GEODE_DLL is_json(matjson::Value const& json);
|
||||
};
|
||||
|
||||
// operators for CC geometry
|
||||
|
|
|
@ -18,6 +18,9 @@ struct matjson::Serialize<ghc::filesystem::path> {
|
|||
static ghc::filesystem::path from_json(matjson::Value const& value) {
|
||||
return value.as_string();
|
||||
}
|
||||
static bool is_json(matjson::Value const& value) {
|
||||
return value.is_string();
|
||||
}
|
||||
};
|
||||
|
||||
namespace geode::utils::file {
|
||||
|
@ -28,12 +31,10 @@ namespace geode::utils::file {
|
|||
template <class T>
|
||||
Result<T> readFromJson(ghc::filesystem::path const& file) {
|
||||
GEODE_UNWRAP_INTO(auto json, readJson(file));
|
||||
try {
|
||||
return json.template as<T>();
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
return Err("Error parsing JSON: {}", e.what());
|
||||
if (!json.template is<T>()) {
|
||||
return Err("JSON is not of type {}", typeid(T).name());
|
||||
}
|
||||
return json.template as<T>();
|
||||
}
|
||||
|
||||
GEODE_DLL Result<> writeString(ghc::filesystem::path const& path, std::string const& data);
|
||||
|
@ -41,13 +42,8 @@ namespace geode::utils::file {
|
|||
|
||||
template <class T>
|
||||
Result<> writeToJson(ghc::filesystem::path const& path, T const& data) {
|
||||
try {
|
||||
GEODE_UNWRAP(writeString(path, matjson::Value(data).dump()));
|
||||
return Ok();
|
||||
}
|
||||
catch(std::exception& e) {
|
||||
return Err("Error serializing JSON: {}", e.what());
|
||||
}
|
||||
GEODE_UNWRAP(writeString(path, matjson::Value(data).dump()));
|
||||
return Ok();
|
||||
}
|
||||
|
||||
GEODE_DLL Result<> createDirectory(ghc::filesystem::path const& path);
|
||||
|
|
|
@ -39,13 +39,13 @@ ipc::IPCFilter::IPCFilter(std::string const& modID, std::string const& messageID
|
|||
matjson::Value ipc::processRaw(void* rawHandle, std::string const& buffer) {
|
||||
matjson::Value reply;
|
||||
|
||||
matjson::Value json;
|
||||
try {
|
||||
json = matjson::parse(buffer);
|
||||
} catch (...) {
|
||||
log::warn("Received IPC message that isn't valid JSON");
|
||||
std::string error;
|
||||
auto res = matjson::parse(buffer, error);
|
||||
if (error.size() > 0) {
|
||||
log::warn("Received IPC message that isn't valid JSON: {}", error);
|
||||
return reply;
|
||||
}
|
||||
matjson::Value json = res.value();
|
||||
|
||||
if (!json.contains("mod") || !json["mod"].is_string()) {
|
||||
log::warn("Received IPC message without 'mod' field");
|
||||
|
|
|
@ -206,23 +206,32 @@ static Result<> flattenGithubRepo(ghc::filesystem::path const& dir) {
|
|||
// own folder for that so let's just bring everything from that
|
||||
// folder to ours
|
||||
GEODE_UNWRAP_INTO(auto files, file::readDirectory(dir));
|
||||
try {
|
||||
// only flatten if there is only one file and it's a directory
|
||||
if (files.size() == 1 && ghc::filesystem::is_directory(files[0])) {
|
||||
for (auto& file : ghc::filesystem::directory_iterator(files[0])) {
|
||||
#ifdef GEODE_IS_WINDOWS
|
||||
ghc::filesystem::path const relative = std::filesystem::relative(file.path().wstring(), files[0].wstring()).wstring();
|
||||
#else
|
||||
auto const relative = ghc::filesystem::relative(file, files[0]);
|
||||
#endif
|
||||
ghc::filesystem::rename(
|
||||
file, dir / relative
|
||||
);
|
||||
std::error_code ec;
|
||||
// only flatten if there is only one file and it's a directory
|
||||
if (files.size() == 1 && ghc::filesystem::is_directory(files[0], ec)) {
|
||||
for (auto& file : ghc::filesystem::directory_iterator(files[0])) {
|
||||
#ifdef GEODE_IS_WINDOWS
|
||||
ghc::filesystem::path const relative = std::filesystem::relative(file.path().wstring(), files[0].wstring(), ec).wstring();
|
||||
#else
|
||||
auto const relative = ghc::filesystem::relative(file, files[0], ec);
|
||||
#endif
|
||||
if (ec) {
|
||||
return Err(fmt::format("Unable to get relative path: {}", ec.message()));
|
||||
}
|
||||
ghc::filesystem::rename(
|
||||
file, dir / relative, ec
|
||||
);
|
||||
if (ec) {
|
||||
return Err(fmt::format("Unable to move file: {}", ec.message()));
|
||||
}
|
||||
ghc::filesystem::remove(files[0]);
|
||||
}
|
||||
} catch(std::exception& e) {
|
||||
return Err(e.what());
|
||||
ghc::filesystem::remove(files[0], ec);
|
||||
if (ec) {
|
||||
return Err(fmt::format("Unable to remove directory: {}", ec.message()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
log::warn("Unable to flatten github repo: {}", dir);
|
||||
}
|
||||
return Ok();
|
||||
}
|
||||
|
@ -310,15 +319,14 @@ void Index::Impl::downloadIndex(std::string commitHash) {
|
|||
.then([this, targetFile, commitHash](auto) {
|
||||
auto targetDir = dirs::getIndexDir() / "v0";
|
||||
// delete old unzipped index
|
||||
try {
|
||||
if (ghc::filesystem::exists(targetDir)) {
|
||||
ghc::filesystem::remove_all(targetDir);
|
||||
std::error_code ec;
|
||||
if (ghc::filesystem::exists(targetDir, ec)) {
|
||||
ghc::filesystem::remove_all(targetDir, ec);
|
||||
if (ec) {
|
||||
IndexUpdateEvent(UpdateFailed(fmt::format("Unable to clear cached index: {}", ec.message()))).post();
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(...) {
|
||||
IndexUpdateEvent(UpdateFailed("Unable to clear cached index")).post();
|
||||
return;
|
||||
}
|
||||
|
||||
std::thread([=, this]() {
|
||||
// unzip new index
|
||||
|
@ -722,15 +730,15 @@ void Index::Impl::installNext(size_t index, IndexInstallList const& list) {
|
|||
}
|
||||
|
||||
// Move the temp file
|
||||
try {
|
||||
ghc::filesystem::rename(
|
||||
dirs::getTempDir() / (item->getMetadata().getID() + ".index"),
|
||||
dirs::getModsDir() / (item->getMetadata().getID() + ".geode")
|
||||
);
|
||||
} catch(std::exception& e) {
|
||||
std::error_code ec;
|
||||
ghc::filesystem::rename(
|
||||
dirs::getTempDir() / (item->getMetadata().getID() + ".index"),
|
||||
dirs::getModsDir() / (item->getMetadata().getID() + ".geode"), ec
|
||||
);
|
||||
if (ec) {
|
||||
return postError(fmt::format(
|
||||
"Unable to install {}: {}",
|
||||
item->getMetadata().getID(), e.what()
|
||||
"Unable to move downloaded file for {}: {}",
|
||||
item->getMetadata().getID(), ec.message()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -164,42 +164,42 @@ Result<> Mod::Impl::loadData() {
|
|||
auto settingPath = m_saveDirPath / "settings.json";
|
||||
if (ghc::filesystem::exists(settingPath)) {
|
||||
GEODE_UNWRAP_INTO(auto settingData, utils::file::readString(settingPath));
|
||||
try {
|
||||
// parse settings.json
|
||||
auto json = matjson::parse(settingData);
|
||||
// parse settings.json
|
||||
std::string error;
|
||||
auto res = matjson::parse(settingData, error);
|
||||
if (error.size() > 0) {
|
||||
return Err("Unable to parse settings.json: " + error);
|
||||
}
|
||||
auto json = res.value();
|
||||
|
||||
JsonChecker checker(json);
|
||||
auto root = checker.root("[settings.json]");
|
||||
JsonChecker checker(json);
|
||||
auto root = checker.root("[settings.json]");
|
||||
|
||||
m_savedSettingsData = json;
|
||||
m_savedSettingsData = json;
|
||||
|
||||
for (auto& [key, value] : root.items()) {
|
||||
// check if this is a known setting
|
||||
if (auto setting = this->getSetting(key)) {
|
||||
// load its value
|
||||
if (!setting->load(value.json())) {
|
||||
log::logImpl(
|
||||
Severity::Error,
|
||||
m_self,
|
||||
"{}: Unable to load value for setting \"{}\"",
|
||||
m_metadata.getID(),
|
||||
key
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (auto& [key, value] : root.items()) {
|
||||
// check if this is a known setting
|
||||
if (auto setting = this->getSetting(key)) {
|
||||
// load its value
|
||||
if (!setting->load(value.json())) {
|
||||
log::logImpl(
|
||||
Severity::Warning,
|
||||
Severity::Error,
|
||||
m_self,
|
||||
"Encountered unknown setting \"{}\" while loading "
|
||||
"settings",
|
||||
"{}: Unable to load value for setting \"{}\"",
|
||||
m_metadata.getID(),
|
||||
key
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
return Err(std::string("Unable to parse settings: ") + e.what());
|
||||
else {
|
||||
log::logImpl(
|
||||
Severity::Warning,
|
||||
m_self,
|
||||
"Encountered unknown setting \"{}\" while loading "
|
||||
"settings",
|
||||
key
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -207,12 +207,12 @@ Result<> Mod::Impl::loadData() {
|
|||
auto savedPath = m_saveDirPath / "saved.json";
|
||||
if (ghc::filesystem::exists(savedPath)) {
|
||||
GEODE_UNWRAP_INTO(auto data, utils::file::readString(savedPath));
|
||||
|
||||
try {
|
||||
m_saved = matjson::parse(data);
|
||||
} catch (std::exception& err) {
|
||||
return Err(std::string("Unable to parse saved values: ") + err.what());
|
||||
std::string error;
|
||||
auto res = matjson::parse(data, error);
|
||||
if (error.size() > 0) {
|
||||
return Err("Unable to parse saved values: " + error);
|
||||
}
|
||||
m_saved = res.value();
|
||||
if (!m_saved.is_object()) {
|
||||
log::warn("saved.json was somehow not an object, forcing it to one");
|
||||
m_saved = matjson::Object();
|
||||
|
@ -242,16 +242,15 @@ Result<> Mod::Impl::saveData() {
|
|||
// if some settings weren't provided a custom settings handler (for example,
|
||||
// the mod was not loaded) then make sure to save their previous state in
|
||||
// order to not lose data
|
||||
try {
|
||||
log::debug("Check covered");
|
||||
for (auto& [key, value] : m_savedSettingsData.as_object()) {
|
||||
log::debug("Check if {} is saved", key);
|
||||
if (!coveredSettings.contains(key)) {
|
||||
json[key] = value;
|
||||
}
|
||||
}
|
||||
log::debug("Check covered");
|
||||
if (!m_savedSettingsData.is_object()) {
|
||||
m_savedSettingsData = matjson::Object();
|
||||
}
|
||||
catch (...) {
|
||||
for (auto& [key, value] : m_savedSettingsData.as_object()) {
|
||||
log::debug("Check if {} is saved", key);
|
||||
if (!coveredSettings.contains(key)) {
|
||||
json[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
std::string settingsStr = json.dump();
|
||||
|
@ -704,13 +703,12 @@ bool Mod::Impl::shouldLoad() const {
|
|||
}
|
||||
|
||||
static Result<ModMetadata> getModImplInfo() {
|
||||
std::string err;
|
||||
matjson::Value json;
|
||||
try {
|
||||
json = matjson::parse(LOADER_MOD_JSON);
|
||||
} catch (std::exception& err) {
|
||||
return Err("Unable to parse mod.json: " + std::string(err.what()));
|
||||
std::string error;
|
||||
auto res = matjson::parse(LOADER_MOD_JSON, error);
|
||||
if (error.size() > 0) {
|
||||
return Err("Unable to parse mod.json: " + error);
|
||||
}
|
||||
matjson::Value json = res.value();
|
||||
|
||||
GEODE_UNWRAP_INTO(auto info, ModMetadata::create(json));
|
||||
return Ok(info);
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <about.hpp>
|
||||
#include <matjson.hpp>
|
||||
#include <utility>
|
||||
#include <clocale>
|
||||
|
||||
#include "ModMetadataImpl.hpp"
|
||||
#include "LoaderImpl.hpp"
|
||||
|
@ -72,11 +73,13 @@ Result<ModMetadata> ModMetadata::Impl::createFromSchemaV010(ModJson const& rawJs
|
|||
return Err("[mod.json] could not find GD version for current platform");
|
||||
}
|
||||
if (ver != "*") {
|
||||
try {
|
||||
// assume gd version is always a valid double
|
||||
(void) std::stod(ver);
|
||||
} catch (...) {
|
||||
return Err("[mod.json] has invalid target GD version");
|
||||
double val = 0.0;
|
||||
errno = 0;
|
||||
if (std::setlocale(LC_NUMERIC, "en_US.utf8")) {
|
||||
val = std::strtod(ver.c_str(), nullptr);
|
||||
if (errno == ERANGE) {
|
||||
return Err("[mod.json] has invalid target GD version");
|
||||
}
|
||||
}
|
||||
impl->m_gdVersion = ver;
|
||||
}
|
||||
|
@ -212,20 +215,21 @@ Result<ModMetadata> ModMetadata::Impl::create(ModJson const& json) {
|
|||
Result<ModMetadata> ModMetadata::Impl::createFromFile(ghc::filesystem::path const& path) {
|
||||
GEODE_UNWRAP_INTO(auto read, utils::file::readString(path));
|
||||
|
||||
try {
|
||||
GEODE_UNWRAP_INTO(auto info, ModMetadata::create(matjson::parse(read)));
|
||||
|
||||
auto impl = info.m_impl.get();
|
||||
|
||||
impl->m_path = path;
|
||||
if (path.has_parent_path()) {
|
||||
GEODE_UNWRAP(info.addSpecialFiles(path.parent_path()));
|
||||
}
|
||||
return Ok(info);
|
||||
std::string error;
|
||||
auto res = matjson::parse(read, error);
|
||||
if (error.size() > 0) {
|
||||
return Err(std::string("Unable to parse mod.json: ") + error);
|
||||
}
|
||||
catch (std::exception& err) {
|
||||
return Err(std::string("Unable to parse mod.json: ") + err.what());
|
||||
|
||||
GEODE_UNWRAP_INTO(auto info, ModMetadata::create(res.value()));
|
||||
|
||||
auto impl = info.m_impl.get();
|
||||
|
||||
impl->m_path = path;
|
||||
if (path.has_parent_path()) {
|
||||
GEODE_UNWRAP(info.addSpecialFiles(path.parent_path()));
|
||||
}
|
||||
return Ok(info);
|
||||
}
|
||||
|
||||
Result<ModMetadata> ModMetadata::Impl::createFromGeodeFile(ghc::filesystem::path const& path) {
|
||||
|
@ -244,20 +248,18 @@ Result<ModMetadata> ModMetadata::Impl::createFromGeodeZip(file::Unzip& unzip) {
|
|||
auto jsonData, unzip.extract("mod.json").expect("Unable to read mod.json: {error}")
|
||||
);
|
||||
|
||||
std::string err;
|
||||
ModJson json;
|
||||
try {
|
||||
json = matjson::parse(std::string(jsonData.begin(), jsonData.end()));
|
||||
}
|
||||
catch (std::exception& err) {
|
||||
return Err(err.what());
|
||||
std::string error;
|
||||
auto res = matjson::parse(std::string(jsonData.begin(), jsonData.end()), error);
|
||||
if (error.size() > 0) {
|
||||
return Err(std::string("Unable to parse mod.json: ") + error);
|
||||
}
|
||||
ModJson json = res.value();
|
||||
|
||||
auto res = ModMetadata::create(json);
|
||||
if (!res) {
|
||||
return Err("\"" + unzip.getPath().string() + "\" - " + res.unwrapErr());
|
||||
auto res2 = ModMetadata::create(json);
|
||||
if (!res2) {
|
||||
return Err("\"" + unzip.getPath().string() + "\" - " + res2.unwrapErr());
|
||||
}
|
||||
auto info = res.unwrap();
|
||||
auto info = res2.unwrap();
|
||||
auto impl = info.m_impl.get();
|
||||
impl->m_path = unzip.getPath();
|
||||
|
||||
|
@ -568,6 +570,10 @@ struct matjson::Serialize<geode::ModMetadata::Dependency::Importance> {
|
|||
return geode::ModMetadata::Dependency::Importance::Suggested;
|
||||
throw matjson::JsonException(R"(Expected importance to be "required", "recommended" or "suggested")");
|
||||
}
|
||||
|
||||
static bool is_json(matjson::Value const& value) {
|
||||
return value.is_string();
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
|
@ -587,4 +593,8 @@ struct matjson::Serialize<geode::ModMetadata::Incompatibility::Importance> {
|
|||
return geode::ModMetadata::Incompatibility::Importance::Conflicting;
|
||||
throw matjson::JsonException(R"(Expected importance to be "breaking" or "conflicting")");
|
||||
}
|
||||
|
||||
static bool is_json(matjson::Value const& value) {
|
||||
return value.is_string();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -35,5 +35,6 @@ void console::log(std::string const& msg, Severity severity) {
|
|||
}
|
||||
|
||||
void console::messageBox(char const* title, std::string const& info, Severity severity) {
|
||||
cocos2d::CCMessageBox(info.c_str(), title);
|
||||
console::log(info, severity);
|
||||
// cocos2d::CCMessageBox(info.c_str(), title);
|
||||
}
|
||||
|
|
|
@ -448,28 +448,29 @@ void InvalidGeodeFileCell::onInfo(CCObject*) {
|
|||
|
||||
void InvalidGeodeFileCell::FLAlert_Clicked(FLAlertLayer*, bool btn2) {
|
||||
if (btn2) {
|
||||
try {
|
||||
if (ghc::filesystem::remove(m_info.path)) {
|
||||
std::error_code ec;
|
||||
if (ghc::filesystem::remove(m_info.path, ec)) {
|
||||
FLAlertLayer::create(
|
||||
"File Removed", "Removed <cy>" + m_info.path.string() + "</c>", "OK"
|
||||
)->show();
|
||||
}
|
||||
else {
|
||||
if (ec) {
|
||||
FLAlertLayer::create(
|
||||
"File Removed", "Removed <cy>" + m_info.path.string() + "</c>", "OK"
|
||||
"Unable to Remove File",
|
||||
"Unable to remove <cy>" + m_info.path.string() + "</c>: <cr>" +
|
||||
ec.message() + "</c>",
|
||||
"OK"
|
||||
)->show();
|
||||
}
|
||||
else {
|
||||
FLAlertLayer::create(
|
||||
"Unable to Remove File",
|
||||
"Unable to remove <cy>" + m_info.path.string() + "</c>", "OK"
|
||||
"Unable to remove <cy>" + m_info.path.string() + "</c>",
|
||||
"OK"
|
||||
)->show();
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
FLAlertLayer::create(
|
||||
"Unable to Remove File",
|
||||
"Unable to remove <cy>" + m_info.path.string() + "</c>: <cr>" +
|
||||
std::string(e.what()) + "</c>",
|
||||
"OK"
|
||||
)
|
||||
->show();
|
||||
}
|
||||
if (m_layer) {
|
||||
m_layer->reloadList();
|
||||
}
|
||||
|
|
|
@ -161,18 +161,16 @@ bool SearchFilterPopup::setup(ModListLayer* layer, ModListType type) {
|
|||
}
|
||||
|
||||
void SearchFilterPopup::onTag(CCObject* sender) {
|
||||
try {
|
||||
auto toggle = static_cast<CCMenuItemToggler*>(sender);
|
||||
auto tag = static_cast<CCString*>(toggle->getUserObject())->getCString();
|
||||
if (!toggle->isToggled()) {
|
||||
m_modLayer->getQuery().tags.insert(tag);
|
||||
}
|
||||
else {
|
||||
auto toggle = static_cast<CCMenuItemToggler*>(sender);
|
||||
auto tag = static_cast<CCString*>(toggle->getUserObject())->getCString();
|
||||
if (!toggle->isToggled()) {
|
||||
m_modLayer->getQuery().tags.insert(tag);
|
||||
}
|
||||
else {
|
||||
if (m_modLayer->getQuery().tags.count(tag) > 0) {
|
||||
m_modLayer->getQuery().tags.erase(tag);
|
||||
}
|
||||
}
|
||||
catch (...) {
|
||||
}
|
||||
}
|
||||
|
||||
void SearchFilterPopup::onShowInstalled(CCObject* sender) {
|
||||
|
|
|
@ -7,25 +7,34 @@
|
|||
#include <Geode/binding/CCMenuItemToggler.hpp>
|
||||
#include <Geode/loader/Loader.hpp>
|
||||
#include <Geode/loader/Dirs.hpp>
|
||||
#include <charconv>
|
||||
#include <clocale>
|
||||
|
||||
// Helpers
|
||||
|
||||
template <class Num>
|
||||
Num parseNumForInput(std::string const& str) {
|
||||
try {
|
||||
if constexpr (std::is_same_v<Num, int64_t>) {
|
||||
return std::stoll(str);
|
||||
}
|
||||
else if constexpr (std::is_same_v<Num, double>) {
|
||||
return std::stod(str);
|
||||
}
|
||||
else {
|
||||
static_assert(!std::is_same_v<Num, Num>, "Impl Num for parseNumForInput");
|
||||
if constexpr (std::is_same_v<Num, int64_t>) {
|
||||
int64_t i = 0;
|
||||
auto res = std::from_chars(str.data(), str.data() + str.size(), i);
|
||||
if (res.ec == std::errc()) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
catch (...) {
|
||||
return 0;
|
||||
else if constexpr (std::is_same_v<Num, double>) {
|
||||
double val = 0.0;
|
||||
errno = 0;
|
||||
if (std::setlocale(LC_NUMERIC, "en_US.utf8")) {
|
||||
val = std::strtod(str.c_str(), nullptr);
|
||||
if (errno == 0) {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
static_assert(!std::is_same_v<Num, Num>, "Impl Num for parseNumForInput");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
|
|
@ -4,25 +4,30 @@
|
|||
#include <Geode/binding/SliderThumb.hpp>
|
||||
#include <Geode/ui/ColorPickPopup.hpp>
|
||||
#include <Geode/utils/cocos.hpp>
|
||||
#include <charconv>
|
||||
#include <clocale>
|
||||
|
||||
using namespace geode::prelude;
|
||||
|
||||
static GLubyte parseInt(char const* str) {
|
||||
try {
|
||||
return static_cast<GLubyte>(std::stoi(str));
|
||||
}
|
||||
catch (...) {
|
||||
return 255;
|
||||
int i = 0;
|
||||
auto res = std::from_chars(str, str + strlen(str), i);
|
||||
if (res.ec == std::errc()) {
|
||||
return static_cast<GLubyte>(i);
|
||||
}
|
||||
return 255;
|
||||
}
|
||||
|
||||
static GLubyte parseFloat(char const* str) {
|
||||
try {
|
||||
return static_cast<GLubyte>(std::stof(str) * 255.f);
|
||||
}
|
||||
catch (...) {
|
||||
return 255;
|
||||
float val = 0.0f;
|
||||
errno = 0;
|
||||
if (std::setlocale(LC_NUMERIC, "en_US.utf8")) {
|
||||
val = std::strtof(str, nullptr);
|
||||
if (errno == 0) {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return 255.f;
|
||||
}
|
||||
|
||||
bool ColorPickPopup::setup(ccColor4B const& color, bool isRGBA) {
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <Geode/utils/ranges.hpp>
|
||||
#include <Geode/utils/string.hpp>
|
||||
#include <md4c.h>
|
||||
#include <charconv>
|
||||
|
||||
using namespace geode::prelude;
|
||||
|
||||
|
@ -72,13 +73,12 @@ public:
|
|||
Result<ccColor3B> colorForIdentifier(std::string const& tag) {
|
||||
if (utils::string::contains(tag, ' ')) {
|
||||
auto hexStr = utils::string::split(utils::string::normalize(tag), " ").at(1);
|
||||
try {
|
||||
auto hex = std::stoi(hexStr, nullptr, 16);
|
||||
return Ok(cc3x(hex));
|
||||
}
|
||||
catch (...) {
|
||||
int hex = 0;
|
||||
auto res = std::from_chars(hexStr.data(), hexStr.data() + hexStr.size(), hex, 16);
|
||||
if (res.ec != std::errc()) {
|
||||
return Err("Invalid hex");
|
||||
}
|
||||
return Ok(cc3x(hex));
|
||||
}
|
||||
else {
|
||||
auto colorText = tag.substr(1);
|
||||
|
@ -180,10 +180,9 @@ void MDTextArea::onGDProfile(CCObject* pSender) {
|
|||
auto href = as<CCString*>(as<CCNode*>(pSender)->getUserObject());
|
||||
auto profile = std::string(href->getCString());
|
||||
profile = profile.substr(profile.find(":") + 1);
|
||||
try {
|
||||
ProfilePage::create(std::stoi(profile), false)->show();
|
||||
}
|
||||
catch (...) {
|
||||
int id = 0;
|
||||
auto res = std::from_chars(profile.data(), profile.data() + profile.size(), id);
|
||||
if (res.ec != std::errc()) {
|
||||
FLAlertLayer::create(
|
||||
"Error",
|
||||
"Invalid profile ID: <cr>" + profile +
|
||||
|
@ -192,7 +191,9 @@ void MDTextArea::onGDProfile(CCObject* pSender) {
|
|||
"OK"
|
||||
)
|
||||
->show();
|
||||
return;
|
||||
}
|
||||
ProfilePage::create(id, false)->show();
|
||||
}
|
||||
|
||||
void MDTextArea::FLAlert_Clicked(FLAlertLayer* layer, bool btn) {
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
#include <Geode/modify/LoadingLayer.hpp>
|
||||
#include <Geode/utils/cocos.hpp>
|
||||
#include <matjson.hpp>
|
||||
#include <charconv>
|
||||
|
||||
using namespace geode::prelude;
|
||||
|
||||
bool matjson::Serialize<ccColor3B>::is_json(matjson::Value const& json) {
|
||||
return json.is_array() || json.is_object() || json.is_string();
|
||||
}
|
||||
|
||||
matjson::Value matjson::Serialize<ccColor3B>::to_json(ccColor3B const& color) {
|
||||
return matjson::Object {
|
||||
{ "r", color.r },
|
||||
|
@ -53,6 +58,10 @@ ccColor3B matjson::Serialize<ccColor3B>::from_json(matjson::Value const& json) {
|
|||
return color;
|
||||
}
|
||||
|
||||
bool matjson::Serialize<ccColor4B>::is_json(matjson::Value const& json) {
|
||||
return json.is_array() || json.is_object() || json.is_string();
|
||||
}
|
||||
|
||||
matjson::Value matjson::Serialize<ccColor4B>::to_json(ccColor4B const& color) {
|
||||
return matjson::Object {
|
||||
{ "r", color.r },
|
||||
|
@ -113,10 +122,8 @@ Result<ccColor3B> geode::cocos::cc3bFromHexString(std::string const& hexValue) {
|
|||
return Err("Hex value too large");
|
||||
}
|
||||
int numValue;
|
||||
try {
|
||||
numValue = std::stoi(hexValue, 0, 16);
|
||||
}
|
||||
catch (...) {
|
||||
auto res = std::from_chars(hexValue.data(), hexValue.data() + hexValue.size(), numValue, 16);
|
||||
if (res.ec != std::errc()) {
|
||||
return Err("Invalid hex value");
|
||||
}
|
||||
switch (hexValue.size()) {
|
||||
|
@ -156,10 +163,8 @@ Result<ccColor4B> geode::cocos::cc4bFromHexString(std::string const& hexValue) {
|
|||
return Err("Hex value too large");
|
||||
}
|
||||
int numValue;
|
||||
try {
|
||||
numValue = std::stoi(hexValue, 0, 16);
|
||||
}
|
||||
catch (...) {
|
||||
auto res = std::from_chars(hexValue.data(), hexValue.data() + hexValue.size(), numValue, 16);
|
||||
if (res.ec != std::errc()) {
|
||||
return Err("Invalid hex value");
|
||||
}
|
||||
switch (hexValue.size()) {
|
||||
|
|
|
@ -45,12 +45,11 @@ Result<matjson::Value> utils::file::readJson(ghc::filesystem::path const& path)
|
|||
auto str = utils::file::readString(path);
|
||||
if (!str)
|
||||
return Err(str.unwrapErr());
|
||||
try {
|
||||
return Ok(matjson::parse(str.value()));
|
||||
}
|
||||
catch(std::exception const& e) {
|
||||
return Err("Unable to parse JSON: " + std::string(e.what()));
|
||||
}
|
||||
std::string error;
|
||||
auto res = matjson::parse(str.value(), error);
|
||||
if (error.size())
|
||||
return Err("Unable to parse JSON: " + error);
|
||||
return Ok(res.value());
|
||||
}
|
||||
|
||||
Result<ByteVector> utils::file::readBinary(ghc::filesystem::path const& path) {
|
||||
|
@ -103,31 +102,29 @@ Result<> utils::file::writeBinary(ghc::filesystem::path const& path, ByteVector
|
|||
}
|
||||
|
||||
Result<> utils::file::createDirectory(ghc::filesystem::path const& path) {
|
||||
try {
|
||||
std::error_code ec;
|
||||
#ifdef GEODE_IS_WINDOWS
|
||||
std::filesystem::create_directory(path.wstring());
|
||||
std::filesystem::create_directory(path.wstring(), ec);
|
||||
#else
|
||||
ghc::filesystem::create_directory(path);
|
||||
ghc::filesystem::create_directory(path, ec);
|
||||
#endif
|
||||
return Ok();
|
||||
}
|
||||
catch (...) {
|
||||
if (ec) {
|
||||
return Err("Unable to create directory");
|
||||
}
|
||||
return Ok();
|
||||
}
|
||||
|
||||
Result<> utils::file::createDirectoryAll(ghc::filesystem::path const& path) {
|
||||
try {
|
||||
std::error_code ec;
|
||||
#ifdef GEODE_IS_WINDOWS
|
||||
std::filesystem::create_directories(path.wstring());
|
||||
std::filesystem::create_directories(path.wstring(), ec);
|
||||
#else
|
||||
ghc::filesystem::create_directories(path);
|
||||
ghc::filesystem::create_directories(path, ec);
|
||||
#endif
|
||||
return Ok();
|
||||
}
|
||||
catch (...) {
|
||||
return Err("Unable to create directories");
|
||||
if (ec) {
|
||||
return Err("Unable to create directory");
|
||||
}
|
||||
return Ok();
|
||||
}
|
||||
|
||||
Result<std::vector<ghc::filesystem::path>> utils::file::readDirectory(
|
||||
|
@ -546,7 +543,8 @@ Result<> Unzip::intoDir(
|
|||
GEODE_UNWRAP(unzip.extractAllTo(to));
|
||||
}
|
||||
if (deleteZipAfter) {
|
||||
try { ghc::filesystem::remove(from); } catch(...) {}
|
||||
std::error_code ec;
|
||||
ghc::filesystem::remove(from, ec);
|
||||
}
|
||||
return Ok();
|
||||
}
|
||||
|
|
|
@ -98,14 +98,14 @@ Result<ByteVector> web::fetchBytes(std::string const& url) {
|
|||
}
|
||||
|
||||
Result<matjson::Value> web::fetchJSON(std::string const& url) {
|
||||
std::string res;
|
||||
GEODE_UNWRAP_INTO(res, fetch(url));
|
||||
try {
|
||||
return Ok(matjson::parse(res));
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
return Err(e.what());
|
||||
std::string data;
|
||||
GEODE_UNWRAP_INTO(data, fetch(url));
|
||||
std::string error;
|
||||
auto res = matjson::parse(data, error);
|
||||
if (error.size() > 0) {
|
||||
return Err("Error parsing JSON: " + error);
|
||||
}
|
||||
return Ok(res.value());
|
||||
}
|
||||
|
||||
Result<std::string> web::fetch(std::string const& url) {
|
||||
|
@ -397,11 +397,8 @@ void SentAsyncWebRequest::Impl::doCancel() {
|
|||
if (std::holds_alternative<ghc::filesystem::path>(m_target)) {
|
||||
auto path = std::get<ghc::filesystem::path>(m_target);
|
||||
if (ghc::filesystem::exists(path)) {
|
||||
try {
|
||||
ghc::filesystem::remove(path);
|
||||
}
|
||||
catch (...) {
|
||||
}
|
||||
std::error_code ec;
|
||||
ghc::filesystem::remove(path, ec);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -665,11 +662,11 @@ AsyncWebResult<ByteVector> AsyncWebResponse::bytes() {
|
|||
|
||||
AsyncWebResult<matjson::Value> AsyncWebResponse::json() {
|
||||
return this->as(+[](ByteVector const& bytes) -> Result<matjson::Value> {
|
||||
try {
|
||||
return Ok(matjson::parse(std::string(bytes.begin(), bytes.end())));
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
return Err(std::string(e.what()));
|
||||
std::string error;
|
||||
auto res = matjson::parse(std::string(bytes.begin(), bytes.end()), error);
|
||||
if (error.size() > 0) {
|
||||
return Err("Error parsing JSON: " + error);
|
||||
}
|
||||
return Ok(res.value());
|
||||
});
|
||||
}
|
||||
|
|
|
@ -40,12 +40,9 @@ public:
|
|||
: SettingValue(key, modID), m_icon(icon) {}
|
||||
|
||||
bool load(matjson::Value const& json) override {
|
||||
try {
|
||||
m_icon = static_cast<Icon>(json.as<int>());
|
||||
return true;
|
||||
} catch(...) {
|
||||
return false;
|
||||
}
|
||||
if (!json.is<int>()) return false;
|
||||
m_icon = static_cast<Icon>(json.as<int>());
|
||||
return true;
|
||||
}
|
||||
bool save(matjson::Value& json) const override {
|
||||
json = static_cast<int>(m_icon);
|
||||
|
|
Loading…
Add table
Reference in a new issue