-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
114 lines (98 loc) · 3.61 KB
/
Copy pathMain.cpp
File metadata and controls
114 lines (98 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <tchar.h>
#include "Utils.h"
/*
* replace "steam_login" -> steam_login
* "steam_pass" -> steam_pass in 'filename' inside the current working directory
* replace previous appID to new in following commands: "app_update" and "app_run"
*/
void patchScript(char* steam_login, char* steam_pass, const char* appID, std::wstring filename) {
TCHAR tempfilename[256]{ TEXT("test") };
std::ifstream configFile{ filename };
std::ofstream tempFile{ tempfilename };
std::string line;
while (std::getline(configFile, line))
{
int pos;
bool written = false;
if ((pos = line.find("steam_login")) != std::basic_string<char>::npos)
{
line.insert(pos, steam_login);
line.erase(pos + strlen(steam_login), strlen("steam_login"));
written = true;
}
if ((pos = line.find("steam_pass")) != std::basic_string<char>::npos)
{
line.insert(pos, steam_pass);
line.erase(pos + strlen(steam_pass), strlen("steam_pass"));
written = true;
}
if ((pos = line.find("app_update")) != std::basic_string<char>::npos)
{
line.clear();
line.append("app_update " + std::string{ appID } + " validate");
written = true;
}
if ((pos = line.find("app_run")) != std::basic_string<char>::npos)
{
line.clear();
line.append("app_run " + std::string{ appID });
written = true;
}
(!configFile.eof()) ? tempFile << line << '\n' : tempFile << line;
}
configFile.close();
tempFile.close();
std::ifstream updatedFile{ tempfilename };
std::ofstream originFile{ filename };
originFile << updatedFile.rdbuf();
updatedFile.close();
originFile.close();
std::basic_string<TCHAR> abs_path{ current_dir() + L"\\" + tempfilename };
if (!DeleteFile(abs_path.c_str())) {
std::cout << "Unable to delete temp file, error code: " << GetLastError() << std::endl;
}
}
int main(int argc, TCHAR* argv[])
{
/*
Step 0: Initialize
* Encapsulate steam login and password to system variables
* Patch script file with steam credentials, appID and absolute path to script file
* Prepare fields to create process
*/
auto appID = "232890"; // appID: 232890 -> Stronghold Crusader 2
std::wstring script_file = current_dir() + L"\\script.txt";
patchScript(std::getenv("steam_login"), std::getenv("steam_pass"), appID, script_file);
STARTUPINFO cif;
ZeroMemory(&cif, sizeof(STARTUPINFO));
PROCESS_INFORMATION pi;
std::wstring cmdParams{ L" +runscript " + script_file }; // SteamCMD params
/*
Step 1:
* Create and run the process SteamCMD with our script which authorize to account, change install directory and download the game
* Run the game and close SteamCMD
* [TODO]: Handle exceptions and errors (e.g. wrong login/password, enter a Steam-guard, invalid path to directory, appID,
* not enough space on the disc, downloading&validating > time-out of WaitForSingleObject and etc)
*/
if (!CreateProcess(L"c:\\steamcmd\\steamcmd.exe", (LPWSTR)cmdParams.c_str(), // note: server should has SteamCMD app in C:\
NULL, NULL, FALSE, CREATE_NEW_CONSOLE | HIGH_PRIORITY_CLASS, NULL, NULL, &cif, &pi)) {
std::cout << "Unable to create the process!" << std::endl;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, 1000 * 60 * 30); // 30 min time-out
DWORD exitCode;
if (!GetExitCodeProcess(pi.hProcess, &exitCode)) {
std::cout << "Unable to get exit code of the process: %d" << GetLastError() << std::endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 1;
}
std::cout << "Exit code: " << exitCode << std::endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}