1
0

Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // Commented out because generated warning since m_positionMap could never be anything other than null
  38. // private static Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> m_positionMap;
  39. private static CSharpCodeProvider CScodeProvider = new CSharpCodeProvider();
  40. static void Main(string[] args)
  41. {
  42. string source = null;
  43. if (args.Length == 0)
  44. {
  45. Console.WriteLine("No input file specified");
  46. Environment.Exit(1);
  47. }
  48. if (!File.Exists(args[0]))
  49. {
  50. Console.WriteLine("Input file does not exist");
  51. Environment.Exit(1);
  52. }
  53. try
  54. {
  55. ICodeConverter cvt = (ICodeConverter) new CSCodeGenerator();
  56. source = cvt.Convert(File.ReadAllText(args[0]));
  57. }
  58. catch(Exception e)
  59. {
  60. Console.WriteLine("Conversion failed:\n"+e.Message);
  61. Environment.Exit(1);
  62. }
  63. source = CreateCSCompilerScript(source);
  64. try
  65. {
  66. Console.WriteLine(CompileFromDotNetText(source,"a.out"));
  67. }
  68. catch(Exception e)
  69. {
  70. Console.WriteLine("Conversion failed: "+e.Message);
  71. Environment.Exit(1);
  72. }
  73. Environment.Exit(0);
  74. }
  75. private static string CreateCSCompilerScript(string compileScript)
  76. {
  77. compileScript = String.Empty +
  78. "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\r\n" +
  79. String.Empty + "namespace SecondLife { " +
  80. String.Empty + "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" +
  81. @"public Script() { } " +
  82. compileScript +
  83. "} }\r\n";
  84. return compileScript;
  85. }
  86. private static string CompileFromDotNetText(string Script, string asset)
  87. {
  88. string OutFile = asset;
  89. string disp ="OK";
  90. try
  91. {
  92. File.Delete(OutFile);
  93. }
  94. catch (Exception e) // NOTLEGIT - Should be just FileIOException
  95. {
  96. throw new Exception("Unable to delete old existing "+
  97. "script-file before writing new. Compile aborted: " +
  98. e.ToString());
  99. }
  100. // Do actual compile
  101. CompilerParameters parameters = new CompilerParameters();
  102. parameters.IncludeDebugInformation = true;
  103. string rootPath =
  104. Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
  105. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  106. "OpenSim.Region.ScriptEngine.Shared.dll"));
  107. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  108. "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
  109. parameters.GenerateExecutable = false;
  110. parameters.OutputAssembly = OutFile;
  111. parameters.IncludeDebugInformation = true;
  112. parameters.WarningLevel = 1;
  113. parameters.TreatWarningsAsErrors = false;
  114. CompilerResults results = CScodeProvider.CompileAssemblyFromSource(parameters, Script);
  115. if (results.Errors.Count > 0)
  116. {
  117. string errtext = String.Empty;
  118. foreach (CompilerError CompErr in results.Errors)
  119. {
  120. string severity = CompErr.IsWarning ? "Warning" : "Error";
  121. KeyValuePair<int, int> lslPos;
  122. lslPos = FindErrorPosition(CompErr.Line, CompErr.Column);
  123. string text = CompErr.ErrorText;
  124. text = ReplaceTypes(CompErr.ErrorText);
  125. // The Second Life viewer's script editor begins
  126. // countingn lines and columns at 0, so we subtract 1.
  127. errtext += String.Format("Line ({0},{1}): {4} {2}: {3}\n",
  128. lslPos.Key - 1, lslPos.Value - 1,
  129. CompErr.ErrorNumber, text, severity);
  130. }
  131. disp = "Completed with errors";
  132. if (!File.Exists(OutFile))
  133. {
  134. throw new Exception(errtext);
  135. }
  136. }
  137. if (!File.Exists(OutFile))
  138. {
  139. string errtext = String.Empty;
  140. errtext += "No compile error. But not able to locate compiled file.";
  141. throw new Exception(errtext);
  142. }
  143. // Because windows likes to perform exclusive locks, we simply
  144. // write out a textual representation of the file here
  145. //
  146. // Read the binary file into a buffer
  147. //
  148. FileInfo fi = new FileInfo(OutFile);
  149. if (fi == null)
  150. {
  151. string errtext = String.Empty;
  152. errtext += "No compile error. But not able to stat file.";
  153. throw new Exception(errtext);
  154. }
  155. Byte[] data = new Byte[fi.Length];
  156. try
  157. {
  158. FileStream fs = File.Open(OutFile, FileMode.Open, FileAccess.Read);
  159. fs.Read(data, 0, data.Length);
  160. fs.Close();
  161. }
  162. catch (Exception)
  163. {
  164. string errtext = String.Empty;
  165. errtext += "No compile error. But not able to open file.";
  166. throw new Exception(errtext);
  167. }
  168. // Convert to base64
  169. //
  170. string filetext = System.Convert.ToBase64String(data);
  171. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  172. Byte[] buf = enc.GetBytes(filetext);
  173. FileStream sfs = File.Create(OutFile+".text");
  174. sfs.Write(buf, 0, buf.Length);
  175. sfs.Close();
  176. string posmap = String.Empty;
  177. // if (m_positionMap != null)
  178. // {
  179. // foreach (KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> kvp in m_positionMap)
  180. // {
  181. // KeyValuePair<int, int> k = kvp.Key;
  182. // KeyValuePair<int, int> v = kvp.Value;
  183. // posmap += String.Format("{0},{1},{2},{3}\n",
  184. // k.Key, k.Value, v.Key, v.Value);
  185. // }
  186. // }
  187. buf = enc.GetBytes(posmap);
  188. FileStream mfs = File.Create(OutFile+".map");
  189. mfs.Write(buf, 0, buf.Length);
  190. mfs.Close();
  191. return disp;
  192. }
  193. private static string ReplaceTypes(string message)
  194. {
  195. message = message.Replace(
  196. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString",
  197. "string");
  198. message = message.Replace(
  199. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger",
  200. "integer");
  201. message = message.Replace(
  202. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat",
  203. "float");
  204. message = message.Replace(
  205. "OpenSim.Region.ScriptEngine.Shared.LSL_Types.list",
  206. "list");
  207. return message;
  208. }
  209. private static KeyValuePair<int, int> FindErrorPosition(int line, int col)
  210. {
  211. //return FindErrorPosition(line, col, m_positionMap);
  212. return FindErrorPosition(line, col, null);
  213. }
  214. private class kvpSorter : IComparer<KeyValuePair<int,int>>
  215. {
  216. public int Compare(KeyValuePair<int,int> a,
  217. KeyValuePair<int,int> b)
  218. {
  219. return a.Key.CompareTo(b.Key);
  220. }
  221. }
  222. public static KeyValuePair<int, int> FindErrorPosition(int line,
  223. int col, Dictionary<KeyValuePair<int, int>,
  224. KeyValuePair<int, int>> positionMap)
  225. {
  226. if (positionMap == null || positionMap.Count == 0)
  227. return new KeyValuePair<int, int>(line, col);
  228. KeyValuePair<int, int> ret = new KeyValuePair<int, int>();
  229. if (positionMap.TryGetValue(new KeyValuePair<int, int>(line, col),
  230. out ret))
  231. return ret;
  232. List<KeyValuePair<int,int>> sorted =
  233. new List<KeyValuePair<int,int>>(positionMap.Keys);
  234. sorted.Sort(new kvpSorter());
  235. int l = 1;
  236. int c = 1;
  237. foreach (KeyValuePair<int, int> cspos in sorted)
  238. {
  239. if (cspos.Key >= line)
  240. {
  241. if (cspos.Key > line)
  242. return new KeyValuePair<int, int>(l, c);
  243. if (cspos.Value > col)
  244. return new KeyValuePair<int, int>(l, c);
  245. c = cspos.Value;
  246. if (c == 0)
  247. c++;
  248. }
  249. else
  250. {
  251. l = cspos.Key;
  252. }
  253. }
  254. return new KeyValuePair<int, int>(l, c);
  255. }
  256. }
  257. }