File: ScriptSyncDialog.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (481 lines) | stat: -rw-r--r-- 12,442 bytes parent folder | download | duplicates (2)
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
/*
 * Descent 3
 * Copyright (C) 2024 Parallax Software
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// ScriptSyncDialog.cpp : implementation file
//

#include "mfc_compatibility.h"
#include "editor.h"
#include "ScriptSyncDialog.h"
#include "ddio.h"
#include "cfile.h"
#include "psglob.h"
#include "gamefile.h"
#include "manage.h"
#include "ScriptCompilerAPI.h"
#include "mem.h"
#include "mono.h"
#include "textaux.h"
#include "appdatabase.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

bool ManageFindFirst(char *buffer, char *wildcard);
bool ManageFindNext(char *buffer);
void ManageFindClose(void);

uint8_t DetermineScriptType(char *filename);

// returns true if the given .cpp file is has an out of date dll (or non-existant).  Working
//	dir MUST be set to data\scripts before calling this function
bool IsScriptOutofDate(char *name);
// returns true if the given .cpp/.dll file is out of sync (or non-existant).  Working directory
// doesn't necessarily have to be set to data\scripts.
bool IsScriptOutofSync(char *name);

/////////////////////////////////////////////////////////////////////////////
// CScriptSyncDialog dialog

CScriptSyncDialog::CScriptSyncDialog(CWnd *pParent /*=NULL*/) : CDialog(CScriptSyncDialog::IDD, pParent) {
  //{{AFX_DATA_INIT(CScriptSyncDialog)
  // NOTE: the ClassWizard will add member initialization here
  //}}AFX_DATA_INIT
}

void CScriptSyncDialog::DoDataExchange(CDataExchange *pDX) {
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CScriptSyncDialog)
  DDX_Control(pDX, IDC_LIST, m_List);
  //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CScriptSyncDialog, CDialog)
//{{AFX_MSG_MAP(CScriptSyncDialog)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScriptSyncDialog message handlers

/////////////////////////////////////////////////////////////////////////////
// CMyListCtrl

CMyListCtrl::CMyListCtrl() {}

CMyListCtrl::~CMyListCtrl() {}

BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
//{{AFX_MSG_MAP(CMyListCtrl)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyListCtrl message handlers

#define COLUMN_NAME 0
#define COLUMN_STATE 1

#define COLUMN_NAME_SIZE 0.49f
#define COLUMN_STATE_SIZE 0.49f

void CMyListCtrl::Initialize(void) {
  ModifyStyleEx(0, LVS_EX_FULLROWSELECT);

  RECT rect;
  GetWindowRect(&rect);
  int width = rect.right - rect.left;
  int ret;

  ret = InsertColumn(COLUMN_NAME, "Name", LVCFMT_LEFT, (int)(width * COLUMN_NAME_SIZE));
  ret = InsertColumn(COLUMN_STATE, "State", LVCFMT_LEFT, (int)(width * COLUMN_STATE_SIZE));
}

int CMyListCtrl::AddItem(char *name, int num) {
  int item_count = GetItemCount();

  int added = InsertItem(item_count, name);
  if (added == -1) {
    return -1;
  }

  bool ret = CMyListCtrl::SetItem(added, num);

  if (!ret) {
    DeleteItem(added);
    return -1;
  }

  // all went ok
  return added;
}

bool CMyListCtrl::GetItem(int item, char *name, char *state) {
  int item_count = GetItemCount();

  if (item >= item_count)
    return false;

  GetItemText(item, COLUMN_NAME, name, 256);
  GetItemText(item, COLUMN_STATE, state, 40);

  return true;
}

bool CMyListCtrl::SetItem(int item, int state) {
  char buffer[256];
  switch (state) {
  case STATE_COMPILED:
    strcpy(buffer, "Compiled");
    break;
  case STATE_OOD:
    strcpy(buffer, "Out of Date");
    break;
  case STATE_ERROOD:
    strcpy(buffer, "Error/Out of Date");
    break;
  case STATE_OOS:
    strcpy(buffer, "Out of Sync");
    break;
  case STATE_ERROOS:
    strcpy(buffer, "Error/Out of Sync");
    break;
  case STATE_MISSING:
    strcpy(buffer, "Missing");
    break;
  case STATE_NOCOMPILER:
    strcpy(buffer, "No Compiler");
    break;
  }

  return (CListCtrl::SetItem(item, COLUMN_STATE, LVIF_TEXT, buffer, 0, 0, 0, 0) != FALSE);
}

