Skip to content

Commit 3bb1ae5

Browse files
author
makipl
committed
Patch for 1.0.3.0- Thomas edition pre-release
1 parent 7de37da commit 3bb1ae5

File tree

3 files changed

+187
-190
lines changed

3 files changed

+187
-190
lines changed

ff8_demaster/coreHeader.h

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ extern DWORD DIRECT_IO_EXPORT_DIR_LEN;
3535

3636
extern bx::DefaultAllocator texAllocator;
3737

38+
#define uint unsigned int
39+
3840
using safe_bimg = std::unique_ptr<bimg::ImageContainer, decltype(&bimg::imageFree)>;
3941
safe_bimg safe_bimg_init(bimg::ImageContainer* img = nullptr);
4042
safe_bimg LoadImageFromFile(const char* const filename);

ff8_demaster/dllmain.cpp

+121-127
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,7 @@
77
#define CRASHLOG OutputDebug("%s::%d::%s\n", __FILE__, __LINE__,__func__)
88

99
/*
10-
KURSE ALL SEEDS!
11-
Fushururu
12-
Are you with the Garden Master or Cid?
13-
>be non-human
14-
>pay a lot of money to build BalambGarden
15-
>after that some useless guy is the boss
16-
>no one knows about you
17-
>you are living in the basement
18-
>alone
19-
20-
Yeah... So looks like money can't buy
21-
umm... well.. life? Hey, you are student
22-
in the garden built from my money
23-
and you were trained to be missionary
24-
and then you just walk in and kill me
25-
NICE JOB
10+
Thomas the tank Engine
2611
*/
2712

