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