MCPcopy Create free account
hub / github.com/XorTroll/uLaunch / LoadConfig

Function LoadConfig

libs/uCommon/source/ul/cfg/cfg_Config.cpp:264–329  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

262 }
263
264 Config LoadConfig() {
265 ConvertOldConfig();
266
267 Config cfg = {};
268 const auto config_path = os::IsEmuMMC() ? EmuMMCConfigPath : SysMMCConfigPath;
269 const auto cfg_file_size = fs::GetFileSize(config_path);
270
271 auto cfg_file_buf = new u8[cfg_file_size]();
272 UL_ON_SCOPE_EXIT(
273 delete[] cfg_file_buf;
274 );
275
276 if(fs::ReadFile(config_path, cfg_file_buf, cfg_file_size)) {
277 size_t cur_offset = 0;
278 const auto cfg_header = *reinterpret_cast<ConfigHeader*>(cfg_file_buf);
279 if(cfg_header.magic == ConfigHeader::Magic) {
280 cur_offset += sizeof(ConfigHeader);
281 if(cur_offset <= cfg_file_size) {
282 cfg.entries.reserve(cfg_header.entry_count);
283 for(u32 i = 0; i < cfg_header.entry_count; i++) {
284 ConfigEntry ent = {};
285 ent.header = *reinterpret_cast<ConfigEntryHeader*>(cfg_file_buf + cur_offset);
286 cur_offset += sizeof(ConfigEntryHeader);
287 if(cur_offset > cfg_file_size) {
288 break;
289 }
290 switch(ent.header.type) {
291 case ConfigEntryType::Bool: {
292 if(ent.header.size != sizeof(bool)) {
293 return CreateNewAndLoadConfig();
294 }
295 ent.bool_value = *reinterpret_cast<bool*>(cfg_file_buf + cur_offset);
296 break;
297 }
298 case ConfigEntryType::U64: {
299 if(ent.header.size != sizeof(u64)) {
300 return CreateNewAndLoadConfig();
301 }
302 ent.u64_value = *reinterpret_cast<u64*>(cfg_file_buf + cur_offset);
303 break;
304 }
305 case ConfigEntryType::String: {
306 if(ent.header.size == 0) {
307 ent.str_value = "";
308 }
309 else {
310 ent.str_value = std::string(reinterpret_cast<char*>(cfg_file_buf + cur_offset), ent.header.size);
311 }
312 break;
313 }
314 }
315 cur_offset += ent.header.size;
316 if(cur_offset > cfg_file_size) {
317 break;
318 }
319 cfg.entries.push_back(std::move(ent));
320 }
321 if(cur_offset <= cfg_file_size) {

Callers

nothing calls this directly

Calls 5

ConvertOldConfigFunction · 0.85
IsEmuMMCFunction · 0.85
GetFileSizeFunction · 0.85
ReadFileFunction · 0.85
CreateNewAndLoadConfigFunction · 0.85

Tested by

no test coverage detected