From 37b140ce46c3afe6eeb7dc50e40632de59910fe2 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Sat, 15 Jul 2017 16:46:09 +0200 Subject: [PATCH] add command line support - enable generic Gtk options - allow to pass map filename as argument and open it --- radiant/main.cpp | 37 +++++++++++++++++++++++++++++++++++-- radiant/map.cpp | 3 ++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/radiant/main.cpp b/radiant/main.cpp index 196297ed..c0486710 100644 --- a/radiant/main.cpp +++ b/radiant/main.cpp @@ -558,8 +558,38 @@ int main( int argc, char* argv[] ){ } #endif + static GOptionEntry entries[] = { + { NULL } + }; + GError *error = NULL; + const char* mapname = NULL; + gtk_disable_setlocale(); - gtk_init( &argc, &argv ); + if ( !gtk_init_with_args( &argc, &argv, "", entries, NULL, &error) ) { + g_print( "%s\n", error->message ); + return -1; + } + + // Gtk already removed parsed `--options` + if (argc == 2) { + if ( strlen( argv[1] ) > 1 ) { + if ( g_str_has_suffix( argv[1], ".map" ) ) { + if ( g_path_is_absolute( argv[1] ) ) { + mapname = argv[1]; + } + else { + mapname = g_build_filename( g_get_current_dir(), argv[1], NULL ); + } + } + else { + g_print( "bad file name, will not load: %s\n", argv[1] ); + } + } + } + else if (argc > 2) { + g_print ( "%s\n", "too many arguments" ); + return -1; + } // redirect Gtk warnings to the console g_log_set_handler( "Gdk", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | @@ -620,7 +650,10 @@ int main( int argc, char* argv[] ){ hide_splash(); - if ( g_bLoadLastMap && !g_strLastMap.empty() ) { + if ( mapname != NULL ) { + Map_LoadFile( mapname ); + } + else if ( g_bLoadLastMap && !g_strLastMap.empty() ) { Map_LoadFile( g_strLastMap.c_str() ); } else diff --git a/radiant/map.cpp b/radiant/map.cpp index 18d25c09..7e39d800 100644 --- a/radiant/map.cpp +++ b/radiant/map.cpp @@ -932,6 +932,8 @@ void Map_LoadFile( const char *filename ){ globalOutputStream() << "Loading map from " << filename << "\n"; ScopeDisableScreenUpdates disableScreenUpdates( "Processing...", "Loading Map" ); + MRU_AddFile( filename ); + { ScopeTimer timer( "map load" ); @@ -1813,7 +1815,6 @@ void OpenMap(){ const char* filename = map_open( "Open Map" ); if ( filename != 0 ) { - MRU_AddFile( filename ); Map_RegionOff(); Map_Free(); Map_LoadFile( filename ); -- 2.39.2