Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Remove use of SourceInterface for IFileSystem #805

Merged
merged 5 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions primedev/core/filesystem/filesystem.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "filesystem.h"
#include "core/sourceinterface.h"
#include "core/tier1.h"
#include "mods/modmanager.h"

#include <iostream>
Expand All @@ -10,23 +10,23 @@ std::string sCurrentModPath;

ConVar* Cvar_ns_fs_log_reads;

SourceInterface<IFileSystem>* g_pFilesystem;
IFileSystem* g_pFilesystem;

std::string ReadVPKFile(const char* path)
{
// read scripts.rson file, todo: check if this can be overwritten
FileHandle_t fileHandle = (*g_pFilesystem)->m_vtable2->Open(&(*g_pFilesystem)->m_vtable2, path, "rb", "GAME", 0);
FileHandle_t fileHandle = g_pFilesystem->m_vtable2->Open(&g_pFilesystem->m_vtable2, path, "rb", "GAME", 0);

std::stringstream fileStream;
int bytesRead = 0;
char data[4096];
do
{
bytesRead = (*g_pFilesystem)->m_vtable2->Read(&(*g_pFilesystem)->m_vtable2, data, (int)std::size(data), fileHandle);
bytesRead = g_pFilesystem->m_vtable2->Read(&g_pFilesystem->m_vtable2, data, (int)std::size(data), fileHandle);
fileStream.write(data, bytesRead);
} while (bytesRead == std::size(data));

(*g_pFilesystem)->m_vtable2->Close(*g_pFilesystem, fileHandle);
g_pFilesystem->m_vtable2->Close(&*g_pFilesystem, fileHandle);
F1F7Y marked this conversation as resolved.
Show resolved Hide resolved
F1F7Y marked this conversation as resolved.
Show resolved Hide resolved

return fileStream.str();
}
Expand Down Expand Up @@ -65,12 +65,12 @@ void SetNewModSearchPaths(Mod* mod)
if ((fs::absolute(mod->m_ModDirectory) / MOD_OVERRIDE_DIR).string().compare(sCurrentModPath))
{
o_pAddSearchPath(
&*(*g_pFilesystem), (fs::absolute(mod->m_ModDirectory) / MOD_OVERRIDE_DIR).string().c_str(), "GAME", PATH_ADD_TO_HEAD);
&*g_pFilesystem, (fs::absolute(mod->m_ModDirectory) / MOD_OVERRIDE_DIR).string().c_str(), "GAME", PATH_ADD_TO_HEAD);
F1F7Y marked this conversation as resolved.
Show resolved Hide resolved
sCurrentModPath = (fs::absolute(mod->m_ModDirectory) / MOD_OVERRIDE_DIR).string();
}
}
else // push compiled to head
o_pAddSearchPath(&*(*g_pFilesystem), fs::absolute(GetCompiledAssetsPath()).string().c_str(), "GAME", PATH_ADD_TO_HEAD);
o_pAddSearchPath(&*g_pFilesystem, fs::absolute(GetCompiledAssetsPath()).string().c_str(), "GAME", PATH_ADD_TO_HEAD);
F1F7Y marked this conversation as resolved.
Show resolved Hide resolved
}

bool TryReplaceFile(const char* pPath, bool shouldCompile)
Expand Down Expand Up @@ -167,12 +167,12 @@ ON_DLL_LOAD("filesystem_stdio.dll", Filesystem, (CModule module))
o_pCBaseFileSystem__OpenEx = module.Offset(0x15F50).RCast<decltype(o_pCBaseFileSystem__OpenEx)>();
HookAttach(&(PVOID&)o_pCBaseFileSystem__OpenEx, (PVOID)h_CBaseFileSystem__OpenEx);

g_pFilesystem = new SourceInterface<IFileSystem>("filesystem_stdio.dll", "VFileSystem017");
g_pFilesystem = Sys_GetFactoryPtr("filesystem_stdio.dll", "VFileSystem017").RCast<IFileSystem*>();

o_pAddSearchPath = reinterpret_cast<decltype(o_pAddSearchPath)>((*g_pFilesystem)->m_vtable->AddSearchPath);
o_pAddSearchPath = reinterpret_cast<decltype(o_pAddSearchPath)>(g_pFilesystem->m_vtable->AddSearchPath);
HookAttach(&(PVOID&)o_pAddSearchPath, (PVOID)h_AddSearchPath);
o_pReadFromCache = reinterpret_cast<decltype(o_pReadFromCache)>((*g_pFilesystem)->m_vtable->ReadFromCache);
o_pReadFromCache = reinterpret_cast<decltype(o_pReadFromCache)>(g_pFilesystem->m_vtable->ReadFromCache);
HookAttach(&(PVOID&)o_pReadFromCache, (PVOID)h_ReadFromCache);
o_pMountVPK = reinterpret_cast<decltype(o_pMountVPK)>((*g_pFilesystem)->m_vtable->MountVPK);
o_pMountVPK = reinterpret_cast<decltype(o_pMountVPK)>(g_pFilesystem->m_vtable->MountVPK);
HookAttach(&(PVOID&)o_pMountVPK, (PVOID)h_MountVPK);
}
3 changes: 1 addition & 2 deletions primedev/core/filesystem/filesystem.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "core/sourceinterface.h"

// taken from ttf2sdk
typedef void* FileHandle_t;
Expand Down Expand Up @@ -48,7 +47,7 @@ class IFileSystem
VTable2* m_vtable2;
};

extern SourceInterface<IFileSystem>* g_pFilesystem;
extern IFileSystem* g_pFilesystem;

std::string ReadVPKFile(const char* path);
std::string ReadVPKOriginalFile(const char* path);
2 changes: 1 addition & 1 deletion primedev/mods/modmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ void ModManager::LoadMods()
modVpk.m_sVpkPath = (file.path().parent_path() / vpkName).string();

if (m_bHasLoadedMods && modVpk.m_bAutoLoad)
(*g_pFilesystem)->m_vtable->MountVPK(*g_pFilesystem, vpkName.c_str());
g_pFilesystem->m_vtable->MountVPK(&*g_pFilesystem, vpkName.c_str());
F1F7Y marked this conversation as resolved.
Show resolved Hide resolved
F1F7Y marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion primedev/scripts/scriptdatatables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ REPLACE_SQFUNC(GetDataTable, (ScriptContext::UI | ScriptContext::CLIENT | Script
diskAssetPath /= fs::path(pAssetName);

std::string sDiskAssetPath(diskAssetPath.string());
if ((*g_pFilesystem)->m_vtable2->FileExists(&(*g_pFilesystem)->m_vtable2, sDiskAssetPath.c_str(), "GAME"))
if (g_pFilesystem->m_vtable2->FileExists(&g_pFilesystem->m_vtable2, sDiskAssetPath.c_str(), "GAME"))
{
std::string sTableCSV = ReadVPKFile(sDiskAssetPath.c_str());
if (!sTableCSV.size())
Expand Down
Loading