This repository was archived by the owner on Mar 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmdLine.cpp
More file actions
218 lines (165 loc) · 5.67 KB
/
CmdLine.cpp
File metadata and controls
218 lines (165 loc) · 5.67 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "StdH.h"
#include "CmdLine.h"
extern CTString cmd_strWorld = ""; // world to load
extern INDEX cmd_iGoToMarker = -1; // marker to go to
extern CTString cmd_strScript = ""; // script to execute
extern CTString cmd_strServer = ""; // server to connect to
extern INDEX cmd_iPort = -1; // port to connect to
extern CTString cmd_strPassword = ""; // network password
extern CTString cmd_strOutput = ""; // output from parsing command line
extern BOOL cmd_bServer = FALSE; // set to run as server
extern BOOL cmd_bQuickJoin = FALSE; // do not ask for players and network settings
static CTString _strCmd;
// Get first next word or quoted string
CTString GetNextParam(void) {
// strip leading spaces/tabs
_strCmd.TrimSpacesLeft();
// if nothing left
if (_strCmd == "") {
// no word to return
return "";
}
// if the first char is quote
if (_strCmd[0] == '"') {
// find first next quote
const char *pchClosingQuote = strchr(_strCmd + 1, '"');
// if not found
if (pchClosingQuote == NULL) {
// error in command line
cmd_strOutput += LOCALIZE("Command line error!\n");
// finish parsing
_strCmd = "";
return "";
}
INDEX iQuote = pchClosingQuote - _strCmd;
// get the quoted string
CTString strWord;
CTString strRest;
_strCmd.Split(iQuote, strWord, strRest);
// remove the quotes
strWord.DeleteChar(0);
strRest.DeleteChar(0);
// get the word
_strCmd = strRest;
return strWord;
// if the first char is not quote
} else {
// find first next space
INDEX iSpace;
INDEX ctChars = _strCmd.Length();
for (iSpace = 0; iSpace < ctChars; iSpace++) {
if (isspace(_strCmd[iSpace])) {
break;
}
}
// get the word string
CTString strWord;
CTString strRest;
_strCmd.Split(iSpace, strWord, strRest);
// remove the space
strRest.DeleteChar(0);
// get the word
_strCmd = strRest;
return strWord;
}
}
// Parse command line parameters and set results to static variables
void ParseCommandLine(CTString strCmd, BOOL bOnLaunch) {
cmd_strOutput = CTString(0, LOCALIZE("Command line: '%s'\n"), strCmd);
// if no command line
if (strCmd.Length() == 0) {
// do nothing
return;
}
_strCmd = strCmd;
FOREVER {
CTString strWord = GetNextParam();
if (strWord == "") {
cmd_strOutput += "\n";
return;
} else if (strWord == "+level") {
cmd_strWorld = GetNextParam();
} else if (strWord == "+server") {
cmd_bServer = TRUE;
} else if (strWord == "+quickjoin") {
cmd_bQuickJoin = TRUE;
} else if (strWord == "+password") {
cmd_strPassword = GetNextParam();
} else if (strWord == "+connect") {
cmd_strServer = GetNextParam();
const char *pcColon = strchr(cmd_strServer, ':');
if (pcColon != NULL) {
CTString strServer;
CTString strPort;
cmd_strServer.Split(pcColon - cmd_strServer, strServer, strPort);
cmd_strServer = strServer;
strPort.ScanF(":%d", &cmd_iPort);
}
} else if (strWord == "+goto") {
GetNextParam().ScanF("%d", &cmd_iGoToMarker);
// [Cecil] Commands only on launch
} else if (bOnLaunch) {
if (strWord == "+game") {
CTString strMod = GetNextParam();
if (strMod != "SeriousSam") { // (we ignore default mod - always use base dir in that case)
_fnmMod = "Mods\\" + strMod + "\\";
}
} else if (strWord == "+cdpath") {
_fnmCDPath = GetNextParam();
} else if (bOnLaunch && strWord == "+script") {
cmd_strScript = GetNextParam();
} else if (bOnLaunch && strWord == "+logfile") {
_strLogFile = GetNextParam();
} else {
// [Cecil] This is the first time I seriously use goto in C++ in my entire life
goto UnknownOption;
}
} else {
UnknownOption:
cmd_strOutput += CTString(0, LOCALIZE(" Unknown option: '%s'\n"), strWord);
}
}
}
// [Cecil] Execute commands set by the command line
BOOL ExecuteCommandLine(void) {
if (cmd_strPassword != "") {
_pShell->SetString("net_strConnectPassword", cmd_strPassword);
}
// If connecting to server from command line
if (cmd_strServer != "") {
CTString strPort = "";
if (cmd_iPort > 0) {
_pShell->SetINDEX("net_iPort", cmd_iPort);
strPort.PrintF(":%d", cmd_iPort);
}
CPrintF(LOCALIZE("Command line connection: '%s%s'\n"), cmd_strServer, strPort);
// Go to join menu
GetGameAPI()->JoinAddress() = cmd_strServer;
if (cmd_bQuickJoin) {
extern void JoinNetworkGame(void);
JoinNetworkGame();
} else {
StartMenus("join");
}
// If starting world from command line
} else if (cmd_strWorld != "") {
// [Cecil] Start map from the game directory (passes "..\\Levels\\ExampleWorld.wld" into StartMap() command)
_pShell->Execute(CTString(0, "StartMap(\"%s\");", "..\\\\" + cmd_strWorld));
// Hasn't started anything
} else {
return FALSE;
}
// Started a new game
return TRUE;
};