1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
// Reads .gat files by name-mapping .wlk files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "utils.h"
#include "grfio.h"
#include "mmo.h"
#include "socket.h"
//----------------------------
// file entry table struct
//----------------------------
typedef struct
{
size_t declen;
int16_t next; // next index into the filelist[] array, or -1
char fn[128 - 4 - 2]; // file name
} FILELIST;
#define FILELIST_LIMIT 32768 // limit to number of filelists - if you increase this, change all shorts to int
#define FILELIST_ADDS 1024 // amount to increment when reallocing
static FILELIST *filelist = NULL;
/// Number of entries used
static uint16_t filelist_entrys = 0;
/// Number of FILELIST entries actually allocated
static uint16_t filelist_maxentry = 0;
/// First index of the given hash, into the filelist[] array
static int16_t filelist_hash[256] = {[0 ... 255] = -1};
/// Hash a filename
static uint8_t filehash (const char *fname)
{
// Larger than the return type - upper bits are used in the process
uint32_t hash = 0;
while (*fname)
{
hash = (hash << 1) + (hash >> 7) * 9 + (unsigned char)*fname;
fname++;
}
return hash;
}
/// Find the filelist entry for the given filename, or NULL if it is not
FILELIST *filelist_find (const char *fname)
{
int16_t index = filelist_hash[filehash (fname)];
while (index >= 0)
{
if (strcmp (filelist[index].fn, fname) == 0)
return &filelist[index];
index = filelist[index].next;
}
return NULL;
}
/// Copy a temporary entry into the hash map
static FILELIST *filelist_add (FILELIST * entry)
{
if (filelist_entrys >= FILELIST_LIMIT)
{
fprintf (stderr, "filelist limit : filelist_add\n");
exit (1);
}
if (filelist_entrys >= filelist_maxentry)
{
RECREATE(filelist, FILELIST, filelist_maxentry + FILELIST_ADDS);
memset (filelist + filelist_maxentry, '\0',
FILELIST_ADDS * sizeof (FILELIST));
filelist_maxentry += FILELIST_ADDS;
}
uint16_t new_index = filelist_entrys++;
uint8_t hash = filehash (entry->fn);
entry->next = filelist_hash[hash];
filelist_hash[hash] = new_index;
filelist[new_index] = *entry;
return &filelist[new_index];
}
static FILELIST *filelist_modify (FILELIST * entry)
{
FILELIST *fentry = filelist_find (entry->fn);
if (fentry)
{
entry->next = fentry->next;
*fentry = *entry;
return fentry;
}
return filelist_add (entry);
}
/// Change fname data/*.gat to lfname data/*.wlk
// TODO even if the file exists, don't keep reopening it every time one loads
void grfio_resnametable (const char *fname, char *lfname)
{
char restable[] = "data/resnametable.txt";
FILE *fp = fopen_ (restable, "rb");
if (fp == NULL)
{
fprintf(stderr, "No resnametable, can't look for %s\n", fname);
strcpy(lfname, fname);
char* ext = lfname + strlen(lfname) - 4;
if (!strcmp(ext, ".gat"))
strcpy(ext, ".wlk");
return;
}
char line[512];
while (fgets (line, sizeof (line), fp))
{
char w1[256], w2[256];
if (
// line is of the form foo.gat#foo.wlk#
(sscanf (line, "%[^#]#%[^#]#", w1, w2) == 2)
// strip data/ from foo.gat before comparing
&& (!strcmp (w1, fname + 5)))
{
strcpy (lfname, "data/");
strcpy (lfname + 5, w2);
fclose_ (fp);
return;
}
}
fprintf(stderr, "Unable to find resource: %s\n", fname);
fclose_ (fp);
strcpy(lfname, fname);
char* ext = lfname + strlen(lfname) - 4;
if (!strcmp(ext, ".gat"))
strcpy(ext, ".wlk");
return;
}
/// Size of resource
size_t grfio_size (const char *fname)
{
FILELIST *entry = filelist_find (fname);
if (entry)
return entry->declen;
char lfname[256];
FILELIST lentry;
struct stat st;
grfio_resnametable (fname, lfname);
for (char *p = lfname; *p; p++)
if (*p == '\\')
*p = '/';
if (stat (lfname, &st) == 0)
{
strncpy (lentry.fn, fname, sizeof (lentry.fn) - 1);
lentry.declen = st.st_size;
entry = filelist_modify (&lentry);
}
else
{
printf ("%s not found\n", fname);
return 0;
}
return entry->declen;
}
void *grfio_reads (const char *fname, size_t *size)
{
char lfname[256];
grfio_resnametable (fname, lfname);
for (char *p = &lfname[0]; *p != 0; p++)
if (*p == '\\')
*p = '/'; // * At the time of Unix
FILE *in = fopen_ (lfname, "rb");
if (!in)
{
fprintf (stderr, "%s not found\n", fname);
return NULL;
}
FILELIST lentry;
FILELIST *entry = filelist_find (fname);
if (entry)
{
lentry.declen = entry->declen;
}
else
{
fseek (in, 0, SEEK_END);
lentry.declen = ftell (in);
fseek (in, 0, SEEK_SET);
strncpy (lentry.fn, fname, sizeof (lentry.fn) - 1);
entry = filelist_modify (&lentry);
}
uint8_t *buf2;
CREATE (buf2, uint8_t, lentry.declen + 1024);
if (!fread (buf2, 1, lentry.declen, in))
exit(1);
fclose_ (in);
in = NULL;
if (size)
*size = entry->declen;
return buf2;
}
|