DEADSOFTWARE

deac9e3e2898da9726eeb48a4bc84d8d36b59ec8
[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"
25 #include "cp866.h"
27 #include "map.h" // MAP_load
29 #include "common/streams.h"
30 #include "common/files.h"
31 #include "common/wadres.h"
33 int d_start, d_end;
34 static int m_start, m_end;
35 static int s_start, s_end;
37 void F_startup (void) {
38 logo("F_startup: setup file system\n");
39 }
41 void F_addwad (const char *fn) {
42 static int i = 0;
43 static FILE_Stream wadh[MAX_WADS];
44 if (i < MAX_WADS) {
45 if (FILE_Open(&wadh[i], fn, "rb")) {
46 if (WADRES_addwad(&wadh[i].base)) {
47 i += 1;
48 } else {
49 ERR_failinit("Invalid WAD %s", fn);
50 }
51 } else {
52 ERR_failinit("Unable to add WAD %s", fn);
53 }
54 } else {
55 ERR_failinit("Too many wads");
56 }
57 }
59 void F_initwads (void) {
60 if (!WADRES_rehash()) {
61 ERR_failinit("F_initwads: failed rehash");
62 }
63 }
65 void F_allocres (void) {
66 d_start = F_getresid("D_START");
67 d_end = F_getresid("D_END");
68 m_start = F_getresid("M_START");
69 m_end = F_getresid("M_END");
70 s_start = F_getresid("S_START");
71 s_end = F_getresid("S_END");
72 }
74 void F_loadres (int r, void *p) {
75 WADRES_getdata(r, p);
76 }
78 int F_findres (const char n[8]) {
79 return WADRES_find(n);
80 }
82 int F_getresid (const char n[8]) {
83 int i = F_findres(n);
84 if (i == -1) {
85 ERR_fatal("F_getresid: resource %.8s not found", n);
86 }
87 return i;
88 }
90 void F_getresname (char n[8], int r) {
91 WADRES_getname(r, n);
92 }
94 int F_getsprid (const char n[4], int s, int d, char *dir) {
95 s += 'A';
96 d += '0';
97 for (int i = s_start + 1; i < s_end; i++) {
98 char wn[8];
99 byte a, b;
100 WADRES_getname(i, wn);
101 if (cp866_strncasecmp(wn, n, 4) == 0 && (wn[4] == s || wn[6] == s)) {
102 a = wn[4] == s ? wn[5] : 0;
103 b = wn[6] == s ? wn[7] : 0;
104 if (a == '0' || b == '0' || a == d || b == d) {
105 if (dir != NULL) {
106 *dir = (a != '0' && b == '0') || (a != d && b == d);
108 return i;
112 ERR_fatal("F_getsprid: image %.4s%c%c not found", n, s, d);
113 return -1;
116 int F_getreslen (int r) {
117 return WADRES_getsize(r);
120 void F_nextmus (char *s) {
121 int i = F_findres(s);
122 if (i <= m_start || i >= m_end) {
123 i = m_start;
125 for (++i; ; ++i) {
126 if (i >= m_end) {
127 i = m_start + 1;
129 WADRES_getname(i, s);
130 if (cp866_strcasecmp(s, "MENU") == 0 ||
131 cp866_strcasecmp(s, "INTERMUS") == 0 ||
132 cp866_strcasecmp(s, "\x8a\x8e\x8d\x85\x96\x0") == 0) {
133 continue;
135 if (cp866_strncasecmp(s, "DMI", 3) != 0) {
136 break;
141 void F_randmus (char *s) {
142 int i;
143 int n = myrand(10);
144 for (i = 0; i < n; i++) {
145 F_nextmus(s);
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);