add strings to console variables
[vg.git] / vg_console.h
index 208b5b392b9245887583b6f7b34ebc32cb35f2b3..029b3e69efe417011ead5965a481b7c93abd201c 100644 (file)
@@ -1,5 +1,7 @@
 /* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved */
 
+/* TODO: String types using dynamic, vg_str! */
+
 #ifndef VG_CONSOLE_H
 #define VG_CONSOLE_H
 
@@ -7,8 +9,9 @@
   #define VG_GAME
 #endif
 
-#include "vg/vg_imgui.h"
-#include "vg/vg_log.h"
+#include "vg_imgui.h"
+#include "vg_log.h"
+#include "vg_string.h"
 
 #define VG_VAR_F32( NAME, ... ) \
    { u32 flags=0x00; __VA_ARGS__ ;\
@@ -18,8 +21,8 @@
    { u32 flags=0x00; __VA_ARGS__ ;\
    vg_console_reg_var( #NAME, &NAME, k_var_dtype_i32, flags ); }
 
-#define VG_VAR_PERSISTENT 0x1
-#define VG_VAR_CHEAT      0x2
+#define VG_VAR_PERSISTENT  0x1
+#define VG_VAR_CHEAT       0x2
 
 typedef struct vg_var vg_var;
 typedef struct vg_cmd vg_cmd;
@@ -32,10 +35,10 @@ struct vg_console{
                enum vg_var_dtype{
                        k_var_dtype_i32,
                        k_var_dtype_u32,
-                       k_var_dtype_f32
+                       k_var_dtype_f32,
+         k_var_dtype_str
                } 
                data_type;
-
       u32 flags;
        } 
        vars[ 128 ];
@@ -67,35 +70,33 @@ struct vg_console{
        char history[32][96];
        int history_last, history_pos, history_count;
        
-       int enabled;
+       i32 enabled, cheats;
 }
-vg_console;
+static vg_console;
 
-
-VG_STATIC void _vg_console_draw( void );
+static void _vg_console_draw( void );
 void           _vg_console_println( const char *str );
-VG_STATIC int  _vg_console_list( int argc, char const *argv[] );
-VG_STATIC void _vg_console_init(void);
-VG_STATIC void _vg_console_write_persistent(void);
-VG_STATIC void _vg_console_free(void);
-VG_STATIC void  vg_execute_console_input( const char *cmd );
+static int  _vg_console_list( int argc, char const *argv[] );
+static void _vg_console_init(void);
+static void _vg_console_write_persistent(void);
+static void _vg_console_free(void);
+static void  vg_execute_console_input( const char *cmd, bool silent );
 
 /*
  * Console interface
  */
-VG_STATIC void console_history_get( char* buf, int entry_num );
-VG_STATIC int _vg_console_enabled(void);
-VG_STATIC void console_proc_key( SDL_Keysym ev );
+static void console_history_get( char* buf, int entry_num );
+static int _vg_console_enabled(void);
+static void console_proc_key( SDL_Keysym ev );
 
 /*
  * Implementation
  */
-VG_STATIC int _vg_console_enabled(void) 
-{ 
+static int _vg_console_enabled(void){ 
        return vg_console.enabled; 
 }
 
-VG_STATIC 
+static 
 void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type, 
                          u32 flags )
 {
@@ -111,7 +112,7 @@ void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type,
    vg_info( "Console variable '%s' registered\n", alias );
 }
 
-VG_STATIC 
+static 
 void vg_console_reg_cmd( const char *alias,
                          int (*function)(int argc, const char *argv[]),
                          void (*poll_suggest)(int argc, const char *argv[]) )
@@ -128,8 +129,7 @@ void vg_console_reg_cmd( const char *alias,
    vg_info( "Console function '%s' registered\n", alias );
 }
 
-VG_STATIC int _vg_console_list( int argc, char const *argv[] )
-{
+static int _vg_console_list( int argc, char const *argv[] ){
        for( int i=0; i<vg_console.function_count; i ++ ){
                struct vg_cmd *cmd = &vg_console.functions[ i ];
                vg_info( "* %s\n", cmd->name );
@@ -137,22 +137,25 @@ VG_STATIC int _vg_console_list( int argc, char const *argv[] )
        
        for( int i=0; i<vg_console.var_count; i ++ ){
                struct vg_var *cv = &vg_console.vars[ i ];
-               vg_info( "%s\n", cv->name );
+               vg_info( "%s %s\n", 
+               (const char *[]){ "i32","u32","f32","str" }[cv->data_type],
+               cv->name );
        }
        
        return 0;
 }
 
-int _test_break( int argc, const char *argv[] )
-{
+int _test_break( int argc, const char *argv[] ){
    vg_fatal_error( "Test crash from main, after loading (console)" );
    return 0;
 }
 
-int _vg_console_exec( int argc, const char *argv[] )
-{
+int _vg_console_exec( int argc, const char *argv[] ){
    if( argc < 1 ) return 0;
 
+   int silent=0;
+   if( argc == 2 ) silent=1;
+
    char path[256];
    strcpy( path, "cfg/" );
    strncat( path, argv[0], 250 );
@@ -165,7 +168,7 @@ int _vg_console_exec( int argc, const char *argv[] )
                        line[ strcspn( line, "\r\n#" ) ] = 0x00;
 
                        if( line[0] != 0x00 ){
-                               vg_execute_console_input( line );
+                               vg_execute_console_input( line, silent );
                        }
                }
 
@@ -178,44 +181,56 @@ int _vg_console_exec( int argc, const char *argv[] )
    return 0;
 }
 
-VG_STATIC void _vg_console_init(void)
-{
+static void _vg_console_init(void){
    vg_console_reg_cmd( "list", _vg_console_list, NULL );
    vg_console_reg_cmd( "crash", _test_break, NULL );
    vg_console_reg_cmd( "exec", _vg_console_exec, NULL );
+   vg_console_reg_var( "cheats", &vg_console.cheats, k_var_dtype_i32, 
+#ifdef VG_DEVWINDOW
+                       VG_VAR_PERSISTENT
+#else 
+                       0
+#endif
+                       );
 }
 
-VG_STATIC void vg_console_load_autos(void)
-{
-   _vg_console_exec( 1, (const char *[]){ "auto.conf" } );
+static void vg_console_load_autos(void){
+   _vg_console_exec( 2, (const char *[]){ "auto.conf", "silent" } );
 }
 
-VG_STATIC void _vg_console_write_persistent(void)
-{
+static void _vg_console_write_persistent(void){
        FILE *fp = fopen( "cfg/auto.conf", "w" );
+
+   if( !fp ){
+      vg_error( "Cannot open cfg/auto.conf\n" );
+      return;
+   }
        
        for( int i=0; i<vg_console.var_count; i ++ ){
                struct vg_var *cv = &vg_console.vars[i];
 
                if( cv->flags & VG_VAR_PERSISTENT ){
-                       switch( cv->data_type ){
-                               case k_var_dtype_i32:
-                                       fprintf( fp, "%s %d\n", cv->name, *(i32 *)(cv->data) );
-                               break;
-                               case k_var_dtype_u32:
-                                       fprintf( fp, "%s %u\n", cv->name, *(u32 *)(cv->data) );
-                               break;
-                               case k_var_dtype_f32:
-                                       fprintf( fp, "%s %.5f\n", cv->name, *(float *)(cv->data ) );
-                               break;
-                       }
+         if( cv->data_type == k_var_dtype_i32 ){
+            fprintf( fp, "%s %d\n", cv->name, *(i32 *)(cv->data) );
+         }
+         else if( cv->data_type == k_var_dtype_u32 ){
+            fprintf( fp, "%s %u\n", cv->name, *(u32 *)(cv->data) );
+         }
+         else if( cv->data_type == k_var_dtype_f32 ){
+            fprintf( fp, "%s %.5f\n", cv->name, *(float *)(cv->data ) );
+         }
+         else if( cv->data_type == k_var_dtype_str ){
+            vg_str *str = cv->data;
+            if( str->buffer && (str->i > 0) )
+               fprintf( fp, "%s %s\n", cv->name, str->buffer );
+         }
                }
        }
 
        fclose( fp );
 }
 
-VG_STATIC void _vg_console_free(void)
+static void _vg_console_free(void)
 {
        _vg_console_write_persistent();
 }
@@ -225,7 +240,7 @@ VG_STATIC void _vg_console_free(void)
  * returns number of tokens
  * dst must be as long as src
  */
-VG_STATIC int vg_console_tokenize( const char *src, char *dst, 
+static int vg_console_tokenize( const char *src, char *dst, 
                                    const char *args[8] )
 {
        int arg_count = 0,
@@ -260,7 +275,7 @@ VG_STATIC int vg_console_tokenize( const char *src, char *dst,
    return arg_count;
 }
 
-VG_STATIC vg_var *vg_console_match_var( const char *kw )
+static vg_var *vg_console_match_var( const char *kw )
 {
        for( int i=0; i<vg_console.var_count; i ++ ){
                struct vg_var *cv = &vg_console.vars[ i ];
@@ -272,7 +287,7 @@ VG_STATIC vg_var *vg_console_match_var( const char *kw )
    return NULL;
 }
 
-VG_STATIC vg_cmd *vg_console_match_cmd( const char *kw )
+static vg_cmd *vg_console_match_cmd( const char *kw )
 {
        for( int i=0; i<vg_console.function_count; i ++ ){
                struct vg_cmd *cmd = &vg_console.functions[ i ];
@@ -284,8 +299,7 @@ VG_STATIC vg_cmd *vg_console_match_cmd( const char *kw )
    return NULL;
 }
 
-VG_STATIC void vg_execute_console_input( const char *cmd )
-{
+static void vg_execute_console_input( const char *cmd, bool silent ){
        char temp[512];
        char const *args[8];
        int arg_count = vg_console_tokenize( cmd, temp, args );
@@ -301,9 +315,15 @@ VG_STATIC void vg_execute_console_input( const char *cmd )
    if( cv ){
       /* Cvar Matched, try get value */
       if( arg_count >= 2 ){
+         if( cv->flags & VG_VAR_CHEAT ){
+            if( !vg_console.cheats && !silent ){
+               vg_error( "variable is cheat protected\n" );
+               return;
+            }
+         }
+
          if( (cv->data_type == k_var_dtype_u32) ||
-             (cv->data_type == k_var_dtype_i32) )
-         {
+             (cv->data_type == k_var_dtype_i32) ){
             int *ptr = cv->data;
             *ptr = atoi( args[1] ); 
          }
@@ -311,6 +331,16 @@ VG_STATIC void vg_execute_console_input( const char *cmd )
             float *ptr = cv->data;
             *ptr = atof( args[1] );
          }
+         else if( cv->data_type == k_var_dtype_str ){
+            vg_str *str = cv->data;
+            vg_strfree( str );
+            vg_strnull( str, NULL, -1 );
+
+            for( int i=1; i<arg_count; i ++ ){
+               vg_strcat( str, args[i] );
+               if( i!=arg_count-1 ) vg_strcatch( str, ' ' );
+            }
+         }
       }
       else{
          if( cv->data_type == k_var_dtype_i32 )
@@ -319,6 +349,10 @@ VG_STATIC void vg_execute_console_input( const char *cmd )
             vg_info( "= %u\n", *((u32 *)cv->data) );
          else if( cv->data_type == k_var_dtype_f32 )
             vg_info( "= %.4f\n", *((float *)cv->data) );
+         else if( cv->data_type == k_var_dtype_str ){
+            vg_str *str = cv->data;
+            vg_info( "= '%s'\n", str->buffer );
+         }
       }
    
       return;
@@ -328,11 +362,11 @@ VG_STATIC void vg_execute_console_input( const char *cmd )
       return;
    }
        
-       vg_error( "No command/var named '%s'. Use 'list' to view all\n", args[0] );
+   if( !silent )
+      vg_error( "No command/var named '%s'. Use 'list' to view all\n",args[0]);
 }
 
-u32 str_lev_distance( const char *s1, const char *s2 )
-{
+u32 str_lev_distance( const char *s1, const char *s2 ){
    u32 m = strlen( s1 ),
        n = strlen( s2 );
 
@@ -369,8 +403,7 @@ u32 str_lev_distance( const char *s1, const char *s2 )
    return costs[n];
 }
 
-u32 str_lcs( const char *s1, const char *s2 )
-{
+u32 str_lcs( const char *s1, const char *s2 ){
    u32 m = VG_MIN( 31, strlen( s1 ) ),
        n = VG_MIN( 31, strlen( s2 ) );
 
@@ -394,7 +427,7 @@ u32 str_lcs( const char *s1, const char *s2 )
 }
 
 /* str must not fuckoff ever! */
-VG_STATIC void console_suggest_score_text( const char *str, const char *input,
+static void console_suggest_score_text( const char *str, const char *input,
                                            int minscore )
 {
    /* filter duplicates */
@@ -430,7 +463,7 @@ VG_STATIC void console_suggest_score_text( const char *str, const char *input,
    }
 }
 
-VG_STATIC void console_update_suggestions(void)
+static void console_update_suggestions(void)
 {
    if( vg_ui.focused_control_type != k_ui_control_textbox ||
        vg_ui.textbuf != vg_console.input )
@@ -502,7 +535,7 @@ VG_STATIC void console_update_suggestions(void)
 /*
  * Suggestion controls
  */
-VG_STATIC void _console_fetch_suggestion(void)
+static void _console_fetch_suggestion(void)
 {
    char *target = &vg_console.input[ vg_console.suggestion_pastepos ];
 
@@ -522,7 +555,7 @@ VG_STATIC void _console_fetch_suggestion(void)
    }
 }
 
-VG_STATIC void _console_suggest_store_normal(void)
+static void _console_suggest_store_normal(void)
 {
    if( vg_console.suggestion_select == -1 ){
       char *target = &vg_console.input[ vg_console.suggestion_pastepos ];
@@ -530,7 +563,7 @@ VG_STATIC void _console_suggest_store_normal(void)
    }
 }
 
-VG_STATIC void _console_suggest_next(void)
+static void _console_suggest_next(void)
 {
    if( vg_console.suggestion_count ){
       _console_suggest_store_normal();
@@ -544,7 +577,7 @@ VG_STATIC void _console_suggest_next(void)
    }
 }
 
-VG_STATIC void _console_suggest_prev(void)
+static void _console_suggest_prev(void)
 {
    if( vg_console.suggestion_count ){
       _console_suggest_store_normal();
@@ -558,14 +591,14 @@ VG_STATIC void _console_suggest_prev(void)
    }
 }
 
-VG_STATIC void _vg_console_on_update( char *buf, u32 len )
+static void _vg_console_on_update( char *buf, u32 len )
 {
    if( buf == vg_console.input ){
       console_update_suggestions();
    }
 }
 
-VG_STATIC void console_history_get( char* buf, int entry_num )
+static void console_history_get( char* buf, int entry_num )
 {
        if( !vg_console.history_count )
                return;
@@ -576,7 +609,7 @@ VG_STATIC void console_history_get( char* buf, int entry_num )
        strcpy( buf, vg_console.history[ pick ] );
 }
 
-VG_STATIC void _vg_console_on_up( char *buf, u32 len )
+static void _vg_console_on_up( char *buf, u32 len )
 {
    if( buf == vg_console.input ){
       vg_console.history_pos = 
@@ -601,7 +634,7 @@ VG_STATIC void _vg_console_on_up( char *buf, u32 len )
    }
 }
 
-VG_STATIC void _vg_console_on_down( char *buf, u32 len )
+static void _vg_console_on_down( char *buf, u32 len )
 {
    if( buf == vg_console.input ){
       vg_console.history_pos = VG_MAX( 0, vg_console.history_pos-1 );
@@ -613,7 +646,7 @@ VG_STATIC void _vg_console_on_down( char *buf, u32 len )
    }
 }
 
-VG_STATIC void _vg_console_on_enter( char *buf, u32 len )
+static void _vg_console_on_enter( char *buf, u32 len )
 {
    if( buf == vg_console.input ){
       if( !strlen( vg_console.input ) ) 
@@ -634,7 +667,7 @@ VG_STATIC void _vg_console_on_enter( char *buf, u32 len )
       }
 
       vg_console.history_pos = -1;
-      vg_execute_console_input( vg_console.input );
+      vg_execute_console_input( vg_console.input, 0 );
       _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
                                &vg_ui.textbox.cursor_pos, -10000, 1 );
 
@@ -643,14 +676,14 @@ VG_STATIC void _vg_console_on_enter( char *buf, u32 len )
    }
 }
 
-VG_STATIC void _vg_console_draw(void)
+static void _vg_console_draw(void)
 {
        if( !vg_console.enabled ) return;
 
    SDL_AtomicLock( &log_print_sl );
 
        int ptr = vg_log.log_line_current;
-   int const fh = 14, log_lines = 32;
+   int const fh = vg_ui.font->line_height, log_lines = 32;
    int console_lines = VG_MIN( log_lines, vg_log.log_line_count );
 
    ui_rect rect_log   = { 0, 0,                vg.window_x, log_lines*fh },
@@ -682,9 +715,10 @@ VG_STATIC void _vg_console_draw(void)
       .up = _vg_console_on_up,
       .down = _vg_console_on_down,
       .change = _vg_console_on_update,
-      .enter = _vg_console_on_enter
+      .enter = _vg_console_on_enter,
    };
-   ui_textbox( rect_input, vg_console.input, vg_list_size(vg_console.input),
+   ui_textbox( rect_input, NULL, 
+               vg_console.input, vg_list_size(vg_console.input), 1,
                UI_TEXTBOX_AUTOFOCUS, &callbacks );
 
    /* 
@@ -694,9 +728,9 @@ VG_STATIC void _vg_console_draw(void)
       ui_rect rect_suggest;
       rect_copy( rect_input, rect_suggest );
 
-      rect_suggest[0] += 6 + UI_GLYPH_SPACING_X*vg_console.suggestion_pastepos;
+      rect_suggest[0] += 6 + vg_ui.font->spacing*vg_console.suggestion_pastepos;
       rect_suggest[1] += rect_input[3];
-      rect_suggest[2]  = UI_GLYPH_SPACING_X * vg_console.suggestion_maxlen;
+      rect_suggest[2]  = vg_ui.font->spacing * vg_console.suggestion_maxlen;
       rect_suggest[3]  = vg_console.suggestion_count * fh;
 
       ui_fill( rect_suggest, bg_colour );