source: abuse/trunk/src/cache.cpp

Last change on this file was 635, checked in by Sam Hocevar, 12 years ago

lisp: refactor Lisp spaces so that they are real objects, and get rid
of the unused USER_SPACE.

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