Skip to content

Commit 95ad428

Browse files
committed
Include d3dx9 tools in module
1 parent 6c83586 commit 95ad428

File tree

12 files changed

+194
-120
lines changed

12 files changed

+194
-120
lines changed

Common/AutoUpdate.cpp

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
#define WIN32_LEAN_AND_MEAN
1818
#include <Windows.h>
19-
#include <Shldisp.h>
20-
#include <shlwapi.h>
2119
#include <shellapi.h>
2220
#include <urlmon.h>
2321
#include <sstream>
@@ -128,101 +126,6 @@ bool GetURLString(char*URL, std::string &data)
128126
return true;
129127
}
130128

131-
HRESULT UnZipFile(BSTR sourceZip, BSTR destFolder)
132-
{
133-
HRESULT hr = E_FAIL;
134-
IShellDispatch *pISD = nullptr;
135-
Folder *pToFolder = nullptr;
136-
Folder *pFromFolder = nullptr;
137-
138-
do
139-
{
140-
if (FAILED(CoInitialize(NULL)))
141-
{
142-
Logging::Log() << __FUNCTION__ " Failed to CoInitialize!";
143-
break;
144-
}
145-
146-
if (FAILED(CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&pISD)) || !pISD)
147-
{
148-
Logging::Log() << __FUNCTION__ " Failed to CoCreateInstance!";
149-
break;
150-
}
151-
152-
VARIANT vFile;
153-
VariantInit(&vFile);
154-
vFile.vt = VT_BSTR;
155-
vFile.bstrVal = sourceZip;
156-
157-
if (FAILED(pISD->NameSpace(vFile, &pFromFolder)))
158-
{
159-
Logging::Log() << __FUNCTION__ " Failed to get source NameSpace! " << sourceZip;
160-
break;
161-
}
162-
163-
VARIANT vDir;
164-
VariantInit(&vDir);
165-
vDir.vt = VT_BSTR;
166-
vDir.bstrVal = destFolder;
167-
if (!PathFileExists(destFolder))
168-
{
169-
CreateDirectory(destFolder, nullptr);
170-
}
171-
172-
// Destination is our zip file
173-
if (FAILED(pISD->NameSpace(vDir, &pToFolder)) || !pToFolder)
174-
{
175-
Logging::Log() << __FUNCTION__ " Failed to get destination NameSpace! " << destFolder;
176-
break;
177-
}
178-
179-
FolderItems *fi = nullptr;
180-
if (FAILED(pFromFolder->Items(&fi)))
181-
{
182-
Logging::Log() << __FUNCTION__ " Failed to get file list from zip file!";
183-
break;
184-
}
185-
186-
VARIANT vOpt;
187-
VariantInit(&vOpt);
188-
vOpt.vt = VT_I4;
189-
vOpt.lVal = FOF_NO_UI;
190-
191-
VARIANT newV;
192-
VariantInit(&newV);
193-
newV.vt = VT_DISPATCH;
194-
newV.pdispVal = fi;
195-
196-
if (FAILED(pToFolder->CopyHere(newV, vOpt)))
197-
{
198-
Logging::Log() << __FUNCTION__ " Failed to extract files out of zip file!";
199-
break;
200-
}
201-
202-
hr = S_OK;
203-
}
204-
while (false);
205-
206-
if (pToFolder)
207-
{
208-
pToFolder->Release();
209-
}
210-
211-
if (pFromFolder)
212-
{
213-
pFromFolder->Release();
214-
}
215-
216-
if (pISD)
217-
{
218-
pISD->Release();
219-
}
220-
221-
CoUninitialize();
222-
223-
return hr;
224-
}
225-
226129
DWORD MatchCount(std::string &path1, std::string &path2)
227130
{
228131
DWORD size = min(path1.size(), path2.size());

Common/LoadModules.cpp

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,15 @@
2121
#include <Windows.h>
2222
#include <Shlwapi.h>
2323
#include <vector>
24+
#include "Resources\sh2-enhce.h"
2425
#include "LoadModules.h"
25-
#include "External\Hooking\Hook.h"
26-
#include "FileSystemHooks.h"
26+
#include "Patches\Patches.h"
2727
#include "Settings.h"
2828
#include "Utils.h"
2929
#include "Logging\Logging.h"
3030

31-
int n = 0;
32-
LPCWSTR exTempPath = L"~tmp_sh2_enhce";
33-
3431
typedef void(WINAPI *PFN_InitializeASI)(void);
3532

36-
3733
// Find asi plugins to load
3834
void FindFiles(WIN32_FIND_DATA* fd)
3935
{
@@ -132,6 +128,72 @@ void InitializeASI(HMODULE hModule)
132128
p_InitializeASI();
133129
}
134130

131+
void ExtractD3DX9Tools()
132+
{
133+
do {
134+
// Get Silent Hill 2 file path
135+
wchar_t sh2path[MAX_PATH];
136+
if (!GetSH2FolderPath(sh2path, MAX_PATH))
137+
{
138+
break;
139+
}
140+
141+
// Remove Silent Hill 2 process name from path
142+
wchar_t *pdest = wcsrchr(sh2path, '\\');
143+
if (pdest)
144+
{
145+
wcscpy_s(pdest, MAX_PATH - wcslen(sh2path), L"\\");
146+
}
147+
148+
// Append virtual store and Silent Hill 2 to local appdata path
149+
wchar_t d3dx9zip[MAX_PATH];
150+
wcscpy(d3dx9zip, sh2path);
151+
if (!PathAppend(d3dx9zip, L"D3DX9.zip"))
152+
{
153+
break;
154+
}
155+
156+
HRSRC hResource = FindResource(m_hModule, MAKEINTRESOURCE(IDR_D3DX9_TOOLS), RT_RCDATA);
157+
if (hResource)
158+
{
159+
HGLOBAL hLoadedResource = LoadResource(m_hModule, hResource);
160+
if (hLoadedResource)
161+
{
162+
LPVOID pLockedResource = LockResource(hLoadedResource);
163+
if (pLockedResource)
164+
{
165+
DWORD dwResourceSize = SizeofResource(m_hModule, hResource);
166+
if (dwResourceSize != 0)
167+
{
168+
Logging::Log() << "Extracting the " << d3dx9zip << " file...";
169+
170+
std::fstream fsModule;
171+
fsModule.open(d3dx9zip, std::ios_base::out | std::ios_base::binary);
172+
if (fsModule.is_open())
173+
{
174+
// Write zip file to disk
175+
fsModule.write((char*)pLockedResource, dwResourceSize);
176+
fsModule.close();
177+
// Extract zip file
178+
UnZipFile(d3dx9zip, sh2path);
179+
// Delete zip file
180+
DeleteFile(d3dx9zip);
181+
// Return
182+
return;
183+
}
184+
else
185+
{
186+
RelaunchSilentHill2();
187+
}
188+
}
189+
}
190+
}
191+
}
192+
} while (false);
193+
194+
Logging::Log() << __FUNCTION__ << " Error: could not extract the 'D3DX9.zip' file!";
195+
}
196+
135197
HRESULT DeleteAllfiles(LPCWSTR lpFolder)
136198
{
137199
WIN32_FIND_DATA FindFileData;

Common/LoadModules.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
void LoadASIPlugins(bool LoadFromScriptsOnly);
44
void InitializeASI(HMODULE hModule);
5+
void ExtractD3DX9Tools();
56
HRESULT DeleteAllfiles(LPCWSTR lpFolder);

Common/Utils.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#define WIN32_LEAN_AND_MEAN
1818
#include <Windows.h>
19+
#include <Shldisp.h>
20+
#include <shellapi.h>
1921
#include <atlstr.h>
2022
#include <string>
2123
#include <iostream>
@@ -481,6 +483,100 @@ void GetFileSize(uintmax_t fsize, char *strOutput, size_t size)
481483
strcpy_s(strOutput, size, strSize.c_str());
482484
}
483485

486+
HRESULT UnZipFile(BSTR sourceZip, BSTR destFolder)
487+
{
488+
HRESULT hr = E_FAIL;
489+
IShellDispatch *pISD = nullptr;
490+
Folder *pToFolder = nullptr;
491+
Folder *pFromFolder = nullptr;
492+
493+
do
494+
{
495+
if (FAILED(CoInitialize(NULL)))
496+
{
497+
Logging::Log() << __FUNCTION__ " Failed to CoInitialize!";
498+
break;
499+
}
500+
501+
if (FAILED(CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&pISD)) || !pISD)
502+
{
503+
Logging::Log() << __FUNCTION__ " Failed to CoCreateInstance!";
504+
break;
505+
}
506+
507+
VARIANT vFile;
508+
VariantInit(&vFile);
509+
vFile.vt = VT_BSTR;
510+
vFile.bstrVal = sourceZip;
511+
512+
if (FAILED(pISD->NameSpace(vFile, &pFromFolder)))
513+
{
514+
Logging::Log() << __FUNCTION__ " Failed to get source NameSpace! " << sourceZip;
515+
break;
516+
}
517+
518+
VARIANT vDir;
519+
VariantInit(&vDir);
520+
vDir.vt = VT_BSTR;
521+
vDir.bstrVal = destFolder;
522+
if (!PathFileExists(destFolder))
523+
{
524+
CreateDirectory(destFolder, nullptr);
525+
}
526+
527+
// Destination is our zip file
528+
if (FAILED(pISD->NameSpace(vDir, &pToFolder)) || !pToFolder)
529+
{
530+
Logging::Log() << __FUNCTION__ " Failed to get destination NameSpace! " << destFolder;
531+
break;
532+
}
533+
534+
FolderItems *fi = nullptr;
535+
if (FAILED(pFromFolder->Items(&fi)))
536+
{
537+
Logging::Log() << __FUNCTION__ " Failed to get file list from zip file!";
538+
break;
539+
}
540+
541+
VARIANT vOpt;
542+
VariantInit(&vOpt);
543+
vOpt.vt = VT_I4;
544+
vOpt.lVal = FOF_NO_UI;
545+
546+
VARIANT newV;
547+
VariantInit(&newV);
548+
newV.vt = VT_DISPATCH;
549+
newV.pdispVal = fi;
550+
551+
if (FAILED(pToFolder->CopyHere(newV, vOpt)))
552+
{
553+
Logging::Log() << __FUNCTION__ " Failed to extract files out of zip file!";
554+
break;
555+
}
556+
557+
hr = S_OK;
558+
} while (false);
559+
560+
if (pToFolder)
561+
{
562+
pToFolder->Release();
563+
}
564+
565+
if (pFromFolder)
566+
{
567+
pFromFolder->Release();
568+
}
569+
570+
if (pISD)
571+
{
572+
pISD->Release();
573+
}
574+
575+
CoUninitialize();
576+
577+
return hr;
578+
}
579+
484580
bool GetModulePath(char *path, rsize_t size)
485581
{
486582
static char modpath[MAX_PATH] = { '\0' };

Common/Utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <vector>
4+
#include <Shldisp.h>
45

56
void *GetAddressOfData(const void *data, size_t len, DWORD step = 1);
67
void *GetAddressOfData(const void *data, size_t len, DWORD step, DWORD start, DWORD distance = 0x0FFFFFFF);
@@ -18,6 +19,7 @@ void SetSingleCoreAffinity();
1819
void AddHandleToVector(HMODULE dll);
1920
void UnloadAllModules();
2021
DWORD ConvertFloat(float num);
22+
HRESULT UnZipFile(BSTR sourceZip, BSTR destFolder);
2123
bool GetModulePath(char *path, rsize_t size);
2224
bool GetModulePath(wchar_t *path, rsize_t size);
2325
bool GetSH2FolderPath(char *path, rsize_t size);

Patches/LaunchAsAdmin.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,19 @@ void CheckArgumentsForPID()
8686
}
8787

