Oolite
Loading...
Searching...
No Matches
main.m
Go to the documentation of this file.
1/*
2
3main.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#if defined(GNUSTEP_BASE_LIBRARY) || defined(OOLITE_SDL_MAC)
26#import <Foundation/NSAutoreleasePool.h>
27#if (GNUSTEP_BASE_MAJOR_VERSION == 1 && (GNUSTEP_BASE_MINOR_VERSION == 24 && GNUSTEP_BASE_SUBMINOR_VERSION >= 9) || (GNUSTEP_BASE_MINOR_VERSION > 24)) || (GNUSTEP_BASE_MAJOR_VERSION > 1)
28#import <Foundation/NSDate.h>
29#endif
30#import "GameController.h"
31#import "OOLoggingExtended.h"
32#import <Foundation/NSString.h>
33
34#if OOLITE_WINDOWS
35#include <SDL3/SDL_main.h>
36#include <SDL3/SDL_stdinc.h>
37#include <locale.h>
38// Make sure that a high performance GPU is
39// selected, if more than one are available
40__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
41__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
42#elif OOLITE_LINUX
43#include <unistd.h>
44#elif OOLITE_MAC_OS_X
45#include <mach-o/dyld.h>
46#include <unistd.h>
47#endif
48
49GameController* controller;
50#endif
51
52#ifdef OO_DEBUG_MONITOR_ENABLED
53uint32_t gDebugFlags = 0;
54#endif
55
56#ifndef STRINGIFY // provided by GNUstep
57#define STRINGIFY_(s) #s
58#define STRINGIFY(s) STRINGIFY_(s)
59#endif
60
61// This macro is normally defined in the build command
62#ifndef OO_VERSION_FULL
63#define OO_VERSION_FULL "Undefined"
64#endif
65
76int main(int argc, char* argv[])
77{
78#if defined(GNUSTEP_BASE_LIBRARY) || defined(OOLITE_SDL_MAC)
79 int i;
80
81#if (GNUSTEP_BASE_MAJOR_VERSION == 1 && (GNUSTEP_BASE_MINOR_VERSION == 24 && GNUSTEP_BASE_SUBMINOR_VERSION >= 9) || (GNUSTEP_BASE_MINOR_VERSION > 24)) || (GNUSTEP_BASE_MAJOR_VERSION > 1)
82 [NSDate class]; // See github issue #202
83#endif
84
85#if OOLITE_WINDOWS
86
87#define OO_SHOW_MSG(ooMsg, ooMsgTitle, ooMsgFlags) MessageBox(NULL, ooMsg, ooMsgTitle, ooMsgFlags)
88#define TABS1 "\t"
89#define TABS2 "\t\t"
90#define TABS3 "\t\t\t"
91#define TABS4 ""
92
93// Detect current working directory and set up GNUstep environment variables
94#define MAX_PATH_LEN 256
95 char currentWorkingDir[MAX_PATH_LEN];
96 DWORD bufferSize = MAX_PATH_LEN;
97
98 QueryFullProcessImageName(GetCurrentProcess(), 0, currentWorkingDir, &bufferSize);
99 // Strip the exe filenameb (from last backslash onwards), leave just the path
100 char* probeString = strrchr(currentWorkingDir, '\\');
101 if (probeString)
102 *probeString = '\0'; // currentWorkingDir now contains the path we need
103
104 // Prepend system PATH env variable with our own executable's path
105 char* pathEnvVar = "Path";
106 const char* systemPath = SDL_getenv(pathEnvVar);
107 if (!systemPath) {
108 pathEnvVar = "PATH";
109 systemPath = SDL_getenv(pathEnvVar);
110 }
111 size_t currentWorkingDirLen = strlen(currentWorkingDir);
112 size_t systemPathLen = strlen(systemPath);
113 // the max possible length of the string below is systemPath plus the path
114 // we have determined for us, plus one char for the ";" and one char for the null terminator
115 char* finalPath = malloc(systemPathLen + currentWorkingDirLen + 2 * sizeof(char));
116 strcpy(finalPath, currentWorkingDir);
117 strcat(finalPath, ";");
118 strcat(finalPath, systemPath);
119
120 SDL_setenv_unsafe("GNUSTEP_PATH_HANDLING", "windows", YES);
121 SDL_setenv_unsafe(pathEnvVar, finalPath, YES);
122 SDL_setenv_unsafe("GNUSTEP_SYSTEM_ROOT", currentWorkingDir, YES);
123 SDL_setenv_unsafe("GNUSTEP_LOCAL_ROOT", currentWorkingDir, YES);
124 SDL_setenv_unsafe("GNUSTEP_NETWORK_ROOT", currentWorkingDir, YES);
125 SDL_setenv_unsafe("GNUSTEP_USERS_ROOT", currentWorkingDir, YES);
126#if OO_GAME_DATA_TO_USER_FOLDER
127 SDL_setenv_unsafe("HOMEPATH", strcat(SDL_getenv("LOCALAPPDATA"), "\\Oolite\\oolite.app"), YES);
128#else
129 SDL_setenv_unsafe("HOMEPATH", currentWorkingDir, YES);
130#endif
131
132 SetCurrentDirectory(currentWorkingDir);
133
134 free(finalPath);
135
136 /* Windows ambitiously starts apps with the C library locale set to the
137 system locale rather than the "C" locale as per spec. Fixing here so
138 numbers don't behave strangely.
139 */
140 setlocale(LC_ALL, "C");
141
142#else // Linux and Mac
143#if OOLITE_LINUX
144 char result[PATH_MAX];
145 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX - 1);
146 result[count] = '\0';
147 char* last_slash = strrchr(result, '/');
148 if (last_slash != NULL) {
149 *last_slash = '\0'; // Chop off the executable name, leaving just the directory
150 }
151 chdir(result);
152#elif OOLITE_MAC_OS_X
153 // Run from the folder holding Resources and AddOns: Contents in an app bundle
154 // (executable in Contents/MacOS), otherwise the folder holding the executable.
155 char result[PATH_MAX];
156 uint32_t resultSize = sizeof(result);
157 char resolved[PATH_MAX];
158 if (_NSGetExecutablePath(result, &resultSize) == 0 && realpath(result, resolved) != NULL) {
159 char* last_slash = strrchr(resolved, '/');
160 if (last_slash != NULL) {
161 *last_slash = '\0';
162 }
163 chdir(resolved);
164 size_t length = strlen(resolved);
165 if (length >= 15 && strcmp(resolved + length - 15, "/Contents/MacOS") == 0) {
166 chdir("..");
167 }
168 }
169#endif
170#define OO_SHOW_MSG(ooMsg, ooTitle, ooFlags) fprintf(stdout, "%s", ooMsg)
171#define TABS1 "\t\t"
172#define TABS2 "\t\t\t"
173#define TABS3 "\t\t\t\t"
174#define TABS4 "\t"
175#endif
176
177 // Need this because we're not using the default run loop's autorelease
178 // pool.
179 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
181
182 @try {
183 // dajt: allocate and set the NSApplication delegate manually because not
184 // using NIB to do this
185 controller = [[GameController alloc] init];
186
187 for (i = 1; i < argc; i++) {
188 if (strcmp("-load", argv[i]) == 0) {
189 i++;
190 }
191 NSString* argument = [NSString stringWithCString:argv[i]];
192 if (i < argc && [[argument lowercaseString] hasSuffix:@".oolite-save"]) {
193 [controller setPlayerFileToLoad:argument];
194 }
195
196 if (!strcmp("-help", argv[i]) || !strcmp("--help", argv[i])) {
197 char const* processName = [[[NSProcessInfo processInfo] processName] UTF8String];
198 char s[2048];
199 snprintf(s, sizeof(s), "Usage: %s [options]\n\n"
200 "Options can be any of the following: \n\n"
201 "--compile-sysdesc" TABS2 "Compile system descriptions *\n"
202 "--export-sysdesc" TABS2 "Export system descriptions *\n"
204 "-hdr" TABS3 "Start up in HDR mode\n"
205#endif
206 "-load [filepath]" TABS2 "Load commander from [filepath]\n" TABS3 TABS4 "(\"-load\" is optional)\n"
207 "-message [messageString]" TABS1 "Display [messageString] at startup\n"
208 "-nodust " TABS2 TABS4 "Do not draw space dust\n"
209 "-noshaders" TABS2 TABS4 "Start up with shaders disabled\n"
210 "-nosplash " TABS2 TABS4 "Force disable splash screen on startup\n"
211 "-nosound " TABS2 TABS4 "Start up with sound disabled\n"
212 "-novsync" TABS3 "Force disable V-Sync\n"
213 "--openstep" TABS2 TABS4 "When compiling or exporting\n" TABS3 TABS4 "system descriptions, use openstep\n" TABS3 TABS4 "format *\n"
214 "-showversion" TABS2 TABS4 "Display version at startup screen\n"
215 "-splash" TABS3 TABS4 "Force splash screen on startup\n"
216 "-verify-oxp [filepath] " TABS1 "Verify OXP at [filepath] *\n"
217 "--xml" TABS3 TABS4 "When compiling or exporting\n" TABS3 TABS4 "system descriptions, use xml\n" TABS3 TABS4 "format *\n"
218 "\n"
219 "Options marked with \"*\" are available only in Test Release configuration.\n"
220 "Version " OO_VERSION_FULL "\n"
221 "Debug functionality enabled (Test Release): "
222#ifdef OO_DEBUG_MONITOR_ENABLED
223 "yes\n"
224#else
225 "no\n"
226#endif
227 "Built with "
229 "Clang version " STRINGIFY(__clang_major__) "." STRINGIFY(__clang_minor__) "." STRINGIFY(__clang_patchlevel__)
230#else
231 "GCC version " STRINGIFY(__GNUC__) "." STRINGIFY(__GNUC_MINOR__) "." STRINGIFY(__GNUC_PATCHLEVEL__)
232#endif
233 "\n\n",
234 processName);
235 OO_SHOW_MSG(s, processName, MB_OK);
236 OOLog(@"process.args", @"%s option detected, exiting after help page has been displayed.", argv[i]);
237 return 0;
238 }
239 }
240
241 // Release anything allocated during the controller initialisation that
242 // is no longer required.
243 DESTROY(pool);
244
245 // Call applicationDidFinishLaunching because NSApp is not running in
246 // GNUstep port.
247 [controller applicationDidFinishLaunching:nil];
248 }
249 @catch (NSException* exception) {
250 OOLogERR(kOOLogException, @"Root exception handler hit - terminating. This is an internal error, please report it. Exception name: %@, reason: %@", [exception name], [exception reason]);
251 return EXIT_FAILURE;
252 }
253#endif
254
255 // never reached
256 return 0;
257}
int main(int argc, const char *argv[])
Definition main.m:123
#define gDebugFlags
#define OOLITE_WINDOWS
#define OOLITE_HAVE_CLANG
#define DESTROY(x)
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
NSString *const kOOLogException
Definition OOLogging.m:600
#define OOLog(class, format,...)
Definition OOLogging.h:84
void OOLoggingInit(void)
Definition OOLogging.m:534
unsigned count
#define STRINGIFY(s)
Definition main.m:58
#define OO_VERSION_FULL
Definition main.m:63
void applicationDidFinishLaunching:(NSNotification *notification)
void setPlayerFileToLoad:(NSString *filename)