Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
4.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PREFABS_H
  8. #define PREFABS_H
  9. #pragma once
  10. #include <afxtempl.h>
  11. #pragma warning(push, 1)
  12. #pragma warning(disable:4701 4702 4530)
  13. #include <fstream>
  14. #pragma warning(pop)
  15. class BoundBox;
  16. class CMapClass;
  17. class CPrefab;
  18. class CPrefabLibrary;
  19. const POSITION ENUM_START = POSITION(1);
  20. const int MAX_NOTES = 501;
  21. enum
  22. {
  23. pt3D,
  24. };
  25. enum LibraryType_t
  26. {
  27. LibType_None,
  28. LibType_HalfLife,
  29. LibType_HalfLife2,
  30. };
  31. typedef CTypedPtrList<CPtrList, CPrefab*> CPrefabList;
  32. typedef CTypedPtrList<CPtrList, CPrefabLibrary*> CPrefabLibraryList;
  33. class CPrefab
  34. {
  35. public:
  36. CPrefab(void);
  37. virtual ~CPrefab(void);
  38. // load/save flags:
  39. enum
  40. {
  41. lsRMF = 0x00, // default
  42. lsMAP = 0x01,
  43. lsRaw = 0x02,
  44. lsUpdateFilePos = 0x04
  45. };
  46. virtual int Save(LPCTSTR pszFilename, DWORD = 0) = 0;
  47. virtual int Load(DWORD = 0) = 0;
  48. // set info:
  49. void SetName(LPCTSTR pszName)
  50. { strcpy(szName, pszName); }
  51. void SetNotes(LPCTSTR pszNotes)
  52. { strcpy(szNotes, pszNotes); }
  53. // get info:
  54. LPCTSTR GetName() { return szName; }
  55. LPCTSTR GetNotes() { return szNotes; }
  56. // unique id assigned at creation time:
  57. DWORD GetID() { return dwID; }
  58. DWORD GetLibraryID() { return dwLibID; }
  59. // common interface:
  60. virtual CMapClass *CreateInBox(BoundBox *pBox) = 0;
  61. virtual int GetType() = 0;
  62. virtual void FreeData() = 0;
  63. virtual bool IsLoaded() = 0;
  64. // filetype determination:
  65. typedef enum
  66. {
  67. pftUnknown,
  68. pftRMF,
  69. pftMAP,
  70. pftScript
  71. } pfiletype_t;
  72. // static misc stuff:
  73. static pfiletype_t CheckFileType(LPCTSTR pszFilename);
  74. static CPrefab* FindID(DWORD dwID);
  75. // caching:
  76. static void AddMRU(CPrefab *pPrefab);
  77. static void EnableCaching(BOOL = TRUE);
  78. static void FreeAllData(); // free ALL objects' data
  79. protected:
  80. char szName[31];
  81. char szNotes[MAX_NOTES];
  82. DWORD dwID;
  83. DWORD dwLibID; // library id
  84. DWORD dwFileOffset;
  85. DWORD dwFileSize; // size in file - for copying purposes
  86. static CPrefabList PrefabList;
  87. static CPrefabList MRU;
  88. static BOOL bCacheEnabled;
  89. friend class CPrefabLibrary;
  90. friend class CPrefabLibraryRMF;
  91. friend class CPrefabLibraryVMF;
  92. };
  93. //
  94. // A collection of prefabs.
  95. //
  96. class CPrefabLibrary
  97. {
  98. public:
  99. CPrefabLibrary();
  100. ~CPrefabLibrary();
  101. virtual int Load(LPCTSTR pszFilename) = 0;
  102. virtual bool DeleteFile(void) = 0;
  103. virtual int Save(LPCTSTR pszFilename = NULL, BOOL bIndexOnly = FALSE) = 0;
  104. virtual bool IsFile(const char *szFile) = 0;
  105. void SetNameFromFilename(LPCTSTR pszFilename);
  106. virtual int SetName(const char *pszName) = 0;
  107. void SetNotes(LPCTSTR pszNotes)
  108. {
  109. strcpy(szNotes, pszNotes);
  110. }
  111. // get info:
  112. LPCTSTR GetName() { return m_szName; }
  113. LPCTSTR GetNotes() { return szNotes; }
  114. inline bool IsType(LibraryType_t eType);
  115. // unique id assigned at creation time:
  116. DWORD GetID() { return dwID; }
  117. CPrefab * EnumPrefabs(POSITION& p);
  118. void Add(CPrefab *pPrefab);
  119. void Remove(CPrefab *pPrefab);
  120. void Sort();
  121. static CPrefabLibrary *FindID(DWORD dwID);
  122. static CPrefabLibrary *EnumLibraries(POSITION &p, LibraryType_t eType = LibType_None);
  123. static void LoadAllLibraries(void);
  124. static void FreeAllLibraries(void);
  125. static CPrefabLibrary *FindOpenLibrary(LPCTSTR pszFilename);
  126. protected:
  127. void FreePrefabs();
  128. static CPrefabLibraryList PrefabLibraryList;
  129. CPrefabList Prefabs;
  130. char m_szName[31];
  131. char szNotes[MAX_NOTES];
  132. DWORD dwID;
  133. LibraryType_t m_eType; // HalfLife or HalfLife2 library?
  134. friend class CPrefab;
  135. friend class CPrefabRMF;
  136. friend class CPrefabVMF;
  137. };
  138. class CPrefabLibraryRMF : public CPrefabLibrary
  139. {
  140. public:
  141. CPrefabLibraryRMF();
  142. ~CPrefabLibraryRMF();
  143. bool IsFile(const char *szFile);
  144. int Load(LPCTSTR pszFilename);
  145. bool DeleteFile(void);
  146. int Save(LPCTSTR pszFilename = NULL, BOOL bIndexOnly = FALSE);
  147. int SetName(const char *pszName);
  148. std::fstream m_file;
  149. protected:
  150. DWORD m_dwDirOffset; // dir offset in open file
  151. CString m_strOpenFileName; // open file name
  152. friend class CPrefab;
  153. };
  154. class CPrefabLibraryVMF : public CPrefabLibrary
  155. {
  156. public:
  157. CPrefabLibraryVMF();
  158. ~CPrefabLibraryVMF();
  159. bool IsFile(const char *szFile);
  160. int Load(LPCTSTR pszFilename);
  161. bool DeleteFile(void);
  162. int Save(LPCTSTR pszFilename = NULL, BOOL bIndexOnly = FALSE);
  163. int SetName(const char *pszName);
  164. protected:
  165. char m_szFolderName[MAX_PATH];
  166. friend class CPrefab;
  167. };
  168. //-----------------------------------------------------------------------------
  169. // Purpose: Returns whether this library is of a given type. Half-Life used
  170. // .ol files to represent prefab libraries, Half-Life 2 uses a folder
  171. // of VMF files.
  172. //-----------------------------------------------------------------------------
  173. bool CPrefabLibrary::IsType(LibraryType_t eType)
  174. {
  175. return(m_eType == eType);
  176. }
  177. #endif // PREFABS_H