8888
// Relaunch Silent Hill 2 with administrator rights
89-
void RelaunchSilentHill2(const wchar_t *sh2path)
89+
void RelaunchSilentHill2()
9090
{
91+
// Only relaunch once
92+
static bool RunOnce = false;
93+
if (ProcessRelaunched || RunOnce)
94+
{
95+
return;
96+
}
97+
RunOnce = true;
98+
9199
// Check Silent Hill 2 file path
92-
if (!ProcessRelaunched && PathFileExistsW(sh2path))
100+
wchar_t sh2path[MAX_PATH];
101+
if (GetSH2FolderPath(sh2path, MAX_PATH))
93102
{
94103
// Get pid
95104
wchar_t parameter[40] = { '\0' };
@@ -123,14 +132,14 @@ void CheckAdminAccess()
123132
// Check if restart is needed
124133
if (NeedsRestart)
125134
{
126-
RelaunchSilentHill2(sh2path);
135+
RelaunchSilentHill2();
127136
return;
128137
}
129138

130139
// Check compatibility options
131140
if (CheckCompatibilityMode && GetEnvironmentVariableA("__COMPAT_LAYER", nullptr, 0))
132141
{
133-
RelaunchSilentHill2(sh2path);
142+
RelaunchSilentHill2();
134143
return;
135144
}
136145

@@ -150,7 +159,7 @@ void CheckAdminAccess()
150159
}
151160
else
152161
{
153-
RelaunchSilentHill2(sh2path);
162+
RelaunchSilentHill2();
154163
return;
155164
}
156165
}

Patches/Patches.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ DWORD *GetLoadingScreenPointer();
5353

5454
// Function patch declaration
5555
void CheckArgumentsForPID();
56+
void RelaunchSilentHill2();
5657
void CheckAdminAccess();
5758
void RemoveVirtualStoreFiles();
5859
void RemoveCompatibilityMode();

Resources/BuildNo.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define BUILD_NUMBER 1859
1+
#define BUILD_NUMBER 1860

Resources/D3DX9.zip

1.75 MB
Binary file not shown.

Resources/UALx86.rc

80 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)