From 3bb39483dc1bcf8107b2eba1a6af49c1b63bc830 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Thu, 27 Jan 2022 17:11:21 +0000 Subject: [PATCH] Use zone allocator instead of static mem allocator --- src/Components/Modules/AntiCheat.cpp | 2 +- .../Modules/AssetInterfaces/IFont_s.cpp | 43 +++++++++++-------- src/Utils/Memory.hpp | 2 + 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/Components/Modules/AntiCheat.cpp b/src/Components/Modules/AntiCheat.cpp index 01dbea5c..af7b678d 100644 --- a/src/Components/Modules/AntiCheat.cpp +++ b/src/Components/Modules/AntiCheat.cpp @@ -634,7 +634,7 @@ namespace Components LUID luid; TOKEN_PRIVILEGES tp = { 0 }; DWORD cb = sizeof(TOKEN_PRIVILEGES); - if (!LookupPrivilegeValueA(nullptr, SE_DEBUG_NAME, &luid)) return; + if (!LookupPrivilegeValueW(nullptr, SE_DEBUG_NAME, &luid)) return; tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; diff --git a/src/Components/Modules/AssetInterfaces/IFont_s.cpp b/src/Components/Modules/AssetInterfaces/IFont_s.cpp index a820011f..edeb572b 100644 --- a/src/Components/Modules/AssetInterfaces/IFont_s.cpp +++ b/src/Components/Modules/AssetInterfaces/IFont_s.cpp @@ -53,15 +53,15 @@ namespace Assets auto& glyph = glyphs[i++]; glyph.letter = ch; - glyph.s0 = x / static_cast(pw); - glyph.s1 = (x + gw) / static_cast(pw); - glyph.t0 = y / static_cast(ph); - glyph.t1 = (y + gh) / static_cast(ph); + glyph.s0 = static_cast(x / pw); + glyph.s1 = static_cast((x + gw) / pw); + glyph.t0 = static_cast(y / ph); + glyph.t1 = static_cast((y + gh) / ph); glyph.pixelWidth = static_cast(gw); glyph.pixelHeight = static_cast(gh); glyph.x0 = static_cast(x0); glyph.y0 = static_cast(y0 + yOffset); - glyph.dx = static_cast(roundf(scale * advance)); + glyph.dx = static_cast(std::roundf(scale * advance)); // Advance to next col x = x + gw + 1; @@ -92,7 +92,7 @@ namespace Assets } } - void IFont_s::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone*) + void IFont_s::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) { Components::FileSystem::File fontDefFile(Utils::String::VA("%s.json", name.data())); Components::FileSystem::File fontFile(Utils::String::VA("%s.ttf", name.data())); @@ -105,11 +105,13 @@ namespace Assets if (!errors.empty()) { Components::Logger::Error("Font define %s is broken: %s.", name.data(), errors.data()); + return; } if (!fontDef.is_object()) { Components::Logger::Error("Font define %s is invaild.", name.data(), errors.data()); + return; } int w = fontDef["textureWidth"].int_value(); @@ -118,22 +120,28 @@ namespace Assets int size = fontDef["size"].int_value(); int yOffset = fontDef["yOffset"].int_value(); - uint8_t* pixels = Utils::Memory::AllocateArray(w * h); + auto* pixels = builder->getAllocator()->allocateArray(w * h); // Setup assets - auto* texName = Utils::Memory::DuplicateString(Utils::String::VA("if_%s", name.data() + 6 /* skip "fonts/" */)); - auto* fontName = Utils::Memory::DuplicateString(name.data()); - auto* glowMaterialName = Utils::Memory::DuplicateString(Utils::String::VA("%s_glow", name.data())); + const auto* texName = builder->getAllocator()->duplicateString(Utils::String::VA("if_%s", name.data() + 6 /* skip "fonts/" */)); + const auto* fontName = builder->getAllocator()->duplicateString(name.data()); + const auto* glowMaterialName = builder->getAllocator()->duplicateString(Utils::String::VA("%s_glow", name.data())); + + auto* image = builder->getAllocator()->allocate(); + std::memcpy(image, Game::DB_FindXAssetHeader(Game::ASSET_TYPE_IMAGE, "gamefonts_pc").image, sizeof(Game::GfxImage)); - auto* image = Utils::Memory::Duplicate(Game::DB_FindXAssetHeader(Game::ASSET_TYPE_IMAGE, "gamefonts_pc").image); image->name = texName; - - auto* material = Utils::Memory::Duplicate(Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc").material); - material->textureTable = Utils::Memory::Duplicate(material->textureTable); + + auto* material = builder->getAllocator()->allocate(); + std::memcpy(material, Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc").material, sizeof(Game::Material)); + + material->textureTable = builder->getAllocator()->allocate(); material->textureTable->u.image = image; material->info.name = fontName; - auto* glowMaterial = Utils::Memory::Duplicate(Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc_glow").material); + auto* glowMaterial = builder->getAllocator()->allocate(); + std::memcpy(glowMaterial, Game::DB_FindXAssetHeader(Game::ASSET_TYPE_MATERIAL, "fonts/gamefonts_pc_glow").material, sizeof(Game::Material)); + glowMaterial->textureTable = material->textureTable; glowMaterial->info.name = glowMaterialName; @@ -161,14 +169,14 @@ namespace Assets charset.push_back(i); } - auto* font = Utils::Memory::Allocate(); + auto* font = builder->getAllocator()->allocate(); font->fontName = fontName; font->pixelHeight = size; font->material = material; font->glowMaterial = glowMaterial; font->glyphCount = charset.size(); - font->glyphs = Utils::Memory::AllocateArray(charset.size()); + font->glyphs = builder->getAllocator()->allocateArray(charset.size()); // Generate glyph data int result = PackFonts(reinterpret_cast(fontFile.getBuffer().data()), charset, font->glyphs, static_cast(size), pixels, w, h, yOffset); @@ -239,7 +247,6 @@ namespace Assets } Utils::IO::WriteFile(Utils::String::VA("userraw\\images\\%s.iwi", texName), outIwi); - Utils::Memory::Free(pixels); } } diff --git a/src/Utils/Memory.hpp b/src/Utils/Memory.hpp index f5eb5e50..88bb7007 100644 --- a/src/Utils/Memory.hpp +++ b/src/Utils/Memory.hpp @@ -81,10 +81,12 @@ namespace Utils this->pool.push_back(data); return data; } + template inline T* allocate() { return this->allocateArray(1); } + template inline T* allocateArray(size_t count = 1) { return static_cast(this->allocate(count * sizeof(T)));