Subversion Repositories spk

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#include "spk.h"
2
 
3
namespace SPK {
18 cycrow 4
	s_int GetAutomaticFiletype(CyString file, CyString *extradir, bool bUseSpecial)
1 cycrow 5
	{
6
		CFileIO File(file);
7
		CyString dir = File.GetDirIO().TopDir();
8
 
9
		CyString basename = File.GetBaseName();
10
		// could be a script, text file or map
11
		s_int type = -1;
12
		if ( dir.Compare("types") || dir.Compare("mov") || dir.Compare("s") )
13
		{
14
			type = FILETYPE_EXTRA;
15
			if ( extradir )
16
				*extradir = dir;
17
		}
18
		else if ( File.CheckFileExtension("xml") || File.CheckFileExtension("pck") )
19
		{
20
			// TC text file
21
			type = FILETYPE_SCRIPT;
22
			if ( File.GetFilename().IsIn("-L") && File.GetFilename().Left(4).ToInt() )
23
				type = FILETYPE_TEXT;
24
			// X2/X3 text file
25
			else if ( basename.Length() >= 5 && basename.Length() <= 8 && File.GetBaseName().ToInt() )
26
				type = FILETYPE_TEXT;
27
			else if ( dir.Compare("maps") )
28
				type = FILETYPE_MAP;
29
			else if ( dir.Compare("t") )
30
				type = FILETYPE_TEXT;
18 cycrow 31
			else if ( (bUseSpecial) && (dir.Compare("scripts") && basename.IsIn("uninstall")) )
32
				type = FILETYPE_SCRIPT_UNINSTALL;
1 cycrow 33
			else if ( dir.Compare("uninstall") || basename.IsIn("uninstall") )
34
				type = FILETYPE_UNINSTALL;
18 cycrow 35
			else if ( dir.IsIn("uninstall") )
36
				type = FILETYPE_UNINSTALL;
1 cycrow 37
			else if ( dir.Compare("director") )
38
				type = FILETYPE_MISSION;
39
		}
40
		// could be a model file or scene file
41
		else if ( File.CheckFileExtension("pbd") || File.CheckFileExtension("bod") )
42
		{
43
			if ( dir.IsIn("cockpit") || basename.IsIn("cockpit") )
44
				type = FILETYPE_COCKPITSCENE;
45
			else
46
				type = FILETYPE_SHIPSCENE;
47
		}
48
		// can only be a model
49
		else if ( File.CheckFileExtension("pbb") || File.CheckFileExtension("bob") )
50
			type = FILETYPE_SHIPMODEL;
51
		// texture file
52
		else if ( File.CheckFileExtension("dds") )
53
			type = FILETYPE_SHIPOTHER;
54
		else if ( File.CheckFileExtension("txt") || dir.Compare("readme") )
55
			type = FILETYPE_README;
56
		else if ( File.CheckFileExtension("cat") || File.CheckFileExtension("dat") )
57
			type = FILETYPE_MOD;
58
		else if ( File.CheckFileExtension("mp3") )
59
			type = FILETYPE_SOUND;
60
		else if ( dir.Compare("loadscr") )
61
			type = FILETYPE_SCREEN;
62
		else if ( dir.Compare("graphics") )
63
			type = FILETYPE_ADVERT;
64
		else if ( File.CheckFileExtension("jpg") || File.CheckFileExtension("bmp") )
65
			type = FILETYPE_SCREEN;
66
		else if ( File.CheckFileExtension("gif") )
67
			type = FILETYPE_ADVERT;
68
 
69
		return type;
70
	}
71
 
18 cycrow 72
	//TODO: check for special types
1 cycrow 73
	void AssignAutomaticFiletypes(CyStringList *list)
74
	{
75
		for ( SStringList *str = list->Head(); str; str = str->next )
76
		{
77
			CyString extradir;
18 cycrow 78
			s_int type = SPK::GetAutomaticFiletype(str->str, &extradir, false);
1 cycrow 79
 
80
			if ( type == FILETYPE_EXTRA && !extradir.Empty() )
81
				str->data = CyString::Number(type) + " " + extradir;
82
			else
83
				str->data = CyString::Number(type);
84
		}
85
	}
86
 
87
	CyString GetSizeString ( unsigned long lSize)
88
	{
89
		float size = (float)lSize;
90
 
91
		int level = 0;
92
		while ( size > 1000 )
93
		{
94
			size /= 1024;
95
			++level;
96
			if ( level >= 3 )
97
				break;
98
		}
99
 
100
		CyString s;
101
		char str[20];
102
		switch ( level )
103
		{
104
			case 0:
105
				SPRINTF(str, "%.0f"), size );
106
				s = str;
107
				s += "B";
108
				break;
109
			case 1:
110
				SPRINTF(str, "%.2f"), size );
111
				s = str;
112
				s += "KB";
113
				break;
114
			case 2:
115
				SPRINTF(str, "%.2f"), size );
116
				s = str;
117
				s += "MB";
118
				break;
119
			case 3:
120
				SPRINTF(str, "%.3f"), size );
121
				s = str;
122
				s += "GB";
123
				break;
124
		}
125
		return s;
126
	}
127
 
128
	bool WriteScriptStyleSheet(CyString dest)
