|
1 | 1 | #pragma once |
2 | 2 | #include <Geode/Geode.hpp> |
3 | 3 | #include <matjson.hpp> |
| 4 | +#include <regex> |
4 | 5 |
|
5 | 6 | #include "CustomAlert.hpp" |
6 | 7 | #include "CustomChest.hpp" |
@@ -102,16 +103,37 @@ inline std::map<std::string, int> ITEM_TYPES = { |
102 | 103 | { "spiderAnimationFast", 19 }, |
103 | 104 | }; |
104 | 105 |
|
105 | | -inline matjson::Value readJSON(std::filesystem::path path) { |
| 106 | +inline std::regex errorPattern(R"(>:(\d+):(\d+)$)"); |
| 107 | + |
| 108 | +inline matjson::Value readJSON(std::filesystem::path path, std::string& err) { |
106 | 109 | std::ifstream jsonFile(path); |
107 | 110 | auto parsed = matjson::parse(jsonFile); |
108 | 111 | if (!parsed) { |
109 | | - geode::log::error("{}", parsed.unwrapErr()); |
| 112 | + err = parsed.unwrapErr(); |
| 113 | + geode::log::error("{}", err); |
110 | 114 | return nullptr; |
111 | 115 | } |
112 | 116 | else return parsed.unwrap(); |
113 | 117 | } |
114 | 118 |
|
| 119 | +// Read JSON or display an error popup |
| 120 | +inline matjson::Value getTextboxByID(std::filesystem::path path, std::filesystem::path shortPath, std::string id, std::string typeStr) { |
| 121 | + std::string err; |
| 122 | + auto rawJSON = readJSON(path, err); |
| 123 | + if (rawJSON == nullptr) { |
| 124 | + err = std::regex_replace(err, errorPattern, "line $1, position $2"); |
| 125 | + FLAlertLayer::create("Custom Textboxes", fmt::format("Failed to load <cy>{}</c>\n\nError: <cr>{}</c>", shortPath, err), "OK")->show(); |
| 126 | + return nullptr; |
| 127 | + } |
| 128 | + |
| 129 | + if (!rawJSON.contains(id)) { |
| 130 | + if (Mod::get()->getSettingValue<bool>("missingIDWarn")) FLAlertLayer::create("Custom Textboxes", fmt::format("{} ID not found!\nID: <cy>{}</c> ", typeStr, id), "OK")->show(); |
| 131 | + return nullptr; |
| 132 | + }; |
| 133 | + |
| 134 | + return rawJSON[id]; |
| 135 | +} |
| 136 | + |
115 | 137 | inline std::string getStr(matjson::Value data, char const* key, char const* fallback) { |
116 | 138 | if (!data.contains(key)) return fallback; |
117 | 139 | else return data[key].asString().unwrapOr(fallback); |
@@ -247,6 +269,15 @@ inline IconData getIcon(matjson::Value data, char const* key) { |
247 | 269 | } |
248 | 270 |
|
249 | 271 | inline SoundData getCustomSound(matjson::Value data) { |
| 272 | + if (data.isArray()) { |
| 273 | + auto arr = data.asArray().unwrap(); |
| 274 | + if (arr.size() == 1) return getCustomSound(arr[0]); |
| 275 | + else if (arr.size() > 1) { |
| 276 | + auto val = arr[rand() % arr.size()]; |
| 277 | + if (val != nullptr && !val.isArray()) return getCustomSound(val); |
| 278 | + } |
| 279 | + } |
| 280 | + |
250 | 281 | float vol = 1.0f; |
251 | 282 | float speed = 0.0f; |
252 | 283 | std::string sound; |
|
0 commit comments