Compiler.cs 6.2 KB

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