| 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 | |
| 19 | // ScriptSyncDialog.cpp : implementation file |
| 20 | // |
| 21 | |
| 22 | #include "mfc_compatibility.h" |
| 23 | #include "editor.h" |
| 24 | #include "ScriptSyncDialog.h" |
| 25 | #include "ddio.h" |
| 26 | #include "cfile.h" |
| 27 | #include "psglob.h" |
| 28 | #include "gamefile.h" |
| 29 | #include "manage.h" |
| 30 | #include "ScriptCompilerAPI.h" |
| 31 | #include "mem.h" |
| 32 | #include "mono.h" |
| 33 | #include "textaux.h" |
| 34 | #include "appdatabase.h" |
| 35 | |
| 36 | #ifdef _DEBUG |
| 37 | #define new DEBUG_NEW |
| 38 | #undef THIS_FILE |
| 39 | static char THIS_FILE[] = __FILE__; |
| 40 | #endif |
| 41 | |
| 42 | bool ManageFindFirst(char *buffer, char *wildcard); |
| 43 | bool ManageFindNext(char *buffer); |
| 44 | void ManageFindClose(void); |
| 45 | |
| 46 | uint8_t DetermineScriptType(char *filename); |
| 47 | |
| 48 | // returns true if the given .cpp file is has an out of date dll (or non-existant). Working |
| 49 | // dir MUST be set to data\scripts before calling this function |
| 50 | bool IsScriptOutofDate(char *name); |
| 51 | // returns true if the given .cpp/.dll file is out of sync (or non-existant). Working directory |
| 52 | // doesn't necessarily have to be set to data\scripts. |
| 53 | bool IsScriptOutofSync(char *name); |
| 54 | |
| 55 | ///////////////////////////////////////////////////////////////////////////// |
| 56 | // CScriptSyncDialog dialog |
| 57 | |
| 58 | CScriptSyncDialog::CScriptSyncDialog(CWnd *pParent /*=NULL*/) : CDialog(CScriptSyncDialog::IDD, pParent) { |
| 59 | //{{AFX_DATA_INIT(CScriptSyncDialog) |
| 60 | // NOTE: the ClassWizard will add member initialization here |
| 61 | //}}AFX_DATA_INIT |
| 62 | } |
| 63 | |
| 64 | void CScriptSyncDialog::DoDataExchange(CDataExchange *pDX) { |
| 65 | CDialog::DoDataExchange(pDX); |
| 66 | //{{AFX_DATA_MAP(CScriptSyncDialog) |
| 67 | DDX_Control(pDX, IDC_LIST, m_List); |
| 68 | //}}AFX_DATA_MAP |
| 69 | } |
| 70 | |
| 71 | BEGIN_MESSAGE_MAP(CScriptSyncDialog, CDialog) |
| 72 | //{{AFX_MSG_MAP(CScriptSyncDialog) |
| 73 | ON_WM_PAINT() |
| 74 | ON_WM_TIMER() |
| 75 | ON_WM_DESTROY() |
| 76 | //}}AFX_MSG_MAP |
| 77 | END_MESSAGE_MAP() |
| 78 | |
| 79 | ///////////////////////////////////////////////////////////////////////////// |
| 80 | // CScriptSyncDialog message handlers |
| 81 | |
| 82 | ///////////////////////////////////////////////////////////////////////////// |
| 83 | // CMyListCtrl |
| 84 | |
| 85 | CMyListCtrl::CMyListCtrl() {} |
| 86 | |
| 87 | CMyListCtrl::~CMyListCtrl() {} |
| 88 | |
| 89 | BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl) |
| 90 | //{{AFX_MSG_MAP(CMyListCtrl) |
| 91 | // NOTE - the ClassWizard will add and remove mapping macros here. |
| 92 | //}}AFX_MSG_MAP |
| 93 | END_MESSAGE_MAP() |
| 94 | |
| 95 | ///////////////////////////////////////////////////////////////////////////// |
| 96 | // CMyListCtrl message handlers |
| 97 | |
| 98 | #define COLUMN_NAME 0 |
| 99 | #define COLUMN_STATE 1 |
| 100 | |
| 101 | #define COLUMN_NAME_SIZE 0.49f |
| 102 | #define COLUMN_STATE_SIZE 0.49f |
| 103 | |
| 104 | void CMyListCtrl::Initialize(void) { |
| 105 | ModifyStyleEx(0, LVS_EX_FULLROWSELECT); |
| 106 | |
| 107 | RECT rect; |
| 108 | GetWindowRect(&rect); |
| 109 | int width = rect.right - rect.left; |
| 110 | int ret; |
| 111 | |
| 112 | ret = InsertColumn(COLUMN_NAME, "Name", LVCFMT_LEFT, (int)(width * COLUMN_NAME_SIZE)); |
| 113 | ret = InsertColumn(COLUMN_STATE, "State", LVCFMT_LEFT, (int)(width * COLUMN_STATE_SIZE)); |
| 114 | } |
| 115 | |
| 116 | int CMyListCtrl::AddItem(char *name, int num) { |
| 117 | int item_count = GetItemCount(); |
| 118 | |
| 119 | int added = InsertItem(item_count, name); |
| 120 | if (added == -1) { |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | bool ret = CMyListCtrl::SetItem(added, num); |
| 125 | |
| 126 | if (!ret) { |
| 127 | DeleteItem(added); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | // all went ok |
| 132 | return added; |
| 133 | } |
| 134 | |
| 135 | bool CMyListCtrl::GetItem(int item, char *name, char *state) { |
| 136 | int item_count = GetItemCount(); |
| 137 | |
| 138 | if (item >= item_count) |
| 139 | return false; |
| 140 | |
| 141 | GetItemText(item, COLUMN_NAME, name, 256); |
| 142 | GetItemText(item, COLUMN_STATE, state, 40); |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | bool CMyListCtrl::SetItem(int item, int state) { |
| 148 | char buffer[256]; |
| 149 | switch (state) { |
| 150 | case STATE_COMPILED: |
| 151 | strcpy(buffer, "Compiled"); |
| 152 | break; |
| 153 | case STATE_OOD: |
| 154 | strcpy(buffer, "Out of Date"); |
| 155 | break; |
| 156 | case STATE_ERROOD: |
| 157 | strcpy(buffer, "Error/Out of Date"); |
| 158 | break; |
| 159 | case STATE_OOS: |
| 160 | strcpy(buffer, "Out of Sync"); |
| 161 | break; |
| 162 | case STATE_ERROOS: |
| 163 | strcpy(buffer, "Error/Out of Sync"); |
| 164 | break; |
| 165 | case STATE_MISSING: |
| 166 | strcpy(buffer, "Missing"); |
| 167 | break; |
| 168 | case STATE_NOCOMPILER: |
| 169 | strcpy(buffer, "No Compiler"); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | return (CListCtrl::SetItem(item, COLUMN_STATE, LVIF_TEXT, buffer, 0, 0, 0, 0) != FALSE); |
| 174 | } |
| 175 | |
| 176 | void CMyListCtrl::SelectItem(int index) { |
| 177 | int item_count = GetItemCount(); |
| 178 | if (index < 0 || index >= item_count) |
| 179 | return; |
| 180 | |
| 181 | LVITEM lv; |
| 182 | lv.mask = LVIF_STATE; |
| 183 | |
| 184 | lv.iSubItem = 0; |
| 185 | lv.stateMask = LVIS_SELECTED; |
| 186 | lv.state = 0; |
| 187 | |
| 188 | for (int i = 0; i < item_count; i++) { |
| 189 | lv.iItem = i; |
| 190 | CListCtrl::SetItem(&lv); |
| 191 | } |
| 192 | |
| 193 | lv.iItem = index; |
| 194 | lv.state = 0xFFFFFFFF; |
| 195 | CListCtrl::SetItem(&lv); |
| 196 | } |
| 197 | |
| 198 | class tManageFindIn { |
| 199 | public: |
| 200 | tManageFindIn() { searching = false; } |
| 201 | |
| 202 | bool searching; |
| 203 | bool in_Gamefiles; |
| 204 | int curr_index; |
| 205 | char glob_string[PAGENAME_LEN]; |
| 206 | }; |
| 207 | tManageFindIn ManageFindInData; |
| 208 | |
| 209 | bool ManageFindFirst(char *buffer, char *wildcard) { |
| 210 | if (ManageFindInData.searching) |
| 211 | ManageFindClose(); |
| 212 | |
| 213 | ManageFindInData.searching = true; |
| 214 | strcpy(ManageFindInData.glob_string, wildcard); |
| 215 | ManageFindInData.curr_index = 0; |
| 216 | ManageFindInData.in_Gamefiles = true; |
| 217 | |
| 218 | return ManageFindNext(buffer); |
| 219 | } |
| 220 | |
| 221 | bool ManageFindNext(char *buffer) { |
| 222 | if (!ManageFindInData.searching) |
| 223 | return false; |
| 224 | |
| 225 | if (ManageFindInData.in_Gamefiles) { |
| 226 | |
| 227 | for (; ManageFindInData.curr_index < MAX_GAMEFILES; ManageFindInData.curr_index++) { |
| 228 | if (Gamefiles[ManageFindInData.curr_index].used && |
| 229 | PSGlobMatch(ManageFindInData.glob_string, Gamefiles[ManageFindInData.curr_index].name, false, false)) { |
| 230 | // match |
| 231 | strcpy(buffer, Gamefiles[ManageFindInData.curr_index].name); |
| 232 | ManageFindInData.curr_index++; |
| 233 | return true; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | ManageFindInData.curr_index = 0; |
| 238 | ManageFindInData.in_Gamefiles = false; |
| 239 | } |
| 240 | |
| 241 | for (; ManageFindInData.curr_index < MAX_TRACKLOCKS; ManageFindInData.curr_index++) { |
| 242 | if (GlobalTrackLocks[ManageFindInData.curr_index].used && |
| 243 | GlobalTrackLocks[ManageFindInData.curr_index].pagetype == PAGETYPE_GAMEFILE && |
| 244 | PSGlobMatch(ManageFindInData.glob_string, GlobalTrackLocks[ManageFindInData.curr_index].name, false, false)) { |
| 245 | // match |
| 246 | strcpy(buffer, GlobalTrackLocks[ManageFindInData.curr_index].name); |
| 247 | ManageFindInData.curr_index++; |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | void ManageFindClose(void) { ManageFindInData.searching = false; } |
| 256 | |
| 257 | ///////////////////////////////////////////////////////////////// |
| 258 | BOOL CScriptSyncDialog::OnInitDialog() { |
| 259 | CDialog::OnInitDialog(); |
| 260 | early_exit = false; |
| 261 | first_paint = true; |
| 262 | m_InStuff = false; |
| 263 | m_Done = false; |
| 264 | m_Index = 0; |
| 265 | m_NumFiles = 0; |
| 266 | m_HasCompiler = -1; |
| 267 | m_Files = NULL; |
| 268 | m_FillingIn = true; |
| 269 | m_ErrorCount = 0; |
| 270 | ddio_GetWorkingDir(old_dir, _MAX_PATH); |
| 271 | ddio_SetWorkingDir(LocalScriptDir); |
| 272 | m_List.Initialize(); |
| 273 | BuildList(); |
| 274 | |
| 275 | return TRUE; |
| 276 | } |
| 277 | |
| 278 | void CScriptSyncDialog::defer(void) { |
| 279 | MSG msg; |
| 280 | |
| 281 | while (1) { |
| 282 | if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { |
| 283 | if (msg.message == WM_QUIT) { |
| 284 | // QUIT APP. |
| 285 | early_exit = true; |
| 286 | } |
| 287 | TranslateMessage(&msg); |
| 288 | DispatchMessage(&msg); |
| 289 | } else { |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void CScriptSyncDialog::BuildList(void) { |
| 296 | char buffer[256]; |
| 297 | m_NumFiles = 0; |
| 298 | m_Files = NULL; |
| 299 | |
| 300 | if (ManageFindFirst(buffer, "*.cpp")) { |
| 301 | m_NumFiles++; |
| 302 | while (ManageFindNext(buffer)) { |
| 303 | m_NumFiles++; |
| 304 | } |
| 305 | } |
| 306 | ManageFindClose(); |
| 307 | |
| 308 | if (m_NumFiles == 0) |
| 309 | return; |
| 310 | |
| 311 | m_Files = (tFileInfo *)mem_malloc(sizeof(tFileInfo) * m_NumFiles); |
| 312 | m_NumFiles = 0; |
| 313 | |
| 314 | if (ManageFindFirst(buffer, "*.cpp")) { |
| 315 | strcpy(m_Files[m_NumFiles].filename, buffer); |
| 316 | m_NumFiles++; |
| 317 | |
| 318 | while (ManageFindNext(buffer)) { |
| 319 | strcpy(m_Files[m_NumFiles].filename, buffer); |
| 320 | m_NumFiles++; |
| 321 | } |
| 322 | } |
| 323 | ManageFindClose(); |
| 324 | } |
| 325 | |
| 326 | CString SS_CompileOutputData; |
| 327 | |
| 328 | void SS_CompileOutputCallback(char *str) { SS_CompileOutputData += str; } |
| 329 | |
| 330 | void CScriptSyncDialog::DoStuff(void) { |
| 331 | m_InStuff = true; |
| 332 | defer(); |
| 333 | |
| 334 | if (m_Index >= m_NumFiles) { |
| 335 | if (m_FillingIn) { |
| 336 | m_FillingIn = false; |
| 337 | m_Index = 0; |
| 338 | } else { |
| 339 | m_Done = true; |
| 340 | goto done; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (m_FillingIn) { |
| 345 | char dllfilename[_MAX_PATH]; |
| 346 | |
| 347 | ddio_SplitPath(m_Files[m_Index].filename, NULL, dllfilename, NULL); |
| 348 | strcat(dllfilename, ".dll"); |
| 349 | |
| 350 | if (!cfexist(dllfilename)) { |
| 351 | m_Files[m_Index].state = STATE_MISSING; |
| 352 | } else { |
| 353 | if (IsScriptOutofSync(m_Files[m_Index].filename)) { |
| 354 | m_Files[m_Index].state = STATE_OOS; |
| 355 | } else if (IsScriptOutofDate(m_Files[m_Index].filename)) { |
| 356 | m_Files[m_Index].state = STATE_OOD; |
| 357 | } else |
| 358 | m_Files[m_Index].state = STATE_COMPILED; |
| 359 | } |
| 360 | m_List.AddItem(m_Files[m_Index].filename, m_Files[m_Index].state); |
| 361 | m_List.EnsureVisible(m_Index, false); |
| 362 | } else { |
| 363 | if (m_Files) { |
| 364 | switch (m_Files[m_Index].state) { |
| 365 | case STATE_COMPILED: |
| 366 | break; |
| 367 | case STATE_MISSING: |
| 368 | case STATE_OOD: |
| 369 | case STATE_OOS: { |
| 370 | char Compiler_path[_MAX_PATH]; |
| 371 | int len = _MAX_PATH; |
| 372 | |
| 373 | // attempt to recompile the script |
| 374 | // make sure there is a compiler defined |
| 375 | if (m_HasCompiler == -1) { |
| 376 | if (Database->read("EditorCompiler", Compiler_path, &len)) { |
| 377 | if (!cfexist(Compiler_path)) { |
| 378 | m_HasCompiler = 0; |
| 379 | } else { |
| 380 | m_HasCompiler = 1; |
| 381 | } |
| 382 | } else { |
| 383 | m_HasCompiler = 0; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (m_HasCompiler) { |
| 388 | tCompilerInfo ci; |
| 389 | char dllfilename[_MAX_PATH]; |
| 390 | SS_CompileOutputData = ""; |
| 391 | |
| 392 | m_List.EnsureVisible(m_Index, false); |
| 393 | m_List.SelectItem(m_Index); |
| 394 | defer(); |
| 395 | |
| 396 | ci.callback = SS_CompileOutputCallback; |
| 397 | ci.script_type = DetermineScriptType(m_Files[m_Index].filename); |
| 398 | strcpy(ci.source_filename, m_Files[m_Index].filename); |
| 399 | ScriptCompile(&ci); |
| 400 | |
| 401 | ddio_SplitPath(m_Files[m_Index].filename, NULL, dllfilename, NULL); |
| 402 | strcat(dllfilename, ".dll"); |
| 403 | if (!cfexist(dllfilename)) { |
| 404 | m_Files[m_Index].state = STATE_MISSING; |
| 405 | } else { |
| 406 | if (IsScriptOutofSync(m_Files[m_Index].filename)) { |
| 407 | m_Files[m_Index].state = STATE_ERROOS; |
| 408 | } else if (IsScriptOutofDate(m_Files[m_Index].filename)) { |
| 409 | m_Files[m_Index].state = STATE_ERROOD; |
| 410 | } else |
| 411 | m_Files[m_Index].state = STATE_COMPILED; |
| 412 | } |
| 413 | |
| 414 | if (m_Files[m_Index].state == STATE_MISSING || m_Files[m_Index].state == STATE_ERROOD || |
| 415 | m_Files[m_Index].state == STATE_ERROOS) { |
| 416 | m_ErrorCount++; |
| 417 | char *string; |
| 418 | string = mem_strdup(SS_CompileOutputData.GetBuffer(0)); |
| 419 | if (string) { |
| 420 | char buffer[2048]; |
| 421 | const char *next_line; |
| 422 | |
| 423 | mprintf(0, "===========================================\n"); |
| 424 | next_line = textaux_CopyTextLine(string, buffer); |
| 425 | while (next_line) { |
| 426 | mprintf(0, "%s\n", buffer); |
| 427 | next_line = textaux_CopyTextLine(next_line, buffer); |
| 428 | } |
| 429 | mprintf(0, "===========================================\n"); |
| 430 | mem_free(string); |
| 431 | } |
| 432 | } |
| 433 | } else { |
| 434 | m_Files[m_Index].state = STATE_NOCOMPILER; |
| 435 | } |
| 436 | |
| 437 | m_List.SetItem(m_Index, m_Files[m_Index].state); |
| 438 | |
| 439 | } break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | done: |
| 445 | m_Index++; |
| 446 | m_InStuff = false; |
| 447 | } |
| 448 | |
| 449 | void CScriptSyncDialog::OnPaint() { |
| 450 | CPaintDC dc(this); // device context for painting |
| 451 | |
| 452 | if (first_paint) { |
| 453 | first_paint = false; |
| 454 | SetTimer(0x12, 200, NULL); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void CScriptSyncDialog::OnTimer(UINT nIDEvent) { |
| 459 | defer(); |
| 460 | if (m_Done) { |
| 461 | CDialog::OnOK(); |
| 462 | } else { |
| 463 | if (!m_InStuff && nIDEvent == 0x12) { |
| 464 | DoStuff(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | CDialog::OnTimer(nIDEvent); |
| 469 | |
| 470 | if (!m_Done) |
| 471 | SetTimer(0x12, 50, NULL); |
| 472 | } |
| 473 | |
| 474 | void CScriptSyncDialog::OnDestroy() { |
| 475 | CDialog::OnDestroy(); |
| 476 | |
| 477 | if (m_Files) { |
| 478 | mem_free(m_Files); |
| 479 | } |
| 480 | ddio_SetWorkingDir(old_dir); |
| 481 | } |