Skip to content

Commit

Permalink
Include d3dx9 tools in module
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Feb 1, 2021
1 parent 6c83586 commit 95ad428
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 120 deletions.
97 changes: 0 additions & 97 deletions Common/AutoUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Shldisp.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <urlmon.h>
#include <sstream>
Expand Down Expand Up @@ -128,101 +126,6 @@ bool GetURLString(char*URL, std::string &data)
return true;
}

HRESULT UnZipFile(BSTR sourceZip, BSTR destFolder)
{
HRESULT hr = E_FAIL;
IShellDispatch *pISD = nullptr;
Folder *pToFolder = nullptr;
Folder *pFromFolder = nullptr;

do
{
if (FAILED(CoInitialize(NULL)))
{
Logging::Log() << __FUNCTION__ " Failed to CoInitialize!";
break;
}

if (FAILED(CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&pISD)) || !pISD)
{
Logging::Log() << __FUNCTION__ " Failed to CoCreateInstance!";
break;
}

VARIANT vFile;
VariantInit(&vFile);
vFile.vt = VT_BSTR;
vFile.bstrVal = sourceZip;

if (FAILED(pISD->NameSpace(vFile, &pFromFolder)))
{
Logging::Log() << __FUNCTION__ " Failed to get source NameSpace! " << sourceZip;
break;
}

VARIANT vDir;
VariantInit(&vDir);
vDir.vt = VT_BSTR;
vDir.bstrVal = destFolder;
if (!PathFileExists(destFolder))
{
CreateDirectory(destFolder, nullptr);
}

// Destination is our zip file
if (FAILED(pISD->NameSpace(vDir, &pToFolder)) || !pToFolder)
{
Logging::Log() << __FUNCTION__ " Failed to get destination NameSpace! " << destFolder;
break;
}

FolderItems *fi = nullptr;
if (FAILED(pFromFolder->Items(&fi)))
{
Logging::Log() << __FUNCTION__ " Failed to get file list from zip file!";
break;
}

VARIANT vOpt;
VariantInit(&vOpt);
vOpt.vt = VT_I4;
vOpt.lVal = FOF_NO_UI;

VARIANT newV;
VariantInit(&newV);
newV.vt = VT_DISPATCH;
newV.pdispVal = fi;

if (FAILED(pToFolder->CopyHere(newV, vOpt)))
{
Logging::Log() << __FUNCTION__ " Failed to extract files out of zip file!";
break;
}

hr = S_OK;
}
while (false);

if (pToFolder)
{
pToFolder->Release();
}

if (pFromFolder)
{
pFromFolder->Release();
}

if (pISD)
{
pISD->Release();
}

CoUninitialize();

return hr;
}

DWORD MatchCount(std::string &path1, std::string &path2)
{
DWORD size = min(path1.size(), path2.size());
Expand Down
74 changes: 68 additions & 6 deletions Common/LoadModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@
#include <Windows.h>
#include <Shlwapi.h>
#include <vector>
#include "Resources\sh2-enhce.h"
#include "LoadModules.h"
#include "External\Hooking\Hook.h"
#include "FileSystemHooks.h"
#include "Patches\Patches.h"
#include "Settings.h"
#include "Utils.h"
#include "Logging\Logging.h"

int n = 0;
LPCWSTR exTempPath = L"~tmp_sh2_enhce";

typedef void(WINAPI *PFN_InitializeASI)(void);


// Find asi plugins to load
void FindFiles(WIN32_FIND_DATA* fd)
{
Expand Down Expand Up @@ -132,6 +128,72 @@ void InitializeASI(HMODULE hModule)
p_InitializeASI();
}

void ExtractD3DX9Tools()
{
do {
// Get Silent Hill 2 file path
wchar_t sh2path[MAX_PATH];
if (!GetSH2FolderPath(sh2path, MAX_PATH))
{
break;
}

// Remove Silent Hill 2 process name from path
wchar_t *pdest = wcsrchr(sh2path, '\\');
if (pdest)
{
wcscpy_s(pdest, MAX_PATH - wcslen(sh2path), L"\\");
}

// Append virtual store and Silent Hill 2 to local appdata path
wchar_t d3dx9zip[MAX_PATH];
wcscpy(d3dx9zip, sh2path);
if (!PathAppend(d3dx9zip, L"D3DX9.zip"))
{
break;
}

HRSRC hResource = FindResource(m_hModule, MAKEINTRESOURCE(IDR_D3DX9_TOOLS), RT_RCDATA);
if (hResource)
{
HGLOBAL hLoadedResource = LoadResource(m_hModule, hResource);
if (hLoadedResource)
{
LPVOID pLockedResource = LockResource(hLoadedResource);
if (pLockedResource)
{
DWORD dwResourceSize = SizeofResource(m_hModule, hResource);
if (dwResourceSize != 0)
{
Logging::Log() << "Extracting the " << d3dx9zip << " file...";

std::fstream fsModule;
fsModule.open(d3dx9zip, std::ios_base::out | std::ios_base::binary);
if (fsModule.is_open())
{
// Write zip file to disk
fsModule.write((char*)pLockedResource, dwResourceSize);
fsModule.close();
// Extract zip file
UnZipFile(d3dx9zip, sh2path);
// Delete zip file
DeleteFile(d3dx9zip);
// Return
return;
}
else
{
RelaunchSilentHill2();
}
}
}
}
}
} while (false);

Logging::Log() << __FUNCTION__ << " Error: could not extract the 'D3DX9.zip' file!";
}

HRESULT DeleteAllfiles(LPCWSTR lpFolder)
{
WIN32_FIND_DATA FindFileData;
Expand Down
1 change: 1 addition & 0 deletions Common/LoadModules.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

void LoadASIPlugins(bool LoadFromScriptsOnly);
void InitializeASI(HMODULE hModule);
void ExtractD3DX9Tools();
HRESULT DeleteAllfiles(LPCWSTR lpFolder);
96 changes: 96 additions & 0 deletions Common/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <Shldisp.h>
#include <shellapi.h>
#include <atlstr.h>
#include <string>
#include <iostream>
Expand Down Expand Up @@ -481,6 +483,100 @@ void GetFileSize(uintmax_t fsize, char *strOutput, size_t size)
strcpy_s(strOutput, size, strSize.c_str());
}

HRESULT UnZipFile(BSTR sourceZip, BSTR destFolder)
{
HRESULT hr = E_FAIL;
IShellDispatch *pISD = nullptr;
Folder *pToFolder = nullptr;
Folder *pFromFolder = nullptr;

do
{
if (FAILED(CoInitialize(NULL)))
{
Logging::Log() << __FUNCTION__ " Failed to CoInitialize!";
break;
}

if (FAILED(CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&pISD)) || !pISD)
{
Logging::Log() << __FUNCTION__ " Failed to CoCreateInstance!";
break;
}

VARIANT vFile;
VariantInit(&vFile);
vFile.vt = VT_BSTR;
vFile.bstrVal = sourceZip;

if (FAILED(pISD->NameSpace(vFile, &pFromFolder)))
{
Logging::Log() << __FUNCTION__ " Failed to get source NameSpace! " << sourceZip;
break;
}

VARIANT vDir;
VariantInit(&vDir);
vDir.vt = VT_BSTR;
vDir.bstrVal = destFolder;
if (!PathFileExists(destFolder))
{
CreateDirectory(destFolder, nullptr);
}

// Destination is our zip file
if (FAILED(pISD->NameSpace(vDir, &pToFolder)) || !pToFolder)
{
Logging::Log() << __FUNCTION__ " Failed to get destination NameSpace! " << destFolder;
break;
}

FolderItems *fi = nullptr;
if (FAILED(pFromFolder->Items(&fi)))
{
Logging::Log() << __FUNCTION__ " Failed to get file list from zip file!";
break;
}

VARIANT vOpt;
VariantInit(&vOpt);
vOpt.vt = VT_I4;
vOpt.lVal = FOF_NO_UI;

VARIANT newV;
VariantInit(&newV);
newV.vt = VT_DISPATCH;
newV.pdispVal = fi;

if (FAILED(pToFolder->CopyHere(newV, vOpt)))
{
Logging::Log() << __FUNCTION__ " Failed to extract files out of zip file!";
break;
}

hr = S_OK;
} while (false);

if (pToFolder)
{
pToFolder->Release();
}

if (pFromFolder)
{
pFromFolder->Release();
}

if (pISD)
{
pISD->Release();
}

CoUninitialize();

return hr;
}

