Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using Microsoft.CSharp;
  31. using OpenSim.Region.ScriptEngine.Shared.CodeTools;
  32. using System.CodeDom.Compiler;
  33. namespace OpenSim.Tools.LSL.Compiler
  34. {
  35. class Program
  36. {
  37. private static Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> m_positionMap;
  38. private static CSharpCodeProvider CScodeProvider = new CSharpCodeProvider();
  39. static void Main(string[] args)
  40. {
  41. string source = null;
  42. if(args.Length == 0)
  43. {
  44. Console.WriteLine("No input file specified");
  45. Environment.Exit(1);
  46. }
  47. if(!File.Exists(args[0]))
  48. {
  49. Console.WriteLine("Input file does not exist");
  50. Environment.Exit(1);
  51. }
  52. try
  53. {
  54. ICodeConverter cvt = (ICodeConverter) new CSCodeGenerator();
  55. source = cvt.Convert(File.ReadAllText(args[0]));
  56. }
  57. catch(Exception e)
  58. {
  59. Console.WriteLine("Conversion failed:\n"+e.Message);
  60. Environment.Exit(1);
  61. }
  62. source = CreateCSCompilerScript(source);
  63. try
  64. {
  65. Console.WriteLine(CompileFromDotNetText(source,"a.out"));
  66. }
  67. catch(Exception e)
  68. {
  69. Console.WriteLine("Conversion failed: "+e.Message);
  70. Environment.Exit(1);
  71. }
  72. Environment.Exit(0);
  73. }
  74. private static string CreateCSCompilerScript(string compileScript)
  75. {
  76. compileScript = String.Empty +
  77. "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\r\n" +
  78. String.Empty + "namespace SecondLife { " +
  79. String.Empty + "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" +
  80. @"public Script() { } " +
  81. compileScript +
  82. "} }\r\n";
  83. return compileScript;
  84. }
  85. private static string CompileFromDotNetText(string Script, string asset)
  86. {
  87. string OutFile = asset;
  88. string disp ="OK";
  89. try
  90. {
  91. File.Delete(OutFile);
  92. }
  93. catch (Exception e) // NOTLEGIT - Should be just FileIOException
  94. {
  95. throw new Exception("Unable to delete old existing "+
  96. "script-file before writing new. Compile aborted: " +
  97. e.ToString());
  98. }
  99. // Do actual compile
  100. CompilerParameters parameters = new CompilerParameters();
  101. parameters.IncludeDebugInformation = true;
  102. string rootPath =
  103. Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
  104. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  105. "OpenSim.Region.ScriptEngine.Shared.dll"));
  106. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  107. "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
  108. parameters.GenerateExecutable = false;
  109. parameters.OutputAssembly = OutFile;
  110. parameters.IncludeDebugInformation = true;
  111. parameters.WarningLevel = 1;
  112. parameters.TreatWarningsAsErrors = false;
  113. CompilerResults results = CScodeProvider.CompileAssemblyFromSource(parameters, Script);
  114. if (results.Errors.Count > 0)
  115. {
  116. string errtext = String.Empty;
  117. foreach (CompilerError CompErr in results.Errors)
  118. {
  119. string severity = "Error";
  120. if ( CompErr.IsWarning )
  121. {
  122. severity = "Warning";
  123. }
  124. KeyValuePair<int, int> lslPos;
  125. lslPos = FindErrorPosition(CompErr.Line, CompErr.Column);
  126. string text = CompErr.ErrorText;
  127. text = ReplaceTypes(CompErr.ErrorText);
  128. // The Second Life viewer's script editor begins
  129. // countingn lines and columns at 0, so we subtract 1.
  130. errtext += String.Format("Line ({0},{1}): {4} {2}: {3}\n",
  131. lslPos.Key - 1, lslPos.Value - 1,
  132. CompErr.ErrorNumber, text, severity);
  133. }
  134. disp = "Completed with errors";
  135. if (!File.Exists(OutFile))
  136. {
  137. throw new Exception(errtext);
  138. }
  139. }
  140. if (!File.Exists(OutFile))
  141. {
  142. string errtext = String.Empty;
  143. errtext += "No compile error. But not able to locate compiled file.";
  144. throw new Exception(errtext);
  145. }
  146. // Because windows likes to perform exclusive locks, we simply
  147. // write out a textual representation of the file here
  148. //
  149. // Read the binary file into a buffer
  150. //
  151. FileInfo fi = new FileInfo(OutFile);
  152. if (fi == null)
  153. {
  154. string errtext = String.Empty;
  155. errtext += "No compile error. But not able to stat file.";
  156. throw new Exception(errtext);
  157. }
  158. Byte[] data = new Byte[fi.Length];
  159. try
  160. {
  161. FileStream fs = File.Open(OutFile, FileMode.Open, FileAccess.Read);
  162. fs.Read(data, 0, data.Length);
  163. fs.Close();
  164. }
  165. catch (Exception)
  166. {
  167. string errtext = String.Empty;
  168. errtext += "No compile error. But not able to open file.";
  169. throw new Exception(errtext);
  170. }
  171. // Convert to base64
  172. //
  173. string filetext = System.Convert.ToBase64String(data);
  174. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  175. Byte[] buf = enc.GetBytes(filetext);
  176. FileStream sfs = File.Create(OutFile+".text");
  177. sfs.Write(buf, 0, buf.Length);
  178. sfs.Close();
  179. string posmap = String.Empty;
  180. if (m_positionMap != null)
  181. {
  182. foreach (KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> kvp in m_positionMap)
  183. {
  184. KeyValuePair<int, int> k = kvp.Key;
  185. KeyValuePair<int, int> v = kvp.Value;
  186. posmap += String.Format("{0},{1},{2},{3}\n",
  187. k.Key, k.Value, v.Key, v.Value);
  188. }
  189. }
  190. buf = enc.GetBytes(posmap);
  191. FileStream mfs = File.Create(OutFile+".map");
  192. mfs.Write(buf, 0, buf.Length);
  193. mfs.Close();
  194. return disp;
  195. }
  196. private static string ReplaceTypes(string message)
  197. {
  198. message = message.Replace(
  199. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString",
  200. "string");
  201. message = message.Replace(
  202. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger",
  203. "integer");
  204. message = message.Replace(
  205. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat",
  206. "float");
  207. message = message.Replace(
  208. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.list",
  209. "list");
  210. return message;
  211. }
  212. private static KeyValuePair<int, int> FindErrorPosition(int line, int col)
  213. {
  214. return FindErrorPosition(line, col, m_positionMap);
  215. }
  216. private class kvpSorter : IComparer<KeyValuePair<int,int>>
  217. {
  218. public int Compare(KeyValuePair<int,int> a,
  219. KeyValuePair<int,int> b)
  220. {
  221. return a.Key.CompareTo(b.Key);
  222. }
  223. }
  224. public static KeyValuePair<int, int> FindErrorPosition(int line,
  225. int col, Dictionary<KeyValuePair<int, int>,
  226. KeyValuePair<int, int>> positionMap)
  227. {
  228. if (positionMap == null || positionMap.Count == 0)
  229. return new KeyValuePair<int, int>(line, col);
  230. KeyValuePair<int, int> ret = new KeyValuePair<int, int>();
  231. if (positionMap.TryGetValue(new KeyValuePair<int, int>(line, col),
  232. out ret))
  233. return ret;
  234. List<KeyValuePair<int,int>> sorted =
  235. new List<KeyValuePair<int,int>>(positionMap.Keys);
  236. sorted.Sort(new kvpSorter());
  237. int l = 1;
  238. int c = 1;
  239. foreach (KeyValuePair<int, int> cspos in sorted)
  240. {
  241. if (cspos.Key >= line)
  242. {
  243. if (cspos.Key > line)
  244. return new KeyValuePair<int, int>(l, c);
  245. if (cspos.Value > col)
  246. return new KeyValuePair<int, int>(l, c);
  247. c = cspos.Value;
  248. if (c == 0)
  249. c++;
  250. }
  251. else
  252. {
  253. l = cspos.Key;
  254. }
  255. }
  256. return new KeyValuePair<int, int>(l, c);
  257. }
  258. }
  259. }