Skip to content

Commit

Permalink
th07: add snapshot support
Browse files Browse the repository at this point in the history
  • Loading branch information
32th-System committed Jul 12, 2022
1 parent 0b1beb1 commit 3518bf3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 116 deletions.
119 changes: 3 additions & 116 deletions thprac/src/thprac/thprac_th06.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "thprac_utils.h"
#include "../3rdParties/d3d8/include/d3d8.h"

namespace THPrac {

Expand Down Expand Up @@ -771,121 +772,6 @@ namespace TH06 {
bool mParamStatus = false;
THPracParam mRepParam;
};
class THSnapshot {
private:
static void Snapshot()
{
wchar_t dir[] = L"snapshot/th000.bmp";
HANDLE hFile;
CreateDirectoryW(L"snapshot", NULL);
for (int i = 0; i < 1000; i++) {
dir[13] = i % 10 + 0x30;
dir[12] = ((i % 100 - i % 10) / 10) + 0x30;
dir[11] = ((i - i % 100) / 100) + 0x30;
hFile = CreateFileW(dir, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
break;
}
if (hFile == INVALID_HANDLE_VALUE)
return;

auto header = "\x42\x4d\x36\x10\x0e\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00\x80\x02\x00\x00\xe0\x01\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
void* bmp = nullptr;
DWORD bytesRead;
bmp = GetSnapshotData();
if (bmp) {
WriteFile(hFile, header, 0x36, &bytesRead, NULL);
WriteFile(hFile, bmp, 0xe1000, &bytesRead, NULL);
free(bmp);
}

CloseHandle(hFile);
}
static void* GetSnapshotData()
{
int hFile = 0;
void* surface = nullptr;
int32_t rect[2] { 0, 0 };
void* bmp = malloc(0xe2000);
//MB_INFO("FINDME");

__asm {
lea eax, surface
push eax
push 0
push 0
mov eax, 0x6c6d20
mov ecx, [eax]
push ecx
mov eax, 0x6c6d20
mov eax, [eax]
mov eax, [eax]
call dword ptr[eax + 0x40]
push 0
push 0
lea eax, rect
push eax
mov ecx, surface
push ecx
mov eax, [ecx]
call dword ptr[eax + 0x24]
mov esi, rect[4]
mov edi, bmp
mov ebx, 1
mov ecx, 0x1df

snap_loop_1:
cmp ecx, 0
jl snap_end
mov eax, ecx
mov edx, rect[0]
imul eax, edx
add esi, eax
mov ebx, 1
snap_loop_2:
cmp ebx, 0x280
jg snap_loop_3
movzx eax, byte ptr[esi]
mov byte ptr[edi], al
inc esi
inc edi
movzx eax, byte ptr[esi]
mov byte ptr[edi], al
inc esi
inc edi
movzx eax, byte ptr[esi]
mov byte ptr[edi], al
inc esi
inc edi
inc esi
inc ebx
jmp snap_loop_2
snap_loop_3 :
mov esi, rect[4]
dec ecx
jmp snap_loop_1

snap_end :
mov ecx, surface
push ecx
mov eax, [ecx]
call dword ptr[eax + 0x28]
mov ecx, surface
push ecx
mov eax, [ecx]
call dword ptr[eax + 0x8]
}

return bmp;
}

public:
static void Update()
{
if (Gui::KeyboardInputUpdate(VK_HOME) == 1)
Snapshot();
}
};

class THAdvOptWnd : public Gui::PPGuiWnd {
// Option Related Functions
Expand Down Expand Up @@ -2247,7 +2133,8 @@ namespace TH06 {
EHOOK_DY(th06_render, (void*)0x41cb6d)
{
GameGuiRender(IMPL_WIN32_DX8);
THSnapshot::Update();
if (Gui::KeyboardInputUpdate(VK_HOME) == 1)
THSnapshot::Snapshot(*(IDirect3DDevice8**)0x6c6d20);
}
HOOKSET_ENDDEF()

Expand Down
2 changes: 2 additions & 0 deletions thprac/src/thprac/thprac_th07.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,8 @@ namespace TH07 {
EHOOK_DY(th07_render, (void*)0x42feb9)
{
GameGuiRender(IMPL_WIN32_DX8);
if (Gui::KeyboardInputUpdate(VK_HOME) == 1)
THSnapshot::Snapshot(*(IDirect3DDevice8**)0x575958);
}
HOOKSET_ENDDEF()

Expand Down
91 changes: 91 additions & 0 deletions thprac/src/thprac/thprac_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "thprac_launcher_cfg.h"
#include <metrohash128.h>
#include "thprac_data_anly.h"
#include "../3rdParties/d3d8/include/d3d8.h"

namespace THPrac {

Expand Down Expand Up @@ -1267,6 +1268,96 @@ void* VFSOriginal(const char* file_name, int32_t* file_size, int32_t is_file)
#endif
#pragma endregion

#pragma region Snapshot
namespace THSnapshot {
void* GetSnapshotData(IDirect3DDevice8* d3d8)
{
int32_t rect[2] = {};
void* bmp = malloc(0xE2000);
// MB_INFO("FINDME");

IDirect3DSurface8* surface = NULL;
d3d8->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &surface);
surface->LockRect((D3DLOCKED_RECT*)&rect, NULL, 0);

// Serious bruh moment. For some reason ACK wrote this entire function
// in Assembly when it there's no reason why you couldn't write it in C
// I rewrote part of it but I couldn't figure out this loop so it's still
// written in Assembly.
__asm {
mov esi, rect[4]
mov edi, bmp
mov ebx, 1
mov ecx, 0x1df
snap_loop_1:
cmp ecx, 0
jl snap_end
mov eax, ecx
mov edx, rect[0]
imul eax, edx
add esi, eax
mov ebx, 1
snap_loop_2:
cmp ebx, 0x280
jg snap_loop_3
movzx eax, byte ptr[esi]
mov byte ptr[edi], al
inc esi
inc edi
movzx eax, byte ptr[esi]
mov byte ptr[edi], al
inc esi
inc edi
movzx eax, byte ptr[esi]
mov byte ptr[edi], al
inc esi
inc edi
inc esi
inc ebx
jmp snap_loop_2
snap_loop_3 :
mov esi, rect[4]
dec ecx
jmp snap_loop_1
snap_end :
}

surface->UnlockRect();
surface->Release();

return bmp;
}
void Snapshot(IDirect3DDevice8* d3d8)
{
wchar_t dir[] = L"snapshot/th000.bmp";
HANDLE hFile;
CreateDirectoryW(L"snapshot", NULL);
for (int i = 0; i < 1000; i++) {
dir[13] = i % 10 + 0x30;
dir[12] = ((i % 100 - i % 10) / 10) + 0x30;
dir[11] = ((i - i % 100) / 100) + 0x30;
hFile = CreateFileW(dir, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
break;
}
if (hFile == INVALID_HANDLE_VALUE)
return;

auto header = "\x42\x4d\x36\x10\x0e\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00\x80\x02\x00\x00\xe0\x01\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
void* bmp = nullptr;
DWORD bytesRead;
bmp = GetSnapshotData(d3d8);
if (bmp) {
WriteFile(hFile, header, 0x36, &bytesRead, NULL);
WriteFile(hFile, bmp, 0xE2000, &bytesRead, NULL);
free(bmp);
}

CloseHandle(hFile);
}
};
#pragma endregion

DWORD WINAPI CheckDLLFunction(const wchar_t* path, const char* funcName)
{
#define MakePointer(t, p, offset) ((t)((PUINT8)(p) + offset))
Expand Down
7 changes: 7 additions & 0 deletions thprac/src/thprac/thprac_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,13 @@ inline long RoundUp(long n, long m)

#pragma endregion

#pragma region Snapshot
namespace THSnapshot {
void* GetSnapshotData(IDirect3DDevice8* d3d8);
void Snapshot(IDirect3DDevice8* d3d8);
}
#pragma endregion

template <typename T>
static std::function<T(void)> GetRndGenerator(T min, T max, std::mt19937::result_type seed = 0)
{
Expand Down

0 comments on commit 3518bf3

Please sign in to comment.