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

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

cache: get rid of the lisp cache code, it cannot be used properly as
it conflicts with the garbage collector: cached data contains pointers
that would need to be updated at each collect.

File size: 27.1 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(int32_t filenumber)
165{
166  CHECK(filenumber>=0 && filenumber<total_files);
167  return files[filenumber]->filename;
168}
169
170uint32_t CrcManager::get_crc(int32_t 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(int32_t 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
815int32_t CacheList::alloc_id()
816{
817  int id;
818  if (prof_data)
819  {
820    the_game->show_help("new id allocated, cache profiling turned off\n");
821    prof_uninit();
822  }
823
824  // see if we previously allocated an id, if so check the next spot in the array
825  // otherwise we will have to scan the whole list for a free id and possible
826  // grow the list.
827  if (last_registered+1<total && list[last_registered+1].file_number<0)
828    id=last_registered+1;
829  else
830  {
831    int i;
832    CacheItem *ci=list;
833    for (i=0,id=-1; i<total && id<0; i++,ci++)        // scan list for a free id
834    {
835      if (ci->file_number<0)
836        id=i;
837    }
838
839    if (id<0)                                 // if no free id's then make list bigger
840    {
841      int add_size=20;
842      list=(CacheItem *)realloc(list,(sizeof(CacheItem)*(total+add_size)));
843      for (i=0; i<add_size; i++)
844      {
845        list[total+i].file_number=-1;         // mark new entries as new
846    list[total+i].last_access=-1;
847    list[total+i].data=NULL;
848      }
849      id=total;
850      if (prof_data)                          // new id's have been added old prof_data size won't work
851      { free(prof_data); prof_data=NULL; }
852      total+=add_size;
853    }
854  }
855  last_registered=id;
856  return id;
857}
858
859int32_t CacheList::reg_lisp_block(Cell *block)
860{
861    if (lcache_number == -1)
862        lcache_number = crc_manager.get_filenumber(lfname);
863
864    int id = alloc_id(), fn = crc_manager.get_filenumber(lfname);
865    CacheItem *ci = list + id;
866    CHECK(id < total && list[id].file_number < 0);
867
868    ci->file_number = fn;
869    ci->last_access = -1;
870    ci->type = SPEC_EXTERNAL_LCACHE;
871    ci->data = (void *)block; // we can't cache it out so it must be in memory
872    return id;
873}
874
875int32_t CacheList::reg_object(char const *filename, void *object, int type, int rm_dups)
876{
877  char *name;
878  if (item_type(object)==L_CONS_CELL)      // see if we got a object with a filename included
879  {
880    filename=lstring_value(lcar(object));
881    name=lstring_value(lcdr(object));
882  }
883  else name=lstring_value(object);        // otherwise should be a string
884  return reg(filename,name,type,rm_dups);
885}
886
887extern int total_files_open;
888
889int32_t CacheList::reg(char const *filename, char const *name, int type, int rm_dups)
890{
891    int id=alloc_id(),i,fn=crc_manager.get_filenumber(filename);
892    CacheItem *ci=list+id;
893    CHECK(id<total && list[id].file_number<0);
894
895    if( type == SPEC_EXTERN_SFX ) // If a extern sound effect then just make sure it's there
896    {
897        bFILE *check=open_file(filename,"rb");
898        if (check->open_failure())
899        {
900            delete check;
901            if( sound_avail )
902            {
903                printf("Unable to open file '%s' for reading\n",filename);
904                exit(0);
905            }
906            else
907            {
908                // Sound is disabled, we don't really care if the sound file
909                // is there or not, just pretend it's all ok.
910                return id;
911            }
912        }
913        char buf[4];
914        check->read(buf,4);
915        delete check;
916        if (memcmp(buf,"RIFF",4))
917        {
918            printf("File %s is not a WAV file\n",filename);
919            exit(0);
920        }
921        ci->file_number=fn;
922        ci->last_access=-1;
923        ci->data=NULL;
924        ci->offset=0;
925        ci->type=type;
926        return id;
927    }
928
929    spec_directory *sd=sd_cache.get_spec_directory(filename);
930
931    if (!sd)
932    {
933        printf("Unable to open filename %s for requested item %s\n",filename,name);
934        exit(0);
935    }
936
937    spec_entry *se;
938    if (type!=-1)
939    {
940        se=sd->find(name,type);
941        if (!se) se=sd->find(name);
942    }
943    else se=sd->find(name);
944
945
946    if (!se)
947    {
948        printf("No such item %s in file %s\n",name,filename);
949        exit(0);
950    }
951    else if (type>=0 && (type!=se->type && ((type!=SPEC_CHARACTER2 && type!=SPEC_CHARACTER)  ||
952                        (se->type!=SPEC_CHARACTER && se->type!=SPEC_CHARACTER2))))
953    {
954        printf("Item %s of file %s should be type %s\n",name,filename,spec_types[type]);
955        exit(0);
956    }
957
958    if (rm_dups)
959    {
960        for (i=0; i<total; i++)
961            if (list[i].file_number == fn && (unsigned)list[i].offset == se->offset)
962                return i;
963    }
964
965    ci->file_number=fn;
966    ci->last_access=-1;
967    ci->data=NULL;
968    ci->offset=se->offset;
969    ci->type=se->type;
970    return id;
971}
972
973
974void CacheList::normalize()
975{
976  int j;
977  CacheItem *ci=list;
978  last_access=-1;
979  for (j=0; j<total; j++,ci++)
980  {
981    if (ci->last_access>=0)
982      ci->last_access=ci->last_access>>16;        // shift everything over by 16
983    if (ci->last_access>last_access)            //  and find new largest timestamp
984      last_access=ci->last_access;
985  }
986  last_access++;
987}
988
989backtile *CacheList::backt(int id)
990{
991  CacheItem *me=list+id;
992  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
993
994  if (me->last_access>=0)
995  {
996    touch(me);
997    return (backtile *)me->data;
998  }
999  else
1000  {
1001    touch(me);
1002    locate(me);
1003    me->data=(void *)new backtile(fp);
1004    last_offset=fp->tell();
1005    return (backtile *)me->data;
1006  }
1007}
1008
1009
1010foretile *CacheList::foret(int id)
1011{
1012  CacheItem *me=list+id;
1013  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1014
1015  if (me->last_access>=0)
1016  {
1017    touch(me);
1018    return (foretile *)me->data;
1019  }
1020  else
1021  {
1022    touch(me);
1023    locate(me);
1024    me->data=(void *)new foretile(fp);
1025    last_offset=fp->tell();
1026    return (foretile *)me->data;
1027  }
1028}
1029
1030figure *CacheList::fig(int id)
1031{
1032  CacheItem *me=list+id;
1033//  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1034  if (me->last_access>=0)
1035  {
1036    touch(me);
1037    return (figure *)me->data;
1038  }
1039  else
1040  {
1041    touch(me);
1042    locate(me);
1043    me->data=(void *)new figure(fp,me->type);
1044    last_offset=fp->tell();
1045    return (figure *)me->data;
1046  }
1047}
1048
1049image *CacheList::img(int id)
1050{
1051  CacheItem *me=list+id;
1052  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1053  if (me->last_access>=0)
1054  {
1055    touch(me);
1056    return (image *)me->data;
1057  }
1058  else
1059  {
1060    touch(me);                                           // hold me, feel me, be me!
1061    locate(me);
1062    image *im=new image(fp);
1063    me->data=(void *)im;
1064    last_offset=fp->tell();
1065
1066    return (image *)me->data;
1067  }
1068}
1069
1070sound_effect *CacheList::sfx(int id)
1071{
1072  CacheItem *me=list+id;
1073  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1074  if (me->last_access>=0)
1075  {
1076    touch(me);                                           // hold me, feel me, be me!
1077    return (sound_effect *)me->data;
1078  }
1079  else
1080  {
1081    touch(me);                                           // hold me, feel me, be me!
1082    char *fn=crc_manager.get_filename(me->file_number);
1083    me->data=(void *)new sound_effect(fn);
1084    return (sound_effect *)me->data;
1085  }
1086}
1087
1088
1089part_frame *CacheList::part(int id)
1090{
1091  CacheItem *me=list+id;
1092  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1093  if (me->last_access>=0)
1094  {
1095    touch(me);                                           // hold me, feel me, be me!
1096    return (part_frame *)me->data;
1097  }
1098  else
1099  {
1100    touch(me);
1101    locate(me);
1102    me->data=(void *)new part_frame(fp);
1103    last_offset=fp->tell();
1104    return (part_frame *)me->data;
1105  }
1106}
1107
1108
1109Cell *CacheList::lblock(int id)
1110{
1111  CacheItem *me=list+id;
1112  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1113  return (Cell *)me->data;
1114}
1115
1116CacheList cache;
1117
1118void CacheList::free_oldest()
1119{
1120  int32_t old_time = last_access;
1121  CacheItem *ci=list,*oldest=NULL;
1122  ful=1;
1123
1124  for (int i = 0; i < total; i++, ci++)
1125  {
1126    if (ci->data && ci->last_access < old_time)
1127    {
1128      oldest = ci;
1129      old_time = ci->last_access;
1130    }
1131  }
1132  if (oldest)
1133  {
1134    dprintf("mem_maker : freeing %s\n",spec_types[oldest->type]);
1135    unmalloc(oldest);
1136  }
1137  else
1138  {
1139    close_graphics();
1140    printf("Out of memory, please remove any TSR's device drivers you can\n");
1141    exit(0);
1142  }
1143}
1144
1145
1146void CacheList::show_accessed()
1147{
1148  int old=last_access,new_old_accessed;
1149  CacheItem *ci,*new_old;
1150
1151  do
1152  {
1153    new_old_accessed=-1;
1154    new_old=NULL;
1155    ci=list;
1156    for (int i=0; i<total; i++,ci++)
1157    {
1158      if (ci->last_access<old && ci->last_access>0 && ci->last_access>new_old_accessed)
1159      {
1160    new_old_accessed=ci->last_access;
1161        new_old=ci;
1162      }
1163    }
1164    if (new_old)
1165    {
1166      ci=new_old;
1167      old=ci->last_access;
1168      printf("type=(%20s) file=(%20s) access=(%6ld)\n",spec_types[ci->type],
1169         crc_manager.get_filename(ci->file_number),
1170         (long int)ci->last_access);
1171    }
1172  } while (new_old);
1173}
1174
1175int CacheList::loaded(int id)
1176{
1177  CacheItem *me=list+id;
1178  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id");
1179  if (me->last_access>=0)
1180    return 1;
1181  else return 0;
1182}
1183
1184char_tint *CacheList::ctint(int id)
1185{
1186  CacheItem *me=list+id;
1187  CONDITION(id<total && id>=0 && me->file_number>=0,"Bad id" && me->type==SPEC_PALETTE);
1188  if (me->last_access>=0)
1189  {
1190    touch(me);
1191    return (char_tint *)me->data;
1192  }
1193  else
1194  {
1195    touch(me);
1196    locate(me);
1197    me->data=(void *)new char_tint(fp);
1198    last_offset=fp->tell();
1199    return (char_tint *)me->data;
1200  }
1201}
1202
Note: See TracBrowser for help on using the repository browser.