mirror of
https://github.com/opensim/opensim.git
synced 2026-08-05 17:05:55 +08:00
remove ^M, as native storage should be UNIX format, and ^M in/out mashing
will happen on the windows side now that eol-style is correct
This commit is contained in:
@@ -1,113 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using Microsoft.CSharp;
|
||||
using System.CodeDom.Compiler;
|
||||
using System.Reflection;
|
||||
|
||||
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
||||
{
|
||||
|
||||
public class Compiler
|
||||
{
|
||||
private LSL2CSConverter LSL_Converter = new LSL2CSConverter();
|
||||
private CSharpCodeProvider codeProvider = new CSharpCodeProvider();
|
||||
private static UInt64 scriptCompileCounter = 0;
|
||||
//private ICodeCompiler icc = codeProvider.CreateCompiler();
|
||||
public string CompileFromFile(string LSOFileName)
|
||||
{
|
||||
switch (System.IO.Path.GetExtension(LSOFileName).ToLower())
|
||||
{
|
||||
case ".txt":
|
||||
case ".lsl":
|
||||
Common.SendToDebug("Source code is LSL, converting to CS");
|
||||
return CompileFromLSLText(File.ReadAllText(LSOFileName));
|
||||
case ".cs":
|
||||
Common.SendToDebug("Source code is CS");
|
||||
return CompileFromCSText(File.ReadAllText(LSOFileName));
|
||||
default:
|
||||
throw new Exception("Unknown script type.");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Converts script from LSL to CS and calls CompileFromCSText
|
||||
/// </summary>
|
||||
/// <param name="Script">LSL script</param>
|
||||
/// <returns>Filename to .dll assembly</returns>
|
||||
public string CompileFromLSLText(string Script)
|
||||
{
|
||||
return CompileFromCSText(LSL_Converter.Convert(Script));
|
||||
}
|
||||
/// <summary>
|
||||
/// Compile CS script to .Net assembly (.dll)
|
||||
/// </summary>
|
||||
/// <param name="Script">CS script</param>
|
||||
/// <returns>Filename to .dll assembly</returns>
|
||||
public string CompileFromCSText(string Script)
|
||||
{
|
||||
|
||||
|
||||
// Output assembly name
|
||||
scriptCompileCounter++;
|
||||
string OutFile = Path.Combine("ScriptEngines", "Script_" + scriptCompileCounter + ".dll");
|
||||
try
|
||||
{
|
||||
System.IO.File.Delete(OutFile);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Exception attempting to delete old compiled script: " + e.ToString());
|
||||
}
|
||||
//string OutFile = Path.Combine("ScriptEngines", "SecondLife.Script.dll");
|
||||
|
||||
// DEBUG - write source to disk
|
||||
try
|
||||
{
|
||||
File.WriteAllText(Path.Combine("ScriptEngines", "debug_" + Path.GetFileNameWithoutExtension(OutFile) + ".cs"), Script);
|
||||
}
|
||||
catch { }
|
||||
|
||||
// Do actual compile
|
||||
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
|
||||
parameters.IncludeDebugInformation = true;
|
||||
// Add all available assemblies
|
||||
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
//Console.WriteLine("Adding assembly: " + asm.Location);
|
||||
//parameters.ReferencedAssemblies.Add(asm.Location);
|
||||
}
|
||||
|
||||
string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
|
||||
string rootPathSE = Path.GetDirectoryName(this.GetType().Assembly.Location);
|
||||
//Console.WriteLine("Assembly location: " + rootPath);
|
||||
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("OpenSim.Region.Environment");
|
||||
parameters.GenerateExecutable = false;
|
||||
parameters.OutputAssembly = OutFile;
|
||||
parameters.IncludeDebugInformation = false;
|
||||
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, Script);
|
||||
|
||||
// Go through errors
|
||||
// TODO: Return errors to user somehow
|
||||
if (results.Errors.Count > 0)
|
||||
{
|
||||
|
||||
string errtext = "";
|
||||
foreach (CompilerError CompErr in results.Errors)
|
||||
{
|
||||
errtext += "Line number " + (CompErr.Line - 1) +
|
||||
", Error Number: " + CompErr.ErrorNumber +
|
||||
", '" + CompErr.ErrorText + "'\r\n";
|
||||
}
|
||||
throw new Exception(errtext);
|
||||
}
|
||||
|
||||
|
||||
return OutFile;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using Microsoft.CSharp;
|
||||
using System.CodeDom.Compiler;
|
||||
using System.Reflection;
|
||||
|
||||
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
||||
{
|
||||
|
||||
public class Compiler
|
||||
{
|
||||
private LSL2CSConverter LSL_Converter = new LSL2CSConverter();
|
||||
private CSharpCodeProvider codeProvider = new CSharpCodeProvider();
|
||||
private static UInt64 scriptCompileCounter = 0;
|
||||
//private ICodeCompiler icc = codeProvider.CreateCompiler();
|
||||
public string CompileFromFile(string LSOFileName)
|
||||
{
|
||||
switch (System.IO.Path.GetExtension(LSOFileName).ToLower())
|
||||
{
|
||||
case ".txt":
|
||||
case ".lsl":
|
||||
Common.SendToDebug("Source code is LSL, converting to CS");
|
||||
return CompileFromLSLText(File.ReadAllText(LSOFileName));
|
||||
case ".cs":
|
||||
Common.SendToDebug("Source code is CS");
|
||||
return CompileFromCSText(File.ReadAllText(LSOFileName));
|
||||
default:
|
||||
throw new Exception("Unknown script type.");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Converts script from LSL to CS and calls CompileFromCSText
|
||||
/// </summary>
|
||||
/// <param name="Script">LSL script</param>
|
||||
/// <returns>Filename to .dll assembly</returns>
|
||||
public string CompileFromLSLText(string Script)
|
||||
{
|
||||
return CompileFromCSText(LSL_Converter.Convert(Script));
|
||||
}
|
||||
/// <summary>
|
||||
/// Compile CS script to .Net assembly (.dll)
|
||||
/// </summary>
|
||||
/// <param name="Script">CS script</param>
|
||||
/// <returns>Filename to .dll assembly</returns>
|
||||
public string CompileFromCSText(string Script)
|
||||
{
|
||||
|
||||
|
||||
// Output assembly name
|
||||
scriptCompileCounter++;
|
||||
string OutFile = Path.Combine("ScriptEngines", "Script_" + scriptCompileCounter + ".dll");
|
||||
try
|
||||
{
|
||||
System.IO.File.Delete(OutFile);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Exception attempting to delete old compiled script: " + e.ToString());
|
||||
}
|
||||
//string OutFile = Path.Combine("ScriptEngines", "SecondLife.Script.dll");
|
||||
|
||||
// DEBUG - write source to disk
|
||||
try
|
||||
{
|
||||
File.WriteAllText(Path.Combine("ScriptEngines", "debug_" + Path.GetFileNameWithoutExtension(OutFile) + ".cs"), Script);
|
||||
}
|
||||
catch { }
|
||||
|
||||
// Do actual compile
|
||||
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
|
||||
parameters.IncludeDebugInformation = true;
|
||||
// Add all available assemblies
|
||||
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
//Console.WriteLine("Adding assembly: " + asm.Location);
|
||||
//parameters.ReferencedAssemblies.Add(asm.Location);
|
||||
}
|
||||
|
||||
string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
|
||||
string rootPathSE = Path.GetDirectoryName(this.GetType().Assembly.Location);
|
||||
//Console.WriteLine("Assembly location: " + rootPath);
|
||||
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("OpenSim.Region.Environment");
|
||||
parameters.GenerateExecutable = false;
|
||||
parameters.OutputAssembly = OutFile;
|
||||
parameters.IncludeDebugInformation = false;
|
||||
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, Script);
|
||||
|
||||
// Go through errors
|
||||
// TODO: Return errors to user somehow
|
||||
if (results.Errors.Count > 0)
|
||||
{
|
||||
|
||||
string errtext = "";
|
||||
foreach (CompilerError CompErr in results.Errors)
|
||||
{
|
||||
errtext += "Line number " + (CompErr.Line - 1) +
|
||||
", Error Number: " + CompErr.ErrorNumber +
|
||||
", '" + CompErr.ErrorText + "'\r\n";
|
||||
}
|
||||
throw new Exception(errtext);
|
||||
}
|
||||
|
||||
|
||||
return OutFile;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,265 +1,265 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
||||
{
|
||||
public class LSL2CSConverter
|
||||
{
|
||||
//private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled);
|
||||
private Dictionary<string, string> dataTypes = new Dictionary<string, string>();
|
||||
private Dictionary<string, string> quotes = new Dictionary<string, string>();
|
||||
|
||||
public LSL2CSConverter()
|
||||
{
|
||||
// Only the types we need to convert
|
||||
dataTypes.Add("void", "void");
|
||||
dataTypes.Add("integer", "int");
|
||||
dataTypes.Add("float", "double");
|
||||
dataTypes.Add("string", "string");
|
||||
dataTypes.Add("key", "string");
|
||||
dataTypes.Add("vector", "LSL_Types.Vector3");
|
||||
dataTypes.Add("rotation", "LSL_Types.Quaternion");
|
||||
dataTypes.Add("list", "list");
|
||||
dataTypes.Add("null", "null");
|
||||
|
||||
}
|
||||
|
||||
public string Convert(string Script)
|
||||
{
|
||||
string Return = "";
|
||||
Script = " \r\n" + Script;
|
||||
|
||||
//
|
||||
// Prepare script for processing
|
||||
//
|
||||
|
||||
// Clean up linebreaks
|
||||
Script = Regex.Replace(Script, @"\r\n", "\n");
|
||||
Script = Regex.Replace(Script, @"\n", "\r\n");
|
||||
|
||||
|
||||
// QUOTE REPLACEMENT
|
||||
// temporarily replace quotes so we can work our magic on the script without
|
||||
// always considering if we are inside our outside ""'s
|
||||
string _Script = "";
|
||||
string C;
|
||||
bool in_quote = false;
|
||||
bool quote_replaced = false;
|
||||
string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_";
|
||||
string quote = "";
|
||||
bool last_was_escape = false;
|
||||
int quote_replaced_count = 0;
|
||||
for (int p = 0; p < Script.Length; p++)
|
||||
{
|
||||
|
||||
C = Script.Substring(p, 1);
|
||||
while (true)
|
||||
{
|
||||
// found " and last was not \ so this is not an escaped \"
|
||||
if (C == "\"" && last_was_escape == false)
|
||||
{
|
||||
// Toggle inside/outside quote
|
||||
in_quote = !in_quote;
|
||||
if (in_quote)
|
||||
{
|
||||
quote_replaced_count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (quote == "")
|
||||
{
|
||||
// We didn't replace quote, probably because of empty string?
|
||||
_Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
|
||||
}
|
||||
// We just left a quote
|
||||
quotes.Add(quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
|
||||
quote = "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!in_quote)
|
||||
{
|
||||
// We are not inside a quote
|
||||
quote_replaced = false;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// We are inside a quote
|
||||
if (!quote_replaced)
|
||||
{
|
||||
// Replace quote
|
||||
_Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
|
||||
quote_replaced = true;
|
||||
}
|
||||
quote += C;
|
||||
break;
|
||||
}
|
||||
_Script += C;
|
||||
break;
|
||||
}
|
||||
last_was_escape = false;
|
||||
if (C == @"\")
|
||||
{
|
||||
last_was_escape = true;
|
||||
}
|
||||
}
|
||||
Script = _Script;
|
||||
//
|
||||
// END OF QUOTE REPLACEMENT
|
||||
//
|
||||
|
||||
|
||||
|
||||
//
|
||||
// PROCESS STATES
|
||||
// Remove state definitions and add state names to start of each event within state
|
||||
//
|
||||
int ilevel = 0;
|
||||
int lastlevel = 0;
|
||||
string ret = "";
|
||||
string cache = "";
|
||||
bool in_state = false;
|
||||
string current_statename = "";
|
||||
for (int p = 0; p < Script.Length; p++)
|
||||
{
|
||||
C = Script.Substring(p, 1);
|
||||
while (true)
|
||||
{
|
||||
// inc / dec level
|
||||
if (C == @"{")
|
||||
ilevel++;
|
||||
if (C == @"}")
|
||||
ilevel--;
|
||||
if (ilevel < 0)
|
||||
ilevel = 0;
|
||||
cache += C;
|
||||
|
||||
// if level == 0, add to return
|
||||
if (ilevel == 1 && lastlevel == 0)
|
||||
{
|
||||
// 0 => 1: Get last
|
||||
Match m = Regex.Match(cache, @"(?![a-zA-Z_]+)\s*([a-zA-Z_]+)[^a-zA-Z_\(\)]*{", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
in_state = false;
|
||||
if (m.Success)
|
||||
{
|
||||
// Go back to level 0, this is not a state
|
||||
in_state = true;
|
||||
current_statename = m.Groups[1].Captures[0].Value;
|
||||
//Console.WriteLine("Current statename: " + current_statename);
|
||||
cache = Regex.Replace(cache, @"(?<s1>(?![a-zA-Z_]+)\s*)" + @"([a-zA-Z_]+)(?<s2>[^a-zA-Z_\(\)]*){", "${s1}${s2}", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
}
|
||||
ret += cache;
|
||||
cache = "";
|
||||
}
|
||||
if (ilevel == 0 && lastlevel == 1)
|
||||
{
|
||||
// 1 => 0: Remove last }
|
||||
if (in_state == true)
|
||||
{
|
||||
cache = cache.Remove(cache.Length - 1, 1);
|
||||
//cache = Regex.Replace(cache, "}$", "", RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
//Replace function names
|
||||
// void dataserver(key query_id, string data) {
|
||||
//cache = Regex.Replace(cache, @"([^a-zA-Z_]\s*)((?!if|switch|for)[a-zA-Z_]+\s*\([^\)]*\)[^{]*{)", "$1" + "<STATE>" + "$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
//Console.WriteLine("Replacing using statename: " + current_statename);
|
||||
cache = Regex.Replace(cache, @"^(\s*)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1public " + current_statename + "_event_$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
}
|
||||
|
||||
ret += cache;
|
||||
cache = "";
|
||||
in_state = true;
|
||||
current_statename = "";
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
lastlevel = ilevel;
|
||||
}
|
||||
ret += cache;
|
||||
cache = "";
|
||||
|
||||
Script = ret;
|
||||
ret = "";
|
||||
|
||||
|
||||
|
||||
foreach (string key in dataTypes.Keys)
|
||||
{
|
||||
string val;
|
||||
dataTypes.TryGetValue(key, out val);
|
||||
|
||||
// Replace CAST - (integer) with (int)
|
||||
Script = Regex.Replace(Script, @"\(" + key + @"\)", @"(" + val + ")", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
// Replace return types and function variables - integer a() and f(integer a, integer a)
|
||||
Script = Regex.Replace(Script, @"(^|;|}|[\(,])(\s*)" + key + @"(\s*)", @"$1$2" + val + "$3", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
}
|
||||
|
||||
// Add "void" in front of functions that needs it
|
||||
Script = Regex.Replace(Script, @"^(\s*public\s+)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
// Replace <x,y,z> and <x,y,z,r>
|
||||
Script = Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Quaternion($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
Script = Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Vector3($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
// Replace List []'s
|
||||
Script = Regex.Replace(Script, @"\[([^\]]*)\]", @"List.Parse($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
|
||||
// Replace (string) to .ToString() //
|
||||
Script = Regex.Replace(Script, @"\(string\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.ToString()", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
Script = Regex.Replace(Script, @"\((float|int)\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.Parse($2)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
|
||||
// REPLACE BACK QUOTES
|
||||
foreach (string key in quotes.Keys)
|
||||
{
|
||||
string val;
|
||||
quotes.TryGetValue(key, out val);
|
||||
Script = Script.Replace(key, "\"" + val + "\"");
|
||||
}
|
||||
|
||||
|
||||
// Add namespace, class name and inheritance
|
||||
|
||||
Return = "";// +
|
||||
//"using System; " +
|
||||
//"using System.Collections.Generic; " +
|
||||
//"using System.Text; " +
|
||||
//"using OpenSim.Region.ScriptEngine.Common; " +
|
||||
//"using integer = System.Int32; " +
|
||||
//"using key = System.String; ";
|
||||
|
||||
//// Make a Using out of DataTypes
|
||||
//// Using integer = System.Int32;
|
||||
//string _val;
|
||||
//foreach (string key in DataTypes.Keys)
|
||||
//{
|
||||
// DataTypes.TryGetValue(key, out _val);
|
||||
// if (key != _val)
|
||||
// {
|
||||
// Return += "using " + key + " = " + _val + "; ";
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
Return += "" +
|
||||
"namespace SecondLife { ";
|
||||
Return += "" +
|
||||
//"[Serializable] " +
|
||||
"public class Script : OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass { ";
|
||||
Return += @"public Script() { } ";
|
||||
Return += Script;
|
||||
Return += "} }\r\n";
|
||||
|
||||
return Return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
||||
{
|
||||
public class LSL2CSConverter
|
||||
{
|
||||
//private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled);
|
||||
private Dictionary<string, string> dataTypes = new Dictionary<string, string>();
|
||||
private Dictionary<string, string> quotes = new Dictionary<string, string>();
|
||||
|
||||
public LSL2CSConverter()
|
||||
{
|
||||
// Only the types we need to convert
|
||||
dataTypes.Add("void", "void");
|
||||
dataTypes.Add("integer", "int");
|
||||
dataTypes.Add("float", "double");
|
||||
dataTypes.Add("string", "string");
|
||||
dataTypes.Add("key", "string");
|
||||
dataTypes.Add("vector", "LSL_Types.Vector3");
|
||||
dataTypes.Add("rotation", "LSL_Types.Quaternion");
|
||||
dataTypes.Add("list", "list");
|
||||
dataTypes.Add("null", "null");
|
||||
|
||||
}
|
||||
|
||||
public string Convert(string Script)
|
||||
{
|
||||
string Return = "";
|
||||
Script = " \r\n" + Script;
|
||||
|
||||
//
|
||||
// Prepare script for processing
|
||||
//
|
||||
|
||||
// Clean up linebreaks
|
||||
Script = Regex.Replace(Script, @"\r\n", "\n");
|
||||
Script = Regex.Replace(Script, @"\n", "\r\n");
|
||||
|
||||
|
||||
// QUOTE REPLACEMENT
|
||||
// temporarily replace quotes so we can work our magic on the script without
|
||||
// always considering if we are inside our outside ""'s
|
||||
string _Script = "";
|
||||
string C;
|
||||
bool in_quote = false;
|
||||
bool quote_replaced = false;
|
||||
string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_";
|
||||
string quote = "";
|
||||
bool last_was_escape = false;
|
||||
int quote_replaced_count = 0;
|
||||
for (int p = 0; p < Script.Length; p++)
|
||||
{
|
||||
|
||||
C = Script.Substring(p, 1);
|
||||
while (true)
|
||||
{
|
||||
// found " and last was not \ so this is not an escaped \"
|
||||
if (C == "\"" && last_was_escape == false)
|
||||
{
|
||||
// Toggle inside/outside quote
|
||||
in_quote = !in_quote;
|
||||
if (in_quote)
|
||||
{
|
||||
quote_replaced_count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (quote == "")
|
||||
{
|
||||
// We didn't replace quote, probably because of empty string?
|
||||
_Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
|
||||
}
|
||||
// We just left a quote
|
||||
quotes.Add(quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
|
||||
quote = "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!in_quote)
|
||||
{
|
||||
// We are not inside a quote
|
||||
quote_replaced = false;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// We are inside a quote
|
||||
if (!quote_replaced)
|
||||
{
|
||||
// Replace quote
|
||||
_Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
|
||||
quote_replaced = true;
|
||||
}
|
||||
quote += C;
|
||||
break;
|
||||
}
|
||||
_Script += C;
|
||||
break;
|
||||
}
|
||||
last_was_escape = false;
|
||||
if (C == @"\")
|
||||
{
|
||||
last_was_escape = true;
|
||||
}
|
||||
}
|
||||
Script = _Script;
|
||||
//
|
||||
// END OF QUOTE REPLACEMENT
|
||||
//
|
||||
|
||||
|
||||
|
||||
//
|
||||
// PROCESS STATES
|
||||
// Remove state definitions and add state names to start of each event within state
|
||||
//
|
||||
int ilevel = 0;
|
||||
int lastlevel = 0;
|
||||
string ret = "";
|
||||
string cache = "";
|
||||
bool in_state = false;
|
||||
string current_statename = "";
|
||||
for (int p = 0; p < Script.Length; p++)
|
||||
{
|
||||
C = Script.Substring(p, 1);
|
||||
while (true)
|
||||
{
|
||||
// inc / dec level
|
||||
if (C == @"{")
|
||||
ilevel++;
|
||||
if (C == @"}")
|
||||
ilevel--;
|
||||
if (ilevel < 0)
|
||||
ilevel = 0;
|
||||
cache += C;
|
||||
|
||||
// if level == 0, add to return
|
||||
if (ilevel == 1 && lastlevel == 0)
|
||||
{
|
||||
// 0 => 1: Get last
|
||||
Match m = Regex.Match(cache, @"(?![a-zA-Z_]+)\s*([a-zA-Z_]+)[^a-zA-Z_\(\)]*{", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
in_state = false;
|
||||
if (m.Success)
|
||||
{
|
||||
// Go back to level 0, this is not a state
|
||||
in_state = true;
|
||||
current_statename = m.Groups[1].Captures[0].Value;
|
||||
//Console.WriteLine("Current statename: " + current_statename);
|
||||
cache = Regex.Replace(cache, @"(?<s1>(?![a-zA-Z_]+)\s*)" + @"([a-zA-Z_]+)(?<s2>[^a-zA-Z_\(\)]*){", "${s1}${s2}", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
}
|
||||
ret += cache;
|
||||
cache = "";
|
||||
}
|
||||
if (ilevel == 0 && lastlevel == 1)
|
||||
{
|
||||
// 1 => 0: Remove last }
|
||||
if (in_state == true)
|
||||
{
|
||||
cache = cache.Remove(cache.Length - 1, 1);
|
||||
//cache = Regex.Replace(cache, "}$", "", RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
//Replace function names
|
||||
// void dataserver(key query_id, string data) {
|
||||
//cache = Regex.Replace(cache, @"([^a-zA-Z_]\s*)((?!if|switch|for)[a-zA-Z_]+\s*\([^\)]*\)[^{]*{)", "$1" + "<STATE>" + "$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
//Console.WriteLine("Replacing using statename: " + current_statename);
|
||||
cache = Regex.Replace(cache, @"^(\s*)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1public " + current_statename + "_event_$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
}
|
||||
|
||||
ret += cache;
|
||||
cache = "";
|
||||
in_state = true;
|
||||
current_statename = "";
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
lastlevel = ilevel;
|
||||
}
|
||||
ret += cache;
|
||||
cache = "";
|
||||
|
||||
Script = ret;
|
||||
ret = "";
|
||||
|
||||
|
||||
|
||||
foreach (string key in dataTypes.Keys)
|
||||
{
|
||||
string val;
|
||||
dataTypes.TryGetValue(key, out val);
|
||||
|
||||
// Replace CAST - (integer) with (int)
|
||||
Script = Regex.Replace(Script, @"\(" + key + @"\)", @"(" + val + ")", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
// Replace return types and function variables - integer a() and f(integer a, integer a)
|
||||
Script = Regex.Replace(Script, @"(^|;|}|[\(,])(\s*)" + key + @"(\s*)", @"$1$2" + val + "$3", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||
}
|
||||
|
||||
// Add "void" in front of functions that needs it
|
||||
Script = Regex.Replace(Script, @"^(\s*public\s+)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
// Replace <x,y,z> and <x,y,z,r>
|
||||
Script = Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Quaternion($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
Script = Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Vector3($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
// Replace List []'s
|
||||
Script = Regex.Replace(Script, @"\[([^\]]*)\]", @"List.Parse($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
|
||||
// Replace (string) to .ToString() //
|
||||
Script = Regex.Replace(Script, @"\(string\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.ToString()", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
Script = Regex.Replace(Script, @"\((float|int)\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.Parse($2)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||
|
||||
|
||||
// REPLACE BACK QUOTES
|
||||
foreach (string key in quotes.Keys)
|
||||
{
|
||||
string val;
|
||||
quotes.TryGetValue(key, out val);
|
||||
Script = Script.Replace(key, "\"" + val + "\"");
|
||||
}
|
||||
|
||||
|
||||
// Add namespace, class name and inheritance
|
||||
|
||||
Return = "";// +
|
||||
//"using System; " +
|
||||
//"using System.Collections.Generic; " +
|
||||
//"using System.Text; " +
|
||||
//"using OpenSim.Region.ScriptEngine.Common; " +
|
||||
//"using integer = System.Int32; " +
|
||||
//"using key = System.String; ";
|
||||
|
||||
//// Make a Using out of DataTypes
|
||||
//// Using integer = System.Int32;
|
||||
//string _val;
|
||||
//foreach (string key in DataTypes.Keys)
|
||||
//{
|
||||
// DataTypes.TryGetValue(key, out _val);
|
||||
// if (key != _val)
|
||||
// {
|
||||
// Return += "using " + key + " = " + _val + "; ";
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
Return += "" +
|
||||
"namespace SecondLife { ";
|
||||
Return += "" +
|
||||
//"[Serializable] " +
|
||||
"public class Script : OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass { ";
|
||||
Return += @"public Script() { } ";
|
||||
Return += Script;
|
||||
Return += "} }\r\n";
|
||||
|
||||
return Return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user