forked from braindx/vbjin-ovr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luaconsole.cpp
799 lines (688 loc) · 22.3 KB
/
luaconsole.cpp
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
#include "resource.h"
#include <assert.h>
#include <process.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
//#include "OpenArchive.h"
#ifdef WIN32
//#include "common.h"
#include "main.h"
//#include "driver.h"
#endif
#include "lua-engine.h"
#define MAX_RECENT_SCRIPTS 15
static char Str_Tmp [1024]; // shadow added because the global one is completely unreliable
char Recent_Scripts[MAX_RECENT_SCRIPTS][1024];
struct ControlLayoutInfo
{
int controlID;
enum LayoutType // what to do when the containing window resizes
{
NONE, // leave the control where it was
RESIZE_END, // resize the control
MOVE_START, // move the control
};
LayoutType horizontalLayout;
LayoutType verticalLayout;
};
struct ControlLayoutState
{
int x,y,width,height;
bool valid;
ControlLayoutState() : valid(false) {}
};
static ControlLayoutInfo controlLayoutInfos [] = {
{IDC_LUACONSOLE, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::RESIZE_END},
{IDC_EDIT_LUAPATH, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::NONE},
{IDC_BUTTON_LUARUN, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE},
{IDC_BUTTON_LUASTOP, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE},
};
static const int numControlLayoutInfos = sizeof(controlLayoutInfos)/sizeof(*controlLayoutInfos);
extern std::vector<HWND> LuaScriptHWnds;
struct LuaPerWindowInfo {
std::string filename;
HANDLE fileWatcherThread;
bool started;
bool closeOnStop;
bool subservient;
int width; int height;
ControlLayoutState layoutState [numControlLayoutInfos];
LuaPerWindowInfo() : fileWatcherThread(NULL), started(false), closeOnStop(false), subservient(false), width(405), height(244) {}
};
std::map<HWND, LuaPerWindowInfo> LuaWindowInfo;
static char Lua_Dir[1024]="";
#undef max
int WINAPI FileSysWatcher (LPVOID arg)
{
HWND hDlg = (HWND)arg;
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
while(true)
{
char filename [1024], directory [1024];
strncpy(filename, info.filename.c_str(), 1024);
filename[1023] = 0;
strcpy(directory, filename);
char* slash = strrchr(directory, '/');
slash = std::max(slash, strrchr(directory, '\\'));
if(slash)
*slash = 0;
char* bar = strchr(filename, '|');
if(bar) *bar = '\0';
WIN32_FILE_ATTRIBUTE_DATA origData;
GetFileAttributesEx (filename, GetFileExInfoStandard, (LPVOID)&origData);
HANDLE hNotify = FindFirstChangeNotification(directory, FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE);
if(hNotify)
{
DWORD dwWaitResult = WaitForSingleObject(hNotify, 500);
if(dwWaitResult != STATUS_TIMEOUT)
{
if(dwWaitResult == WAIT_ABANDONED)
return dwWaitResult;
WIN32_FILE_ATTRIBUTE_DATA data;
GetFileAttributesEx (filename, GetFileExInfoStandard, (LPVOID)&data);
// at this point it could be any file in the directory that changed
// so check to make sure it was the file we care about
if(memcmp(&origData.ftLastWriteTime, &data.ftLastWriteTime, sizeof(FILETIME)))
{
RequestAbortLuaScript((int)hDlg, "terminated to reload the script");
PostMessage(hDlg, WM_COMMAND, IDC_BUTTON_LUARUN, 0);
}
}
//FindNextChangeNotification(hNotify); // let's not try to reuse it...
FindCloseChangeNotification(hNotify); // but let's at least make sure to release it!
}
else
{
Sleep(500);
}
}
return 0;
}
void RegisterWatcherThread (HWND hDlg)
{
HANDLE thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) FileSysWatcher, (LPVOID) hDlg, CREATE_SUSPENDED, NULL);
SetThreadPriority(thread, THREAD_PRIORITY_LOWEST);
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
info.fileWatcherThread = thread;
ResumeThread(thread);
}
void KillWatcherThread (HWND hDlg)
{
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
TerminateThread(info.fileWatcherThread, 0);
info.fileWatcherThread = NULL;
}
// some extensions that might commonly be near lua files that almost certainly aren't lua files.
static const char* s_nonLuaExtensions [] = {"txt", "nfo", "htm", "html", "jpg", "jpeg", "png", "bmp", "gif", "mp3", "wav", "lnk", "exe", "bat", "gmv", "gm2", "luasav", "sav", "srm", "brm", "cfg", "wch", "gs*", "bin","smd","gen","32x","cue","iso","raw"};
void Update_Recent_Script(const char* Path, bool dontPutAtTop)
{
// char LogicalName[1024], PhysicalName[1024];
// bool exists = ObtainFile(Path, LogicalName, PhysicalName, "luacheck", s_nonLuaExtensions, sizeof(s_nonLuaExtensions)/sizeof(*s_nonLuaExtensions));
// ReleaseTempFileCategory("luacheck"); // delete the temporary (physical) file if any
// if(!exists)
// return;
int i;
for(i = 0; i < MAX_RECENT_SCRIPTS; i++)
{
if (!(strcmp(Recent_Scripts[i], Path)))
{
// move recent item to the top of the list
if(i == 0 || dontPutAtTop)
return;
char temp [1024];
strcpy(temp, Recent_Scripts[i]);
int j;
for(j = i; j > 0; j--)
strcpy(Recent_Scripts[j], Recent_Scripts[j-1]);
strcpy(Recent_Scripts[0], temp);
// MustUpdateMenu = 1;
return;
}
}
if(!dontPutAtTop)
{
// add to start of recent list
for(i = MAX_RECENT_SCRIPTS-1; i > 0; i--)
strcpy(Recent_Scripts[i], Recent_Scripts[i - 1]);
strcpy(Recent_Scripts[0], Path);
}
else
{
// add to end of recent list
for(i = 0; i < MAX_RECENT_SCRIPTS; i++)
{
if(!*Recent_Scripts[i])
{
strcpy(Recent_Scripts[i], Path);
break;
}
}
}
// MustUpdateMenu = 1;
}
HWND IsScriptFileOpen(const char* Path)
{
for(std::map<HWND, LuaPerWindowInfo>::iterator iter = LuaWindowInfo.begin(); iter != LuaWindowInfo.end(); ++iter)
{
LuaPerWindowInfo& info = iter->second;
const char* filename = info.filename.c_str();
const char* pathPtr = Path;
// case-insensitive slash-direction-insensitive compare
bool same = true;
while(*filename || *pathPtr)
{
if((*filename == '/' || *filename == '\\') && (*pathPtr == '/' || *pathPtr == '\\'))
{
do {filename++;} while(*filename == '/' || *filename == '\\');
do {pathPtr++;} while(*pathPtr == '/' || *pathPtr == '\\');
}
else if(tolower(*filename) != tolower(*pathPtr))
{
same = false;
break;
}
else
{
filename++;
pathPtr++;
}
}
if(same)
return iter->first;
}
return NULL;
}
void PrintToWindowConsole(int hDlgAsInt, const char* str)
{
HWND hDlg = (HWND)hDlgAsInt;
HWND hConsole = GetDlgItem(hDlg, IDC_LUACONSOLE);
int length = GetWindowTextLength(hConsole);
if(length >= 250000)
{
// discard first half of text if it's getting too long
SendMessage(hConsole, EM_SETSEL, 0, length/2);
SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)"");
length = GetWindowTextLength(hConsole);
}
SendMessage(hConsole, EM_SETSEL, length, length);
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
{
SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)str);
}
}
extern int Show_Genesis_Screen(HWND hWnd);
void OnStart(int hDlgAsInt)
{
HWND hDlg = (HWND)hDlgAsInt;
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
info.started = true;
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), false); // disable browse while running because it misbehaves if clicked in a frameadvance loop
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), true);
SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Restart");
SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), ""); // clear the console
// Show_Genesis_Screen(HWnd); // otherwise we might never show the first thing the script draws
}
void OnStop(int hDlgAsInt, bool statusOK)
{
HWND hDlg = (HWND)hDlgAsInt;
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
HWND prevWindow = GetActiveWindow();
SetActiveWindow(hDlg); // bring to front among other script/secondary windows, since a stopped script will have some message for the user that would be easier to miss otherwise
if(prevWindow == g_hWnd) SetActiveWindow(prevWindow);
info.started = false;
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), true);
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), false);
SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Run");
// if(statusOK)
// Show_Genesis_Screen(MainWindow->getHWnd()); // otherwise we might never show the last thing the script draws
if(info.closeOnStop)
PostMessage(hDlg, WM_CLOSE, 0, 0);
}
const char* MakeScriptPathAbsolute(const char* filename, const char* extraDirToCheck);
void UpdateFileEntered(HWND hDlg)
{
char local_str_tmp [1024];
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_GETTEXT,(WPARAM)512,(LPARAM)local_str_tmp);
// if it exists, make sure we're using an absolute path to it
const char* filename = local_str_tmp;
FILE* file = fopen(filename, "rb");
if(file)
{
fclose(file);
filename = MakeScriptPathAbsolute(local_str_tmp, NULL);
if(filename != local_str_tmp && stricmp(filename, local_str_tmp))
{
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_SETTEXT,(WPARAM)512,(LPARAM)filename);
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,EM_SETSEL,0,-1);
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,EM_SETSEL,-1,-1);
return;
}
}
// use ObtainFile to support opening files within archives
// char LogicalName[1024], PhysicalName[1024];
// bool exists = ObtainFile(filename, LogicalName, PhysicalName, "luacheck", s_nonLuaExtensions, sizeof(s_nonLuaExtensions)/sizeof(*s_nonLuaExtensions));
// bool readonly = exists ? ((GetFileAttributes(PhysicalName) & FILE_ATTRIBUTE_READONLY) != 0) : (strchr(LogicalName, '|') != NULL || strchr(filename, '|') != NULL);
// ReleaseTempFileCategory("luacheck"); // delete the temporary (physical) file if any
bool exists = true;
bool readonly = false;
char LogicalName[1024];
snprintf(LogicalName, MAX_PATH, "%s", filename);
if(exists)
{
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
info.filename = LogicalName;
char* slash = strrchr(LogicalName, '/');
slash = std::max(slash, strrchr(LogicalName, '\\'));
if(slash)
slash++;
else
slash = LogicalName;
SetWindowText(hDlg, slash);
// Build_Main_Menu();
PostMessage(hDlg, WM_COMMAND, IDC_BUTTON_LUARUN, 0);
}
const char* ext = strrchr(LogicalName, '.');
bool isLuaFile = ext && !_stricmp(ext, ".lua");
if(exists)
{
SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUAEDIT), isLuaFile ? (readonly ? "View" : "Edit") : "Open");
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUAEDIT), true);
}
else
{
SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUAEDIT), "Create");
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUAEDIT), isLuaFile && !readonly);
}
}
//extern "C" int Clear_Sound_Buffer(void);
static int Change_File_L(char *Dest, char *Dir, char *Titre, char *Filter, char *Ext, HWND hwnd)
{
OPENFILENAME ofn;
// SetCurrentDirectory(Desmume_Path);
if (!strcmp(Dest, ""))
{
strcpy(Dest, "default.");
strcat(Dest, Ext);
}
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = g_hInstance;
ofn.lpstrFile = Dest;
ofn.nMaxFile = 2047;
ofn.lpstrFilter = Filter;
ofn.nFilterIndex = 1;
ofn.lpstrInitialDir = Dir;
ofn.lpstrTitle = Titre;
ofn.lpstrDefExt = Ext;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&ofn)) return 1;
return 0;
}
LRESULT CALLBACK LuaScriptProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
RECT r;
RECT r2;
int dx1, dy1, dx2, dy2;
switch(uMsg)
{
case WM_INITDIALOG: {
if(std::find(LuaScriptHWnds.begin(), LuaScriptHWnds.end(), hDlg) == LuaScriptHWnds.end())
{
LuaScriptHWnds.push_back(hDlg);
// Build_Main_Menu();
}
// if (Full_Screen)
// {
// while (ShowCursor(false) >= 0);
// while (ShowCursor(true) < 0);
// }
// HANDLE hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_LUA));
// SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
// remove the 30000 character limit from the console control
SendMessage(GetDlgItem(hDlg, IDC_LUACONSOLE),EM_LIMITTEXT,0,0);
GetWindowRect(g_hWnd, &r);
dx1 = (r.right - r.left) / 2;
dy1 = (r.bottom - r.top) / 2;
GetWindowRect(hDlg, &r2);
dx2 = (r2.right - r2.left) / 2;
dy2 = (r2.bottom - r2.top) / 2;
int windowIndex = std::find(LuaScriptHWnds.begin(), LuaScriptHWnds.end(), hDlg) - LuaScriptHWnds.begin();
int staggerOffset = windowIndex * 24;
r.left += staggerOffset;
r.right += staggerOffset;
r.top += staggerOffset;
r.bottom += staggerOffset;
// push it away from the main window if we can
const int width = (r.right-r.left);
const int width2 = (r2.right-r2.left);
if(r.left+width2 + width < GetSystemMetrics(SM_CXSCREEN))
{
r.right += width;
r.left += width;
}
else if((int)r.left - (int)width2 > 0)
{
r.right -= width2;
r.left -= width2;
}
SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
LuaPerWindowInfo info;
{
RECT r3;
GetClientRect(hDlg, &r3);
info.width = r3.right - r3.left;
info.height = r3.bottom - r3.top;
}
LuaWindowInfo[hDlg] = info;
RegisterWatcherThread(hDlg);
OpenLuaContext((int)hDlg, PrintToWindowConsole, OnStart, OnStop);
DragAcceptFiles(hDlg, TRUE);
return true;
} break;
case WM_MENUSELECT:
case WM_ENTERSIZEMOVE:
// Clear_Sound_Buffer();
break;
case WM_SIZING:
{
// enforce a minimum window size
LPRECT r = (LPRECT) lParam;
int minimumWidth = 333;
int minimumHeight = 117;
if(r->right - r->left < minimumWidth)
if(wParam == WMSZ_LEFT || wParam == WMSZ_TOPLEFT || wParam == WMSZ_BOTTOMLEFT)
r->left = r->right - minimumWidth;
else
r->right = r->left + minimumWidth;
if(r->bottom - r->top < minimumHeight)
if(wParam == WMSZ_TOP || wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOPRIGHT)
r->top = r->bottom - minimumHeight;
else
r->bottom = r->top + minimumHeight;
}
return TRUE;
case WM_SIZE:
{
// resize or move controls in the window as necessary when the window is resized
LuaPerWindowInfo& windowInfo = LuaWindowInfo[hDlg];
int prevDlgWidth = windowInfo.width;
int prevDlgHeight = windowInfo.height;
int dlgWidth = LOWORD(lParam);
int dlgHeight = HIWORD(lParam);
int deltaWidth = dlgWidth - prevDlgWidth;
int deltaHeight = dlgHeight - prevDlgHeight;
for(int i = 0; i < numControlLayoutInfos; i++)
{
ControlLayoutInfo layoutInfo = controlLayoutInfos[i];
ControlLayoutState& layoutState = windowInfo.layoutState[i];
HWND hCtrl = GetDlgItem(hDlg,layoutInfo.controlID);
int x,y,width,height;
if(layoutState.valid)
{
x = layoutState.x;
y = layoutState.y;
width = layoutState.width;
height = layoutState.height;
}
else
{
RECT r;
GetWindowRect(hCtrl, &r);
POINT p = {r.left, r.top};
ScreenToClient(hDlg, &p);
x = p.x;
y = p.y;
width = r.right - r.left;
height = r.bottom - r.top;
}
switch(layoutInfo.horizontalLayout)
{
case ControlLayoutInfo::RESIZE_END: width += deltaWidth; break;
case ControlLayoutInfo::MOVE_START: x += deltaWidth; break;
default: break;
}
switch(layoutInfo.verticalLayout)
{
case ControlLayoutInfo::RESIZE_END: height += deltaHeight; break;
case ControlLayoutInfo::MOVE_START: y += deltaHeight; break;
default: break;
}
SetWindowPos(hCtrl, 0, x,y, width,height, 0);
layoutState.x = x;
layoutState.y = y;
layoutState.width = width;
layoutState.height = height;
layoutState.valid = true;
}
windowInfo.width = dlgWidth;
windowInfo.height = dlgHeight;
RedrawWindow(hDlg, NULL, NULL, RDW_INVALIDATE);
}
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_BUTTON_LUABROWSE:
{
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
char Str_Tmp [1024]; // shadow added because the global one is unreliable
strcpy(Str_Tmp,info.filename.c_str());
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_GETTEXT,(WPARAM)512,(LPARAM)Str_Tmp);
char* bar = strchr(Str_Tmp, '|');
if(bar) *bar = '\0';
// DialogsOpen++;
// Clear_Sound_Buffer();
if(Change_File_L(Str_Tmp, Lua_Dir, "Load Lua Script", "Lua Script\0*.lua*\0All Files\0*.*\0\0", "lua", hDlg))
{
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_SETTEXT,0,(LPARAM)Str_Tmp);
}
// DialogsOpen--;
} break;
case IDC_BUTTON_LUAEDIT:
{
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
char Str_Tmp [1024]; // shadow added because the global one is unreliable
strcpy(Str_Tmp,info.filename.c_str());
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_GETTEXT,(WPARAM)512,(LPARAM)Str_Tmp);
char LogicalName[1024], PhysicalName[1024];
// bool exists = ObtainFile(Str_Tmp, LogicalName, PhysicalName, "luaview", s_nonLuaExtensions, sizeof(s_nonLuaExtensions)/sizeof(*s_nonLuaExtensions));
snprintf(LogicalName, MAX_PATH, "%s", Str_Tmp);
strcpy(PhysicalName, Str_Tmp);
bool exists = true;
bool created = false;
if(!exists)
{
FILE* file = fopen(Str_Tmp, "r");
if(!file)
{
file = fopen(Str_Tmp, "w");
if(file)
{
created = true;
exists = true;
strcpy(PhysicalName, Str_Tmp);
}
}
if(file)
fclose(file);
}
if(exists)
{
// tell the OS to open the file with its associated editor,
// without blocking on it or leaving a command window open.
if((int)ShellExecute(NULL, "edit", PhysicalName, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC)
if((int)ShellExecute(NULL, "open", PhysicalName, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC)
ShellExecute(NULL, NULL, "notepad", PhysicalName, NULL, SW_SHOWNORMAL);
}
if(created)
{
UpdateFileEntered(hDlg);
}
} break;
case IDC_EDIT_LUAPATH:
{
switch(HIWORD(wParam))
{
case EN_CHANGE:
{
UpdateFileEntered(hDlg);
} break;
}
} break;
case IDC_BUTTON_LUARUN:
{
HWND focus = GetFocus();
HWND textbox = GetDlgItem(hDlg, IDC_EDIT_LUAPATH);
if(focus != textbox)
SetActiveWindow(g_hWnd);
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
strcpy(Str_Tmp,info.filename.c_str());
char LogicalName[1024], PhysicalName[1024];
// bool exists = ObtainFile(Str_Tmp, LogicalName, PhysicalName, "luarun", s_nonLuaExtensions, sizeof(s_nonLuaExtensions)/sizeof(*s_nonLuaExtensions));
snprintf(LogicalName, MAX_PATH, "%s", Str_Tmp);
snprintf(PhysicalName, MAX_PATH, "%s", Str_Tmp);
Update_Recent_Script(LogicalName, info.subservient);
RunLuaScriptFile((int)hDlg, PhysicalName);
} break;
case IDC_BUTTON_LUASTOP:
{
PrintToWindowConsole((int)hDlg, "user clicked stop button\r\n");
SetActiveWindow(g_hWnd);
StopLuaScript((int)hDlg);
} break;
case IDC_NOTIFY_SUBSERVIENT:
{
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
info.subservient = lParam ? true : false;
} break;
//case IDOK:
case IDCANCEL:
{ LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
if(info.filename.empty())
{
// if (Full_Screen)
// {
// while (ShowCursor(true) < 0);
// while (ShowCursor(false) >= 0);
// }
// DialogsOpen--;
DragAcceptFiles(hDlg, FALSE);
KillWatcherThread(hDlg);
LuaScriptHWnds.erase(remove(LuaScriptHWnds.begin(), LuaScriptHWnds.end(), hDlg), LuaScriptHWnds.end());
LuaWindowInfo.erase(hDlg);
CloseLuaContext((int)hDlg);
// Build_Main_Menu();
EndDialog(hDlg, true);
}
} return true;
}
return false;
} break;
case WM_CLOSE:
{
LuaPerWindowInfo& info = LuaWindowInfo[hDlg];
PrintToWindowConsole((int)hDlg, "user closed script window\r\n");
StopLuaScript((int)hDlg);
if(info.started)
{
// not stopped yet, wait to close until we are, otherwise we'll crash
info.closeOnStop = true;
return false;
}
// if (Full_Screen)
// {
// while (ShowCursor(true) < 0);
// while (ShowCursor(false) >= 0);
// }
// DialogsOpen--;
DragAcceptFiles(hDlg, FALSE);
KillWatcherThread(hDlg);
LuaScriptHWnds.erase(remove(LuaScriptHWnds.begin(), LuaScriptHWnds.end(), hDlg), LuaScriptHWnds.end());
LuaWindowInfo.erase(hDlg);
CloseLuaContext((int)hDlg);
// Build_Main_Menu();
EndDialog(hDlg, true);
} return true;
case WM_DROPFILES:
{
HDROP hDrop = (HDROP)wParam;
DragQueryFile(hDrop, 0, Str_Tmp, 1024);
DragFinish(hDrop);
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_SETTEXT,0,(LPARAM)Str_Tmp );
UpdateFileEntered(hDlg);
} return true;
}
return false;
}
char Desmume_Path [1024];
static const char* PathWithoutPrefixDotOrSlash(const char* path)
{
while(*path &&
((*path == '.' && (path[1] == '\\' || path[1] == '/')) ||
*path == '\\' || *path == '/' || *path == ' '))
path++;
return path;
}
const char* MakeScriptPathAbsolute(const char* filename, const char* extraDirToCheck)
{
static char filename2 [1024];
if(filename[0] && filename[1] != ':')
{
char tempFile [1024], curDir [1024];
strncpy(tempFile, filename, 1024);
tempFile[1023] = 0;
const char* tempFilePtr = PathWithoutPrefixDotOrSlash(tempFile);
for(int i=0; i<=4; i++)
{
if((!*tempFilePtr || tempFilePtr[1] != ':') && i != 2)
strcpy(curDir, i!=1 ? ((i!=3||!extraDirToCheck) ? Lua_Dir : extraDirToCheck) : Desmume_Path);
else
curDir[0] = 0;
_snprintf(filename2, 1024, "%s%s", curDir, tempFilePtr);
char* bar = strchr(filename2, '|');
if(bar) *bar = 0;
FILE* file = fopen(filename2, "rb");
if(bar) *bar = '|';
if(file || i==4)
filename = filename2;
if(file)
{
fclose(file);
break;
}
}
}
return filename;
}
extern void RequestAbortLuaScript(int uid, const char* message);
const char* OpenLuaScript(const char* filename, const char* extraDirToCheck, bool makeSubservient)
{
if(LuaScriptHWnds.size() < 16)
{
// make the filename absolute before loading
filename = MakeScriptPathAbsolute(filename, extraDirToCheck);
// now check if it's already open and load it if it isn't
HWND IsScriptFileOpen(const char* Path);
HWND scriptHWnd = IsScriptFileOpen(filename);
if(!scriptHWnd)
{
HWND prevWindow = GetActiveWindow();
HWND hDlg = CreateDialog(g_hInstance, MAKEINTRESOURCE(IDD_LUA), g_hWnd, (DLGPROC) LuaScriptProc);
SendMessage(hDlg,WM_COMMAND,IDC_NOTIFY_SUBSERVIENT,TRUE);
SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_SETTEXT,0,(LPARAM)filename);
// DialogsOpen++;
SetActiveWindow(prevWindow);
}
else
{
RequestAbortLuaScript((int)scriptHWnd, "terminated to restart because of a call to gens.openscript");
SendMessage(scriptHWnd, WM_COMMAND, IDC_BUTTON_LUARUN, 0);
}
}
else return "Too many script windows are already open.";
return NULL;
}