Skip to content

Commit 42b15eb

Browse files
committed
swars: Remade open_file_from_wad()
1 parent 8fe7a60 commit 42b15eb

File tree

2 files changed

+75
-29
lines changed

2 files changed

+75
-29
lines changed

src/swars.sx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183638,15 +183638,6 @@ ascB: /* 0x15BF1F */
183638183638
aSMap03d_bB: /* 0x15BF24 */
183639183639
.string "%s/map%03d.b&b"
183640183640

183641-
qdata_keys_dat_2:
183642-
.string "qdata/keys.dat"
183643-
183644-
aSyndicate02d02: /* 0x15C035 */
183645-
.string "SYNDICATE %02d:%02d:%02d NC"
183646-
aChurch02d02d02: /* 0x15C051 */
183647-
.string "CHURCH %02d:%02d:%02d NC"
183648-
aUnguided02d02d: /* 0x15C06A */
183649-
.string "UNGUIDED %02d:%02d:%02d NC"
183650183641
aS_idx: /* 0x15C085 */
183651183642
.string "%s.idx"
183652183643
aS_wad: /* 0x15C08C */

src/wadfile.c

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,9 @@
2929
#include "swlog.h"
3030
/******************************************************************************/
3131

32-
TbFileHandle open_file_from_wad(const char *filename, const char *wadfile)
33-
{
34-
TbFileHandle ret;
35-
asm volatile ("call ASM_open_file_from_wad\n"
36-
: "=r" (ret) : "a" (filename), "d" (wadfile));
37-
return ret;
38-
}
39-
40-
int load_file_wad(const char *filename, const char *wadfile, void *outbuf)
32+
TbResult wadfile_format_entfile_name(char *entfile, const char *filename)
4133
{
42-
char locfname[DISKPATH_SIZE];
43-
char locstr[64];
44-
struct WADIndexEntry fentry;
4534
const char *only_fname;
46-
TbFileHandle fh;
47-
long nread;
4835
int i;
4936

5037
only_fname = strrchr(filename, '/');
@@ -55,29 +42,97 @@ int load_file_wad(const char *filename, const char *wadfile, void *outbuf)
5542

5643
for (i = 0; only_fname[i] != '\0'; i++)
5744
{
58-
locstr[i] = toupper(only_fname[i]);
45+
// Does not make sense to format WAD entry name longer than size within index
46+
if (i >= (int)sizeof(((struct WADIndexEntry *)0)->Filename)) {
47+
entfile[i-1] = '\0';
48+
return Lb_FAIL;
49+
}
50+
entfile[i] = toupper(only_fname[i]);
5951
}
60-
locstr[i] = '\0';
52+
entfile[i] = '\0';
53+
54+
return Lb_SUCCESS;
55+
}
56+
57+
TbResult wadfile_find_index_entry(struct WADIndexEntry *fentry, const char *wadfile, const char *entfile)
58+
{
59+
char locfname[DISKPATH_SIZE];
60+
TbFileHandle fh;
61+
int nread;
6162

6263
sprintf(locfname, "%s.idx", wadfile);
6364
fh = LbFileOpen(locfname, Lb_FILE_MODE_READ_ONLY);
6465
if (fh == INVALID_FILE)
65-
return -1;
66+
return Lb_FAIL;
67+
6668
do {
67-
nread = LbFileRead(fh, &fentry, sizeof(struct WADIndexEntry));
68-
} while ((strcmp(locstr, fentry.Filename) != 0) &&
69+
nread = LbFileRead(fh, fentry, sizeof(struct WADIndexEntry));
70+
} while ((strcmp(entfile, fentry->Filename) != 0) &&
6971
(nread == sizeof(struct WADIndexEntry)));
72+
7073
LbFileClose(fh);
7174

7275
if (nread != sizeof(struct WADIndexEntry))
76+
return Lb_FAIL;
77+
78+
return Lb_SUCCESS;
79+
}
80+
81+
TbFileHandle open_file_from_wad(const char *filename, const char *wadfile)
82+
{
83+
#if 0
84+
TbFileHandle ret;
85+
asm volatile ("call ASM_open_file_from_wad\n"
86+
: "=r" (ret) : "a" (filename), "d" (wadfile));
87+
return ret;
88+
#else
89+
char locfname[DISKPATH_SIZE];
90+
char locstr[64];
91+
struct WADIndexEntry fentry;
92+
TbFileHandle fh;
93+
TbResult ret;
94+
95+
ret = wadfile_format_entfile_name(locstr, filename);
96+
if (ret != Lb_SUCCESS)
97+
return INVALID_FILE;
98+
99+
ret = wadfile_find_index_entry(&fentry, wadfile, locstr);
100+
if (ret != Lb_SUCCESS)
101+
return INVALID_FILE;
102+
103+
sprintf(locfname, "%s.wad", wadfile);
104+
fh = LbFileOpen(locfname, Lb_FILE_MODE_READ_ONLY);
105+
if (fh == INVALID_FILE)
106+
return fh;
107+
108+
LbFileSeek(fh, fentry.Offset, Lb_FILE_SEEK_BEGINNING);
109+
return fh;
110+
#endif
111+
}
112+
113+
int load_file_wad(const char *filename, const char *wadfile, void *outbuf)
114+
{
115+
char locfname[DISKPATH_SIZE];
116+
char locstr[64];
117+
struct WADIndexEntry fentry;
118+
TbFileHandle fh;
119+
TbResult ret;
120+
long nread;
121+
122+
ret = wadfile_format_entfile_name(locstr, filename);
123+
if (ret != Lb_SUCCESS)
124+
return -1;
125+
126+
ret = wadfile_find_index_entry(&fentry, wadfile, locstr);
127+
if (ret != Lb_SUCCESS)
73128
return -1;
74129

75130
sprintf(locfname, "%s.wad", wadfile);
76131
fh = LbFileOpen(locfname, Lb_FILE_MODE_READ_ONLY);
77132
if (fh == INVALID_FILE)
78133
return -1;
79134

80-
LbFileSeek(fh, fentry.Offset, 0);
135+
LbFileSeek(fh, fentry.Offset, Lb_FILE_SEEK_BEGINNING);
81136
nread = LbFileRead(fh, outbuf, fentry.Length);
82137
LbFileClose(fh);
83138
return nread;

0 commit comments

Comments
 (0)