Debian Codegraph @

src/command_line.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2016, 2019-2021, 2024 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "command_line.h"
29
30 #include <stdio.h>
31
32 #include <SDL_mixer.h>
33 #include <SDL_version.h>
34
35 #include <cdogs/XGetopt.h>
36 #include <cdogs/config.h>
37 #include <cdogs/log.h>
38 #include <cdogs/sys_config.h>
39 #include <cdogs/utils.h>
40
41 void PrintTitle(void)
42 {
43 printf("C-Dogs SDL %s\n", CDOGS_SDL_VERSION);
44 printf(
45 "SDL version %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION,
46 SDL_PATCHLEVEL);
47 printf(
48 "SDL_mixer version %d.%d.%d\n", SDL_MIXER_MAJOR_VERSION,
49 SDL_MIXER_MINOR_VERSION, SDL_MIXER_PATCHLEVEL);
50 }
51
52 void PrintHelp(void)
53 {
54 printf(
55 "%s\n", "Video Options:\n"
56 " --fullscreen Try and use a fullscreen video mode.\n"
57 " --scale=n Set pixel size to a factor of n\n"
58 " Factors: 1 - 16\n"
59 " --screen=WxH Set window size to W x H\n");
60
61 printf(
62 "%s\n",
63 "Config:\n"
64 " --config=K,V Set arbitrary config option\n"
65 " Example: --config=Game.FriendlyFire=true\n"
66 " --config List all config options\n");
67
68 printf(
69 "Logging: logging is enabled per module and set at certain levels.\n"
70 "Log modules are: ");
71 for (int i = 0; i < (int)LM_COUNT; i++)
72 {
73 printf("%s", LogModuleName((LogModule)i));
74 if (i < (int)LM_COUNT - 1)
75 printf(", ");
76 else
77 printf("\n");
78 }
79 printf("Log levels are: ");
80 for (int i = 0; i < (int)LL_COUNT; i++)
81 {
82 printf("%s", LogLevelName((LogLevel)i));
83 if (i < (int)LL_COUNT - 1)
84 printf(", ");
85 else
86 printf("\n");
87 }
88 printf(
89 " --log=M,L Enable logging for module M at level L.\n\n"
90 " --log=L Enable logging for all modules at level L.\n\n"
91 " --logfile=F Log to file by filename\n\n");
92
93 printf(
94 "%s\n",
95 "Other:\n"
96 " --connect=host (Experimental) connect to a game server\n"
97 " --demo (Experimental) run game for 30 seconds\n");
98 }
99
100 void ProcessCommandLine(char *buf, const int argc, char *argv[])
101 {
102 buf[0] = '\0';
103 for (int i = 0; i < argc; i++)
104 {
105 strcat(buf, " ");
106 // HACK: for OS X, blank out the -psn_XXXX argument so that it doesn't
107 // break arg parsing
108 #ifdef __APPLE__
109 if (strncmp(argv[i], "-psn", strlen("-psn")) == 0)
110 {
111 argv[i] = "";
112 }
113 #endif
114 strcat(buf, argv[i]);
115 }
116 }
117
118 static void PrintConfig(const Config *c, const int indent);
119 bool ParseArgs(
120 const int argc, char *argv[], ENetAddress *connectAddr,
121 const char **loadCampaign, int *demoQuitTimer)
122 {
123 struct option longopts[] = {
124 {"fullscreen", no_argument, NULL, 'f'},
125 {"scale", required_argument, NULL, 's'},
126 {"screen", required_argument, NULL, 'c'},
127 {"connect", required_argument, NULL, 'x'},
128 {"listen_port", required_argument, NULL, 'p'},
129 {"config", optional_argument, NULL, 'C'},
130 {"log", required_argument, NULL, 1000},
131 {"logfile", required_argument, NULL, 1001},
132 {"demo", no_argument, NULL, 1002},
133 {"help", no_argument, NULL, 'h'},
134 {0, 0, NULL, 0}};
135 int opt = 0;
136 int idx = 0;
137 while ((opt = getopt_long(
138 argc, argv, "fs:c:x:C::\0:\0:h", longopts, &idx)) != -1)
139 {
140 switch (opt)
141 {
142 case 'f':
143 ConfigGet(&gConfig, "Graphics.Fullscreen")->u.Bool.Value = true;
144 break;
145 case 's':
146 ConfigSetInt(&gConfig, "Graphics.ScaleFactor", atoi(optarg));
147 break;
148 case 'c':
149 sscanf(
150 optarg, "%dx%d",
151 &ConfigGet(&gConfig, "Graphics.WindowWidth")->u.Int.Value,
152 &ConfigGet(&gConfig, "Graphics.WindowHeight")->u.Int.Value);
153 LOG(LM_MAIN, LL_DEBUG, "Window size %dx%d set...",
154 ConfigGetInt(&gConfig, "Graphics.WindowWidth"),
155 ConfigGetInt(&gConfig, "Graphics.WindowHeight"));
156 break;
157 case 'm': {
158 ConfigGet(&gConfig, "Graphics.ShakeMultiplier")->u.Int.Value =
159 MAX(atoi(optarg), 0);
160 printf(
161 "Shake multiplier: %d\n",
162 ConfigGetInt(&gConfig, "Graphics.ShakeMultiplier"));
163 }
164 break;
165 case 'h':
166 PrintHelp();
167 return false;
168 case 'p':
169 ConfigGet(&gConfig, "ListenPort")->u.Int.Value =
170 MAX(atoi(optarg), 0);
171 printf("Listen port: %d\n", ConfigGetInt(&gConfig, "ListenPort"));
172 break;
173 case 1000: {
174 char *comma = strchr(optarg, ',');
175 if (comma)
176 {
177 // Set logging level for a single module
178 // The module and level are comma separated
179 *comma = '\0';
180 const LogLevel ll = StrLogLevel(comma + 1);
181 LogModuleSetLevel(StrLogModule(optarg), ll);
182 printf("Logging %s at %s\n", optarg, LogLevelName(ll));
183 }
184 else
185 {
186 // Set logging level for all modules
187 const LogLevel ll = StrLogLevel(optarg);
188 for (int i = 0; i < (int)LM_COUNT; i++)
189 {
190 LogModuleSetLevel((LogModule)i, ll);
191 }
192 printf("Logging everything at %s\n", LogLevelName(ll));
193 }
194 }
195 break;
196 case 1001:
197 LogOpenFile(optarg);
198 break;
199 case 1002:
200 *demoQuitTimer = 30 * 1000;
201 printf("Entering demo mode; will auto-quit in 30 seconds\n");
202 break;
203 case 'x':
204 if (enet_address_set_host(connectAddr, optarg) != 0)
205 {
206 printf("Error: unknown host %s\n", optarg);
207 }
208 break;
209 case 'C':
210 if (optarg == NULL)
211 {
212 PrintConfig(&gConfig, 0);
213 return false;
214 }
215 else
216 {
217 char *comma = strchr(optarg, ',');
218 if (comma == NULL)
219 {
220 const Config *c = ConfigGet(&gConfig, optarg);
221 if (c == NULL)
222 {
223 PrintHelp();
224 }
225 else
226 {
227 PrintConfig(c, 0);
228 }
229 return false;
230 }
231 *comma = '\0';
232 const char *value = comma + 1;
233 if (!ConfigTrySetFromString(&gConfig, optarg, value))
234 {
235 PrintHelp();
236 return false;
237 }
238 }
239 break;
240 default:
241 PrintHelp();
242 // Ignore unknown arguments
243 break;
244 }
245 }
246 if (optind < argc)
247 {
248 // non-option ARGV-elements
249 for (; optind < argc; optind++)
250 {
251 // Load campaign
252 *loadCampaign = argv[optind];
253 }
254 }
255
256 return true;
257 }
258 static void PrintConfig(const Config *c, const int indent)
259 {
260 // Print this config
261 for (int i = 0; i < indent; i++)
262 {
263 printf("-");
264 }
265 if (c->Name != NULL)
266 {
267 printf("%s\n", c->Name);
268 }
269 if (c->Type == CONFIG_TYPE_GROUP)
270 {
271 CA_FOREACH(const Config, child, c->u.Group)
272 PrintConfig(child, indent + 1);
273 CA_FOREACH_END()
274 }
275 }