1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com or |
---|
8 | * Jonathan Clark. |
---|
9 | */ |
---|
10 | |
---|
11 | #include "config.h" |
---|
12 | |
---|
13 | #if (defined(__MACH__) || !defined(__APPLE__)) |
---|
14 | # include <sys/stat.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | #include <fcntl.h> |
---|
18 | #include <string.h> |
---|
19 | |
---|
20 | #include "common.h" |
---|
21 | |
---|
22 | #include "cache.h" |
---|
23 | #include "lisp.h" |
---|
24 | #include "video.h" |
---|
25 | #include "dprint.h" |
---|
26 | #include "exitproc.h" |
---|
27 | #include "lcache.h" |
---|
28 | #include "status.h" |
---|
29 | #include "game.h" |
---|
30 | #include "lisp_gc.h" |
---|
31 | #include "level.h" |
---|
32 | #include "status.h" |
---|
33 | #include "crc.h" |
---|
34 | #include "dev.h" |
---|
35 | #include "specache.h" |
---|
36 | #include "netface.h" |
---|
37 | |
---|
38 | #define touch(x) { (x)->last_access=last_access++; \ |
---|
39 | if ((x)->last_access<0) { normalize(); (x)->last_access=1; } } |
---|
40 | |
---|
41 | CrcManager crc_manager; |
---|
42 | |
---|
43 | int past_startup=0; |
---|
44 | |
---|
45 | int crc_man_write_crc_file(char const *filename) |
---|
46 | { |
---|
47 | return crc_manager.write_crc_file(filename); |
---|
48 | } |
---|
49 | |
---|
50 | int CrcManager::write_crc_file(char const *filename) // return 0 on failure |
---|
51 | { |
---|
52 | char msg[100]; |
---|
53 | sprintf(msg, "%s", symbol_str("calc_crc")); // this may take some time, show the user a status indicator |
---|
54 | if (stat_man) stat_man->push(msg,NULL); |
---|
55 | |
---|
56 | int i,total=0; |
---|
57 | for (i=0; i<total_files; i++) |
---|
58 | { |
---|
59 | int failed=0; |
---|
60 | get_crc(i,failed); |
---|
61 | |
---|
62 | if (failed) |
---|
63 | { |
---|
64 | jFILE *fp=new jFILE(get_filename(i),"rb"); |
---|
65 | if (!fp->open_failure()) |
---|
66 | { |
---|
67 | set_crc(i,crc_file(fp)); |
---|
68 | total++; |
---|
69 | } |
---|
70 | delete fp; |
---|
71 | } else total++; |
---|
72 | if (stat_man) |
---|
73 | stat_man->update(i*100/total_files); |
---|
74 | } |
---|
75 | if (stat_man) stat_man->pop(); |
---|
76 | jFILE *fp=new jFILE(NET_CRC_FILENAME,"wb"); |
---|
77 | if (fp->open_failure()) |
---|
78 | { |
---|
79 | delete fp; |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | fp->write_uint16(total); |
---|
84 | total=0; |
---|
85 | for (i=0; i<total_files; i++) |
---|
86 | { |
---|
87 | uint32_t crc; |
---|
88 | int failed=0; |
---|
89 | crc=get_crc(i,failed); |
---|
90 | if (!failed) |
---|
91 | { |
---|
92 | fp->write_uint32(crc); |
---|
93 | uint8_t len=strlen(get_filename(i))+1; |
---|
94 | fp->write_uint8(len); |
---|
95 | fp->write(get_filename(i),len); |
---|
96 | total++; |
---|
97 | } |
---|
98 | } |
---|
99 | delete fp; |
---|
100 | return 1; |
---|
101 | } |
---|
102 | |
---|
103 | int CrcManager::load_crc_file(char const *filename) |
---|
104 | { |
---|
105 | bFILE *fp=open_file(filename,"rb"); |
---|
106 | if (fp->open_failure()) |
---|
107 | { |
---|
108 | delete fp; |
---|
109 | return 0; |
---|
110 | } else |
---|
111 | { |
---|
112 | short total=fp->read_uint16(); |
---|
113 | int i; |
---|
114 | for (i=0; i<total; i++) |
---|
115 | { |
---|
116 | char name[256]; |
---|
117 | uint32_t crc=fp->read_uint32(); |
---|
118 | uint8_t len=fp->read_uint8(); |
---|
119 | fp->read(name,len); |
---|
120 | set_crc(get_filenumber(name),crc); |
---|
121 | } |
---|
122 | delete fp; |
---|
123 | } |
---|
124 | return 1; |
---|
125 | } |
---|
126 | |
---|
127 | void CrcManager::clean_up() |
---|
128 | { |
---|
129 | for (int i=0; i<total_files; i++) |
---|
130 | delete files[i]; |
---|
131 | if (total_files) |
---|
132 | free(files); |
---|
133 | total_files=0; |
---|
134 | files=NULL; |
---|
135 | } |
---|
136 | |
---|
137 | CrcedFile::~CrcedFile() |
---|
138 | { |
---|
139 | free(filename); |
---|
140 | } |
---|
141 | |
---|
142 | CrcedFile::CrcedFile(char const *name) |
---|
143 | { |
---|
144 | filename = strdup(name); |
---|
145 | crc_calculated=0; |
---|
146 | } |
---|
147 | |
---|
148 | CrcManager::CrcManager() |
---|
149 | { |
---|
150 | total_files=0; |
---|
151 | files=NULL; |
---|
152 | } |
---|
153 | |
---|
154 | int CrcManager::get_filenumber(char const *filename) |
---|
155 | { |
---|
156 | for (int i=0; i<total_files; i++) |
---|
157 | if (!strcmp(filename,files[i]->filename)) return i; |
---|
158 | total_files++; |
---|
159 | files=(CrcedFile **)realloc(files,total_files*sizeof(CrcedFile *)); |
---|
160 | files[total_files-1]=new CrcedFile(filename); |
---|
161 | return total_files-1; |
---|
162 | } |
---|
163 | |
---|
164 | char *CrcManager::get_filename(int filenumber) |
---|
165 | { |
---|
166 | CHECK(filenumber>=0 && filenumber<total_files); |
---|
167 | return files[filenumber]->filename; |
---|
168 | } |
---|
169 | |
---|
170 | uint32_t CrcManager::get_crc(int filenumber, int &failed) |
---|
171 | { |
---|
172 | CHECK(filenumber>=0 && filenumber<total_files); |
---|
173 | if (files[filenumber]->crc_calculated) |
---|
174 | { |
---|
175 | failed=0; |
---|
176 | return files[filenumber]->crc; |
---|
177 | } |
---|
178 | failed=1; |
---|
179 | return 0; |
---|
180 | } |
---|
181 | |
---|
182 | void CrcManager::set_crc(int filenumber, uint32_t crc) |
---|
183 | { |
---|
184 | CHECK(filenumber>=0 && filenumber<total_files); |
---|
185 | files[filenumber]->crc_calculated=1; |
---|
186 | files[filenumber]->crc=crc; |
---|
187 | } |
---|
188 | |
---|
189 | void CacheList::unmalloc(CacheItem *i) |
---|
190 | { |
---|
191 | switch (i->type) |
---|
192 | { |
---|
193 | case SPEC_CHARACTER2 : |
---|
194 | case SPEC_CHARACTER : delete ((figure *)i->data); break; |
---|
195 | case SPEC_FORETILE : delete ((foretile *)i->data); break; |
---|
196 | case SPEC_BACKTILE : delete ((backtile *)i->data); break; |
---|
197 | case SPEC_IMAGE : delete ((image *)i->data); break; |
---|
198 | case SPEC_EXTERN_SFX : delete ((sound_effect *)i->data); break; |
---|
199 | case SPEC_PARTICLE : delete ((part_frame *)i->data); break; |
---|
200 | case SPEC_EXTERNAL_LCACHE : if (i->data) free(i->data); break; |
---|
201 | case SPEC_PALETTE : delete ((char_tint *)i->data); break; |
---|
202 | default : |
---|
203 | printf("Trying to unmalloc unknown type\n"); |
---|
204 | } |
---|
205 | i->data=NULL; |
---|
206 | i->last_access=-1; |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | void CacheList::prof_init() |
---|
212 | { |
---|
213 | if (prof_data) |
---|
214 | free(prof_data); |
---|
215 | |
---|
216 | prof_data=(int *)malloc(sizeof(int)*total); |
---|
217 | memset(prof_data,0,sizeof(int)*total); |
---|
218 | } |
---|
219 | |
---|
220 | static int c_sorter(const void *a, const void *b) |
---|
221 | { |
---|
222 | return cache.compare(*(int *)a,*(int *)b); |
---|
223 | } |
---|
224 | |
---|
225 | int CacheList::compare(int a, int b) |
---|
226 | { |
---|
227 | if (prof_data[a]<prof_data[b]) |
---|
228 | return 1; |
---|
229 | else if (prof_data[a]>prof_data[b]) |
---|
230 | return -1; |
---|
231 | else return 0; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | int CacheList::prof_size() |
---|
236 | { |
---|
237 | int size=0; // count up the size for a spec enrty |
---|
238 | size+=2; // total filenames |
---|
239 | int i; |
---|
240 | for (i=0; i<crc_manager.total_filenames(); i++) |
---|
241 | size+=strlen(crc_manager.get_filename(i))+2; // filename + 0 + size of string |
---|
242 | |
---|
243 | size+=4; // number of entries saved |
---|
244 | |
---|
245 | for (i=0; i<total; i++) |
---|
246 | if (list[i].last_access>0) // don't save unaccessed counts |
---|
247 | size+=2+4+1; // filenumber & offset & type |
---|
248 | |
---|
249 | return size; |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | void CacheList::prof_write(bFILE *fp) |
---|
254 | { |
---|
255 | if (prof_data) |
---|
256 | { |
---|
257 | int *ordered_ids=(int *)malloc(sizeof(int)*total); |
---|
258 | int i; |
---|
259 | for (i=0; i<total; i++) ordered_ids[i]=i; |
---|
260 | qsort(ordered_ids,total,sizeof(int),c_sorter); |
---|
261 | |
---|
262 | if (fp) |
---|
263 | { |
---|
264 | fp->write_uint16(crc_manager.total_filenames()); |
---|
265 | for (i=0; i<crc_manager.total_filenames(); i++) |
---|
266 | { |
---|
267 | int l=strlen(crc_manager.get_filename(i))+1; |
---|
268 | fp->write_uint8(l); |
---|
269 | fp->write(crc_manager.get_filename(i),l); |
---|
270 | } |
---|
271 | |
---|
272 | int tsaved=0; |
---|
273 | for (i=0; i<total; i++) |
---|
274 | if (list[i].last_access>0) tsaved++; |
---|
275 | fp->write_uint32(tsaved); |
---|
276 | |
---|
277 | for (i=0; i<total; i++) |
---|
278 | { |
---|
279 | int id=ordered_ids[i]; |
---|
280 | if (list[id].last_access>0) // don't save unaccessed counts |
---|
281 | { |
---|
282 | fp->write_uint8(list[id].type); // save type, if type changed on reload |
---|
283 | // don't cache in-> its a different refrence |
---|
284 | fp->write_uint16(list[id].file_number); |
---|
285 | fp->write_uint32(list[id].offset); |
---|
286 | } |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | free(ordered_ids); |
---|
291 | |
---|
292 | } else dprintf("Cache profiling was not initialized\n"); |
---|
293 | } |
---|
294 | |
---|
295 | void CacheList::prof_uninit() |
---|
296 | { |
---|
297 | if (prof_data) |
---|
298 | { |
---|
299 | free(prof_data); |
---|
300 | prof_data=NULL; |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | int *sorted_id_list; |
---|
305 | |
---|
306 | |
---|
307 | static int s_offset_compare(const void *a, const void *b) |
---|
308 | { |
---|
309 | return cache.offset_compare(*(int *)a,*(int *)b); |
---|
310 | } |
---|
311 | |
---|
312 | int CacheList::offset_compare(int a, int b) |
---|
313 | { |
---|
314 | if (list[a].offset<list[b].offset) |
---|
315 | return -1; |
---|
316 | else if (list[a].offset>list[b].offset) |
---|
317 | return 1; |
---|
318 | else if (list[a].file_number<list[b].file_number) |
---|
319 | return -1; |
---|
320 | else if (list[a].file_number>list[b].file_number) |
---|
321 | return 1; |
---|
322 | else return 0; |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | int CacheList::search(int *sarray, uint16_t filenum, int32_t offset) |
---|
327 | { |
---|
328 | int x1=0,x2=total-1; |
---|
329 | int split; |
---|
330 | do |
---|
331 | { |
---|
332 | split=(x1+x2)/2; |
---|
333 | CacheItem *e=list+sarray[split]; |
---|
334 | |
---|
335 | if (e->offset<offset) // search to the right |
---|
336 | x1=split+1; |
---|
337 | else if (e->offset>offset) |
---|
338 | x2=split-1; |
---|
339 | else if (e->file_number<filenum) |
---|
340 | x1=split+1; |
---|
341 | else if (e->file_number>filenum) |
---|
342 | x2=split-1; |
---|
343 | else return sarray[split]; |
---|
344 | } while (x1<=x2); |
---|
345 | return -1; |
---|
346 | } |
---|
347 | |
---|
348 | static int load_chars() // returns 0 if cache filled |
---|
349 | { |
---|
350 | int i; |
---|
351 | for (i=0; i<total_objects; i++) |
---|
352 | { |
---|
353 | if (figures[i]->get_cflag(CFLAG_NEED_CACHE_IN)) |
---|
354 | { |
---|
355 | figures[i]->set_cflag(CFLAG_CACHED_IN,0); |
---|
356 | figures[i]->cache_in(); |
---|
357 | figures[i]->set_cflag(CFLAG_NEED_CACHE_IN,0); |
---|
358 | } |
---|
359 | } |
---|
360 | return 1; |
---|
361 | |
---|
362 | } |
---|
363 | |
---|
364 | void CacheList::note_need(int id) |
---|
365 | { |
---|
366 | if (list[id].last_access<0) |
---|
367 | list[id].last_access=-2; |
---|
368 | else |
---|
369 | list[id].last_access=2; |
---|
370 | } |
---|
371 | |
---|
372 | void CacheList::preload_cache_object(int type) |
---|
373 | { |
---|
374 | if (type<0xffff) |
---|
375 | { |
---|
376 | if (!figures[type]->get_cflag(CFLAG_NEED_CACHE_IN)) // see if it's already marked |
---|
377 | { |
---|
378 | figures[type]->set_cflag(CFLAG_NEED_CACHE_IN,1); |
---|
379 | void *cache_fun=figures[type]->get_fun(OFUN_GET_CACHE_LIST); |
---|
380 | |
---|
381 | if (cache_fun) |
---|
382 | { |
---|
383 | int sp=current_space; |
---|
384 | current_space=PERM_SPACE; |
---|
385 | |
---|
386 | void *call_with=NULL; |
---|
387 | push_onto_list(LNumber::Create(type),call_with); |
---|
388 | |
---|
389 | void *CacheList = ((LSymbol *)cache_fun)->EvalFunction(call_with); |
---|
390 | PtrRef r1(CacheList); |
---|
391 | |
---|
392 | if (CacheList && lcar(CacheList)) |
---|
393 | { |
---|
394 | void *obj_list=lcar(CacheList); |
---|
395 | while (obj_list) |
---|
396 | { |
---|
397 | int t=lnumber_value(CAR(obj_list)); |
---|
398 | if (t<0 || t>=total_objects) |
---|
399 | lbreak("Get cache list returned a bad object number %d\n",t); |
---|
400 | else |
---|
401 | preload_cache_object(t); |
---|
402 | obj_list=CDR(obj_list); |
---|
403 | } |
---|
404 | } |
---|
405 | if (CacheList && lcdr(CacheList)) |
---|
406 | { |
---|
407 | void *id_list=lcar(lcdr(CacheList)); |
---|
408 | while (id_list) |
---|
409 | { |
---|
410 | int id=lnumber_value(CAR(id_list)); |
---|
411 | if (id<0 || id>=total) |
---|
412 | lbreak("Get cache list returned a bad id number %d\n",id); |
---|
413 | else if (list[id].last_access<0) |
---|
414 | list[id].last_access=-2; |
---|
415 | else list[id].last_access=2; |
---|
416 | |
---|
417 | id_list=CDR(id_list); |
---|
418 | } |
---|
419 | } |
---|
420 | current_space=sp; |
---|
421 | |
---|
422 | } |
---|
423 | } |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | void CacheList::preload_cache(level *lev) |
---|
428 | { |
---|
429 | game_object *f; |
---|
430 | int i; |
---|
431 | for (i=0; i<total_objects; i++) // mark all types as not needing loading |
---|
432 | figures[i]->set_cflag(CFLAG_NEED_CACHE_IN,0); |
---|
433 | |
---|
434 | for (f=lev->first_object(); f; f=f->next) // go through each object and get requested items to cache in |
---|
435 | preload_cache_object(f->otype); |
---|
436 | |
---|
437 | |
---|
438 | int j; |
---|
439 | uint16_t *fg_line; |
---|
440 | for (j=0; j<lev->foreground_height(); j++) |
---|
441 | { |
---|
442 | fg_line=lev->get_fgline(j); |
---|
443 | for (i=0; i<lev->foreground_width(); i++,fg_line++) |
---|
444 | { |
---|
445 | int id=foretiles[fgvalue(*fg_line)]; |
---|
446 | if (id>=0 && id<nforetiles) |
---|
447 | { |
---|
448 | if (list[id].last_access<0) |
---|
449 | list[id].last_access=-2; |
---|
450 | else list[id].last_access=2; |
---|
451 | } |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | uint16_t *bg_line; |
---|
456 | for (j=0; j<lev->background_height(); j++) |
---|
457 | { |
---|
458 | bg_line=lev->get_bgline(j); |
---|
459 | for (i=0; i<lev->background_width(); i++,bg_line++) |
---|
460 | { |
---|
461 | int id=backtiles[bgvalue(*bg_line)]; |
---|
462 | if (id>=0 && id<nbacktiles) |
---|
463 | { |
---|
464 | if (list[id].last_access<0) |
---|
465 | list[id].last_access=-2; |
---|
466 | else list[id].last_access=2; |
---|
467 | } |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | load_chars(); |
---|
472 | } |
---|
473 | |
---|
474 | void CacheList::load_cache_prof_info(char *filename, level *lev) |
---|
475 | { |
---|
476 | int j; |
---|
477 | for (j=0; j<this->total; j++) |
---|
478 | if (list[j].last_access>=0) // reset all loaded cache items to 0, all non-load to -1 |
---|
479 | list[j].last_access=0; |
---|
480 | |
---|
481 | preload_cache(lev); // preliminary guesses at stuff to load |
---|
482 | |
---|
483 | int load_fail=1; |
---|
484 | bFILE *fp=open_file(filename,"rb"); |
---|
485 | if (!fp->open_failure()) |
---|
486 | { |
---|
487 | spec_directory sd(fp); |
---|
488 | spec_entry *se=sd.find("cache profile info"); // see if the cache profile info is in the file |
---|
489 | if (se) |
---|
490 | { |
---|
491 | fp->seek(se->offset,0); |
---|
492 | |
---|
493 | char name[255]; |
---|
494 | int tnames=0; |
---|
495 | int *fnum_remap; // remaps old filenumbers into current ones |
---|
496 | |
---|
497 | tnames=fp->read_uint16(); |
---|
498 | if (tnames) /// make sure there isn't bad info in the file |
---|
499 | { |
---|
500 | fnum_remap=(int *)malloc(sizeof(int)*tnames); |
---|
501 | |
---|
502 | int i; |
---|
503 | for (i=0; i<tnames; i++) |
---|
504 | { |
---|
505 | fp->read(name,fp->read_uint8()); |
---|
506 | fnum_remap[i]=-1; // initialize the map to no-map |
---|
507 | |
---|
508 | int j; |
---|
509 | for (j=0; j<crc_manager.total_filenames(); j++) |
---|
510 | if (!strcmp(crc_manager.get_filename(j),name)) |
---|
511 | fnum_remap[i]=j; |
---|
512 | } |
---|
513 | |
---|
514 | int tsaved = fp->read_uint32(); |
---|
515 | |
---|
516 | |
---|
517 | int *priority=(int *)malloc(tsaved*sizeof(int)); |
---|
518 | memset(priority,0xff,tsaved*sizeof(int)); // initialize to -1 |
---|
519 | int tmatches=0; |
---|
520 | |
---|
521 | sorted_id_list=(int *)malloc(sizeof(int)*total); |
---|
522 | for (j=0; j<total; j++) sorted_id_list[j]=j; |
---|
523 | qsort(sorted_id_list,total,sizeof(int),s_offset_compare); |
---|
524 | |
---|
525 | for (i=0; i<tsaved; i++) |
---|
526 | { |
---|
527 | fp->read_uint8(); // read type |
---|
528 | short file_num=fp->read_uint16(); |
---|
529 | if (file_num>=tnames) // bad data? |
---|
530 | file_num=-1; |
---|
531 | else file_num=fnum_remap[file_num]; |
---|
532 | |
---|
533 | uint32_t offset=fp->read_uint32(); |
---|
534 | |
---|
535 | // search for a match |
---|
536 | j=search(sorted_id_list,file_num,offset); |
---|
537 | if (j!=-1) |
---|
538 | { |
---|
539 | if (list[j].last_access<0) // if not loaded |
---|
540 | list[j].last_access=-2; // mark as needing loading |
---|
541 | else list[j].last_access=2; // mark as loaded and needing to stay that way |
---|
542 | priority[i]=j; |
---|
543 | tmatches++; |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | free(sorted_id_list); // was used for searching, no longer needed |
---|
548 | |
---|
549 | for (j=0; j<total; j++) |
---|
550 | if (list[j].last_access==0) |
---|
551 | unmalloc(list+j); // free any cache entries that are not accessed at all in the level |
---|
552 | |
---|
553 | |
---|
554 | ful=0; |
---|
555 | int tcached=0; |
---|
556 | for (j=0; j<total; j++) // now load all of the objects until full |
---|
557 | { |
---|
558 | // stat_man->update(j*70/total+25); |
---|
559 | if (list[j].file_number>=0 && list[j].last_access==-2) |
---|
560 | { |
---|
561 | list[j].last_access=-1; |
---|
562 | if (!ful) |
---|
563 | { |
---|
564 | switch (list[j].type) |
---|
565 | { |
---|
566 | case SPEC_BACKTILE : backt(j); break; |
---|
567 | case SPEC_FORETILE : foret(j); break; |
---|
568 | case SPEC_CHARACTER : |
---|
569 | case SPEC_CHARACTER2 : fig(j); break; |
---|
570 | case SPEC_IMAGE : img(j); break; |
---|
571 | case SPEC_PARTICLE : part(j); break; |
---|
572 | case SPEC_EXTERN_SFX : sfx(j); break; |
---|
573 | case SPEC_EXTERNAL_LCACHE : lblock(j); break; |
---|
574 | case SPEC_PALETTE : ctint(j); break; |
---|
575 | } |
---|
576 | tcached++; |
---|
577 | } |
---|
578 | } |
---|
579 | } |
---|
580 | load_fail=0; |
---|
581 | // if (full()) |
---|
582 | // dprintf("Cache filled while loading\n"); |
---|
583 | |
---|
584 | if (tsaved>tmatches) |
---|
585 | tmatches=tsaved+1; |
---|
586 | |
---|
587 | last_access=tmatches+1; |
---|
588 | for (i=0; i<tsaved; i++) // reorder the last access of each cache to reflect prioirties |
---|
589 | { |
---|
590 | if (priority[i]!=-1) |
---|
591 | { |
---|
592 | if (list[priority[i]].last_access!=-1) // make sure this wasn't the last item |
---|
593 | list[priority[i]].last_access=tmatches--; |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | free(priority); |
---|
598 | free(fnum_remap); |
---|
599 | |
---|
600 | |
---|
601 | } |
---|
602 | } |
---|
603 | } |
---|
604 | |
---|
605 | if (load_fail) // no cache file, go solely on above gueses |
---|
606 | { |
---|
607 | int j; |
---|
608 | for (j=0; j<total; j++) // now load all of the objects until full, don't free old stuff |
---|
609 | { |
---|
610 | // stat_man->update(j*70/total+25); |
---|
611 | |
---|
612 | if (list[j].file_number>=0 && list[j].last_access==-2) |
---|
613 | { |
---|
614 | list[j].last_access=-1; |
---|
615 | if (!ful) |
---|
616 | { |
---|
617 | switch (list[j].type) |
---|
618 | { |
---|
619 | case SPEC_BACKTILE : backt(j); break; |
---|
620 | case SPEC_FORETILE : foret(j); break; |
---|
621 | case SPEC_CHARACTER : |
---|
622 | case SPEC_CHARACTER2 : fig(j); break; |
---|
623 | case SPEC_IMAGE : img(j); break; |
---|
624 | case SPEC_PARTICLE : part(j); break; |
---|
625 | case SPEC_EXTERN_SFX : sfx(j); break; |
---|
626 | case SPEC_EXTERNAL_LCACHE : lblock(j); break; |
---|
627 | case SPEC_PALETTE : ctint(j); break; |
---|
628 | } |
---|
629 | } |
---|
630 | } |
---|
631 | } |
---|
632 | if (full()) |
---|
633 | dprintf("Cache filled while loading\n"); |
---|
634 | } |
---|
635 | delete fp; |
---|
636 | } |
---|
637 | |
---|
638 | |
---|
639 | void CacheList::prof_poll_start() |
---|
640 | { |
---|
641 | poll_start_access=last_access; |
---|
642 | } |
---|
643 | |
---|
644 | void CacheList::prof_poll_end() |
---|
645 | { |
---|
646 | if (prof_data) |
---|
647 | { |
---|
648 | int i=0; |
---|
649 | for (; i<total; i++) |
---|
650 | { |
---|
651 | if (list[i].last_access>=poll_start_access) |
---|
652 | prof_data[i]++; |
---|
653 | } |
---|
654 | } |
---|
655 | } |
---|
656 | |
---|
657 | void CacheList::unreg(int id) |
---|
658 | { |
---|
659 | if (list[id].file_number >= 0) |
---|
660 | { |
---|
661 | unmalloc(&list[id]); |
---|
662 | list[id].file_number = -1; |
---|
663 | } |
---|
664 | else |
---|
665 | printf("Error : trying to unregister free object\n"); |
---|
666 | } |
---|
667 | |
---|
668 | CacheList::CacheList() |
---|
669 | { |
---|
670 | // Start out with a decent sized cache buffer because it's going to |
---|
671 | // get allocated anyway. |
---|
672 | total = 0; |
---|
673 | list = NULL; |
---|
674 | last_registered = -1; |
---|
675 | fp = NULL; |
---|
676 | last_access = 1; |
---|
677 | used = ful = 0; |
---|
678 | last_dir = NULL; |
---|
679 | last_file = -1; |
---|
680 | prof_data = NULL; |
---|
681 | } |
---|
682 | |
---|
683 | CacheList::~CacheList() |
---|
684 | { |
---|
685 | } |
---|
686 | |
---|
687 | void CacheList::empty() |
---|
688 | { |
---|
689 | for (int i=0; i<total; i++) |
---|
690 | { |
---|
691 | if (list[i].file_number>=0 && list[i].last_access!=-1) |
---|
692 | unmalloc(&list[i]); |
---|
693 | } |
---|
694 | free(list); |
---|
695 | if (fp) delete fp; |
---|
696 | if (last_dir) delete last_dir; |
---|
697 | |
---|
698 | if (prof_data) |
---|
699 | { |
---|
700 | delete prof_data; |
---|
701 | prof_data=NULL; |
---|
702 | } |
---|
703 | |
---|
704 | total=0; // reinitalize |
---|
705 | list=NULL; |
---|
706 | last_registered=-1; |
---|
707 | fp=NULL; |
---|
708 | |
---|
709 | last_access=1; |
---|
710 | used=ful=0; |
---|
711 | last_dir=NULL; |
---|
712 | last_file=-1; |
---|
713 | prof_data=NULL; |
---|
714 | } |
---|
715 | |
---|
716 | void CacheList::locate(CacheItem *i, int local_only) |
---|
717 | { |
---|
718 | // dprintf("cache in %s, type %d, offset %d\n",crc_manager.get_filename(i->file_number),i->type,i->offset); |
---|
719 | if (i->file_number!=last_file) |
---|
720 | { |
---|
721 | if (fp) delete fp; |
---|
722 | if (last_dir) delete last_dir; |
---|
723 | if (local_only) |
---|
724 | fp=new jFILE(crc_manager.get_filename(i->file_number),"rb"); |
---|
725 | else |
---|
726 | fp=open_file(crc_manager.get_filename(i->file_number),"rb"); |
---|
727 | |
---|
728 | |
---|
729 | if (fp->open_failure()) |
---|
730 | { |
---|
731 | printf("Ooch. Could not open file %s\n",crc_manager.get_filename(i->file_number)); |
---|
732 | delete fp; |
---|
733 | exit(0); |
---|
734 | } |
---|
735 | |
---|
736 | last_offset=-1; |
---|
737 | last_dir=new spec_directory(fp); |
---|
738 | last_file=i->file_number; |
---|
739 | } |
---|
740 | if (i->offset!=last_offset) |
---|
741 | { |
---|
742 | fp->seek(i->offset,SEEK_SET); |
---|
743 | last_offset=i->offset; |
---|
744 | } |
---|
745 | used=1; |
---|
746 | } |
---|
747 | |
---|
748 | int CacheList::AllocId() |
---|
749 | { |
---|
750 | if (prof_data) |
---|
751 | { |
---|
752 | the_game->show_help("new id allocated, cache profiling turned off\n"); |
---|
753 | prof_uninit(); |
---|
754 | } |
---|
755 | |
---|
756 | // See if we previously allocated an id, if so check the next spot in |
---|
757 | // the array otherwise we will have to scan the whole list for a free |
---|
758 | // id and possibly grow the list. |
---|
759 | int ret = last_registered + 1; |
---|
760 | if (ret >= total || list[ret].file_number < 0) |
---|
761 | { |
---|
762 | // Scan list for a free id |
---|
763 | CacheItem *ci = list; |
---|
764 | ret = -1; |
---|
765 | for (int i = 0; i < total && ret < 0; i++, ci++) |
---|
766 | if (ci->file_number < 0) |
---|
767 | ret = i; |
---|
768 | |
---|
769 | if (ret < 0) // if no free id then make list bigger |
---|
770 | { |
---|
771 | int add_size = 20; |
---|
772 | list = (CacheItem *)realloc(list, |
---|
773 | (sizeof(CacheItem) * (total + add_size))); |
---|
774 | for (int i = 0; i < add_size; i++) |
---|
775 | { |
---|
776 | list[total + i].file_number = -1; // mark new entries as new |
---|
777 | list[total + i].last_access = -1; |
---|
778 | list[total + i].data = NULL; |
---|
779 | } |
---|
780 | ret = total; |
---|
781 | // If new id's have been added, old prof_data size won't work |
---|
782 | if (prof_data) |
---|
783 | { |
---|
784 | free(prof_data); |
---|
785 | prof_data = NULL; |
---|
786 | } |
---|
787 | total += add_size; |
---|
788 | } |
---|
789 | } |
---|
790 | last_registered = ret; |
---|
791 | return ret; |
---|
792 | } |
---|
793 | |
---|
794 | int CacheList::reg_object(char const *filename, LObject *object, |
---|
795 | int type, int rm_dups) |
---|
796 | { |
---|
797 | // See if we got a object with a filename included. Otherwise, |
---|
798 | // it's a string. |
---|
799 | if (item_type(object) == L_CONS_CELL) |
---|
800 | { |
---|
801 | filename = lstring_value(lcar(object)); |
---|
802 | object = lcdr(object); |
---|
803 | } |
---|
804 | |
---|
805 | return reg(filename, lstring_value(object), type, rm_dups); |
---|
806 | } |
---|
807 | |
---|
808 | extern int total_files_open; |
---|
809 | |
---|
810 | int CacheList::reg(char const *filename, char const *name, int type, int rm_dups) |
---|
811 | { |
---|
812 | int fn = crc_manager.get_filenumber(filename); |
---|
813 | int offset = 0; |
---|
814 | |
---|
815 | if (type == SPEC_EXTERN_SFX) |
---|
816 | { |
---|
817 | // If an extern sound effect then just make sure it's there. If sound |
---|
818 | // is disabled, ignore the load error, just pretend it's all OK. |
---|
819 | bFILE *check = open_file(filename, "rb"); |
---|
820 | if (!check->open_failure()) |
---|
821 | { |
---|
822 | char buf[4]; |
---|
823 | check->read(buf, 4); |
---|
824 | if (memcmp(buf, "RIFF", 4)) |
---|
825 | { |
---|
826 | printf("File %s is not a WAV file\n", filename); |
---|
827 | exit(0); |
---|
828 | } |
---|
829 | } |
---|
830 | else if (sound_avail) |
---|
831 | { |
---|
832 | printf("Unable to open file '%s' for reading\n", filename); |
---|
833 | exit(0); |
---|
834 | } |
---|
835 | delete check; |
---|
836 | } |
---|
837 | else |
---|
838 | { |
---|
839 | // If a classic spec item, look for it in the archive. |
---|
840 | spec_directory *sd = sd_cache.get_spec_directory(filename); |
---|
841 | |
---|
842 | if (!sd) |
---|
843 | { |
---|
844 | printf("Unable to open file %s for item %s\n", filename, name); |
---|
845 | exit(0); |
---|
846 | } |
---|
847 | |
---|
848 | spec_entry *se = NULL; |
---|
849 | if (type != -1) |
---|
850 | se = sd->find(name, type); |
---|
851 | if (!se) |
---|
852 | se = sd->find(name); |
---|
853 | if (!se) |
---|
854 | { |
---|
855 | printf("No such item %s in file %s\n", name, filename); |
---|
856 | exit(0); |
---|
857 | } |
---|
858 | |
---|
859 | if (type >= 0 && type != se->type && |
---|
860 | ((type != SPEC_CHARACTER2 && type != SPEC_CHARACTER) |
---|
861 | || (se->type != SPEC_CHARACTER && se->type != SPEC_CHARACTER2))) |
---|
862 | { |
---|
863 | printf("Item %s of file %s should be type %s\n", |
---|
864 | name, filename, spec_types[type]); |
---|
865 | exit(0); |
---|
866 | } |
---|
867 | |
---|
868 | type = se->type; |
---|
869 | offset = se->offset; |
---|
870 | } |
---|
871 | |
---|
872 | // Check whether there is another entry pointing to the same |
---|
873 | // file and offset, and return it as a shortcut. |
---|
874 | if (rm_dups) |
---|
875 | { |
---|
876 | for (int i = 0; i < total; i++) |
---|
877 | if (list[i].file_number == fn && list[i].offset == offset) |
---|
878 | return i; |
---|
879 | } |
---|
880 | |
---|
881 | int id = AllocId(); |
---|
882 | |
---|
883 | CHECK(id < total && list[id].file_number < 0); |
---|
884 | |
---|
885 | list[id].file_number = fn; |
---|
886 | list[id].last_access = -1; |
---|
887 | list[id].data = NULL; |
---|
888 | list[id].offset = offset; |
---|
889 | list[id].type = type; |
---|
890 | |
---|
891 | return id; |
---|
892 | } |
---|
893 | |
---|
894 | |
---|
895 | void CacheList::normalize() |
---|
896 | { |
---|
897 | int j; |
---|
898 | CacheItem *ci=list; |
---|
899 | last_access=-1; |
---|
900 | for (j=0; j<total; j++,ci++) |
---|
901 | { |
---|
902 | if (ci->last_access>=0) |
---|
903 | ci->last_access=ci->last_access>>16; // shift everything over by 16 |
---|
904 | if (ci->last_access>last_access) // and find new largest timestamp |
---|
905 | last_access=ci->last_access; |
---|
906 | } |
---|
907 | last_access++; |
---|
908 | } |
---|
909 | |
---|
910 | backtile *CacheList::backt(int id) |
---|
911 | { |
---|
912 | CacheItem *me=list+id; |
---|
913 | CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id"); |
---|
914 | |
---|
915 | if (me->last_access>=0) |
---|
916 | { |
---|
917 | touch(me); |
---|
918 | return (backtile *)me->data; |
---|
919 | } |
---|
920 | else |
---|
921 | { |
---|
922 | touch(me); |
---|
923 | locate(me); |
---|
924 | me->data=(void *)new backtile(fp); |
---|
925 | last_offset=fp->tell(); |
---|
926 | return (backtile *)me->data; |
---|
927 | } |
---|
928 | } |
---|
929 | |
---|
930 | |
---|
931 | foretile *CacheList::foret(int id) |
---|
932 | { |
---|
933 | CacheItem *me=list+id; |
---|
934 | CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id"); |
---|
935 | |
---|
936 | if (me->last_access>=0) |
---|
937 | { |
---|
938 | touch(me); |
---|
939 | return (foretile *)me->data; |
---|
940 | } |
---|
941 | else |
---|
942 | { |
---|
943 | touch(me); |
---|
944 | locate(me); |
---|
945 | me->data=(void *)new foretile(fp); |
---|
946 | last_offset=fp->tell(); |
---|
947 | return (foretile *)me->data; |
---|
948 | } |
---|
949 | } |
---|
950 | |
---|
951 | figure *CacheList::fig(int id) |
---|
952 | { |
---|
953 | CacheItem *me=list+id; |
---|
954 | // CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id"); |
---|
955 | if (me->last_access>=0) |
---|
956 | { |
---|
957 | touch(me); |
---|
958 | return (figure *)me->data; |
---|
959 | } |
---|
960 | else |
---|
961 | { |
---|
962 | touch(me); |
---|
963 | locate(me); |
---|
964 | me->data=(void *)new figure(fp,me->type); |
---|
965 | last_offset=fp->tell(); |
---|
966 | return (figure *)me->data; |
---|
967 | } |
---|
968 | } |
---|
969 | |
---|
970 | image *CacheList::img(int id) |
---|
971 | { |
---|
972 | CacheItem *me=list+id; |
---|
973 | CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id"); |
---|
974 | if (me->last_access>=0) |
---|
975 | { |
---|
976 | touch(me); |
---|
977 | return (image *)me->data; |
---|
978 | } |
---|
979 | else |
---|
980 | { |
---|
981 | touch(me); // hold me, feel me, be me! |
---|
982 | locate(me); |
---|
983 | image *im=new image(fp); |
---|
984 | me->data=(void *)im; |
---|
985 | last_offset=fp->tell(); |
---|
986 | |
---|
987 | return (image *)me->data; |
---|
988 | } |
---|
989 | } |
---|
990 | |
---|
991 | sound_effect *CacheList::sfx(int id) |
---|
992 | { |
---|
993 | CacheItem *me=list+id; |
---|
994 | CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id"); |
---|
995 | if (me->last_access>=0) |
---|
996 | { |
---|
997 | touch(me); // hold me, feel me, be me! |
---|
998 | return (sound_effect *)me->data; |
---|
999 | } |
---|
1000 | else |
---|
1001 | { |
---|
1002 | touch(me); // hold me, feel me, be me! |
---|
1003 | char *fn=crc_manager.get_filename(me->file_number); |
---|
1004 | me->data=(void *)new sound_effect(fn); |
---|
1005 | return (sound_effect *)me->data; |
---|
1006 | } |
---|
1007 | } |
---|
1008 | |
---|
1009 | |
---|
1010 | part_frame *CacheList::part(int id) |
---|
1011 | { |
---|
1012 | CacheItem *me=list+id; |
---|
1013 | CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id"); |
---|
1014 | if (me->last_access>=0) |
---|
1015 | { |
---|
1016 | touch(me); // hold me, feel me, be me! |
---|
1017 | return (part_frame *)me->data; |
---|
1018 | } |
---|
1019 | else |
---|
1020 | { |
---|
1021 | touch(me); |
---|
1022 | locate(me); |
---|
1023 | me->data=(void *)new part_frame(fp); |
---|
1024 | last_offset=fp->tell(); |
---|
1025 | return (part_frame *)me->data; |
---|
1026 | } |
---|
1027 | } |
---|
1028 | |
---|
1029 | |
---|
1030 | LObject *CacheList::lblock(int id) |
---|
1031 | { |
---|
1032 | CacheItem *me=list+id; |
---|
1033 | CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id"); |
---|
1034 | return (LObject *)me->data; |
---|
1035 | } |
---|
1036 | |
---|
1037 | CacheList cache; |
---|
1038 | |
---|
1039 | void CacheList::free_oldest() |
---|
1040 | { |
---|
1041 | int32_t old_time = last_access; |
---|
1042 | CacheItem *ci=list,*oldest=NULL; |
---|
1043 | ful=1; |
---|
1044 | |
---|
1045 | for (int i = 0; i < total; i++, ci++) |
---|
1046 | { |
---|
1047 | if (ci->data && ci->last_access < old_time) |
---|
1048 | { |
---|
1049 | oldest = ci; |
---|
1050 | old_time = ci->last_access; |
---|
1051 | } |
---|
1052 | } |
---|
1053 | if (oldest) |
---|
1054 | { |
---|
1055 | dprintf("mem_maker : freeing %s\n",spec_types[oldest->type]); |
---|
1056 | unmalloc(oldest); |
---|
1057 | } |
---|
1058 | else |
---|
1059 | { |
---|
1060 | close_graphics(); |
---|
1061 | printf("Out of memory, please remove any TSR's device drivers you can\n"); |
---|
1062 | exit(0); |
---|
1063 | } |
---|
1064 | } |
---|
1065 | |
---|
1066 | |
---|
1067 | void CacheList::show_accessed() |
---|
1068 | { |
---|
1069 | int old=last_access,new_old_accessed; |
---|
1070 | CacheItem *ci,*new_old; |
---|
1071 | |
---|
1072 | do |
---|
1073 | { |
---|
1074 | new_old_accessed=-1; |
---|
1075 | new_old=NULL; |
---|
1076 | ci=list; |
---|
1077 | for (int i=0; i<total; i++,ci++) |
---|
1078 | { |
---|
1079 | if (ci->last_access<old && ci->last_access>0 && ci->last_access>new_old_accessed) |
---|
1080 | { |
---|
1081 | new_old_accessed=ci->last_access; |
---|
1082 | new_old=ci; |
---|
1083 | } |
---|
1084 | } |
---|
1085 | if (new_old) |
---|
1086 | { |
---|
1087 | ci=new_old; |
---|
1088 | old=ci->last_access; |
---|
1089 | printf("type=(%20s) file=(%20s) access=(%6ld)\n",spec_types[ci->type], |
---|
1090 | crc_manager.get_filename(ci->file_number), |
---|
1091 | (long int)ci->last_access); |
---|
1092 | } |
---|
1093 | } while (new_old); |
---|
1094 | } |
---|
1095 | |
---|
1096 | int CacheList::loaded(int id) |
---|
1097 | { |
---|
1098 | CacheItem *me=list+id; |
---|
1099 | CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id"); |
---|
1100 | if (me->last_access>=0) |
---|
1101 | return 1; |
---|
1102 | else return 0; |
---|
1103 | } |
---|
1104 | |
---|
1105 | char_tint *CacheList::ctint(int id) |
---|
1106 | { |
---|
1107 | CacheItem *me=list+id; |
---|
1108 | CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id" && me->type==SPEC_PALETTE); |
---|
1109 | if (me->last_access>=0) |
---|
1110 | { |
---|
1111 | touch(me); |
---|
1112 | return (char_tint *)me->data; |
---|
1113 | } |
---|
1114 | else |
---|
1115 | { |
---|
1116 | touch(me); |
---|
1117 | locate(me); |
---|
1118 | me->data=(void *)new char_tint(fp); |
---|
1119 | last_offset=fp->tell(); |
---|
1120 | return (char_tint *)me->data; |
---|
1121 | } |
---|
1122 | } |
---|
1123 | |
---|