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