source: abuse/trunk/src/cache.cpp @ 503

Last change on this file since 503 was 503, checked in by Sam Hocevar, 11 years ago

cache: fix a crash caused by empty SFX objects being reused for other
purposes but still being deleted later on.

File size: 27.0 KB
Line 
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 "cache.h"
21#include "lisp.h"
22#include "video.h"
23#include "dprint.h"
24#include "exitproc.h"
25#include "lcache.h"
26#include "status.h"
27#include "game.h"
28#include "lisp_gc.h"
29#include "level.h"
30#include "status.h"
31#include "crc.h"
32#include "dev.h"
33#include "specache.h"
34#include "netface.h"
35
36char lfname[100]="";          // name of compiled lisp code cache file
37
38#define touch(x) { (x)->last_access=last_access++; \
39           if ((x)->last_access<0) { normalize(); (x)->last_access=1; } }
40
41CrcManager crc_manager;
42
43int past_startup=0;
44
45int crc_man_write_crc_file(char const *filename)
46{
47  return crc_manager.write_crc_file(filename);
48}
49
50int 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
103int 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
127void 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
137CrcedFile::~CrcedFile()
138{
139  free(filename);
140}
141
142CrcedFile::CrcedFile(char const *name)
143{
144  filename = strdup(name);
145  crc_calculated=0;
146}
147
148CrcManager::CrcManager()
149{
150  total_files=0;
151  files=NULL;
152}
153
154int 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
164char *CrcManager::get_filename(int filenumber)
165{
166  CHECK(filenumber>=0 && filenumber<total_files);
167  return files[filenumber]->filename;
168}
169
170uint32_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
182void 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
189void 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
211void 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
220static int c_sorter(const void *a, const void *b)
221{
222  return cache.compare(*(int *)a,*(int *)b);
223}
224
225int 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
235int 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
253void 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
295void CacheList::prof_uninit()
296{
297  if (prof_data)
298  {
299    free(prof_data);
300    prof_data=NULL;
301  }
302}
303
304int *sorted_id_list;
305
306
307static int s_offset_compare(const void *a, const void *b)
308{
309  return cache.offset_compare(*(int *)a,*(int *)b);
310}
311
312int 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
326int 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
348static 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
364void 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
372void 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
427void 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
474void 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
639void CacheList::prof_poll_start()
640{
641  poll_start_access=last_access;
642}
643
644void 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
657void CacheList::unreg(int id)
658{
659  if (list[id].file_number)
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
668static void cache_cleanup2()
669{ unlink(lfname);
670}
671
672static void cache_cleanup(int ret, void *arg)
673{ unlink(lfname);
674}
675
676void CacheList::create_lcache()
677{
678#ifdef WIN32
679    char *prefix="c:\\";
680#else
681    char const *prefix = "/tmp/";     // for UNIX store lisp cache in tmp dir
682    int flags = O_CREAT | O_RDWR;
683#endif
684
685    int cfail = 1, num = 0;
686    do
687    {
688        sprintf(lfname,"%slcache%02d.tmp",prefix,num);
689
690#if defined( __APPLE__ )
691        unlink(lfname);
692        FILE *fp=fopen(lfname,"wb");
693        if (fp)
694        {
695            fclose(fp);
696            cfail=0;
697        }
698#else
699        int fd=open(lfname,flags,S_IRWXU | S_IRWXG | S_IRWXO);     // can we get exclusive rights to this file?
700        if (fd<0) close(fd); else cfail=0;
701#endif
702
703        if (cfail)
704            num++;
705
706    } while (cfail && num<15);
707
708    if (cfail)
709    {
710        fprintf(stderr,"Error : Unable to open cache file for compiled code.\n"
711            "        Please delete all files named lcacheXX.tmp\n"
712            "        and make sure you have write permission to\n"
713            "        directory (%s)\n",prefix);
714        exit(0);
715    }
716    else
717    {
718        exit_proc(cache_cleanup,cache_cleanup2);    // make sure this file gets deleted on exit..
719    }
720    lcache_number=-1;
721}
722
723CacheList::CacheList()
724{
725  // start out with a decent sized cache buffer because it's going to get allocated anyway.
726  total=0;
727  list=NULL;
728  last_registered=-1;
729  cache_file=fp=NULL;
730  last_access=1;
731  used=ful=0;
732  last_dir=NULL;
733  last_file=-1;
734  prof_data=NULL;
735  cache_read_file=NULL;
736  create_lcache();
737}
738
739CacheList::~CacheList()
740{
741}
742
743void CacheList::empty()
744{
745  for (int i=0; i<total; i++)
746  {
747    if (list[i].file_number>=0 && list[i].last_access!=-1)
748      unmalloc(&list[i]);
749  }
750  free(list);
751  if (fp) delete fp;
752  if (last_dir) delete last_dir;
753  if (cache_file)
754  {
755    delete cache_file;
756    cache_file=NULL;
757  }
758  unlink(lfname);
759
760  if (prof_data)
761  {
762    delete prof_data;
763    prof_data=NULL;
764  }
765
766  total=0;                    // reinitalize
767  list=NULL;
768  last_registered=-1;
769  cache_file=fp=NULL;
770  if (cache_read_file)
771  {
772    delete cache_read_file;
773    cache_read_file=NULL;
774  }
775
776  last_access=1;
777  used=ful=0;
778  last_dir=NULL;
779  last_file=-1;
780  prof_data=NULL;
781}
782
783void CacheList::locate(CacheItem *i, int local_only)
784{
785//  dprintf("cache in %s, type %d, offset %d\n",crc_manager.get_filename(i->file_number),i->type,i->offset);
786  if (i->file_number!=last_file)
787  {
788    if (fp) delete fp;
789    if (last_dir) delete last_dir;
790    if (local_only)
791      fp=new jFILE(crc_manager.get_filename(i->file_number),"rb");
792    else
793      fp=open_file(crc_manager.get_filename(i->file_number),"rb");
794
795
796    if (fp->open_failure())
797    {
798      printf("Ooch. Could not open file %s\n",crc_manager.get_filename(i->file_number));
799      delete fp;
800      exit(0);
801    }
802
803    last_offset=-1;
804    last_dir=new spec_directory(fp);
805    last_file=i->file_number;
806  }
807  if (i->offset!=last_offset)
808  {
809    fp->seek(i->offset,SEEK_SET);
810    last_offset=i->offset;
811  }
812  used=1;
813}
814
815int CacheList::AllocId()
816{
817    if (prof_data)
818    {
819        the_game->show_help("new id allocated, cache profiling turned off\n");
820        prof_uninit();
821    }
822
823    // See if we previously allocated an id, if so check the next spot in
824    // the array otherwise we will have to scan the whole list for a free
825    // id and possibly grow the list.
826    int ret = last_registered + 1;
827    if (ret >= total || list[ret].file_number < 0)
828    {
829        // Scan list for a free id
830        CacheItem *ci = list;
831        ret = -1;
832        for (int i = 0; i < total && ret < 0; i++, ci++)
833            if (ci->file_number < 0)
834                ret = i;
835
836        if (ret < 0) // if no free id then make list bigger
837        {
838            int add_size = 20;
839            list = (CacheItem *)realloc(list,
840                                  (sizeof(CacheItem) * (total + add_size)));
841            for (int i = 0; i < add_size; i++)
842            {
843                list[total + i].file_number = -1; // mark new entries as new
844                list[total + i].last_access = -1;
845                list[total + i].data = NULL;
846            }
847            ret = total;
848            // If new id's have been added, old prof_data size won't work
849            if (prof_data)
850            {
851                free(prof_data);
852                prof_data = NULL;
853            }
854            total += add_size;
855        }
856    }
857    last_registered = ret;
858    return ret;
859}
860
861int CacheList::reg_object(char const *filename, LObject *object,
862                          int type, int rm_dups)
863{
864    // See if we got a object with a filename included. Otherwise,
865    // it's a string.
866    if (item_type(object) == L_CONS_CELL)
867    {
868        filename = lstring_value(lcar(object));
869        object = lcdr(object);
870    }
871
872    return reg(filename, lstring_value(object), type, rm_dups);
873}
874
875extern int total_files_open;
876
877int CacheList::reg(char const *filename, char const *name, int type, int rm_dups)
878{
879    int fn = crc_manager.get_filenumber(filename);
880    int offset = 0;
881
882    if (type == SPEC_EXTERN_SFX)
883    {
884        // If an extern sound effect then just make sure it's there. If sound
885        // is disabled, ignore the load error, just pretend it's all OK.
886        bFILE *check = open_file(filename, "rb");
887        if (!check->open_failure())
888        {
889            char buf[4];
890            check->read(buf, 4);
891            if (memcmp(buf, "RIFF", 4))
892            {
893                printf("File %s is not a WAV file\n", filename);
894                exit(0);
895            }
896        }
897        else if (sound_avail)
898        {
899            printf("Unable to open file '%s' for reading\n", filename);
900            exit(0);
901        }
902        delete check;
903    }
904    else
905    {
906        // If a classic spec item, look for it in the archive.
907        spec_directory *sd = sd_cache.get_spec_directory(filename);
908
909        if (!sd)
910        {
911            printf("Unable to open file %s for item %s\n", filename, name);
912            exit(0);
913        }
914
915        spec_entry *se = NULL;
916        if (type != -1)
917            se = sd->find(name, type);
918        if (!se)
919            se = sd->find(name);
920        if (!se)
921        {
922            printf("No such item %s in file %s\n", name, filename);
923            exit(0);
924        }
925
926        if (type >= 0 && type != se->type &&
927             ((type != SPEC_CHARACTER2 && type != SPEC_CHARACTER)
928               || (se->type != SPEC_CHARACTER && se->type != SPEC_CHARACTER2)))
929        {
930            printf("Item %s of file %s should be type %s\n",
931                   name, filename, spec_types[type]);
932            exit(0);
933        }
934
935        type = se->type;
936        offset = se->offset;
937    }
938
939    // Check whether there is another entry pointing to the same
940    // file and offset, and return it as a shortcut.
941    if (rm_dups)
942    {
943        for (int i = 0; i < total; i++)
944            if (list[i].file_number == fn && list[i].offset == offset)
945                return i;
946    }
947
948    int id = AllocId();
949
950    CHECK(id < total && list[id].file_number < 0);
951
952    list[id].file_number = fn;
953    list[id].last_access = -1;
954    list[id].data = NULL;
955    list[id].offset = offset;
956    list[id].type = type;
957
958    return id;
959}
960
961
962void CacheList::normalize()
963{
964  int j;
965  CacheItem *ci=list;
966  last_access=-1;
967  for (j=0; j<total; j++,ci++)
968  {
969    if (ci->last_access>=0)
970      ci->last_access=ci->last_access>>16;        // shift everything over by 16
971    if (ci->last_access>last_access)            //  and find new largest timestamp
972      last_access=ci->last_access;
973  }
974  last_access++;
975}
976
977backtile *CacheList::backt(int id)
978{
979  CacheItem *me=list+id;
980  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
981
982  if (me->last_access>=0)
983  {
984    touch(me);
985    return (backtile *)me->data;
986  }
987  else
988  {
989    touch(me);
990    locate(me);
991    me->data=(void *)new backtile(fp);
992    last_offset=fp->tell();
993    return (backtile *)me->data;
994  }
995}
996
997
998foretile *CacheList::foret(int id)
999{
1000  CacheItem *me=list+id;
1001  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1002
1003  if (me->last_access>=0)
1004  {
1005    touch(me);
1006    return (foretile *)me->data;
1007  }
1008  else
1009  {
1010    touch(me);
1011    locate(me);
1012    me->data=(void *)new foretile(fp);
1013    last_offset=fp->tell();
1014    return (foretile *)me->data;
1015  }
1016}
1017
1018figure *CacheList::fig(int id)
1019{
1020  CacheItem *me=list+id;
1021//  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1022  if (me->last_access>=0)
1023  {
1024    touch(me);
1025    return (figure *)me->data;
1026  }
1027  else
1028  {
1029    touch(me);
1030    locate(me);
1031    me->data=(void *)new figure(fp,me->type);
1032     last_offset=fp->tell();
1033    return (figure *)me->data;
1034  }
1035}
1036
1037image *CacheList::img(int id)
1038{
1039  CacheItem *me=list+id;
1040  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1041  if (me->last_access>=0)
1042  {
1043    touch(me);
1044    return (image *)me->data;
1045  }
1046  else
1047  {
1048    touch(me);                                           // hold me, feel me, be me!
1049    locate(me);
1050    image *im=new image(fp);
1051    me->data=(void *)im;
1052    last_offset=fp->tell();
1053
1054    return (image *)me->data;
1055  }
1056}
1057
1058sound_effect *CacheList::sfx(int id)
1059{
1060  CacheItem *me=list+id;
1061  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1062  if (me->last_access>=0)
1063  {
1064    touch(me);                                           // hold me, feel me, be me!
1065    return (sound_effect *)me->data;
1066  }
1067  else
1068  {
1069    touch(me);                                           // hold me, feel me, be me!
1070    char *fn=crc_manager.get_filename(me->file_number);
1071    me->data=(void *)new sound_effect(fn);
1072    return (sound_effect *)me->data;
1073  }
1074}
1075
1076
1077part_frame *CacheList::part(int id)
1078{
1079  CacheItem *me=list+id;
1080  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1081  if (me->last_access>=0)
1082  {
1083    touch(me);                                           // hold me, feel me, be me!
1084    return (part_frame *)me->data;
1085  }
1086  else
1087  {
1088    touch(me);
1089    locate(me);
1090    me->data=(void *)new part_frame(fp);
1091    last_offset=fp->tell();
1092    return (part_frame *)me->data;
1093  }
1094}
1095
1096
1097LObject *CacheList::lblock(int id)
1098{
1099  CacheItem *me=list+id;
1100  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1101  return (LObject *)me->data;
1102}
1103
1104CacheList cache;
1105
1106void CacheList::free_oldest()
1107{
1108  int32_t old_time = last_access;
1109  CacheItem *ci=list,*oldest=NULL;
1110  ful=1;
1111
1112  for (int i = 0; i < total; i++, ci++)
1113  {
1114    if (ci->data && ci->last_access < old_time)
1115    {
1116      oldest = ci;
1117      old_time = ci->last_access;
1118    }
1119  }
1120  if (oldest)
1121  {
1122    dprintf("mem_maker : freeing %s\n",spec_types[oldest->type]);
1123    unmalloc(oldest);
1124  }
1125  else
1126  {
1127    close_graphics();
1128    printf("Out of memory, please remove any TSR's device drivers you can\n");
1129    exit(0);
1130  }
1131}
1132
1133
1134void CacheList::show_accessed()
1135{
1136  int old=last_access,new_old_accessed;
1137  CacheItem *ci,*new_old;
1138
1139  do
1140  {
1141    new_old_accessed=-1;
1142    new_old=NULL;
1143    ci=list;
1144    for (int i=0; i<total; i++,ci++)
1145    {
1146      if (ci->last_access<old && ci->last_access>0 && ci->last_access>new_old_accessed)
1147      {
1148    new_old_accessed=ci->last_access;
1149        new_old=ci;
1150      }
1151    }
1152    if (new_old)
1153    {
1154      ci=new_old;
1155      old=ci->last_access;
1156      printf("type=(%20s) file=(%20s) access=(%6ld)\n",spec_types[ci->type],
1157         crc_manager.get_filename(ci->file_number),
1158         (long int)ci->last_access);
1159    }
1160  } while (new_old);
1161}
1162
1163int CacheList::loaded(int id)
1164{
1165  CacheItem *me=list+id;
1166  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1167  if (me->last_access>=0)
1168    return 1;
1169  else return 0;
1170}
1171
1172char_tint *CacheList::ctint(int id)
1173{
1174  CacheItem *me=list+id;
1175  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id" && me->type==SPEC_PALETTE);
1176  if (me->last_access>=0)
1177  {
1178    touch(me);
1179    return (char_tint *)me->data;
1180  }
1181  else
1182  {
1183    touch(me);
1184    locate(me);
1185    me->data=(void *)new char_tint(fp);
1186    last_offset=fp->tell();
1187    return (char_tint *)me->data;
1188  }
1189}
1190
Note: See TracBrowser for help on using the repository browser.