File: settings.cpp

package info (click to toggle)
sludge 2.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,852 kB
  • sloc: cpp: 32,432; sh: 1,237; makefile: 634; xml: 284
file content (368 lines) | stat: -rw-r--r-- 12,547 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>


#include "sludge_functions.h"
#include "splitter.hpp"
#include "settings.h"
#include "translation.h"
#include "helpers.h"
#include "moreio.h"
#include "messbox.h"
#include "version.h"
#include "interface.h"

settingsStruct settings;
programSettingsStruct programSettings;

chrRenderingSettingsStruct chrRenderingSettings;

char * tempDirectory = NULL;
bool silent = true;


void noSettings () {

	if (settings.quitMessage) delete settings.quitMessage;
	settings.quitMessage = joinStrings ("Are you sure you want to quit?", "");
	if (settings.customIcon) delete settings.customIcon;
	settings.customIcon = joinStrings ("", "");
	if (settings.customLogo) delete settings.customLogo;
	settings.customLogo = joinStrings ("", "");
	if (settings.runtimeDataFolder) delete settings.runtimeDataFolder;
	settings.runtimeDataFolder = joinStrings ("save", "");
	if (settings.finalFile) delete settings.finalFile;
	settings.finalFile =joinStrings ("Gamedata", "");
	if (settings.windowName) delete settings.windowName;
	settings.windowName = joinStrings ("New SLUDGE Game", "");
	if (settings.originalLanguage) delete settings.originalLanguage;
	settings.originalLanguage =joinStrings ("English", "");
	settings.screenWidth = 640;
	settings.screenHeight = 480;
	settings.frameSpeed = 20;
	settings.winMouseImage = 2; // Hide the mouse
	settings.ditherImages = true;
	settings.runFullScreen = true;
	settings.forceSilent = false;
	settings.startupShowLogo = true;
	settings.startupShowLoading = true;
	settings.startupInvisible = false;
	chrRenderingSettingsFillDefaults(true);
}


unsigned int stringToInt (const char * st) {
	unsigned int a = 0;
	for (int i = 0; st[i]; i ++) {
		a *= 10;
		a += st[i] - '0';
	}
	return a;
}

void readDir (char * t) {
	stringArray * splitLine = splitString (t, '=', ONCE);
	if (splitLine -> next) {
		if (strcmp (splitLine -> string, "quitmessage") == 0) {
			delete settings.quitMessage;
			settings.quitMessage = joinStrings ("", splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "customicon") == 0) {
			if (settings.customIcon) delete settings.customIcon;
			settings.customIcon = joinStrings ("", splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "customlogo") == 0) {
			if (settings.customLogo) delete settings.customLogo;
			settings.customLogo = joinStrings ("", splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "datafolder") == 0) {
			if (settings.runtimeDataFolder) delete settings.runtimeDataFolder;
			settings.runtimeDataFolder = joinStrings ("", splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "finalfile") == 0) {
			if (settings.finalFile) delete settings.finalFile;
			settings.finalFile = joinStrings ("", splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "windowname") == 0) {
			if (settings.windowName) delete settings.windowName;
			settings.windowName = joinStrings ("", splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "language") == 0) {
			if (settings.originalLanguage) delete settings.originalLanguage;
			settings.originalLanguage = joinStrings ("", splitLine -> next -> string);

		// NEW MOUSE SETTING
		} else if (strcmp (splitLine -> string, "mouse") == 0) {
			settings.winMouseImage = stringToInt (splitLine -> next -> string);
				
		// OLD MOUSE SETTING
		} else if (strcmp (splitLine -> string, "hidemouse") == 0) {
			settings.winMouseImage = (splitLine -> next -> string[0] == 'Y') ? 2 : 1;

		} else if (strcmp (splitLine -> string, "ditherimages") == 0) {
			settings.ditherImages = (splitLine -> next -> string[0] == 'Y');

		} else if (strcmp (splitLine -> string, "fullscreen") == 0) {
			settings.runFullScreen = (splitLine -> next -> string[0] == 'Y');

		} else if (strcmp (splitLine -> string, "showlogo") == 0) {
			settings.startupShowLogo = (splitLine -> next -> string[0] == 'Y');

		} else if (strcmp (splitLine -> string, "showloading") == 0) {
			settings.startupShowLoading = (splitLine -> next -> string[0] == 'Y');

		} else if (strcmp (splitLine -> string, "invisible") == 0) {
			settings.startupInvisible = (splitLine -> next -> string[0] == 'Y');

		} else if (strcmp (splitLine -> string, "makesilent") == 0) {
			settings.forceSilent = (splitLine -> next -> string[0] == 'Y');

		} else if (strcmp (splitLine -> string, "height") == 0) {
			settings.screenHeight = stringToInt (splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "width") == 0) {
			settings.screenWidth = stringToInt (splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "chrRender_def_enabled") == 0) {
			chrRenderingSettings.defEnabled = (splitLine -> next -> string[0] == 'Y');
		} else if (strcmp (splitLine -> string, "chrRender_def_softX") == 0) {
			chrRenderingSettings.defSoftnessX = stringToInt (splitLine -> next -> string);
		} else if (strcmp (splitLine -> string, "chrRender_def_softY") == 0) {
			chrRenderingSettings.defSoftnessY = stringToInt (splitLine -> next -> string);
		} else if (strcmp (splitLine -> string, "chrRender_max_enabled") == 0) {
			chrRenderingSettings.maxEnabled = (splitLine -> next -> string[0] == 'Y');
		} else if (strcmp (splitLine -> string, "chrRender_max_readIni") == 0) {
			chrRenderingSettings.maxReadIni = (splitLine -> next -> string[0] == 'Y');
		} else if (strcmp (splitLine -> string, "chrRender_max_softX") == 0) {
			chrRenderingSettings.maxSoftnessX = stringToInt (splitLine -> next -> string);
		} else if (strcmp (splitLine -> string, "chrRender_max_softY") == 0) {
			chrRenderingSettings.maxSoftnessY = stringToInt (splitLine -> next -> string);

		} else if (strcmp (splitLine -> string, "speed") == 0) {
			settings.frameSpeed = stringToInt (splitLine -> next -> string);
		}
	}
	while (destroyFirst (splitLine)){;}
}


