File: dumpfiles.cpp

package info (click to toggle)
sludge 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,844 kB
  • ctags: 2,981
  • sloc: cpp: 31,952; sh: 1,259; xml: 874; makefile: 672
file content (211 lines) | stat: -rw-r--r-- 5,893 bytes parent folder | download | duplicates (7)
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "splitter.hpp"
#include "interface.h"
#include "settings.h"
#include "helpers.h"
#include "hsi.h"
#include "messbox.h"
#include "moreio.h"
#include "floor.h"
#include "translation.h"
#include "dumpfiles.h"
#include "compilerinfo.h"

bool dumpFileInto (FILE * writer, const char * thisFile) {
	int a;
	FILE * reader = fopen (thisFile, "rb");
	char buf[256];

	if (! reader) return false;
	while (true) {
		a = fread(buf, 1, 256, reader);
		if (! a) break;
		fwrite (buf, 1, a, writer);
	}
	fclose (reader);
	return true;
}

int getFileType (const char * filename) {
	int reply = FILETYPE_UNKNOWN;

	if (strlen (filename) >= 4) {
		char * compareMe = copyString(filename + (strlen (filename) - 4));
		for (int ii = 0; ii < 4; ii ++)
		{
			if (compareMe[ii] >= 'A' && compareMe[ii] <= 'Z')
				compareMe[ii] += 'a' - 'A';
		}

		if (strcmp (compareMe, ".tga") == 0) {
			reply = FILETYPE_TGA;
		} else if (strcmp (compareMe, ".flo") == 0) {
			reply = FILETYPE_FLOOR;
		} else if ( strcmp (compareMe, ".mid") == 0 ||
					strcmp (compareMe, ".rmi") == 0) {
			reply = FILETYPE_MIDI;
		} else if ( strcmp (compareMe, ".wav") == 0 ||
//					strcmp (compareMe, ".mp3") == 0 ||
					strcmp (compareMe, ".ogg") == 0 ||
				   // WebM is counted as audio so sound will be initialized for it
				   strcmp (compareMe, "webm") == 0 ||
					strcmp (compareMe + 1, ".xm") == 0 ||
					strcmp (compareMe + 1, ".it") == 0 ||
					strcmp (compareMe, ".s3m") == 0 ||
					strcmp (compareMe, ".mod") == 0
//					strcmp (compareMe, ".mo3") == 0 ||
//					strcmp (compareMe, ".avi") == 0) {
				   ) {
			reply = FILETYPE_AUDIO;
		} else if (	strcmp (compareMe, ".duc") == 0 ||
					strcmp (compareMe, ".zbu") == 0 ||
					strcmp (compareMe, ".png") == 0) {
			reply = FILETYPE_RAW;
		}
		delete compareMe;
	}

	return reply;
}

