1
1
/*
2
- Created by: Istari`.
3
- Description: runs the BFMEI with the supplied params taken from the "lotrbfme_params.txt" file. Made it to work with Gameranger .
4
- Version: 2.1
2
+ Created by: byt3m
3
+ Description: Executes Battle For Middle Earth with the supplied parameters. Works with gameranger .
4
+ Version: 2.2
5
5
*/
6
6
7
-
8
7
#include " pch.h"
9
8
10
-
11
9
using namespace std ;
12
10
13
-
14
- string ReadFile (string filepath)
11
+ #define MAXBUFCHARS 500
12
+ PROCESS_INFORMATION lpProcessInfo;
13
+ STARTUPINFOA lpStartupInfo;
14
+ char executionParameters[MAXBUFCHARS];
15
+ stringstream iniFile;
16
+ stringstream ssApplication;
17
+ stringstream ssCommandLine;
18
+
19
+ // Returns the last Win32 error, in string format. Returns an empty string if there is no error.
20
+ // https://stackoverflow.com/questions/1387064/how-to-get-the-error-message-from-the-error-code-returned-by-getlasterror
21
+ std::string GetLastErrorAsString ()
15
22
{
16
- fstream parametersfile;
17
- string tp;
18
-
19
- parametersfile.open (filepath, ios::in);
20
-
21
- if (parametersfile.is_open ())
22
- {
23
- while (getline (parametersfile, tp))
24
- {
25
- parametersfile.close ();
26
- return tp;
27
- }
23
+ // Get the error message ID, if any.
24
+ DWORD errorMessageID = ::GetLastError ();
25
+ if (errorMessageID == 0 ) {
26
+ return std::string (); // No error message has been recorded
28
27
}
29
- }
30
28
29
+ LPSTR messageBuffer = nullptr ;
31
30
32
- bool OpenBFME ( string parameters)
33
- {
34
- PROCESS_INFORMATION ProcessInfo;
35
- STARTUPINFOA StartupInfo ;
31
+ // Ask Win32 to give us the string version of that message ID.
32
+ // The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet know how long the message string will be).
33
+ size_t size = FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
34
+ NULL , errorMessageID, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0 , NULL ) ;
36
35
37
- ZeroMemory (&StartupInfo, sizeof (StartupInfo));
36
+ // Copy the error message into a std::string.
37
+ std::string message (messageBuffer, size);
38
38
39
- StartupInfo.cb = sizeof StartupInfo;
39
+ // Free the Win32's string's buffer.
40
+ LocalFree (messageBuffer);
41
+
42
+ return message;
43
+ }
40
44
41
- LPCSTR Application = " C:\\ Program Files (x86)\\ EA GAMES\\ The Battle for Middle-earth (tm)\\ lotrbfme_.exe" ;
42
- LPCSTR CurrentDirectory = " C:\\ Program Files (x86)\\ EA GAMES\\ The Battle for Middle-earth (tm)" ;
45
+ void printLastError ()
46
+ {
47
+ printf (" [X] Error code %d: " , GetLastError ());
48
+ printf (GetLastErrorAsString ().c_str ());
49
+ }
43
50
44
- ifstream ifile (Application);
51
+ string getModulePath ()
52
+ {
53
+ char buffer[MAXBUFCHARS];
54
+ GetModuleFileNameA (NULL , buffer, MAXBUFCHARS);
55
+ return string (buffer).substr (0 , string (buffer).find_last_of (" \\ " ));
56
+ }
45
57
46
- if (!ifile)
47
- return false ;
58
+ int main (int argc, char * argv[])
59
+ {
60
+ // Get INI file path
61
+ printf (" [i] Getting ini file path.\n " );
62
+ iniFile << getModulePath ().c_str () << " \\ lotrbfme_parameters.ini" ;
48
63
49
- LPSTR cString = _strdup (parameters.c_str ());
64
+ // Get original binary path
65
+ printf (" [i] Getting original binary \" lotrbfme_.exe\" path.\n " );
66
+ ssApplication << getModulePath ().c_str () << " \\ lotrbfme_.exe" ;
50
67
51
- if (!CreateProcessA (Application, cString, NULL , NULL , TRUE , 0 , NULL , CurrentDirectory, &StartupInfo, &ProcessInfo))
68
+ // Read info from ini file
69
+ printf (" [i] Reading \" execution_parameters\" from ini file.\n " );
70
+ if (!GetPrivateProfileStringA (" Settings" , " execution_parameters" , NULL , executionParameters, MAXBUFCHARS, iniFile.str ().c_str ()))
52
71
{
53
- // printf("CreateProcess failed (%d).\n", GetLastError());
54
- MessageBoxA (NULL , " CreateProcessA Failed!" , " ERROR" , MB_OK | MB_ICONERROR);
55
- return false ;
72
+ printf (" [X] Error reading \" execution_parameters\" from ini file.\n " );
73
+ printLastError ();
74
+ cin.get ();
75
+ return 1 ;
56
76
}
57
- else
77
+
78
+ // Build lpCommandLine
79
+ printf (" [i] Building lpCommandLine\n " );
80
+ ssCommandLine << ssApplication.str () << " " << executionParameters;
81
+
82
+ // Execute BFME
83
+ ZeroMemory (&lpStartupInfo, sizeof (lpStartupInfo));
84
+ lpStartupInfo.cb = sizeof lpStartupInfo;
85
+ printf (" [i] Executing BFME.\n " );
86
+ if (!CreateProcessA (ssApplication.str ().c_str (), LPSTR (ssCommandLine.str ().c_str ()), NULL , NULL , 1 , 0 , NULL , NULL , &lpStartupInfo, &lpProcessInfo))
58
87
{
59
- WaitForSingleObject (ProcessInfo.hProcess , INFINITE);
60
- return true ;
88
+ printf (" [X] CreateProcessA failed.\n " );
89
+ printLastError ();
90
+ cin.get ();
91
+ return 1 ;
61
92
}
62
- }
63
93
64
-
65
- int main ()
66
- {
67
- if (!OpenBFME (ReadFile (" lotrbfme_params.txt" )))
68
- system (" pause" );
94
+ printf (" \n Press enter to close this window...\n " );
95
+ cin.get ();
96
+ return 0 ;
69
97
}
0 commit comments