2813
/*
@@ -36,6 +21,7 @@ DWORD THIS_SIZE = 0;
3621
DWORD OPENGL_HANDLE = 0;
3722
const char* DIRECT_IO_EXPORT_DIR = "DEMASTER_EXP\\";
3823
std::unique_ptr<FILE, decltype(&fclose)> logFile{ nullptr, fclose };
24+
void checkGlew();
3925
DWORD* tex_header = 0;
4026
DWORD attr = -1;
4127
DWORD DIRECT_IO_EXPORT_DIR_LEN = -1;
@@ -52,6 +38,10 @@ BOOL LINEAR_PATCH, OPENGL_HOOK;
5238
int BATTLE_STAGE_ANIMATION_DELAY;
5339
BOOL BATTLE_STAGE_FORCE_RELOAD;
5440

41+
static bool glewInitialized = false;
42+
43+
static float frames = 0.0f;
44+
5545
void OutputDebug(const char* fmt, ...)
5646
{
5747

@@ -117,15 +107,103 @@ GLFWwindow* hookGlfwWindow(int width, int height, const char* title, GLFWmonitor
117107

118108
}
119109

110+
typedef void __stdcall _glViewport(GLint x, GLint y, GLsizei width, GLsizei height);
111+
112+
static LPVOID oglViewport;
113+
114+
void * __stdcall hookGlViewport(GLint x, GLint y, GLsizei width, GLsizei height)
115+
{
116+
checkGlew();
117+
static bool backdoorUsed = false;
118+
if(glewInitialized && !backdoorUsed)
119+
{
120+
LPVOID test1 = __glewBufferData;
121+
LPVOID test2 = glBufferData;
122+
//Maki: PUT BACKDOORS FOR EXTENSIONS HERE
123+
124+
backdoorUsed = true;
125+
}
126+
127+
return ((void* (__stdcall*)(GLint, GLint, GLsizei, GLsizei))oglViewport)(x, y, width, height);
128+
}
129+
130+
static LPVOID oglSwaPbuffers;
131+
132+
#define DEMFUNC(name,...) void * __stdcall hook##name##(##__VA_ARGS__##)
133+
134+
DEMFUNC(GlSwap, int);
135+
136+
137+
120138

121-
#define GETADDR(wntl) IMAGE_BASE + GetAddress(##wntl)
122139
void GetWindow()
123140
{
124-
unsigned int wndTitle = GETADDR(WINDOWTITLE);
141+
unsigned int wndTitle = IMAGE_BASE + GetAddress(WINDOWTITLE);
125142
_dllmainBackAddr2 = (DWORD)InjectJMP(wndTitle, (DWORD)_asm_ReplaceWindowTitle, 5);
126-
wndTitle = wndTitle + 0x12 + (*(DWORD*)(GETADDR(WINDOWTITLE) + 0x12)) + 4;
143+
uint wndGlfw = wndTitle + 0x12 + (*(DWORD*)(IMAGE_BASE + GetAddress(WINDOWTITLE) + 0x12)) + 4;
127144
//MH_CreateHook((LPVOID)wndTitle, hookGlfwWindow, glfwWindowTrampoline);
128-
MH_CreateHook((LPVOID)wndTitle, hookGlfwWindow, &glfwWindowTrampoline);
145+
MH_CreateHook((LPVOID)wndGlfw, hookGlfwWindow, &glfwWindowTrampoline); //Maki: We need trampoline here!
146+
MH_STATUS hookApiViewport = MH_CreateHookApi(L"OPENGL32", "glViewport", hookGlViewport, &oglViewport);
147+
}
148+
149+
//typedef void APIENTRY glClear(GLbitfield);
150+
//typedef void (__stdcall * glClear)(GLbitfield);
151+
//typedef void __stdcall * glClear(GLbitfield mask);
152+
typedef void(__stdcall* oglClear)(GLbitfield);
153+
typedef void(__stdcall* oglClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
154+
typedef void(__stdcall* oglBufferData)(GLenum target, GLsizeiptr size, const void* data, GLenum usage);
155+
156+
static oglClear _oglClear;
157+
static oglClearColor _oglClearColor;
158+
static oglBufferData _oglBufferData;
159+
160+
void __stdcall kglClear(GLbitfield mask)
161+
{
162+
_oglClear(mask);
163+
}
164+
165+
void __stdcall kglClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
166+
{
167+
//_oglClearColor(red, green, 1.0f, 1.0f); //Maki: Try it :D
168+
_oglClearColor(red, green, blue, alpha);
169+
170+
}
171+
172+
void __stdcall koglBufferData(GLenum target, GLsizeiptr size, const void* data, GLenum usage)
173+
{
174+
_oglBufferData(target, size, data, usage);
175+
}
176+
177+
static LPVOID* oglBindBuffer;
178+
179+
180+
//Maki: probably to be replaced by manual hook/minhook - it's just additional layer of doing the same things
181+
//Maki: btw- this works on independent thread- so screw your context xD although they should share it naturally
182+
int kieroThread()
183+
{
184+
if (kiero::init(kiero::RenderType::OpenGL) == kiero::Status::Success)
185+
{
186+
// define KIERO_USE_MINHOOK must be 1
187+
// the index of the required function can be found in the METHODSTABLE.txt
188+
//kiero::bind(10, (void**)&_oglClear, kglClear); //Maki: This makes Nsight crash
189+
//kiero::bind(12, (void**)&_oglClearColor, kglClearColor);
190+
//kiero::bind(336, (void**)&_oglBufferData, koglBufferData);
191+
//glewInit();
192+
//auto testing = glBufferData;
193+
//LPVOID orig;
194+
//MH_CreateHook(glBufferData, koglBufferData, &orig);
195+
//MH_EnableHook(MH_ALL_HOOKS);
196+
197+
198+
// MH_CreateHook(bufferTest, koglBufferData, oglBindBuffer);
199+
MH_EnableHook(MH_ALL_HOOKS);
200+
201+
202+
// If you just need to get the function address you can use the kiero::getMethodsTable function
203+
//_oglClear = (oglClear)kiero::getMethodsTable()[10];
204+
}
205+
206+
return 0;
129207
}
130208

131209
//DO NOT DELETE- it acts as an anchor for EFIGS.dll import
@@ -137,32 +215,6 @@ EXPORT void InitTest()
137215

138216
DWORD lastJMP;
139217

140-
void DEB_JMP(char* c, DWORD a, DWORD b, DWORD cc, DWORD d, DWORD e)
141-
{
142-
if (c < (char*)IMAGE_BASE)
143-
return;
144-
char localD[32];
145-
localD[0] = '\0';
146-
sprintf(localD, "Wrong address at: %08x\n", (unsigned int)c);
147-
if (IsBadReadPtr(c, 4))
148-
{
149-
__asm
150-
{
151-
//INT 3
152-
}
153-
return;
154-
}
155-
if (*c == 183)
156-
{
157-
__asm
158-
{
159-
INT 3
160-
}
161-
}
162-
OutputDebug(c, a, b, cc, d, e);
163-
return;
164-
}
165-
166218
__declspec(naked) void nullsub()
167219
{
168220
__asm
@@ -208,6 +260,8 @@ void ReadConfigFile()
208260
BATTLE_STAGE_FORCE_RELOAD = conf.GetInteger("", "BATTLE_STAGE_FORCE_RELOAD", 0);
209261
}
210262

263+
264+
//Maki: Why this doesn't work?
211265
LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS* ep)
212266
{
213267
DemasteredStackWalker sw;
@@ -229,25 +283,26 @@ LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS* ep)
229283
}
230284
safe_bimg safe_bimg_init(bimg::ImageContainer* img)
231285
{
232-
return { img, bimg::imageFree };
286+
return {img, bimg::imageFree};
233287
}
234-
safe_bimg LoadImageFromFile(const char* const filename)
235-
{
236-
static bool glewLoaded = false;
237288

238-
if (!glewLoaded)
239-
{
240-
glewLoaded = true;
241289

242-
// INIT GLEW - Add recent OpenGL extension support ( required for Texture Compression )
243-
GLenum err = glewInit();
244290

245-
if (GLEW_OK != err)
246-
{
247-
/* Problem: glewInit failed, something is seriously wrong. */
248-
OutputDebug("%s - GLEW Error: %s\n", __func__, glewGetErrorString(err));
249-
}
250-
}
291+
void checkGlew()
292+
{
293+
if (glewInitialized) return;
294+
295+
glewInitialized = true;
296+
GLenum err = glewInit();
297+
if (GLEW_OK != err)
298+
/* Problem: glewInit failed, something is seriously wrong. */
299+
OutputDebug("%s - GLEW Error: %s\n", __func__, glewGetErrorString(err));
300+
301+
}
302+
303+
safe_bimg LoadImageFromFile(const char* const filename)
304+
{
305+
checkGlew();
251306

252307
char msg[1024]{ 0 };
253308

@@ -304,18 +359,15 @@ bool DDSorPNG(char* buffer, size_t in_size, const char* fmt, ...)
304359
void RenderTexture(bimg::ImageContainer* img)
305360
{
306361
TextureFormatInfo& texInfo = s_textureFormat[img->m_format];
307-
if (bimg::isCompressed(img->m_format))
308-
{
309-
RenderCompressedTexture(img, texInfo);
310-
}
362+
if (isCompressed(img->m_format))
363+
RenderCompressedTexture(img, texInfo);
311364
else
312-
{
313-
RenderUncompressedTexture(img, texInfo);
314-
}
365+
RenderUncompressedTexture(img, texInfo);
315366
}
316367

317368
void RenderUncompressedTexture(bimg::ImageContainer* img, TextureFormatInfo& texInfo)
318369
{
370+
checkGlew();
319371
uint32_t width = img->m_width;
320372
uint32_t height = img->m_height;
321373
uint32_t depth = img->m_depth;
@@ -328,7 +380,7 @@ void RenderUncompressedTexture(bimg::ImageContainer* img, TextureFormatInfo& tex
328380
depth = 1;
329381

330382
bimg::ImageMip mip;
331-
if (bimg::imageGetRawData(*img, 0, lod + startLod, img->m_data, img->m_size, mip))
383+
if (imageGetRawData(*img, 0, lod + startLod, img->m_data, img->m_size, mip))
332384
glTexImage2D(GL_TEXTURE_2D, lod, texInfo.m_internalFmt, img->m_width, img->m_height, 0, texInfo.m_fmt, texInfo.m_type, mip.m_data);
333385
}
334386
}
@@ -357,64 +409,6 @@ void RenderCompressedTexture(bimg::ImageContainer* img, TextureFormatInfo& texIn
357409
OutputDebug("Texture is compressed, but compression is not supported on your GPU. Skipping draw.");
358410
}
359411

360-
//typedef void APIENTRY glClear(GLbitfield);
361-
//typedef void (__stdcall * glClear)(GLbitfield);
362-
//typedef void __stdcall * glClear(GLbitfield mask);
363-
typedef void(__stdcall* oglClear)(GLbitfield);
364-
typedef void(__stdcall* oglClearColor)(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
365-
typedef void(__stdcall* oglBufferData)(GLenum target, GLsizeiptr size, const void* data, GLenum usage);
366-
367-
static oglClear _oglClear;
368-
static oglClearColor _oglClearColor;
369-
static oglBufferData _oglBufferData;
370-
371-
void __stdcall kglClear(GLbitfield mask)
372-
{
373-
_oglClear(mask);
374-
}
375-
376-
void __stdcall kglClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
377-
{
378-
//_oglClearColor(red, green, 1.0f, 1.0f); //Maki: Try it :D
379-
_oglClearColor(red, green, blue, alpha);
380-
381-
}
382-
383-
void __stdcall koglBufferData(GLenum target, GLsizeiptr size, const void* data, GLenum usage)
384-
{
385-
_oglBufferData(target, size, data, usage);
386-
}
387-
388-
static LPVOID* oglBindBuffer;
389-
390-
391-
int kieroThread()
392-
{
393-
if (kiero::init(kiero::RenderType::OpenGL) == kiero::Status::Success)
394-
{
395-
// define KIERO_USE_MINHOOK must be 1
396-
// the index of the required function can be found in the METHODSTABLE.txt
397-
kiero::bind(10, (void**)&_oglClear, kglClear);
398-
kiero::bind(12, (void**)&_oglClearColor, kglClearColor);
399-
//kiero::bind(336, (void**)&_oglBufferData, koglBufferData);
400-
//glewInit();
401-
//auto testing = glBufferData;
402-
//LPVOID orig;
403-
//MH_CreateHook(glBufferData, koglBufferData, &orig);
404-
//MH_EnableHook(MH_ALL_HOOKS);
405-
406-
407-
// MH_CreateHook(bufferTest, koglBufferData, oglBindBuffer);
408-
MH_EnableHook(MH_ALL_HOOKS);
409-
410-
411-
// If you just need to get the function address you can use the kiero::getMethodsTable function
412-
//_oglClear = (oglClear)kiero::getMethodsTable()[10];
413-
}
414-
415-
return 0;
416-
}
417-
418412
BOOL WINAPI DllMain(
419413

420414
HINSTANCE hinstDLL, // handle to DLL module

0 commit comments

Comments
 (0)