bool dumpFiles (FILE * mainFile, stringArray * & theSA) {

	if (! theSA) return true;

	// Display first...

	setCompilerText (COMPILER_TXT_ACTION, "Attaching sprites, images and audio files");
	setCompilerText (COMPILER_TXT_ITEM, "");

	int numFiles = countElements (theSA);

	clearRect (numFiles, P_BOTTOM);

	// Now the hard work
	unsigned long remainingIndexSize[numFiles];
	unsigned long currentRemainingIndex;
	unsigned long filesize;

	currentRemainingIndex = remainingIndexSize[0] = numFiles * 4 - 4;

	long lookup = ftell(mainFile);
	fseek(mainFile, lookup+currentRemainingIndex+4, SEEK_SET);

	int i = 0;

	FILE * inFile;
	gotoSourceDirectory ();

	char buf[0xFFFF];

	bool killAfterAdd;
	while (theSA) {
		setCompilerText (COMPILER_TXT_FILENAME, theSA -> string);

		killAfterAdd = false;
		if (! theSA -> string[0]) {
			// Nothing - used in forceSilent mode to replace audio
			put4bytes (0, mainFile);

			// Save the distance from here to the data in the index...
			remainingIndexSize[i] = currentRemainingIndex;
		} else {
			switch (getFileType (theSA->string)) {
				case FILETYPE_TGA:
				convertTGA (theSA -> string);
				killAfterAdd = programSettings.compilerKillImages;
				break;

				case FILETYPE_FLOOR:
				convertFloor (theSA -> string);
				killAfterAdd = true;
				break;

				case FILETYPE_MIDI:
				addComment (ERRORTYPE_PROJECTWARNING, "The current version of the SLUDGE engine cannot play MIDI files such as", theSA->string, NULL, 0);
				break;

				case FILETYPE_UNKNOWN:
				return addComment (ERRORTYPE_PROJECTERROR, "Tried to include a file which is not supported by SLUDGE.\n\nSupported music types: .XM, .MOD, .S3M, .IT\nSupported sampled sound types: .WAV, .OGG\nSupported graphic types: .TGA, .PNG\nSupported movie format: .WebM\nSLUDGE-specific types: .FLO, .ZBU, .DUC\n\nThe file you tried to include was:", theSA -> string, NULL, 0);
			}

			inFile = fopen (theSA -> string, "rb");
			if (inFile == NULL) {
				return addComment (ERRORTYPE_PROJECTERROR, "Can't read resource file", theSA -> string, NULL, 0);
			}
			fseek (inFile, 0, 2);
			filesize = ftell (inFile);
			fseek (inFile, 0, 0);

			// Save the size at the start of the data...
			put4bytes (filesize, mainFile);

			// Save the distance from here to the data in the index...
			remainingIndexSize[i] = currentRemainingIndex;

			currentRemainingIndex += filesize;

			int a = 0;
			while (filesize -= a) {
				a = fread(buf, 1, 0xFFFF, inFile);
				if (! a) break;
				fwrite (buf, 1, a, mainFile);
			}

			fclose (inFile);
			if (killAfterAdd) unlink (theSA -> string);
		}
		destroyFirst (theSA);
		percRect (++ i, P_BOTTOM);
	}
	long filePos = ftell(mainFile);

	setCompilerText (COMPILER_TXT_ACTION, "Adding look-up table");
	setCompilerText (COMPILER_TXT_FILENAME, "");
	percRect (++ i, P_BOTTOM);

	fseek(mainFile, lookup, SEEK_SET);
	for (i=0;i<numFiles;i++) {
		put4bytes (remainingIndexSize[i], mainFile);
	}
	fseek(mainFile, filePos, SEEK_SET);

	return true;
}

bool saveStrings (FILE * mainFile, FILE * textFile, stringArray * theSA) {
	stringArray * wholeStringThing = theSA;

	FILE * projectFile, * indexFile;
	int indexSize = countElements (theSA) * 4 + ftell (mainFile) + 4;

	if (! gotoTempDirectory ()) return false;
	projectFile = fopen ("txtdata.tmp", "wb");
	indexFile = fopen ("txtindex.tmp", "wb");
	if (! (projectFile && indexFile)) return false;

	while (theSA) {
		put4bytes ((ftell (projectFile) + indexSize), indexFile);
		writeString (theSA -> string, projectFile);
		if (textFile) fprintf (textFile, "%s\n", theSA -> string);
		theSA = theSA -> next;
	}

	put4bytes (ftell (projectFile) + indexSize, mainFile);

	fclose (projectFile);
	fclose (indexFile);
	if (textFile) fclose (textFile);

	if (! gotoTempDirectory ()) return false;
	if (dumpFileInto (mainFile, "txtindex.tmp") && dumpFileInto (mainFile, "txtdata.tmp")) {
		unlink ("txtindex.tmp");
		unlink ("txtdata.tmp");
		return addAllTranslationData (wholeStringThing, mainFile);
	} else {
		return false;
	}
}