data.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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 : Command & Conquer *
  23. * *
  24. * $Archive:: /G/wwlib/data.cpp $*
  25. * *
  26. * $Author:: Neal_k $*
  27. * *
  28. * $Modtime:: 9/24/99 4:52p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Load_Alloc_Data -- Allocates a buffer and loads the file into it. *
  35. * Load_Uncompress -- Loads and uncompresses data to a buffer. *
  36. * Hires_Load -- Allocates memory for, and loads, a resolution dependant file. *
  37. * Fetch_String -- Fetches a string resource. *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "always.h"
  40. #include "data.h"
  41. /***********************************************************************************************
  42. * Load_Alloc_Data -- Allocates a buffer and loads the file into it. *
  43. * *
  44. * This is the C++ replacement for the Load_Alloc_Data function. It will allocate the *
  45. * memory big enough to hold the file and then read the file into it. *
  46. * *
  47. * INPUT: file -- The file to read. *
  48. * *
  49. * mem -- The memory system to use for allocation. *
  50. * *
  51. * OUTPUT: Returns with a pointer to the allocated and filled memory block. *
  52. * *
  53. * WARNINGS: none *
  54. * *
  55. * HISTORY: *
  56. * 10/17/1994 JLB : Created. *
  57. *=============================================================================================*/
  58. void * Load_Alloc_Data(FileClass & file)
  59. {
  60. void * ptr = NULL;
  61. if (file.Is_Available()) {
  62. long size = file.Size();
  63. ptr = W3DNEWARRAY char[size];
  64. if (ptr != NULL) {
  65. file.Read(ptr, size);
  66. }
  67. }
  68. return(ptr);
  69. }
  70. /***********************************************************************************************
  71. * Load_Uncompress -- Loads and uncompresses data to a buffer. *
  72. * *
  73. * This is the C++ counterpart to the Load_Uncompress function. It will load the file *
  74. * specified into the graphic buffer indicated and uncompress it. *
  75. * *
  76. * INPUT: file -- The file to load and uncompress. *
  77. * *
  78. * uncomp_buff -- The graphic buffer that initial loading will use. *
  79. * *
  80. * dest_buff -- The buffer that will hold the uncompressed data. *
  81. * *
  82. * reserved_data -- This is an optional pointer to a buffer that will hold any *
  83. * reserved data the compressed file may contain. This is *
  84. * typically a palette. *
  85. * *
  86. * OUTPUT: Returns with the size of the uncompressed data in the destination buffer. *
  87. * *
  88. * WARNINGS: none *
  89. * *
  90. * HISTORY: *
  91. * 10/17/1994 JLB : Created. *
  92. *=============================================================================================*/
  93. long Load_Uncompress(FileClass & file, Buffer & uncomp_buff, Buffer & dest_buff, void * reserved_data)
  94. {
  95. unsigned short size;
  96. void * sptr = uncomp_buff.Get_Buffer();
  97. void * dptr = dest_buff.Get_Buffer();
  98. int opened = false;
  99. CompHeaderType header;
  100. /*
  101. ** The file must be opened in order to be read from. If the file
  102. ** isn't opened, then open it. Record this fact so that it can be
  103. ** restored to its closed state at the end.
  104. */
  105. if (!file.Is_Open()) {
  106. if (!file.Open()) {
  107. return(0);
  108. }
  109. opened = true;
  110. }
  111. /*
  112. ** Read in the size of the file (supposedly).
  113. */
  114. file.Read(&size, sizeof(size));
  115. /*
  116. ** Read in the header block. This block contains the compression type
  117. ** and skip data (among other things).
  118. */
  119. file.Read(&header, sizeof(header));
  120. size -= (unsigned short)sizeof(header);
  121. /*
  122. ** If there are skip bytes then they must be processed. Either read
  123. ** them into the buffer provided or skip past them. No check is made
  124. ** to ensure that the reserved data buffer is big enough (watch out!).
  125. */
  126. if (header.Skip) {
  127. size -= header.Skip;
  128. if (reserved_data) {
  129. file.Read(reserved_data, header.Skip);
  130. } else {
  131. file.Seek(header.Skip, SEEK_CUR);
  132. }
  133. header.Skip = 0;
  134. }
  135. /*
  136. ** Determine where is the proper place to load the data. If both buffers
  137. ** specified are identical, then the data should be loaded at the end of
  138. ** the buffer and decompressed at the beginning.
  139. */
  140. if (uncomp_buff.Get_Buffer() == dest_buff.Get_Buffer()) {
  141. sptr = (char *)sptr + uncomp_buff.Get_Size()-(size+sizeof(header));
  142. }
  143. /*
  144. ** Read in the bulk of the data.
  145. */
  146. memmove(sptr, &header, sizeof(header));
  147. // Mem_Copy(&header, sptr, sizeof(header));
  148. file.Read((char *)sptr + sizeof(header), size);
  149. /*
  150. ** Decompress the data.
  151. */
  152. size = (unsigned short) Uncompress_Data(sptr, dptr);
  153. /*
  154. ** Close the file if necessary.
  155. */
  156. if (opened) {
  157. file.Close();
  158. }
  159. return((long)size);
  160. }
  161. typedef struct SRecord {
  162. int ID; // ID number of the string resource.
  163. int TimeStamp; // 'Time' that this string was last requested.
  164. char String[2048]; // Copy of string resource.
  165. SRecord(void) : ID(-1), TimeStamp(-1) {}
  166. } SRecord;
  167. /***********************************************************************************************
  168. * Fetch_String -- Fetches a string resource. *
  169. * *
  170. * Fetches a string resource and returns a pointer to its text. *
  171. * *
  172. * INPUT: id -- The ID number of the string resource to fetch. *
  173. * *
  174. * OUTPUT: Returns with a pointer to the actual text of the string resource. *
  175. * *
  176. * WARNINGS: none *
  177. * *
  178. * HISTORY: *
  179. * 12/25/1996 JLB : Created. *
  180. *=============================================================================================*/
  181. char const * Fetch_String(int id)
  182. {
  183. #ifdef _UNIX
  184. return("");
  185. #else
  186. static SRecord _buffers[64];
  187. static int _time = 0;
  188. /*
  189. ** Determine if the string ID requested is valid. If not then return an empty string pointer.
  190. */
  191. if (id == -1 || id == TXT_NONE) return("");
  192. /*
  193. ** Adjust the 'time stamp' tracking value. This is an artificial value used merely to track
  194. ** the relative age of the strings requested.
  195. */
  196. _time = _time+1;
  197. /*
  198. ** Check to see if the requested string has already been fetched into a buffer. If so, then
  199. ** return a pointer to that string (update the time stamp as well).
  200. */
  201. for (int index = 0; index < ARRAY_SIZE(_buffers); index++) {
  202. if (_buffers[index].ID == id) {
  203. _buffers[index].TimeStamp = _time;
  204. return(_buffers[index].String);
  205. }
  206. }
  207. /*
  208. ** Find a suitable buffer to hold the string to be fetched. The buffer should either be
  209. ** empty or have the oldest fetched string.
  210. */
  211. int oldest = -1;
  212. int oldtime = -1;
  213. for (int text = 0; text < ARRAY_SIZE(_buffers); text++) {
  214. if (oldest == -1 || oldtime > _buffers[text].TimeStamp) {
  215. oldest = text;
  216. oldtime = _buffers[text].TimeStamp;
  217. if (oldtime == -1 || _buffers[text].ID == -1) break;
  218. }
  219. }
  220. /*
  221. ** A suitable buffer has been found so fetch the string resource and then return a pointer
  222. ** to the string.
  223. */
  224. char * stringptr = _buffers[oldest].String;
  225. _buffers[oldest].ID = id;
  226. _buffers[oldest].TimeStamp = _time;
  227. if (LoadString(ProgramInstance, id, stringptr, sizeof(_buffers[oldest].String)) == 0) {
  228. return("");
  229. }
  230. stringptr[sizeof(_buffers[oldest].String)-1] = '\0';
  231. return(stringptr);
  232. #endif
  233. }
  234. void const * Fetch_Resource(LPCSTR resname, LPCSTR restype)
  235. {
  236. #ifdef _UNIX
  237. return(NULL);
  238. #else
  239. /*
  240. ** Fetch the program instance if it hasn't already been recorded.
  241. */
  242. // if (ProgramInstance == 0) {
  243. // ProgramInstance = GetModuleHandle("LANGUAGE");
  244. // }
  245. HRSRC handle = FindResource(ProgramInstance, resname, restype);
  246. if (handle == NULL) {
  247. return(NULL);
  248. }
  249. HGLOBAL rhandle = LoadResource(ProgramInstance, handle);
  250. if (rhandle == NULL) {
  251. return(NULL);
  252. }
  253. return(LockResource(rhandle));
  254. #endif
  255. }
  256. int Load_Picture(FileClass & file, Buffer & scratchbuf, Buffer & destbuf, unsigned char * palette, PicturePlaneType )
  257. {
  258. return(Load_Uncompress(file, scratchbuf, destbuf, palette ) / 8000);
  259. }
  260. /***********************************************************************************************
  261. * Hires_Load -- Allocates memory for, and loads, a resolution dependant file. *
  262. * *
  263. * *
  264. * *
  265. * INPUT: Name of file to load *
  266. * *
  267. * OUTPUT: Ptr to loaded file *
  268. * *
  269. * WARNINGS: Caller is responsible for releasing the memory allocated *
  270. * *
  271. * *
  272. * HISTORY: *
  273. * 5/13/96 3:20PM ST : Created *
  274. *=============================================================================================*/
  275. void * Hires_Load(FileClass & file)
  276. {
  277. int length;
  278. void * return_ptr;
  279. if (file.Is_Available()) {
  280. length = file.Size();
  281. return_ptr = W3DNEWARRAY char[length];
  282. file.Read(return_ptr, length);
  283. return (return_ptr);
  284. } else {
  285. return (NULL);
  286. }
  287. }