data.cpp 14 KB

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