VpkLib: light cleanup

Improve global names, initialize VPKDirHeader_t structure, make inlines for checking if language or target exists.
This commit is contained in:
Kawe Mazidjatari 2024-04-22 02:38:48 +02:00
parent aa6a5dcb6e
commit 299ab763e4
5 changed files with 70 additions and 45 deletions

View File

@ -66,4 +66,22 @@ enum MoveType_t
MOVETYPE_ZEROG // ? 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 #endif // CONST_H

View File

@ -73,4 +73,17 @@ inline const char* const g_LanguageNames[] = {
"polish", "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 #endif // LOCALIZE_H

View File

@ -13,6 +13,8 @@
#include "windows/console.h" #include "windows/console.h"
#include "vpklib/packedstore.h" #include "vpklib/packedstore.h"
#include "public/const.h"
#include "localize/ilocalize.h"
#include "vstdlib/keyvaluessystem.h" #include "vstdlib/keyvaluessystem.h"
#include "filesystem/filesystem_std.h" #include "filesystem/filesystem_std.h"
@ -83,8 +85,8 @@ static void ReVPK_Usage()
usage.Format( usage.Format(
"ReVPK instructions and options:\n" "ReVPK instructions and options:\n"
"For packing; run 'revpk %s' with the following parameters:\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- locale prefix for the directory file ( defaults to \"%s\" )\n"
"\t<%s>\t- context scope for the VPK files [\"server\", \"client\"]\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- 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 to the workspace containing the manifest file\n"
"\t<%s>\t- ( optional ) path in which the VPK files will be built\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" "\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" "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 ) 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: 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. "numThreads", // Num helper threads.
-1, LZHAM_MAX_HELPER_THREADS, -1, -1, LZHAM_MAX_HELPER_THREADS, -1,
@ -106,7 +111,7 @@ static void ReVPK_Usage()
"fastest", "faster", "default", "better", "uber", "fastest", "faster", "default", "better", "uber",
UNPACK_COMMAND,// Unpack parameters: UNPACK_COMMAND,// Unpack parameters:
"fileName", "inputDir", "sanitize" "fileName", "outPath", "sanitize"
); );
Warning(eDLL_T::FS, "%s", usage.Get()); 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 // For clients, we need an enable file which the engine uses to determine
// whether or not to mount the front-end VPK file. // 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); ReVPK_WriteEnableFile(buildPath);
} }

View File