129
	{
130
		CyStringList list;
131
		list.PushBack("<?xml version=\"1.0\" ?>");
132
		list.PushBack("<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" >");
133
		list.PushBack("<xsl:template match=\"/\">");
134
		list.PushBack("<html>");
135
		list.PushBack("<head>");
136
		list.PushBack("<title>X3TC Script: <xsl:value-of select=\"script/name\"/></title>");
137
		list.PushBack("</head>");
138
		list.PushBack("<body style=\"color:white;background-color:black\">");
139
		list.PushBack("<xsl:apply-templates/>");
140
		list.PushBack("</body>");
141
		list.PushBack("</html>");
142
		list.PushBack("</xsl:template>");
143
		list.PushBack("<xsl:template match=\"script\">");
144
		list.PushBack("<h1>Script <xsl:value-of select=\"name\"/></h1>");
145
		list.PushBack("Version: <xsl:value-of select=\"version\"/><br/>");
146
		list.PushBack("for Script Engine Version: <xsl:value-of select=\"engineversion\"/><br/>");
147
		list.PushBack("<h3>Description</h3>");
148
		list.PushBack("<xsl:value-of select=\"description\"/>");
149
		list.PushBack("<xsl:apply-templates/>");
150
		list.PushBack("<xsl:choose><xsl:when test=\"signature\"><br/>signed</xsl:when></xsl:choose>");
151
		list.PushBack("</xsl:template>");
152
		list.PushBack("<xsl:template match=\"name\">");
153
		list.PushBack("</xsl:template>");
154
		list.PushBack("<xsl:template match=\"version\">");
155
		list.PushBack("</xsl:template>");
156
		list.PushBack("<xsl:template match=\"engineversion\">");
157
		list.PushBack("</xsl:template>");
158
		list.PushBack("<xsl:template match=\"description\">");
159
		list.PushBack("</xsl:template>");
160
		list.PushBack("<xsl:template match=\"arguments\">");
161
		list.PushBack("<h3>Arguments</h3>");
162
		list.PushBack("<ul>");
163
		list.PushBack("<xsl:apply-templates/>");
164
		list.PushBack("</ul>");
165
		list.PushBack("</xsl:template>");
166
		list.PushBack("<xsl:template match=\"argument\">");
167
		list.PushBack("<li>");
168
		list.PushBack("<xsl:value-of select=\"@index\"/>: <xsl:value-of select=\"@name\"/> , <xsl:value-of select=\"@type\"/> , '<xsl:value-of select=\"@desc\"/>'");
169
		list.PushBack("</li>");
170
		list.PushBack("</xsl:template>");
171
		list.PushBack("<xsl:template match=\"sourcetext\">");
172
		list.PushBack("<h3>Source Text</h3>");
173
		list.PushBack("<div style=\"color:white;background-color:darkgray\">");
174
		list.PushBack("<br/>");
175
		list.PushBack("<code>");
176
		list.PushBack("<xsl:apply-templates/>");
177
		list.PushBack("</code>");
178
		list.PushBack("<br/>");
179
		list.PushBack("</div>");
180
		list.PushBack("</xsl:template>");
181
		list.PushBack("<xsl:template match=\"line\">");
182
		list.PushBack("<b style=\"color:blue\"><xsl:value-of select=\"@linenr\"/>&#160;<xsl:choose><xsl:when test=\"@interruptable\"><b style=\"color:cyan\"><xsl:value-of select=\"@interruptable\"/></b></xsl:when><xsl:otherwise>&#160;</xsl:otherwise></xsl:choose></b>&#160;<b style=\"color:#777777\"><xsl:value-of select=\"translate(@indent,'&#160;','|&#255;')\"/></b><xsl:apply-templates/><br/>");
183
		list.PushBack("</xsl:template>");
184
		list.PushBack("<xsl:template match=\"text\">");
185
		list.PushBack("<xsl:value-of select=\".\"/>");
186
		list.PushBack("</xsl:template>");
187
		list.PushBack("<xsl:template match=\"var\">");
188
		list.PushBack("<b style=\"color:darkgreen\">");
189
		list.PushBack("<xsl:value-of select=\".\"/>");
190
		list.PushBack("</b>");
191
		list.PushBack("</xsl:template>");
192
		list.PushBack("<xsl:template match=\"comment\"><b style=\"color:black\"><xsl:value-of select=\".\"/></b></xsl:template>");
193
		list.PushBack("<xsl:template match=\"call\">'<a><xsl:attribute name=\"href\"><xsl:value-of select=\".\"/>.xml</xsl:attribute><xsl:value-of select=\".\"/></a>'</xsl:template>");
194
		list.PushBack("<xsl:template match=\"codearray\">");
195
		list.PushBack("</xsl:template>");
196
		list.PushBack("<xsl:template match=\"signature\">");
197
		list.PushBack("</xsl:template>");
198
		list.PushBack("</xsl:stylesheet>");
199
 
200
		CFileIO File(dest + "/x2script.xsl");
201
		return File.WriteFile(&list);
202
	}
203
 
204
	CyString ConvertTimeString(time_t time)
205
	{
206
		struct tm   *currDate;
207
		char    dateString[100];
208
 
209
		time_t n = time;
210
		currDate = localtime(&n);
211
		strftime(dateString, sizeof dateString, "(%d/%m/%Y) %H:%M", currDate);
212
 
213
		return CyString(dateString);
214
	}
215
 
216
	CyString FormatTextName(int id, int lang, bool newstyle)
217
	{
218
		if ( newstyle )
219
			return CyString::Number(id).PadNumber(4) + "-L" + CyString::Number(lang).PadNumber(3);
220
		else
221
			return CyString::Number(lang) + CyString::Number(id).PadNumber(4);
222
	}
223
};