[49] | 1 | #ifndef __SPECS_HPP_ |
---|
| 2 | #define __SPECS_HPP_ |
---|
| 3 | #include "linked.hpp" |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include "jmalloc.hpp" |
---|
| 6 | #include "system.h" |
---|
| 7 | #include <fcntl.h> |
---|
| 8 | #include <stdlib.h> |
---|
| 9 | |
---|
| 10 | extern char *spec_types[]; |
---|
| 11 | |
---|
| 12 | #define SPEC_INVALID_TYPE 0 |
---|
| 13 | #define SPEC_COLOR_TABLE 1 |
---|
| 14 | #define SPEC_PALETTE 2 |
---|
| 15 | #define SPEC_IMAGE 4 |
---|
| 16 | #define SPEC_FORETILE 5 |
---|
| 17 | #define SPEC_BACKTILE 6 |
---|
| 18 | #define SPEC_CHARACTER 7 |
---|
| 19 | #define SPEC_MORPH_POINTS_8 8 |
---|
| 20 | #define SPEC_MORPH_POINTS_16 9 |
---|
| 21 | #define SPEC_GRUE_OBJS 10 |
---|
| 22 | #define SPEC_EXTERN_SFX 11 |
---|
| 23 | #define SPEC_DMX_MUS 12 |
---|
| 24 | #define SPEC_PATCHED_MORPH 13 |
---|
| 25 | #define SPEC_NORMAL_FILE 14 |
---|
| 26 | #define SPEC_COMPRESS1_FILE 15 |
---|
| 27 | #define SPEC_VECTOR_IMAGE 16 |
---|
| 28 | #define SPEC_LIGHT_LIST 17 |
---|
| 29 | #define SPEC_GRUE_FGMAP 18 |
---|
| 30 | #define SPEC_GRUE_BGMAP 19 |
---|
| 31 | #define SPEC_DATA_ARRAY 20 |
---|
| 32 | #define SPEC_CHARACTER2 21 |
---|
| 33 | #define SPEC_PARTICLE 22 |
---|
| 34 | #define SPEC_EXTERNAL_LCACHE 23 |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | #define SPEC_SIGNATURE "SPEC1.0" |
---|
| 38 | #define SPEC_SIG_SIZE 8 |
---|
| 39 | |
---|
| 40 | #define SPEC_FLAG_LINK 1 |
---|
| 41 | |
---|
| 42 | #define SPEC_SEARCH_INSIDE_OUTSIDE 1 |
---|
| 43 | #define SPEC_SEARCH_OUTSIDE_INSIDE 2 |
---|
| 44 | #define SPEC_SEARCH_INSIDE_ONLY 3 |
---|
| 45 | |
---|
| 46 | /* file specs |
---|
| 47 | 8 signature |
---|
| 48 | 2 number entries |
---|
| 49 | [entries] |
---|
| 50 | 1 entry type |
---|
| 51 | 1 entry name length |
---|
| 52 | X entry name (with null terminator) |
---|
| 53 | 1 flags |
---|
| 54 | if (flags&LINK) |
---|
| 55 | 1 link filename length |
---|
| 56 | X link filename |
---|
| 57 | else |
---|
| 58 | 4 data size |
---|
| 59 | 4 offset |
---|
| 60 | */ |
---|
| 61 | |
---|
| 62 | void set_spec_main_file(char *filename, int search_order=SPEC_SEARCH_OUTSIDE_INSIDE); |
---|
| 63 | |
---|
| 64 | void set_filename_prefix(char *prefix); |
---|
| 65 | char *get_filename_prefix(); |
---|
| 66 | void set_save_filename_prefix(char *prefix); |
---|
| 67 | char *get_save_filename_prefix(); |
---|
| 68 | #define JFILE_CLONED 1 |
---|
| 69 | |
---|
| 70 | class bFILE // base file type which other files should be derived from (jFILE & NFS for now) |
---|
| 71 | { |
---|
| 72 | protected : |
---|
| 73 | unsigned char *rbuf,*wbuf; |
---|
| 74 | unsigned long rbuf_start,rbuf_end,rbuf_size, |
---|
| 75 | wbuf_end,wbuf_size; // can't seek while writing! |
---|
| 76 | int flush_writes(); // returns 0 on failure, else # of bytes written |
---|
| 77 | |
---|
| 78 | virtual int unbuffered_read(void *buf, size_t count) = 0; |
---|
| 79 | virtual int unbuffered_write(void *buf, size_t count) = 0; |
---|
| 80 | virtual int unbuffered_tell() = 0; |
---|
| 81 | virtual int unbuffered_seek(long offset, int whence) = 0; // whence=SEEK_SET, SEEK_CUR, |
---|
| 82 | // SEEK_END, ret=0=success |
---|
| 83 | virtual int allow_read_buffering(); |
---|
| 84 | virtual int allow_write_buffering(); |
---|
| 85 | public : |
---|
| 86 | bFILE(); |
---|
| 87 | virtual int open_failure() = 0; |
---|
| 88 | int read(void *buf, size_t count); // returns number of bytes read, calls unbuffer_read |
---|
| 89 | int write(void *buf, size_t count); // returns number of bytes written |
---|
| 90 | int seek(long offset, int whence); // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success |
---|
| 91 | int tell(); |
---|
| 92 | virtual int file_size() = 0; |
---|
| 93 | |
---|
| 94 | virtual ~bFILE(); |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | // these read and writes, allways read/write Intel endian-ness |
---|
| 98 | unsigned short read_short(); |
---|
| 99 | unsigned long read_long(); |
---|
| 100 | unsigned char read_byte(); |
---|
| 101 | double read_double(); |
---|
| 102 | void write_short(unsigned short x); |
---|
| 103 | void write_long(unsigned long x); |
---|
| 104 | void write_byte(unsigned char x); |
---|
| 105 | void write_double(double x); |
---|
| 106 | void set_read_buffer_size(long size); |
---|
| 107 | } ; |
---|
| 108 | |
---|
| 109 | class jFILE : public bFILE // this file type will use virtual opens inside of a spe |
---|
| 110 | { |
---|
| 111 | char *fname; |
---|
| 112 | char *tmp_write_name; |
---|
| 113 | int access; |
---|
| 114 | int fd,flags; |
---|
| 115 | long start_offset,file_length; // offset of file from actual file begining |
---|
| 116 | |
---|
| 117 | long current_offset; // current offset |
---|
| 118 | |
---|
| 119 | public : |
---|
| 120 | int get_fd() const { return fd; } |
---|
| 121 | |
---|
| 122 | void open_internal(char *filename, char *mode, int flags); |
---|
| 123 | void open_external(char *filename, char *mode, int flags); |
---|
| 124 | |
---|
| 125 | jFILE(char *filename, char *access_string); // same as fopen parameters |
---|
| 126 | jFILE(FILE *file_pointer); // assumes fp is at begining of file |
---|
| 127 | virtual int open_failure() { return fd<0; } |
---|
| 128 | virtual int unbuffered_read(void *buf, size_t count); // returns number of bytes read |
---|
| 129 | virtual int unbuffered_write(void *buf, size_t count); // returns number of bytes written |
---|
| 130 | virtual int unbuffered_seek(long offset, int whence); // whence=SEEK_SET, SEEK_CUR, |
---|
| 131 | // SEEK_END, ret=0=success |
---|
| 132 | virtual int unbuffered_tell(); |
---|
| 133 | virtual int file_size() { return file_length; } |
---|
| 134 | virtual ~jFILE(); |
---|
| 135 | } ; |
---|
| 136 | |
---|
| 137 | class spec_entry |
---|
| 138 | { |
---|
| 139 | public : |
---|
| 140 | char *name; |
---|
| 141 | unsigned long size,offset; |
---|
| 142 | unsigned char type; |
---|
| 143 | |
---|
| 144 | spec_entry(unsigned char spec_type, |
---|
| 145 | char *object_name, |
---|
| 146 | char *link_filename, |
---|
| 147 | unsigned long data_size, |
---|
| 148 | unsigned long data_offset) |
---|
| 149 | { type=spec_type; |
---|
| 150 | name=strcpy((char *)jmalloc(strlen(object_name)+1,"spec_entry::name"),object_name); |
---|
| 151 | size=data_size; offset=data_offset; |
---|
| 152 | } |
---|
| 153 | void print(); |
---|
| 154 | ~spec_entry() { if (name) jfree(name); } |
---|
| 155 | } ; |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | class spec_directory |
---|
| 159 | { |
---|
| 160 | public : |
---|
| 161 | void startup(bFILE *fp); |
---|
| 162 | |
---|
| 163 | int total; |
---|
| 164 | spec_entry **entries; |
---|
| 165 | void *data; |
---|
| 166 | long size; |
---|
| 167 | // spec_directory(char *filename); ;; not allowed anymore, user must construct file first! |
---|
| 168 | spec_directory(FILE *fp); |
---|
| 169 | spec_directory(bFILE *fp); |
---|
| 170 | spec_directory(); |
---|
| 171 | spec_entry *find(char *name); |
---|
| 172 | spec_entry *find(char *name, int type); |
---|
| 173 | spec_entry *find(int type); |
---|
| 174 | long find_number(char*name); |
---|
| 175 | long find_number(int type); |
---|
| 176 | void remove(spec_entry *e); |
---|
| 177 | void add_by_hand(spec_entry *e); |
---|
| 178 | void calc_offsets(); |
---|
| 179 | long data_start_offset(); // returns the firsyt offset past directory items |
---|
| 180 | long data_end_offset(); // this should be the end of the file |
---|
| 181 | long type_total(int type); |
---|
| 182 | jFILE *write(char *filename); |
---|
| 183 | int write(bFILE *fp); |
---|
| 184 | void print(); |
---|
| 185 | void delete_entries(); // if the directory was created by hand instead of by file |
---|
| 186 | ~spec_directory(); |
---|
| 187 | } ; |
---|
| 188 | |
---|
| 189 | /*jFILE *add_directory_entry(char *filename, |
---|
| 190 | unsigned short data_type, |
---|
| 191 | char *data_name, |
---|
| 192 | unsigned long data_size, |
---|
| 193 | char *link_filename=NULL);*/ |
---|
| 194 | |
---|
| 195 | unsigned short read_short(FILE *fp); |
---|
| 196 | unsigned long read_long(FILE *fp); |
---|
| 197 | unsigned short read_other_long(FILE *fp); |
---|
| 198 | unsigned long read_other_short(FILE *fp); |
---|
| 199 | unsigned char read_byte(FILE *fp); |
---|
| 200 | |
---|
| 201 | void write_short(FILE *fp, unsigned short x); |
---|
| 202 | void write_long(FILE *fp, unsigned long x); |
---|
| 203 | void write_other_short(FILE *fp, unsigned short x); |
---|
| 204 | void write_other_long(FILE *fp, unsigned long x); |
---|
| 205 | void write_byte(FILE *fp, unsigned char x); |
---|
| 206 | |
---|
| 207 | void set_spec_main_file(char *filename, int Search_order); |
---|
| 208 | void set_file_opener(bFILE *(*open_fun)(char *, char *)); |
---|
| 209 | void set_no_space_handler(void (*handle_fun)()); |
---|
| 210 | bFILE *open_file(char *filename, char *mode); |
---|
| 211 | #endif |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | |
---|