X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Ffiles.c;h=2eab3a877e8a1cd07c4a1e82ada8ec1b6bf2b8a6;hb=3d9acf8660ae1f0148c8578b8e9ca168795deec4;hp=f6ef6b5303dd1e740d4875a436ee075581ecdf14;hpb=c46b82f5794be555af272740e67ead316b9b1f90;p=flatwaifu.git diff --git a/src/files.c b/src/files.c index f6ef6b5..2eab3a8 100644 --- a/src/files.c +++ b/src/files.c @@ -1,543 +1,224 @@ -/* - Copyright (C) Prikol Software 1996-1997 - Copyright (C) Aleksey Volynskov 1996-1997 - Copyright (C) 2011 - - This file is part of the Doom2D:Rembo project. - - Doom2D:Rembo is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 as - published by the Free Software Foundation. - - Doom2D:Rembo is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, see or - write to the Free Software Foundation, Inc., - 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ +/* Copyright (C) 1996-1997 Aleksey Volynskov + * Copyright (C) 2011 Rambo + * Copyright (C) 2020 SovietPony + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License ONLY. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include "glob.h" #include #include #include -#include -#include "error.h" -#include "view.h" -#include "items.h" -#include "switch.h" +#include #include "files.h" -#include "map.h" -#include "my.h" - -char *S_getinfo(void); - -extern void *snd_drv; - -typedef struct{ - byte n,i,v,d; -}dmv; - -byte seq[255],seqn; -dmv *pat=NULL; -unsigned *patp; -void **dmi; - -static int inum=0; +#include "error.h" -void G_savegame(FILE*); -void W_savegame(FILE*); -void DOT_savegame(FILE*); -void SMK_savegame(FILE*); -void FX_savegame(FILE*); -void IT_savegame(FILE*); -void MN_savegame(FILE*); -void PL_savegame(FILE*); -void SW_savegame(FILE*); -void WP_savegame(FILE*); +#include "map.h" // MAP_load +#include "save.h" // SAVE_getname -void G_loadgame(FILE*); -void W_loadgame(FILE*); -void DOT_loadgame(FILE*); -void SMK_loadgame(FILE*); -void FX_loadgame(FILE*); -void IT_loadgame(FILE*); -void MN_loadgame(FILE*); -void PL_loadgame(FILE*); -void SW_loadgame(FILE*); -void WP_loadgame(FILE*); +#ifdef UNIX +# include +#endif -byte savname[7][24],savok[7]; +#include "common/streams.h" +#include "common/files.h" +#include "common/wadres.h" +#include "common/cp866.h" -int d_start,d_end,m_start,m_end,s_start,s_end,wad_num; -mwad_t wad[MAX_WAD]; +int d_start, d_end; -char wads[MAX_WADS][__MAX_PATH]; -static FILE* wadh[MAX_WADS]; +char savname[SAVE_MAX][SAVE_MAXLEN]; +char savok[SAVE_MAX]; -char f_drive[__MAX_DRIVE],f_dir[__MAX_DIR],f_name[__MAX_FNAME],f_ext[__MAX_EXT], - f_path[__MAX_PATH]; +static int m_start, m_end; +static int s_start, s_end; -void F_startup(void) { - logo("F_startup: настройка файловой системы\n"); - memset(wads,0,sizeof(wads)); +void F_startup (void) { + logo("F_startup: setup file system\n"); } -char *getsavfpname(int n, int ro) -{ - static char fn[]="savgame0.dat"; - fn[7]=n+'0'; -#ifndef WIN32 - static char p[100]; - char *e = getenv("HOME"); - strncpy(p,e,60); - strcat(p,"/.doom2d-rembo"); - if (!ro) mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - strcat(p,"/"); - strcat(p,fn); -#else - strcpy(p,fn); -#endif - return p; -} - -void F_getsavnames (void) { - int i; - FILE *h; - short ver; - char *p; - for (i = 0; i < 7; ++i) { - p = getsavfpname(i, 1); - memset(savname[i], 0, 24); - savok[i] = 0; - h = fopen(p, "rb"); - if (h != NULL) { - ver = -1; - myfread(savname[i], 24, 1, h); - ver = myfread16(h); - savname[i][23] = 0; - savok[i] = (ver == 3) ? 1 : 0; - fclose(h); +void F_addwad (const char *fn) { + static int i = 0; + static FILE_Stream wadh[MAX_WADS]; + if (i < MAX_WADS) { + if (FILE_Open(&wadh[i], fn, "rb")) { + if (WADRES_addwad(&wadh[i].base)) { + i += 1; + } else { + ERR_failinit("Invalid WAD %s", fn); + } + } else { + ERR_failinit("Unable to add WAD %s", fn); } + } else { + ERR_failinit("Too many wads"); } } -void F_savegame (int n, char *s) { - char *p = getsavfpname(n, 0); - FILE *h = fopen(p, "wb"); - if (h != NULL) { - myfwrite(s, 24, 1, h); // slot name - myfwrite16(3, h); // version - G_savegame(h); - W_savegame(h); - DOT_savegame(h); - SMK_savegame(h); - FX_savegame(h); - IT_savegame(h); - MN_savegame(h); - PL_savegame(h); - SW_savegame(h); - WP_savegame(h); - fclose(h); +void F_initwads (void) { + if (!WADRES_rehash()) { + ERR_failinit("F_initwads: failed rehash"); } } -void F_loadgame (int n) { - short ver; - char *p = getsavfpname(n, 1); - FILE *h = fopen(p, "rb"); - if (h != NULL) { - fseek(h, 24, SEEK_SET); // skip name - ver = myfread16(h); // version - if (ver == 3) { - G_loadgame(h); - W_loadgame(h); - DOT_loadgame(h); - SMK_loadgame(h); - FX_loadgame(h); - IT_loadgame(h); - MN_loadgame(h); - PL_loadgame(h); - SW_loadgame(h); - WP_loadgame(h); - } - fclose(h); - } +void F_allocres (void) { + d_start = F_getresid("D_START"); + d_end = F_getresid("D_END"); + m_start = F_getresid("M_START"); + m_end = F_getresid("M_END"); + s_start = F_getresid("S_START"); + s_end = F_getresid("S_END"); } -void F_addwad(char *fn) { - int i; - - for(i=0;i= MAX_WAD) { - ERR_failinit("Слишком много элементов WAD'а"); - } - wad[p].o = w.o; - wad[p].l = w.l; - memcpy(wad[p].n, w.n, 8); - wad[p].f = 0; - ++p; - } - - //fclose(h); - - for (i = 1; i < MAX_WADS; ++i) { - if (wads[i][0] != 0) { - logo(" подключается %s\n", wads[i]); - if ((wadh[i] = h = fopen(wads[i], "rb")) == NULL) { - ERR_failinit("Не могу открыть файл2: %s", wads[i]); - } - mysplitpath(wads[i], f_drive, f_dir, f_name, f_ext); - if (strcasecmp(f_ext, ".lmp") == 0) { - for (k = 0; k < MAX_WAD; ++k) { - if (strncasecmp(wad[k].n, f_name, 8) == 0) { - wad[k].o = 0; - wad[k].l = myfilelength(h); - wad[k].f = i; - break; - } - if (k >= MAX_WAD) { - if (p >= MAX_WAD) { - ERR_failinit("Слишком много элементов WAD'а"); - } - memset(wad[p].n, 0, 8); - strncpy(wad[p].n, f_name, 8); - wad[p].o = 0; - wad[p].l = myfilelength(h); - wad[p].f = i; - ++p; - } - } - - s[0] = '\0'; - myfread(s, 1, 4, h); - if (strncmp(s, "IWAD", 4) != 0 && strncmp(s, "PWAD", 4) != 0) { - ERR_failinit("Нет подписи IWAD или PWAD (2)"); - } - n = myfread32(h); // num - o = myfread32(h); // offset - fseek(h, o, SEEK_SET); - for (j = 0; j < n; ++j) { - w.o = myfread32(h); // offset - w.o = myfread32(h); // len - myfread(w.n, 1, 8, h); // name - for (k = 0; k < MAX_WAD; ++k) { - if (strncasecmp(wad[k].n, w.n, 8) == 0) { - wad[k].o = w.o; - wad[k].l = w.l; - wad[k].f = i; - break; - } - if (k >= MAX_WAD) { - if (p >= MAX_WAD) { - ERR_failinit("Слишком много элементов WAD'а"); - } - memcpy(wad[p].n, w.n, 8); - wad[p].o = w.o; - wad[p].l = w.l; - wad[p].f = i; - ++p; - } - } + return i; +} + +void F_getresname (char n[8], int r) { + WADRES_getname(r, n); +} + +int F_getsprid (const char n[4], int s, int d, char *dir) { + s += 'A'; + d += '0'; + for (int i = s_start + 1; i < s_end; i++) { + char wn[8]; + byte a, b; + WADRES_getname(i, wn); + if (cp866_strncasecmp(wn, n, 4) == 0 && (wn[4] == s || wn[6] == s)) { + a = wn[4] == s ? wn[5] : 0; + b = wn[6] == s ? wn[7] : 0; + if (a == '0' || b == '0' || a == d || b == d) { + if (dir != NULL) { + *dir = (a != '0' && b == '0') || (a != d && b == d); } + return i; } } - } - - wad_num = p; -} - -// allocate resources -// (called from M_startup) -void F_allocres(void) { - d_start=F_getresid("D_START"); - d_end=F_getresid("D_END"); - m_start=F_getresid("M_START"); - m_end=F_getresid("M_END"); - s_start=F_getresid("S_START"); - s_end=F_getresid("S_END"); -} - -// load resource -void F_loadres(int r,void *p,dword o,dword l) { - - int oo; - FILE *fh; - - - oo=ftell(fh=wadh[wad[r].f]); - - if(fseek(fh,wad[r].o+o,SEEK_SET)!=0) - ERR_fatal("Ошибка при чтении файла"); - - if((dword)myfreadc(p,1,l,fh)!=l) - ERR_fatal("Ошибка при загрузке ресурса %.8s",wad[r].n); - - fseek(fh,oo,SEEK_SET); - -} - - -void F_saveres(int r,void *p,dword o,dword l) { - - int oo; - FILE* fh; - oo=ftell(fh=wadh[wad[r].f]); - if(fseek(fh,wad[r].o+o,SEEK_SET)!=0) - ERR_fatal("Ошибка при чтении файла"); - myfwrite(p,l,1,fh); - fseek(fh,oo,SEEK_SET); - -} - -// get resource id -int F_getresid(char *n) { - int i; - - for(i=0;i= m_end) { + i = m_start; + } + for (++i; ; ++i) { + if (i >= m_end) { + i = m_start + 1; } - ERR_fatal("F_getsprid: изображение %.4s%c%c не найдено",n,(byte)s,(byte)d); - return -1; -} - -int F_getreslen(int r) { - return wad[r].l; + WADRES_getname(i, s); + if (cp866_strcasecmp(s, "MENU") == 0 || + cp866_strcasecmp(s, "INTERMUS") == 0 || + cp866_strcasecmp(s, "\x8a\x8e\x8d\x85\x96\x0") == 0) { + continue; + } + if (cp866_strncasecmp(s, "DMI", 3) != 0) { + break; + } + } } -void F_nextmus(char *s) { +void F_randmus (char *s) { int i; - i=F_findres(s); - if(i<=m_start || i>=m_end) i=m_start; - for(++i;;++i) { - if(i>=m_end) i=m_start+1; - - if (strcasecmp(wad[i].n,"MENU") == 0 || - strcasecmp(wad[i].n,"INTERMUS") == 0 || - strcasecmp(wad[i].n,"\x8a\x8e\x8d\x85\x96\x0") == 0) - continue; - - if(strncasecmp(wad[i].n,"DMI",3)!=0) break; + int n = myrand(10); + for (i = 0; i < n; i++) { + F_nextmus(s); } - memcpy(s,wad[i].n,8); } -void F_randmus(char *s) { - int n = myrand(10); - int i; - for (i=0; i