34cf4058 |
1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | * Mupen64plus - Config_nogui.cpp * |
3 | * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * |
4 | * Copyright (C) 2008 Tillin9 * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | * * |
11 | * This program is distributed in the hope that it will be useful, * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 | * GNU General Public License for more details. * |
15 | * * |
16 | * You should have received a copy of the GNU General Public License * |
17 | * along with this program; if not, write to the * |
18 | * Free Software Foundation, Inc., * |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
20 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
21 | |
22 | #include <errno.h> |
23 | #include <string.h> |
24 | #include <stdlib.h> |
25 | #include <unistd.h> |
26 | |
27 | #include "Config.h" |
28 | #include "gles2N64.h" |
29 | #include "RSP.h" |
30 | #include "Textures.h" |
31 | #include "OpenGL.h" |
32 | |
33 | #include "Config.h" |
34 | #include "Common.h" |
35 | |
36 | |
37 | Config config; |
38 | |
39 | struct Option |
40 | { |
41 | const char* name; |
42 | int* data; |
43 | const int initial; |
44 | }; |
45 | |
46 | |
47 | #define CONFIG_VERSION 2 |
48 | |
49 | Option configOptions[] = |
50 | { |
51 | {"#gles2n64 Graphics Plugin for N64", NULL, 0}, |
52 | {"#by Orkin / glN64 developers and Adventus.", NULL, 0}, |
53 | |
54 | {"config version", &config.version, 0}, |
55 | {"", NULL, 0}, |
56 | |
57 | {"#Window Settings:", NULL, 0}, |
58 | {"window xpos", &config.window.xpos, 0}, |
59 | {"window ypos", &config.window.ypos, 0}, |
60 | {"window width", &config.window.width, 800}, |
61 | {"window height", &config.window.height, 480}, |
62 | {"window refwidth", &config.window.refwidth, 800}, |
63 | {"window refheight", &config.window.refheight, 480}, |
64 | {"", NULL, 0}, |
65 | |
66 | {"#Framebuffer Settings:",NULL,0}, |
67 | // {"framebuffer enable", &config.framebuffer.enable, 0}, |
68 | {"framebuffer bilinear", &config.framebuffer.bilinear, 0}, |
69 | {"framebuffer width", &config.framebuffer.width, 400}, |
70 | {"framebuffer height", &config.framebuffer.height, 240}, |
71 | // {"framebuffer width", &config.framebuffer.width, 800}, |
72 | // {"framebuffer height", &config.framebuffer.height, 480}, |
73 | {"", NULL, 0}, |
74 | |
75 | {"#VI Settings:", NULL, 0}, |
76 | {"video force", &config.video.force, 0}, |
77 | {"video width", &config.video.width, 320}, |
78 | {"video height", &config.video.height, 240}, |
79 | {"", NULL, 0}, |
80 | |
81 | {"#Render Settings:", NULL, 0}, |
82 | {"enable fog", &config.enableFog, 0}, |
83 | {"enable primitive z", &config.enablePrimZ, 1}, |
84 | {"enable lighting", &config.enableLighting, 1}, |
85 | {"enable alpha test", &config.enableAlphaTest, 1}, |
86 | {"enable clipping", &config.enableClipping, 0}, |
87 | {"enable face culling", &config.enableFaceCulling, 1}, |
88 | {"enable noise", &config.enableNoise, 0}, |
89 | {"", NULL, 0}, |
90 | |
91 | {"#Texture Settings:", NULL, 0}, |
92 | {"texture 2xSAI", &config.texture.sai2x, 0}, |
93 | {"texture force bilinear", &config.texture.forceBilinear, 0}, |
94 | {"texture max anisotropy", &config.texture.maxAnisotropy, 0}, |
95 | {"texture use IA", &config.texture.useIA, 0}, |
96 | {"texture fast CRC", &config.texture.fastCRC, 1}, |
97 | {"texture pow2", &config.texture.pow2, 1}, |
98 | {"", NULL, 0}, |
99 | |
100 | {"#Frame skip:", NULL, 0}, |
101 | {"auto frameskip", &config.autoFrameSkip, 0}, |
102 | {"max frameskip", &config.maxFrameSkip, 0}, |
103 | {"target FPS", &config.targetFPS, 20}, |
104 | {"frame render rate", &config.frameRenderRate, 1}, |
105 | {"vertical sync", &config.verticalSync, 0}, |
106 | {"", NULL, 0}, |
107 | |
108 | {"#Other Settings:", NULL, 0}, |
109 | {"update mode", &config.updateMode, SCREEN_UPDATE_AT_VI_UPDATE }, |
110 | {"ignore offscreen rendering", &config.ignoreOffscreenRendering, 0}, |
111 | {"force screen clear", &config.forceBufferClear, 0}, |
112 | {"flip vertical", &config.screen.flipVertical, 0}, |
113 | // paulscode: removed from pre-compile to a config option |
114 | //// (part of the Galaxy S Zelda crash-fix |
115 | {"tribuffer opt", &config.tribufferOpt, 1}, |
116 | // |
117 | {"", NULL, 0}, |
118 | |
119 | {"#Hack Settings:", NULL, 0}, |
120 | {"hack banjo tooie", &config.hackBanjoTooie, 0}, |
121 | {"hack zelda", &config.hackZelda, 0}, |
122 | {"hack alpha", &config.hackAlpha, 0}, |
123 | {"hack z", &config.zHack, 0}, |
124 | |
125 | }; |
126 | |
127 | const int configOptionsSize = sizeof(configOptions) / sizeof(Option); |
128 | |
129 | void Config_WriteConfig(const char *filename) |
130 | { |
131 | config.version = CONFIG_VERSION; |
132 | FILE* f = fopen(filename, "w"); |
133 | if (!f) |
134 | { |
135 | LOG(LOG_ERROR, "Could Not Open %s for writing\n", filename); |
136 | } |
137 | |
138 | for(int i=0; i<configOptionsSize; i++) |
139 | { |
140 | Option *o = &configOptions[i]; |
141 | fprintf(f, "%s", o->name); |
142 | if (o->data) fprintf(f,"=%i", *(o->data)); |
143 | fprintf(f, "\n"); |
144 | } |
145 | |
146 | |
147 | fclose(f); |
148 | } |
149 | |
150 | void Config_SetDefault() |
151 | { |
152 | for(int i=0; i < configOptionsSize; i++) |
153 | { |
154 | Option *o = &configOptions[i]; |
155 | if (o->data) *(o->data) = o->initial; |
156 | } |
157 | } |
158 | |
159 | void Config_SetOption(char* line, char* val) |
160 | { |
161 | for(int i=0; i< configOptionsSize; i++) |
162 | { |
163 | Option *o = &configOptions[i]; |
164 | if (strcasecmp(line, o->name) == 0) |
165 | { |
166 | if (o->data) |
167 | { |
168 | int v = atoi(val); |
169 | *(o->data) = v; |
170 | LOG(LOG_VERBOSE, "Config Option: %s = %i\n", o->name, v); |
171 | } |
172 | break; |
173 | } |
174 | } |
175 | } |
176 | |
177 | void Config_LoadRomConfig(unsigned char* header) |
178 | { |
179 | char line[4096]; |
180 | |
181 | // get the name of the ROM |
182 | for (int i=0; i<20; i++) config.romName[i] = header[0x20+i]; |
183 | config.romName[20] = '\0'; |
184 | while (config.romName[strlen(config.romName)-1] == ' ') |
185 | { |
186 | config.romName[strlen(config.romName)-1] = '\0'; |
187 | } |
188 | |
189 | switch(header[0x3e]) |
190 | { |
191 | // PAL codes |
192 | case 0x44: |
193 | case 0x46: |
194 | case 0x49: |
195 | case 0x50: |
196 | case 0x53: |
197 | case 0x55: |
198 | case 0x58: |
199 | case 0x59: |
200 | config.romPAL = true; |
201 | break; |
202 | |
203 | // NTSC codes |
204 | case 0x37: |
205 | case 0x41: |
206 | case 0x45: |
207 | case 0x4a: |
208 | config.romPAL = false; |
209 | break; |
210 | |
211 | // Fallback for unknown codes |
212 | default: |
213 | config.romPAL = false; |
214 | } |
215 | |
216 | LOG(LOG_MINIMAL, "Rom is %s\n", config.romPAL ? "PAL" : "NTSC"); |
ac4f8e43 |
217 | printf("Rom is %s\n", config.romPAL ? "PAL" : "NTSC"); |
34cf4058 |
218 | |
219 | const char *filename = ConfigGetSharedDataFilepath("gles2n64rom.conf"); |
220 | FILE *f = fopen(filename,"r"); |
221 | if (!f) |
222 | { |
223 | LOG(LOG_MINIMAL, "Could not find %s Rom settings file, using global.\n", filename); |
ac4f8e43 |
224 | printf("Could not find %s Rom settings file, using global.\n", filename); |
34cf4058 |
225 | return; |
226 | } |
227 | else |
228 | { |
229 | LOG(LOG_MINIMAL, "[gles2N64]: Searching %s Database for \"%s\" ROM\n", filename, config.romName); |
ac4f8e43 |
230 | printf("[gles2N64]: Searching %s Database for \"%s\" ROM\n", filename, config.romName); |
34cf4058 |
231 | bool isRom = false; |
232 | while (!feof(f)) |
233 | { |
234 | fgets(line, 4096, f); |
235 | if (line[0] == '\n') continue; |
236 | |
237 | if (strncmp(line,"rom name=", 9) == 0) |
238 | { |
239 | //Depending on the editor, end lines could be terminated by "LF" or "CRLF" |
240 | char* lf = strchr(line, '\n'); //Line Feed |
241 | char* cr = strchr(line, '\r'); //Carriage Return |
242 | if (lf) *lf='\0'; |
243 | if (cr) *cr='\0'; |
244 | isRom = (strcasecmp(config.romName, line+9) == 0); |
245 | } |
246 | else |
247 | { |
248 | if (isRom) |
249 | { |
250 | char* val = strchr(line, '='); |
251 | if (!val) continue; |
252 | *val++ = '\0'; |
253 | Config_SetOption(line,val); |
254 | LOG(LOG_MINIMAL, "%s = %s", line, val); |
ac4f8e43 |
255 | printf("%s = %s", line, val); |
34cf4058 |
256 | } |
257 | } |
258 | } |
259 | } |
260 | |
261 | fclose(f); |
262 | } |
263 | |
264 | void Config_LoadConfig() |
265 | { |
266 | FILE *f; |
267 | char line[4096]; |
268 | |
269 | // default configuration |
270 | Config_SetDefault(); |
271 | |
272 | // read configuration |
273 | const char *filename = ConfigGetSharedDataFilepath("gles2n64.conf"); |
274 | f = fopen(filename, "r"); |
275 | if (!f) |
276 | { |
277 | LOG(LOG_MINIMAL, "[gles2N64]: Couldn't open config file '%s' for reading: %s\n", filename, strerror( errno ) ); |
278 | LOG(LOG_MINIMAL, "[gles2N64]: Attempting to write new Config \n"); |
ac4f8e43 |
279 | printf("[gles2N64]: Couldn't open config file '%s' for reading: %s\n", filename, strerror( errno ) ); |
280 | printf("[gles2N64]: Attempting to write new Config \n"); |
34cf4058 |
281 | Config_WriteConfig(filename); |
282 | } |
283 | else |
284 | { |
285 | LOG(LOG_MINIMAL, "[gles2n64]: Loading Config from %s \n", filename); |
ac4f8e43 |
286 | printf("[gles2n64]: Loading Config from %s \n", filename); |
34cf4058 |
287 | |
288 | while (!feof( f )) |
289 | { |
290 | char *val; |
291 | fgets( line, 4096, f ); |
292 | |
293 | if (line[0] == '#' || line[0] == '\n') |
294 | continue; |
295 | |
296 | val = strchr( line, '=' ); |
297 | if (!val) continue; |
298 | |
299 | *val++ = '\0'; |
300 | |
301 | Config_SetOption(line,val); |
302 | } |
303 | |
304 | if (config.version < CONFIG_VERSION) |
305 | { |
306 | LOG(LOG_WARNING, "[gles2N64]: Wrong config version, rewriting config with defaults\n"); |
ac4f8e43 |
307 | printf("[gles2N64]: Wrong config version, rewriting config with defaults\n"); |
34cf4058 |
308 | Config_SetDefault(); |
309 | Config_WriteConfig(filename); |
310 | } |
311 | |
312 | fclose(f); |
313 | } |
314 | } |
315 | |