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