naev 0.12.6
naevlua.c
1#define NOMAIN 1
2#include "naev.c"
3
4#include "lualib.h"
5#include "nlua_bkg.h"
6#include "nlua_camera.h"
7#include "nlua_cli.h"
8#include "nlua_linopt.h"
9#include "nlua_music.h"
10#include "nlua_tk.h"
11
12const char *__asan_default_options()
13{
14 return "detect_leaks=0";
15}
16
17int main( int argc, char **argv )
18{
19 char conf_file_path[PATH_MAX], **search_path;
20
21#ifdef DEBUGGING
22 /* Set Debugging flags. */
23 memset( debug_flags, 0, DEBUG_FLAGS_MAX );
24#endif /* DEBUGGING */
25
26 env_detect( argc, argv );
27
28 log_init();
29
30 /* Set up PhysicsFS. */
31 if ( PHYSFS_init( env.argv0 ) == 0 ) {
32 char buf[STRMAX];
33 snprintf( buf, sizeof( buf ), "PhysicsFS initialization failed: %s",
34 PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) );
35#if SDL_VERSION_ATLEAST( 3, 0, 0 )
36 SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR,
37 _( "Naev Critical Error" ), buf,
38 gl_screen.window );
39#endif /* SDL_VERSION_ATLEAST( 3, 0, 0 ) */
40 ERR( "%s", buf );
41 return -1;
42 }
43 PHYSFS_permitSymbolicLinks( 1 );
44
45 /* Set up locales. */
47 init_linebreak();
48
49 /* Parse version. */
50 if ( semver_parse( naev_version( 0 ), &version_binary ) )
51 WARN( _( "Failed to parse version string '%s'!" ), naev_version( 0 ) );
52
53 /* Print the version */
54 LOG( " %s v%s (%s)", APPNAME, naev_version( 0 ), HOST );
55
56#if __LINUX__
57 if ( env.isAppImage )
58 LOG( "AppImage detected. Running from: %s", env.appdir );
59 else
60 DEBUG( "AppImage not detected." );
61#endif /*__LINUX__ */
62
63 /* Initializes SDL for possible warnings. */
64 if ( SDL_Init( 0 ) ) {
65 char buf[STRMAX];
66 snprintf( buf, sizeof( buf ), _( "Unable to initialize SDL: %s" ),
67 SDL_GetError() );
68#if SDL_VERSION_ATLEAST( 3, 0, 0 )
69 SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR,
70 _( "Naev Critical Error" ), buf,
71 gl_screen.window );
72#endif /* SDL_VERSION_ATLEAST( 3, 0, 0 ) */
73 ERR( "%s", buf );
74 return -1;
75 }
76
77 /* Initialize the threadpool */
78 threadpool_init();
79
80 /* Set up debug signal handlers. */
82
83#if HAS_UNIX
84 /* Set window class and name. */
85 SDL_setenv( "SDL_VIDEO_X11_WMCLASS", APPNAME, 0 );
86#endif /* HAS_UNIX */
87
88 /* Must be initialized before input_init is called. */
89 if ( SDL_InitSubSystem( SDL_INIT_VIDEO ) < 0 ) {
90 WARN( _( "Unable to initialize SDL Video: %s" ), SDL_GetError() );
91 return -1;
92 }
93
94 /* We'll be parsing XML. */
95 LIBXML_TEST_VERSION
96 xmlInitParser();
97
98 /* Input must be initialized for config to work. */
99 input_init();
100
101 lua_init(); /* initializes lua */
102 fps_init(); /* Not actually necessary, but removes warning. */
103
104 conf_setDefaults(); /* set the default config values */
105
106 /*
107 * Attempts to load the data path from datapath.lua
108 * At this early point in the load process, the binary path
109 * is the only place likely to be checked.
110 */
111 conf_loadConfigPath();
112
113 /* Create the home directory if needed. */
115 WARN( _( "Unable to create config directory '%s'" ), nfile_configPath() );
116
117 /* Set the configuration. */
118 snprintf( conf_file_path, sizeof( conf_file_path ), "%s" CONF_FILE,
120
121 conf_loadConfig( conf_file_path ); /* Lua to parse the configuration file */
122 int opt = conf_parseCLI( argc, argv ); /* parse CLI arguments */
123 if ( opt >= argc ) {
124 LOG( _( "Missing Lua file!" ) );
125 }
126 char luafile[PATH_MAX];
127 strncpy( luafile, argv[opt++], sizeof( luafile ) );
128
129 /* Set up I/O. */
131 log_redirect();
133 gettext_setLanguage( conf.language ); /* now that we can find translations */
134 LOG( _( "Loaded configuration: %s" ), conf_file_path );
135 search_path = PHYSFS_getSearchPath();
136 LOG( "%s", _( "Read locations, searched in order:" ) );
137 for ( char **p = search_path; *p != NULL; p++ )
138 LOG( " %s", *p );
139 PHYSFS_freeList( search_path );
140 /* Logging the cache path is noisy, noisy is good at the DEBUG level. */
141 DEBUG( _( "Cache location: %s" ), nfile_cachePath() );
142 LOG( _( "Write location: %s\n" ), PHYSFS_getWriteDir() );
143
144 /* Enable FPU exceptions. */
145 if ( conf.fpu_except )
147
148 /* Load the start info. */
149 if ( start_load() ) {
150 char buf[STRMAX];
151 snprintf( buf, sizeof( buf ), _( "Failed to load module start data." ) );
152#if SDL_VERSION_ATLEAST( 3, 0, 0 )
153 SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR,
154 _( "Naev Critical Error" ), buf,
155 gl_screen.window );
156#endif /* SDL_VERSION_ATLEAST( 3, 0, 0 ) */
157 ERR( "%s", buf );
158 }
159 LOG( " %s", start_name() );
160 DEBUG_BLANK();
161
162 /* Display the SDL Version. */
164 DEBUG_BLANK();
165
166 /* random numbers */
167 rng_init();
168
169 /*
170 * OpenGL
171 */
172 if ( gl_init( SDL_WINDOW_HIDDEN ) ) { /* initializes video output */
173 char buf[STRMAX];
174 snprintf( buf, sizeof( buf ),
175 _( "Initializing video output failed, exiting…" ) );
176#if SDL_VERSION_ATLEAST( 3, 0, 0 )
177 SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR,
178 _( "Naev Critical Error" ), buf,
179 gl_screen.window );
180#endif /* SDL_VERSION_ATLEAST( 3, 0, 0 ) */
181 ERR( "%s", buf );
182 // SDL_Quit();
183 exit( EXIT_FAILURE );
184 }
186
187 /* Have to set up fonts before rendering anything. */
188 // DEBUG("Using '%s' as main font and '%s' as monospace font.",
189 // _(FONT_DEFAULT_PATH), _(FONT_MONOSPACE_PATH));
190 gl_fontInit( &gl_defFont, _( FONT_DEFAULT_PATH ), conf.font_size_def,
191 FONT_PATH_PREFIX, 0 ); /* initializes default font to size */
192 gl_fontInit( &gl_smallFont, _( FONT_DEFAULT_PATH ), conf.font_size_small,
193 FONT_PATH_PREFIX, 0 ); /* small font */
194 gl_fontInit( &gl_defFontMono, _( FONT_MONOSPACE_PATH ), conf.font_size_def,
195 FONT_PATH_PREFIX, 0 );
196
197 /* Detect size changes that occurred after window creation. */
198 naev_resize();
199
200 /* Display the load screen. */
202 loadscreen_update( 0., _( "Initializing subsystems…" ) );
203 last_t = SDL_GetPerformanceCounter();
204
205 /*
206 * Input
207 */
208 if ( ( conf.joystick_ind >= 0 ) || ( conf.joystick_nam != NULL ) ) {
209 if ( joystick_init() )
210 WARN( _( "Error initializing joystick input" ) );
211 if ( conf.joystick_nam !=
212 NULL ) { /* use the joystick name to find a joystick */
213 if ( joystick_use( joystick_get( conf.joystick_nam ) ) ) {
214 WARN( _( "Failure to open any joystick, falling back to default "
215 "keybinds" ) );
216 input_setDefault( 1 );
217 }
218 free( conf.joystick_nam );
219 } else if ( conf.joystick_ind >= 0 ) /* use a joystick id instead */
220 if ( joystick_use( conf.joystick_ind ) ) {
221 WARN( _( "Failure to open any joystick, falling back to default "
222 "keybinds" ) );
223 input_setDefault( 1 );
224 }
225 }
226
227 /*
228 * OpenAL - Sound
229 */
230 if ( conf.nosound ) {
231 LOG( _( "Sound is disabled!" ) );
232 sound_disabled = 1;
233 music_disabled = 1;
234 }
235 if ( sound_init() )
236 WARN( _( "Problem setting up sound!" ) );
237 music_choose( "load" );
238
239 /* FPS stuff. */
240 fps_setPos( 15., (double)( gl_screen.h - 15 - gl_defFontMono.h ) );
241
242 /* Misc graphics init */
243 render_init();
244 nebu_init(); /* Initializes the nebula */
245 gui_init(); /* initializes the GUI graphics */
246 toolkit_init(); /* initializes the toolkit */
247 map_init(); /* initializes the map. */
248 map_system_init(); /* Initialise the solar system map */
249 cond_init(); /* Initialize conditional subsystem. */
250 cli_init(); /* Initialize console. */
251
252 /* Data loading */
253 load_all();
254
255 /* Detect size changes that occurred during load. */
256 naev_resize();
257
258 /* Unload load screen. */
260
261 nlua_env nenv = nlua_newEnv( "naevlua" );
262 nlua_loadStandard( nenv );
263 nlua_loadTex( nenv );
264 nlua_loadCol( nenv );
265 nlua_loadBackground( nenv );
266 nlua_loadCLI( nenv );
267 nlua_loadCamera( nenv );
268 nlua_loadMusic( nenv );
269 nlua_loadTk( nenv );
270 nlua_loadLinOpt( nenv );
271 /* Reload IO library that was sandboxed out. */
272 lua_pushcfunction( naevL, luaopen_io );
273 nlua_pcall( nenv, 0, 1 );
274 nlua_setenv( naevL, nenv, "io" );
275 /* Finally run the file. */
276 if ( luaL_loadfile( naevL, luafile ) != 0 ) {
277 WARN( _( "Script '%s' Lua error:\n%s" ), luafile,
278 lua_tostring( naevL, -1 ) );
279 lua_pop( naevL, 1 );
280 return -1;
281 }
282 LOG( _( "Processing '%s'..." ), luafile );
283 nlua_pushenv( naevL, nenv );
284 lua_setfenv( naevL, -2 );
285 /* Add argument table. */
286 lua_newtable( naevL );
287 int nargs = 0;
288 lua_pushstring( naevL, luafile );
289 lua_rawseti( naevL, -2, 0 );
290 while ( opt < argc ) {
291 lua_pushstring( naevL, argv[opt++] );
292 lua_rawseti( naevL, -2, ++nargs );
293 }
294 nlua_setenv( naevL, nenv, "arg" );
295 /* Run the code. */
296 if ( nlua_pcall( nenv, 0, LUA_MULTRET ) != 0 ) {
297 WARN( _( "Script '%s' Lua error:\n%s" ), luafile,
298 lua_tostring( naevL, -1 ) );
299 lua_pop( naevL, 1 );
300 return -1;
301 }
302 unload_all();
303 return 0;
304}
int cond_init(void)
Initializes the conditional subsystem.
Definition cond.c:23
int cli_init(void)
Initializes the CLI environment.
Definition console.c:508
void debug_sigInit(void)
Sets up the back-tracing signal handler.
Definition debug.c:259
void debug_enableFPUExcept(void)
Enables FPU exceptions. Artificially limited to Linux until link issues are figured out.
Definition debug_fpu.c:27
glFont gl_smallFont
Definition font.c:159
glFont gl_defFont
Definition font.c:158
int gl_fontInit(glFont *font, const char *fname, const unsigned int h, const char *prefix, unsigned int flags)
Initializes a font.
Definition font.c:1670
glFont gl_defFontMono
Definition font.c:160
void gettext_setLanguage(const char *lang)
Set the translation language.
Definition gettext.c:135
void gettext_init(void)
Initialize the translation system. There's no presumption that PhysicsFS is available,...
Definition gettext.c:53
int gui_init(void)
Initializes the GUI system.
Definition gui.c:1714
void input_init(void)
Initializes the input subsystem (does not set keys).
Definition input.c:371
void input_setDefault(int wasd)
Sets the default input keys.
Definition input.c:267
int joystick_get(const char *namjoystick)
Gets the joystick index by name.
Definition joystick.c:35
int joystick_init(void)
Initializes the joystick subsystem.
Definition joystick.c:147
int joystick_use(int indjoystick)
Makes the game use a joystick by index.
Definition joystick.c:54
void log_init(void)
Sets up the logging subsystem. (Calling this ensures logging output is preserved until we have a plac...
Definition log.c:164
void log_redirect(void)
Sets up redirection of stdout and stderr to files. PhysicsFS must be initialized for this to work.
Definition log.c:130
int music_disabled
Definition music.c:29
int music_choose(const char *situation)
Actually runs the music stuff, based on situation.
Definition music.c:426
Controls the overall game flow: data loading/unloading and game loop.
int main(int argc, char **argv)
The entry point of Naev.
Definition naev.c:166
static void fps_init(void)
Initializes the fps engine.
Definition naev.c:936
static void loadscreen_unload(void)
Frees the loading screen.
Definition naev.c:671
static void print_SDLversion(void)
Prints the SDL version to console.
Definition naev.c:1198
static void unload_all(void)
Unloads all data, simplifies main().
Definition naev.c:762
void naev_resize(void)
Wrapper for gl_resize that handles non-GL reinitialization.
Definition naev.c:874
static void window_caption(void)
Sets the window caption.
Definition naev.c:1114
static void loadscreen_update(double done, const char *msg)
Renders the load screen with message.
Definition naev.c:652
static void loadscreen_load(void)
Loads a loading screen.
Definition naev.c:560
static semver_t version_binary
Definition naev.c:104
static Uint64 last_t
Definition naev.c:100
void fps_setPos(double x, double y)
Sets the position to display the FPS.
Definition naev.c:944
#define APPNAME
Definition naev.h:30
const char * naev_version(int long_version)
Returns the version in a human readable string.
#define PATH_MAX
Definition naev.h:57
void ndata_setupReadDirs(void)
Sets up the PhysicsFS search path.
Definition ndata.c:155
void ndata_setupWriteDir(void)
Gets Naev's data path (for user data such as saves and screenshots)
Definition ndata.c:132
int nebu_init(void)
Initializes the nebula.
Definition nebula.c:73
const char * nfile_configPath(void)
Gets Naev's config path (for user preferences such as conf.lua)
Definition nfile.c:113
int nfile_dirMakeExist(const char *path)
Creates a directory if it doesn't exist.
Definition nfile.c:277
const char * nfile_cachePath(void)
Gets Naev's cache path (for cached data such as generated textures)
Definition nfile.c:162
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
Definition nlua.c:914
lua_State * naevL
Definition nlua.c:54
int nlua_loadBackground(nlua_env env)
Loads the graphics library.
Definition nlua_bkg.c:33
int nlua_loadCamera(nlua_env env)
Loads the camera library.
Definition nlua_camera.c:48
int nlua_loadCLI(nlua_env env)
Loads the CLI Lua library.
Definition nlua_cli.c:24
int nlua_loadCol(nlua_env env)
Loads the colour library.
Definition nlua_colour.c:57
int nlua_loadLinOpt(nlua_env env)
Loads the linopt library.
Definition nlua_linopt.c:73
int nlua_loadMusic(nlua_env env)
Music Lua module.
Definition nlua_music.c:52
int nlua_loadTex(nlua_env env)
Loads the texture library.
Definition nlua_tex.c:59
int nlua_loadTk(nlua_env env)
Loads the Toolkit Lua library.
Definition nlua_tk.c:98
int gl_init(unsigned int extra_flags)
Initializes SDL/OpenGL and the works.
Definition opengl.c:600
glInfo gl_screen
Definition opengl.c:47
void rng_init(void)
Initializes the random subsystem.
Definition rng.c:53
int sound_disabled
Definition sound.c:130
int sound_init(void)
Initializes the sound subsystem.
Definition sound.c:609
int start_load(void)
Loads the module start data.
Definition start.c:51
const char * start_name(void)
Gets the module name.
Definition start.c:182
int toolkit_init(void)
Initializes the toolkit.
Definition toolkit.c:2664