build system revision
[vg.git] / vg_console.c
1 #include "vg_engine.h"
2 #include "vg_console.h"
3 #include "vg_imgui.h"
4 #include "vg_log.h"
5 #include "vg_string.h"
6 #include <string.h>
7
8 struct vg_console vg_console;
9
10 void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type,
11 u32 flags )
12 {
13 if( vg_thread_purpose() == k_thread_purpose_main )
14 vg_fatal_error( "FIXME: Cannot register VAR from main thread" );
15
16 if( vg_console.var_count > vg_list_size(vg_console.vars) )
17 vg_fatal_error( "Too many vars registered" );
18
19 vg_var *var = &vg_console.vars[ vg_console.var_count ++ ];
20 var->name = alias;
21 var->data = ptr;
22 var->data_type = type;
23 var->flags = flags;
24
25 vg_info( "Console variable '%s' registered\n", alias );
26 }
27
28 void vg_console_reg_cmd( const char *alias,
29 int (*function)(int argc, const char *argv[]),
30 void (*poll_suggest)(int argc, const char *argv[]) )
31 {
32 if( vg_thread_purpose() == k_thread_purpose_main )
33 vg_fatal_error( "FIXME: Cannot register CMD from main thread" );
34
35 if( vg_console.function_count > vg_list_size(vg_console.functions) )
36 vg_fatal_error( "Too many functions registered" );
37
38 vg_cmd *cmd = &vg_console.functions[ vg_console.function_count ++ ];
39
40 cmd->function = function;
41 cmd->poll_suggest = poll_suggest;
42 cmd->name = alias;
43
44 vg_info( "Console function '%s' registered\n", alias );
45 }
46
47 static int _vg_console_list( int argc, char const *argv[] )
48 {
49 for( int i=0; i<vg_console.function_count; i ++ ){
50 struct vg_cmd *cmd = &vg_console.functions[ i ];
51 vg_info( "* %s\n", cmd->name );
52 }
53
54 for( int i=0; i<vg_console.var_count; i ++ ){
55 struct vg_var *cv = &vg_console.vars[ i ];
56 vg_info( "%s %s\n",
57 (const char *[]){ "i32","u32","f32","str" }[cv->data_type],
58 cv->name );
59 }
60
61 return 0;
62 }
63
64 static void vg_console_write_persistent(void)
65 {
66 FILE *fp = fopen( "cfg/auto.conf", "w" );
67
68 if( !fp ){
69 vg_error( "Cannot open cfg/auto.conf\n" );
70 return;
71 }
72
73 for( int i=0; i<vg_console.var_count; i ++ ){
74 struct vg_var *cv = &vg_console.vars[i];
75
76 if( cv->flags & VG_VAR_PERSISTENT ){
77 if( cv->data_type == k_var_dtype_i32 ){
78 fprintf( fp, "%s %d\n", cv->name, *(i32 *)(cv->data) );
79 }
80 else if( cv->data_type == k_var_dtype_u32 ){
81 fprintf( fp, "%s %u\n", cv->name, *(u32 *)(cv->data) );
82 }
83 else if( cv->data_type == k_var_dtype_f32 ){
84 fprintf( fp, "%s %.5f\n", cv->name, *(float *)(cv->data ) );
85 }
86 else if( cv->data_type == k_var_dtype_str ){
87 vg_str *str = cv->data;
88 if( str->buffer && (str->i > 0) )
89 fprintf( fp, "%s %s\n", cv->name, str->buffer );
90 }
91 }
92 }
93
94 fclose( fp );
95 }
96
97 static void _vg_console_free(void)
98 {
99 vg_console_write_persistent();
100 }
101
102 /*
103 * splits src into tokens and fills out args as pointers to those tokens
104 * returns number of tokens
105 * dst must be as long as src
106 */
107 static int vg_console_tokenize( const char *src, char *dst,
108 const char *args[8] )
109 {
110 int arg_count = 0,
111 in_token = 0;
112
113 for( int i=0;; i ++ ){
114 if( src[i] ){
115 if( src[i] == ' ' || src[i] == '\t' ){
116 if( in_token )
117 dst[i] = '\0';
118
119 in_token = 0;
120
121 if( arg_count == 8 )
122 break;
123 }
124 else{
125 dst[i] = src[i];
126
127 if( !in_token ){
128 args[ arg_count ++ ] = &dst[i];
129 in_token = 1;
130 }
131 }
132 }
133 else{
134 dst[i] = '\0';
135 break;
136 }
137 }
138
139 return arg_count;
140 }
141
142 vg_var *vg_console_match_var( const char *kw )
143 {
144 for( int i=0; i<vg_console.var_count; i ++ ){
145 struct vg_var *cv = &vg_console.vars[ i ];
146 if( !strcmp( cv->name, kw ) ){
147 return cv;
148 }
149 }
150
151 return NULL;
152 }
153
154 vg_cmd *vg_console_match_cmd( const char *kw )
155 {
156 for( int i=0; i<vg_console.function_count; i ++ ){
157 struct vg_cmd *cmd = &vg_console.functions[ i ];
158 if( !strcmp( cmd->name, kw ) ){
159 return cmd;
160 }
161 }
162
163 return NULL;
164 }
165
166 void vg_execute_console_input( const char *cmd, bool silent )
167 {
168 char temp[512];
169 char const *args[8];
170 int arg_count = vg_console_tokenize( cmd, temp, args );
171
172 if( arg_count == 0 )
173 return;
174
175 vg_var *cv = vg_console_match_var( args[0] );
176 vg_cmd *fn = vg_console_match_cmd( args[0] );
177
178 if( cv ){
179 /* Cvar Matched, try get value */
180 if( arg_count >= 2 ){
181 if( cv->flags & VG_VAR_CHEAT ){
182 if( !vg_console.cheats && !silent ){
183 vg_error( "variable is cheat protected\n" );
184 return;
185 }
186 }
187
188 if( (cv->data_type == k_var_dtype_u32) ||
189 (cv->data_type == k_var_dtype_i32) ){
190 int *ptr = cv->data;
191 *ptr = atoi( args[1] );
192 }
193 else if( cv->data_type == k_var_dtype_f32 ){
194 float *ptr = cv->data;
195 *ptr = atof( args[1] );
196 }
197 else if( cv->data_type == k_var_dtype_str ){
198 vg_str *str = cv->data;
199 vg_strfree( str );
200 vg_strnull( str, NULL, -1 );
201
202 for( int i=1; i<arg_count; i ++ ){
203 vg_strcat( str, args[i] );
204 if( i!=arg_count-1 ) vg_strcatch( str, ' ' );
205 }
206 }
207 }
208 else{
209 if( cv->data_type == k_var_dtype_i32 )
210 vg_info( "= %d\n", *((int *)cv->data) );
211 else if( cv->data_type == k_var_dtype_u32 )
212 vg_info( "= %u\n", *((u32 *)cv->data) );
213 else if( cv->data_type == k_var_dtype_f32 )
214 vg_info( "= %.4f\n", *((float *)cv->data) );
215 else if( cv->data_type == k_var_dtype_str ){
216 vg_str *str = cv->data;
217 vg_info( "= '%s'\n", str->buffer );
218 }
219 }
220
221 return;
222 }
223 else if( fn ){
224 fn->function( arg_count-1, args+1 );
225 return;
226 }
227
228 if( !silent )
229 vg_error( "No command/var named '%s'. Use 'list' to view all\n",args[0]);
230 }
231
232 u32 str_lev_distance( const char *s1, const char *s2 ){
233 u32 m = strlen( s1 ),
234 n = strlen( s2 );
235
236 if( m==0 ) return n;
237 if( n==0 ) return m;
238
239 u32 costs[ 256 ];
240
241 for( u32 k=0; k<=n; k++ )
242 costs[k] = k;
243
244 u32 i = 0;
245 for( u32 i=0; i<m; i++ ){
246 costs[0] = i+1;
247
248 u32 corner = i;
249
250 for( u32 j=0; j<n; j++ ){
251 u32 upper = costs[j+1];
252
253 if( s1[i] == s2[j] )
254 costs[ j+1 ] = corner;
255 else{
256 u32 t = (upper < corner)? upper: corner;
257 costs[j+1] = ((costs[j] < t)? costs[j]: t) + 1;
258 }
259
260 corner = upper;
261 }
262 }
263
264 return costs[n];
265 }
266
267 u32 str_lcs( const char *s1, const char *s2 ){
268 u32 m = VG_MIN( 31, strlen( s1 ) ),
269 n = VG_MIN( 31, strlen( s2 ) );
270
271 int suff[32][32],
272 result = 0;
273
274 for( int i=0; i<=m; i++ ){
275 for( int j=0; j<=n; j++ ){
276 if( i == 0 || j == 0 )
277 suff[i][j] = 0;
278 else if( s1[i-1] == s2[j-1] ){
279 suff[i][j] = suff[i-1][j-1] + 1;
280 result = VG_MAX( result, suff[i][j] );
281 }
282 else
283 suff[i][j] = 0;
284 }
285 }
286
287 return result;
288 }
289
290 /* str must not fuckoff ever! */
291 void console_suggest_score_text( const char *str, const char *input,
292 int minscore )
293 {
294 /* filter duplicates */
295 for( int i=0; i<vg_console.suggestion_count; i++ )
296 if( !strcmp( vg_console.suggestions[i].str, str ) )
297 return;
298
299 /* calc score */
300 u32 score = str_lcs( str, input );
301
302 if( score < minscore )
303 return;
304
305 int best_pos = vg_console.suggestion_count;
306 for( int j=best_pos-1; j>=0; j -- )
307 if( score > vg_console.suggestions[j].lev_score )
308 best_pos = j;
309
310 /* insert if good score */
311 if( best_pos < vg_list_size( vg_console.suggestions ) ){
312 int start = VG_MIN( vg_console.suggestion_count,
313 vg_list_size( vg_console.suggestions )-1 );
314 for( int j=start; j>best_pos; j -- )
315 vg_console.suggestions[j] = vg_console.suggestions[j-1];
316
317 vg_console.suggestions[ best_pos ].str = str;
318 vg_console.suggestions[ best_pos ].len = strlen( str );
319 vg_console.suggestions[ best_pos ].lev_score = score;
320
321 if( vg_console.suggestion_count <
322 vg_list_size( vg_console.suggestions ) )
323 vg_console.suggestion_count ++;
324 }
325 }
326
327 static void console_update_suggestions(void)
328 {
329 if( vg_ui.focused_control_type != k_ui_control_textbox ||
330 vg_ui.textbuf != vg_console.input )
331 return;
332
333 vg_console.suggestion_count = 0;
334 vg_console.suggestion_select = -1;
335 vg_console.suggestion_maxlen = 0;
336
337 /*
338 * - must be typing something
339 * - must be at the end
340 * - prev char must not be a whitespace
341 * - cursors should match
342 */
343
344 if( vg_ui.textbox.cursor_pos == 0 ) return;
345 if( vg_ui.textbox.cursor_pos != vg_ui.textbox.cursor_user ) return;
346 if( vg_console.input[ vg_ui.textbox.cursor_pos ] != '\0' ) return;
347
348 if( (vg_console.input[ vg_ui.textbox.cursor_pos -1 ] == ' ') ||
349 (vg_console.input[ vg_ui.textbox.cursor_pos -1 ] == '\t') )
350 return;
351
352 char temp[128];
353 const char *args[8];
354
355 int token_count = vg_console_tokenize( vg_console.input, temp, args );
356 if( !token_count ) return;
357 vg_console.suggestion_pastepos = args[token_count-1]-temp;
358
359 /* Score all our commands and cvars */
360 if( token_count == 1 ){
361 for( int i=0; i<vg_console.var_count; i++ ){
362 vg_var *cvar = &vg_console.vars[i];
363 console_suggest_score_text( cvar->name, args[0], 1 );
364 }
365
366 for( int i=0; i<vg_console.function_count; i++ ){
367 vg_cmd *cmd = &vg_console.functions[i];
368 console_suggest_score_text( cmd->name, args[0], 1 );
369 }
370 }
371 else{
372 vg_cmd *cmd = vg_console_match_cmd( args[0] );
373 vg_var *var = vg_console_match_var( args[0] );
374
375 if( cmd )
376 if( cmd->poll_suggest )
377 cmd->poll_suggest( token_count-1, &args[1] );
378 }
379
380 /* some post processing */
381 for( int i=0; i<vg_console.suggestion_count; i++ ){
382 vg_console.suggestion_maxlen = VG_MAX( vg_console.suggestion_maxlen,
383 vg_console.suggestions[i].len );
384
385 if( vg_console.suggestions[i].lev_score <
386 vg_console.suggestions[0].lev_score/2 )
387 {
388 vg_console.suggestion_count = i;
389 return;
390 }
391 }
392 }
393
394 /*
395 * Suggestion controls
396 */
397 static void _console_fetch_suggestion(void)
398 {
399 char *target = &vg_console.input[ vg_console.suggestion_pastepos ];
400
401 if( vg_console.suggestion_select == -1 ){
402 strcpy( target, vg_console.input_copy );
403 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
404 &vg_ui.textbox.cursor_pos, 10000, 1 );
405 }
406 else{
407 strncpy( target,
408 vg_console.suggestions[ vg_console.suggestion_select ].str,
409 vg_list_size( vg_console.input )-1 );
410
411 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
412 &vg_ui.textbox.cursor_pos, 10000, 1 );
413 _ui_textbox_put_char( ' ' );
414 }
415 }
416
417 static void _console_suggest_store_normal(void)
418 {
419 if( vg_console.suggestion_select == -1 ){
420 char *target = &vg_console.input[ vg_console.suggestion_pastepos ];
421 strcpy( vg_console.input_copy, target );
422 }
423 }
424
425 static void console_suggest_next(void)
426 {
427 if( vg_console.suggestion_count ){
428 _console_suggest_store_normal();
429
430 vg_console.suggestion_select ++;
431
432 if( vg_console.suggestion_select >= vg_console.suggestion_count )
433 vg_console.suggestion_select = -1;
434
435 _console_fetch_suggestion();
436 }
437 }
438
439 static void console_suggest_prev(void)
440 {
441 if( vg_console.suggestion_count ){
442 _console_suggest_store_normal();
443
444 vg_console.suggestion_select --;
445
446 if( vg_console.suggestion_select < -1 )
447 vg_console.suggestion_select = vg_console.suggestion_count-1;
448
449 _console_fetch_suggestion();
450 }
451 }
452
453 static void _vg_console_on_update( char *buf, u32 len )
454 {
455 if( buf == vg_console.input ){
456 console_update_suggestions();
457 }
458 }
459
460 static void console_history_get( char* buf, int entry_num )
461 {
462 if( !vg_console.history_count )
463 return;
464
465 int offset = VG_MIN( entry_num, vg_console.history_count -1 ),
466 pick = (vg_console.history_last - offset) %
467 vg_list_size( vg_console.history );
468 strcpy( buf, vg_console.history[ pick ] );
469 }
470
471 static void _vg_console_on_up( char *buf, u32 len )
472 {
473 if( buf == vg_console.input ){
474 vg_console.history_pos =
475 VG_MAX
476 (
477 0,
478 VG_MIN
479 (
480 vg_console.history_pos+1,
481 VG_MIN
482 (
483 vg_list_size( vg_console.history ),
484 vg_console.history_count - 1
485 )
486 )
487 );
488
489 console_history_get( vg_console.input, vg_console.history_pos );
490 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
491 &vg_ui.textbox.cursor_pos,
492 vg_list_size(vg_console.input)-1, 1);
493 }
494 }
495
496 static void _vg_console_on_down( char *buf, u32 len )
497 {
498 if( buf == vg_console.input ){
499 vg_console.history_pos = VG_MAX( 0, vg_console.history_pos-1 );
500 console_history_get( vg_console.input, vg_console.history_pos );
501
502 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
503 &vg_ui.textbox.cursor_pos,
504 vg_list_size(vg_console.input)-1, 1 );
505 }
506 }
507
508 static void _vg_console_on_enter( char *buf, u32 len )
509 {
510 if( buf == vg_console.input ){
511 if( !strlen( vg_console.input ) )
512 return;
513
514 vg_info( "%s\n", vg_console.input );
515
516 if( strcmp( vg_console.input,
517 vg_console.history[ vg_console.history_last ]) )
518 {
519 vg_console.history_last = ( vg_console.history_last + 1) %
520 vg_list_size(vg_console.history );
521 vg_console.history_count =
522 VG_MIN( vg_list_size( vg_console.history ),
523 vg_console.history_count + 1 );
524 strcpy( vg_console.history[ vg_console.history_last ],
525 vg_console.input );
526 }
527
528 vg_console.history_pos = -1;
529 vg_execute_console_input( vg_console.input, 0 );
530 _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
531 &vg_ui.textbox.cursor_pos, -10000, 1 );
532
533 vg_console.input[0] = '\0';
534 console_update_suggestions();
535 }
536 }
537
538 int vg_console_exec( int argc, const char *argv[] )
539 {
540 if( argc < 1 ) return 0;
541
542 int silent=0;
543 if( argc == 2 ) silent=1;
544
545 char path[256];
546 strcpy( path, "cfg/" );
547 strncat( path, argv[0], 250 );
548
549 FILE *fp = fopen( path, "r" );
550 if( fp ){
551 char line[256];
552
553 while( fgets( line, sizeof( line ), fp ) ){
554 line[ strcspn( line, "\r\n#" ) ] = 0x00;
555
556 if( line[0] != 0x00 ){
557 vg_execute_console_input( line, silent );
558 }
559 }
560
561 fclose( fp );
562 }
563 else{
564 vg_error( "Could not open '%s'\n", path );
565 }
566
567 return 0;
568 }
569
570
571 void vg_console_init(void)
572 {
573 vg_console_reg_cmd( "list", _vg_console_list, NULL );
574 vg_console_reg_cmd( "exec", vg_console_exec, NULL );
575 vg_console_reg_var( "cheats", &vg_console.cheats, k_var_dtype_i32,
576 #ifdef VG_DEVWINDOW
577 VG_VAR_PERSISTENT
578 #else
579 0
580 #endif
581 );
582 }
583
584 void vg_console_load_autos(void)
585 {
586 vg_console_exec( 2, (const char *[]){ "auto.conf", "silent" } );
587 }
588
589 void vg_console_draw(void)
590 {
591 if( !vg_console.enabled ) return;
592
593 SDL_AtomicLock( &vg_log.print_sl );
594
595 int ptr = vg_log.log_line_current;
596 int const fh = vg_ui.font->line_height, log_lines = 32;
597 int console_lines = VG_MIN( log_lines, vg_log.log_line_count );
598
599 ui_rect rect_log = { 0, 0, vg.window_x, log_lines*fh },
600 rect_input = { 0, log_lines*fh + 1, vg.window_x, fh*2 },
601 rect_line = { 0, 0, vg.window_x, fh };
602
603 /*
604 * log
605 */
606 u32 bg_colour = (ui_colour( k_ui_bg )&0x00ffffff)|0x9f000000;
607
608 ui_fill( rect_log, bg_colour );
609 rect_line[1] = rect_log[1]+rect_log[3]-fh;
610
611 for( int i=0; i<console_lines; i ++ ){
612 ptr --;
613
614 if( ptr < 0 ) ptr = vg_list_size( vg_log.log )-1;
615
616 ui_text( rect_line, vg_log.log[ptr], 1, k_ui_align_left, 0 );
617 rect_line[1] -= fh;
618 }
619
620 /*
621 * Input area
622 */
623
624 struct ui_textbox_callbacks callbacks = {
625 .up = _vg_console_on_up,
626 .down = _vg_console_on_down,
627 .change = _vg_console_on_update,
628 .enter = _vg_console_on_enter,
629 };
630 ui_textbox( rect_input, NULL,
631 vg_console.input, vg_list_size(vg_console.input), 1,
632 UI_TEXTBOX_AUTOFOCUS, &callbacks );
633
634 /*
635 * suggestions
636 */
637 if( vg_console.suggestion_count ){
638 ui_rect rect_suggest;
639 rect_copy( rect_input, rect_suggest );
640
641 rect_suggest[0] += 6 + vg_ui.font->spacing*vg_console.suggestion_pastepos;
642 rect_suggest[1] += rect_input[3];
643 rect_suggest[2] = vg_ui.font->spacing * vg_console.suggestion_maxlen;
644 rect_suggest[3] = vg_console.suggestion_count * fh;
645
646 ui_fill( rect_suggest, bg_colour );
647
648 rect_suggest[3] = fh;
649
650 for( int i=0; i<vg_console.suggestion_count; i ++ ){
651 u32 text_colour;
652 if( i == vg_console.suggestion_select ){
653 ui_fill( rect_suggest, ui_colour( k_ui_orange ) );
654 text_colour = ui_colourcont( k_ui_orange );
655 }
656 else text_colour = ui_colourcont( k_ui_bg );
657
658 ui_text( rect_suggest, vg_console.suggestions[i].str, 1,
659 k_ui_align_left, text_colour );
660
661 rect_suggest[1] += fh;
662 }
663 }
664
665 SDL_AtomicUnlock( &vg_log.print_sl );
666 }