Fix item schema crashes and undefined behaviour

This commit is contained in:
BenCat07 2020-07-05 13:36:12 +02:00
parent 77938ce282
commit 3eae4bd666

View File

@ -15,6 +15,7 @@
#include <settings/Bool.hpp> #include <settings/Bool.hpp>
#include "core/sharedobj.hpp" #include "core/sharedobj.hpp"
#include "filesystem.h"
#include "DetourHook.hpp" #include "DetourHook.hpp"
#include "hack.hpp" #include "hack.hpp"
@ -501,35 +502,41 @@ void generate_schema()
} }
static CatCommand generateschema("schema_generate", "Generate custom schema", generate_schema); static CatCommand generateschema("schema_generate", "Generate custom schema", generate_schema);
void Schema_Reload() bool InitSchema(const char *fileName, const char *pathID, CUtlVector<CUtlString> *pVecErrors /* = NULL */)
{ {
static auto GetItemSchema = reinterpret_cast<void *(*) (void)>(gSignatures.GetClientSignature("55 89 E5 57 56 53 83 EC ? 8B 1D ? ? ? ? 85 DB 89 D8")); static auto GetItemSchema = reinterpret_cast<void *(*) (void)>(gSignatures.GetClientSignature("55 89 E5 57 56 53 83 EC ? 8B 1D ? ? ? ? 85 DB 89 D8"));
static auto BInitTextBuffer = reinterpret_cast<bool (*)(void *, CUtlBuffer &, int)>(gSignatures.GetClientSignature("55 89 E5 57 56 53 8D 9D ? ? ? ? 81 EC ? ? ? ? 8B 7D ? 89 1C 24 ")); static auto BInitTextBuffer = reinterpret_cast<bool (*)(void *, CUtlBuffer &, CUtlVector<CUtlString> *)>(gSignatures.GetClientSignature("55 89 E5 57 56 53 8D 9D ? ? ? ? 81 EC ? ? ? ? 8B 7D ? 89 1C 24 "));
void *schema = (void *) ((unsigned) GetItemSchema() + 0x4); void *schema = (void *) ((uintptr_t) GetItemSchema() + 0x4);
FILE *file = fopen(paths::getDataPath("/items_game.txt").c_str(), "r"); // Read the raw data
if (!file || ferror(file) != 0) CUtlBuffer bufRawData;
bool bReadFileOK = g_IFileSystem->ReadFile(fileName, pathID, bufRawData);
// Log something here if bReadFileOK is false
if (!bReadFileOK)
{ {
logging::Info("Error loading file"); logging::Info(("Failed reading item schema from " + std::string(fileName)).c_str());
if (file) return false;
fclose(file);
return;
} }
// CUtlBuffer else
char *text_buffer = new char[1000 * 1000 * 5]; logging::Info(("Read item schema from " + std::string(fileName)).c_str());
size_t buffer_size = fread(text_buffer, sizeof(char), 1000 * 1000 * 5, file);
CUtlBuffer buf(text_buffer, buffer_size, 9); // Wrap it with a text buffer reader
CUtlBuffer bufText(bufRawData.Base(), bufRawData.TellPut(), CUtlBuffer::READ_ONLY | CUtlBuffer::TEXT_BUFFER);
fclose(file); // Use the standard init path
logging::Info("Loading item schema..."); return BInitTextBuffer(schema, bufText, pVecErrors);
bool ret = BInitTextBuffer(schema, buf, 0xDEADCA7);
logging::Info("Loading %s", ret ? "Successful" : "Unsuccessful");
delete[] text_buffer;
} }
void Schema_Reload()
{
logging::Info("Loading item schema...");
bool ret = InitSchema(paths::getDataPath("/items_game.txt").c_str(), nullptr, nullptr);
logging::Info("Loading %s", ret ? "Successful" : "Unsuccessful");
}
CatCommand schema("schema", "Load custom schema", Schema_Reload); CatCommand schema("schema", "Load custom schema", Schema_Reload);
CatCommand update_gui_color("gui_color_update", "Update the GUI Color", []() { CatCommand update_gui_color("gui_color_update", "Update the GUI Color", []() {