diff --git a/header/gMod_FileLoader.h b/header/gMod_FileLoader.h index 5fce0ad..59659e2 100644 --- a/header/gMod_FileLoader.h +++ b/header/gMod_FileLoader.h @@ -17,7 +17,6 @@ struct TpfEntry { class gMod_FileLoader { std::string file_name; - std::ifstream stream; const std::vector TPF_PASSWORD{ 0x73, 0x2A, 0x63, 0x7D, 0x5F, 0x0A, 0xA6, 0xBD, 0x7D, 0x65, 0x7E, 0x67, 0x61, 0x2A, 0x7F, 0x7F, diff --git a/header/uMod_Error.h b/header/uMod_Error.h index 8ea2819..94240e0 100644 --- a/header/uMod_Error.h +++ b/header/uMod_Error.h @@ -2,6 +2,7 @@ // define return values, a value less than zero indicates an error #define RETURN_OK 0 +#define RETURN_EXISTS -70 #define RETURN_FATAL_ERROR -1 #define RETURN_NO_MEMORY -2 diff --git a/source/gMod_FileLoader.cpp b/source/gMod_FileLoader.cpp index 2c44d35..c9d22df 100644 --- a/source/gMod_FileLoader.cpp +++ b/source/gMod_FileLoader.cpp @@ -6,10 +6,6 @@ gMod_FileLoader::gMod_FileLoader(const std::string& fileName) { file_name = std::filesystem::absolute(fileName).string(); - stream = std::ifstream(file_name, std::ios::binary); - if (!stream) { - throw std::runtime_error("Failed to open file: " + file_name); - } } std::vector gMod_FileLoader::Load() @@ -41,6 +37,9 @@ std::vector gMod_FileLoader::GetTpfContents() }); zipArchive->open(); LoadEntries(*zipArchive, entries); + zipArchive->close(); + istream.close(); + delete(zipArchive); } catch (const std::exception& e) { throw std::runtime_error("Failed to open zip file: " + file_name + "\n" + e.what()); @@ -57,6 +56,7 @@ std::vector gMod_FileLoader::GetFileContents() libzippp::ZipArchive zipArchive(file_name); zipArchive.open(); LoadEntries(zipArchive, entries); + zipArchive.close(); } catch (const std::exception& e) { throw std::runtime_error("Failed to open zip file: " + file_name); diff --git a/source/uMod_TextureServer.cpp b/source/uMod_TextureServer.cpp index 5144a22..96a760a 100644 --- a/source/uMod_TextureServer.cpp +++ b/source/uMod_TextureServer.cpp @@ -1,6 +1,8 @@ #include "uMod_Main.h" #include "gMod_FileLoader.h" +long long loadedSize = 0; + uMod_TextureServer::uMod_TextureServer(char* game, char* uModName) { Message("uMod_TextureServer(): %p\n", this); @@ -188,7 +190,9 @@ int uMod_TextureServer::AddFile(char* dataPtr, unsigned int size, MyTypeHash has temp = CurrentMod[i]; break; } // we need to reload it - return RETURN_OK; // we still have added this texture + + + return RETURN_EXISTS; // we still have added this texture } } if (temp == nullptr) // if not found, look through all old textures @@ -202,7 +206,8 @@ int uMod_TextureServer::AddFile(char* dataPtr, unsigned int size, MyTypeHash has if (force) { break; // we must reload it } - return RETURN_OK; // we should not reload it + + return RETURN_EXISTS; // we should not reload it } } } @@ -514,7 +519,14 @@ void uMod_TextureServer::LoadModsFromFile(char* source) if (!entries.empty()) { Message("Initialize: Texture count %zu %s\n", entries.size(), line.c_str()); for (const auto& tpf_entry : entries) { - AddFile(static_cast(tpf_entry.data), static_cast(tpf_entry.size), tpf_entry.crc_hash, false); + loadedSize += tpf_entry.size; + if (AddFile(static_cast(tpf_entry.data), static_cast(tpf_entry.size), tpf_entry.crc_hash, false) == RETURN_EXISTS) { + //Texture is already loaded, so we need to clear our data + loadedSize -= tpf_entry.size; + delete[](tpf_entry.data); + } + + Message("LoadModsFromFile: Loaded %d bytes", loadedSize); } PropagateUpdate(nullptr);