DEADSOFTWARE

2eab3a877e8a1cd07c4a1e82ada8ec1b6bf2b8a6
[flatwaifu.git] / src / files.c
1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #include "glob.h"
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include "files.h"
24 #include "error.h"
26 #include "map.h" // MAP_load
27 #include "save.h" // SAVE_getname
29 #ifdef UNIX
30 # include <sys/stat.h>
31 #endif
33 #include "common/streams.h"
34 #include "common/files.h"
35 #include "common/wadres.h"
36 #include "common/cp866.h"
38 int d_start, d_end;
40 char savname[SAVE_MAX][SAVE_MAXLEN];
41 char savok[SAVE_MAX];
43 static int m_start, m_end;
44 static int s_start, s_end;
46 void F_startup (void) {
47 logo("F_startup: setup file system\n");
48 }
50 void F_addwad (const char *fn) {
51 static int i = 0;
52 static FILE_Stream wadh[MAX_WADS];
53 if (i < MAX_WADS) {
54 if (FILE_Open(&wadh[i], fn, "rb")) {
55 if (WADRES_addwad(&wadh[i].base)) {
56 i += 1;
57 } else {
58 ERR_failinit("Invalid WAD %s", fn);
59 }
60 } else {
61 ERR_failinit("Unable to add WAD %s", fn);
62 }
63 } else {
64 ERR_failinit("Too many wads");
65 }
66 }
68 void F_initwads (void) {
69 if (!WADRES_rehash()) {
70 ERR_failinit("F_initwads: failed rehash");
71 }
72 }
74 void F_allocres (void) {
75 d_start = F_getresid("D_START");
76 d_end = F_getresid("D_END");
77 m_start = F_getresid("M_START");
78 m_end = F_getresid("M_END");
79 s_start = F_getresid("S_START");
80 s_end = F_getresid("S_END");
81 }
83 void F_loadres (int r, void *p) {
84 WADRES_getdata(r, p);
85 }
87 int F_findres (const char n[8]) {
88 return WADRES_find(n);
89 }
91 int F_getresid (const char n[8]) {
92 int i = F_findres(n);
93 if (i == -1) {
94 ERR_fatal("F_getresid: resource %.8s not found", n);
95 }
96 return i;
97 }
99 void F_getresname (char n[8], int r) {
100 WADRES_getname(r, n);
103 int F_getsprid (const char n[4], int s, int d, char *dir) {
104 s += 'A';
105 d += '0';
106 for (int i = s_start + 1; i < s_end; i++) {
107 char wn[8];
108 byte a, b;
109 WADRES_getname(i, wn);
110 if (cp866_strncasecmp(wn, n, 4) == 0 && (wn[4] == s || wn[6] == s)) {
111 a = wn[4] == s ? wn[5] : 0;
112 b = wn[6] == s ? wn[7] : 0;
113 if (a == '0' || b == '0' || a == d || b == d) {
114 if (dir != NULL) {
115 *dir = (a != '0' && b == '0') || (a != d && b == d);
117 return i;
121 ERR_fatal("F_getsprid: image %.4s%c%c not found", n, s, d);
122 return -1;
125 int F_getreslen (int r) {
126 return WADRES_getsize(r);
129 void F_nextmus (char *s) {
130 int i = F_findres(s);
131 if (i <= m_start || i >= m_end) {
132 i = m_start;
134 for (++i; ; ++i) {
135 if (i >= m_end) {
136 i = m_start + 1;
138 WADRES_getname(i, s);
139 if (cp866_strcasecmp(s, "MENU") == 0 ||
140 cp866_strcasecmp(s, "INTERMUS") == 0 ||
141 cp866_strcasecmp(s, "\x8a\x8e\x8d\x85\x96\x0") == 0) {
142 continue;
144 if (cp866_strncasecmp(s, "DMI", 3) != 0) {
145 break;
150 void F_randmus (char *s) {
151 int i;
152 int n = myrand(10);
153 for (i = 0; i < n; i++) {
154 F_nextmus(s);
158 void F_loadmap (char n[8]) {
159 int id = F_getresid(n);
160 if (id != -1) {
161 Stream *r = WADRES_getbasereader(id);
162 long offset = WADRES_getoffset(id);
163 stream_setpos(r, offset);
164 if (!MAP_load(r)) {
165 ERR_fatal("Failed to load map");
167 } else {
168 ERR_fatal("Failed to load map: resource %.8s not found", n);
172 static char *getsavfpname (int n, int ro) {
173 static char fn[] = "savgame0.dat";
174 static char p[100];
175 fn[7] = n + '0';
176 #ifdef UNIX
177 char *e = getenv("HOME");
178 strncpy(p, e, 60);
179 strcat(p, "/.flatwaifu");
180 if (!ro) {
181 mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
183 strcat(p, "/");
184 strcat(p, fn);
185 #else
186 strcpy(p, fn);
187 #endif
188 return p;
191 void F_getsavnames (void) {
192 FILE_Stream rd;
193 for (int i = 0; i < SAVE_MAX; ++i) {
194 savok[i] = 0;
195 char *p = getsavfpname(i, 1);
196 if (FILE_Open(&rd, p, "rb")) {
197 savok[i] = SAVE_getname(&rd.base, savname[i]);
198 FILE_Close(&rd);
200 if (!savok[i]) {
201 memset(savname[i], 0, 24);
202 } else {
203 savname[i][23] = 0;
208 void F_loadgame (int n) {
209 FILE_Stream rd;
210 char *p = getsavfpname(n, 1);
211 if (FILE_Open(&rd, p, "rb")) {
212 SAVE_load(&rd.base);
213 FILE_Close(&rd);
217 void F_savegame (int n, char *s) {
218 FILE_Stream wr;
219 char *p = getsavfpname(n, 0);
220 if (FILE_Open(&wr, p, "wb")) {
221 SAVE_save(&wr.base, s);
222 FILE_Close(&wr);