log.c
[voxelands-alt.git] / src / core / config.c
bloba169f2c1e73eadd72ac9782629d733f7f28c0579
1 /************************************************************************
2 * config.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
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 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "nvp.h"
22 #include "crypto.h"
23 #include "file.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 static struct {
30 nvp_t *items;
31 nvp_t *files;
32 int isinit;
33 } config = {
34 NULL,
35 NULL,
39 typedef struct config_s {
40 char* default_value;
41 int (*setter)(char* v);
42 } config_t;
44 /* get the value of a config setting */
45 char* config_get(char* name)
47 nvp_t *n = nvp_get(&config.items,name);
48 if (!n)
49 return NULL;
51 if (!n->value)
52 return ((config_t*)n->data)->default_value;
54 return n->value;
57 /* get a config setting as an int value */
58 int config_get_int(char* name)
60 char* v = config_get(name);
61 if (v)
62 return strtol(v,NULL,10);
64 return 0;
67 /* get a config setting as a float value */
68 float config_get_float(char* name)
70 char* v = config_get(name);
71 if (v)
72 return strtof(v,NULL);
74 return 0.0;
77 /* get a config setting as a boolean value */
78 int config_get_bool(char* name)
80 char* v = config_get(name);
81 if (v) {
82 if (!strcmp(v,"true"))
83 return 1;
84 if (!strcmp(v,"yes"))
85 return 1;
86 if (!strcmp(v,"on"))
87 return 1;
88 if (!strcmp(v,"enabled"))
89 return 1;
90 if (!strcmp(v,"active"))
91 return 1;
92 if (strtol(v,NULL,10))
93 return 1;
96 return 0;
99 /* set the value of a config setting */
100 void config_set(char* name, char* value)
102 config_t *c;
103 nvp_t *n = nvp_get(&config.items,name);
105 if (!n) {
106 if (!value)
107 return;
109 c = malloc(sizeof(config_t));
110 c->default_value = NULL;
111 c->setter = NULL;
113 nvp_set(&config.items,name,value,c);
115 return;
118 if (n->value)
119 free(n->value);
120 n->value = NULL;
122 if (value)
123 n->value = strdup(value);
125 c = n->data;
126 if (c->setter) {
127 if (!n->value && c->default_value) {
128 c->setter(c->default_value);
129 }else{
130 c->setter(n->value);
135 /* set a config setting to an int value */
136 void config_set_int(char* name, int value)
138 char str[20];
139 sprintf(str,"%d",value);
140 config_set(name,str);
143 /* set a config setting to a float value */
144 void config_set_float(char* name, float value)
146 char str[50];
147 sprintf(str,"%f",value);
148 config_set(name,str);
151 /* set the default value of a config setting */
152 void config_set_default(char* name, char* value, int (*setter)(char* v))
154 config_t *c;
155 nvp_t *n = nvp_get(&config.items,name);
157 if (!n) {
158 if (!value && !setter)
159 return;
161 c = malloc(sizeof(config_t));
162 if (value) {
163 c->default_value = strdup(value);
164 }else{
165 c->default_value = NULL;
167 c->setter = setter;
169 nvp_set(&config.items,name,value,c);
171 if (setter)
172 setter(value);
174 return;
177 c = n->data;
179 if (c->default_value)
180 free(c->default_value);
181 c->default_value = NULL;
183 if (value)
184 c->default_value = strdup(value);
187 /* set the default of a config setting to an int value */
188 void config_set_default_int(char* name, int value, int (*setter)(char* v))
190 char str[20];
191 sprintf(str,"%d",value);
192 config_set_default(name,str,setter);
195 /* set the default of a config setting to a float value */
196 void config_set_default_float(char* name, float value, int (*setter)(char* v))
198 char str[50];
199 sprintf(str,"%f",value);
200 config_set_default(name,str,setter);
203 /* load a config file */
204 void config_load(char* file)
206 char buff[2048];
207 int s;
208 char* l;
210 file_t *f = file_load("config",file);
211 if (!f)
212 return;
214 while ((s = file_readline(f,buff,2048)) > -1) {
215 if (!s || buff[0] == '#')
216 continue;
217 l = trim(buff);
218 /* TODO: uncomment when command.c is written
219 if (l && l[0])
220 cmd_exec(l);*/
223 file_free(f);
226 /* initialise configuration, load config files and defaults, etc */
227 void config_init(int argc, char** argv)
229 int i;
230 nvp_t *n;
232 /* TODO: this should get adapted and moved to config_default.c
233 * TODO: also call config_default_init()
234 cmd_add_setter(RTG_SETTER_VFUNC,"sys.control",event_set_bind);
235 cmd_add_setter(RTG_SETTER_LITERAL,"wm.width",wm_width_setter);
236 cmd_add_setter(RTG_SETTER_LITERAL,"wm.height",wm_height_setter);
237 cmd_add_setter(RTG_SETTER_LITERAL,"wm.framecap",wm_cap_setter);
238 cmd_add_setter(RTG_SETTER_LITERAL,"wm.fullscreen",wm_fs_setter);
239 #ifdef RTG3D
240 cmd_add_setter(RTG_SETTER_LITERAL,"wm.distance",wm_distance_setter);
241 #endif
242 cmd_add_setter(RTG_SETTER_LITERAL,"sound.enabled",sound_setter);
243 cmd_add_setter(RTG_SETTER_LITERAL,"sound.effects.volume",sound_effects_setter);
244 cmd_add_setter(RTG_SETTER_LITERAL,"sound.music.volume",sound_music_setter);
245 cmd_add_setter(RTG_SETTER_LITERAL,"gl.anisotropic",opengl_anisotropic_setter);
246 cmd_add_setter(RTG_SETTER_LITERAL,"gl.bilinear",opengl_bilinear_setter);
247 cmd_add_setter(RTG_SETTER_LITERAL,"gl.trilinear",opengl_trilinear_setter);
248 cmd_add_setter(RTG_SETTER_LITERAL,"gl.mipmaps",opengl_mipmap_setter);
249 #ifdef RTG3D
250 cmd_add_setter(RTG_SETTER_LITERAL,"gl.shaders",shader_setter);
251 cmd_add_setter(RTG_SETTER_LITERAL,"gl.backfacecull",opengl_backfacecull_setter);
252 cmd_add_setter(RTG_SETTER_LITERAL,"gl.particles.enabled",opengl_particles_setter);
253 cmd_add_setter(RTG_SETTER_LITERAL,"gl.particles.max",opengl_particles_max_setter);
254 #endif
255 cmd_add_setter(RTG_SETTER_VFUNC,"ui.elements",ui_element_setter);
258 /* add the default config file to the to-exec list */
259 nvp_set(&config.files,"default.cfg","true",NULL);
261 for (i=1; i<(argc-1); i++) {
262 if (!strcmp(argv[i],"exec")) {
263 i += 1;
264 nvp_set(&config.files,argv[i],"true",NULL);
265 }else if (!strcmp(argv[i],"ignore")) {
266 i += 1;
267 nvp_set(&config.files,argv[i],"false",NULL);
271 n = config.files;
272 while (n) {
273 if (n->value && !strcmp(n->value,"true"))
274 config_load(n->name);
275 n = n->next;
278 for (i=0; i<argc; i++) {
279 if (!strcmp(argv[i],"set") && i+2 < argc) {
280 config_set(argv[i+1],argv[i+2]);
281 i+=2;
282 }else if (!strcmp(argv[i],"unset") && i+1 < argc) {
283 i+=1;
284 config_set(argv[i],NULL);
285 }else if (!strcmp(argv[i],"exec")) {
286 i += 1;
287 }else if (!strcmp(argv[i],"ignore")) {
288 i += 1;
292 config.isinit = 1;
295 /* save the current config */
296 void config_save()
298 file_t *f;
299 nvp_t *n;
301 n = config.files;
302 while (n) {
303 if (n->value && !strcmp(n->value,"true"))
304 break;
305 n = n->next;
308 /* TODO: should probably force saving to somewhere, custom.cfg? */
309 if (!n)
310 return;
312 f = file_create("config",n->value);
313 if (!f)
314 return;
316 n = config.items;
317 while (n) {
318 if (n->value)
319 file_writef(f,"set %s %s\n",n->name,n->value);
320 n = n->next;
323 file_flush(f);
324 file_free(f);