1
0

Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 System.Text;
  31. using Microsoft.CSharp;
  32. using OpenSim.Region.ScriptEngine.Shared.CodeTools;
  33. using System.CodeDom.Compiler;
  34. namespace OpenSim.Tools.LSL.Compiler
  35. {
  36. class Program
  37. {
  38. // Commented out because generated warning since m_positionMap could never be anything other than null
  39. // private static Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> m_positionMap;
  40. private static CSharpCodeProvider CScodeProvider = new CSharpCodeProvider();
  41. static void Main(string[] args)
  42. {
  43. string source = null;
  44. if (args.Length == 0)
  45. {
  46. Console.WriteLine("No input file specified");
  47. Environment.Exit(1);
  48. }
  49. if (!File.Exists(args[0]))
  50. {
  51. Console.WriteLine("Input file does not exist");
  52. Environment.Exit(1);
  53. }
  54. try
  55. {
  56. ICodeConverter cvt = (ICodeConverter) new CSCodeGenerator();
  57. source = cvt.Convert(File.ReadAllText(args[0]));
  58. }
  59. catch(Exception e)
  60. {
  61. Console.WriteLine("Conversion failed:\n"+e.Message);
  62. Environment.Exit(1);
  63. }
  64. source = CreateCSCompilerScript(source);
  65. try
  66. {
  67. Console.WriteLine(CompileFromDotNetText(source,"a.out"));
  68. }
  69. catch(Exception e)
  70. {
  71. Console.WriteLine("Conversion failed: "+e.Message);
  72. Environment.Exit(1);
  73. }
  74. Environment.Exit(0);
  75. }
  76. private static string CreateCSCompilerScript(string compileScript)
  77. {
  78. compileScript = String.Empty +
  79. "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\r\n" +
  80. String.Empty + "namespace SecondLife { " +
  81. String.Empty + "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" +
  82. @"public Script() { } " +
  83. compileScript +
  84. "} }\r\n";
  85. return compileScript;
  86. }
  87. private static string CompileFromDotNetText(string Script, string asset)
  88. {
  89. string OutFile = asset;
  90. string disp ="OK";
  91. try
  92. {
  93. File.Delete(OutFile);
  94. }
  95. catch (Exception e) // NOTLEGIT - Should be just FileIOException
  96. {
  97. throw new Exception("Unable to delete old existing "+
  98. "script-file before writing new. Compile aborted: " +
  99. e.ToString());
  100. }
  101. // Do actual compile
  102. CompilerParameters parameters = new CompilerParameters();
  103. parameters.IncludeDebugInformation = true;
  104. string rootPath =
  105. Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
  106. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  107. "OpenSim.Region.ScriptEngine.Shared.dll"));
  108. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
  109. "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
  110. parameters.GenerateExecutable = false;
  111. parameters.OutputAssembly = OutFile;
  112. parameters.IncludeDebugInformation = true;
  113. parameters.WarningLevel = 1;
  114. parameters.TreatWarningsAsErrors = false;
  115. CompilerResults results = CScodeProvider.CompileAssemblyFromSource(parameters, Script);
  116. if (results.Errors.Count > 0)
  117. {
  118. string errtext = String.Empty;
  119. foreach (CompilerError CompErr in results.Errors)
  120. {
  121. string severity = CompErr.IsWarning ? "Warning" : "Error";
  122. KeyValuePair<int, int> lslPos;
  123. lslPos = FindErrorPosition(CompErr.Line, CompErr.Column);
  124. string text = CompErr.ErrorText;
  125. text = ReplaceTypes(CompErr.ErrorText);
  126. // The Second Life viewer's script editor begins
  127. // countingn lines and columns at 0, so we subtract 1.
  128. errtext += String.Format("Line ({0},{1}): {4} {2}: {3}\n",
  129. lslPos.Key - 1, lslPos.Value - 1,
  130. CompErr.ErrorNumber, text, severity);
  131. }
  132. disp = "Completed with errors";
  133. if (!File.Exists(OutFile))
  134. {
  135. throw new Exception(errtext);
  136. }
  137. }
  138. if (!File.Exists(OutFile))
  139. {
  140. string errtext = String.Empty;
  141. errtext += "No compile error. But not able to locate compiled file.";
  142. throw new Exception(errtext);
  143. }
  144. // Because windows likes to perform exclusive locks, we simply
  145. // write out a textual representation of the file here
  146. //
  147. // Read the binary file into a buffer
  148. //
  149. FileInfo fi = new FileInfo(OutFile);
  150. if (fi == null)
  151. {
  152. string errtext = String.Empty;
  153. errtext += "No compile error. But not able to stat file.";
  154. throw new Exception(errtext);
  155. }
  156. Byte[] data = new Byte[fi.Length];
  157. try
  158. {
  159. FileStream fs = File.Open(OutFile, FileMode.Open, FileAccess.Read);
  160. fs.Read(data, 0, data.Length);
  161. fs.Close();
  162. }
  163. catch (Exception)
  164. {
  165. string errtext = String.Empty;
  166. errtext += "No compile error. But not able to open file.";
  167. throw new Exception(errtext);
  168. }
  169. // Convert to base64
  170. //
  171. string filetext = System.Convert.ToBase64String(data);
  172. Byte[] buf = Encoding.ASCII.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 = Encoding.ASCII.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. }