bool readSettings (FILE * fp) {
	char * grabLine;
	bool keepGoing = true;
	noSettings ();

	while (keepGoing) {
		grabLine = readText (fp);
		if (grabLine && grabLine[0]) {
			readDir (grabLine);
		} else
			keepGoing = false;
		delete grabLine;
	}

	if (! settings.finalFile) return addComment (ERRORTYPE_PROJECTERROR, "Vital line missing from project", "finalfile", NULL,0);

	return true;
}

void killSettingsStrings ()
{
	if (settings.quitMessage) delete settings.quitMessage;
	if (settings.customIcon) delete settings.customIcon;
	if (settings.customLogo) delete settings.customLogo;
	if (settings.runtimeDataFolder) delete settings.runtimeDataFolder;
	if (settings.finalFile) delete settings.finalFile;
	if (settings.windowName) delete settings.windowName;
}


/* killTempDir - Removes the temporary directory
 *
 * Not really needed on Mac OS, since the OS takes care of that for us every reboot,
 * but it's nice to clean up anyway, and I don't think Windows cleans up that way.
 *
 */
void killTempDir() {
    /*
	gotoTempDirectory ();

	struct dirent **eps;
	int n = scandir (tempDirectory, &eps, NULL, NULL);
	if (n > 0)
	{
		int cnt;
		for (cnt = 0; cnt < n; ++cnt) {
			unlink (eps[cnt]->d_name);
			free (eps[cnt]);
		}
		free (eps);
	}
*/
	gotoSourceDirectory ();

	rmdir(tempDirectory);
	tempDirectory = NULL;
}

static void fileWriteBool (FILE * fp, const char * theString, bool theBool)
{
	fprintf (fp, "%s=%c\n", theString, theBool ? 'Y' : 'N');
}

void writeSettings (FILE * fp) {

	fprintf 		(fp, "[SETTINGS]\nwindowname=%s\n",	settings.windowName);
	fprintf 		(fp, "finalfile=%s\n", 			settings.finalFile);
	fprintf 		(fp, "language=%s\n", 			settings.originalLanguage);
	fprintf 		(fp, "datafolder=%s\n", 		settings.runtimeDataFolder);
	fprintf 		(fp, "quitmessage=%s\n", 		settings.quitMessage);
	fprintf 		(fp, "customicon=%s\n", 		settings.customIcon);
	fprintf 		(fp, "customlogo=%s\n", 		settings.customLogo);
	fprintf 		(fp, "mouse=%i\n", 			settings.winMouseImage);
	fprintf 		(fp, "fullscreen=%c\n", 		settings.runFullScreen ? 'Y':'N');
	fprintf 		(fp, "makesilent=%c\n", 		settings.forceSilent ? 'Y':'N');
	fprintf 		(fp, "showlogo=%c\n", 			settings.startupShowLogo ? 'Y':'N');
	fprintf 		(fp, "showloading=%c\n", 		settings.startupShowLoading ? 'Y':'N');
	fprintf 		(fp, "invisible=%c\n", 			settings.startupInvisible ? 'Y':'N');
	fileWriteBool 		(fp, "ditherimages", 			settings.ditherImages);
	fprintf 	  	(fp, "width=%i\n",			settings.screenWidth);
	fprintf 	  	(fp, "height=%i\n", 			settings.screenHeight);
	fprintf 		(fp, "speed=%i\n", 			settings.frameSpeed);

	fileWriteBool		(fp, "chrRender_def_enabled", 		chrRenderingSettings.defEnabled);
	fprintf 	 	(fp, "chrRender_def_softX=%i\n",	chrRenderingSettings.defSoftnessX);
	fprintf 	 	(fp, "chrRender_def_softY=%i\n",	chrRenderingSettings.defSoftnessY);

	fileWriteBool		(fp, "chrRender_max_enabled", 		chrRenderingSettings.maxEnabled);
	fileWriteBool		(fp, "chrRender_max_readIni", 		chrRenderingSettings.maxReadIni);
	fprintf 	 	(fp, "chrRender_max_softX=%i\n",	chrRenderingSettings.maxSoftnessX);
	fprintf 	 	(fp, "chrRender_max_softY=%i\n",	chrRenderingSettings.maxSoftnessY);

	fprintf 		(fp, "\n[FILES]\n");
}