void CMyListCtrl::SelectItem(int index) {
  int item_count = GetItemCount();
  if (index < 0 || index >= item_count)
    return;

  LVITEM lv;
  lv.mask = LVIF_STATE;

  lv.iSubItem = 0;
  lv.stateMask = LVIS_SELECTED;
  lv.state = 0;

  for (int i = 0; i < item_count; i++) {
    lv.iItem = i;
    CListCtrl::SetItem(&lv);
  }

  lv.iItem = index;
  lv.state = 0xFFFFFFFF;
  CListCtrl::SetItem(&lv);
}

class tManageFindIn {
public:
  tManageFindIn() { searching = false; }

  bool searching;
  bool in_Gamefiles;
  int curr_index;
  char glob_string[PAGENAME_LEN];
};
tManageFindIn ManageFindInData;

bool ManageFindFirst(char *buffer, char *wildcard) {
  if (ManageFindInData.searching)
    ManageFindClose();

  ManageFindInData.searching = true;
  strcpy(ManageFindInData.glob_string, wildcard);
  ManageFindInData.curr_index = 0;
  ManageFindInData.in_Gamefiles = true;

  return ManageFindNext(buffer);
}

bool ManageFindNext(char *buffer) {
  if (!ManageFindInData.searching)
    return false;

  if (ManageFindInData.in_Gamefiles) {

    for (; ManageFindInData.curr_index < MAX_GAMEFILES; ManageFindInData.curr_index++) {
      if (Gamefiles[ManageFindInData.curr_index].used &&
          PSGlobMatch(ManageFindInData.glob_string, Gamefiles[ManageFindInData.curr_index].name, false, false)) {
        // match
        strcpy(buffer, Gamefiles[ManageFindInData.curr_index].name);
        ManageFindInData.curr_index++;
        return true;
      }
    }

    ManageFindInData.curr_index = 0;
    ManageFindInData.in_Gamefiles = false;
  }

  for (; ManageFindInData.curr_index < MAX_TRACKLOCKS; ManageFindInData.curr_index++) {
    if (GlobalTrackLocks[ManageFindInData.curr_index].used &&
        GlobalTrackLocks[ManageFindInData.curr_index].pagetype == PAGETYPE_GAMEFILE &&
        PSGlobMatch(ManageFindInData.glob_string, GlobalTrackLocks[ManageFindInData.curr_index].name, false, false)) {
      // match
      strcpy(buffer, GlobalTrackLocks[ManageFindInData.curr_index].name);
      ManageFindInData.curr_index++;
      return true;
    }
  }

  return false;
}

void ManageFindClose(void) { ManageFindInData.searching = false; }

/////////////////////////////////////////////////////////////////
BOOL CScriptSyncDialog::OnInitDialog() {
  CDialog::OnInitDialog();
  early_exit = false;
  first_paint = true;
  m_InStuff = false;
  m_Done = false;
  m_Index = 0;
  m_NumFiles = 0;
  m_HasCompiler = -1;
  m_Files = NULL;
  m_FillingIn = true;
  m_ErrorCount = 0;
  ddio_GetWorkingDir(old_dir, _MAX_PATH);
  ddio_SetWorkingDir(LocalScriptDir);
  m_List.Initialize();
  BuildList();

  return TRUE;
}

void CScriptSyncDialog::defer(void) {
  MSG msg;

  while (1) {
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
      if (msg.message == WM_QUIT) {
        //	QUIT APP.
        early_exit = true;
      }
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    } else {
      break;
    }
  }
}

void CScriptSyncDialog::BuildList(void) {
  char buffer[256];
  m_NumFiles = 0;
  m_Files = NULL;

  if (ManageFindFirst(buffer, "*.cpp")) {
    m_NumFiles++;
    while (ManageFindNext(buffer)) {
      m_NumFiles++;
    }
  }
  ManageFindClose();

  if (m_NumFiles == 0)
    return;

  m_Files = (tFileInfo *)mem_malloc(sizeof(tFileInfo) * m_NumFiles);
  m_NumFiles = 0;

  if (ManageFindFirst(buffer, "*.cpp")) {
    strcpy(m_Files[m_NumFiles].filename, buffer);
    m_NumFiles++;

    while (ManageFindNext(buffer)) {
      strcpy(m_Files[m_NumFiles].filename, buffer);
      m_NumFiles++;
    }
  }
  ManageFindClose();
}

CString SS_CompileOutputData;

void SS_CompileOutputCallback(char *str) { SS_CompileOutputData += str; }

