From 299ab763e4f74a05e6a2bb34eb922bc23b743583 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 22 Apr 2024 02:38:48 +0200 Subject: [PATCH] VpkLib: light cleanup Improve global names, initialize VPKDirHeader_t structure, make inlines for checking if language or target exists. --- r5dev/public/const.h | 18 ++++++++++++++ r5dev/public/localize/ilocalize.h | 13 ++++++++++ r5dev/revpk/revpk.cpp | 19 +++++++++------ r5dev/vpklib/packedstore.cpp | 40 ++++++++++--------------------- r5dev/vpklib/packedstore.h | 25 +++++++++++-------- 5 files changed, 70 insertions(+), 45 deletions(-) diff --git a/r5dev/public/const.h b/r5dev/public/const.h index ebca76a7..63535f79 100644 --- a/r5dev/public/const.h +++ b/r5dev/public/const.h @@ -66,4 +66,22 @@ enum MoveType_t MOVETYPE_ZEROG // ? }; +inline const char* const g_GameDllTargets[] = { + "server", + "client" +}; + +inline bool V_GameTargetExists(const char* const pTarget) +{ + for (size_t i = 0; i < V_ARRAYSIZE(g_GameDllTargets); i++) + { + if (V_strcmp(pTarget, g_GameDllTargets[i]) == NULL) + { + return true; + } + } + + return false; +} + #endif // CONST_H \ No newline at end of file diff --git a/r5dev/public/localize/ilocalize.h b/r5dev/public/localize/ilocalize.h index 2c145557..a696c69a 100644 --- a/r5dev/public/localize/ilocalize.h +++ b/r5dev/public/localize/ilocalize.h @@ -73,4 +73,17 @@ inline const char* const g_LanguageNames[] = { "polish", }; +inline bool V_LocaleExists(const char* const pLocale) +{ + for (size_t i = 0; i < V_ARRAYSIZE(g_LanguageNames); i++) + { + if (V_strcmp(pLocale, g_LanguageNames[i]) == NULL) + { + return true; + } + } + + return false; +} + #endif // LOCALIZE_H diff --git a/r5dev/revpk/revpk.cpp b/r5dev/revpk/revpk.cpp index b6fc6842..5434d36d 100644 --- a/r5dev/revpk/revpk.cpp +++ b/r5dev/revpk/revpk.cpp @@ -13,6 +13,8 @@ #include "windows/console.h" #include "vpklib/packedstore.h" +#include "public/const.h" +#include "localize/ilocalize.h" #include "vstdlib/keyvaluessystem.h" #include "filesystem/filesystem_std.h" @@ -83,8 +85,8 @@ static void ReVPK_Usage() usage.Format( "ReVPK instructions and options:\n" "For packing; run 'revpk %s' with the following parameters:\n" - "\t<%s>\t- locale prefix for the directory tree file\n" - "\t<%s>\t- context scope for the VPK files [\"server\", \"client\"]\n" + "\t<%s>\t- locale prefix for the directory file ( defaults to \"%s\" )\n" + "\t<%s>\t- context scope for the VPK files [\"%s\", \"%s\"]\n" "\t<%s>\t- level name for the VPK files\n" "\t<%s>\t- ( optional ) path to the workspace containing the manifest file\n" "\t<%s>\t- ( optional ) path in which the VPK files will be built\n" @@ -92,12 +94,15 @@ static void ReVPK_Usage() "\t<%s>\t- ( optional ) the level of compression [\"%s\", \"%s\", \"%s\", \"%s\", \"%s\"]\n\n" "For unpacking; run 'revpk %s' with the following parameters:\n" - "\t<%s>\t- path and name of the target directory tree or data block file\n" + "\t<%s>\t- path and name of the target VPK files\n" "\t<%s>\t- ( optional ) path in which the VPK files will be unpacked\n" - "\t<%s>\t- ( optional ) whether to parse the directory tree file name from the data block file name\n", + "\t<%s>\t- ( optional ) whether to parse the directory file name from the pack file name\n", PACK_COMMAND, // Pack parameters: - "locale", "context", "levelName", "workspacePath", "buildPath", + "locale", g_LanguageNames[0], + "context", g_GameDllTargets[0], g_GameDllTargets[1], + + "levelName", "workspacePath", "buildPath", "numThreads", // Num helper threads. -1, LZHAM_MAX_HELPER_THREADS, -1, @@ -106,7 +111,7 @@ static void ReVPK_Usage() "fastest", "faster", "default", "better", "uber", UNPACK_COMMAND,// Unpack parameters: - "fileName", "inputDir", "sanitize" + "fileName", "outPath", "sanitize" ); Warning(eDLL_T::FS, "%s", usage.Get()); @@ -173,7 +178,7 @@ static void ReVPK_Pack(const CCommand& args) // For clients, we need an enable file which the engine uses to determine // whether or not to mount the front-end VPK file. - if (V_strcmp(contextName, DIR_TARGET[EPackedStoreTargets::STORE_TARGET_CLIENT]) == NULL) + if (V_strcmp(contextName, g_GameDllTargets[EPackedStoreTargets::STORE_TARGET_CLIENT]) == NULL) { ReVPK_WriteEnableFile(buildPath); } diff --git a/r5dev/vpklib/packedstore.cpp b/r5dev/vpklib/packedstore.cpp index 29a402cf..3acd2162 100644 --- a/r5dev/vpklib/packedstore.cpp +++ b/r5dev/vpklib/packedstore.cpp @@ -463,7 +463,7 @@ void CPackedStoreBuilder::PackStore(const VPKPair_t& vpkPair, const char* worksp return; } - std::unique_ptr pEntryBuffer(new uint8_t[ENTRY_MAX_LEN]); + std::unique_ptr pEntryBuffer(new uint8_t[VPK_ENTRY_MAX_LEN]); if (!pEntryBuffer) { @@ -587,8 +587,8 @@ void CPackedStoreBuilder::UnpackStore(const VPKDir_t& vpkDir, const char* worksp workspacePath.AppendSlash(); workspacePath.FixSlashes('/'); - std::unique_ptr pDestBuffer(new uint8_t[ENTRY_MAX_LEN]); - std::unique_ptr pSourceBuffer(new uint8_t[ENTRY_MAX_LEN]); + std::unique_ptr pDestBuffer(new uint8_t[VPK_ENTRY_MAX_LEN]); + std::unique_ptr pSourceBuffer(new uint8_t[VPK_ENTRY_MAX_LEN]); if (!pDestBuffer || !pSourceBuffer) { @@ -652,7 +652,7 @@ void CPackedStoreBuilder::UnpackStore(const VPKDir_t& vpkDir, const char* worksp continue; } - size_t nDstLen = ENTRY_MAX_LEN; + size_t nDstLen = VPK_ENTRY_MAX_LEN; assert(fragment.m_nCompressedSize <= nDstLen); if (fragment.m_nCompressedSize > nDstLen) @@ -747,13 +747,13 @@ VPKEntryBlock_t::VPKEntryBlock_t(const uint8_t* pData, size_t nLen, int64_t nOff m_EntryPath.FixSlashes('/'); - size_t nFragmentCount = (nLen + ENTRY_MAX_LEN - 1) / ENTRY_MAX_LEN; + size_t nFragmentCount = (nLen + VPK_ENTRY_MAX_LEN - 1) / VPK_ENTRY_MAX_LEN; size_t nFileSize = nLen; int64_t nCurrentOffset = nOffset; for (size_t i = 0; i < nFragmentCount; i++) // Fragment data into 1 MiB chunks. { - size_t nSize = std::min(ENTRY_MAX_LEN, nFileSize); + size_t nSize = std::min(VPK_ENTRY_MAX_LEN, nFileSize); nFileSize -= nSize; m_Fragments.AddToTail(VPKChunkDescriptor_t(nLoadFlags, nTextureFlags, nCurrentOffset, nSize, nSize)); nCurrentOffset += nSize; @@ -802,15 +802,7 @@ VPKChunkDescriptor_t::VPKChunkDescriptor_t(uint32_t nLoadFlags, uint16_t nTextur //----------------------------------------------------------------------------- VPKPair_t::VPKPair_t(const char* pLocale, const char* pTarget, const char* pLevel, int nPatch) { - bool bFoundLocale = false; - - for (size_t i = 0; i < SDK_ARRAYSIZE(g_LanguageNames); i++) - { - if (V_strcmp(pLocale, g_LanguageNames[i]) == NULL) - { - bFoundLocale = true; - } - } + const bool bFoundLocale = V_LocaleExists(pLocale); if (!bFoundLocale) { @@ -818,20 +810,12 @@ VPKPair_t::VPKPair_t(const char* pLocale, const char* pTarget, const char* pLeve pLocale = g_LanguageNames[0]; } - bool bFoundTarget = false; - - for (size_t i = 0; i < SDK_ARRAYSIZE(DIR_TARGET); i++) - { - if (V_strcmp(pTarget, DIR_TARGET[i]) == NULL) - { - bFoundTarget = true; - } - } + const bool bFoundTarget = V_GameTargetExists(pTarget); if (!bFoundTarget) { - Warning(eDLL_T::FS, "Target '%s' not supported; using default '%s'\n", pTarget, DIR_TARGET[STORE_TARGET_SERVER]); - pTarget = DIR_TARGET[STORE_TARGET_SERVER]; + Warning(eDLL_T::FS, "Target '%s' not supported; using default '%s'\n", pTarget, g_GameDllTargets[STORE_TARGET_SERVER]); + pTarget = g_GameDllTargets[STORE_TARGET_SERVER]; } m_PackName.Format("%s_%s.bsp.pak000_%03d.vpk", pTarget, pLevel, nPatch); @@ -902,9 +886,9 @@ VPKDir_t::VPKDir_t(const CUtlString& dirFilePath, bool bSanitizeName) CUtlString packDirToSearch; packDirToSearch.Append(g_LanguageNames[i]); - for (size_t j = 0; j < SDK_ARRAYSIZE(DIR_TARGET); j++) + for (size_t j = 0; j < SDK_ARRAYSIZE(g_GameDllTargets); j++) { - const char* targetName = DIR_TARGET[j]; + const char* targetName = g_GameDllTargets[j]; if (sanitizedName.Find(targetName) != -1) { diff --git a/r5dev/vpklib/packedstore.h b/r5dev/vpklib/packedstore.h index d065f24a..88e2bcaf 100644 --- a/r5dev/vpklib/packedstore.h +++ b/r5dev/vpklib/packedstore.h @@ -1,6 +1,4 @@ -#ifndef PACKEDSTORE_H -#define PACKEDSTORE_H -/******************************************************************* +/******************************************************************* * ██████╗ ██╗ ██╗ ██╗██████╗ ██╗ ██╗ ██╗ ██╗██████╗ * * ██╔══██╗███║ ██║ ██║██╔══██╗██║ ██╔╝ ██║ ██║██╔══██╗ * * ██████╔╝╚██║ ██║ ██║██████╔╝█████╔╝ ██║ ██║██████╔╝ * @@ -8,6 +6,10 @@ * ██║ ██║ ██║ ╚████╔╝ ██║ ██║ ██╗ ███████╗██║██████╔╝ * * ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝╚═════╝ * *******************************************************************/ +#ifndef PACKEDSTORE_H +#define PACKEDSTORE_H + +#include "public/const.h" #include "public/ipackedstore.h" #include "public/ifilesystem.h" #include "public/tier1/strtools.h" @@ -19,18 +21,12 @@ constexpr unsigned int VPK_HEADER_MARKER = 0x55AA1234; constexpr unsigned int VPK_MAJOR_VERSION = 2; constexpr unsigned int VPK_MINOR_VERSION = 3; constexpr unsigned int VPK_DICT_SIZE = 20; -constexpr int ENTRY_MAX_LEN = 1024 * 1024; +constexpr unsigned int VPK_ENTRY_MAX_LEN = 1024 * 1024; constexpr int PACKFILEPATCH_MAX = 512; constexpr int PACKFILEINDEX_SEP = 0x0; constexpr int PACKFILEINDEX_END = 0xffff; constexpr const char VPK_IGNORE_FILE[] = ".vpkignore"; -static const char* const DIR_TARGET[] -{ - "server", - "client" -}; - //----------------------------------------------------------------------------- // KeyValues structure for the VPK manifest file. This struct gets populated by // the VPK's corresponding manifest file, which ultimately determines how each @@ -131,6 +127,15 @@ struct VPKDirHeader_t uint16_t m_nMinorVersion; // Vpk minor version. uint32_t m_nDirectorySize; // Directory tree size. uint32_t m_nSignatureSize; // Directory signature. + + VPKDirHeader_t() + { + m_nHeaderMarker = 0; + m_nMajorVersion = 0; + m_nMinorVersion = 0; + m_nDirectorySize = 0; + m_nSignatureSize = 0; + } }; //-----------------------------------------------------------------------------