@ -463,7 +463,7 @@ void CPackedStoreBuilder::PackStore(const VPKPair_t& vpkPair, const char* worksp
return; return;
} }
std::unique_ptr<uint8_t[]> pEntryBuffer(new uint8_t[ENTRY_MAX_LEN]); std::unique_ptr<uint8_t[]> pEntryBuffer(new uint8_t[VPK_ENTRY_MAX_LEN]);
if (!pEntryBuffer) if (!pEntryBuffer)
{ {
@ -587,8 +587,8 @@ void CPackedStoreBuilder::UnpackStore(const VPKDir_t& vpkDir, const char* worksp
workspacePath.AppendSlash(); workspacePath.AppendSlash();
workspacePath.FixSlashes('/'); workspacePath.FixSlashes('/');
std::unique_ptr<uint8_t[]> pDestBuffer(new uint8_t[ENTRY_MAX_LEN]); std::unique_ptr<uint8_t[]> pDestBuffer(new uint8_t[VPK_ENTRY_MAX_LEN]);
std::unique_ptr<uint8_t[]> pSourceBuffer(new uint8_t[ENTRY_MAX_LEN]); std::unique_ptr<uint8_t[]> pSourceBuffer(new uint8_t[VPK_ENTRY_MAX_LEN]);
if (!pDestBuffer || !pSourceBuffer) if (!pDestBuffer || !pSourceBuffer)
{ {
@ -652,7 +652,7 @@ void CPackedStoreBuilder::UnpackStore(const VPKDir_t& vpkDir, const char* worksp
continue; continue;
} }
size_t nDstLen = ENTRY_MAX_LEN; size_t nDstLen = VPK_ENTRY_MAX_LEN;
assert(fragment.m_nCompressedSize <= nDstLen); assert(fragment.m_nCompressedSize <= nDstLen);
if (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('/'); 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; size_t nFileSize = nLen;
int64_t nCurrentOffset = nOffset; int64_t nCurrentOffset = nOffset;
for (size_t i = 0; i < nFragmentCount; i++) // Fragment data into 1 MiB chunks. for (size_t i = 0; i < nFragmentCount; i++) // Fragment data into 1 MiB chunks.
{ {
size_t nSize = std::min<uint64_t>(ENTRY_MAX_LEN, nFileSize); size_t nSize = std::min<uint64_t>(VPK_ENTRY_MAX_LEN, nFileSize);
nFileSize -= nSize; nFileSize -= nSize;
m_Fragments.AddToTail(VPKChunkDescriptor_t(nLoadFlags, nTextureFlags, nCurrentOffset, nSize, nSize)); m_Fragments.AddToTail(VPKChunkDescriptor_t(nLoadFlags, nTextureFlags, nCurrentOffset, nSize, nSize));
nCurrentOffset += 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) VPKPair_t::VPKPair_t(const char* pLocale, const char* pTarget, const char* pLevel, int nPatch)
{ {
bool bFoundLocale = false; const bool bFoundLocale = V_LocaleExists(pLocale);
for (size_t i = 0; i < SDK_ARRAYSIZE(g_LanguageNames); i++)
{
if (V_strcmp(pLocale, g_LanguageNames[i]) == NULL)
{
bFoundLocale = true;
}
}
if (!bFoundLocale) if (!bFoundLocale)
{ {
@ -818,20 +810,12 @@ VPKPair_t::VPKPair_t(const char* pLocale, const char* pTarget, const char* pLeve
pLocale = g_LanguageNames[0]; pLocale = g_LanguageNames[0];
} }
bool bFoundTarget = false; const bool bFoundTarget = V_GameTargetExists(pTarget);
for (size_t i = 0; i < SDK_ARRAYSIZE(DIR_TARGET); i++)
{
if (V_strcmp(pTarget, DIR_TARGET[i]) == NULL)
{
bFoundTarget = true;
}
}
if (!bFoundTarget) if (!bFoundTarget)
{ {
Warning(eDLL_T::FS, "Target '%s' not supported; using default '%s'\n", 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 = DIR_TARGET[STORE_TARGET_SERVER]; pTarget = g_GameDllTargets[STORE_TARGET_SERVER];
} }
m_PackName.Format("%s_%s.bsp.pak000_%03d.vpk", pTarget, pLevel, nPatch); 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; CUtlString packDirToSearch;
packDirToSearch.Append(g_LanguageNames[i]); 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) if (sanitizedName.Find(targetName) != -1)
{ {

View File

@ -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/ipackedstore.h"
#include "public/ifilesystem.h" #include "public/ifilesystem.h"
#include "public/tier1/strtools.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_MAJOR_VERSION = 2;
constexpr unsigned int VPK_MINOR_VERSION = 3; constexpr unsigned int VPK_MINOR_VERSION = 3;
constexpr unsigned int VPK_DICT_SIZE = 20; 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 PACKFILEPATCH_MAX = 512;
constexpr int PACKFILEINDEX_SEP = 0x0; constexpr int PACKFILEINDEX_SEP = 0x0;
constexpr int PACKFILEINDEX_END = 0xffff; constexpr int PACKFILEINDEX_END = 0xffff;
constexpr const char VPK_IGNORE_FILE[] = ".vpkignore"; 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 // KeyValues structure for the VPK manifest file. This struct gets populated by
// the VPK's corresponding manifest file, which ultimately determines how each // 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. uint16_t m_nMinorVersion; // Vpk minor version.
uint32_t m_nDirectorySize; // Directory tree size. uint32_t m_nDirectorySize; // Directory tree size.
uint32_t m_nSignatureSize; // Directory signature. uint32_t m_nSignatureSize; // Directory signature.
VPKDirHeader_t()
{
m_nHeaderMarker = 0;
m_nMajorVersion = 0;
m_nMinorVersion = 0;
m_nDirectorySize = 0;
m_nSignatureSize = 0;
}
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------