Rev 277 | Go to most recent revision | Details | 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 | |||
11 | struct grpfile grpfiles[numgrpfiles] = { |
||
12 | { "Registered Version 1.3d", 0xBBC9CE44, 26524524, GAMEDUKE, NULL }, |
||
13 | { "Registered Version 1.4", 0xF514A6AC, 44348015, GAMEDUKE, NULL }, |
||
14 | { "Registered Version 1.5", 0xFD3DCFF1, 44356548, GAMEDUKE, NULL }, |
||
15 | { "Shareware Version", 0x983AD923, 11035779, GAMEDUKE, NULL }, |
||
16 | { "Mac Shareware Version", 0xC5F71561, 10444391, GAMEDUKE, NULL }, |
||
17 | { "Mac Registered Version", 0x00000000, 0, GAMEDUKE, NULL }, |
||
18 | { "NAM", 0x75C1F07B, 43448927, GAMENAM, NULL }, |
||
19 | }; |
||
20 | struct grpfile *foundgrps = NULL; |
||
21 | |||
22 | #define GRPCACHEFILE "grpfiles.cache" |
||
23 | static struct grpcache { |
||
24 | struct grpcache *next; |
||
25 | char name[BMAX_PATH+1]; |
||
26 | int size; |
||
27 | int mtime; |
||
28 | int crcval; |
||
29 | } *grpcache = NULL, *usedgrpcache = NULL; |
||
30 | |||
31 | static int LoadGroupsCache(void) |
||
32 | { |
||
33 | struct grpcache *fg; |
||
34 | |||
35 | int fsize, fmtime, fcrcval; |
||
36 | char *fname; |
||
37 | |||
38 | scriptfile *script; |
||
39 | |||
40 | script = scriptfile_fromfile(GRPCACHEFILE); |
||
41 | if (!script) return -1; |
||
42 | |||
43 | while (!scriptfile_eof(script)) { |
||
44 | if (scriptfile_getstring(script, &fname)) break; // filename |
||
45 | if (scriptfile_getnumber(script, &fsize)) break; // filesize |
||
46 | if (scriptfile_getnumber(script, &fmtime)) break; // modification time |
||
47 | if (scriptfile_getnumber(script, &fcrcval)) break; // crc checksum |
||
48 | |||
49 | fg = calloc(1, sizeof(struct grpcache)); |
||
50 | fg->next = grpcache; |
||
51 | grpcache = fg; |
||
52 | |||
53 | strncpy(fg->name, fname, BMAX_PATH); |
||
54 | fg->size = fsize; |
||
55 | fg->mtime = fmtime; |
||
56 | fg->crcval = fcrcval; |
||
57 | } |
||
58 | |||
59 | scriptfile_close(script); |
||
60 | return 0; |
||
61 | } |
||
62 | |||
63 | static void FreeGroupsCache(void) |
||
64 | { |
||
65 | struct grpcache *fg; |
||
66 | |||
67 | while (grpcache) { |
||
68 | fg = grpcache->next; |
||
69 | free(grpcache); |
||
70 | grpcache = fg; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | int ScanGroups(void) |
||
75 | { |
||
76 | CACHE1D_FIND_REC *srch, *sidx; |
||
77 | struct grpcache *fg, *fgg; |
||
78 | struct grpfile *grp; |
||
79 | char *fn; |
||
80 | struct Bstat st; |
||
81 | |||
82 | initprintf("Scanning for GRP files...\n"); |
||
83 | |||
84 | LoadGroupsCache(); |
||
85 | |||
86 | srch = klistpath("/", "*.grp", CACHE1D_FIND_FILE); |
||
87 | |||
88 | for (sidx = srch; sidx; sidx = sidx->next) { |
||
89 | for (fg = grpcache; fg; fg = fg->next) { |
||
90 | if (!Bstrcmp(fg->name, sidx->name)) break; |
||
91 | } |
||
92 | |||
93 | if (fg) { |
||
94 | if (findfrompath(sidx->name, &fn)) continue; // failed to resolve the filename |
||
95 | if (Bstat(fn, &st)) { free(fn); continue; } // failed to stat the file |
||
96 | free(fn); |
||
97 | if (fg->size == st.st_size && fg->mtime == st.st_mtime) { |
||
98 | grp = (struct grpfile *)calloc(1, sizeof(struct grpfile)); |
||
99 | grp->name = strdup(sidx->name); |
||
100 | grp->crcval = fg->crcval; |
||
101 | grp->size = fg->size; |
||
102 | grp->next = foundgrps; |
||
103 | foundgrps = grp; |
||
104 | |||
105 | fgg = (struct grpcache *)calloc(1, sizeof(struct grpcache)); |
||
106 | strcpy(fgg->name, fg->name); |
||
107 | fgg->size = fg->size; |
||
108 | fgg->mtime = fg->mtime; |
||
109 | fgg->crcval = fg->crcval; |
||
110 | fgg->next = usedgrpcache; |
||
111 | usedgrpcache = fgg; |
||
112 | continue; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | { |
||
117 | int b, fh; |
||
118 | int crcval; |
||
119 | char buf[16*512]; |
||
120 | |||
121 | fh = openfrompath(sidx->name, BO_RDONLY|BO_BINARY, BS_IREAD); |
||
122 | if (fh < 0) continue; |
||
123 | if (fstat(fh, &st)) continue; |
||
124 | |||
125 | initprintf(" Checksumming %s...", sidx->name); |
||
126 | crc32init((unsigned long *)&crcval); |
||
127 | do { |
||
128 | b = read(fh, buf, sizeof(buf)); |
||
129 | if (b > 0) crc32block((unsigned long *)&crcval, buf, b); |
||
130 | } while (b == sizeof(buf)); |
||
131 | crc32finish((unsigned long *)&crcval); |
||
132 | close(fh); |
||
133 | initprintf(" Done\n"); |
||
134 | |||
135 | grp = (struct grpfile *)calloc(1, sizeof(struct grpfile)); |
||
136 | grp->name = strdup(sidx->name); |
||
137 | grp->crcval = crcval; |
||
138 | grp->size = st.st_size; |
||
139 | grp->next = foundgrps; |
||
140 | foundgrps = grp; |
||
141 | |||
142 | fgg = (struct grpcache *)calloc(1, sizeof(struct grpcache)); |
||
143 | strncpy(fgg->name, sidx->name, BMAX_PATH); |
||
144 | fgg->size = st.st_size; |
||
145 | fgg->mtime = st.st_mtime; |
||
146 | fgg->crcval = crcval; |
||
147 | fgg->next = usedgrpcache; |
||
148 | usedgrpcache = fgg; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | klistfree(srch); |
||
153 | FreeGroupsCache(); |
||
154 | |||
155 | if (usedgrpcache) { |
||
156 | FILE *fp; |
||
157 | fp = fopen(GRPCACHEFILE, "wt"); |
||
158 | if (fp) { |
||
159 | for (fg = usedgrpcache; fg; fg=fgg) { |
||
160 | fgg = fg->next; |
||
161 | fprintf(fp, "\"%s\" %d %d %d\n", fg->name, fg->size, fg->mtime, fg->crcval); |
||
162 | free(fg); |
||
163 | } |
||
164 | fclose(fp); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | return 0; |
||
169 | } |
||
170 | |||
171 | void FreeGroups(void) |
||
172 | { |
||
173 | struct grpfile *fg; |
||
174 | |||
175 | while (foundgrps) { |
||
176 | fg = foundgrps->next; |
||
177 | free((char*)foundgrps->name); |
||
178 | free(foundgrps); |
||
179 | foundgrps = fg; |
||
180 | } |
||
181 | } |
||
182 |