Rev 1076 | Rev 1205 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
241 | terminx | 1 | #include "compat.h" |
2 | #include "baselayer.h" |
||
3 | |||
4 | #include "scriptfile.h" |
||
5 | #include "cache1d.h" |
||
6 | #include "crc32.h" |
||
7 | |||
8 | #include "duke3d.h" |
||
9 | #include "grpscan.h" |
||
10 | |||
335 | terminx | 11 | struct grpfile grpfiles[numgrpfiles] = |
559 | terminx | 12 | { |
13 | { "Duke Nukem 3D", 0xBBC9CE44, 26524524, GAMEDUKE, NULL }, |
||
14 | { "Duke Nukem 3D: Atomic Edition", 0xF514A6AC, 44348015, GAMEDUKE, NULL }, |
||
15 | { "Duke Nukem 3D: Atomic Edition", 0xFD3DCFF1, 44356548, GAMEDUKE, NULL }, |
||
16 | { "Duke Nukem 3D Shareware", 0x983AD923, 11035779, GAMEDUKE, NULL }, |
||
17 | { "Duke Nukem 3D Mac Shareware", 0xC5F71561, 10444391, GAMEDUKE, NULL }, |
||
886 | terminx | 18 | // { "Duke Nukem 3D Mac", 0x00000000, 0, GAMEDUKE, NULL }, |
559 | terminx | 19 | { "NAM", 0x75C1F07B, 43448927, GAMENAM, NULL }, |
20 | { "Napalm", 0x3DE1589A, 44365728, GAMENAM, NULL }, |
||
21 | { "WW2GI", 0x907B82BF, 77939508, GAMEWW2, NULL }, |
||
22 | }; |
||
241 | terminx | 23 | struct grpfile *foundgrps = NULL; |
24 | |||
25 | #define GRPCACHEFILE "grpfiles.cache" |
||
335 | terminx | 26 | static struct grpcache |
27 | { |
||
241 | terminx | 28 | struct grpcache *next; |
29 | char name[BMAX_PATH+1]; |
||
30 | int size; |
||
31 | int mtime; |
||
32 | int crcval; |
||
335 | terminx | 33 | } |
34 | *grpcache = NULL, *usedgrpcache = NULL; |
||
241 | terminx | 35 | |
36 | static int LoadGroupsCache(void) |
||
37 | { |
||
38 | struct grpcache *fg; |
||
39 | |||
40 | int fsize, fmtime, fcrcval; |
||
41 | char *fname; |
||
42 | |||
43 | scriptfile *script; |
||
44 | |||
45 | script = scriptfile_fromfile(GRPCACHEFILE); |
||
46 | if (!script) return -1; |
||
47 | |||
335 | terminx | 48 | while (!scriptfile_eof(script)) |
49 | { |
||
241 | terminx | 50 | if (scriptfile_getstring(script, &fname)) break; // filename |
51 | if (scriptfile_getnumber(script, &fsize)) break; // filesize |
||
52 | if (scriptfile_getnumber(script, &fmtime)) break; // modification time |
||
53 | if (scriptfile_getnumber(script, &fcrcval)) break; // crc checksum |
||
54 | |||
808 | terminx | 55 | fg = Bcalloc(1, sizeof(struct grpcache)); |
241 | terminx | 56 | fg->next = grpcache; |
57 | grpcache = fg; |
||
58 | |||
59 | strncpy(fg->name, fname, BMAX_PATH); |
||
60 | fg->size = fsize; |
||
61 | fg->mtime = fmtime; |
||
62 | fg->crcval = fcrcval; |
||
63 | } |
||
64 | |||
65 | scriptfile_close(script); |
||
66 | return 0; |
||
67 | } |
||
68 | |||
69 | static void FreeGroupsCache(void) |
||
70 | { |
||
71 | struct grpcache *fg; |
||
72 | |||
335 | terminx | 73 | while (grpcache) |
74 | { |
||
241 | terminx | 75 | fg = grpcache->next; |
76 | free(grpcache); |
||
77 | grpcache = fg; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | int ScanGroups(void) |
||
82 | { |
||
83 | CACHE1D_FIND_REC *srch, *sidx; |
||
84 | struct grpcache *fg, *fgg; |
||
85 | struct grpfile *grp; |
||
86 | char *fn; |
||
87 | struct Bstat st; |
||
88 | |||
89 | initprintf("Scanning for GRP files...\n"); |
||
90 | |||
91 | LoadGroupsCache(); |
||
92 | |||
93 | srch = klistpath("/", "*.grp", CACHE1D_FIND_FILE); |
||
94 | |||
335 | terminx | 95 | for (sidx = srch; sidx; sidx = sidx->next) |
96 | { |
||
97 | for (fg = grpcache; fg; fg = fg->next) |
||
98 | { |
||
241 | terminx | 99 | if (!Bstrcmp(fg->name, sidx->name)) break; |
100 | } |
||
101 | |||
335 | terminx | 102 | if (fg) |
103 | { |
||
241 | terminx | 104 | if (findfrompath(sidx->name, &fn)) continue; // failed to resolve the filename |
335 | terminx | 105 | if (Bstat(fn, &st)) |
106 | { |
||
107 | free(fn); |
||
108 | continue; |
||
109 | } // failed to stat the file |
||
241 | terminx | 110 | free(fn); |
335 | terminx | 111 | if (fg->size == st.st_size && fg->mtime == st.st_mtime) |
112 | { |
||
808 | terminx | 113 | grp = (struct grpfile *)Bcalloc(1, sizeof(struct grpfile)); |
241 | terminx | 114 | grp->name = strdup(sidx->name); |
115 | grp->crcval = fg->crcval; |
||
116 | grp->size = fg->size; |
||
117 | grp->next = foundgrps; |
||
118 | foundgrps = grp; |
||
119 | |||
808 | terminx | 120 | fgg = (struct grpcache *)Bcalloc(1, sizeof(struct grpcache)); |
241 | terminx | 121 | strcpy(fgg->name, fg->name); |
122 | fgg->size = fg->size; |
||
123 | fgg->mtime = fg->mtime; |
||
124 | fgg->crcval = fg->crcval; |
||
125 | fgg->next = usedgrpcache; |
||
126 | usedgrpcache = fgg; |
||
127 | continue; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | { |
||
132 | int b, fh; |
||
133 | int crcval; |
||
134 | char buf[16*512]; |
||
135 | |||
136 | fh = openfrompath(sidx->name, BO_RDONLY|BO_BINARY, BS_IREAD); |
||
137 | if (fh < 0) continue; |
||
138 | if (fstat(fh, &st)) continue; |
||
139 | |||
140 | initprintf(" Checksumming %s...", sidx->name); |
||
584 | terminx | 141 | crc32init((unsigned int *)&crcval); |
335 | terminx | 142 | do |
143 | { |
||
241 | terminx | 144 | b = read(fh, buf, sizeof(buf)); |
584 | terminx | 145 | if (b > 0) crc32block((unsigned int *)&crcval, (unsigned char *)buf, b); |
335 | terminx | 146 | } |
147 | while (b == sizeof(buf)); |
||
584 | terminx | 148 | crc32finish((unsigned int *)&crcval); |
241 | terminx | 149 | close(fh); |
150 | initprintf(" Done\n"); |
||
151 | |||
808 | terminx | 152 | grp = (struct grpfile *)Bcalloc(1, sizeof(struct grpfile)); |
241 | terminx | 153 | grp->name = strdup(sidx->name); |
154 | grp->crcval = crcval; |
||
155 | grp->size = st.st_size; |
||
156 | grp->next = foundgrps; |
||
157 | foundgrps = grp; |
||
158 | |||
808 | terminx | 159 | fgg = (struct grpcache *)Bcalloc(1, sizeof(struct grpcache)); |
241 | terminx | 160 | strncpy(fgg->name, sidx->name, BMAX_PATH); |
161 | fgg->size = st.st_size; |
||
162 | fgg->mtime = st.st_mtime; |
||
163 | fgg->crcval = crcval; |
||
164 | fgg->next = usedgrpcache; |
||
165 | usedgrpcache = fgg; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | klistfree(srch); |
||
170 | FreeGroupsCache(); |
||
171 | |||
335 | terminx | 172 | if (usedgrpcache) |
173 | { |
||
1000 | terminx | 174 | int i = 0; |
241 | terminx | 175 | FILE *fp; |
176 | fp = fopen(GRPCACHEFILE, "wt"); |
||
335 | terminx | 177 | if (fp) |
178 | { |
||
179 | for (fg = usedgrpcache; fg; fg=fgg) |
||
180 | { |
||
241 | terminx | 181 | fgg = fg->next; |
182 | fprintf(fp, "\"%s\" %d %d %d\n", fg->name, fg->size, fg->mtime, fg->crcval); |
||
183 | free(fg); |
||
1000 | terminx | 184 | i++; |
241 | terminx | 185 | } |
186 | fclose(fp); |
||
187 | } |
||
1178 | terminx | 188 | // initprintf("Found %d recognized GRP %s.\n",i,i>1?"files":"file"); |
1000 | terminx | 189 | return 0; |
241 | terminx | 190 | } |
1000 | terminx | 191 | initprintf("Found no recognized GRP files!\n"); |
241 | terminx | 192 | return 0; |
193 | } |
||
194 | |||
195 | void FreeGroups(void) |
||
196 | { |
||
197 | struct grpfile *fg; |
||
198 | |||
335 | terminx | 199 | while (foundgrps) |
200 | { |
||
241 | terminx | 201 | fg = foundgrps->next; |
202 | free((char*)foundgrps->name); |
||
203 | free(foundgrps); |
||
204 | foundgrps = fg; |
||
205 | } |
||
206 | } |
||
207 |