void chrRenderingSettingsFillDefaults(bool enable)
{
	chrRenderingSettings.defEnabled = true;
	chrRenderingSettings.defSoftnessX = 4;
	chrRenderingSettings.defSoftnessY = 4;

	chrRenderingSettings.maxEnabled = enable;
	chrRenderingSettings.maxReadIni = true;
	chrRenderingSettings.maxSoftnessX = 100;
	chrRenderingSettings.maxSoftnessY = 100;
}

bool gotoTempDirectory () {
	if (! tempDirectory) {
		tempDirectory = joinStrings(getTempDir(), "/SLUDGE_Tmp_XXXXXX");
		fixPath (tempDirectory, true);
#ifdef WIN32
		mkdir (getTempDir());
		if (mktemp (tempDirectory)) {
			if (mkdir (tempDirectory))
				tempDirectory = NULL;
		}
#else
		mkdir (getTempDir(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
		if (!mkdtemp (tempDirectory)) {
			return addComment (ERRORTYPE_SYSTEMERROR, "Can't create temporary directory", tempDirectory, NULL, 0);
		}
#endif
	}
	if (! tempDirectory) return false;
	bool r = chdir (tempDirectory);
	if (r) return addComment (ERRORTYPE_SYSTEMERROR, "Can't move to temporary directory", tempDirectory, NULL, 0);
	return true;
}

FILE * openFinalFile (const char * addMe, const char * mode) {
	char * fullName = joinStrings (settings.finalFile, addMe);
	if (! fullName) return NULL;

	gotoSourceDirectory ();
	FILE * fp = fopen (fullName, mode);
	delete fullName;

	return fp;
}

typedef struct _FILETIME {
	uint32_t dwLowDateTime;
	uint32_t dwHighDateTime;
} FILETIME;


#define MOUSE_1		1 << 2
#define MOUSE_2		1 << 4

int winMouseLookup[4] = {
	MOUSE_2,
	0,
	MOUSE_1,
	MOUSE_2 | MOUSE_1
};


void writeFinalData (FILE * mainFile) {
	fprintf (mainFile, "SLUDGE");
	fputc (0, mainFile);
	fprintf (mainFile, "\r\nSLUDGE data file\r\nSLUDGE is (c) Hungry Software and contributors 2006-2014\r\nThis data file must be run using the SLUDGE engine available at http://opensludge.github.io/\r\n");
	fputc (0, mainFile);

	fputc (MAJOR_VERSION, mainFile);		// Major version
	fputc (MINOR_VERSION, mainFile);		// Minor version

	if (programSettings.compilerVerbose) {
		fputc (1, mainFile);
		writeDebugData (mainFile);
	} else {
		fputc (0, mainFile);
	}

	put2bytes (settings.screenWidth, mainFile);
	put2bytes (settings.screenHeight, mainFile);
	fputc ((1 /* reg */	) +
		   (settings.runFullScreen								  << 1) +
		   (winMouseLookup[settings.winMouseImage & 3]				  ) +
		   (silent										  << 3) +
		   // 4 used for mouse image
		   (settings.startupInvisible 							  << 5) +
		   ((! settings.startupShowLogo)								  << 6) +
		   ((! settings.startupShowLoading)							  << 7),

		   mainFile);

	fputc (settings.frameSpeed, mainFile);
	writeString ("", mainFile); // Unused - was used for registration.

	// Now write the date and time of compilation...

	/* TODO - but not used by the engine anyway
		SYSTEMTIME systemTime;*/
	FILETIME fileTime;
	//GetSystemTime (& systemTime);
	//SystemTimeToFileTime (& systemTime, & fileTime);
	fwrite (& fileTime, sizeof (fileTime), 1, mainFile);

	// More bits

	writeString (settings.runtimeDataFolder, mainFile);

	addTranslationIDTable (mainFile, settings.originalLanguage);

	// Max anti-alias settings
	fputc (chrRenderingSettings.maxReadIni, mainFile);
	fputc (chrRenderingSettings.maxEnabled, mainFile);
	putFloat (chrRenderingSettings.maxSoftnessX / 16.f, mainFile);
	putFloat (chrRenderingSettings.maxSoftnessY / 16.f, mainFile);

	writeString ("okSoFar", mainFile);
}