Debian Codegraph @

editor/ScriptMassCompile.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/editor/ScriptMassCompile.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:39 $
23 * $Author: kevinb $
24 *
25 * Implementation of mass script compile dialog
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 9 4/03/99 1:36a Jeff
30 * added no-check out option for mass compiler
31 *
32 * 8 2/25/99 12:34p Jeff
33 * don't display message if someone has a file checked out during mass
34 * compile
35 *
36 * 7 2/21/99 8:05p Jeff
37 * better handling of out-of-sync scripts
38 *
39 * 6 1/11/99 3:34p Jeff
40 * added checks for when going editor->game to see if scripts are out of
41 * date, if so give the option of breaking out. Add some options to mass
42 * script compiler, along with a toolbar shortcut.
43 *
44 * 5 12/30/98 4:39p Jeff
45 * selectable to what scripts to display (checked out, non-checked out or
46 * both)
47 *
48 * 4 12/20/98 9:57p Jeff
49 * finished mass compile dialog
50 *
51 * 3 12/18/98 12:36p Jeff
52 * created a function to determine a script's type and hooked it in
53 *
54 * 2 12/18/98 12:11p Jeff
55 * added mass script compile dialog
56 *
57 * $NoKeywords: $
58 */
59
60 // ScriptMassCompile.cpp : implementation file
61 //
62
63 #include "stdafx.h"
64 #include "editor.h"
65 #include "ScriptMassCompile.h"
66 #include "cfile.h"
67 #include "gamefile.h"
68 #include "gamefilepage.h"
69 #include "AppDatabase.h"
70 #include "Descent.h"
71 #include "mono.h"
72 #include "ddio.h"
73 #include "ScriptCompilerAPI.h"
74 #include <string.h>
75 #include <stdarg.h>
76 #include "mem.h"
77 #include "pserror.h"
78
79 #ifdef _DEBUG
80 #define new DEBUG_NEW
81 #undef THIS_FILE
82 static char THIS_FILE[] = __FILE__;
83 #endif
84
85 /////////////////////////////////////////////////////////////////////////////
86 // CScriptMassCompile dialog
87 CString MassScriptEditContent;
88 CEdit *MassScriptEdit;
89 void writeline(char *format, ...);
90
91 CScriptMassCompile::CScriptMassCompile(CWnd *pParent /*=NULL*/) : CDialog(CScriptMassCompile::IDD, pParent) {
92 //{{AFX_DATA_INIT(CScriptMassCompile)
93 m_AutoCheckIn = FALSE;
94 m_DisplayType = 2;
95 m_NoCheckout = FALSE;
96 //}}AFX_DATA_INIT
97 }
98
99 void CScriptMassCompile::DoDataExchange(CDataExchange *pDX) {
100 CDialog::DoDataExchange(pDX);
101 //{{AFX_DATA_MAP(CScriptMassCompile)
102 DDX_Control(pDX, IDC_LIST, m_ScriptList);
103 DDX_Check(pDX, IDC_AUTOCHECKIN, m_AutoCheckIn);
104 DDX_Radio(pDX, IDC_LOCKED, m_DisplayType);
105 DDX_Check(pDX, IDC_NOCHECKOUT, m_NoCheckout);
106 //}}AFX_DATA_MAP
107 }
108
109 BEGIN_MESSAGE_MAP(CScriptMassCompile, CDialog)
110 //{{AFX_MSG_MAP(CScriptMassCompile)
111 ON_BN_CLICKED(IDC_BUILD, OnBuild)
112 ON_BN_CLICKED(IDC_LOCKED, OnLocked)
113 ON_BN_CLICKED(IDC_NOTLOCKED, OnNotlocked)
114 ON_BN_CLICKED(IDC_BOTH, OnBoth)
115 ON_BN_CLICKED(IDC_SELECTALL, OnSelectall)
116 ON_BN_CLICKED(IDC_OODSCRIPTS, OnOodscripts)
117 ON_BN_CLICKED(IDC_OOSSCRIPTS, OnOosscripts)
118 ON_BN_CLICKED(IDC_NOCHECKOUT, OnNocheckout)
119 //}}AFX_MSG_MAP
120 END_MESSAGE_MAP()
121
122 /////////////////////////////////////////////////////////////////////////////
123 // CScriptMassCompile message handlers
124
125 void CScriptMassCompile::OnBuild() {
126 UpdateData(true);
127
128 MassScriptEditContent = "";
129 writeline("Starting Mass Build.");
130
131 int i, list_size, listbox_size, index;
132 int *index_map = NULL;
133 CCheckListBox *list;
134 char tempbuffer[_MAX_PATH];
135 CWnd *wnd;
136
137 list = (CCheckListBox *)GetDlgItem(IDC_LIST);
138 listbox_size = list->GetCount();
139 list_size = 0;
140 for (i = 0; i < listbox_size; i++) {
141 if (list->GetCheck(i)) {
142 list_size++;
143 }
144 }
145
146 if (list_size == 0)
147 return;
148 index_map = (int *)mem_malloc(list_size * sizeof(int));
149 if (!index_map)
150 return;
151
152 index = 0;
153 for (i = 0; i < listbox_size; i++) {
154 if (list->GetCheck(i)) {
155 index_map[index] = i;
156 index++;
157 }
158 }
159
160 wnd = (CWnd *)GetDlgItem(IDC_FILENAME);
161 bool ret1, ret2, ret3, ret4;
162
163 for (i = 0; i < list_size; i++) {
164 index = index_map[i];
165
166 SetStepText(1, "-Not Done-");
167 SetStepText(2, "-Not Done-");
168 SetStepText(3, "-Not Done-");
169 SetStepText(4, "-Not Done-");
170
171 list->GetText(index, tempbuffer);
172 wnd->SetWindowText(tempbuffer);
173 writeline("------------------------------- %s --------------------------", tempbuffer);
174 Descent->defer();
175
176 ret1 = Step1(tempbuffer);
177 Descent->defer();
178
179 ret2 = Step2(tempbuffer);
180 Descent->defer();
181
182 ret3 = Step3(tempbuffer, ret2);
183 Descent->defer();
184
185 if (ret1) {
186 ret4 = Step4(tempbuffer);
187 Descent->defer();
188 } else {
189 SetStepText(4, "Skipping due to results of Step 1");
190 writeline("Step 4: Skipped due to result of Step 1 (maybe already checked out)");
191 ret4 = true;
192 }
193 }
194
195 OutrageMessageBox("Mass Compile Complete");
196
197 mem_free(index_map);
198 }
199
200 void CScriptMassCompile::OnOK() {
201 UpdateData(true);
202 Database->write("EditorScriptAutoCheckin", m_AutoCheckIn);
203 CDialog::OnOK();
204 }
205
206 void writeline(char *format, ...) {
207 char buffer[2048];
208 va_list marker;
209 va_start(marker, format);
210 std::vsprintf(buffer, format, marker);
211 va_end(marker);
212
213 MassScriptEditContent += buffer;
214 MassScriptEditContent += "\r\n";
215
216 MassScriptEdit->SetWindowText(MassScriptEditContent.GetBuffer(0));
217 Descent->defer();
218
219 int total_lines = MassScriptEdit->GetLineCount();
220 int curr_index = MassScriptEdit->GetFirstVisibleLine();
221 if ((total_lines - curr_index) > 16) {
222 // we need to scroll down a line
223 MassScriptEdit->LineScroll((total_lines - curr_index) - 16);
224 }
225 }
226
227 uint8_t DetermineScriptType(char *filename) {
228 CFILE *file;
229 file = cfopen(filename, "rt");
230 if (!file) {
231 return 255;
232 }
233
234 char buffer[4096];
235 bool done = false;
236 uint8_t script_type = ST_GAME;
237
238 cf_ReadString(buffer, 4096, file);
239 while (!(cfeof(file) || done)) {
240 if (strstr(buffer, "GetTriggerScriptID")) {
241 // we found it!
242 script_type = ST_LEVEL;
243 done = true;
244 }
245
246 cf_ReadString(buffer, 4096, file);
247 }
248
249 cfclose(file);
250
251 return script_type;
252 }
253
254 void CScriptMassCompile::BuildList(void) {
255 CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST);
256 if (!list)
257 return;
258
259 list->ResetContent();
260
261 char buffer[_MAX_PATH];
262 int length;
263
264 for (int i = 0; i < MAX_GAMEFILES; i++) {
265 if (Gamefiles[i].used) {
266 // we're only adding .cpp's to this list
267 length = strlen(Gamefiles[i].name);
268 if (length > 4 && !stricmp(&Gamefiles[i].name[length - 4], ".cpp")) {
269 switch (m_DisplayType) {
270 case 0: // checked out only
271 {
272 if (mng_FindTrackLock(Gamefiles[i].name, PAGETYPE_GAMEFILE) != -1) {
273 strcpy(buffer, Gamefiles[i].name);
274 buffer[length - 4] = '\0';
275 list->AddString(buffer);
276 }
277 } break;
278 case 1: // non-check out only
279 {
280 if (mng_FindTrackLock(Gamefiles[i].name, PAGETYPE_GAMEFILE) == -1) {
281 strcpy(buffer, Gamefiles[i].name);
282 buffer[length - 4] = '\0';
283 list->AddString(buffer);
284 }
285 } break;
286 case 2: // both
287 {
288 strcpy(buffer, Gamefiles[i].name);
289 buffer[length - 4] = '\0';
290 list->AddString(buffer);
291 } break;
292 };
293 }
294 }
295 }
296 }
297
298 BOOL CScriptMassCompile::OnInitDialog() {
299 CDialog::OnInitDialog();
300
301 bool bval;
302 if (Database->read("EditorScriptAutoCheckin", &bval))
303 m_AutoCheckIn = bval;
304 else
305 m_AutoCheckIn = true;
306 UpdateData(false);
307
308 MassScriptEditContent = "";
309 MassScriptEdit = (CEdit *)GetDlgItem(IDC_OUTPUT);
310 writeline("System Initialized.");
311
312 if (!Network_up) {
313 writeline("WARNING: You did not say yes to the data update, therefore, your work will not be saved for the scripts "
314 "that you don't currently have checked out.");
315 }
316 writeline("----------------------------------------------------------------------------------------------------------"
317 "--------------------------------------");
318
319 BuildList();
320
321 return TRUE;
322 }
323
324 void CScriptMassCompile::SetStepText(int step, char *format, ...) {
325 if (step < 1 || step > 4)
326 return;
327
328 char buffer[1024];
329 va_list marker;
330 va_start(marker, format);
331 std::vsprintf(buffer, format, marker);
332 va_end(marker);
333
334 CWnd *wnd;
335 int msg_to;
336
337 switch (step) {
338 case 1:
339 wnd = (CWnd *)GetDlgItem(IDC_STEP1);
340 msg_to = IDC_STEP1;
341 break;
342 case 2:
343 wnd = (CWnd *)GetDlgItem(IDC_STEP2);
344 msg_to = IDC_STEP2;
345 break;
346 case 3:
347 wnd = (CWnd *)GetDlgItem(IDC_STEP3);
348 msg_to = IDC_STEP3;
349 break;
350 case 4:
351 wnd = (CWnd *)GetDlgItem(IDC_STEP4);
352 msg_to = IDC_STEP4;
353 break;
354 };
355 wnd->SetWindowText(buffer);
356 SendDlgItemMessage(msg_to, WM_PAINT, 0, 0);
357 Descent->defer();
358 }
359
360 // return true if the file was checked out for this step
361 bool CheckOutGamefile(char *tempbuffer, bool show_ok_confirmation, bool report_who_has_locked);
362 bool CScriptMassCompile::Step1(char *filename) {
363 if (!Network_up) {
364 SetStepText(1, "Skipping, Network 'Down'");
365 writeline("Step 1: Skipping, Due to the NO on data update");
366 return false;
367 }
368
369 if (m_NoCheckout) {
370 SetStepText(1, "Skipping, 'no checkout' option checked");
371 writeline("Step 1: Skipping, 'no checkout' option checked");
372 return false;
373 }
374
375 char source_filename[_MAX_PATH], compiled_filename[_MAX_PATH];
376
377 // make the locker
378 if (!mng_MakeLocker()) {
379 SetStepText(1, "Unable to create locker, can't check out");
380 writeline("Step 1: Can't make locker file, skipping check out");
381 return false;
382 }
383
384 // make real filenames
385 strcpy(source_filename, filename);
386 strcpy(compiled_filename, filename);
387 strcat(source_filename, ".cpp");
388 strcat(compiled_filename, ".dll");
389
390 bool found_script = false, found_compiled = false;
391 int i;
392
393 // make sure the compiled script is in the gamefiles
394 for (i = 0; i < MAX_GAMEFILES; i++) {
395 if (Gamefiles[i].used) {
396 if (!found_script && !stricmp(Gamefiles[i].name, source_filename))
397 found_script = true;
398 if (!found_compiled && !stricmp(Gamefiles[i].name, compiled_filename))
399 found_compiled = true;
400 }
401 if (found_script && found_compiled)
402 break;
403 }
404
405 if (!found_script) {
406 // ummm, this shouldn't happen
407 MessageBox("Hmmm, I couldn't find the source file for the script in the manage system...this isn't good", "Uh oh");
408 SetStepText(1, "File Not Found");
409 writeline("Step 1: Couldn't find source script in manage system");
410 mng_EraseLocker();
411 return false;
412 }
413
414 bool ntco_script = true, ntco_compiled = true;
415 if (!found_compiled)
416 ntco_compiled = false;
417
418 // see if we have these checked out
419 if (mng_FindTrackLock(source_filename, PAGETYPE_GAMEFILE) != -1)
420 ntco_script = false;
421
422 if (ntco_compiled && mng_FindTrackLock(compiled_filename, PAGETYPE_GAMEFILE) != -1)
423 ntco_compiled = false;
424
425 // ok, we know what files we need to check out
426 bool sret = false, cret = false;
427
428 if (ntco_script) {
429 writeline("Step 1: Checking out %s", source_filename);
430 sret = CheckOutGamefile(source_filename, false, false);
431 if (!sret)
432 writeline("Step 1: Unable to check out %s", source_filename);
433 }
434
435 if (ntco_compiled) {
436 writeline("Step 1: Checking out %s", compiled_filename);
437 cret = CheckOutGamefile(compiled_filename, false, false);
438 if (!cret)
439 writeline("Step 1: Unable to check out %s", compiled_filename);
440 }
441
442 if ((ntco_script || ntco_compiled)) {
443 if (((!ntco_script) || sret) && ((!ntco_compiled) || cret)) {
444 SetStepText(1, "Files checked out");
445 writeline("Step 1: Success");
446 } else {
447 SetStepText(1, "Error checking files out");
448 writeline("Step 1: Errors have occurred");
449 }
450 } else {
451 SetStepText(1, "No files needed to be checked out");
452 writeline("Step 1: Success");
453 }
454
455 mng_EraseLocker();
456 return (sret || cret);
457 }
458
459 // return true if it's a level script
460 bool CScriptMassCompile::Step2(char *filename) {
461 char source_filename[_MAX_PATH];
462 strcpy(source_filename, filename);
463 strcat(source_filename, ".cpp");
464
465 if (DetermineScriptType(source_filename) == ST_LEVEL) {
466 writeline("Step 2: Determined to be a level script");
467 SetStepText(2, "Level Script");
468 return true;
469 } else {
470 writeline("Step 2: Determined to be a game script");
471 SetStepText(2, "Game Script");
472 return false;
473 }
474
475 return false;
476 }
477
478 void masscompilercallback(char *str) {
479 MassScriptEditContent += str;
480
481 MassScriptEdit->SetWindowText(MassScriptEditContent.GetBuffer(0));
482
483 int total_lines = MassScriptEdit->GetLineCount();
484 int curr_index = MassScriptEdit->GetFirstVisibleLine();
485 if ((total_lines - curr_index) > 16) {
486 // we need to scroll down a line
487 MassScriptEdit->LineScroll((total_lines - curr_index) - 16);
488 }
489
490 Descent->defer();
491 }
492
493 bool CScriptMassCompile::Step3(char *filename, bool islevel) {
494 tCompilerInfo ci;
495 ci.callback = masscompilercallback;
496 ci.script_type = (islevel) ? ST_LEVEL : ST_GAME;
497 strcpy(ci.source_filename, filename);
498 strcat(ci.source_filename, ".cpp");
499
500 char compiled_filename[_MAX_PATH], full_path[_MAX_PATH];
501 strcpy(compiled_filename, filename);
502 strcat(compiled_filename, ".dll");
503 ddio_MakePath(full_path, LocalScriptDir, compiled_filename, NULL);
504 if (cfexist(full_path)) {
505 ddio_DeleteFile(full_path);
506 }
507
508 writeline("===START COMPILE===");
509 ScriptCompile(&ci);
510 writeline("");
511 writeline("===END COMPILE===");
512
513 bool ret;
514 if (cfexist(full_path)) {
515 writeline("Step 3: DLL Created");
516 SetStepText(3, "DLL Created");
517 ret = true;
518 } else {
519 writeline("Step 3: DLL Not Created");
520 SetStepText(3, "DLL NOT Created");
521 ret = false;
522 }
523
524 return ret;
525 }
526
527 bool CheckInGamefile(char *tempbuffer, bool giveok);
528 bool AddNewGameFile(char *fullpath, char *directory);
529 bool CScriptMassCompile::Step4(char *filename) {
530 if (!Network_up) {
531 SetStepText(4, "Skipping, Network 'Down'");
532 writeline("Step 4: Skipping, Due to the NO on data update");
533 return false;
534 }
535
536 if (!m_AutoCheckIn) {
537 SetStepText(4, "Skipping, Auto Checkin box not set");
538 writeline("Step 4: Skipping, Auto Checkin box not set");
539 return false;
540 }
541
542 char source_filename[_MAX_PATH], compiled_filename[_MAX_PATH];
543
544 // make the locker
545 if (!mng_MakeLocker()) {
546 SetStepText(4, "Unable to create locker, can't check in");
547 writeline("Step 4: Can't make locker file, skipping check in");
548 return false;
549 }
550
551 // make real filenames
552 strcpy(source_filename, filename);
553 strcpy(compiled_filename, filename);
554 strcat(source_filename, ".cpp");
555 strcat(compiled_filename, ".dll");
556
557 bool ntci_source = false, ntci_compiled = false;
558 int i;
559
560 for (i = 0; i < MAX_GAMEFILES; i++) {
561 if (Gamefiles[i].used) {
562 if (!ntci_source && !stricmp(Gamefiles[i].name, source_filename) &&
563 (mng_FindTrackLock(source_filename, PAGETYPE_GAMEFILE) != -1)) {
564 ntci_source = true;
565 }
566 if (!ntci_compiled && !stricmp(Gamefiles[i].name, compiled_filename) &&
567 (mng_FindTrackLock(source_filename, PAGETYPE_GAMEFILE) != -1)) {
568 ntci_compiled = true;
569 }
570 }
571 if (ntci_source && ntci_compiled)
572 break;
573 }
574
575 if (!ntci_source && !ntci_compiled) {
576 SetStepText(4, "Skipping, files not checked out");
577 writeline("Step 4: Skipping because both source and compiled are not checked out or not in the manage system");
578 mng_EraseLocker();
579 return false;
580 }
581
582 // check to make sure the compiled version exists
583 if (!cfexist(compiled_filename)) {
584 writeline("Step 4: %s does not exist...possibly due to compilation errors", compiled_filename);
585 writeline("Step 4: Aborting check in");
586 SetStepText(4, "Aborted");
587 mng_EraseLocker();
588 return false;
589 }
590
591 // see if we need to add the compiled version to manage system
592 if (!ntci_compiled) {
593 int j;
594 for (j = 0; j < MAX_GAMEFILES; j++) {
595 if (Gamefiles[j].used) {
596 if (!stricmp(Gamefiles[j].name, compiled_filename))
597 break;
598 }
599 }
600 if (j >= MAX_GAMEFILES) {
601 // compiled file doesn't exist, we'll need to add it
602 writeline("Step 4: Attempting to add %s to manage system", compiled_filename);
603 char fullpath[_MAX_PATH];
604 ddio_MakePath(fullpath, LocalScriptDir, compiled_filename, NULL);
605 bool ret = AddNewGameFile(fullpath, "scripts");
606 if (!ret) {
607 writeline("Step 4: Unable to add %s to manage system", compiled_filename);
608 writeline("Step 4: Aborting");
609 SetStepText(4, "Aborted");
610 mng_EraseLocker();
611 return false;
612 } else
613 ntci_compiled = true;
614 }
615 }
616
617 // we have at least 1 file to check in
618 bool sret = true, cret = true;
619
620 if (ntci_source) {
621 sret = CheckInGamefile(source_filename, false);
622 if (!sret) {
623 writeline("Step 4: Unable to check in source file");
624 }
625 }
626
627 if (ntci_compiled) {
628 cret = CheckInGamefile(compiled_filename, false);
629 if (!cret) {
630 writeline("Step 4: Unable to check in compiled file");
631 }
632 }
633
634 if (!sret || !cret) {
635 SetStepText(4, "There were errors checking in files");
636 writeline("Step 4: Errors had occurred");
637 } else {
638 SetStepText(4, "Success");
639 writeline("Step 4: Success");
640 }
641
642 mng_EraseLocker();
643 return (sret || cret);
644 }
645
646 void CScriptMassCompile::OnLocked() {
647 UpdateData(true);
648 BuildList();
649 }
650
651 void CScriptMassCompile::OnNotlocked() {
652 UpdateData(true);
653 BuildList();
654 }
655
656 void CScriptMassCompile::OnBoth() {
657 UpdateData(true);
658 BuildList();
659 }
660
661 void CScriptMassCompile::OnSelectall() {
662 CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST);
663 if (!list)
664 return;
665
666 int count = list->GetCount();
667 for (int i = 0; i < count; i++) {
668 list->SetCheck(i, 1);
669 list->SetSel(i, TRUE);
670 }
671 }
672
673 bool IsScriptOutofDate(char *name);
674 void CScriptMassCompile::OnOodscripts() {
675 CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST);
676 if (!list)
677 return;
678
679 char old_dir[_MAX_PATH];
680 ddio_GetWorkingDir(old_dir, _MAX_PATH);
681 ddio_SetWorkingDir(LocalScriptDir);
682
683 char filename[_MAX_PATH];
684 int count = list->GetCount();
685 for (int i = 0; i < count; i++) {
686 list->GetText(i, filename);
687 strcat(filename, ".cpp");
688
689 if (IsScriptOutofDate(filename)) {
690 list->SetCheck(i, 1);
691 list->SetSel(i, TRUE);
692 } else {
693 list->SetCheck(i, 0);
694 list->SetSel(i, FALSE);
695 }
696 }
697
698 ddio_SetWorkingDir(old_dir);
699 }
700 bool IsScriptOutofSync(char *name);
701 void CScriptMassCompile::OnOosscripts() {
702 CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST);
703 if (!list)
704 return;
705
706 char filename[_MAX_PATH];
707 int count = list->GetCount();
708 for (int i = 0; i < count; i++) {
709 list->GetText(i, filename);
710 strcat(filename, ".cpp");
711
712 if (IsScriptOutofSync(filename)) {
713 list->SetCheck(i, 1);
714 list->SetSel(i, TRUE);
715 } else {
716 list->SetCheck(i, 0);
717 list->SetSel(i, FALSE);
718 }
719 }
720 }
721
722 void CScriptMassCompile::OnNocheckout() {
723 // TODO: Add your control notification handler code here
724 }