LevelEdit.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  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. // LevelEdit.cpp : Defines the class behaviors for the application.
  19. //
  20. #include "stdafx.h"
  21. #include "leveledit.h"
  22. #include "mainfrm.h"
  23. #include "leveleditdoc.h"
  24. #include "leveleditview.h"
  25. #include "assetmgr.h"
  26. #include "ww3d.h"
  27. #include "wwdebug.h"
  28. #include "utils.h"
  29. #include "editorassetmgr.h"
  30. #include "_assetmgr.h"
  31. #include "wwphystrig.h"
  32. #include "regkeys.h"
  33. #include "_wwaudio.h"
  34. #include "win.h"
  35. #include "sceneeditor.h"
  36. #include "editorsaveload.h"
  37. #include "generatingvisdialog.h"
  38. #include "splash.h"
  39. #include "ffactory.h"
  40. #include "wwsaveload.h"
  41. #include "wwmath.h"
  42. #include "wwphys.h"
  43. #include "translatedb.h"
  44. #include "presetmgr.h"
  45. #include "phys.h"
  46. #include "utils.h"
  47. #include "colorutils.h"
  48. #include "assetpackagemgr.h"
  49. #include "editorbuild.h"
  50. #ifdef _DEBUG
  51. #define new DEBUG_NEW
  52. #undef THIS_FILE
  53. static char THIS_FILE[] = __FILE__;
  54. #endif
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Local prototypes
  57. /////////////////////////////////////////////////////////////////////////////
  58. void Register_Light_Icon (void);
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CLevelEditApp
  61. BEGIN_MESSAGE_MAP(CLevelEditApp, CWinApp)
  62. //{{AFX_MSG_MAP(CLevelEditApp)
  63. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  64. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  65. //}}AFX_MSG_MAP
  66. // Standard file based document commands
  67. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  68. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  69. END_MESSAGE_MAP()
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CLevelEditApp construction
  72. CLevelEditApp::CLevelEditApp()
  73. {
  74. // TODO: add construction code here,
  75. // Place all significant initialization in InitInstance
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // The one and only CLevelEditApp object
  79. CLevelEditApp theApp;
  80. /////////////////////////////////////////////////////////////////////////////
  81. //
  82. // fnMessage_Print_Func
  83. //
  84. /////////////////////////////////////////////////////////////////////////////
  85. void
  86. fnMessage_Print_Func (DebugType /*type*/, const char *message)
  87. {
  88. ::OutputDebugString (message);
  89. ::Output_Message (message);
  90. return ;
  91. }
  92. /////////////////////////////////////////////////////////////////////////////
  93. //
  94. // fnAssert_Print_Func
  95. //
  96. /////////////////////////////////////////////////////////////////////////////
  97. void
  98. fnAssert_Print_Func (const char *message)
  99. {
  100. ::Output_Message (message);
  101. return ;
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. //
  105. // fnTrigger_Func
  106. //
  107. /////////////////////////////////////////////////////////////////////////////
  108. bool
  109. fnTrigger_Func (int trigger_num)
  110. {
  111. // Assume not triggered
  112. bool retval = false;
  113. // Are we checking for inverted 'vis'?
  114. if (trigger_num == WWPHYS_TRIGGER_INVERT_VIS) {
  115. retval = bool(::GetKeyState (VK_F9) < 0);
  116. }
  117. // Return the true/false result code
  118. return retval;
  119. }
  120. bool File_Exists (LPCSTR filename)
  121. {
  122. WIN32_FIND_DATA find_info;
  123. HANDLE file_handle = ::FindFirstFile (filename, &find_info);
  124. if (file_handle != INVALID_HANDLE_VALUE) {
  125. ::FindClose (file_handle);
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. /////////////////////////////////////////////////////////////////////////////
  132. //
  133. // InitInstance
  134. //
  135. /////////////////////////////////////////////////////////////////////////////
  136. BOOL
  137. CLevelEditApp::InitInstance (void)
  138. {
  139. //
  140. // Get the installation directory of this application
  141. //
  142. TCHAR install_path[MAX_PATH] = { 0 };
  143. ::GetModuleFileName (::AfxGetInstanceHandle (), install_path, sizeof (install_path));
  144. if (install_path[::lstrlen(install_path)-1] == '\\') {
  145. install_path[::lstrlen(install_path)-1] = 0;
  146. }
  147. _pThe3DAssetManager = new EditorAssetMgrClass;
  148. //
  149. // Let the user select which asset package they wish to work with
  150. //
  151. AssetPackageMgrClass::Initialize ();
  152. AssetPackageMgrClass::Show_Package_Selection_UI ();
  153. //
  154. // Display the splash screen
  155. //
  156. SplashClass splash_screen;
  157. splash_screen.Show (true);
  158. //
  159. // Initialize the libraries
  160. //
  161. WWMath::Init ();
  162. WWPhys::Init ();
  163. WWSaveLoad::Init ();
  164. TranslateDBClass::Initialize ();
  165. // Standard initialization
  166. // If you are not using these features and wish to reduce the size
  167. // of your final executable, you should remove from the following
  168. // the specific initialization routines you do not need.
  169. #ifdef _AFXDLL
  170. Enable3dControls(); // Call this when using MFC in a shared DLL
  171. #else
  172. Enable3dControlsStatic(); // Call this when linking to MFC statically
  173. #endif
  174. RegisterColorPicker (::AfxGetInstanceHandle ());
  175. RegisterColorBar (::AfxGetInstanceHandle ());
  176. // Change the registry key under which our settings are stored.
  177. // You should modify this string to be something appropriate
  178. // such as the name of your company or organization.
  179. SetRegistryKey(_T("Westwood Studios"));
  180. //
  181. // Handle the command line
  182. //
  183. LPCTSTR cmd_line = ::strstr (m_lpCmdLine, "-j=");
  184. if (cmd_line != NULL) {
  185. cmd_line += 3;
  186. Set_Silent_Mode (true);
  187. }
  188. LoadStdProfileSettings(9); // Load standard INI file options (including MRU)
  189. ::WWDebug_Install_Message_Handler (fnMessage_Print_Func);
  190. ::WWDebug_Install_Assert_Handler (fnAssert_Print_Func);
  191. ::WWDebug_Install_Trigger_Handler (fnTrigger_Func);
  192. //
  193. // Disable the 3DFX logo
  194. //
  195. _putenv ("FX_GLIDE_NO_SPLASH=1");
  196. // Register the application's document templates. Document templates
  197. // serve as the connection between documents, frame windows and views.
  198. #ifdef PUBLIC_EDITOR_VER
  199. CSingleDocTemplate* pDocTemplate;
  200. pDocTemplate = new CSingleDocTemplate(
  201. IDR_MAINFRAME2,
  202. RUNTIME_CLASS(CLevelEditDoc),
  203. RUNTIME_CLASS(CMainFrame), // main SDI frame window
  204. RUNTIME_CLASS(CLevelEditView));
  205. AddDocTemplate(pDocTemplate);
  206. #else
  207. CSingleDocTemplate* pDocTemplate;
  208. pDocTemplate = new CSingleDocTemplate(
  209. IDR_MAINFRAME,
  210. RUNTIME_CLASS(CLevelEditDoc),
  211. RUNTIME_CLASS(CMainFrame), // main SDI frame window
  212. RUNTIME_CLASS(CLevelEditView));
  213. AddDocTemplate(pDocTemplate);
  214. #endif //PUBLIC_EDITOR_VER
  215. // Enable DDE Execute open
  216. EnableShellOpen();
  217. RegisterShellFileTypes(TRUE);
  218. // Parse command line for standard shell commands, DDE, file open
  219. CCommandLineInfo cmdInfo;
  220. ParseCommandLine(cmdInfo);
  221. // Dispatch commands specified on the command line
  222. if (!ProcessShellCommand(cmdInfo))
  223. return FALSE;
  224. // The one and only window has been initialized, so show and update it.
  225. m_pMainWnd->ShowWindow(SW_SHOW);
  226. m_pMainWnd->UpdateWindow();
  227. // Enable drag/drop open
  228. m_pMainWnd->DragAcceptFiles();
  229. ProgramInstance = m_hInstance;
  230. MainWindow = m_pMainWnd->m_hWnd;
  231. PresetMgrClass::Initialize ();
  232. //
  233. // Update the installation key in the registry
  234. //
  235. const char *INSTALL_KEY = "Software\\Westwood Studios\\LevelEdit\\Install";
  236. const char *INSTALL_VALUE = "Exe";
  237. HKEY reg_key = NULL;
  238. if (::RegCreateKeyEx ( HKEY_CURRENT_USER,
  239. INSTALL_KEY,
  240. 0L,
  241. NULL,
  242. 0L,
  243. KEY_ALL_ACCESS,
  244. NULL,
  245. &reg_key,
  246. NULL) == ERROR_SUCCESS)
  247. {
  248. ::RegSetValueEx ( reg_key,
  249. INSTALL_VALUE,
  250. 0L,
  251. REG_SZ,
  252. (BYTE *)install_path,
  253. ::lstrlen (install_path) + 1);
  254. ::RegCloseKey (reg_key);
  255. }
  256. //
  257. // Register an icon with the light file
  258. //
  259. Register_Light_Icon ();
  260. //
  261. // Handle the command line
  262. //
  263. if (cmd_line != NULL) {
  264. ::Perform_Job (cmd_line, true);
  265. Set_Modified (false);
  266. m_pMainWnd->PostMessage (WM_CLOSE);
  267. }
  268. return TRUE;
  269. }
  270. /////////////////////////////////////////////////////////////////////////////
  271. // CAboutDlg dialog used for App About
  272. class CAboutDlg : public CDialog
  273. {
  274. public:
  275. CAboutDlg();
  276. // Dialog Data
  277. //{{AFX_DATA(CAboutDlg)
  278. enum { IDD = IDD_ABOUTBOX };
  279. //}}AFX_DATA
  280. // ClassWizard generated virtual function overrides
  281. //{{AFX_VIRTUAL(CAboutDlg)
  282. public:
  283. protected:
  284. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  285. //}}AFX_VIRTUAL
  286. // Implementation
  287. protected:
  288. //{{AFX_MSG(CAboutDlg)
  289. // No message handlers
  290. //}}AFX_MSG
  291. DECLARE_MESSAGE_MAP()
  292. };
  293. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  294. {
  295. //{{AFX_DATA_INIT(CAboutDlg)
  296. //}}AFX_DATA_INIT
  297. }
  298. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  299. {
  300. CDialog::DoDataExchange(pDX);
  301. //{{AFX_DATA_MAP(CAboutDlg)
  302. //}}AFX_DATA_MAP
  303. }
  304. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  305. //{{AFX_MSG_MAP(CAboutDlg)
  306. // No message handlers
  307. //}}AFX_MSG_MAP
  308. END_MESSAGE_MAP()
  309. // App command to run the dialog
  310. void CLevelEditApp::OnAppAbout()
  311. {
  312. CAboutDlg aboutDlg;
  313. aboutDlg.DoModal();
  314. }
  315. /////////////////////////////////////////////////////////////////////////////
  316. // CLevelEditApp commands
  317. /*
  318. **
  319. */
  320. void Debug_Refs(void)
  321. {
  322. #ifndef NDEBUG
  323. TRACE("Detecting Active Refs...\r\n");
  324. //ODS("At time %s", cMiscUtil::Get_Text_Time());
  325. RefCountNodeClass * first = RefCountClass::ActiveRefList.First();
  326. RefCountNodeClass * node = first;
  327. while (node->Is_Valid())
  328. {
  329. RefCountClass * obj = node->Get();
  330. ActiveRefStruct * ref = &(obj->ActiveRefInfo);
  331. bool display = true;
  332. int count = 0;
  333. RefCountNodeClass * search = first;
  334. while (search->Is_Valid()) {
  335. if (search == node) { // if this is not the first one
  336. if (count != 0) {
  337. display = false;
  338. break;
  339. }
  340. }
  341. RefCountClass * search_obj = search->Get();
  342. ActiveRefStruct * search_ref = &(search_obj->ActiveRefInfo);
  343. if ( ref->File && search_ref->File &&
  344. !strcmp(search_ref->File, ref->File) &&
  345. (search_ref->Line == ref->Line) ) {
  346. count++;
  347. } else if ( (ref->File == NULL) && (search_ref->File == NULL) ) {
  348. count++;
  349. }
  350. search = search->Next();
  351. }
  352. if ( display ) {
  353. TRACE ( "%d Active Ref: %s %d %p\n", count, ref->File,ref->Line,obj);
  354. static int num_printed = 0;
  355. if (++num_printed > 20) {
  356. TRACE( "And Many More......\n");
  357. break;
  358. }
  359. }
  360. node = node->Next();
  361. }
  362. TRACE("Done.\r\n");
  363. //ODS("At time %s", cMiscUtil::Get_Text_Time());
  364. #endif
  365. }
  366. /////////////////////////////////////////////////////////////
  367. //
  368. // ExitInstance
  369. //
  370. int
  371. CLevelEditApp::ExitInstance (void)
  372. {
  373. // Remove the debug handlers we installed earlier
  374. ::WWDebug_Install_Message_Handler (NULL);
  375. ::WWDebug_Install_Assert_Handler (NULL);
  376. ::WWDebug_Install_Trigger_Handler (NULL);
  377. //
  378. // Free the audio manager
  379. //
  380. SAFE_DELETE (_pTheAudioManager);
  381. //
  382. // Shutdown the libraries
  383. //
  384. WWMath::Shutdown ();
  385. WWPhys::Shutdown ();
  386. WWSaveLoad::Shutdown ();
  387. TranslateDBClass::Shutdown ();
  388. PresetMgrClass::Shutdown ();
  389. //
  390. // Check for active refs
  391. //
  392. Debug_Refs ();
  393. // Allow the base class to process this message
  394. return CWinApp::ExitInstance();
  395. }
  396. /////////////////////////////////////////////////////////////
  397. //
  398. // OnFileOpen
  399. //
  400. void
  401. CLevelEditApp::OnFileOpen (void)
  402. {
  403. CFileDialog dialog (TRUE,
  404. ".lvl",
  405. NULL,
  406. OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER,
  407. "Level Files|*.lvl||",
  408. ::AfxGetMainWnd ());
  409. //
  410. // Setup the initial directory
  411. //
  412. CString full_path = theApp.GetProfileString (CONFIG_KEY, LAST_SAVE_DIR_VALUE);
  413. if (full_path.GetLength () != 0) {
  414. dialog.m_ofn.lpstrInitialDir = full_path;
  415. }
  416. //
  417. // Let the user pick a file
  418. //
  419. if (dialog.DoModal () == IDOK) {
  420. OpenDocumentFile (dialog.GetPathName ());
  421. }
  422. // Determine which directory we should use as a default for the open
  423. /*CString full_path = theApp.GetProfileString (CONFIG_KEY, LAST_SAVE_DIR_VALUE);
  424. if (full_path.GetLength () != 0) {
  425. ::SetCurrentDirectory (full_path);
  426. ::Get_Current_Document ()->Set_Current_Path (full_path);
  427. }
  428. // Allow the base class to process this message
  429. CWinApp::OnFileOpen ();*/
  430. return ;
  431. }
  432. /////////////////////////////////////////////////////////////
  433. //
  434. // Register_Light_Icon
  435. //
  436. /////////////////////////////////////////////////////////////
  437. void
  438. Register_Light_Icon (void)
  439. {
  440. DWORD disp = 0;
  441. HKEY reg_key = NULL;
  442. const char *LIGHT_ICON_APP_NAME = "W3D Light";
  443. const char *LIGHT_ICON_KEY = "W3D Light\\DefaultIcon";
  444. //
  445. // Open the file extension association registry key
  446. //
  447. if (::RegCreateKeyEx ( HKEY_CLASSES_ROOT,
  448. ".wlt",
  449. 0,
  450. NULL,
  451. REG_OPTION_NON_VOLATILE,
  452. KEY_ALL_ACCESS,
  453. NULL,
  454. &reg_key,
  455. &disp) == ERROR_SUCCESS)
  456. {
  457. //
  458. // Write the name of the application under this extension's default value
  459. //
  460. ::RegSetValueEx (reg_key, NULL, 0, REG_SZ, (const BYTE *)LIGHT_ICON_APP_NAME, ::lstrlen (LIGHT_ICON_APP_NAME) + 1);
  461. ::RegCloseKey (reg_key);
  462. //
  463. // Open the application key
  464. //
  465. if (::RegCreateKeyEx ( HKEY_CLASSES_ROOT,
  466. LIGHT_ICON_KEY,
  467. 0,
  468. NULL,
  469. REG_OPTION_NON_VOLATILE,
  470. KEY_ALL_ACCESS,
  471. NULL,
  472. &reg_key,
  473. &disp) == ERROR_SUCCESS)
  474. {
  475. //
  476. // Build a string representing the path to the level editor and
  477. // the icon's index.
  478. //
  479. TCHAR path[MAX_PATH] = { 0 };
  480. ::GetModuleFileName (NULL, path, MAX_PATH);
  481. CString icon_path;
  482. icon_path.Format ("%s,-%d", path, IDI_LIGHT);
  483. //
  484. // Write the icon's location under this key
  485. //
  486. ::RegSetValueEx (reg_key, NULL, 0, REG_SZ, (const BYTE *)(LPCTSTR)icon_path, ::lstrlen (icon_path) + 1);
  487. ::RegCloseKey (reg_key);
  488. }
  489. }
  490. return ;
  491. }
  492. /////////////////////////////////////////////////////////////
  493. //
  494. // PreTranslateMessage
  495. //
  496. // This is a hack to ensure that translator keys are
  497. // processed even if we are in one of the sidebar windows.
  498. //
  499. /////////////////////////////////////////////////////////////
  500. BOOL
  501. CLevelEditApp::PreTranslateMessage (MSG *pMsg)
  502. {
  503. BOOL retval = false;
  504. //
  505. // Is this window registered for global translation?
  506. //
  507. if (pMsg->hwnd != NULL && ::GetProp (pMsg->hwnd, "TRANS_ACCS") == (HANDLE)1) {
  508. //
  509. // Translate the accelerator
  510. //
  511. CMainFrame *main_wnd = (CMainFrame *)::AfxGetMainWnd ();
  512. if (main_wnd != NULL) {
  513. HACCEL accel_table = main_wnd->GetDefaultAccelerator ();
  514. if (::TranslateAccelerator (main_wnd->m_hWnd, accel_table, pMsg)) {
  515. retval = true;
  516. }
  517. }
  518. }
  519. if (retval == false) {
  520. retval = CWinApp::PreTranslateMessage (pMsg);
  521. }
  522. return retval;
  523. }