1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
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.
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.
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/>.
24 # include <sys/stat.h>
42 #include "common/streams.h"
43 #include "common/files.h"
44 #include "common/wadres.h"
47 static int m_start
, m_end
;
48 static int s_start
, s_end
;
50 void F_startup (void) {
51 logo("F_startup: setup file system\n");
54 void F_addwad (const char *fn
) {
56 static FILE_Reader wadh
[MAX_WADS
];
58 if (FILE_OpenReader(&wadh
[i
], fn
)) {
59 if (WADRES_addwad(&wadh
[i
].base
)) {
62 ERR_failinit("Invalid WAD %s", fn
);
65 ERR_failinit("Unable to add WAD %s", fn
);
68 ERR_failinit("Too many wads");
72 void F_initwads (void) {
73 if (!WADRES_rehash()) {
74 ERR_failinit("F_initwads: failed rehash");
78 void F_allocres (void) {
79 d_start
= F_getresid("D_START");
80 d_end
= F_getresid("D_END");
81 m_start
= F_getresid("M_START");
82 m_end
= F_getresid("M_END");
83 s_start
= F_getresid("S_START");
84 s_end
= F_getresid("S_END");
87 void F_loadres (int r
, void *p
) {
91 int F_findres (const char n
[8]) {
92 return WADRES_find(n
);
95 int F_getresid (const char n
[8]) {
98 ERR_fatal("F_getresid: resource %.8s not found", n
);
103 void F_getresname (char n
[8], int r
) {
104 WADRES_getname(r
, n
);
107 int F_getsprid (const char n
[4], int s
, int d
, char *dir
) {
110 for (int i
= s_start
+ 1; i
< s_end
; i
++) {
113 WADRES_getname(i
, wn
);
114 if (cp866_strncasecmp(wn
, n
, 4) == 0 && (wn
[4] == s
|| wn
[6] == s
)) {
115 a
= wn
[4] == s
? wn
[5] : 0;
116 b
= wn
[6] == s
? wn
[7] : 0;
117 if (a
== '0' || b
== '0' || a
== d
|| b
== d
) {
119 *dir
= (a
!= '0' && b
== '0') || (a
!= d
&& b
== d
);
125 ERR_fatal("F_getsprid: image %.4s%c%c not found", n
, s
, d
);
129 int F_getreslen (int r
) {
130 return WADRES_getsize(r
);
133 void F_nextmus (char *s
) {
134 int i
= F_findres(s
);
135 if (i
<= m_start
|| i
>= m_end
) {
142 WADRES_getname(i
, s
);
143 if (cp866_strcasecmp(s
, "MENU") == 0 ||
144 cp866_strcasecmp(s
, "INTERMUS") == 0 ||
145 cp866_strcasecmp(s
, "\x8a\x8e\x8d\x85\x96\x0") == 0) {
148 if (cp866_strncasecmp(s
, "DMI", 3) != 0) {
154 void F_randmus (char *s
) {
157 for (i
= 0; i
< n
; i
++) {
162 // reads bytes from file until CR
163 void F_readstr (FILE* h
, char *s
, int m
) {
169 len
= myfreadc(&c
, 1, 1, h
);
170 if (len
== 0 || c
== 13 || c
== 10) {
179 void F_loadmap (char n
[8]) {
180 int id
= F_getresid(n
);
182 Reader
*r
= WADRES_getbasereader(id
);
183 long offset
= WADRES_getoffset(id
);
184 r
->setpos(r
, offset
);
186 ERR_fatal("Failed to load map");
189 ERR_fatal("Failed to load map: resource %.8s not found", n
);