DEADSOFTWARE

headers describes that c-files implements
[flatwaifu.git] / src / config.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <SDL_keyboard.h>
28 #include "map.h"
29 #include "sound.h"
30 #include "music.h"
31 #include "view.h"
32 #include "monster.h"
33 #include "player.h"
34 #include "menu.h"
35 #include "files.h"
36 #include "render.h"
37 #include "error.h"
38 #include "my.h"
40 enum{NONE,BYTE,WORD,DWORD,STRING,SW_ON,SW_OFF,FILES,KEY};
42 typedef struct{
43 char *par,*cfg;
44 void *p;
45 byte t,o;
46 }cfg_t;
48 byte cheat=0;
49 byte shot_vga=0;
50 char cd_path[128]="";
51 char cfg_file[128]="default.cfg";
53 static char buf[256];
55 static cfg_t cfg[]={
56 {"file",NULL,NULL,FILES,0},
57 {"cheat",NULL,&cheat,SW_ON,0},
58 {"vga","screenshot",&shot_vga,SW_ON,0},
59 {"sndvol","sound_volume",&snd_vol,WORD,0},
60 {"musvol","music_volume",&mus_vol,WORD,0},
61 // {"fullscr","fullscreen",&fullscreen,SW_ON,0},
62 // {"window",NULL,&fullscreen,SW_OFF,0},
63 {NULL,"sky",&w_horiz,SW_ON,0},
64 {"mon",NULL,&nomon,SW_OFF,0},
65 // {"gamma","gamma",&gammaa,DWORD,0},
66 {"warp",NULL,&_warp,BYTE,0},
67 {"width","screen_width",&SCRW,DWORD,0},
68 {"height","screen_height",&SCRH,DWORD,0},
69 {NULL,"music_random",&music_random,SW_ON,0},
70 {NULL,"music_time",&music_time,DWORD,0},
71 {NULL,"music_fade",&music_fade,DWORD,0},
72 {NULL,"pl1_left", &pl1.kl,KEY,0},
73 {NULL,"pl1_right",&pl1.kr,KEY,0},
74 {NULL,"pl1_up", &pl1.ku,KEY,0},
75 {NULL,"pl1_down", &pl1.kd,KEY,0},
76 {NULL,"pl1_jump", &pl1.kj,KEY,0},
77 {NULL,"pl1_fire", &pl1.kf,KEY,0},
78 {NULL,"pl1_next", &pl1.kwr,KEY,0},
79 {NULL,"pl1_prev", &pl1.kwl,KEY,0},
80 {NULL,"pl1_use", &pl1.kp,KEY,0},
81 {NULL,"pl2_left", &pl2.kl,KEY,0},
82 {NULL,"pl2_right",&pl2.kr,KEY,0},
83 {NULL,"pl2_up", &pl2.ku,KEY,0},
84 {NULL,"pl2_down", &pl2.kd,KEY,0},
85 {NULL,"pl2_jump", &pl2.kj,KEY,0},
86 {NULL,"pl2_fire", &pl2.kf,KEY,0},
87 {NULL,"pl2_next", &pl2.kwr,KEY,0},
88 {NULL,"pl2_prev", &pl2.kwl,KEY,0},
89 {NULL,"pl2_use", &pl2.kp,KEY,0},
90 {"config",NULL,cfg_file,STRING,0},
91 {NULL,NULL,NULL,NONE,0}
92 };
94 void CFG_args(int argc, char *argv[]) {
95 int j;
96 dword n;
97 char *s;
99 logo("CFG_args: проверка командной строки\n");
101 int i;
102 char *pbuf = buf;
103 for (i=1;i<argc;i++){
104 strcpy(pbuf,argv[i]);
105 pbuf+=strlen(argv[i]);
106 strcpy(pbuf," ");
107 pbuf++;
110 for(s=strtok(buf," \r\n\t");s;s=strtok(NULL," \r\n\t")) {//for(s=strtok(getcmd(buf)," \r\n\t");s;s=strtok(NULL," \r\n\t")) {
111 next:
112 if(*s=='/' || *s=='-') ++s;
113 for(j=0;cfg[j].t;++j) if(cfg[j].par) if(strcasecmp(s,cfg[j].par)==0) {
114 switch(cfg[j].t) {
115 case BYTE:
116 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
117 *((byte *)cfg[j].p)=(byte)n;
118 break;
119 case WORD:
120 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
121 *((word *)cfg[j].p)=(word)n;
122 break;
123 case DWORD:
124 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
125 *((dword *)cfg[j].p)=n;
126 break;
127 case STRING:
128 strcpy((char *)cfg[j].p,s=strtok(NULL," \r\n\t"));
129 break;
130 case SW_ON:
131 *((byte *)cfg[j].p)=ON;
132 if(cfg[j+1].t==SW_OFF && cfg[j+1].p==cfg[j].p) cfg[j+1].o=1;
133 if(j>0) if(cfg[j-1].t==SW_OFF && cfg[j-1].p==cfg[j].p) cfg[j-1].o=1;
134 break;
135 case SW_OFF:
136 *((byte *)cfg[j].p)=OFF;
137 if(cfg[j+1].t==SW_ON && cfg[j+1].p==cfg[j].p) cfg[j+1].o=1;
138 if(j>0) if(cfg[j-1].t==SW_ON && cfg[j-1].p==cfg[j].p) cfg[j-1].o=1;
139 break;
140 case FILES:
141 for(s=strtok(NULL," \r\n\t");s;s=strtok(NULL," \r\n\t")) {
142 if(*s=='/' || *s=='-') goto next;
143 #ifdef DEMO
144 logo(" %s НЕ подключен!\n",s);
145 #else
146 F_addwad(s);
147 #endif
148 }break;
149 default:
150 ERR_failinit("!!! Неизвестный тип в cfg !!!");
152 cfg[j].o=1;break;
157 static int get_key (char *name) {
158 int i;
159 for(i=1; i<SDLK_LAST; i++) {
160 char* s = SDL_GetKeyName(i);
161 if (s && strcasecmp(name,s) == 0) {
163 return i;
166 return 0;
169 void CFG_load(void) {
170 int j;
171 FILE *h;
172 dword n;
173 char s[128];
174 char *p1,*p2;
176 char pc[50];
177 char *e = getenv("HOME");
178 strncpy(pc, e, 30);
179 strcpy(&pc[strlen(pc)], "/default.cfg");
180 if (!fexists(pc)) {
181 strcpy(pc, "default.cfg");
182 if (!fexists(pc)) {
183 strcpy(pc, "/usr/share/doom2d-rembo/default.cfg");
184 if (!fexists(pc)) {
185 logo("default.cfg not found\n");
186 return;
191 logo("CFG_load: загрузка конфигурации из %s\n",pc);
192 if((h=fopen(pc,"rb"))==NULL) {
193 perror("Cannot open file");return;
195 while(!feof(h)) {
196 F_readstr(h,s,127);
197 if(*s==';' || s[1]==';')
198 continue; // comment
199 if(!(p1=strtok(s,"\r\n\t=;")))
200 continue; //if(!(p1=strtok(s,"\r\n\t =;"))) continue;
201 if(!(p2=strtok(NULL,"\r\n\t=;")))
202 continue;//if(!(p2=strtok(NULL,"\r\n\t =;"))) continue;
203 for(j=0;cfg[j].t;++j) {
204 if(cfg[j].cfg && !cfg[j].o) {
205 if(strcasecmp(p1,cfg[j].cfg)==0) {
206 switch(cfg[j].t) {
207 case BYTE:
208 n=strtol(p2,NULL,0);
209 *((byte *)cfg[j].p)=(byte)n;
210 break;
211 case WORD:
212 n=strtol(p2,NULL,0);
213 *((word *)cfg[j].p)=(word)n;
214 break;
215 case DWORD:
216 n=strtol(p2,NULL,0);
217 *((dword *)cfg[j].p)=n;
218 break;
219 case STRING:
220 strcpy((char *)cfg[j].p,p2);
221 break;
222 case SW_ON:
223 case SW_OFF:
224 if(strcasecmp(p2,"ON")==0) {
225 *((byte *)cfg[j].p)=ON;
226 break;
228 if(strcasecmp(p2,"OFF")==0) {
229 *((byte *)cfg[j].p)=OFF;
230 break;
232 *((byte *)cfg[j].p)=strtol(p2,NULL,0);
233 break;
234 case FILES:
235 break;
236 case KEY:
238 int k = get_key(p2);
239 if (k) {
240 *((int *)cfg[j].p)=k;
241 } else {
242 logo("Unknown key in cfg: %s=%s\n",p1,p2);
243 logo("List available key names:\n");
244 int i;
245 for(i=1; i<SDLK_LAST; i++) {
246 char* s = SDL_GetKeyName(i);
248 if (!strcasecmp(s,"unknown key") == 0) {
249 logo("%s\n", s);
254 break;
255 default:
256 ERR_failinit("!!! Неизвестный тип в cfg !!!");
257 } // switch
258 break;
259 } // if
260 } // if
261 } // for
262 } // while
263 fclose(h);
266 void CFG_save(void) {
267 /*
268 char s[140],str[140];
269 char *p;
270 FILE *h,*oh;
272 remove("CONFIG.ZZZ");
273 if(rename(cfg_file,"CONFIG.ZZZ")) return;
274 if(!(h=fopen("CONFIG.ZZZ","rt")))
275 {rename("CONFIG.ZZZ",cfg_file);return;}
276 if(!(oh=fopen(cfg_file,"wt")))
277 {fclose(h);rename("CONFIG.ZZZ",cfg_file);return;}
278 for(;;) {
279 if(!fgets(s,128,h)) break;
280 strcpy(str,s);
281 if(!(p=strtok(str,"\r\n\t =;"))) {fprintf(oh,"%s",s);continue;}
282 if(strcasecmp(p,"sound_volume")==0)
283 sprintf(s,"sound_volume=%d\n",snd_vol);
284 else if(strcasecmp(p,"music_volume")==0)
285 sprintf(s,"music_volume=%d\n",mus_vol);
286 else if(strcasecmp(p,"gamma")==0)
287 sprintf(s,"gamma=%d\n",gammaa);
288 else if(strcasecmp(p,"sound_interp")==0)
289 sprintf(s,"sound_interp=%s\n",s_interp?"on":"off");
290 fprintf(oh,"%s",s);
292 fclose(oh);fclose(h);
293 remove("CONFIG.ZZZ");
294 */