Use filestreams instead of c-lib

This commit is contained in:
Lion Kortlepel
2021-09-20 17:05:28 +02:00
parent d7a4322313
commit 3626f4108e
+6 -15
View File
@@ -28,28 +28,19 @@ TLuaPlugin::TLuaPlugin(TLuaEngine& Engine, const TLuaPluginConfig& Config, const
}); });
std::vector<std::pair<fs::path, std::shared_ptr<TLuaResult>>> ResultsToCheck; std::vector<std::pair<fs::path, std::shared_ptr<TLuaResult>>> ResultsToCheck;
for (const auto& Entry : Entries) { for (const auto& Entry : Entries) {
// read in entire file // read in entire file
#if defined(WIN32) try {
std::FILE* File = _wfopen(reinterpret_cast<const wchar_t*>(Entry.c_str()), "r"); std::ifstream FileStream(Entry.string());
#else
std::FILE* File = std::fopen(reinterpret_cast<const char*>(Entry.c_str()), "r");
#endif
if (File) {
auto Size = std::filesystem::file_size(Entry); auto Size = std::filesystem::file_size(Entry);
auto Contents = std::make_shared<std::string>(); auto Contents = std::make_shared<std::string>();
Contents->resize(Size); Contents->resize(Size);
auto NRead = std::fread(Contents->data(), 1, Contents->size(), File); FileStream.rdbuf()->sgetn(Contents->data(), Contents->size());
if (NRead == Contents->size()) {
mFileContents[fs::relative(Entry).string()] = Contents; mFileContents[fs::relative(Entry).string()] = Contents;
// Execute first time // Execute first time
auto Result = mEngine.EnqueueScript(mConfig.StateId, TLuaChunk(Contents, Entry.string(), MainFolder.string())); auto Result = mEngine.EnqueueScript(mConfig.StateId, TLuaChunk(Contents, Entry.string(), MainFolder.string()));
ResultsToCheck.emplace_back(Entry.string(), std::move(Result)); ResultsToCheck.emplace_back(Entry.string(), std::move(Result));
} else { } catch (const std::exception& e) {
beammp_error("Error while reading script file \"" + Entry.string() + "\". Did the file change while reading?"); beammp_error("Error loading file \"" + Entry.string() + "\": " + e.what());
}
std::fclose(File);
} else {
beammp_error("Could not read script file \"" + Entry.string() + "\": " + std::strerror(errno));
} }
} }
for (auto& Result : ResultsToCheck) { for (auto& Result : ResultsToCheck) {