Some fixes and introduce FileReader

This commit is contained in:
momo5502 2016-09-19 22:52:00 +02:00
parent aa89132770
commit 61fcea7742
6 changed files with 61 additions and 88 deletions

View File

@ -2,30 +2,45 @@
namespace Components namespace Components
{ {
FileSystem::File::File(std::string file) : Name(file), Handle(0) void FileSystem::File::Read()
{
char* buffer = nullptr;
int size = Game::FS_ReadFile(this->FilePath.data(), &buffer);
this->Buffer.clear();
if (size >= 0)
{
this->Buffer.append(buffer, size);
Game::FS_FreeFile(buffer);
}
}
FileSystem::FileReader::FileReader(std::string file) : Name(file), Handle(0)
{ {
this->Size = Game::FS_FOpenFileRead(this->Name.data(), &this->Handle, 0); this->Size = Game::FS_FOpenFileRead(this->Name.data(), &this->Handle, 0);
} }
FileSystem::File::~File() FileSystem::FileReader::~FileReader()
{ {
if (this->Exists()) if (this->Exists() && this->Handle)
{ {
Game::FS_FCloseFile(this->Handle); Game::FS_FCloseFile(this->Handle);
} }
} }
bool FileSystem::File::Exists() bool FileSystem::FileReader::Exists()
{ {
return (this->Size > 0); return (this->Size > 0);
} }
std::string FileSystem::File::GetName() std::string FileSystem::FileReader::GetName()
{ {
return this->Name; return this->Name;
} }
std::string FileSystem::File::GetBuffer() std::string FileSystem::FileReader::GetBuffer()
{ {
Utils::Memory::Allocator allocator; Utils::Memory::Allocator allocator;
if (!this->Exists()) return std::string(); if (!this->Exists()) return std::string();
@ -34,7 +49,7 @@ namespace Components
this->Seek(0, FS_SEEK_SET); this->Seek(0, FS_SEEK_SET);
char* buffer = allocator.AllocateArray<char>(this->Size); char* buffer = allocator.AllocateArray<char>(this->Size);
if (!FileSystem::File::Read(buffer, this->Size)) if (!this->Read(buffer, this->Size))
{ {
this->Seek(position, FS_SEEK_SET); this->Seek(position, FS_SEEK_SET);
return std::string(); return std::string();
@ -45,7 +60,7 @@ namespace Components
return std::string(buffer, this->Size); return std::string(buffer, this->Size);
} }
bool FileSystem::File::Read(void* buffer, size_t size) bool FileSystem::FileReader::Read(void* buffer, size_t size)
{ {
if (!this->Exists() || static_cast<size_t>(this->Size) < size || Game::FS_Read(buffer, size, this->Handle) != static_cast<int>(size)) if (!this->Exists() || static_cast<size_t>(this->Size) < size || Game::FS_Read(buffer, size, this->Handle) != static_cast<int>(size))
{ {
@ -55,7 +70,7 @@ namespace Components
return true; return true;
} }
void FileSystem::File::Seek(int offset, int origin) void FileSystem::FileReader::Seek(int offset, int origin)
{ {
if (this->Exists()) if (this->Exists())
{ {

View File

@ -7,9 +7,26 @@ namespace Components
class File class File
{ {
public: public:
File() : Size(-1), Name(), Handle(0) {}; File() {};
File(std::string file); File(std::string file) : FilePath(file) { this->Read(); };
~File();
bool Exists() { return !this->Buffer.empty(); };
std::string GetName() { return this->FilePath; };
std::string& GetBuffer() { return this->Buffer; };
private:
std::string FilePath;
std::string Buffer;
void Read();
};
class FileReader
{
public:
FileReader() : Size(-1), Name(), Handle(0) {};
FileReader(std::string file);
~FileReader();
bool Exists(); bool Exists();
std::string GetName(); std::string GetName();

View File

@ -38,7 +38,7 @@ namespace Components
Game::XModelSurfs* ModelSurfs::LoadXModelSurfaces(std::string name) Game::XModelSurfs* ModelSurfs::LoadXModelSurfaces(std::string name)
{ {
Utils::Memory::Allocator allocator; Utils::Memory::Allocator allocator;
FileSystem::File model(fmt::sprintf("models/%s", name.data())); FileSystem::FileReader model(fmt::sprintf("models/%s", name.data()));
if (!model.Exists()) if (!model.Exists())
{ {

View File

@ -4,16 +4,11 @@ namespace Components
{ {
int Zones::ZoneVersion; int Zones::ZoneVersion;
extern bool* useEntryNames;
Utils::Hook fxEffectLoadHook; Utils::Hook fxEffectLoadHook;
static DWORD fxEffectStringValue[64]; static DWORD fxEffectStringValue[64];
static int fxEffectIndex = 0; static int fxEffectIndex = 0;
int ffVersion = 0;
#define VERSION_ALPHA2 316 #define VERSION_ALPHA2 316
#define VERSION_ALPHA3 318//319 #define VERSION_ALPHA3 318//319
@ -52,26 +47,16 @@ namespace Components
Utils::Hook fxEffectModifyHook; Utils::Hook fxEffectModifyHook;
bool ignoreThisFx = false;
void FxEffectModifyHookFunc(int a1, char* buffer, size_t len) void FxEffectModifyHookFunc(int a1, char* buffer, size_t len)
{ {
char typeNum = *(char*)(buffer + 176); char typeNum = *(char*)(buffer + 176);
//DBG(("ot %i\n", typeNum)); //DBG(("ot %i\n", typeNum));
if (typeNum == 2) if (typeNum == 3)
{
ignoreThisFx = true;
}
else if (typeNum == 3)
{ {
typeNum = 2; typeNum = 2;
} }
else if (typeNum == 4)
{
ignoreThisFx = true;
}
else if (typeNum >= 5) else if (typeNum >= 5)
{ {
typeNum -= 2; typeNum -= 2;
@ -155,7 +140,7 @@ namespace Components
add esp, 0Ch add esp, 0Ch
} }
int elSize = (ffVersion == VERSION_ALPHA2) ? 364 : 360; int elSize = (Zones::ZoneVersion == VERSION_ALPHA2) ? 364 : 360;
static unsigned char tempVar[364]; static unsigned char tempVar[364];
memcpy(&tempVar[0], &buffer[0], 36); memcpy(&tempVar[0], &buffer[0], 36);
@ -198,7 +183,7 @@ namespace Components
void XModelLODTailHookFunc(int i) void XModelLODTailHookFunc(int i)
{ {
int elSize = (ffVersion == VERSION_ALPHA2) ? 364 : 360; int elSize = (Zones::ZoneVersion == VERSION_ALPHA2) ? 364 : 360;
i = (4 - i)/* + 1*/; i = (4 - i)/* + 1*/;
// varXString = (varXModel + 0x16C - (4 * i)) // where the above function parked the new xstring // varXString = (varXModel + 0x16C - (4 * i)) // where the above function parked the new xstring
@ -234,40 +219,6 @@ namespace Components
} }
} }
Utils::Hook fxDefaultHook;
void FxDefaultHookFunc()
{
DWORD* varFxEffectDefPtr = *(DWORD**)0x112ACC0;
*varFxEffectDefPtr = (DWORD)Game::DB_FindXAssetHeader(Game::ASSET_TYPE_FX, "misc/missing_fx").data;
}
void __declspec(naked) FxDefaultHookStub()
{
__asm
{
jmp FxDefaultHookFunc
}
}
Utils::Hook xmodelDefaultHook;
void XModelDefaultHookFunc()
{
DWORD* varXModelPtr = *(DWORD**)0x112A934;
*varXModelPtr = (DWORD)Game::DB_FindXAssetHeader(Game::ASSET_TYPE_XMODEL, "void").data;
}
void __declspec(naked) XModelDefaultHookStub()
{
__asm
{
jmp XModelDefaultHookFunc
}
}
Utils::Hook xsurfaceIntLoadHook; Utils::Hook xsurfaceIntLoadHook;
void XSurfaceIntLoadHookFunc(int a1, char* buffer, size_t len) void XSurfaceIntLoadHookFunc(int a1, char* buffer, size_t len)
@ -361,7 +312,7 @@ namespace Components
DWORD* varXString = (DWORD*)0x112B340; DWORD* varXString = (DWORD*)0x112B340;
// and do the stuff // and do the stuff
Load_Stream(1, varWeaponDef, (ffVersion >= 318) ? 3156 : 3112); Load_Stream(1, varWeaponDef, (Zones::ZoneVersion >= 318) ? 3156 : 3112);
DB_PushStreamPos(3); DB_PushStreamPos(3);
@ -426,7 +377,7 @@ namespace Components
Load_SndAliasCustom(*varsnd_alias_list_name); Load_SndAliasCustom(*varsnd_alias_list_name);
} }
if (ffVersion >= 318) if (Zones::ZoneVersion >= 318)
{ {
for (int i = 0, offset = 1184; i < 2; i++, offset += 4) for (int i = 0, offset = 1184; i < 2; i++, offset += 4)
{ {
@ -536,7 +487,7 @@ namespace Components
DWORD vec2 = DB_AllocStreamPos(3); DWORD vec2 = DB_AllocStreamPos(3);
*(DWORD*)(varWeaponDef + 2556) = vec2; *(DWORD*)(varWeaponDef + 2556) = vec2;
Load_Stream(1, vec2, 8 * *(short*)(varWeaponDef + ((ffVersion >= 318) ? 3076 : 3040))); Load_Stream(1, vec2, 8 * *(short*)(varWeaponDef + ((Zones::ZoneVersion >= 318) ? 3076 : 3040)));
} }
*varXString = varWeaponDef + 2552; *varXString = varWeaponDef + 2552;
@ -547,7 +498,7 @@ namespace Components
DWORD vec2 = DB_AllocStreamPos(3); DWORD vec2 = DB_AllocStreamPos(3);
*(DWORD*)(varWeaponDef + 2560) = vec2; *(DWORD*)(varWeaponDef + 2560) = vec2;
Load_Stream(1, vec2, 8 * *(short*)(varWeaponDef + ((ffVersion >= 318) ? 3078 : 3042))); Load_Stream(1, vec2, 8 * *(short*)(varWeaponDef + ((Zones::ZoneVersion >= 318) ? 3078 : 3042)));
} }
*varXString = varWeaponDef + 2640; *varXString = varWeaponDef + 2640;
@ -595,7 +546,7 @@ namespace Components
*varsnd_alias_list_name = varWeaponDef + 2904; // 2912 *varsnd_alias_list_name = varWeaponDef + 2904; // 2912
Load_SndAliasCustom(*varsnd_alias_list_name); Load_SndAliasCustom(*varsnd_alias_list_name);
if (ffVersion >= 318) if (Zones::ZoneVersion >= 318)
{ {
for (int i = 0, offset = 2972; i < 6; i++, offset += 4) for (int i = 0, offset = 2972; i < 6; i++, offset += 4)
{ {
@ -976,15 +927,13 @@ namespace Components
} }
} }
void PatchMW2_FifthInfinityApply(int fileVersion, bool iw5) void PatchMW2_FifthInfinityApply(bool iw5)
{ {
ffVersion = fileVersion;
// physpreset size // physpreset size
*(BYTE*)0x49CE0A = (iw5) ? 68 : 44; *(BYTE*)0x49CE0A = (iw5) ? 68 : 44;
// XModel size // XModel size
*(DWORD*)0x410D8A = (iw5) ? ((ffVersion == VERSION_ALPHA2) ? 0x16C : 0x168) : 0x130; *(DWORD*)0x410D8A = (iw5) ? ((Zones::ZoneVersion == VERSION_ALPHA2) ? 0x16C : 0x168) : 0x130;
// XSurface size // XSurface size
*(BYTE*)0x48E84A = (iw5) ? 48 : 36; *(BYTE*)0x48E84A = (iw5) ? 48 : 36;
@ -1031,16 +980,13 @@ namespace Components
loadWeaponAttachHook.Install(); loadWeaponAttachHook.Install();
if (ffVersion >= VERSION_ALPHA3) if (Zones::ZoneVersion >= VERSION_ALPHA3)
{ {
gameWorldSpIntHook.Install(); gameWorldSpIntHook.Install();
} }
loadTechniquePassHook.Install(); loadTechniquePassHook.Install();
loadStructuredDataChildArrayHook.Install(); loadStructuredDataChildArrayHook.Install();
//xmodelDefaultHook.Install();
//fxDefaultHook.Install();
} }
else else
{ {
@ -1064,9 +1010,6 @@ namespace Components
loadWeaponAttachHook.Uninstall(); loadWeaponAttachHook.Uninstall();
xmodelDefaultHook.Uninstall();
fxDefaultHook.Uninstall();
gameWorldSpIntHook.Uninstall(); gameWorldSpIntHook.Uninstall();
loadTechniquePassHook.Uninstall(); loadTechniquePassHook.Uninstall();
@ -1085,7 +1028,7 @@ namespace Components
Utils::Hook::Set<DWORD>(0x4158FB, version); Utils::Hook::Set<DWORD>(0x4158FB, version);
} }
PatchMW2_FifthInfinityApply(version, version >= 316); PatchMW2_FifthInfinityApply(version >= 316);
AntiCheat::EmptyHash(); AntiCheat::EmptyHash();
} }
@ -1107,8 +1050,6 @@ namespace Components
gameWorldSpLoadHook.Initialize(0x4F4D0D, GameWorldSpLoadHookFunc, HOOK_CALL); gameWorldSpLoadHook.Initialize(0x4F4D0D, GameWorldSpLoadHookFunc, HOOK_CALL);
loadWeaponDefHook.Initialize(0x47CCD2, Load_WeaponDef_CodC, HOOK_CALL); loadWeaponDefHook.Initialize(0x47CCD2, Load_WeaponDef_CodC, HOOK_CALL);
vehicleLoadHook.Initialize(0x483DA0, VehicleLoadHookFunc, HOOK_CALL); vehicleLoadHook.Initialize(0x483DA0, VehicleLoadHookFunc, HOOK_CALL);
xmodelDefaultHook.Initialize(0x4FCAEE, XModelDefaultHookStub, HOOK_CALL);
fxDefaultHook.Initialize(0x4D9C0E, FxDefaultHookStub, HOOK_CALL);
allocZoneMemoryHook.Initialize(0x415A57, AllocXZoneMemoryHookStub, HOOK_CALL); allocZoneMemoryHook.Initialize(0x415A57, AllocXZoneMemoryHookStub, HOOK_CALL);
sndAliasLoadHook.Initialize(0x4F0AC8, SndAliasLoadHookFunc, HOOK_CALL); sndAliasLoadHook.Initialize(0x4F0AC8, SndAliasLoadHookFunc, HOOK_CALL);
mssSoundLoadHook.Initialize(0x403A5D, MssSoundLoadHookFunc, HOOK_CALL); mssSoundLoadHook.Initialize(0x403A5D, MssSoundLoadHookFunc, HOOK_CALL);

View File

@ -12,7 +12,7 @@ namespace Components
static void InstallPatches(int version); static void InstallPatches(int version);
private: //private:
static int ZoneVersion; static int ZoneVersion;
}; };
} }