1
0

Compiler.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 OpenSim 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.CodeDom.Compiler;
  29. using System.IO;
  30. using System.Reflection;
  31. using Microsoft.CSharp;
  32. namespace OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL
  33. {
  34. public class Compiler
  35. {
  36. private LSL2CSConverter LSL_Converter = new LSL2CSConverter();
  37. private CSharpCodeProvider codeProvider = new CSharpCodeProvider();
  38. private static UInt64 scriptCompileCounter = 0;
  39. //private ICodeCompiler icc = codeProvider.CreateCompiler();
  40. public string CompileFromFile(string LSOFileName)
  41. {
  42. switch (Path.GetExtension(LSOFileName).ToLower())
  43. {
  44. case ".txt":
  45. case ".lsl":
  46. Common.SendToDebug("Source code is LSL, converting to CS");
  47. return CompileFromLSLText(File.ReadAllText(LSOFileName));
  48. case ".cs":
  49. Common.SendToDebug("Source code is CS");
  50. return CompileFromCSText(File.ReadAllText(LSOFileName));
  51. default:
  52. throw new Exception("Unknown script type.");
  53. }
  54. }
  55. /// <summary>
  56. /// Converts script from LSL to CS and calls CompileFromCSText
  57. /// </summary>
  58. /// <param name="Script">LSL script</param>
  59. /// <returns>Filename to .dll assembly</returns>
  60. public string CompileFromLSLText(string Script)
  61. {
  62. if (Script.Substring(0, 4).ToLower() == "//c#")
  63. {
  64. return CompileFromCSText(Script);
  65. }
  66. else
  67. {
  68. return CompileFromCSText(LSL_Converter.Convert(Script));
  69. }
  70. }
  71. /// <summary>
  72. /// Compile CS script to .Net assembly (.dll)
  73. /// </summary>
  74. /// <param name="Script">CS script</param>
  75. /// <returns>Filename to .dll assembly</returns>
  76. public string CompileFromCSText(string Script)
  77. {
  78. // Output assembly name
  79. scriptCompileCounter++;
  80. string OutFile = Path.Combine("ScriptEngines", "Script_" + scriptCompileCounter + ".dll");
  81. try
  82. {
  83. File.Delete(OutFile);
  84. }
  85. catch (Exception e)
  86. {
  87. Console.WriteLine("Exception attempting to delete old compiled script: " + e.ToString());
  88. }
  89. //string OutFile = Path.Combine("ScriptEngines", "SecondLife.Script.dll");
  90. // DEBUG - write source to disk
  91. try
  92. {
  93. File.WriteAllText(
  94. Path.Combine("ScriptEngines", "debug_" + Path.GetFileNameWithoutExtension(OutFile) + ".cs"), Script);
  95. }
  96. catch
  97. {
  98. }
  99. // Do actual compile
  100. CompilerParameters parameters = new CompilerParameters();
  101. parameters.IncludeDebugInformation = true;
  102. // Add all available assemblies
  103. foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
  104. {
  105. //Console.WriteLine("Adding assembly: " + asm.Location);
  106. //parameters.ReferencedAssemblies.Add(asm.Location);
  107. }
  108. string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
  109. string rootPathSE = Path.GetDirectoryName(GetType().Assembly.Location);
  110. //Console.WriteLine("Assembly location: " + rootPath);
  111. parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Common.dll"));
  112. parameters.ReferencedAssemblies.Add(Path.Combine(rootPathSE, "OpenSim.Grid.ScriptEngine.DotNetEngine.dll"));
  113. //parameters.ReferencedAssemblies.Add("OpenSim.Region.Environment");
  114. parameters.GenerateExecutable = false;
  115. parameters.OutputAssembly = OutFile;
  116. parameters.IncludeDebugInformation = false;
  117. CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, Script);
  118. // Go through errors
  119. // TODO: Return errors to user somehow
  120. if (results.Errors.Count > 0)
  121. {
  122. string errtext = "";
  123. foreach (CompilerError CompErr in results.Errors)
  124. {
  125. errtext += "Line number " + (CompErr.Line - 1) +
  126. ", Error Number: " + CompErr.ErrorNumber +
  127. ", '" + CompErr.ErrorText + "'\r\n";
  128. }
  129. throw new Exception(errtext);
  130. }
  131. return OutFile;
  132. }
  133. }
  134. }