EditScript.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/EditScript.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 11/02/00 4:17p $*
  29. * *
  30. * $Revision:: 6 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "stdafx.h"
  36. #include "editscript.h"
  37. #include "..\..\scripts\ScriptEvents.H"
  38. #include "scripts.h"
  39. #include "chunkio.h"
  40. #include "scriptmgr.h"
  41. enum
  42. {
  43. CHUNKID_VARIABLES = 0x10271120,
  44. CHUNKID_VALUES
  45. };
  46. enum
  47. {
  48. VARID_NAME = 0x01,
  49. VARID_PARAM_DESC,
  50. VARID_VALUES
  51. };
  52. ////////////////////////////////////////////////////////////////
  53. //
  54. // EditScriptClass
  55. //
  56. ////////////////////////////////////////////////////////////////
  57. EditScriptClass::EditScriptClass (void)
  58. {
  59. return ;
  60. }
  61. ////////////////////////////////////////////////////////////////
  62. //
  63. // EditScriptClass
  64. //
  65. ////////////////////////////////////////////////////////////////
  66. EditScriptClass::EditScriptClass (const EditScriptClass &src)
  67. {
  68. *this = src;
  69. return ;
  70. }
  71. ////////////////////////////////////////////////////////////////
  72. //
  73. // ~EditScriptClass
  74. //
  75. ////////////////////////////////////////////////////////////////
  76. EditScriptClass::~EditScriptClass (void)
  77. {
  78. return ;
  79. }
  80. ////////////////////////////////////////////////////////////////
  81. //
  82. // operator=
  83. //
  84. ////////////////////////////////////////////////////////////////
  85. const EditScriptClass &
  86. EditScriptClass::operator= (const EditScriptClass &src)
  87. {
  88. m_Name = src.m_Name;
  89. m_ParamValues = src.m_ParamValues;
  90. m_ParamDescription = src.m_ParamDescription;
  91. return *this;
  92. }
  93. ////////////////////////////////////////////////////////////////
  94. //
  95. // operator==
  96. //
  97. ////////////////////////////////////////////////////////////////
  98. bool
  99. EditScriptClass::operator== (const EditScriptClass &src)
  100. {
  101. bool retval = true;
  102. // Are the params the same?
  103. retval &= bool(m_ParamValues.Count () == src.m_ParamValues.Count ());
  104. retval &= bool(m_ParamDescription.Compare_No_Case (src.m_ParamDescription) == 0);
  105. return retval;
  106. }
  107. ////////////////////////////////////////////////////////////////
  108. //
  109. // Set_Composite_Values
  110. //
  111. ////////////////////////////////////////////////////////////////
  112. void
  113. EditScriptClass::Set_Composite_Values (LPCTSTR values)
  114. {
  115. // Build a list of values from the composite string
  116. CString *value_list = NULL;
  117. int count = ::Build_List_From_String (values, ",", &value_list);
  118. // Pass these values onto our list
  119. for (int index = 0; (index < m_ParamValues.Count ()) && (index < count); index ++) {
  120. m_ParamValues[index].value = value_list[index];
  121. }
  122. SAFE_DELETE_ARRAY (value_list);
  123. return ;
  124. }
  125. ////////////////////////////////////////////////////////////////
  126. //
  127. // Build_Composite_String
  128. //
  129. ////////////////////////////////////////////////////////////////
  130. CString
  131. EditScriptClass::Build_Composite_String (void) const
  132. {
  133. CString composite_string;
  134. // Build a composite string out of the values for each param
  135. int count = m_ParamValues.Count ();
  136. for (int index = 0; index < count; index ++) {
  137. const PARAM_VALUE &value = m_ParamValues[index];
  138. composite_string += value.value;
  139. // Make sure the entries are comma delimited
  140. if (index != (count - 1)) {
  141. composite_string += ",";
  142. }
  143. }
  144. // Return a string object containing the values
  145. return composite_string;
  146. }
  147. ////////////////////////////////////////////////////////////////
  148. //
  149. // Update_Data
  150. //
  151. ////////////////////////////////////////////////////////////////
  152. void
  153. EditScriptClass::Update_Data (void)
  154. {
  155. // Start with a clean list of values
  156. m_ParamValues.Delete_All ();
  157. // Build a list of parameters from the description string
  158. CString *param_list = NULL;
  159. int count = ::Build_List_From_String (m_ParamDescription, ",", &param_list);
  160. // Loop through the paramter descriptions
  161. for (int index = 0; index < count; index ++) {
  162. CString param_desc = param_list[index];
  163. CString name = param_desc;
  164. CString def_value;
  165. CString type;
  166. // Look for a default value and a type specifier
  167. int def_index = param_desc.Find ('=');
  168. int type_index = param_desc.Find (':');
  169. // Was there a type specifier?
  170. if (type_index != -1) {
  171. name = param_desc.Left (type_index);
  172. type = &(((LPCTSTR)param_desc)[type_index+1]);
  173. type.TrimLeft ();
  174. type.TrimRight ();
  175. param_desc = name;
  176. }
  177. // Was there a default value?
  178. if (def_index != -1) {
  179. name = param_desc.Left (def_index);
  180. def_value = &(((LPCTSTR)param_desc)[def_index+1]);
  181. }
  182. //
  183. // Add this parameter to our list
  184. //
  185. PARAM_VALUE value;
  186. value.name = name;
  187. value.value = def_value;
  188. value.type = String_To_Type (type);
  189. m_ParamValues.Add (value);
  190. }
  191. SAFE_DELETE_ARRAY (param_list);
  192. return ;
  193. }
  194. ////////////////////////////////////////////////////////////////
  195. //
  196. // String_To_Type
  197. //
  198. ////////////////////////////////////////////////////////////////
  199. PARAM_TYPES
  200. EditScriptClass::String_To_Type (LPCTSTR type_name)
  201. {
  202. // Look through all the known specifiers and see if any match
  203. PARAM_TYPES type = PARAM_TYPE_INT;
  204. bool found = false;
  205. for (int type_index = 0; (type_index < PARAM_TYPE_COUNT) && (found == false); type_index ++) {
  206. // Is this the type specifier we were looking for?
  207. if (::lstrcmpi (type_name, PARAM_TYPE_STRINGS[type_index]) == 0) {
  208. type = (PARAM_TYPES)type_index;
  209. found = true;
  210. }
  211. }
  212. // Return the type specifier ID
  213. return type;
  214. }
  215. ////////////////////////////////////////////////////////////////
  216. //
  217. // Create_Script
  218. //
  219. ////////////////////////////////////////////////////////////////
  220. ScriptClass *
  221. EditScriptClass::Create_Script (void)
  222. {
  223. //
  224. // Create the new script
  225. //
  226. ScriptClass *script = ScriptManager::Create_Script (m_Name);
  227. if (script != NULL) {
  228. //
  229. // Convert our params to a string and pass them
  230. // onto the script
  231. //
  232. CString params = Build_Composite_String ();
  233. script->Set_Parameters_String (params);
  234. }
  235. return script;
  236. }
  237. /////////////////////////////////////////////////////////////////
  238. //
  239. // Save
  240. //
  241. /////////////////////////////////////////////////////////////////
  242. bool
  243. EditScriptClass::Save (ChunkSaveClass &csave)
  244. {
  245. csave.Begin_Chunk (CHUNKID_VARIABLES);
  246. WRITE_MICRO_CHUNK_WWSTRING (csave, VARID_NAME, m_Name);
  247. //WRITE_MICRO_CHUNK_WWSTRING (csave, VARID_PARAM_DESC, m_ParamDescription);
  248. //StringClass values = (LPCTSTR)Build_Composite_String ();
  249. //WRITE_MICRO_CHUNK_WWSTRING (csave, VARID_VALUES, values);
  250. csave.End_Chunk ();
  251. csave.Begin_Chunk (CHUNKID_VALUES);
  252. StringClass values = (LPCTSTR)Build_Composite_String ();
  253. csave.Write ((const char *)values, values.Get_Length () + 1);
  254. csave.End_Chunk ();
  255. return true;
  256. }
  257. /////////////////////////////////////////////////////////////////
  258. //
  259. // Load
  260. //
  261. /////////////////////////////////////////////////////////////////
  262. bool
  263. EditScriptClass::Load (ChunkLoadClass &cload)
  264. {
  265. bool retval = true;
  266. while (cload.Open_Chunk ()) {
  267. switch (cload.Cur_Chunk_ID ())
  268. {
  269. case CHUNKID_VARIABLES:
  270. retval &= Load_Variables (cload);
  271. break;
  272. case CHUNKID_VALUES:
  273. {
  274. //
  275. // Read the parameter value string from the chunk
  276. //
  277. StringClass values;
  278. int len = cload.Cur_Chunk_Length ();
  279. cload.Read (values.Get_Buffer (len), len);
  280. //
  281. // Store the parameter values in our internal data structures
  282. //
  283. Set_Composite_Values (values);
  284. }
  285. break;
  286. }
  287. cload.Close_Chunk ();
  288. }
  289. return retval;
  290. }
  291. ///////////////////////////////////////////////////////////////////////
  292. //
  293. // Load_Variables
  294. //
  295. ///////////////////////////////////////////////////////////////////////
  296. bool
  297. EditScriptClass::Load_Variables (ChunkLoadClass &cload)
  298. {
  299. StringClass values;
  300. //
  301. // Loop through all the microchunks that define the variables
  302. //
  303. while (cload.Open_Micro_Chunk ()) {
  304. switch (cload.Cur_Micro_Chunk_ID ()) {
  305. READ_MICRO_CHUNK_WWSTRING (cload, VARID_NAME, m_Name);
  306. READ_MICRO_CHUNK_WWSTRING (cload, VARID_VALUES, values);
  307. }
  308. cload.Close_Micro_Chunk ();
  309. }
  310. //
  311. // Make sure we use the current version of the parameter list
  312. //
  313. Lookup_Param_Description ();
  314. Set_Composite_Values (values);
  315. return true;
  316. }
  317. ///////////////////////////////////////////////////////////////////////
  318. //
  319. // Lookup_Param_Description
  320. //
  321. ///////////////////////////////////////////////////////////////////////
  322. void
  323. EditScriptClass::Lookup_Param_Description (void)
  324. {
  325. //
  326. // Try to find the default instance of the script
  327. //
  328. EditScriptClass *current_version = ScriptMgrClass::Find_Script (m_Name);
  329. if (current_version != NULL) {
  330. m_ParamDescription = current_version->m_ParamDescription;
  331. } else {
  332. m_ParamDescription = "";
  333. }
  334. Update_Data ();
  335. return ;
  336. }