PresetMgr.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : LevelEdit *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/LevelEdit/PresetMgr.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 2/22/02 4:08p $*
  29. * *
  30. * $Revision:: 24 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "presetmgr.h"
  37. #include "persist.h"
  38. #include "persistfactory.h"
  39. #include "preset.h"
  40. #include "utils.h"
  41. #include "definition.h"
  42. #include "editorchunkids.h"
  43. #include "filemgr.h"
  44. #include "filelocations.h"
  45. #include "resource.h"
  46. #include "presetslibform.h"
  47. #include "nodemgr.h"
  48. #include "nodecategories.h"
  49. #include "definitionfactory.h"
  50. #include "definitionfactorymgr.h"
  51. #include "twiddler.h"
  52. #include "leveledit.h"
  53. #include "regkeys.h"
  54. #include "assetdatabase.h"
  55. #include "editorbuild.h"
  56. ///////////////////////////////////////////////////////////////////////
  57. // Global singleton instance
  58. ///////////////////////////////////////////////////////////////////////
  59. PresetMgrClass _ThePresetMgr;
  60. ///////////////////////////////////////////////////////////////////////
  61. // Static member initialization
  62. ///////////////////////////////////////////////////////////////////////
  63. PresetClass * PresetMgrClass::_PresetListHead = NULL;
  64. DynamicVectorClass<int> PresetMgrClass::_DirtyPresetList;
  65. bool PresetMgrClass::_PresetsDirty = false;
  66. bool PresetMgrClass::_ImmediateCheckInMode = false;
  67. ///////////////////////////////////////////////////////////////////////
  68. // Constants
  69. ///////////////////////////////////////////////////////////////////////
  70. enum
  71. {
  72. CHUNKID_VARIABLES = 0x00000100,
  73. CHUNKID_PRESETS,
  74. CHUNKID_EMBEDDED_NODE_DATA,
  75. CHUNKID_PRESET_ID,
  76. CHUNKID_NODE_LIST
  77. };
  78. ///////////////////////////////////////////////////////////////////////
  79. //
  80. // PresetMgrClass
  81. //
  82. ///////////////////////////////////////////////////////////////////////
  83. PresetMgrClass::PresetMgrClass (void)
  84. {
  85. return ;
  86. }
  87. ///////////////////////////////////////////////////////////////////////
  88. //
  89. // ~PresetMgrClass
  90. //
  91. ///////////////////////////////////////////////////////////////////////
  92. PresetMgrClass::~PresetMgrClass (void)
  93. {
  94. Free_Presets ();
  95. return ;
  96. }
  97. ////////////////////////////////////////////////////////////////////////////
  98. //
  99. // Initialize
  100. //
  101. ////////////////////////////////////////////////////////////////////////////
  102. void
  103. PresetMgrClass::Initialize (void)
  104. {
  105. _ImmediateCheckInMode = (bool)(theApp.GetProfileInt (CONFIG_KEY, IMMEDIATE_CHECKIN_VALUE, 1) == 1);
  106. return ;
  107. }
  108. ////////////////////////////////////////////////////////////////////////////
  109. //
  110. // Shutdown
  111. //
  112. ////////////////////////////////////////////////////////////////////////////
  113. void
  114. PresetMgrClass::Shutdown (void)
  115. {
  116. theApp.WriteProfileInt (CONFIG_KEY, IMMEDIATE_CHECKIN_VALUE, (int)_ImmediateCheckInMode);
  117. return ;
  118. }
  119. ////////////////////////////////////////////////////////////////////////////
  120. //
  121. // Free_Presets
  122. //
  123. ////////////////////////////////////////////////////////////////////////////
  124. void
  125. PresetMgrClass::Free_Presets (void)
  126. {
  127. PresetClass *curr_preset = NULL;
  128. PresetClass *next_preset = NULL;
  129. PresetClass *old_head = _PresetListHead;
  130. _PresetListHead = NULL;
  131. //
  132. // Delete all the presets that are currently in our list
  133. //
  134. for (curr_preset = old_head; curr_preset != NULL; curr_preset = next_preset) {
  135. next_preset = curr_preset->m_NextPreset;
  136. delete curr_preset;
  137. }
  138. return ;
  139. }
  140. ////////////////////////////////////////////////////////////////////////////
  141. //
  142. // Add_Preset
  143. //
  144. ////////////////////////////////////////////////////////////////////////////
  145. void
  146. PresetMgrClass::Add_Preset (PresetClass *preset)
  147. {
  148. WWASSERT (preset->m_NextPreset == NULL);
  149. WWASSERT (preset->m_PrevPreset == NULL);
  150. //
  151. // Don't link the new preset unless its not in the list.
  152. //
  153. if ((preset->m_NextPreset == NULL) &&
  154. (preset->m_PrevPreset == NULL)) {
  155. Link_Preset (preset);
  156. }
  157. return ;
  158. }
  159. ////////////////////////////////////////////////////////////////////////////
  160. //
  161. // Remove_Preset
  162. //
  163. ////////////////////////////////////////////////////////////////////////////
  164. void
  165. PresetMgrClass::Remove_Preset (PresetClass *preset)
  166. {
  167. WWASSERT (preset != 0);
  168. //
  169. // Only unlink the preset if its in the list
  170. //
  171. if ((preset->m_NextPreset != NULL) ||
  172. (preset->m_PrevPreset != NULL) ||
  173. (preset == _PresetListHead))
  174. {
  175. Unlink_Preset (preset);
  176. }
  177. return ;
  178. }
  179. ////////////////////////////////////////////////////////////////////////////
  180. //
  181. // Link_Preset
  182. //
  183. ////////////////////////////////////////////////////////////////////////////
  184. void
  185. PresetMgrClass::Link_Preset (PresetClass *preset)
  186. {
  187. WWASSERT (preset->m_NextPreset == 0);
  188. WWASSERT (preset->m_PrevPreset == 0);
  189. // Add this preset in front of the current head of the list
  190. preset->m_NextPreset = _PresetListHead;
  191. // If the list wasn't empty, link the next definiton back to this preset
  192. if (preset->m_NextPreset != 0) {
  193. preset->m_NextPreset->m_PrevPreset = preset;
  194. }
  195. // Point the head of the list at this preset now
  196. _PresetListHead = preset;
  197. return ;
  198. }
  199. ////////////////////////////////////////////////////////////////////////////
  200. //
  201. // Unlink_Preset
  202. //
  203. ////////////////////////////////////////////////////////////////////////////
  204. void
  205. PresetMgrClass::Unlink_Preset (PresetClass *preset)
  206. {
  207. WWASSERT(preset != 0);
  208. // Handle the preset's prev pointer:
  209. if (preset->m_PrevPreset == 0) {
  210. // this preset is the head
  211. WWASSERT (_PresetListHead == preset);
  212. _PresetListHead = preset->m_NextPreset;
  213. } else {
  214. // link it's prev with it's next
  215. preset->m_PrevPreset->m_NextPreset = preset->m_NextPreset;
  216. }
  217. // Handle the preset's next pointer if its not at the end of the list:
  218. if (preset->m_NextPreset != 0) {
  219. preset->m_NextPreset->m_PrevPreset = preset->m_PrevPreset;
  220. }
  221. // The preset is now un-linked
  222. preset->m_NextPreset = NULL;
  223. preset->m_PrevPreset = NULL;
  224. return ;
  225. }
  226. ///////////////////////////////////////////////////////////////////////
  227. //
  228. // Chunk_ID
  229. //
  230. ///////////////////////////////////////////////////////////////////////
  231. uint32
  232. PresetMgrClass::Chunk_ID (void) const
  233. {
  234. return CHUNKID_PRESETMGR;
  235. }
  236. ///////////////////////////////////////////////////////////////////////
  237. //
  238. // Contains_Data
  239. //
  240. ///////////////////////////////////////////////////////////////////////
  241. bool
  242. PresetMgrClass::Contains_Data (void) const
  243. {
  244. return (_PresetListHead != NULL);
  245. }
  246. ///////////////////////////////////////////////////////////////////////
  247. //
  248. // Save_Embedded_Node_Data
  249. //
  250. ///////////////////////////////////////////////////////////////////////
  251. bool
  252. PresetMgrClass::Save_Embedded_Node_Data (ChunkSaveClass &csave)
  253. {
  254. //
  255. // Loop over all the presets
  256. //
  257. for ( PresetClass *preset = _PresetListHead;
  258. preset != NULL;
  259. preset = preset->m_NextPreset)
  260. {
  261. //
  262. // Does this preset have any embedded nodes?
  263. //
  264. NODE_LIST &node_list = preset->Get_Node_List ();
  265. if (node_list.Count () > 0) {
  266. //
  267. // Save this preset's ID
  268. //
  269. csave.Begin_Chunk (CHUNKID_PRESET_ID);
  270. int id = preset->Get_ID ();
  271. csave.Write (&id, sizeof (id));
  272. csave.End_Chunk ();
  273. //
  274. // Save this preset's node list
  275. //
  276. csave.Begin_Chunk (CHUNKID_NODE_LIST);
  277. NodeMgrClass::Save_Node_List (csave, node_list);
  278. csave.End_Chunk ();
  279. }
  280. }
  281. return true;
  282. }
  283. ///////////////////////////////////////////////////////////////////////
  284. //
  285. // Load_Embedded_Node_Data
  286. //
  287. ///////////////////////////////////////////////////////////////////////
  288. bool
  289. PresetMgrClass::Load_Embedded_Node_Data (ChunkLoadClass &cload)
  290. {
  291. PresetClass *curr_preset = NULL;
  292. while (cload.Open_Chunk ()) {
  293. switch (cload.Cur_Chunk_ID ()) {
  294. case CHUNKID_PRESET_ID:
  295. {
  296. //
  297. // Set the current preset
  298. //
  299. int preset_id = 0;
  300. cload.Read (&preset_id, sizeof (preset_id));
  301. curr_preset = Find_Preset (preset_id);
  302. break;
  303. }
  304. //
  305. // Load the node list for the current preset
  306. //
  307. case CHUNKID_NODE_LIST:
  308. if (curr_preset != NULL) {
  309. NodeMgrClass::Load_Node_List (cload, curr_preset->Get_Node_List ());
  310. }
  311. break;
  312. }
  313. cload.Close_Chunk ();
  314. }
  315. return true;
  316. }
  317. ///////////////////////////////////////////////////////////////////////
  318. //
  319. // Save
  320. //
  321. ///////////////////////////////////////////////////////////////////////
  322. bool
  323. PresetMgrClass::Save (ChunkSaveClass &csave)
  324. {
  325. bool retval = true;
  326. //
  327. // Write the presets to their own chunk
  328. //
  329. csave.Begin_Chunk (CHUNKID_PRESETS);
  330. retval &= Save_Presets (csave);
  331. csave.End_Chunk ();
  332. //
  333. // Write the embedded node data to its own chunk
  334. //
  335. csave.Begin_Chunk (CHUNKID_EMBEDDED_NODE_DATA);
  336. retval &= Save_Embedded_Node_Data (csave);
  337. csave.End_Chunk ();
  338. return retval;
  339. }
  340. ///////////////////////////////////////////////////////////////////////
  341. //
  342. // Load
  343. //
  344. ///////////////////////////////////////////////////////////////////////
  345. bool
  346. PresetMgrClass::Load (ChunkLoadClass &cload)
  347. {
  348. bool retval = true;
  349. while (cload.Open_Chunk ()) {
  350. switch (cload.Cur_Chunk_ID ()) {
  351. //
  352. // Load all the presets from this chunk
  353. //
  354. case CHUNKID_PRESETS:
  355. retval &= Load_Presets (cload);
  356. break;
  357. //
  358. // Load any embedded node data for any presets
  359. // we loaded
  360. //
  361. case CHUNKID_EMBEDDED_NODE_DATA:
  362. retval &= Load_Embedded_Node_Data (cload);
  363. break;
  364. }
  365. cload.Close_Chunk ();
  366. }
  367. return retval;
  368. }
  369. ///////////////////////////////////////////////////////////////////////
  370. //
  371. // Save_Presets
  372. //
  373. ///////////////////////////////////////////////////////////////////////
  374. bool
  375. PresetMgrClass::Save_Presets (ChunkSaveClass &csave)
  376. {
  377. for ( PresetClass *preset = _PresetListHead;
  378. preset != NULL;
  379. preset = preset->m_NextPreset)
  380. {
  381. //
  382. // Save this preset to its own chunk
  383. //
  384. csave.Begin_Chunk (preset->Get_Factory ().Chunk_ID ());
  385. preset->Get_Factory ().Save (csave, preset);
  386. csave.End_Chunk ();
  387. }
  388. return true;
  389. }
  390. ///////////////////////////////////////////////////////////////////////
  391. //
  392. // Load_Presets
  393. //
  394. ///////////////////////////////////////////////////////////////////////
  395. bool
  396. PresetMgrClass::Load_Presets (ChunkLoadClass &cload)
  397. {
  398. bool retval = true;
  399. while (cload.Open_Chunk ()) {
  400. //
  401. // Load this preset from the chunk (if possible)
  402. //
  403. PersistFactoryClass *factory = SaveLoadSystemClass::Find_Persist_Factory (cload.Cur_Chunk_ID ());
  404. if (factory != NULL) {
  405. PresetClass *preset = (PresetClass *)factory->Load (cload);
  406. if (preset != NULL) {
  407. if (preset->Is_Valid ()) {
  408. Add_Preset (preset);
  409. } else {
  410. CString message;
  411. message.Format ("Detected bad preset '%s', removing from library.\r\n", preset->Get_Name ());
  412. ::Output_Message (message);
  413. }
  414. }
  415. }
  416. cload.Close_Chunk ();
  417. }
  418. return retval;
  419. }
  420. ///////////////////////////////////////////////////////////////////////
  421. //
  422. // Find_Preset
  423. //
  424. ///////////////////////////////////////////////////////////////////////
  425. PresetClass *
  426. PresetMgrClass::Find_Preset (uint32 id)
  427. {
  428. PresetClass *retval = NULL;
  429. //
  430. // Lookup the definition, then dig out the preset from the definition
  431. //
  432. DefinitionClass *definition = DefinitionMgrClass::Find_Definition (id, false);
  433. if (definition != NULL) {
  434. retval = (PresetClass *)definition->Get_User_Data ();
  435. }
  436. return retval;
  437. }
  438. ///////////////////////////////////////////////////////////////////////
  439. //
  440. // Find_Typed_Preset
  441. //
  442. ///////////////////////////////////////////////////////////////////////
  443. PresetClass *
  444. PresetMgrClass::Find_Typed_Preset (uint32 class_id, LPCTSTR name)
  445. {
  446. PresetClass *retval = NULL;
  447. //
  448. // Lookup the definition, then dig out the preset from the definition
  449. //
  450. DefinitionClass *definition = DefinitionMgrClass::Find_Typed_Definition (name, class_id, false);
  451. if (definition != NULL) {
  452. retval = (PresetClass *)definition->Get_User_Data ();
  453. }
  454. return retval;
  455. }
  456. ///////////////////////////////////////////////////////////////////////
  457. //
  458. // Find_Preset
  459. //
  460. ///////////////////////////////////////////////////////////////////////
  461. PresetClass *
  462. PresetMgrClass::Find_Preset (LPCTSTR name)
  463. {
  464. PresetClass *retval = NULL;
  465. //
  466. // Lookup the definition, then dig out the preset from the definition
  467. //
  468. DefinitionClass *definition = DefinitionMgrClass::Find_Named_Definition (name, false);
  469. if (definition != NULL) {
  470. retval = (PresetClass *)definition->Get_User_Data ();
  471. }
  472. return retval;
  473. }
  474. ///////////////////////////////////////////////////////////////////////
  475. //
  476. // Get_Next
  477. //
  478. ///////////////////////////////////////////////////////////////////////
  479. PresetClass *
  480. PresetMgrClass::Get_Next (PresetClass *preset)
  481. {
  482. return preset->m_NextPreset;
  483. }
  484. ///////////////////////////////////////////////////////////////////////
  485. //
  486. // Is_One_Of
  487. //
  488. ///////////////////////////////////////////////////////////////////////
  489. bool
  490. PresetMgrClass::Is_One_Of
  491. (
  492. uint32 id_to_find,
  493. ID_TYPE type,
  494. bool include_twiddlers,
  495. PresetClass * preset
  496. )
  497. {
  498. bool retval = false;
  499. DefinitionClass *definition = preset->Get_Definition ();
  500. if (definition != NULL) {
  501. //
  502. // Determine which class ID to check
  503. //
  504. uint32 class_id = definition->Get_Class_ID ();
  505. if (include_twiddlers && class_id == CLASSID_TWIDDLERS) {
  506. class_id = ((TwiddlerClass *)definition)->Get_Indirect_Class_ID ();
  507. }
  508. if (type == ID_CLASS) {
  509. //
  510. // Is this the class we were looking for?
  511. //
  512. if (class_id == id_to_find) {
  513. retval = true;
  514. }
  515. } else if (type == ID_SUPERCLASS) {
  516. //
  517. // Is this the superclass we were looking for?
  518. //
  519. if (SuperClassID_From_ClassID (class_id) == id_to_find) {
  520. retval = true;
  521. }
  522. }
  523. }
  524. return retval;
  525. }
  526. ///////////////////////////////////////////////////////////////////////
  527. //
  528. // Get_First
  529. //
  530. ///////////////////////////////////////////////////////////////////////
  531. PresetClass *
  532. PresetMgrClass::Get_First (uint32 id, ID_TYPE type, bool include_twiddlers)
  533. {
  534. PresetClass *req_preset = NULL;
  535. //
  536. // Loop through all the presets until we've found the
  537. // first one that matches the criteria
  538. //
  539. for ( PresetClass *preset = _PresetListHead;
  540. preset != NULL;
  541. preset = preset->m_NextPreset)
  542. {
  543. if (Is_One_Of (id, type, include_twiddlers, preset)) {
  544. req_preset = preset;
  545. break;
  546. }
  547. }
  548. return req_preset;
  549. }
  550. ///////////////////////////////////////////////////////////////////////
  551. //
  552. // Get_Next
  553. //
  554. ///////////////////////////////////////////////////////////////////////
  555. PresetClass *
  556. PresetMgrClass::Get_Next
  557. (
  558. PresetClass * current,
  559. uint32 id,
  560. ID_TYPE type,
  561. bool include_twiddlers
  562. )
  563. {
  564. PresetClass *req_preset = NULL;
  565. //
  566. // Loop through all the presets until we've found the
  567. // first one that matches the criteria
  568. //
  569. while ((current = current->m_NextPreset) != NULL)
  570. {
  571. if (Is_One_Of (id, type, include_twiddlers, current)) {
  572. req_preset = current;
  573. break;
  574. }
  575. /*DefinitionClass *definition = current->Get_Definition ();
  576. if (definition != NULL) {
  577. uint32 class_id = definition->Get_Class_ID ();
  578. if (type == ID_CLASS) {
  579. //
  580. // Is this the class we were looking for?
  581. //
  582. if (class_id == id) {
  583. req_preset = current;
  584. }
  585. } else if (type == ID_SUPERCLASS) {
  586. //
  587. // Is this the superclass we were looking for?
  588. //
  589. if (SuperClassID_From_ClassID (class_id) == id) {
  590. req_preset = current;
  591. }
  592. } else if (type == ID_PARENT) {
  593. //
  594. // Is this a child of the parent we were looking for?
  595. //
  596. PresetClass *parent = current->Get_Parent ();
  597. if (parent != NULL && parent->Get_ID () == id) {
  598. req_preset = current;
  599. }
  600. }
  601. }*/
  602. }
  603. return req_preset;
  604. }
  605. ///////////////////////////////////////////////////////////////////////
  606. //
  607. // Validate_Version
  608. //
  609. ///////////////////////////////////////////////////////////////////////
  610. void
  611. PresetMgrClass::Validate_Version (void)
  612. {
  613. #ifndef PUBLIC_EDITOR_VER
  614. if (::Get_File_Mgr ()->Is_VSS_Read_Only () == false) {
  615. if (Check_Editor_Version () == false) {
  616. //
  617. // Force the database to be read-only
  618. //
  619. ::Get_File_Mgr ()->Set_Read_Only_VSS (true);
  620. //
  621. // Warn the user
  622. //
  623. Message_Box (NULL, IDS_VERSION_ERROR_MSG, IDS_VERSION_ERROR_TITLE, MB_ICONEXCLAMATION | MB_OK | MB_SETFOREGROUND | MB_SYSTEMMODAL);
  624. }
  625. }
  626. #endif //PUBLIC_EDITOR_VER
  627. return ;
  628. }
  629. ///////////////////////////////////////////////////////////////////////
  630. //
  631. // Check_Out_Database
  632. //
  633. ///////////////////////////////////////////////////////////////////////
  634. bool
  635. PresetMgrClass::Check_Out_Database (uint32 class_id, bool *should_undo_on_err)
  636. {
  637. CWaitCursor wait_cursor;
  638. bool retval = false;
  639. (*should_undo_on_err) = false;
  640. //
  641. // Build a path to the preset library we are going to check in
  642. //
  643. CString path;
  644. ::Get_File_Mgr ()->Get_Preset_Library_Path (class_id, false, path);
  645. //
  646. // Do a quick check to make sure the current version of the
  647. // editor is allowed to update the asset database.
  648. //
  649. Validate_Version ();
  650. //
  651. // Can't check out the database if we are in read-only mode.
  652. //
  653. FileMgrClass *file_mgr = ::Get_File_Mgr ();
  654. AssetDatabaseClass &asset_db = file_mgr->Get_Database_Interface ();
  655. if (file_mgr->Is_VSS_Read_Only () == false) {
  656. //
  657. // If the file does not exist, then simply return success
  658. //
  659. if (asset_db.Does_File_Exist (path) == false) {
  660. retval = true;
  661. (*should_undo_on_err) = false;
  662. } else {
  663. //
  664. // Now, attempt to check out the file
  665. //
  666. retval = Check_Out_Database (path, should_undo_on_err);
  667. }
  668. }
  669. return retval;
  670. }
  671. ///////////////////////////////////////////////////////////////////////
  672. //
  673. // Check_Out_Database
  674. //
  675. ///////////////////////////////////////////////////////////////////////
  676. bool
  677. PresetMgrClass::Check_Out_Database (LPCTSTR path, bool *should_undo_on_err)
  678. {
  679. FileMgrClass *file_mgr = ::Get_File_Mgr ();
  680. AssetDatabaseClass &asset_db = file_mgr->Get_Database_Interface ();
  681. bool retval = false;
  682. //
  683. // Determine the checkout status of the file
  684. //
  685. StringClass user_name;
  686. AssetDatabaseClass::FILE_STATUS status = asset_db.Get_File_Status (path, &user_name);
  687. if (status == AssetDatabaseClass::NOT_CHECKED_OUT) {
  688. //
  689. // Check out the database
  690. //
  691. retval = asset_db.Retry_Check_Out (path, 10, 250);
  692. if (retval) {
  693. ::Get_Presets_Form ()->Load_Databases ();
  694. NodeMgrClass::Reload_Nodes ();
  695. ::Get_Presets_Form ()->Reload_Presets ();
  696. (*should_undo_on_err) = true;
  697. } else {
  698. ::MessageBox (::AfxGetMainWnd ()->m_hWnd, "Unable to check out the preset library.", "VSS Error", MB_ICONERROR | MB_OK);
  699. }
  700. } else if (status == AssetDatabaseClass::CHECKED_OUT) {
  701. //
  702. // Let the user know who has the library checked out.
  703. //
  704. CString message;
  705. CString title;
  706. message.Format (IDS_LIBRARY_CHECKED_OUT_TO_USER_MSG, user_name.Peek_Buffer ());
  707. title.LoadString (IDS_CANT_CHECK_OUT_TITLE);
  708. ::MessageBox (::AfxGetMainWnd ()->m_hWnd, message, title, MB_ICONEXCLAMATION | MB_OK);
  709. } else if (status == AssetDatabaseClass::CHECKED_OUT_TO_ME) {
  710. retval = true;
  711. }
  712. return retval;
  713. }
  714. ///////////////////////////////////////////////////////////////////////
  715. //
  716. // Check_In_Database
  717. //
  718. ///////////////////////////////////////////////////////////////////////
  719. bool
  720. PresetMgrClass::Check_In_Database (LPCTSTR path)
  721. {
  722. CWaitCursor wait_cursor;
  723. FileMgrClass *file_mgr = ::Get_File_Mgr ();
  724. AssetDatabaseClass &asset_db = file_mgr->Get_Database_Interface ();
  725. CString full_path = file_mgr->Make_Full_Path (path);
  726. bool retval = false;
  727. if (Is_Database_Checked_Out (full_path)) {
  728. //
  729. // Check in the database
  730. //
  731. retval = asset_db.Retry_Check_In (full_path, 10, 250);
  732. if (retval == false) {
  733. CString message;
  734. message.Format ("Unable to check in preset library: %s.", full_path);
  735. ::MessageBox (::AfxGetMainWnd ()->m_hWnd, message, "Database Error", MB_ICONERROR | MB_OK);
  736. }
  737. } else if (asset_db.Does_File_Exist (path) == false) {
  738. //
  739. // If the file did not exist, then add it to the database.
  740. //
  741. retval = asset_db.Add_File (full_path);
  742. }
  743. return retval;
  744. }
  745. ///////////////////////////////////////////////////////////////////////
  746. //
  747. // Is_Database_Checked_Out
  748. //
  749. ///////////////////////////////////////////////////////////////////////
  750. bool
  751. PresetMgrClass::Is_Database_Checked_Out (LPCTSTR full_path)
  752. {
  753. //
  754. // Ask VSS if the file is checked out to us or not
  755. //
  756. FileMgrClass *file_mgr = ::Get_File_Mgr ();
  757. AssetDatabaseClass &asset_db = file_mgr->Get_Database_Interface ();
  758. return asset_db.Get_File_Status (full_path) == AssetDatabaseClass::CHECKED_OUT_TO_ME;
  759. }
  760. ///////////////////////////////////////////////////////////////////////
  761. //
  762. // Undo_Database_Check_Out
  763. //
  764. ///////////////////////////////////////////////////////////////////////
  765. bool
  766. PresetMgrClass::Undo_Database_Check_Out (uint32 class_id)
  767. {
  768. bool retval = false;
  769. FileMgrClass *file_mgr = ::Get_File_Mgr ();
  770. //
  771. // Build a path to the preset library we are going to check in
  772. //
  773. CString path;
  774. file_mgr->Get_Preset_Library_Path (class_id, false, path);
  775. //
  776. // We only undo the checkout if its checked out to us
  777. //
  778. if (Is_Database_Checked_Out (path)) {
  779. //
  780. // Undo the checkout
  781. //
  782. AssetDatabaseClass &asset_db = file_mgr->Get_Database_Interface ();
  783. retval = asset_db.Undo_Check_Out (path);
  784. }
  785. return retval;
  786. }
  787. ///////////////////////////////////////////////////////////////////////
  788. //
  789. // Build_Preset_Tree
  790. //
  791. ///////////////////////////////////////////////////////////////////////
  792. void
  793. PresetMgrClass::Build_Preset_Tree (uint32 class_id, PRESET_TREE &tree, bool include_twiddlers)
  794. {
  795. tree.Reset ();
  796. //
  797. // Find all the presets that belong to this class
  798. //
  799. PresetClass *preset = NULL;
  800. for ( preset = Get_First (class_id, ID_CLASS, include_twiddlers);
  801. preset != NULL;
  802. preset = Get_Next (preset, class_id, ID_CLASS, include_twiddlers))
  803. {
  804. //
  805. // Add this preset to the list
  806. //
  807. if ( preset->Get_Parent () == NULL ||
  808. preset->Get_Definition ()->Get_Class_ID () == CLASSID_TWIDDLERS)
  809. {
  810. PRESET_TREE_LEAF *leaf = tree.Add_Sorted (preset, preset->Get_Name ());
  811. if (leaf != NULL) {
  812. //
  813. // Recursively fill in this presets's children
  814. //
  815. Add_Children_To_Tree (preset->Get_ID (), leaf, include_twiddlers);
  816. }
  817. }
  818. }
  819. return ;
  820. }
  821. ///////////////////////////////////////////////////////////////////////
  822. //
  823. // Add_Children_To_Tree
  824. //
  825. ///////////////////////////////////////////////////////////////////////
  826. void
  827. PresetMgrClass::Add_Children_To_Tree
  828. (
  829. uint32 parent_id,
  830. PRESET_TREE_LEAF * leaf,
  831. bool include_twiddlers
  832. )
  833. {
  834. //
  835. // Lookup the parent preset
  836. //
  837. PresetClass *parent_preset = Find_Preset (parent_id);
  838. if (parent_preset != NULL) {
  839. //
  840. // Loop over all the children of this preset
  841. //
  842. int count = parent_preset->Get_Child_Preset_Count ();
  843. for (int index = 0; index < count; index ++) {
  844. PresetClass *child_preset = parent_preset->Get_Child_Preset (index);
  845. if (child_preset != NULL) {
  846. //
  847. // Check to see if this meets the twiddler requirement
  848. //
  849. uint32 class_id = child_preset->Get_Definition ()->Get_Class_ID ();
  850. if (include_twiddlers || class_id != CLASSID_TWIDDLERS) {
  851. //
  852. // Add this preset to the tree
  853. //
  854. PRESET_TREE_LEAF *child_leaf = NULL;
  855. child_leaf = leaf->Add_Child_Sorted (child_preset, child_preset->Get_Name ());
  856. if (child_leaf != NULL) {
  857. //
  858. // Recursively fill in this presets's children
  859. //
  860. Add_Children_To_Tree (child_preset->Get_ID (), child_leaf, include_twiddlers);
  861. }
  862. }
  863. }
  864. }
  865. }
  866. return ;
  867. }
  868. ///////////////////////////////////////////////////////////////////////
  869. //
  870. // Build_Factory_Tree
  871. //
  872. ///////////////////////////////////////////////////////////////////////
  873. void
  874. PresetMgrClass::Build_Factory_Tree (uint32 class_id, FACTORY_TREE &tree)
  875. {
  876. tree.Reset ();
  877. if (class_id == 0) {
  878. //
  879. // Add all the node categories to the tree
  880. //
  881. for (int index = 0; index < PRESET_CATEGORY_COUNT; index ++) {
  882. //
  883. // Add this factory to the tree
  884. //
  885. uint32 factory_class_id = PRESET_CATEGORIES[index].clsid;
  886. //FACTORY_TREE_LEAF *leaf = (FACTORY_TREE_LEAF *)tree.Add (factory_class_id);
  887. FACTORY_TREE_LEAF *leaf = tree.Add_Sorted (factory_class_id, PRESET_CATEGORIES[index].name);
  888. //
  889. // Find all the sub-factories
  890. //
  891. if (DefinitionFactoryMgrClass::Find_Factory (PRESET_CATEGORIES[index].clsid) == NULL) {
  892. for ( DefinitionFactoryClass *factory = DefinitionFactoryMgrClass::Get_First (factory_class_id);
  893. factory != NULL;
  894. factory = DefinitionFactoryMgrClass::Get_Next (factory, factory_class_id))
  895. {
  896. if (factory->Is_Displayed ()) {
  897. leaf->Add_Child_Sorted (factory->Get_Class_ID (), factory->Get_Name ());
  898. }
  899. }
  900. }
  901. }
  902. } else {
  903. //
  904. // Find all the sub-factories
  905. //
  906. for ( DefinitionFactoryClass *factory = DefinitionFactoryMgrClass::Get_First (class_id);
  907. factory != NULL;
  908. factory = DefinitionFactoryMgrClass::Get_Next (factory, class_id))
  909. {
  910. if (factory->Is_Displayed ()) {
  911. tree.Add_Sorted (factory->Get_Class_ID (), factory->Get_Name ());
  912. }
  913. }
  914. }
  915. return ;
  916. }
  917. ///////////////////////////////////////////////////////////////////////
  918. //
  919. // Put_Presets_Back
  920. //
  921. ///////////////////////////////////////////////////////////////////////
  922. void
  923. PresetMgrClass::Put_Presets_Back (PRESET_LIST &preset_list)
  924. {
  925. //
  926. // Simply loop over the presets and add them back to the manager
  927. //
  928. for (int index = 0; index < preset_list.Count (); index ++) {
  929. PresetClass *preset = preset_list[index];
  930. Add_Preset (preset_list[index]);
  931. }
  932. return ;
  933. }
  934. ///////////////////////////////////////////////////////////////////////
  935. //
  936. // Remove_Non_Matching_Presets
  937. //
  938. ///////////////////////////////////////////////////////////////////////
  939. void
  940. PresetMgrClass::Remove_Non_Matching_Presets
  941. (
  942. uint32 class_id,
  943. bool class_id_matters,
  944. bool is_temp,
  945. PRESET_LIST & removed_preset_list
  946. )
  947. {
  948. //
  949. // Loop over all the presets in the system
  950. //
  951. PresetClass *next_preset = NULL;
  952. PresetClass *preset = NULL;
  953. for ( preset = Get_First (); preset != NULL; preset = next_preset) {
  954. next_preset = Get_Next (preset);
  955. //
  956. // Determine what class ID this preset belongs to...
  957. //
  958. uint32 curr_class_id = 0;
  959. DefinitionClass *definition = preset->Get_Definition ();
  960. if (definition != NULL) {
  961. curr_class_id = definition->Get_Class_ID ();
  962. }
  963. //
  964. // If this preset does not meet our requirements, then remove it from the
  965. // the preset meanager so it won't get saved
  966. //
  967. if ( (preset->Get_IsTemporary () != is_temp) ||
  968. (class_id_matters && (curr_class_id != class_id)))
  969. {
  970. Remove_Preset (preset);
  971. removed_preset_list.Add (preset);
  972. }
  973. }
  974. return ;
  975. }
  976. ///////////////////////////////////////////////////////////////////////
  977. //
  978. // Discard_Preset_Changes
  979. //
  980. ///////////////////////////////////////////////////////////////////////
  981. void
  982. PresetMgrClass::Discard_Preset_Changes (void)
  983. {
  984. PresetsFormClass *presets_form = ::Get_Presets_Form ();
  985. if (presets_form == NULL) {
  986. return ;
  987. }
  988. //
  989. // Build a list of all the database files
  990. //
  991. DynamicVectorClass<CString> file_list;
  992. presets_form->Build_DDB_File_List (file_list);
  993. //
  994. // Get access to the vss interface
  995. //
  996. AssetDatabaseClass &asset_db = ::Get_File_Mgr ()->Get_Database_Interface ();
  997. //
  998. // Loop over all the preset databases and undo any checkouts
  999. //
  1000. for (int index = 0; index < file_list.Count (); index ++) {
  1001. CString &filename = file_list[index];
  1002. //
  1003. // Do we have this file checked out?
  1004. //
  1005. if (asset_db.Get_File_Status (filename) == AssetDatabaseClass::CHECKED_OUT_TO_ME) {
  1006. //
  1007. // Undo the checkout
  1008. //
  1009. asset_db.Undo_Check_Out (filename);
  1010. }
  1011. }
  1012. return ;
  1013. }
  1014. ///////////////////////////////////////////////////////////////////////
  1015. //
  1016. // Check_In_Presets
  1017. //
  1018. ///////////////////////////////////////////////////////////////////////
  1019. void
  1020. PresetMgrClass::Check_In_Presets (void)
  1021. {
  1022. PresetsFormClass *presets_form = ::Get_Presets_Form ();
  1023. if (presets_form == NULL) {
  1024. return ;
  1025. }
  1026. #ifdef PUBLIC_EDITOR_VER
  1027. //
  1028. // Simply save the presets together in one file
  1029. //
  1030. CString path = ::Get_File_Mgr ()->Make_Full_Path (OBJECTS_DDB_PATH);
  1031. presets_form->Save_Presets (path, 0, false, false);
  1032. #else
  1033. //
  1034. // Build a list of all the database files
  1035. //
  1036. DynamicVectorClass<CString> file_list;
  1037. presets_form->Build_DDB_File_List (file_list);
  1038. //
  1039. // Get access to the vss interface
  1040. //
  1041. AssetDatabaseClass &asset_db = ::Get_File_Mgr ()->Get_Database_Interface ();
  1042. //
  1043. // Update all the dirty preset files
  1044. //
  1045. Add_Dirty_Preset_Files_To_VSS ();
  1046. //
  1047. // Check each database file to see if we have it checked out...
  1048. // If we do have it checked out, then go ahead and check it in.
  1049. //
  1050. for (int index = 0; index < file_list.Count (); index ++) {
  1051. CString &filename = file_list[index];
  1052. //
  1053. // If we have this file checked out then go ahead and check it in...
  1054. //
  1055. asset_db.Check_In_Ex (filename, ::AfxGetMainWnd ()->m_hWnd);
  1056. }
  1057. #endif //PUBLIC_EDITOR_VER
  1058. _PresetsDirty = false;
  1059. return ;
  1060. }
  1061. ///////////////////////////////////////////////////////////////////////
  1062. //
  1063. // Add_Dirty_Preset
  1064. //
  1065. ///////////////////////////////////////////////////////////////////////
  1066. void
  1067. PresetMgrClass::Add_Dirty_Preset (int preset_id)
  1068. {
  1069. //
  1070. // Make sure we don't add this preset multiple times
  1071. //
  1072. int index = _DirtyPresetList.ID (preset_id);
  1073. if (index < 0) {
  1074. _DirtyPresetList.Add (preset_id);
  1075. }
  1076. _PresetsDirty = true;
  1077. #ifndef PUBLIC_EDITOR_VER
  1078. //
  1079. // Check in the presets immediately (if necessary)
  1080. //
  1081. if (_ImmediateCheckInMode) {
  1082. Check_In_Presets ();
  1083. }
  1084. #endif //!PUBLIC_EDITOR_VER
  1085. return ;
  1086. }
  1087. ///////////////////////////////////////////////////////////////////////
  1088. //
  1089. // Add_Dirty_Preset_Files_To_VSS
  1090. //
  1091. ///////////////////////////////////////////////////////////////////////
  1092. void
  1093. PresetMgrClass::Add_Dirty_Preset_Files_To_VSS (void)
  1094. {
  1095. //
  1096. // Loop over all the presets in our dirty list
  1097. //
  1098. for (int index = 0; index < _DirtyPresetList.Count (); index ++) {
  1099. int preset_id = _DirtyPresetList[index];
  1100. //
  1101. // If this preset still exists, then update its files
  1102. //
  1103. PresetClass *preset = Find_Preset (preset_id);
  1104. if (preset != NULL) {
  1105. preset->Add_Files_To_VSS ();
  1106. }
  1107. }
  1108. //
  1109. // Empty the dirty list
  1110. //
  1111. _DirtyPresetList.Delete_All ();
  1112. return ;
  1113. }
  1114. ///////////////////////////////////////////////////////////////////////
  1115. //
  1116. // Create_Preset
  1117. //
  1118. ///////////////////////////////////////////////////////////////////////
  1119. PresetClass *
  1120. PresetMgrClass::Create_Preset
  1121. (
  1122. uint32 class_id,
  1123. const char * name,
  1124. bool is_temp
  1125. )
  1126. {
  1127. PresetClass *new_preset = NULL;
  1128. //
  1129. // Lookup the factory for this definition
  1130. //
  1131. DefinitionFactoryClass *factory = DefinitionFactoryMgrClass::Find_Factory (class_id);
  1132. if (factory != NULL) {
  1133. //
  1134. // Create a new definition for the preset
  1135. //
  1136. DefinitionClass *definition = factory->Create ();
  1137. //
  1138. // Assign the definition a new ID
  1139. //
  1140. if (is_temp) {
  1141. definition->Set_ID (::Get_Next_Temp_ID ());
  1142. } else {
  1143. definition->Set_ID (DefinitionMgrClass::Get_New_ID (class_id));
  1144. }
  1145. //
  1146. // Create the new preset
  1147. //
  1148. new_preset = new PresetClass;
  1149. new_preset->Set_Definition (definition);
  1150. new_preset->Set_IsTemporary (is_temp);
  1151. //
  1152. // Give the preset a name (if necessary)
  1153. //
  1154. if (name != NULL) {
  1155. new_preset->Set_Name (name);
  1156. }
  1157. }
  1158. return new_preset;
  1159. }
  1160. ///////////////////////////////////////////////////////////////////////
  1161. //
  1162. // Free_All_Embedded_Nodes
  1163. //
  1164. ///////////////////////////////////////////////////////////////////////
  1165. void
  1166. PresetMgrClass::Free_All_Embedded_Nodes (void)
  1167. {
  1168. //
  1169. // Simply ask each preset in the list to free its
  1170. // embedded nodes
  1171. //
  1172. for ( PresetClass *preset = _PresetListHead;
  1173. preset != NULL;
  1174. preset = preset->m_NextPreset)
  1175. {
  1176. preset->Free_Node_List ();
  1177. }
  1178. return ;
  1179. }