MCPcopy Create free account
hub / github.com/R2Northstar/NorthstarLauncher / LoadFileAsync

Method LoadFileAsync

primedev/mods/modsavefiles.cpp:141–185  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

139
140// Loads a file asynchronously.
141template <ScriptContext context> int SaveFileManager::LoadFileAsync(fs::path file)
142{
143 int handle = ++m_iLastRequestHandle;
144 auto mutex = std::ref(fileMutex);
145 std::thread readThread(
146 [mutex, file, handle]()
147 {
148 try
149 {
150 mutex.get().lock();
151
152 std::ifstream fileStr(file);
153 if (fileStr.fail())
154 {
155 spdlog::error("A file was supposed to be loaded but we can't access it?!");
156
157 g_pSquirrel[context]->AsyncCall("NSHandleLoadResult", handle, false, "");
158 mutex.get().unlock();
159 return;
160 }
161
162 std::stringstream stringStream;
163 stringStream << fileStr.rdbuf();
164
165 g_pSquirrel[context]->AsyncCall("NSHandleLoadResult", handle, true, stringStream.str());
166
167 fileStr.close();
168 mutex.get().unlock();
169 // side-note: this causes a leak?
170 // when a file is added to the map, it's never removed.
171 // no idea how to fix this - because we have no way to check if there are other threads waiting to use this file(?)
172 // tried to use m.try_lock(), but it's unreliable, it seems.
173 }
174 catch (std::exception ex)
175 {
176 spdlog::error("LOAD FAILED!");
177 g_pSquirrel[context]->AsyncCall("NSHandleLoadResult", handle, false, "");
178 mutex.get().unlock();
179 spdlog::error(ex.what());
180 }
181 });
182
183 readThread.detach();
184 return handle;
185}
186
187// Deletes a file asynchronously.
188template <ScriptContext context> void SaveFileManager::DeleteFileAsync(fs::path file)

Callers

nothing calls this directly

Calls 2

getMethod · 0.80
AsyncCallMethod · 0.80

Tested by

no test coverage detected