Fix fs xmodsurfs (#372)

* This commit cured my crashes!

* Use correct thread for Ents swapping

* Mutex

* Cleanup

* Cleanup2

Co-authored-by: Diavolo <edoardo.sanguineti222@gmail.com>
Co-authored-by: Louvenarde <louve@louve.systems>
This commit is contained in:
Louve 2022-07-21 00:22:49 +02:00 committed by GitHub
parent 7f2c88e38f
commit c601a1b97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 24 deletions

View File

@ -6,17 +6,28 @@ namespace Components
std::recursive_mutex FileSystem::FSMutex;
Utils::Memory::Allocator FileSystem::MemAllocator;
void FileSystem::File::read()
void FileSystem::File::read(Game::FsThread thread)
{
char* _buffer = nullptr;
int size = Game::FS_ReadFile(this->filePath.data(), &_buffer);
std::lock_guard _(FileSystem::FSMutex);
this->buffer.clear();
assert(!filePath.empty());
if (size >= 0)
int handle;
const auto len = Game::FS_FOpenFileReadForThread(filePath.data(), &handle, thread);
if (handle)
{
this->buffer.append(_buffer, size);
Game::FS_FreeFile(_buffer);
auto* buf = AllocateFile(len + 1);
Game::FS_Read(buf, len, handle);
buf[len] = '\0';
Game::FS_FCloseFile(handle);
this->buffer = buf;
FreeFile(buf);
}
}
@ -185,7 +196,7 @@ namespace Components
else *buffer = nullptr;
if (!path) return -1;
std::lock_guard<std::mutex> _(FileSystem::Mutex);
std::lock_guard _(FileSystem::Mutex);
FileSystem::FileReader reader(path);
int size = reader.getSize();
@ -256,13 +267,13 @@ namespace Components
void FileSystem::FsStartupSync(const char* a1)
{
std::lock_guard<std::recursive_mutex> _(FileSystem::FSMutex);
std::lock_guard _(FileSystem::FSMutex);
return Utils::Hook::Call<void(const char*)>(0x4823A0)(a1); // FS_Startup
}
void FileSystem::FsRestartSync(int a1, int a2)
{
std::lock_guard<std::recursive_mutex> _(FileSystem::FSMutex);
std::lock_guard _(FileSystem::FSMutex);
Maps::GetUserMap()->freeIwd();
Utils::Hook::Call<void(int, int)>(0x461A50)(a1, a2); // FS_Restart
Maps::GetUserMap()->reloadIwd();
@ -270,7 +281,7 @@ namespace Components
void FileSystem::FsShutdownSync(int a1)
{
std::lock_guard<std::recursive_mutex> _(FileSystem::FSMutex);
std::lock_guard _(FileSystem::FSMutex);
Maps::GetUserMap()->freeIwd();
Utils::Hook::Call<void(int)>(0x4A46C0)(a1); // FS_Shutdown
}
@ -283,7 +294,7 @@ namespace Components
int FileSystem::LoadTextureSync(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image)
{
std::lock_guard<std::recursive_mutex> _(FileSystem::FSMutex);
std::lock_guard _(FileSystem::FSMutex);
return Game::Load_Texture(loadDef, image);
}

View File

@ -18,8 +18,9 @@ namespace Components
class File : public AbstractFile
{
public:
File() {};
File(const std::string& file) : filePath(file) { this->read(); };
File() = default;
File(std::string file) : filePath{std::move(file)} { this->read(); };
File(std::string file, Game::FsThread thread) : filePath{std::move(file)} { this->read(thread); };
bool exists() override { return !this->buffer.empty(); };
std::string getName() override { return this->filePath; };
@ -29,7 +30,7 @@ namespace Components
std::string filePath;
std::string buffer;
void read();
void read(Game::FsThread thread = Game::FS_THREAD_MAIN);
};
class RawFile : public AbstractFile

View File

@ -255,7 +255,7 @@ namespace Components
}
static std::string mapEntities;
FileSystem::File ents(name + ".ents");
FileSystem::File ents(name + ".ents", Game::FS_THREAD_DATABASE);
if (ents.exists())
{
mapEntities = ents.getBuffer();

View File

@ -184,18 +184,15 @@ namespace Components
__asm
{
pushad
push edi
call Zones::LoadXModelLodInfo
add esp, 4h
popad
mov eax, [esp + 8h]
push eax
add eax, 8
push eax
call Game::Load_XModelSurfsFixup
add esp, 8h
push 0x4EA703 // Return address
push 0x40D7A0 // Load_XModelSurfsFixup
retn
}
}
@ -3628,7 +3625,7 @@ namespace Components
Utils::Hook(0x45AE3D, Zones::LoadRandomFxGarbage, HOOK_CALL).install()->quick();
Utils::Hook(0x495938, Zones::LoadFxElemDefArrayStub, HOOK_CALL).install()->quick();
Utils::Hook(0x45ADA0, Zones::LoadFxElemDefStub, HOOK_CALL).install()->quick();
Utils::Hook(0x4EA6FE, Zones::LoadXModelLodInfoStub, HOOK_CALL).install()->quick();
Utils::Hook(0x4EA6FE, Zones::LoadXModelLodInfoStub, HOOK_JUMP).install()->quick();
Utils::Hook(0x410D90, Zones::LoadXModel, HOOK_CALL).install()->quick();
Utils::Hook(0x4925C8, Zones::LoadXSurfaceArray, HOOK_CALL).install()->quick();
Utils::Hook(0x4F4D0D, Zones::LoadGameWorldSp, HOOK_CALL).install()->quick();

View File

@ -96,6 +96,17 @@ namespace Game
ASSET_TYPE_INVALID = -1,
};
enum FsThread
{
FS_THREAD_MAIN = 0x0,
FS_THREAD_STREAM = 0x1,
FS_THREAD_DATABASE = 0x2,
FS_THREAD_BACKEND = 0x3,
FS_THREAD_SERVER = 0x4,
FS_THREAD_COUNT = 0x5,
FS_THREAD_INVALID = 0x6,
};
enum materialSurfType_t
{
SURF_TYPE_DEFAULT,