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