Skip to content

Commit a40e3b1

Browse files
committed
Fixed Soku2 incompat, show a warning in the generated ini and fixed some messages
1 parent fe1f285 commit a40e3b1

File tree

3 files changed

+76
-57
lines changed

3 files changed

+76
-57
lines changed

swrstoys/SWRSToys.cpp

+10-55
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
#include <windows.h>
22
#include <cstdio>
33
#include <shlwapi.h>
4-
#include <crtdbg.h>
54
#include <nlohmann/json.hpp>
65
#include <locale>
76
#include <codecvt>
87
#include <fstream>
98
#include <filesystem>
10-
#include <direct.h>
11-
#include <minidumpapiset.h>
129
#include "Module.hpp"
1310
#include "ModConfigMenu.hpp"
1411
#include "SWRSToys.h"
1512

1613
#define CRenderer_Unknown1 ((void (__thiscall *)(int, int))0x404AF0)
17-
#define REAL_VERSION_STR "alpha 0.4.1"
14+
#define REAL_VERSION_STR "alpha 0.4.2"
1815
#ifdef _NOTEX
1916
#define MOD_REAL_VERSION_STR REAL_VERSION_STR " no texture"
2017
#else
@@ -41,7 +38,7 @@ Module *head = nullptr;
4138
SokuLib::SWRFont font;
4239
HMODULE myModule;
4340

44-
std::wstring getLastError(int err = GetLastError())
41+
std::wstring getLastError(int err)
4542
{
4643
wchar_t *s = nullptr;
4744
std::wstring str;
@@ -55,55 +52,6 @@ std::wstring getLastError(int err = GetLastError())
5552
return str;
5653
}
5754

