Skip to content

Commit

Permalink
core: Remove use of SourceInterface for IFileSystem (R2Northstar#805
Browse files Browse the repository at this point in the history
)

`SourceInteface` class goes back to icepick and is not good. We have a replacement, let's use it.
  • Loading branch information
F1F7Y authored and wolf109909 committed Sep 8, 2024
1 parent 3fc20fa commit 4853916
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
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);

return fileStream.str();
}
Expand Down Expand Up @@ -67,13 +67,13 @@ 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);
sCurrentModPath = (fs::absolute(mod->m_ModDirectory) / MOD_OVERRIDE_DIR).string();
}
//NS::log::fs->info("SetNewModSearchPaths done {}", mod->Name);
}
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);
}

bool TryReplaceFile(const char* pPath, bool shouldCompile)
Expand Down Expand Up @@ -211,12 +211,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 @@ -818,7 +818,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());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion primedev/scripts/scriptdatatables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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

0 comments on commit 4853916

Please sign in to comment.