void CScriptSyncDialog::DoStuff(void) {
  m_InStuff = true;
  defer();

  if (m_Index >= m_NumFiles) {
    if (m_FillingIn) {
      m_FillingIn = false;
      m_Index = 0;
    } else {
      m_Done = true;
      goto done;
    }
  }

  if (m_FillingIn) {
    char dllfilename[_MAX_PATH];

    ddio_SplitPath(m_Files[m_Index].filename, NULL, dllfilename, NULL);
    strcat(dllfilename, ".dll");

    if (!cfexist(dllfilename)) {
      m_Files[m_Index].state = STATE_MISSING;
    } else {
      if (IsScriptOutofSync(m_Files[m_Index].filename)) {
        m_Files[m_Index].state = STATE_OOS;
      } else if (IsScriptOutofDate(m_Files[m_Index].filename)) {
        m_Files[m_Index].state = STATE_OOD;
      } else
        m_Files[m_Index].state = STATE_COMPILED;
    }
    m_List.AddItem(m_Files[m_Index].filename, m_Files[m_Index].state);
    m_List.EnsureVisible(m_Index, false);
  } else {
    if (m_Files) {
      switch (m_Files[m_Index].state) {
      case STATE_COMPILED:
        break;
      case STATE_MISSING:
      case STATE_OOD:
      case STATE_OOS: {
        char Compiler_path[_MAX_PATH];
        int len = _MAX_PATH;

        // attempt to recompile the script
        // make sure there is a compiler defined
        if (m_HasCompiler == -1) {
          if (Database->read("EditorCompiler", Compiler_path, &len)) {
            if (!cfexist(Compiler_path)) {
              m_HasCompiler = 0;
            } else {
              m_HasCompiler = 1;
            }
          } else {
            m_HasCompiler = 0;
          }
        }

        if (m_HasCompiler) {
          tCompilerInfo ci;
          char dllfilename[_MAX_PATH];
          SS_CompileOutputData = "";

          m_List.EnsureVisible(m_Index, false);
          m_List.SelectItem(m_Index);
          defer();

          ci.callback = SS_CompileOutputCallback;
          ci.script_type = DetermineScriptType(m_Files[m_Index].filename);
          strcpy(ci.source_filename, m_Files[m_Index].filename);
          ScriptCompile(&ci);

          ddio_SplitPath(m_Files[m_Index].filename, NULL, dllfilename, NULL);
          strcat(dllfilename, ".dll");
          if (!cfexist(dllfilename)) {
            m_Files[m_Index].state = STATE_MISSING;
          } else {
            if (IsScriptOutofSync(m_Files[m_Index].filename)) {
              m_Files[m_Index].state = STATE_ERROOS;
            } else if (IsScriptOutofDate(m_Files[m_Index].filename)) {
              m_Files[m_Index].state = STATE_ERROOD;
            } else
              m_Files[m_Index].state = STATE_COMPILED;
          }

          if (m_Files[m_Index].state == STATE_MISSING || m_Files[m_Index].state == STATE_ERROOD ||
              m_Files[m_Index].state == STATE_ERROOS) {
            m_ErrorCount++;
            char *string;
            string = mem_strdup(SS_CompileOutputData.GetBuffer(0));
            if (string) {
              char buffer[2048];
              const char *next_line;

              mprintf(0, "===========================================\n");
              next_line = textaux_CopyTextLine(string, buffer);
              while (next_line) {
                mprintf(0, "%s\n", buffer);
                next_line = textaux_CopyTextLine(next_line, buffer);
              }
              mprintf(0, "===========================================\n");
              mem_free(string);
            }
          }
        } else {
          m_Files[m_Index].state = STATE_NOCOMPILER;
        }

        m_List.SetItem(m_Index, m_Files[m_Index].state);

      } break;
      }
    }
  }

done:
  m_Index++;
  m_InStuff = false;
}

void CScriptSyncDialog::OnPaint() {
  CPaintDC dc(this); // device context for painting

  if (first_paint) {
    first_paint = false;
    SetTimer(0x12, 200, NULL);
  }
}

void CScriptSyncDialog::OnTimer(UINT nIDEvent) {
  defer();
  if (m_Done) {
    CDialog::OnOK();
  } else {
    if (!m_InStuff && nIDEvent == 0x12) {
      DoStuff();
    }
  }

  CDialog::OnTimer(nIDEvent);

  if (!m_Done)
    SetTimer(0x12, 50, NULL);
}

void CScriptSyncDialog::OnDestroy() {
  CDialog::OnDestroy();

  if (m_Files) {
    mem_free(m_Files);
  }
  ddio_SetWorkingDir(old_dir);
}