add outer product
[vg.git] / vg_console.h
index d7f7f9f3e030452a75e85f29ddb5eeef09200f76..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__ ;\
@@ -20,7 +23,6 @@
 
 #define VG_VAR_PERSISTENT  0x1
 #define VG_VAR_CHEAT       0x2
-#define VG_VAR_CHEATTOGGLE 0x4
 
 typedef struct vg_var vg_var;
 typedef struct vg_cmd vg_cmd;
@@ -33,12 +35,11 @@ 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;
-
-      union{ u32 _u32; f32 _f32; i32 _i32; } defaults;
        } 
        vars[ 128 ];
        
@@ -79,7 +80,7 @@ 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 );
+static void  vg_execute_console_input( const char *cmd, bool silent );
 
 /*
  * Console interface
@@ -108,10 +109,6 @@ void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type,
    var->data_type = type;
    var->flags = flags;
 
-   if     ( type == k_var_dtype_f32 ) var->defaults._f32 = *((f32 *)ptr);
-   else if( type == k_var_dtype_i32 ) var->defaults._i32 = *((i32 *)ptr);
-   else if( type == k_var_dtype_u32 ) var->defaults._u32 = *((u32 *)ptr);
-
    vg_info( "Console variable '%s' registered\n", alias );
 }
 
@@ -140,7 +137,9 @@ 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;
@@ -154,6 +153,9 @@ int _test_break( 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 );
@@ -166,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 );
                        }
                }
 
@@ -179,26 +181,10 @@ int _vg_console_exec( int argc, const char *argv[] ){
    return 0;
 }
 
-int _ccmd_vg_console_defaults( int argc, const char *argv[] ){
-       for( int i=0; i<vg_console.var_count; i ++ ){
-               struct vg_var *cv = &vg_console.vars[i];
-
-      enum vg_var_dtype type = cv->data_type;
-      void *ptr = cv->data;
-
-      if     ( type == k_var_dtype_f32 ) *((f32 *)ptr) = cv->defaults._f32;
-      else if( type == k_var_dtype_i32 ) *((i32 *)ptr) = cv->defaults._i32;
-      else if( type == k_var_dtype_u32 ) *((u32 *)ptr) = cv->defaults._u32;
-       }
-
-   return 0;
-}
-
 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_cmd( "defaults", _ccmd_vg_console_defaults, NULL );
    vg_console_reg_var( "cheats", &vg_console.cheats, k_var_dtype_i32, 
 #ifdef VG_DEVWINDOW
                        VG_VAR_PERSISTENT
@@ -209,27 +195,35 @@ static void _vg_console_init(void){
 }
 
 static void vg_console_load_autos(void){
-   _vg_console_exec( 1, (const char *[]){ "auto.conf" } );
+   _vg_console_exec( 2, (const char *[]){ "auto.conf", "silent" } );
 }
 
 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 );
+         }
                }
        }
 
@@ -305,8 +299,7 @@ static vg_cmd *vg_console_match_cmd( const char *kw )
    return NULL;
 }
 
-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 );
@@ -323,28 +316,31 @@ static void vg_execute_console_input( const char *cmd )
       /* Cvar Matched, try get value */
       if( arg_count >= 2 ){
          if( cv->flags & VG_VAR_CHEAT ){
-            if( !vg_console.cheats ){
+            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] ); 
-
-            if( cv->flags & VG_VAR_CHEATTOGGLE ){
-               if( *ptr ){
-                  _ccmd_vg_console_defaults( 0, NULL );
-               }
-            }
          }
          else if( cv->data_type == k_var_dtype_f32 ){
             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 )
@@ -353,6 +349,10 @@ 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;
@@ -362,7 +362,8 @@ 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 ){
@@ -666,7 +667,7 @@ 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 );