Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit b72325d

Browse files
authored
Version 2.0.0.2
1 parent 4d44b41 commit b72325d

File tree

4 files changed

+78
-50
lines changed

4 files changed

+78
-50
lines changed

lotrbfme/lotrbfme.aps

1008 Bytes
Binary file not shown.

lotrbfme/lotrbfme.cpp

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,97 @@
11
/*
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
55
*/
66

7-
87
#include "pch.h"
98

10-
119
using namespace std;
1210

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()
1522
{
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
2827
}
29-
}
3028

29+
LPSTR messageBuffer = nullptr;
3130

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);
3635

37-
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
36+
//Copy the error message into a std::string.
37+
std::string message(messageBuffer, size);
3838

39-
StartupInfo.cb = sizeof StartupInfo;
39+
//Free the Win32's string's buffer.
40+
LocalFree(messageBuffer);
41+
42+
return message;
43+
}
4044

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+
}
4350

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+
}
4557

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";
4863

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";
5067

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()))
5271
{
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;
5676
}
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))
5887
{
59-
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
60-
return true;
88+
printf("[X] CreateProcessA failed.\n");
89+
printLastError();
90+
cin.get();
91+
return 1;
6192
}
62-
}
6393

64-
65-
int main()
66-
{
67-
if (!OpenBFME(ReadFile("lotrbfme_params.txt")))
68-
system("pause");
94+
printf("\nPress enter to close this window...\n");
95+
cin.get();
96+
return 0;
6997
}

lotrbfme/lotrbfme.rc

2.68 KB
Binary file not shown.

lotrbfme/lotrbfme.vcxproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@
2929
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
3030
<ConfigurationType>Application</ConfigurationType>
3131
<UseDebugLibraries>true</UseDebugLibraries>
32-
<PlatformToolset>v142</PlatformToolset>
32+
<PlatformToolset>v143</PlatformToolset>
3333
<CharacterSet>Unicode</CharacterSet>
3434
</PropertyGroup>
3535
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3636
<ConfigurationType>Application</ConfigurationType>
3737
<UseDebugLibraries>false</UseDebugLibraries>
38-
<PlatformToolset>v142</PlatformToolset>
38+
<PlatformToolset>v143</PlatformToolset>
3939
<WholeProgramOptimization>true</WholeProgramOptimization>
4040
<CharacterSet>Unicode</CharacterSet>
4141
</PropertyGroup>
4242
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4343
<ConfigurationType>Application</ConfigurationType>
4444
<UseDebugLibraries>true</UseDebugLibraries>
45-
<PlatformToolset>v142</PlatformToolset>
45+
<PlatformToolset>v143</PlatformToolset>
4646
<CharacterSet>Unicode</CharacterSet>
4747
</PropertyGroup>
4848
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
4949
<ConfigurationType>Application</ConfigurationType>
5050
<UseDebugLibraries>false</UseDebugLibraries>
51-
<PlatformToolset>v142</PlatformToolset>
51+
<PlatformToolset>v143</PlatformToolset>
5252
<WholeProgramOptimization>true</WholeProgramOptimization>
5353
<CharacterSet>Unicode</CharacterSet>
5454
</PropertyGroup>
@@ -125,7 +125,7 @@
125125
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
126126
</ClCompile>
127127
<Link>
128-
<SubSystem>Windows</SubSystem>
128+
<SubSystem>Console</SubSystem>
129129
<EnableCOMDATFolding>true</EnableCOMDATFolding>
130130
<OptimizeReferences>true</OptimizeReferences>
131131
<GenerateDebugInformation>true</GenerateDebugInformation>

0 commit comments

Comments
 (0)