Utils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. //
  20. // Utils.H
  21. //
  22. // Module containing usefull misc. utility functions
  23. //
  24. #ifndef __UTILS_H
  25. #define __UTILS_H
  26. #include "refcount.h"
  27. #include "shlobj.h"
  28. #include "listtypes.h"
  29. #include "nodetypes.h"
  30. #include "assetmgr.h"
  31. #include "_assetmgr.h"
  32. #include "editorassetmgr.h"
  33. /////////////////////////////////////////////////////////////////////////////
  34. //
  35. // Macros
  36. //
  37. #define SAFE_DELETE(pobject) \
  38. if (pobject) { \
  39. delete pobject; \
  40. pobject = NULL; \
  41. } \
  42. #define SAFE_DELETE_ARRAY(pobject) \
  43. if (pobject) { \
  44. delete [] pobject; \
  45. pobject = NULL; \
  46. } \
  47. #define SAFE_ADD_REF(pobject) \
  48. if (pobject) { \
  49. pobject->Add_Ref (); \
  50. } \
  51. #define SAFE_RELEASE_REF(pobject) \
  52. if (pobject) { \
  53. pobject->Release_Ref (); \
  54. } \
  55. #define MEMBER_RELEASE(pmember) \
  56. SAFE_RELEASE_REF(pmember); \
  57. pmember = NULL; \
  58. #define MEMBER_ADD(pmember, pnew) \
  59. MEMBER_RELEASE (pmember); \
  60. pmember = pnew; \
  61. SAFE_ADD_REF (pmember); \
  62. #define COM_RELEASE(pobject) \
  63. if (pobject) { \
  64. pobject->Release (); \
  65. } \
  66. pobject = NULL; \
  67. #define SAFE_CLOSE(handle) \
  68. if (handle != INVALID_HANDLE_VALUE) { \
  69. ::CloseHandle (handle); \
  70. handle = INVALID_HANDLE_VALUE; \
  71. } \
  72. #define COPY_CHAR_ARRAY(dest, src) \
  73. ::lstrcpyn (dest, src, sizeof (dest)); \
  74. dest[sizeof (dest)-1] = 0; \
  75. #define SANITY_CHECK(expr) \
  76. ASSERT (expr); \
  77. if (!(expr))
  78. #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
  79. #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
  80. #define INLINE_ACCESSOR_CONST(_type, _class, _name) \
  81. inline _type _class::Get_##_name (void) const \
  82. { return m_##_name; } \
  83. inline void _class::Set_##_name (_type val) \
  84. { m_##_name = val; } \
  85. #define INLINE_ACCESSOR(_type, _class, _name) \
  86. inline _type _class::Get_##_name (void) \
  87. { return m_##_name; } \
  88. inline void _class::Set_##_name (_type val) \
  89. { m_##_name = val; } \
  90. /////////////////////////////////////////////////////////////////////////////
  91. //
  92. // Inlines
  93. //
  94. __inline void Delimit_Path (LPTSTR path)
  95. {
  96. if (path != NULL && path[0] != 0 && path[::lstrlen (path) - 1] != '\\') {
  97. ::lstrcat (path, "\\");
  98. }
  99. return ;
  100. }
  101. __inline void Delimit_Path (CString &path)
  102. {
  103. if (path.GetLength () > 0 && path[::lstrlen (path) - 1] != '\\') {
  104. path += CString ("\\");
  105. }
  106. return ;
  107. }
  108. __inline bool Is_Path (LPCTSTR string)
  109. {
  110. bool retval = (::strchr (string, '\\') != NULL);
  111. retval |= (::strchr (string, '/') != NULL);
  112. return retval;
  113. }
  114. __inline bool Is_W3D_Filename (LPCTSTR filename)
  115. {
  116. CString lower_case = filename;
  117. lower_case.MakeLower ();
  118. return (::strstr (lower_case, ".w3d") != NULL);
  119. }
  120. __inline bool Is_Texture_Filename (LPCTSTR filename)
  121. {
  122. CString lower_case = filename;
  123. lower_case.MakeLower ();
  124. return (::strstr (lower_case, ".tga") != NULL);
  125. }
  126. __inline bool Is_Full_Path (LPCTSTR string)
  127. {
  128. return (string[1] == ':') || (string[0] == '\\' && string[1] == '\\');
  129. }
  130. __inline CString Make_Path (LPCTSTR path, LPCTSTR filename)
  131. {
  132. CString full_path;
  133. if (Is_Full_Path (filename)) {
  134. full_path = filename;
  135. } else {
  136. full_path = path;
  137. Delimit_Path (full_path);
  138. full_path += filename;
  139. }
  140. return full_path;
  141. }
  142. __inline void Create_Dir_If_Necessary (LPCTSTR path)
  143. {
  144. if (::GetFileAttributes (path) == 0xFFFFFFFF) {
  145. ::CreateDirectory (path, NULL);
  146. }
  147. return ;
  148. }
  149. __inline RenderObjClass *Create_Render_Obj (LPCTSTR name)
  150. {
  151. return WW3DAssetManager::Get_Instance()->Create_Render_Obj (name);
  152. }
  153. __inline void Set_Current_Directory (LPCTSTR path)
  154. {
  155. _pThe3DAssetManager->Set_Current_Directory (path);
  156. return ;
  157. }
  158. /////////////////////////////////////////////////////////////////////////////
  159. //
  160. // Forward declarations
  161. //
  162. /////////////////////////////////////////////////////////////////////////////
  163. class OverlapPageClass;
  164. class InstancesPageClass;
  165. class GlobalPresetsFormClass;
  166. class CLevelEditDoc;
  167. class CLevelEditView;
  168. class CameraMgr;
  169. class MouseMgrClass;
  170. class FileMgrClass;
  171. class SceneEditorClass;
  172. class NodeMgrClass;
  173. class SelectionMgrClass;
  174. class GroupsPageClass;
  175. class LightAmbientFormClass;
  176. class PresetsFormClass;
  177. class Matrix3D;
  178. class AABoxClass;
  179. class OBBoxClass;
  180. class PersistClass;
  181. class DefinitionClass;
  182. class ConversationPageClass;
  183. /////////////////////////////////////////////////////////////////////////////
  184. //
  185. // Prototypes
  186. //
  187. CLevelEditDoc * Get_Current_Document (void);
  188. CLevelEditView * Get_Main_View (void);
  189. CameraMgr * Get_Camera_Mgr (void);
  190. MouseMgrClass * Get_Mouse_Mgr (void);
  191. FileMgrClass * Get_File_Mgr (void);
  192. SceneEditorClass * Get_Scene_Editor (void);
  193. NodeMgrClass & Get_Node_Mgr (void);
  194. SelectionMgrClass & Get_Selection_Mgr (void);
  195. void Refresh_Main_View (void);
  196. void General_Pump_Messages (void);
  197. void Pump_Messages (void);
  198. GROUP_LIST & Get_Global_Group_List (void);
  199. CImageList * Get_Global_Image_List (void);
  200. //
  201. // Form/Property page management
  202. //
  203. ConversationPageClass * Get_Conversation_Form (void);
  204. OverlapPageClass * Get_Overlap_Form (void);
  205. InstancesPageClass * Get_Instances_Form (void);
  206. LightAmbientFormClass * Get_Ambient_Light_Form (void);
  207. PresetsFormClass * Get_Presets_Form (void);
  208. //
  209. // Misc routines
  210. //
  211. int Get_Next_Temp_ID (void);
  212. LPCTSTR Get_Factory_Name (uint32 class_id);
  213. //
  214. // 'Safe' creation routines (provides error message, etc)
  215. //
  216. PersistClass * Instance_Definition (DefinitionClass *definition);
  217. //
  218. // Painting/subclassing/dialog control routines
  219. //
  220. LRESULT CALLBACK CheckBoxSubclassProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
  221. void Paint_Gradient (HWND hwnd, BYTE base_red, BYTE base_green, BYTE base_blue);
  222. void Constrain_Point_To_Aspect_Ratio (float &xpos, float &ypos);
  223. void Fill_Node_Instance_Combo (HWND hcombobox, NodeClass *pdefault = NULL);
  224. void Fill_Group_Combo (HWND hcombobox, GroupMgrClass *pdefault = NULL);
  225. int Type_To_Icon (NODE_TYPE type, bool btemp = false);
  226. //
  227. // Vis routines
  228. //
  229. void Perform_Job (LPCTSTR filename, bool delete_on_completion);
  230. //
  231. // Directory routines
  232. //
  233. CString Get_Startup_Directory (void);
  234. CString Get_Data_Directory (void);
  235. //
  236. // String manipulation routines
  237. //
  238. CString Get_Filename_From_Path (LPCTSTR path);
  239. CString Strip_Filename_From_Path (LPCTSTR path);
  240. CString Get_Subdir_From_Full_Path (LPCTSTR path);
  241. CString Get_Subdir_And_Filename_From_Path (LPCTSTR path);
  242. int Build_List_From_String (LPCTSTR buffer, LPCTSTR delimiter, CString **pstring_list);
  243. CString Build_String_From_List (const CString *pstring_list, int count, LPCTSTR delimiter);
  244. CString Asset_Name_From_Filename (LPCTSTR filename);
  245. CString Filename_From_Asset_Name (LPCTSTR asset_name);
  246. CString Up_One_Directory (LPCTSTR path);
  247. void Convert_Chars_To_Newline (CString &string);
  248. void Convert_Newline_To_Chars (CString &string);
  249. //
  250. // Message box/status methods
  251. //
  252. UINT Message_Box (HWND hparentwnd, UINT message_id, UINT title_id, UINT style = MB_ICONEXCLAMATION | MB_OK);
  253. void Update_Frame_Count (int frame, int max_frames);
  254. void Set_Modified (bool modified = true);
  255. bool Is_Silent_Mode (void);
  256. void Set_Silent_Mode (bool is_silent);
  257. //
  258. // Dialog routines
  259. //
  260. void SetDlgItemFloat (HWND hdlg, UINT child_id, float value);
  261. float GetDlgItemFloat (HWND hdlg, UINT child_id, bool interpret = false);
  262. float GetWindowFloat (HWND hwnd, bool interpret = false);
  263. void SetWindowFloat (HWND hwnd, float value);
  264. void Make_Edit_Float_Ctrl (HWND edit_wnd);
  265. void Make_Edit_Int_Ctrl (HWND edit_wnd);
  266. HWND Show_VSS_Update_Dialog (HWND hparent_wnd);
  267. void Kill_VSS_Update_Dialog (HWND hdlg);
  268. //
  269. // File routines
  270. //
  271. DWORD Get_File_Size (LPCTSTR path);
  272. bool Get_File_Time (LPCTSTR path, LPFILETIME pcreation_time = NULL, LPFILETIME paccess_time = NULL, LPFILETIME pwrite_time = NULL);
  273. bool Is_Path_Relative (LPCTSTR path);
  274. bool Find_File (LPCTSTR filename, LPCTSTR start_dir, CString &full_path);
  275. int Quick_Compare_Files (LPCTSTR file1, LPCTSTR file2);
  276. int Get_LOD_File_Count (LPCTSTR first_lod_filename, CString *pbase_filename = NULL);
  277. bool Browse_For_Folder (CString &folder, HWND hparentwnd, LPCTSTR default_path, LPCTSTR title, UINT flags = BIF_RETURNONLYFSDIRS);
  278. bool Copy_File (LPCTSTR existing_filename, LPCTSTR new_filename, bool bforce_copy = false);
  279. //
  280. // Version routines
  281. //
  282. bool Check_Editor_Version (void);
  283. //
  284. // Message/Debug routines
  285. //
  286. void Output_Message (LPCTSTR message);
  287. //
  288. // Math routines
  289. //
  290. void Rotate_Matrix (const Matrix3D &input_mat, const Matrix3D &rotation_mat, const Matrix3D &coord_system, Matrix3D *poutput_mat);
  291. bool Get_Collision_Box (const RenderObjClass *model, AABoxClass &box);
  292. bool Get_Collision_Box (RenderObjClass *render_obj, AABoxClass &aabox, OBBoxClass &obbox);
  293. //
  294. // Thread routines
  295. //
  296. typedef UINT (*MY_THREADPROC) (DWORD dwparam1, DWORD dwparam2, DWORD dwparam3, HRESULT *presult);
  297. typedef UINT (*MY_UITHREADPROC) (DWORD dwparam1, DWORD dwparam2, DWORD dwparam3, HRESULT *presult, HWND *phmain_wnd);
  298. void Create_Worker_Thread (MY_THREADPROC fnthread_proc, DWORD dwparam1, DWORD dwparam2, DWORD dwparam3, HRESULT *presult);
  299. void Create_UI_Thread (MY_UITHREADPROC fnthread_proc, DWORD dwparam1, DWORD dwparam2, DWORD dwparam3, HRESULT *presult, HWND *phmain_wnd);
  300. /////////////////////////////////////////////////////////////////////////////
  301. //
  302. // Helper classes
  303. //
  304. class FileAccessRightsClass
  305. {
  306. public:
  307. typedef enum
  308. {
  309. WANTS_READONLY = 1,
  310. WANTS_WRITEABLE
  311. } ACCESS_TYPE;
  312. FileAccessRightsClass (LPCTSTR filename, ACCESS_TYPE type = WANTS_WRITEABLE, bool should_restore = false);
  313. ~FileAccessRightsClass (void);
  314. private:
  315. bool m_bNeedsRestoring;
  316. LONG m_FileAttrs;
  317. CString m_Filename;
  318. };
  319. #endif //__UTILS_H