MCPcopy Create free account
hub / github.com/TranslucentTB/TranslucentTB / LoadFromFile

Method LoadFromFile

TranslucentTB/managers/configmanager.cpp:124–166  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

122}
123
124bool ConfigManager::LoadFromFile(FILE *f)
125{
126 static constexpr std::wstring_view DESERIALIZE_FAILED = L"Failed to deserialize JSON document";
127
128 char buffer[1024];
129 rj::FileReadStream filestream(f, buffer, std::size(buffer));
130
131 rj::AutoUTFInputStream<uint32_t, rj::FileReadStream> in(filestream);
132
133 rj::GenericDocument<rj::UTF16LE<>> doc;
134 if (const rj::ParseResult result = doc.ParseStream<rj::kParseCommentsFlag | rj::kParseTrailingCommasFlag, rj::AutoUTF<uint32_t>>(in))
135 {
136 // remove the schema key to avoid a false unknown key warning
137 doc.RemoveMember(rjh::StringViewToValue(SCHEMA_KEY));
138
139 try
140 {
141 // load the defaults before deserializing to not reuse previous settings
142 // in case some keys are missing from the file
143 m_Config = { };
144 m_Config.Deserialize(doc, [](std::wstring_view unknownKey)
145 {
146 if (Error::ShouldLog<spdlog::level::info>())
147 {
148 MessagePrint(spdlog::level::info, std::format(L"Unknown key found in JSON: {}", unknownKey));
149 }
150 });
151
152 // everything went fine, we can return!
153 return true;
154 }
155 HelperDeserializationErrorCatch(spdlog::level::err, DESERIALIZE_FAILED)
156 StdSystemErrorCatch(spdlog::level::err, DESERIALIZE_FAILED);
157 }
158 else if (result.Code() != rj::kParseErrorDocumentEmpty)
159 {
160 ParseErrorCodeHandle(result.Code(), spdlog::level::err, DESERIALIZE_FAILED);
161 }
162
163 // parsing failed, use defaults
164 m_Config = { };
165 return false;
166}
167
168bool ConfigManager::Load(bool firstLoad)
169{

Callers

nothing calls this directly

Calls 2

StringViewToValueFunction · 0.85
DeserializeMethod · 0.45

Tested by

no test coverage detected