translatedb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/wwtranslatedb/translatedb.h $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 11/12/01 11:43a $*
  29. * *
  30. * $Revision:: 23 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef __TRANSLATE_DB_H
  39. #define __TRANSLATE_DB_H
  40. #include "saveloadsubsystem.h"
  41. #include "vector.h"
  42. #include "bittype.h"
  43. #include "translateobj.h"
  44. #include "tdbcategory.h"
  45. #include "hashtemplate.h"
  46. //////////////////////////////////////////////////////////////////////////
  47. // Singleton instance
  48. //////////////////////////////////////////////////////////////////////////
  49. extern class TranslateDBClass _TheTranslateDB;
  50. //////////////////////////////////////////////////////////////////////////
  51. // Typedefs
  52. //////////////////////////////////////////////////////////////////////////
  53. typedef DynamicVectorClass<TDBObjClass *> TDB_OBJ_LIST;
  54. typedef DynamicVectorClass<TDBCategoryClass *> TDB_CATEGORY_LIST;
  55. extern const WCHAR * STRING_NOT_FOUND;
  56. extern const char * ENGLISH_STRING_NOT_FOUND;
  57. //////////////////////////////////////////////////////////////////////////
  58. // Useful macros
  59. //////////////////////////////////////////////////////////////////////////
  60. #define TRANSLATION TranslateDBClass::Get_String
  61. #define TRANSLATE TranslateDBClass::Get_String
  62. #define TRANSLATE_BY_DESC(desc) TranslateDBClass::Get_String (desc);
  63. //////////////////////////////////////////////////////////////////////////
  64. //
  65. // TranslateDBClass
  66. //
  67. //////////////////////////////////////////////////////////////////////////
  68. class TranslateDBClass : public SaveLoadSubSystemClass
  69. {
  70. public:
  71. //////////////////////////////////////////////////////////////
  72. // Public constants
  73. //////////////////////////////////////////////////////////////
  74. enum
  75. {
  76. LANGID_ENGLISH = 0,
  77. LANGID_FRENCH,
  78. LANGID_GERMAN,
  79. LANGID_SPANISH,
  80. LANGID_CHINESE,
  81. LANGID_JAPANESE,
  82. LANGID_KOREAN
  83. };
  84. typedef enum
  85. {
  86. FILTER_DISABLED = 0,
  87. FILTER_IF_EQUAL,
  88. FILTER_IF_NOT_EQUAL,
  89. } FILTER_OPT;
  90. //////////////////////////////////////////////////////////////
  91. // Public constructors/destructors
  92. //////////////////////////////////////////////////////////////
  93. TranslateDBClass (void) { }
  94. virtual ~TranslateDBClass (void) { }
  95. //////////////////////////////////////////////////////////////
  96. // Public methods
  97. //////////////////////////////////////////////////////////////
  98. //
  99. // Initialization
  100. //
  101. static void Initialize (void);
  102. static void Shutdown (void);
  103. static uint32 Get_Version_Number (void);
  104. static void Update_Version (void);
  105. static bool Is_Loaded() {return (m_ObjectList.Count () > 0);}
  106. //
  107. // From SaveLoadSubSystemClass
  108. //
  109. virtual uint32 Chunk_ID (void) const;
  110. virtual const char * Name (void) const { return "TranslateDBClass"; }
  111. //
  112. // C style header file support
  113. //
  114. static void Import_Strings (const char *filename);
  115. static void Import_C_Header (const char *filename);
  116. static void Export_C_Header (const char *filename);
  117. static void Export_Table (const char *filename);
  118. //
  119. // Data access
  120. //
  121. static const WCHAR * Get_String (uint32 id);
  122. static const WCHAR * Get_String (const char *id_desc);
  123. static const char * Get_English_String (uint32 id);
  124. WWINLINE static TDBObjClass * Find_Object (uint32 id);
  125. WWINLINE static TDBObjClass * Find_Object (const char *id_desc);
  126. //
  127. // Content management
  128. //
  129. static bool Add_Object (TDBObjClass *new_obj);
  130. static bool Remove_Object (int index);
  131. static void Remove_All (void);
  132. //
  133. // Enumeration
  134. //
  135. static int Get_Object_Count (void);
  136. static TDBObjClass * Get_Object (int index);
  137. static TDBObjClass * Get_First_Object (uint32 category_id);
  138. static TDBObjClass * Get_Next_Object (uint32 category_id, TDBObjClass *curr_obj);
  139. //
  140. // Category support
  141. //
  142. static int Get_Category_Count (void);
  143. static TDBCategoryClass * Get_Category (int index);
  144. static TDBCategoryClass * Find_Category (uint32 id);
  145. static TDBCategoryClass * Find_Category (const char *name);
  146. static TDBCategoryClass * Add_Category (const char *name);
  147. static bool Add_Category (TDBCategoryClass *new_category, bool assign_id = true);
  148. static bool Remove_Category (int index);
  149. //
  150. // Language support
  151. //
  152. static void Set_Current_Language (int lang_id) { m_LanguageID = lang_id; }
  153. static uint32 Get_Current_Language (void) { return m_LanguageID; }
  154. //
  155. // Save/load options
  156. //
  157. static bool Is_Single_Language_Export_Enabled (void) { return IsSingleLanguageExport; }
  158. static void Enable_Single_Language_Export (bool onoff) { IsSingleLanguageExport = onoff; }
  159. static void Set_Export_Filter (FILTER_OPT filter, uint32 category_id);
  160. protected:
  161. //////////////////////////////////////////////////////////////
  162. // Protected methods
  163. //////////////////////////////////////////////////////////////
  164. //
  165. // Save/load stuff
  166. //
  167. virtual bool Contains_Data(void) const;
  168. virtual bool Save (ChunkSaveClass &csave);
  169. virtual bool Load (ChunkLoadClass &cload);
  170. bool Load_Variables (ChunkLoadClass &cload);
  171. bool Load_Objects (ChunkLoadClass &cload);
  172. bool Load_Categories (ChunkLoadClass &cload);
  173. static void Validate_Data (void);
  174. static void Free_Objects (void);
  175. static void Free_Categories (void);
  176. //
  177. // ID managment
  178. //
  179. static uint32 Find_Unique_ID (void);
  180. //////////////////////////////////////////////////////////////
  181. // Protected data types
  182. //////////////////////////////////////////////////////////////
  183. enum
  184. {
  185. ID_MIN = 1000,
  186. ID_MAX = 999999
  187. };
  188. private:
  189. //////////////////////////////////////////////////////////////
  190. // Private member data
  191. //////////////////////////////////////////////////////////////
  192. static TDB_OBJ_LIST m_ObjectList;
  193. static HashTemplateClass<StringClass,TDBObjClass*> m_ObjectHash;
  194. static TDB_CATEGORY_LIST m_CategoryList;
  195. static uint32 m_VersionNumber;
  196. static uint32 m_LanguageID;
  197. static bool IsSingleLanguageExport;
  198. static uint32 CategoryExportFilter;
  199. static FILTER_OPT FilterType;
  200. static uint32 FilterCategoryID;
  201. };
  202. //////////////////////////////////////////////////////////////
  203. // Get_String
  204. //////////////////////////////////////////////////////////////
  205. inline const WCHAR *
  206. TranslateDBClass::Get_String (uint32 id)
  207. {
  208. // ID of 0 (zero) is a special case NULL string.
  209. if (id == 0) {
  210. return NULL;
  211. }
  212. const WCHAR *string = STRING_NOT_FOUND;
  213. //
  214. // Check to make sure the database is loaded
  215. //
  216. WWASSERT (Is_Loaded());
  217. //
  218. // Calculate which index this ID refers to
  219. //
  220. int index = (id - ID_MIN);
  221. //
  222. // Check to see if the requested ID is in the range
  223. // of loaded string objects.
  224. //
  225. WWASSERT (index >= 0 && index < m_ObjectList.Count ());
  226. if (index >= 0 && index < m_ObjectList.Count ()) {
  227. //
  228. // Get the translation object
  229. //
  230. TDBObjClass *trans_obj = m_ObjectList[index];
  231. WWASSERT (trans_obj != NULL);
  232. if (trans_obj != NULL) {
  233. //
  234. // Get the string from the object and return it to the caller
  235. //
  236. string = trans_obj->Get_String ();
  237. }
  238. }
  239. return string;
  240. }
  241. //////////////////////////////////////////////////////////////
  242. // Get_String
  243. //////////////////////////////////////////////////////////////
  244. inline const WCHAR *
  245. TranslateDBClass::Get_String (const char *id_desc)
  246. {
  247. // NULL description is a special case NULL string.
  248. if (id_desc == NULL) {
  249. return NULL;
  250. }
  251. const WCHAR *string = STRING_NOT_FOUND;
  252. //
  253. // Lookup the object based on its ID
  254. //
  255. TDBObjClass *translate_obj = Find_Object (id_desc);
  256. if (translate_obj != NULL) {
  257. //
  258. // Get the string from the object and return it to the caller
  259. //
  260. string = translate_obj->Get_String ();
  261. }
  262. return string;
  263. }
  264. //////////////////////////////////////////////////////////////
  265. // Get_English_String
  266. //////////////////////////////////////////////////////////////
  267. inline const char *
  268. TranslateDBClass::Get_English_String (uint32 id)
  269. {
  270. // ID of 0 (zero) is a special case NULL string.
  271. if (id == 0) {
  272. return NULL;
  273. }
  274. const char *string = ENGLISH_STRING_NOT_FOUND;
  275. //
  276. // Check to make sure the database is loaded
  277. //
  278. WWASSERT (m_ObjectList.Count () > 0);
  279. //
  280. // Calculate which index this ID refers to
  281. //
  282. int index = (id - ID_MIN);
  283. //
  284. // Check to see if the requested ID is in the range
  285. // of loaded string objects.
  286. //
  287. WWASSERT (index >= 0 && index < m_ObjectList.Count ());
  288. if (index >= 0 && index < m_ObjectList.Count ()) {
  289. //
  290. // Get the translation object
  291. //
  292. TDBObjClass *trans_obj = m_ObjectList[index];
  293. WWASSERT (trans_obj != NULL);
  294. if (trans_obj != NULL) {
  295. //
  296. // Get the string from the object and return it to the caller
  297. //
  298. string = trans_obj->Get_English_String ();
  299. }
  300. }
  301. return string;
  302. }
  303. //////////////////////////////////////////////////////////////
  304. //
  305. // Find_Object
  306. //
  307. //////////////////////////////////////////////////////////////
  308. WWINLINE TDBObjClass *
  309. TranslateDBClass::Find_Object (const char *id_desc)
  310. {
  311. StringClass lower_case_name(id_desc,true);
  312. _strlwr(lower_case_name.Peek_Buffer());
  313. return m_ObjectHash.Get(lower_case_name);
  314. }
  315. //////////////////////////////////////////////////////////////
  316. // Find_Object
  317. //////////////////////////////////////////////////////////////
  318. WWINLINE TDBObjClass *
  319. TranslateDBClass::Find_Object (uint32 id)
  320. {
  321. TDBObjClass *object = NULL;
  322. //
  323. // Calculate which index this ID refers to
  324. //
  325. int index = (id - ID_MIN);
  326. if (index >= 0 && index < m_ObjectList.Count ()) {
  327. //
  328. // Get the translation object
  329. //
  330. object = m_ObjectList[index];
  331. //
  332. // Make sure this is the object the caller requested
  333. //
  334. WWASSERT (object != NULL && object->Get_ID () == id);
  335. if (object != NULL && object->Get_ID () != id) {
  336. object = NULL;
  337. }
  338. }
  339. return object;
  340. }
  341. #endif //__TRANSLATE_DB_H