58-
LONG WINAPI UnhandledExFilter(PEXCEPTION_POINTERS ExPtr)
59-
{
60-
if (!ExPtr) {
61-
puts("No ExPtr....");
62-
return 0;
63-
}
64-
puts("Caught fatal exception! Generating dump...");
65-
66-
wchar_t buf[2048];
67-
wchar_t buf2[MAX_PATH];
68-
time_t timer;
69-
char timebuffer[31];
70-
struct tm* tm_info;
71-
72-
time(&timer);
73-
tm_info = localtime(&timer);
74-
strftime(timebuffer, sizeof(timebuffer), "%Y-%m-%d-%H-%M-%S", tm_info);
75-
mkdir("crashes");
76-
wsprintfW(buf2, L"crashes/crash_%S.dmp", timebuffer);
77-
wsprintfW(buf, L"Game crashed!\nReceived fatal exception %X at address %x.\n", ExPtr->ExceptionRecord->ExceptionCode, ExPtr->ExceptionRecord->ExceptionAddress);
78-
79-
HANDLE hFile = CreateFileW(buf2, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
80-
81-
if (hFile != INVALID_HANDLE_VALUE) {
82-
MINIDUMP_EXCEPTION_INFORMATION md;
83-
md.ThreadId = GetCurrentThreadId();
84-
md.ExceptionPointers = ExPtr;
85-
md.ClientPointers = FALSE;
86-
BOOL win = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &md, nullptr, nullptr);
87-
88-
if (!win) {
89-
wcscat(buf, L"MiniDumpWriteDump failed.\n");
90-
wcscat(buf, getLastError().c_str());
91-
wcscat(buf, L": ");
92-
} else
93-
wcscat(buf, L"Minidump created ");
94-
wcscat(buf, buf2);
95-
CloseHandle(hFile);
96-
} else {
97-
wcscat(buf, L"CreateFileW(");
98-
wcscat(buf, buf2);
99-
wcscat(buf, L") failed: ");
100-
wcscat(buf, getLastError().c_str());
101-
}
102-
printf("%S\n", buf);
103-
MessageBoxW(nullptr, buf, L"Fatal Error", MB_ICONERROR);
104-
return EXCEPTION_CONTINUE_SEARCH;
105-
}
106-
10755
void loadAssets()
10856
{
10957
if (loaded)
@@ -221,6 +169,14 @@ void generateFakeIni()
221169
{
222170
std::ofstream stream{std::wstring(app_path) + L"\\SWRSToys.ini"};
223171

172+
stream << "; /!\\ DO NOT MODIFY /!\\" << std::endl;
173+
stream << "; This file is autogenerated by SokuModLoader." << std::endl;
174+
stream << "; Any change made here will be LOST." << std::endl;
175+
stream << "; If you want to modify the mod list, go inside the mod config menu in game." << std::endl;
176+
stream << "; If your game can't open, the file you are looking for is ModLoaderSettings.json." << std::endl;
177+
stream << "; To disable a mod there, write false next to \"enabled\"." << std::endl;
178+
stream << "; Alternatively, you can delete ModLoaderSettings.json so that" << std::endl;
179+
stream << "; on next boot, it gets regenerated from this very file (in which case modifying it is fine)." << std::endl;
224180
stream << "[Module]" << std::endl;
225181
for (size_t i = 0; i < modules.size(); i++) {
226182
if (!modules[i].enabled)
@@ -579,7 +535,6 @@ bool Hook(HMODULE this_module)
579535
puts("Loading settings file");
580536
loadSettings(gameHash, this_module);
581537
puts("Placing hooks");
582-
SetUnhandledExceptionFilter(UnhandledExFilter);
583538
VirtualProtect((PVOID)TEXT_SECTION_OFFSET, TEXT_SECTION_SIZE, PAGE_EXECUTE_WRITECOPY, &old);
584539
*(char *)0x81FF00 = 0x90;
585540
*(char *)0x81FF01 = 0x50;

swrstoys/SWRSToys.h

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
bool Hook(HMODULE this_module);
66
void saveSettings();
77
void displaySokuCursor(SokuLib::Vector2i pos, SokuLib::Vector2u size);
8+
std::wstring getLastError(int err = GetLastError());
89

910
extern SokuLib::SWRFont font;
1011
extern HMODULE myModule;

swrstoys/dummy.cpp

+65-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include "dummy.h"
22

3-
#include <Windows.h>
3+
#include <windows.h>
44
#include <SokuLib.hpp>
5+
#include <direct.h>
6+
#include <crtdbg.h>
7+
#include <minidumpapiset.h>
58
#include "SWRSToys.h"
69

710
FARPROC p_Direct3DShaderValidatorCreate9 = NULL;
@@ -22,14 +25,73 @@ FARPROC p_Direct3DCreate9Ex = NULL;
2225
HMODULE orig_module = NULL;
2326
HMODULE this_module = NULL;
2427

28+
int (*ogSecurityInitCookie)();
2529
int (*ogSokuMain)(int a, int b, int c, int d);
2630

31+
LONG WINAPI UnhandledExFilter(PEXCEPTION_POINTERS ExPtr)
32+
{
33+
if (!ExPtr) {
34+
puts("No ExPtr....");
35+
return EXCEPTION_CONTINUE_SEARCH;
36+
}
37+
puts("Caught fatal exception! Generating dump...");
38+
39+
wchar_t buf[8192];
40+
wchar_t buf2[MAX_PATH];
41+
time_t timer;
42+
char timebuffer[31];
43+
struct tm* tm_info;
44+
45+
time(&timer);
46+
tm_info = localtime(&timer);
47+
strftime(timebuffer, sizeof(timebuffer), "%Y-%m-%d-%H-%M-%S", tm_info);
48+
mkdir("crashes");
49+
wsprintfW(buf2, L"crashes/crash_%S.dmp", timebuffer);
50+
wsprintfW(buf, L"Game crashed!\nReceived exception %X at address %x.\n", ExPtr->ExceptionRecord->ExceptionCode, ExPtr->ExceptionRecord->ExceptionAddress);
51+
52+
HANDLE hFile = CreateFileW(buf2, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
53+
54+
if (hFile != INVALID_HANDLE_VALUE) {
55+
MINIDUMP_EXCEPTION_INFORMATION md;
56+
md.ThreadId = GetCurrentThreadId();
57+
md.ExceptionPointers = ExPtr;
58+
md.ClientPointers = FALSE;
59+
BOOL win = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &md, nullptr, nullptr);
60+
61+
if (!win) {
62+
wcscat(buf, L"MiniDumpWriteDump failed.\n");
63+
wcscat(buf, getLastError().c_str());
64+
wcscat(buf, L": ");
65+
wcscat(buf, buf2);
66+
} else {
67+
wcscat(buf, L"Minidump created ");
68+
wcscat(buf, buf2);
69+
wcscat(buf, L"\n\nPlease include this file, the list of mods enabled and a brief explanation of what you were doing when this happened in your bug report.");
70+
}
71+
CloseHandle(hFile);
72+
} else {
73+
wcscat(buf, L"CreateFileW(");
74+
wcscat(buf, buf2);
75+
wcscat(buf, L") failed: ");
76+
wcscat(buf, getLastError().c_str());
77+
}
78+
printf("%S\n", buf);
79+
MessageBoxW(nullptr, buf, L"Error", MB_ICONERROR);
80+
return EXCEPTION_CONTINUE_SEARCH;
81+
}
82+
2783
int SokuMain(int a, int b, int c, int d)
2884
{
29-
Hook(this_module);
85+
SetUnhandledExceptionFilter(UnhandledExFilter);
3086
return ogSokuMain(a, b, c, d);
3187
}
3288

89+
int mySecurityInitCookie()
90+
{
91+
Hook(this_module);
92+
return ogSecurityInitCookie();
93+
}
94+
3395
BOOL APIENTRY DllMain(HMODULE this_module_, DWORD ul_reason_for_call, LPVOID) {
3496
switch (ul_reason_for_call) {
3597
case DLL_PROCESS_ATTACH: {
@@ -63,6 +125,7 @@ BOOL APIENTRY DllMain(HMODULE this_module_, DWORD ul_reason_for_call, LPVOID) {
63125

64126
VirtualProtect((PVOID)TEXT_SECTION_OFFSET, TEXT_SECTION_SIZE, PAGE_EXECUTE_WRITECOPY, &old);
65127
ogSokuMain = SokuLib::TamperNearJmpOpr(0x821844, SokuMain);
128+
ogSecurityInitCookie = SokuLib::TamperNearJmpOpr(0x8218b2, mySecurityInitCookie);
66129
VirtualProtect((PVOID)TEXT_SECTION_OFFSET, TEXT_SECTION_SIZE, old, &old);
67130
break;
68131
}

0 commit comments

Comments
 (0)