* fixed script compilation

* reversed //c# - if
  * rightifyed what's actually converted
afrisby
lbsa71 2007-09-14 09:25:07 +00:00
parent 8849637493
commit 1b1808d45d
2 changed files with 125 additions and 113 deletions

View File

@ -1,120 +1,120 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.IO; using System.IO;
using Microsoft.CSharp; using Microsoft.CSharp;
using System.CodeDom.Compiler; using System.CodeDom.Compiler;
using System.Reflection; using System.Reflection;
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
{ {
public class Compiler public class Compiler
{ {
private LSL2CSConverter LSL_Converter = new LSL2CSConverter(); private LSL2CSConverter LSL_Converter = new LSL2CSConverter();
private CSharpCodeProvider codeProvider = new CSharpCodeProvider(); private CSharpCodeProvider codeProvider = new CSharpCodeProvider();
private static UInt64 scriptCompileCounter = 0; private static UInt64 scriptCompileCounter = 0;
//private ICodeCompiler icc = codeProvider.CreateCompiler(); //private ICodeCompiler icc = codeProvider.CreateCompiler();
public string CompileFromFile(string LSOFileName) public string CompileFromFile(string LSOFileName)
{ {
switch (System.IO.Path.GetExtension(LSOFileName).ToLower()) switch (System.IO.Path.GetExtension(LSOFileName).ToLower())
{ {
case ".txt": case ".txt":
case ".lsl": case ".lsl":
Common.SendToDebug("Source code is LSL, converting to CS"); Common.SendToDebug("Source code is LSL, converting to CS");
return CompileFromLSLText(File.ReadAllText(LSOFileName)); return CompileFromLSLText(File.ReadAllText(LSOFileName));
case ".cs": case ".cs":
Common.SendToDebug("Source code is CS"); Common.SendToDebug("Source code is CS");
return CompileFromCSText(File.ReadAllText(LSOFileName)); return CompileFromCSText(File.ReadAllText(LSOFileName));
default: default:
throw new Exception("Unknown script type."); throw new Exception("Unknown script type.");
} }
} }
/// <summary> /// <summary>
/// Converts script from LSL to CS and calls CompileFromCSText /// Converts script from LSL to CS and calls CompileFromCSText
/// </summary> /// </summary>
/// <param name="Script">LSL script</param> /// <param name="Script">LSL script</param>
/// <returns>Filename to .dll assembly</returns> /// <returns>Filename to .dll assembly</returns>
public string CompileFromLSLText(string Script) public string CompileFromLSLText(string Script)
{ {
if (Script.Substring(0, 4).ToLower() == "//c#") if (Script.Substring(0, 4).ToLower() == "//c#")
{ {
return LSL_Converter.Convert(Script); return CompileFromCSText( Script );
} }
else else
{ {
return CompileFromCSText(LSL_Converter.Convert(Script)); return CompileFromCSText(LSL_Converter.Convert(Script));
} }
} }
/// <summary> /// <summary>
/// Compile CS script to .Net assembly (.dll) /// Compile CS script to .Net assembly (.dll)
/// </summary> /// </summary>
/// <param name="Script">CS script</param> /// <param name="Script">CS script</param>
/// <returns>Filename to .dll assembly</returns> /// <returns>Filename to .dll assembly</returns>
public string CompileFromCSText(string Script) public string CompileFromCSText(string Script)
{ {
// Output assembly name // Output assembly name
scriptCompileCounter++; scriptCompileCounter++;
string OutFile = Path.Combine("ScriptEngines", "Script_" + scriptCompileCounter + ".dll"); string OutFile = Path.Combine("ScriptEngines", "Script_" + scriptCompileCounter + ".dll");
try try
{ {
System.IO.File.Delete(OutFile); System.IO.File.Delete(OutFile);
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("Exception attempting to delete old compiled script: " + e.ToString()); Console.WriteLine("Exception attempting to delete old compiled script: " + e.ToString());
} }
//string OutFile = Path.Combine("ScriptEngines", "SecondLife.Script.dll"); //string OutFile = Path.Combine("ScriptEngines", "SecondLife.Script.dll");
// DEBUG - write source to disk // DEBUG - write source to disk
try try
{ {
File.WriteAllText(Path.Combine("ScriptEngines", "debug_" + Path.GetFileNameWithoutExtension(OutFile) + ".cs"), Script); File.WriteAllText(Path.Combine("ScriptEngines", "debug_" + Path.GetFileNameWithoutExtension(OutFile) + ".cs"), Script);
} }
catch { } catch { }
// Do actual compile // Do actual compile
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.IncludeDebugInformation = true; parameters.IncludeDebugInformation = true;
// Add all available assemblies // Add all available assemblies
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{ {
//Console.WriteLine("Adding assembly: " + asm.Location); //Console.WriteLine("Adding assembly: " + asm.Location);
//parameters.ReferencedAssemblies.Add(asm.Location); //parameters.ReferencedAssemblies.Add(asm.Location);
} }
string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory); string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
string rootPathSE = Path.GetDirectoryName(this.GetType().Assembly.Location); string rootPathSE = Path.GetDirectoryName(this.GetType().Assembly.Location);
//Console.WriteLine("Assembly location: " + rootPath); //Console.WriteLine("Assembly location: " + rootPath);
parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Common.dll")); parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Common.dll"));
parameters.ReferencedAssemblies.Add(Path.Combine(rootPathSE, "OpenSim.Region.ScriptEngine.DotNetEngine.dll")); parameters.ReferencedAssemblies.Add(Path.Combine(rootPathSE, "OpenSim.Region.ScriptEngine.DotNetEngine.dll"));
//parameters.ReferencedAssemblies.Add("OpenSim.Region.Environment"); //parameters.ReferencedAssemblies.Add("OpenSim.Region.Environment");
parameters.GenerateExecutable = false; parameters.GenerateExecutable = false;
parameters.OutputAssembly = OutFile; parameters.OutputAssembly = OutFile;
parameters.IncludeDebugInformation = false; parameters.IncludeDebugInformation = false;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, Script); CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, Script);
// Go through errors // Go through errors
// TODO: Return errors to user somehow // TODO: Return errors to user somehow
if (results.Errors.Count > 0) if (results.Errors.Count > 0)
{ {
string errtext = ""; string errtext = "";
foreach (CompilerError CompErr in results.Errors) foreach (CompilerError CompErr in results.Errors)
{ {
errtext += "Line number " + (CompErr.Line - 1) + errtext += "Line number " + (CompErr.Line - 1) +
", Error Number: " + CompErr.ErrorNumber + ", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + "'\r\n"; ", '" + CompErr.ErrorText + "'\r\n";
} }
throw new Exception(errtext); throw new Exception(errtext);
} }
return OutFile; return OutFile;
} }
} }
} }

View File

@ -0,0 +1,12 @@
//c#
namespace SecondLife {
public class Script : OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass
{
public Script() { }
public void default_event_state_entry( )
{
llSay(0, "testing, I've been touched");
}
}}