]> git.xonotic.org Git - xonotic/netradiant.git/commitdiff
add command line support
authorThomas Debesse <dev@illwieckz.net>
Sat, 15 Jul 2017 14:46:09 +0000 (16:46 +0200)
committerThomas Debesse <dev@illwieckz.net>
Sat, 29 Jul 2017 16:23:06 +0000 (18:23 +0200)
- enable generic Gtk options
- allow to pass map filename as argument
  and open it

radiant/main.cpp
radiant/map.cpp

index 196297ede2ad6e226e14ade58e077afc2585bc31..c04867108bf4dfb4c102c71b5b94b3bcf327f7dc 100644 (file)
@@ -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, "<filename.map>", 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
index 18d25c096c17784ffbef248310e271eadb90f858..7e39d8007296946f8c997e16c3f862e35fc08c0e 100644 (file)
@@ -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 );