-
Notifications
You must be signed in to change notification settings - Fork 46
/
lutro.c
905 lines (751 loc) · 21.8 KB
/
lutro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
#include "lutro.h"
#include "runtime.h"
#include "unzip.h"
#include "image.h"
#include "graphics.h"
#include "input.h"
#include "audio.h"
#include "event.h"
#include "keyboard.h"
#include "sound.h"
#include "filesystem.h"
#include "system.h"
#include "timer.h"
#include "lutro_math.h"
#include "lutro_window.h"
#include "live.h"
#include "mouse.h"
#include "joystick.h"
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <compat/strl.h>
#include <ctype.h>
#ifdef HAVE_JIT
#include "luajit.h"
#endif
// LuaUTF8
#include "deps/luautf8/lutf8lib.h"
// LuaSocket
#ifdef HAVE_LUASOCKET
#include "deps/luasocket/luasocket.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#if !defined(_MSC_VER)
#ifndef __CELLOS_LV2__
#include <libgen.h>
#endif
#include <unistd.h>
#endif
static lua_State *L;
static int16_t input_cache[16];
int g_lua_stack = 0;
static int32_t allocation_count = 0;
lutro_settings_t settings = {
.width = 320,
.height = 240,
.pitch = 0,
.framebuffer = NULL,
.live_enable = 0,
.live_call_load = 0,
.input_cb = NULL,
.delta = 0,
.deltaCounter = 0,
.frameCounter = 0,
.fps = 0
};
struct retro_perf_callback perf_cb;
#if 0
static void dumpstack( lua_State* L )
{
int top = lua_gettop( L );
for ( int i = 1; i <= top; i++ )
{
printf( "%2d %3d ", i, i - top - 1 );
lua_pushvalue( L, i );
switch ( lua_type( L, -1 ) )
{
case LUA_TNIL:
printf( "nil\n" );
break;
case LUA_TNUMBER:
printf( "%e\n", lua_tonumber( L, -1 ) );
break;
case LUA_TBOOLEAN:
printf( "%s\n", lua_toboolean( L, -1 ) ? "true" : "false" );
break;
case LUA_TSTRING:
printf( "\"%s\"\n", lua_tostring( L, -1 ) );
break;
case LUA_TTABLE:
printf( "table\n" );
break;
case LUA_TFUNCTION:
printf( "function\n" );
break;
case LUA_TUSERDATA:
printf( "userdata\n" );
break;
case LUA_TTHREAD:
printf( "thread\n" );
break;
case LUA_TLIGHTUSERDATA:
printf( "light userdata\n" );
break;
default:
printf( "?\n" );
break;
}
}
lua_settop( L, top );
}
#endif
int _lutro_assertf_internal(int ignorable, const char *fmt, ...)
{
fflush(NULL);
va_list argptr;
va_start(argptr, fmt);
vfprintf(stderr, fmt, argptr);
va_end(argptr);
// tips: the fmt input should always be in the predefined format of:
// FILE(LINE): assertion `cond` failed.
// example:
// lutro.cpp(444): assertion `x > 0` failed.
//
// We can use this knowledge to parse the file and line positions and perform additional clever filtering
// or log prep/routing.
int top = lua_gettop(L);
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_pushstring(L, "");
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
fflush(NULL);
const char* msg = lua_tostring(L, -1);
while (*msg == '\r' || *msg == '\n') ++msg; // lua pads some newlines at the beginning.. strip 'em
fprintf(stderr, "%s\n", msg);
lua_pop(L, 1);
if (ignorable)
{
// TODO : suspend the core, show up some user dialog via libretro api?
// (it could even have lots of info, like lua stacktrace... )
// return 0 if ignored.
}
return 1;
}
static int lutro_lua_panic (lua_State *L) {
// currently this aborts, but it is also possible to set up a setjmp/longmp handler for
// these, as the vast majority of them _are_ recoverable, in the sense that we can usually
// bounce all the way out to retro_run and continue executing some unrelated system.
// For example, if keyboard or pad or audio APIs panic, this doesn't have to stop video,
// or other portions of the engine, fron continuing on (usually).
// If implementing longjmp, it would be nice to change this into a play_assert (ignorable).
fprintf(stderr, "lua_panic!\n%s\n", lua_tostring(L, -1));
abort();
}
int traceback(lua_State *L) {
// use lua's provided debug.traceback to get a contextual error.
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
// generally we don't want to stop/assert on lua errors. The majority are ignorable/recoverable
// and the better strategy is to surface the info the the content creator via some interface
// that's slightly more friendly than a console window. (eg, something that captures all the spam
// and produces a summary report of unique errors)
//tool_errorf("%s\n", lua_tostring(L, -1));
fflush(NULL);
fprintf(stderr, "%s\n", lua_tostring(L, -1));
return 1;
}
int lutro_pcall(lua_State *L, int narg, int nret)
{
int handler = lua_gettop(L) - narg - 1;
while (handler && (lua_tocfunction(L, handler) != traceback)) --handler;
dbg_assert(lua_tocfunction(L, handler) == traceback);
return lua_pcall(L, narg, nret, handler);
}
static int dofile(lua_State *L, const char *path)
{
int res;
lua_pushcfunction(L, traceback);
res = luaL_loadfile(L, path);
if (res)
return res;
res = lutro_pcall(L, 0, LUA_MULTRET);
return res;
}
static int lutro_core_preload(lua_State *L)
{
lutro_ensure_global_table(L, "lutro");
return 1;
}
static void init_settings(lua_State *L)
{
lutro_ensure_global_table(L, "lutro");
lua_newtable(L);
lua_pushnumber(L, settings.width);
lua_setfield(L, -2, "width");
lua_pushnumber(L, settings.height);
lua_setfield(L, -2, "height");
lua_setfield(L, -2, "settings");
lua_pop(L, 1);
}
void lutro_init()
{
L = luaL_newstate();
// handler for errors thatr occur outside pcall() scope
lua_atpanic(L, &lutro_lua_panic);
luaL_openlibs(L);
#ifdef HAVE_JIT
luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
#endif
lutro_checked_stack_begin();
init_settings(L);
lutro_preload(L, lutro_core_preload, "lutro");
lutro_preload(L, lutro_image_preload, "lutro.image");
lutro_preload(L, lutro_graphics_preload, "lutro.graphics");
lutro_preload(L, lutro_audio_preload, "lutro.audio");
lutro_preload(L, lutro_event_preload, "lutro.event");
lutro_preload(L, lutro_sound_preload, "lutro.sound");
lutro_preload(L, lutro_input_preload, "lutro.input");
lutro_preload(L, lutro_filesystem_preload, "lutro.filesystem");
lutro_preload(L, lutro_keyboard_preload, "lutro.keyboard");
lutro_preload(L, lutro_system_preload, "lutro.system");
lutro_preload(L, lutro_timer_preload, "lutro.timer");
lutro_preload(L, lutro_math_preload, "lutro.math");
lutro_preload(L, lutro_window_preload, "lutro.window");
lutro_preload(L, lutro_mouse_preload, "lutro.mouse");
lutro_preload(L, lutro_joystick_preload, "lutro.joystick");
// UTF8
lutro_preload(L, luaopen_luautf8, "utf8");
// LuaSocket
#ifdef HAVE_LUASOCKET
_o_open(L);
#endif
#ifdef HAVE_INOTIFY
lutro_preload(L, lutro_live_preload, "lutro.live");
#endif
// if any of these requires fail, the checked stack assertion at the end will
// be triggered. remember that assertions are only avaialable in debug mode.
lutro_require(L, "lutro", 1);
lutro_require(L, "lutro.image", 1);
lutro_require(L, "lutro.graphics", 1);
lutro_require(L, "lutro.audio", 1);
lutro_require(L, "lutro.event", 1);
lutro_require(L, "lutro.sound", 1);
lutro_require(L, "lutro.keyboard", 1);
lutro_require(L, "lutro.input", 1);
lutro_require(L, "lutro.filesystem", 1);
lutro_require(L, "lutro.system", 1);
lutro_require(L, "lutro.timer", 1);
lutro_require(L, "lutro.math", 1);
lutro_require(L, "lutro.window", 1);
lutro_require(L, "lutro.mouse", 1);
lutro_require(L, "lutro.joystick", 1);
#ifdef HAVE_INOTIFY
lutro_require(L, "lutro.live", 1);
#endif
// Mirror the lutro namespace to "love".
luaL_dostring(L, "love = lutro");
// Initialize the filesystem.
lutro_filesystem_init();
lutro_checked_stack_assert(0);
}
void lutro_deinit()
{
#ifdef HAVE_INOTIFY
if (settings.live_enable)
lutro_live_deinit();
#endif
lutro_audio_stop_all(L);
mixer_unref_stopped_sounds(L);
lua_gc(L, LUA_GCSTEP, 0);
lua_close(L);
lutro_audio_deinit();
lutro_filesystem_deinit();
lutro_print_allocation();
}
void lutro_mixer_render(int16_t* buffer)
{
if (!L) return;
mixer_render(L, buffer);
}
int lutro_set_package_path(lua_State* L, const char* path)
{
const char *cur_path;
char new_path[PATH_MAX_LENGTH];
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
cur_path = lua_tostring( L, -1);
strlcpy(new_path, cur_path, sizeof(new_path));
strlcat(new_path, path, sizeof(new_path));
lua_pop(L, 1);
lua_pushstring(L, new_path) ;
lua_setfield(L, -2, "path");
lua_pop(L, 1);
return 1;
}
int lutro_unzip(const char *path, const char *extraction_directory)
{
path_mkdir(extraction_directory);
unzFile *zipfile = unzOpen(path);
if ( zipfile == NULL )
{
printf("%s: not found\n", path);
return -1;
}
unz_global_info global_info;
if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
{
printf("could not read file global info\n");
unzClose(zipfile);
return -1;
}
char read_buffer[8192];
uLong i;
for (i = 0; i < global_info.number_entry; ++i)
{
unz_file_info file_info;
char filename[PATH_MAX_LENGTH];
if (unzGetCurrentFileInfo(zipfile, &file_info, filename, PATH_MAX_LENGTH,
NULL, 0, NULL, 0 ) != UNZ_OK)
{
printf( "could not read file info\n" );
unzClose( zipfile );
return -1;
}
const size_t filename_length = strlen(filename);
if (filename[filename_length-1] == '/')
{
//printf("dir:%s\n", filename);
char abs_path[PATH_MAX_LENGTH];
fill_pathname_join(abs_path,
extraction_directory, filename, sizeof(abs_path));
path_mkdir(abs_path);
}
else
{
//printf("file:%s\n", filename);
if (unzOpenCurrentFile(zipfile) != UNZ_OK)
{
printf("could not open file\n");
unzClose(zipfile);
return -1;
}
char abs_path[PATH_MAX_LENGTH];
fill_pathname_join(abs_path,
extraction_directory, filename, sizeof(abs_path));
FILE *out = fopen(abs_path, "wb");
if (out == NULL)
{
printf("could not open destination file\n");
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return -1;
}
int error = UNZ_OK;
do
{
error = unzReadCurrentFile(zipfile, read_buffer, 8192);
if (error < 0)
{
printf("error %d\n", error);
unzCloseCurrentFile(zipfile);
unzClose(zipfile);
return -1;
}
if (error > 0)
fwrite(read_buffer, error, 1, out);
} while (error > 0);
fclose(out);
}
unzCloseCurrentFile(zipfile);
if (i + 1 < global_info.number_entry)
{
if (unzGoToNextFile(zipfile) != UNZ_OK)
{
printf("cound not read next file\n");
unzClose(zipfile);
return -1;
}
}
}
unzClose(zipfile);
return 0;
}
int lutro_load(const char *path)
{
char mainfile[PATH_MAX_LENGTH];
// conf.lua https://love2d.org/wiki/Config_Files
char conffile[PATH_MAX_LENGTH];
char gamedir[PATH_MAX_LENGTH];
strlcpy(mainfile, path, PATH_MAX_LENGTH);
strlcpy(conffile, path, PATH_MAX_LENGTH);
strlcpy(gamedir, path, PATH_MAX_LENGTH);
if (!path_is_directory(mainfile)) {
path_basedir(gamedir);
}
// Loading a .lutro file.
if (!strcmp(path_get_extension(mainfile), "lutro"))
{
fill_pathname(gamedir, mainfile, "/", sizeof(gamedir));
fill_pathname(gamedir, conffile, "/", sizeof(gamedir));
lutro_unzip(mainfile, gamedir);
}
fill_pathname_join(mainfile, gamedir, "main.lua", sizeof(mainfile));
if (!filestream_exists(mainfile))
fill_pathname_join(mainfile, gamedir, "main.luac", sizeof(mainfile));
fill_pathname_join(conffile, gamedir, "conf.lua", sizeof(conffile));
if (!filestream_exists(conffile))
fill_pathname_join(conffile, gamedir, "conf.luac", sizeof(conffile));
fill_pathname_slash(gamedir, sizeof(gamedir));
char package_path[PATH_MAX_LENGTH];
snprintf(package_path, PATH_MAX_LENGTH, ";%s?.lua;%s?.luac;%s?/init.lua", gamedir, gamedir, gamedir);
lutro_set_package_path(L, package_path);
// Load the configuration file, ignoring any errors.
dofile(L, conffile);
// Now that configuration is in place, load main.lua.
if (dofile(L, mainfile))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
return 0;
}
int oldtop = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_getglobal(L, "lutro");
int tbl_top_lutro = lua_gettop(L);
strlcpy(settings.gamedir, gamedir, PATH_MAX_LENGTH);
lua_getfield(L, -1, "conf");
// Process the custom configuration, if it exists.
if (lua_isfunction(L, -1))
{
lua_getfield(L, -2, "settings");
if(lutro_pcall(L, 1, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
return 0;
}
// no stack cleanup necessary inside oldtop scope.
lua_getfield(L, -1, "settings");
lua_getfield(L, -1, "width");
lua_getfield(L, -2, "height");
lua_getfield(L, -3, "live_enable");
lua_getfield(L, -4, "live_call_load");
settings.width = lua_tointeger(L, -4);
settings.height = lua_tointeger(L, -3);
settings.live_enable = lua_toboolean(L, -2);
settings.live_call_load = lua_toboolean(L, -1);
}
lutro_graphics_init(L);
lutro_audio_init(L);
lutro_event_init();
lutro_math_init();
lutro_joystick_init();
#ifdef HAVE_INOTIFY
if (settings.live_enable)
lutro_live_init();
#endif
lua_settop(L, tbl_top_lutro);
lua_getfield(L, -1, "load");
int result = 1;
// Check if lutro.load() exists.
if (lua_isfunction(L, -1))
{
// It exists, so call lutro.load().
if(lutro_pcall(L, 0, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
result = 0;
}
}
lua_settop(L, oldtop);
return 1;
}
void lutro_gamepadevent(lua_State* L)
{
ENTER_LUA_STACK
unsigned i;
for (i = 0; i < 16; i++)
{
int16_t is_down = settings.input_cb(0, RETRO_DEVICE_JOYPAD, 0, i);
if (is_down != input_cache[i])
{
lua_getglobal(L, "lutro");
lua_getfield(L, -1, is_down ? "gamepadpressed" : "gamepadreleased");
if (lua_isfunction(L, -1))
{
lua_pushnumber(L, i);
lua_pushstring(L, input_find_name(joystick_enum, i));
if (lutro_pcall(L, 2, 0)) // takes care of poping the function too
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
input_cache[i] = is_down;
}
else
{
lua_pop(L, 1); // pop getfield gamepadpressed or gamepadreleased
}
lua_pop(L, 1); // pop getglobal lutro
}
}
EXIT_LUA_STACK
}
void lutro_run(double delta)
{
// Update the Delta and FPS.
settings.delta = delta;
settings.deltaCounter += delta;
settings.frameCounter += 1;
settings.fps = 1 / delta;
if (settings.deltaCounter >= 1.0) {
settings.frameCounter = 0;
settings.deltaCounter = 0;
}
#ifdef HAVE_INOTIFY
if (settings.live_enable)
lutro_live_update(L);
#endif
int oldtop = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_getglobal(L, "lutro");
lua_getfield(L, -1, "update");
if (lua_isfunction(L, -1))
{
lua_pushnumber(L, delta);
if(lutro_pcall(L, 1, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
lua_getfield(L, -1, "draw");
if (lua_isfunction(L, -1))
{
lutro_graphics_begin_frame(L);
if(lutro_pcall(L, 0, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
lutro_graphics_end_frame(L);
}
lutro_keyboardevent(L);
lutro_gamepadevent(L);
lutro_mouseevent(L);
lutro_joystickevent(L);
lua_settop(L, oldtop);
mixer_unref_stopped_sounds(L);
lua_gc(L, LUA_GCSTEP, 0);
}
void lutro_reset()
{
int oldtop = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_getglobal(L, "lutro");
lua_getfield(L, -1, "reset");
if (lua_isfunction(L, -1))
{
lutro_audio_stop_all(L);
if(lutro_pcall(L, 0, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
lua_settop(L, oldtop);
lua_gc(L, LUA_GCSTEP, 0);
}
size_t lutro_serialize_size()
{
size_t size = 0;
int oldtop = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_getglobal(L, "lutro");
lua_getfield(L, -1, "serializeSize");
if (lua_isfunction(L, -1))
{
if (lutro_pcall(L, 0, 1))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
if (lua_isnumber(L, -1))
size = lua_tonumber(L, -1);
else
tool_assertf(false, "Invalid type returned from lutro.serializeSize. An integer result is expected.\n");
}
lua_settop(L, oldtop);
lua_gc(L, LUA_GCSTEP, 0);
return size;
}
bool lutro_serialize(void *data_, size_t size)
{
int oldtop = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_getglobal(L, "lutro");
lua_getfield(L, -1, "serialize");
if (lua_isfunction(L, -1))
{
lua_pushnumber(L, size);
if (lutro_pcall(L, 1, 1))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
else
{
const char* data = lua_tostring(L, -1);
lua_pop(L, 1);
memset(data_, 0, size);
memcpy(data_, data, strlen(data));
}
}
lua_settop(L, oldtop);
lua_gc(L, LUA_GCSTEP, 0);
return true;
}
bool lutro_unserialize(const void *data_, size_t size)
{
int oldtop = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_getglobal(L, "lutro");
lua_getfield(L, -1, "unserialize");
if (lua_isfunction(L, -1))
{
lua_pushstring(L, data_);
lua_pushnumber(L, size);
if (lutro_pcall(L, 2, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
lua_settop(L, oldtop);
lua_gc(L, LUA_GCSTEP, 0);
return true;
}
void lutro_cheat_set(unsigned index, bool enabled, const char *code)
{
int oldtop = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_getglobal(L, "lutro");
lua_getfield(L, -1, "cheat_set");
if (lua_isfunction(L, -1))
{
lua_pushnumber(L, index);
lua_pushboolean(L, enabled);
lua_pushstring(L, code);
if (lutro_pcall(L, 3, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
lua_settop(L, oldtop);
lua_gc(L, LUA_GCSTEP, 0);
}
void lutro_cheat_reset()
{
int oldtop = lua_gettop(L);
lua_pushcfunction(L, traceback);
lua_getglobal(L, "lutro");
lua_getfield(L, -1, "cheat_reset");
if (lua_isfunction(L, -1))
{
if (lutro_pcall(L, 0, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
lua_settop(L, oldtop);
lua_gc(L, LUA_GCSTEP, 0);
}
void lutro_assetPath_init(AssetPathInfo* dest, const char* path)
{
assert (dest);
strlcpy(dest->fullpath, settings.gamedir, sizeof(dest->fullpath));
strlcat(dest->fullpath, path, sizeof(dest->fullpath));
//get file extension
strcpy(dest->ext, path_get_extension(path));
for(int i = 0; dest->ext[i]; i++)
dest->ext[i] = tolower((uint8_t)dest->ext[i]);
}
void *lutro_malloc_internal(size_t size, const char* debug, int line)
{
void *a = malloc(size);
#if TRACE_ALLOCATION
if (a) {
fprintf(stderr,"TRACE ALLOC:%p:malloc:%s:%d\n", a, debug, line);
allocation_count++;
} else {
fprintf(stderr,"TRACE ALLOC:failure:malloc:%s:%d\n", debug, line);
}
#endif
return a;
}
void lutro_free_internal(void *ptr, const char* debug, int line)
{
#if TRACE_ALLOCATION
// Don't trace nop
if (ptr) {
fprintf(stderr,"TRACE ALLOC:%p:free:%s:%d\n", ptr, debug, line);
allocation_count--;
}
#endif
free(ptr);
}
void *lutro_calloc_internal(size_t nmemb, size_t size, const char* debug, int line)
{
void *a = calloc(nmemb, size);
#if TRACE_ALLOCATION
if (a) {
fprintf(stderr,"TRACE ALLOC:%p:calloc:%s:%d\n", a, debug, line);
allocation_count++;
} else {
fprintf(stderr,"TRACE ALLOC:failure:calloc:%s:%d\n", debug, line);
}
#endif
return a;
}
void *lutro_realloc_internal(void *ptr, size_t size, const char* debug, int line)
{
void *a = realloc(ptr, size);
#if TRACE_ALLOCATION
if (a) {
if (ptr == NULL) {
// If original pointer is null, realloc behave as a malloc
fprintf(stderr,"TRACE ALLOC:%p:realloc (malloc):%s:%d\n", a, debug, line);
// Note even if size is 0, realloc can return a pointer suitable to
// be passed to free
allocation_count++;
} else {
fprintf(stderr,"TRACE ALLOC:%p:realloc (move from %p):%s:%d\n", a, ptr, debug, line);
}
} else {
// Either realloc fail, or it released the memory
if (ptr != NULL && size == 0) {
// If size is 0, realloc behave as a free
fprintf(stderr,"TRACE ALLOC:null:realloc (free %p):%s:%d\n", ptr, debug, line);
allocation_count--;
} else {
fprintf(stderr,"TRACE ALLOC:failure:realloc (%p):%s:%d\n", ptr, debug, line);
}
}
#endif
return a;
}
void lutro_print_allocation() {
#if TRACE_ALLOCATION
fprintf(stderr,"TRACE ALLOC:total pending allocations:%d\n", allocation_count);
#endif
}