DEADSOFTWARE

files: add F_getsprid description
[flatwaifu.git] / src / sdl / 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_addwad (const char *fn) {
47 static int i = 0;
48 static FILE_Stream wadh[MAX_WADS];
49 if (i < MAX_WADS) {
50 if (FILE_Open(&wadh[i], fn, "rb")) {
51 if (WADRES_addwad(&wadh[i].base)) {
52 i += 1;
53 } else {
54 ERR_failinit("Invalid WAD %s", fn);
55 }
56 } else {
57 ERR_failinit("Unable to add WAD %s", fn);
58 }
59 } else {
60 ERR_failinit("Too many wads");
61 }
62 }
64 void F_initwads (void) {
65 if (!WADRES_rehash()) {
66 ERR_failinit("F_initwads: failed rehash");
67 }
68 d_start = F_getresid("D_START");
69 d_end = F_getresid("D_END");
70 m_start = F_getresid("M_START");
71 m_end = F_getresid("M_END");
72 s_start = F_getresid("S_START");
73 s_end = F_getresid("S_END");
74 }
76 int F_findres (const char n[8]) {
77 return WADRES_find(n);
78 }
80 int F_getresid (const char n[8]) {
81 int i = F_findres(n);
82 if (i == -1) {
83 ERR_fatal("F_getresid: resource %.8s not found", n);
84 }
85 return i;
86 }
88 void F_getresname (char n[8], int r) {
89 WADRES_getname(r, n);
90 }
92 int F_getsprid (const char n[4], int s, int d, char *dir) {
93 s += 'A';
94 d += '0';
95 for (int i = s_start + 1; i < s_end; i++) {
96 char wn[8];
97 byte a, b;
98 WADRES_getname(i, wn);
99 if (cp866_strncasecmp(wn, n, 4) == 0 && (wn[4] == s || wn[6] == s)) {
100 a = wn[4] == s ? wn[5] : 0;
101 b = wn[6] == s ? wn[7] : 0;
102 if (a == '0' || b == '0' || a == d || b == d) {
103 if (dir != NULL) {
104 *dir = (a != '0' && b == '0') || (a != d && b == d);
106 return i;
110 ERR_fatal("F_getsprid: image %.4s%c%c not found", n, s, d);
111 return -1;
114 int F_getreslen (int r) {
115 return WADRES_getsize(r);
118 /*
119 void F_nextmus (char *s) {
120 int i = F_findres(s);
121 if (i <= m_start || i >= m_end) {
122 i = m_start;
124 for (++i; ; ++i) {
125 if (i >= m_end) {
126 i = m_start + 1;
128 WADRES_getname(i, s);
129 if (cp866_strcasecmp(s, "MENU") == 0 ||
130 cp866_strcasecmp(s, "INTERMUS") == 0 ||
131 cp866_strcasecmp(s, "\x8a\x8e\x8d\x85\x96\x0") == 0) {
132 continue;
134 if (cp866_strncasecmp(s, "DMI", 3) != 0) {
135 break;
140 void F_randmus (char *s) {
141 int i;
142 int n = myrand(10);
143 for (i = 0; i < n; i++) {
144 F_nextmus(s);
147 */
149 void F_loadmap (char n[8]) {
150 int id = F_getresid(n);
151 if (id != -1) {
152 Stream *r = WADRES_getbasereader(id);
153 long offset = WADRES_getoffset(id);
154 stream_setpos(r, offset);
155 if (!MAP_load(r)) {
156 ERR_fatal("Failed to load map");
158 } else {
159 ERR_fatal("Failed to load map: resource %.8s not found", n);
163 static char *getsavfpname (int n, int ro) {
164 static char fn[] = "savgame0.dat";
165 static char p[100];
166 fn[7] = n + '0';
167 #ifdef UNIX
168 char *e = getenv("HOME");
169 strncpy(p, e, 60);
170 strcat(p, "/.flatwaifu");
171 if (!ro) {
172 mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
174 strcat(p, "/");
175 strcat(p, fn);
176 #else
177 strcpy(p, fn);
178 #endif
179 return p;
182 void F_getsavnames (void) {
183 FILE_Stream rd;
184 for (int i = 0; i < SAVE_MAX; ++i) {
185 savok[i] = 0;
186 char *p = getsavfpname(i, 1);
187 if (FILE_Open(&rd, p, "rb")) {
188 savok[i] = SAVE_getname(&rd.base, savname[i]);
189 FILE_Close(&rd);
191 if (!savok[i]) {
192 memset(savname[i], 0, 24);
193 } else {
194 savname[i][23] = 0;
199 void F_loadgame (int n) {
200 FILE_Stream rd;
201 char *p = getsavfpname(n, 1);
202 if (FILE_Open(&rd, p, "rb")) {
203 SAVE_load(&rd.base);
204 FILE_Close(&rd);
208 void F_savegame (int n, char *s) {
209 FILE_Stream wr;
210 char *p = getsavfpname(n, 0);
211 if (FILE_Open(&wr, p, "wb")) {
212 SAVE_save(&wr.base, s);
213 FILE_Close(&wr);