translateobj.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 : wwtranslatedb *
  23. * *
  24. * $Archive:: /Commando/Code/wwtranslatedb/translateobj.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 9/13/01 9:04a $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "translateobj.h"
  36. #include "chunkio.h"
  37. #include "persistfactory.h"
  38. #include "translatedbids.h"
  39. #include "translatedb.h"
  40. #include "tdbcategories.h"
  41. //////////////////////////////////////////////////////////////////////////////
  42. // Persist factory
  43. //////////////////////////////////////////////////////////////////////////////
  44. SimplePersistFactoryClass<TDBObjClass, CHUNKID_TRANSLATE_OBJ> _TranslateObjPersistFactory;
  45. //////////////////////////////////////////////////////////////////////////////
  46. // Constants
  47. //////////////////////////////////////////////////////////////////////////////
  48. enum
  49. {
  50. CHUNKID_VARIABLES = 0x06141108,
  51. CHUNKID_BASE_CLASS,
  52. CHUNKID_ENGLISH_STRING,
  53. CHUNKID_TRANSLATED_STRING
  54. };
  55. enum
  56. {
  57. VARID_ID = 0x01,
  58. VARID_ID_DESC,
  59. VARID_STRING,
  60. VARID_ENGLISH_STRING,
  61. VARID_SOUND_ID,
  62. VARID_ANIMATION_NAME,
  63. VARID_CATEGORY_ID
  64. };
  65. ///////////////////////////////////////////////////////////////////////
  66. //
  67. // TDBObjClass
  68. //
  69. ///////////////////////////////////////////////////////////////////////
  70. TDBObjClass::TDBObjClass (void) :
  71. ID (0),
  72. SoundID (-1),
  73. CategoryID (CATEGORY_DEFAULT)
  74. {
  75. return ;
  76. }
  77. ///////////////////////////////////////////////////////////////////////
  78. //
  79. // TDBObjClass
  80. //
  81. ///////////////////////////////////////////////////////////////////////
  82. TDBObjClass::TDBObjClass (const TDBObjClass &src) :
  83. ID (0),
  84. SoundID (-1),
  85. CategoryID (CATEGORY_DEFAULT)
  86. {
  87. (*this) = src;
  88. return ;
  89. }
  90. ///////////////////////////////////////////////////////////////////////
  91. //
  92. // ~TDBObjClass
  93. //
  94. ///////////////////////////////////////////////////////////////////////
  95. TDBObjClass::~TDBObjClass (void)
  96. {
  97. return ;
  98. }
  99. /////////////////////////////////////////////////////////////////
  100. //
  101. // operator=
  102. //
  103. /////////////////////////////////////////////////////////////////
  104. const TDBObjClass &
  105. TDBObjClass::operator= (const TDBObjClass &src)
  106. {
  107. TranslatedStrings = src.TranslatedStrings;
  108. EnglishString = src.EnglishString;
  109. IDDesc = src.IDDesc;
  110. ID = src.ID;
  111. SoundID = src.SoundID;
  112. AnimationName = src.AnimationName;
  113. CategoryID = src.CategoryID;
  114. return *this;
  115. }
  116. ///////////////////////////////////////////////////////////////////////
  117. //
  118. // Get_Factory
  119. //
  120. ///////////////////////////////////////////////////////////////////////
  121. const PersistFactoryClass &
  122. TDBObjClass::Get_Factory (void) const
  123. {
  124. return _TranslateObjPersistFactory;
  125. }
  126. /////////////////////////////////////////////////////////////////
  127. //
  128. // Save
  129. //
  130. /////////////////////////////////////////////////////////////////
  131. bool
  132. TDBObjClass::Save (ChunkSaveClass &csave)
  133. {
  134. csave.Begin_Chunk (CHUNKID_BASE_CLASS);
  135. PersistClass::Save (csave);
  136. csave.End_Chunk ();
  137. csave.Begin_Chunk (CHUNKID_VARIABLES);
  138. Save_Variables (csave);
  139. csave.End_Chunk ();
  140. //
  141. // Write the english string to its own chunk
  142. //
  143. WRITE_WWSTRING_CHUNK (csave, CHUNKID_ENGLISH_STRING, EnglishString);
  144. //
  145. // Are we saving all the translations or just the current one?
  146. //
  147. if (TranslateDBClass::Is_Single_Language_Export_Enabled ()) {
  148. //
  149. // If we don't have the requested language, save the english
  150. //
  151. int lang_id = TranslateDBClass::Get_Current_Language ();
  152. if (lang_id >= TranslatedStrings.Count ()) {
  153. lang_id = TranslateDBClass::LANGID_ENGLISH;
  154. }
  155. //
  156. // Save the string
  157. //
  158. const WideStringClass &string = TranslatedStrings[lang_id];
  159. WRITE_WIDESTRING_CHUNK (csave, CHUNKID_TRANSLATED_STRING, string);
  160. } else {
  161. //
  162. // Write each translated string to its own chunk
  163. //
  164. for (int index = 0; index < TranslatedStrings.Count (); index ++) {
  165. const WideStringClass &string = TranslatedStrings[index];
  166. WRITE_WIDESTRING_CHUNK(csave, CHUNKID_TRANSLATED_STRING, string);;
  167. }
  168. }
  169. return true;
  170. }
  171. /////////////////////////////////////////////////////////////////
  172. //
  173. // Load
  174. //
  175. /////////////////////////////////////////////////////////////////
  176. bool
  177. TDBObjClass::Load (ChunkLoadClass &cload)
  178. {
  179. while (cload.Open_Chunk ()) {
  180. switch (cload.Cur_Chunk_ID ()) {
  181. READ_WWSTRING_CHUNK (cload, CHUNKID_ENGLISH_STRING, EnglishString);
  182. case CHUNKID_TRANSLATED_STRING:
  183. {
  184. //
  185. // Load the translated string from its chunk
  186. //
  187. WideStringClass string;
  188. cload.Read (string.Get_Buffer((cload.Cur_Chunk_Length () + 1) / 2), cload.Cur_Chunk_Length ());
  189. //
  190. // Add the translated string to our list
  191. //
  192. TranslatedStrings.Add (string);
  193. }
  194. break;
  195. case CHUNKID_BASE_CLASS:
  196. PersistClass::Load (cload);
  197. break;
  198. case CHUNKID_VARIABLES:
  199. Load_Variables (cload);
  200. break;
  201. }
  202. cload.Close_Chunk ();
  203. }
  204. return true;
  205. }
  206. /////////////////////////////////////////////////////////////////
  207. //
  208. // Save_Variables
  209. //
  210. /////////////////////////////////////////////////////////////////
  211. void
  212. TDBObjClass::Save_Variables (ChunkSaveClass &csave)
  213. {
  214. //
  215. // Save each variable to its own micro chunk
  216. //
  217. WRITE_MICRO_CHUNK (csave, VARID_ID, ID);
  218. WRITE_MICRO_CHUNK (csave, VARID_SOUND_ID, SoundID);
  219. WRITE_MICRO_CHUNK (csave, VARID_CATEGORY_ID, CategoryID);
  220. WRITE_MICRO_CHUNK_WWSTRING (csave, VARID_ID_DESC, IDDesc);
  221. WRITE_MICRO_CHUNK_WWSTRING (csave, VARID_ANIMATION_NAME, AnimationName);
  222. return ;
  223. }
  224. /////////////////////////////////////////////////////////////////
  225. //
  226. // Load_Variables
  227. //
  228. /////////////////////////////////////////////////////////////////
  229. void
  230. TDBObjClass::Load_Variables (ChunkLoadClass &cload)
  231. {
  232. while (cload.Open_Micro_Chunk ()) {
  233. switch (cload.Cur_Micro_Chunk_ID ()) {
  234. READ_MICRO_CHUNK (cload, VARID_ID, ID);
  235. READ_MICRO_CHUNK (cload, VARID_SOUND_ID, SoundID);
  236. READ_MICRO_CHUNK (cload, VARID_CATEGORY_ID, CategoryID);
  237. READ_MICRO_CHUNK_WWSTRING (cload, VARID_ID_DESC, IDDesc);
  238. READ_MICRO_CHUNK_WWSTRING (cload, VARID_ENGLISH_STRING, EnglishString);
  239. READ_MICRO_CHUNK_WWSTRING (cload, VARID_ANIMATION_NAME, AnimationName);
  240. case VARID_STRING:
  241. {
  242. //
  243. // Load the translated string from its chunk
  244. //
  245. WideStringClass string;
  246. cload.Read (string.Get_Buffer((cload.Cur_Micro_Chunk_Length () + 1) / 2), cload.Cur_Micro_Chunk_Length ());
  247. //
  248. // Add the translated string to our list
  249. //
  250. TranslatedStrings.Add (string);
  251. }
  252. break;
  253. }
  254. cload.Close_Micro_Chunk ();
  255. }
  256. return ;
  257. }
  258. /////////////////////////////////////////////////////////////////
  259. //
  260. // Set_English_String
  261. //
  262. /////////////////////////////////////////////////////////////////
  263. void
  264. TDBObjClass::Set_English_String (const TCHAR *string)
  265. {
  266. EnglishString = string;
  267. //
  268. // Grow the translated string array out large enough so
  269. // we can index into the english string
  270. //
  271. while (TranslatedStrings.Count () <= TranslateDBClass::LANGID_ENGLISH) {
  272. TranslatedStrings.Add (WideStringClass (L""));
  273. }
  274. TranslatedStrings[TranslateDBClass::LANGID_ENGLISH].Convert_From (string);
  275. return ;
  276. }
  277. /////////////////////////////////////////////////////////////////
  278. //
  279. // Set_ID
  280. //
  281. /////////////////////////////////////////////////////////////////
  282. void
  283. TDBObjClass::Set_ID (uint32 id)
  284. {
  285. ID = id;
  286. return ;
  287. }
  288. /////////////////////////////////////////////////////////////////
  289. //
  290. // Set_ID_Desc
  291. //
  292. /////////////////////////////////////////////////////////////////
  293. void
  294. TDBObjClass::Set_ID_Desc (const TCHAR *desc)
  295. {
  296. IDDesc = desc;
  297. return ;
  298. }
  299. /////////////////////////////////////////////////////////////////
  300. //
  301. // Get_String
  302. //
  303. /////////////////////////////////////////////////////////////////
  304. const WideStringClass &
  305. TDBObjClass::Get_String (void)
  306. {
  307. //
  308. // Lookup the translated string based on the current language
  309. //
  310. return Get_String (TranslateDBClass::Get_Current_Language ());
  311. }
  312. /////////////////////////////////////////////////////////////////
  313. //
  314. // Get_String
  315. //
  316. /////////////////////////////////////////////////////////////////
  317. const WideStringClass &
  318. TDBObjClass::Get_String (uint32 lang_id)
  319. {
  320. //
  321. // Default to english on error
  322. //
  323. if (lang_id >= (uint32)TranslatedStrings.Count ()) {
  324. lang_id = TranslateDBClass::LANGID_ENGLISH;
  325. }
  326. return TranslatedStrings[lang_id];
  327. }
  328. /////////////////////////////////////////////////////////////////
  329. //
  330. // Set_String
  331. //
  332. /////////////////////////////////////////////////////////////////
  333. void
  334. TDBObjClass::Set_String (uint32 lang_id, const WCHAR *string)
  335. {
  336. //
  337. // Grow the translated strings array until we have enough
  338. // to cover the requested language
  339. //
  340. while ((uint32)TranslatedStrings.Count () <= lang_id) {
  341. TranslatedStrings.Add (WideStringClass (L""));
  342. }
  343. //
  344. // Set the string
  345. //
  346. TranslatedStrings[lang_id] = string;
  347. //
  348. // Keep a copy of the english string (if necessary)
  349. //
  350. if (lang_id == TranslateDBClass::LANGID_ENGLISH) {
  351. TranslatedStrings[lang_id].Convert_To (EnglishString);
  352. }
  353. return ;
  354. }
  355. /////////////////////////////////////////////////////////////////
  356. //
  357. // Contains_Translation
  358. //
  359. /////////////////////////////////////////////////////////////////
  360. bool
  361. TDBObjClass::Contains_Translation (uint32 lang_id)
  362. {
  363. bool retval = false;
  364. //
  365. // Simply check to see if the string contains any data
  366. //
  367. if (TranslatedStrings.Count () > (int)lang_id) {
  368. retval = (TranslatedStrings[lang_id].Get_Length () > 0);
  369. }
  370. return retval;
  371. }