bool GetModulePath(char *path, rsize_t size)
{
static char modpath[MAX_PATH] = { '\0' };
Expand Down
2 changes: 2 additions & 0 deletions Common/Utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <vector>
#include <Shldisp.h>

void *GetAddressOfData(const void *data, size_t len, DWORD step = 1);
void *GetAddressOfData(const void *data, size_t len, DWORD step, DWORD start, DWORD distance = 0x0FFFFFFF);
Expand All @@ -18,6 +19,7 @@ void SetSingleCoreAffinity();
void AddHandleToVector(HMODULE dll);
void UnloadAllModules();
DWORD ConvertFloat(float num);
HRESULT UnZipFile(BSTR sourceZip, BSTR destFolder);
bool GetModulePath(char *path, rsize_t size);
bool GetModulePath(wchar_t *path, rsize_t size);
bool GetSH2FolderPath(char *path, rsize_t size);
Expand Down
19 changes: 14 additions & 5 deletions Patches/LaunchAsAdmin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,19 @@ void CheckArgumentsForPID()
}

// Relaunch Silent Hill 2 with administrator rights
void RelaunchSilentHill2(const wchar_t *sh2path)
void RelaunchSilentHill2()
{
// Only relaunch once
static bool RunOnce = false;
if (ProcessRelaunched || RunOnce)
{
return;
}
RunOnce = true;

// Check Silent Hill 2 file path
if (!ProcessRelaunched && PathFileExistsW(sh2path))
wchar_t sh2path[MAX_PATH];
if (GetSH2FolderPath(sh2path, MAX_PATH))
{
// Get pid
wchar_t parameter[40] = { '\0' };
Expand Down Expand Up @@ -123,14 +132,14 @@ void CheckAdminAccess()
// Check if restart is needed
if (NeedsRestart)
{
RelaunchSilentHill2(sh2path);
RelaunchSilentHill2();
return;
}

// Check compatibility options
if (CheckCompatibilityMode && GetEnvironmentVariableA("__COMPAT_LAYER", nullptr, 0))
{
RelaunchSilentHill2(sh2path);
RelaunchSilentHill2();
return;
}

Expand All @@ -150,7 +159,7 @@ void CheckAdminAccess()
}
else
{
RelaunchSilentHill2(sh2path);
RelaunchSilentHill2();
return;
}
}
Expand Down
1 change: 1 addition & 0 deletions Patches/Patches.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ DWORD *GetLoadingScreenPointer();

// Function patch declaration
void CheckArgumentsForPID();
void RelaunchSilentHill2();
void CheckAdminAccess();
void RemoveVirtualStoreFiles();
void RemoveCompatibilityMode();
Expand Down
2 changes: 1 addition & 1 deletion Resources/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 1859
#define BUILD_NUMBER 1860
Binary file added Resources/D3DX9.zip
Binary file not shown.
Binary file modified Resources/UALx86.rc
Binary file not shown.
Loading

0 comments on commit 95ad428

Please sign in to comment.