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
26#ifdef GNUSTEP_BASE_LIBRARY
27#import <Foundation/NSAutoreleasePool.h>
28#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)
29#import <Foundation/NSDate.h>
30#endif
31#import <Foundation/NSString.h>
32#import "GameController.h"
33#import "OOLoggingExtended.h"
34
35#if OOLITE_WINDOWS
36#include <locale.h>
37#include <SDL3/SDL.h>
38#include <SDL3/SDL_main.h>
39// Make sure that a high performance GPU is
40// selected, if more than one are available
41__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
42__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
43#endif
44
45GameController* controller;
46#endif
47
48
49#ifndef NDEBUG
50uint32_t gDebugFlags = 0;
51#endif
52
53// This macro is normally defined in the build command
54#ifndef OO_VERSION_FULL
55#define OO_VERSION_FULL "Undefined"
56#endif
57
68int main(int argc, char *argv[])
69{
70#ifdef GNUSTEP_BASE_LIBRARY
71 int i;
72
73#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)
74 [NSDate class]; // See github issue #202
75#endif
76
77#if OOLITE_WINDOWS
78
79 #define OO_SHOW_MSG(ooMsg, ooMsgTitle, ooMsgFlags) MessageBox(NULL, ooMsg, ooMsgTitle, ooMsgFlags)
80 #define TABS1 "\t"
81 #define TABS2 "\t\t"
82 #define TABS3 "\t\t\t"
83 #define TABS4 ""
84
85 // Detect current working directory and set up GNUstep environment variables
86 #define MAX_PATH_LEN 256
87 char currentWorkingDir[MAX_PATH_LEN];
88 DWORD bufferSize = MAX_PATH_LEN;
89
90 QueryFullProcessImageName(GetCurrentProcess(), 0, currentWorkingDir, &bufferSize);
91 // Strip the exe filenameb (from last backslash onwards), leave just the path
92 char *probeString = strrchr(currentWorkingDir, '\\');
93 if (probeString) *probeString = '\0'; // currentWorkingDir now contains the path we need
94
95 // Prepend system PATH env variable with our own executable's path
96 char *pathEnvVar = "Path";
97 const char *systemPath = SDL_getenv(pathEnvVar);
98 if (!systemPath)
99 {
100 pathEnvVar = "PATH";
101 systemPath = SDL_getenv(pathEnvVar);
102 }
103 size_t currentWorkingDirLen = strlen(currentWorkingDir);
104 size_t systemPathLen = strlen(systemPath);
105 // the max possible length of the string below is systemPath plus the path
106 // we have determined for us, plus one char for the ";" and one char for the null terminator
107 char *finalPath = malloc(systemPathLen + currentWorkingDirLen + 2 * sizeof(char));
108 strcpy(finalPath, currentWorkingDir);
109 strcat(finalPath, ";");
110 strcat(finalPath, systemPath);
111
112 SDL_setenv_unsafe("GNUSTEP_PATH_HANDLING", "windows", YES);
113 SDL_setenv_unsafe(pathEnvVar, finalPath, YES);
114 SDL_setenv_unsafe("GNUSTEP_SYSTEM_ROOT", currentWorkingDir, YES);
115 SDL_setenv_unsafe("GNUSTEP_LOCAL_ROOT", currentWorkingDir, YES);
116 SDL_setenv_unsafe("GNUSTEP_NETWORK_ROOT", currentWorkingDir, YES);
117 SDL_setenv_unsafe("GNUSTEP_USERS_ROOT", currentWorkingDir, YES);
118#if OO_GAME_DATA_TO_USER_FOLDER
119 SDL_setenv_unsafe("HOMEPATH", strcat(SDL_getenv("LOCALAPPDATA"), "\\Oolite\\oolite.app"), YES);
120#else
121 SDL_setenv_unsafe("HOMEPATH", currentWorkingDir, YES);
122#endif
123
124 SetCurrentDirectory(currentWorkingDir);
125
126 free(finalPath);
127
128 /* Windows amibtiously starts apps with the C library locale set to the
129 system locale rather than the "C" locale as per spec. Fixing here so
130 numbers don't behave strangely.
131 */
132 setlocale(LC_ALL, "C");
133
134#else // Linux
135 #define OO_SHOW_MSG(ooMsg, ooTitle, ooFlags) fprintf(stdout, "%s", ooMsg)
136 #define TABS1 "\t\t"
137 #define TABS2 "\t\t\t"
138 #define TABS3 "\t\t\t\t"
139 #define TABS4 "\t"
140#endif
141
142 // Need this because we're not using the default run loop's autorelease
143 // pool.
144 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
146
147 @try
148 {
149 // dajt: allocate and set the NSApplication delegate manually because not
150 // using NIB to do this
151 controller = [[GameController alloc] init];
152
153 for (i = 1; i < argc; i++)
154 {
155 if (strcmp("-load", argv[i]) == 0)
156 {
157 i++;
158 }
159 NSString *argument = [NSString stringWithCString:argv[i]];
160 if (i < argc && [[argument lowercaseString] hasSuffix:@".oolite-save"])
161 {
162 [controller setPlayerFileToLoad:argument];
163 }
164
165 if (!strcmp("-help", argv[i]) || !strcmp("--help", argv[i]))
166 {
167 char const *processName = [[[NSProcessInfo processInfo] processName] UTF8String];
168 char s[2048];
169 snprintf(s, sizeof(s), "Usage: %s [options]\n\n"
170 "Options can be any of the following: \n\n"
171 "--compile-sysdesc"TABS2"Compile system descriptions *\n"
172 "--export-sysdesc"TABS2"Export system descriptions *\n"
174 "-hdr"TABS3"Start up in HDR mode\n"
175#endif
176 "-load [filepath]"TABS2"Load commander from [filepath]\n"TABS3 TABS4"(\"-load\" is optional)\n"
177 "-message [messageString]"TABS1"Display [messageString] at startup\n"
178 "-nodust "TABS2 TABS4"Do not draw space dust\n"
179 "-noshaders"TABS2 TABS4"Start up with shaders disabled\n"
180 "-nosplash "TABS2 TABS4"Force disable splash screen on startup\n"
181 "-nosound "TABS2 TABS4"Start up with sound disabled\n"
182 "-novsync"TABS3"Force disable V-Sync\n"
183 "--openstep"TABS2 TABS4"When compiling or exporting\n"TABS3 TABS4"system descriptions, use openstep\n"TABS3 TABS4"format *\n"
184 "-showversion"TABS2 TABS4"Display version at startup screen\n"
185 "-splash"TABS3 TABS4"Force splash screen on startup\n"
186 "-verify-oxp [filepath] "TABS1"Verify OXP at [filepath] *\n"
187 "--xml"TABS3 TABS4"When compiling or exporting\n"TABS3 TABS4"system descriptions, use xml\n"TABS3 TABS4"format *\n"
188 "\n"
189 "Options marked with \"*\" are available only in Test Release configuration.\n"
190 "Version "OO_VERSION_FULL"\n"
191 "Debug functionality enabled (Test Release): "
192#ifndef NDEBUG
193 "yes\n"
194#else
195 "no\n"
196#endif
197 "Built with "
199 "Clang version " STRINGIFY(__clang_major__) "." STRINGIFY(__clang_minor__) "." STRINGIFY(__clang_patchlevel__)
200#else
201 "GCC version " STRINGIFY(__GNUC__) "." STRINGIFY(__GNUC_MINOR__) "." STRINGIFY(__GNUC_PATCHLEVEL__)
202#endif
203 "\n\n", processName
204 );
205 OO_SHOW_MSG(s, processName, MB_OK);
206 OOLog(@"process.args", @"%s option detected, exiting after help page has been displayed.", argv[i]);
207 return 0;
208 }
209 }
210
211 // Release anything allocated during the controller initialisation that
212 // is no longer required.
213 DESTROY(pool);
214
215 // Call applicationDidFinishLaunching because NSApp is not running in
216 // GNUstep port.
217 [controller applicationDidFinishLaunching: nil];
218 }
219 @catch (NSException *exception)
220 {
221 OOLogERR(kOOLogException, @"Root exception handler hit - terminating. This is an internal error, please report it. Exception name: %@, reason: %@", [exception name], [exception reason]);
222 return EXIT_FAILURE;
223 }
224#endif
225
226 // never reached
227 return 0;
228}
NSUInteger gDebugFlags
Definition main.m:7
#define OOLITE_WINDOWS
Definition OOCocoa.h:249
#define OOLITE_HAVE_CLANG
Definition OOCocoa.h:104
#define DESTROY(x)
Definition OOCocoa.h:75
#define OOLogERR(class, format,...)
Definition OOLogging.h:112
NSString *const kOOLogException
Definition OOLogging.m:651
#define OOLog(class, format,...)
Definition OOLogging.h:88
void OOLoggingInit(void)
Definition OOLogging.m:585
#define OO_VERSION_FULL
Definition main.m:55
int main(int argc, const char *argv[])
Definition main.m:18
void applicationDidFinishLaunching:(NSNotification *notification)
void setPlayerFileToLoad:(NSString *filename)