2022-04-09 00:59:42 +02:00
|
|
|
#include "core/stdafx.h"
|
|
|
|
#include "core/logdef.h"
|
2022-04-09 16:16:40 +02:00
|
|
|
#include "tier1/cvar.h"
|
2022-04-09 00:59:42 +02:00
|
|
|
#include "filesystem/basefilesystem.h"
|
|
|
|
#include "filesystem/filesystem.h"
|
|
|
|
#ifndef DEDICATED
|
|
|
|
#include "gameui/IConsole.h"
|
|
|
|
#endif // !DEDICATED
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: prints the output of the filesystem based on the warning level
|
|
|
|
// Input : *this -
|
|
|
|
// level -
|
|
|
|
// *pFmt -
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
void CBaseFileSystem::Warning(CBaseFileSystem* pFileSystem, FileWarningLevel_t level, const char* pFmt, ...)
|
|
|
|
{
|
2022-05-09 02:20:07 +02:00
|
|
|
if (fs_warning_level_sdk->GetInt() < static_cast<int>(level))
|
2022-04-09 00:59:42 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-06 12:21:21 +01:00
|
|
|
static char szBuf[4096] = {};
|
2022-04-09 00:59:42 +02:00
|
|
|
|
|
|
|
static std::shared_ptr<spdlog::logger> iconsole = spdlog::get("game_console");
|
|
|
|
static std::shared_ptr<spdlog::logger> wconsole = spdlog::get("win_console");
|
2022-05-17 23:00:30 +02:00
|
|
|
static std::shared_ptr<spdlog::logger> fslogger = spdlog::get("fs_warn");
|
2022-04-09 00:59:42 +02:00
|
|
|
|
|
|
|
{/////////////////////////////
|
|
|
|
va_list args{};
|
|
|
|
va_start(args, pFmt);
|
|
|
|
|
|
|
|
vsnprintf(szBuf, sizeof(szBuf), pFmt, args);
|
|
|
|
|
2022-08-09 13:08:58 +02:00
|
|
|
szBuf[sizeof(szBuf) - 1] = '\0';
|
2022-04-09 00:59:42 +02:00
|
|
|
va_end(args);
|
|
|
|
}/////////////////////////////
|
|
|
|
|
|
|
|
fslogger->debug(szBuf);
|
|
|
|
|
|
|
|
if (fs_show_warning_output->GetBool())
|
|
|
|
{
|
|
|
|
wconsole->debug(szBuf);
|
|
|
|
#ifndef DEDICATED
|
|
|
|
iconsole->debug(szBuf);
|
2023-03-25 21:27:49 +01:00
|
|
|
g_pConsole->AddLog(ConLog_t(g_LogStream.str(), ImVec4(1.00f, 1.00f, 0.00f, 1.00f)));
|
2022-04-09 00:59:42 +02:00
|
|
|
|
2023-03-25 21:27:49 +01:00
|
|
|
g_LogStream.str("");
|
|
|
|
g_LogStream.clear();
|
2022-04-09 00:59:42 +02:00
|
|
|
#endif // !DEDICATED
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
2022-11-08 01:11:14 +01:00
|
|
|
// Purpose: attempts to load files from disk if exist before loading from VPK/cache
|
|
|
|
// Input : *pszFilePath -
|
2022-11-06 12:21:21 +01:00
|
|
|
// Output : handle to file on success, NULL on failure
|
2022-04-09 00:59:42 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
2022-11-08 01:11:14 +01:00
|
|
|
bool CBaseFileSystem::VCheckDisk(const char* pszFilePath)
|
2022-04-09 00:59:42 +02:00
|
|
|
{
|
2022-11-08 01:11:14 +01:00
|
|
|
// Only load material files from the disk if the mode isn't zero,
|
|
|
|
// use -novpk to load valve materials from the disk.
|
|
|
|
if (FileSystem()->CheckVPKMode(0) && strstr(pszFilePath, ".vmt"))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-09 00:59:42 +02:00
|
|
|
std::string svFilePath = ConvertToWinPath(pszFilePath);
|
2022-11-24 15:41:52 +01:00
|
|
|
if (svFilePath.find("\\*\\") != string::npos)
|
2022-04-09 00:59:42 +02:00
|
|
|
{
|
|
|
|
// Erase '//*/'.
|
|
|
|
svFilePath.erase(0, 4);
|
|
|
|
}
|
|
|
|
|
2023-03-18 21:36:05 +01:00
|
|
|
fs::path filePath(svFilePath);
|
|
|
|
if (filePath.is_absolute())
|
|
|
|
{
|
|
|
|
// Skip absolute file paths.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-09 00:59:42 +02:00
|
|
|
// TODO: obtain 'mod' SearchPath's instead.
|
|
|
|
svFilePath.insert(0, "platform\\");
|
|
|
|
|
2022-06-12 12:40:26 +02:00
|
|
|
if (::FileExists(svFilePath) /*|| ::FileExists(pszFilePath)*/)
|
2022-11-08 01:11:14 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: loads files from VPK
|
|
|
|
// Input : *this -
|
|
|
|
// *pResults -
|
|
|
|
// *pszFilePath -
|
|
|
|
// Output : handle to file on success, NULL on failure
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
FileHandle_t CBaseFileSystem::VReadFromVPK(CBaseFileSystem* pFileSystem, FileHandle_t pResults, char* pszFilePath)
|
|
|
|
{
|
|
|
|
if (VCheckDisk(pszFilePath))
|
2022-04-09 00:59:42 +02:00
|
|
|
{
|
2022-06-12 12:14:31 +02:00
|
|
|
*reinterpret_cast<int64_t*>(pResults) = -1;
|
|
|
|
return pResults;
|
2022-04-09 00:59:42 +02:00
|
|
|
}
|
2022-11-08 01:11:14 +01:00
|
|
|
|
2022-11-06 12:21:21 +01:00
|
|
|
return v_CBaseFileSystem_LoadFromVPK(pFileSystem, pResults, pszFilePath);
|
2022-04-09 00:59:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
2022-11-08 01:11:14 +01:00
|
|
|
// Purpose: loads files from cache
|
2022-11-06 12:21:21 +01:00
|
|
|
// Input : *this -
|
2022-04-09 00:59:42 +02:00
|
|
|
// *pszFilePath -
|
|
|
|
// *pResults -
|
|
|
|
// Output : true if file exists, false otherwise
|
|
|
|
//---------------------------------------------------------------------------------
|
2022-06-12 12:49:57 +02:00
|
|
|
bool CBaseFileSystem::VReadFromCache(CBaseFileSystem* pFileSystem, char* pszFilePath, void* pResults)
|
2022-04-09 00:59:42 +02:00
|
|
|
{
|
2022-11-08 01:11:14 +01:00
|
|
|
if (VCheckDisk(pszFilePath))
|
2022-04-09 00:59:42 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-08 01:11:14 +01:00
|
|
|
|
2022-11-06 12:21:21 +01:00
|
|
|
return v_CBaseFileSystem_LoadFromCache(pFileSystem, pszFilePath, pResults);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: attempts to mount VPK file for filesystem usage
|
|
|
|
// Input : *this -
|
|
|
|
// *pszVpkPath -
|
|
|
|
// Output : pointer to VPK on success, NULL on failure
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
VPKData_t* CBaseFileSystem::VMountVPKFile(CBaseFileSystem* pFileSystem, const char* pszVpkPath)
|
|
|
|
{
|
|
|
|
int nHandle = v_CBaseFileSystem_GetMountedVPKHandle(pFileSystem, pszVpkPath);
|
|
|
|
VPKData_t* pPakData = v_CBaseFileSystem_MountVPKFile(pFileSystem, pszVpkPath);
|
|
|
|
|
2022-11-10 01:20:45 +01:00
|
|
|
if (pPakData)
|
2022-11-06 12:21:21 +01:00
|
|
|
{
|
2022-11-10 01:20:45 +01:00
|
|
|
if (nHandle < 0) // Only log if VPK hasn't been mounted yet.
|
|
|
|
{
|
|
|
|
::DevMsg(eDLL_T::FS, "Mounted vpk file: '%s' with handle: '%i'\n", pszVpkPath, pPakData->m_nHandle);
|
|
|
|
}
|
2022-11-06 12:21:21 +01:00
|
|
|
}
|
2022-11-10 01:20:45 +01:00
|
|
|
else // VPK failed to load or does not exist...
|
2022-11-06 12:21:21 +01:00
|
|
|
{
|
2022-11-06 14:21:27 +01:00
|
|
|
::Warning(eDLL_T::FS, "Unable to mount vpk file: '%s'\n", pszVpkPath);
|
2022-11-06 12:21:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return pPakData;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: unmount a VPK file
|
|
|
|
// Input : *this -
|
|
|
|
// *pszVpkPath -
|
|
|
|
// Output : pointer to formatted VPK path string
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
const char* CBaseFileSystem::VUnmountVPKFile(CBaseFileSystem* pFileSystem, const char* pszVpkPath)
|
|
|
|
{
|
|
|
|
int nHandle = v_CBaseFileSystem_GetMountedVPKHandle(pFileSystem, pszVpkPath);
|
|
|
|
const char* pRet = v_CBaseFileSystem_UnmountVPKFile(pFileSystem, pszVpkPath);
|
|
|
|
|
|
|
|
if (nHandle >= 0)
|
|
|
|
{
|
2022-11-06 14:21:27 +01:00
|
|
|
::DevMsg(eDLL_T::FS, "Unmounted vpk file: '%s' with handle: '%i'\n", pszVpkPath, nHandle);
|
2022-11-06 12:21:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
2022-04-09 00:59:42 +02:00
|
|
|
}
|
|
|
|
|
2022-11-23 12:18:33 +01:00
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: reads a string until its null terminator
|
|
|
|
// Input : *pFile -
|
|
|
|
// Output : string
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
string CBaseFileSystem::ReadString(FileHandle_t pFile)
|
|
|
|
{
|
|
|
|
string svString;
|
|
|
|
char c = '\0';
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Read(&c, sizeof(char), pFile);
|
|
|
|
|
|
|
|
if (c)
|
|
|
|
svString += c;
|
|
|
|
|
|
|
|
} while (c);
|
|
|
|
|
|
|
|
return svString;
|
|
|
|
}
|
|
|
|
|
2023-01-25 02:26:52 +01:00
|
|
|
void VBaseFileSystem::Attach() const
|
2022-04-09 00:59:42 +02:00
|
|
|
{
|
2022-11-06 12:21:21 +01:00
|
|
|
DetourAttach((LPVOID*)&v_CBaseFileSystem_Warning, &CBaseFileSystem::Warning);
|
|
|
|
DetourAttach((LPVOID*)&v_CBaseFileSystem_LoadFromVPK, &CBaseFileSystem::VReadFromVPK);
|
|
|
|
DetourAttach((LPVOID*)&v_CBaseFileSystem_LoadFromCache, &CBaseFileSystem::VReadFromCache);
|
|
|
|
DetourAttach((LPVOID*)&v_CBaseFileSystem_MountVPKFile, &CBaseFileSystem::VMountVPKFile);
|
|
|
|
DetourAttach((LPVOID*)&v_CBaseFileSystem_UnmountVPKFile, &CBaseFileSystem::VUnmountVPKFile);
|
2022-04-09 00:59:42 +02:00
|
|
|
}
|
|
|
|
|
2023-01-25 02:26:52 +01:00
|
|
|
void VBaseFileSystem::Detach() const
|
2022-04-09 00:59:42 +02:00
|
|
|
{
|
2022-11-06 12:21:21 +01:00
|
|
|
DetourDetach((LPVOID*)&v_CBaseFileSystem_Warning, &CBaseFileSystem::Warning);
|
|
|
|
DetourDetach((LPVOID*)&v_CBaseFileSystem_LoadFromVPK, &CBaseFileSystem::VReadFromVPK);
|
|
|
|
DetourDetach((LPVOID*)&v_CBaseFileSystem_LoadFromCache, &CBaseFileSystem::VReadFromCache);
|
|
|
|
DetourDetach((LPVOID*)&v_CBaseFileSystem_MountVPKFile, &CBaseFileSystem::VMountVPKFile);
|
|
|
|
DetourDetach((LPVOID*)&v_CBaseFileSystem_UnmountVPKFile, &CBaseFileSystem::VUnmountVPKFile);
|
2022-04-09 00:59:42 +02:00
|
|
|
}
|
2022-05-27 22:38:49 +02:00
|
|
|
CBaseFileSystem* g_pFileSystem = nullptr;
|