📄 ogc-loader.cpp
字号:
#include <windows.h>
#include "stdio.h"
#include "ForceLib.h"
#pragma pack(1) // very important !
HINSTANCE mainInstance;
bool fileExists(const char* filename)
{
WIN32_FIND_DATA finddata;
HANDLE handle = FindFirstFile(filename,&finddata);
return (handle!=INVALID_HANDLE_VALUE);
}
//#define OGC_DEBUG
#undef OGC_DEBUG
//==============================================================================
bool promptForFile( char* retFile, const char* tip, char* filter, char* title, bool save, HWND hWnd )
{
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.hInstance = mainInstance;
ofn.nFilterIndex = 1;
ofn.lpstrFile = retFile;
ofn.nMaxFile = 256;
ofn.lpstrInitialDir = tip;
//====
ofn.lpstrFilter = filter;
ofn.lpstrTitle = title;
strcpy( retFile, tip );
bool status;
if( save )
{
ofn.Flags = OFN_OVERWRITEPROMPT;
status = (GetSaveFileName(&ofn)!=0);
} else
{
ofn.Flags = OFN_FILEMUSTEXIST;
status = (GetOpenFileName(&ofn)!=0);
}
return status;
}
STARTUPINFO SI;
PROCESS_INFORMATION PI;
//==================================================================================
bool isApplogAlreadyDisabled()
{
HKEY l_hKey;
DWORD l_UseProfile;
DWORD l_dwBufLen = 4;
DWORD l_ret = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Defrag\\AppStartParams",
0,KEY_QUERY_VALUE, &l_hKey);
if(l_ret!=ERROR_SUCCESS) { RegCloseKey(l_hKey); return false; }
l_ret = RegQueryValueEx(l_hKey,"UseProfile",NULL,NULL,(LPBYTE)&l_UseProfile,&l_dwBufLen);
if(l_ret!=ERROR_SUCCESS) { RegCloseKey(l_hKey); return false; }
RegCloseKey(l_hKey);
return (l_UseProfile==0);
}
//==================================================================================
bool isOnWindowsDrive(char* file)
{
// check for windows drive = hl drive
char windir[400];
GetWindowsDirectory(windir,390);
if( file[1]==':' && windir[1]==':' && tolower(file[0])==tolower(windir[0]) ) return true;
else return false;
}
//==================================================================================
void unHideApplog()
{
char applogdir[400];
GetWindowsDirectory(applogdir,390);
strcat(applogdir,"\\Applog");
SetFileAttributes( applogdir, FILE_ATTRIBUTE_NORMAL );
}
////==================================================================================
//void ErrorExit(char* text)
//{
// MessageBox(0,text,0,0);
// ExitProcess(1);
//}
//
////==================================================================================
//void disableApplog()
//{
// HKEY hk;
//
// if (RegCreateKey(
// HKEY_LOCAL_MACHINE,
// "Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Defrag\\AppStartParams",&hk
// ) != ERROR_SUCCESS )
// {
// ErrorExit("Registry access error. 1");
// }
//
// DWORD NewValue = 0;
// if (RegSetValueEx(hk,"UseProfile",0,REG_DWORD,(BYTE*)&NewValue,4) != ERROR_SUCCESS)
// {
// ErrorExit("Registry access error. 2");
// }
//}
////==================================================================================
//void cleanupApplog()
//{
// WIN32_FIND_DATA findData;
// HANDLE hFindFile;
// char* fileName = findData.cFileName;
//
// char searchPattern[400];
// GetWindowsDirectory(searchPattern,390);
// strcat(searchPattern,"\\Applog\\*.*");
//
// hFindFile = FindFirstFile( searchPattern, &findData );
// if( hFindFile == INVALID_HANDLE_VALUE){ return; }
// do{
// if(strcmp(fileName,".") && strcmp(fileName,".."))
// {
// char fullname[400];
// strcpy(fullname,searchPattern);
// strcpy(fullname + strlen(fullname)-3,fileName);
// DeleteFile(fullname);
// }
// } while( FindNextFile(hFindFile,&findData) );
//}
////==================================================================================
void applogFix()
{
// check for NT
bool IsNT = ((HIWORD(::GetVersion()) & 0x8000) == 0);
#ifdef OGC_DEBUG
IsNT = false;
#endif
if(IsNT) return;
// make it easier for users to find applog
unHideApplog();
// if(!isApplogAlreadyDisabled())
// {
// DWORD ret = MessageBox( 0,
// "OGC Hook has detected that you are running\n"
// "Windows 98 or Windows ME, and have Half-Life installed\n"
// "on the same drive as your Operating System.\n"
// "In order to avoid server side detection by scanning\n"
// "for applog files, OGC Hook can disable the Applog Feature.\n"
// "\nDo you want to disable Applog? (required)"
// ,
// "[OGC] Applog Disabling",
// MB_YESNO|MB_ICONQUESTION
// );
// if( ret != IDYES )
// {
// MessageBox(0,
// "You've chosen to leave Applog enabled.\n"
// "If you want to uninstall OGC Hook, you \n"
// "might want to browse to your WINDOWS\\Applog\n"
// "folder and delete OGC related files manually\n\n"
// "OGC Hook will now quit!\n"
// , "[OGC] Info", MB_OK );
// ExitProcess(0);
// }
//
// // deleting a few files can take ages in win9x. inform the user.
// MessageBox(0,"OGC Hook will now disable Applog"
// " file creation,\nand cleanup the WINDOWS\\Applog folder\n"
// "This may take some time" , "[OGC] Info",MB_OK);
//
// disableApplog();
// cleanupApplog();
//
// MessageBox(0,"OGC Hook is done with Applog \n"
// "cleanup. Half-Life will now be startetd.",
// "[OGC] Info",MB_OK);
// }
// else
// {
// cleanupApplog();
// }
}
//==================================================================================
void removeTrailingCRLF(char* a)
{
while(a[strlen(a)-1]=='\x0D' || a[strlen(a)-1]=='\x0A' ) a[strlen(a)-1]=0;
}
//==================================================================================
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
mainInstance = hInstance;
// setup
char hookEXE [400] = ""; // "c:\\program files\\ogc hook\\notepad.exe"
char hookDLL [400] = ""; // "c:\\program files\\ogc hook\\notepad.dll"
char hookINI [400] = ""; // "c:\\program files\\ogc hook\\notepad.ini"
char hlCmdLine[400] = "";
char hlEXE [400] = "";
GetModuleFileName(0,hookEXE,390);
int len = strlen(hookEXE);
strcpy(hookDLL,hookEXE);
strcpy(hookINI,hookEXE);
hookDLL[len-3]='d';hookDLL[len-2]='l';hookDLL[len-1]='l';
hookINI[len-3]='i';hookINI[len-2]='n';hookINI[len-1]='i';
// file check
#ifndef OGC_DEBUG
if( !fileExists(hookDLL) ) {
MessageBox(0,hookDLL,"DLL NOT FOUND",MB_ICONERROR|MB_TOPMOST);
return 1;
}
#endif
// try to get hl path out of the ini
bool success = false;
do{
FILE* file = fopen(hookINI,"r");
if(file)
{
fgets(hlEXE ,390,file);hlEXE[398]=0; removeTrailingCRLF(hlEXE);
fclose(file);
}
if(!fileExists(hlEXE))
{
MessageBox(0,"Please point me to your HL/CS executable,\nhl.exe or cstrike.exe","[OGC] Request",MB_ICONEXCLAMATION);
bool ok = promptForFile( hlEXE, "c:\\sierra\\half-life\\hl.exe",
"hl.exe or cstrike.exe\0hl.exe;cstrike.exe\0",
"Show me your HL/CS Directory", false, 0);
if(!ok) { return 0; }
file = fopen(hookINI,"w");
if(file)
{
fprintf(file,"%s\x0D\x0A",hlEXE);
fclose(file);
} else {
MessageBox(0,hookINI,"write error",0);
break;
}
if(fileExists(hlEXE)) success = true;
}
else
{
success = true;
}
} while(!success);
if( !strcmpi(hlEXE,hookEXE) )
{
MessageBox(0,"cannot start myself","Error",MB_ICONEXCLAMATION);
DeleteFile(hookINI);
return 1;
}
// disable applog for win9x
#ifndef OGC_DEBUG
if( isOnWindowsDrive(hlEXE) ) applogFix();
#else
applogFix();
#endif
// command line
if(!strstr(lpCmdLine,"-game")) strcpy(hlCmdLine," -game cstrike ");
strcat(hlCmdLine,lpCmdLine);
// start the shit !
ZeroMemory(&SI,sizeof(STARTUPINFO));
ZeroMemory(&PI,sizeof(PROCESS_INFORMATION));
SI.cb = sizeof(STARTUPINFO);
// set the right directory:
char hlDir[400];
strcpy(hlDir,hlEXE);
char* pos = hlDir+strlen(hlDir);
while(pos>=hlDir && *pos!='\\') --pos;
*pos = 0;
char* hlBaseFileName = pos+1;
SetCurrentDirectory(hlDir);
if (!CreateProcess(hlBaseFileName,hlCmdLine,NULL,NULL,FALSE,CREATE_SUSPENDED,NULL,NULL,&SI,&PI))
{
MessageBox(0,"Cannot create the Halflife Process!","[OGC] Error",MB_ICONERROR|MB_TOPMOST);
return -1;
}
// call the dll function
if (!ForceLibrary(hookDLL,&PI))
{
MessageBox(0,"Injection failed.","[OGC] Error",MB_ICONERROR|MB_TOPMOST);
TerminateProcess(PI.hProcess,-1);
return -1;
}
// let the main thread run
ResumeThread(PI.hThread);
// WaitForSingleObject(PI.hProcess,INFINITE);
// clean up
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -