* Optimized usings

* Shortened type references
* Removed redundant 'this' qualifier
This commit is contained in:
lbsa71
2007-10-30 09:05:31 +00:00
parent c32d1f0562
commit 67e12b95ea
353 changed files with 15459 additions and 10338 deletions

View File

@@ -27,16 +27,13 @@
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using Microsoft.CSharp;
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
{
public class Compiler
{
private LSL2CSConverter LSL_Converter = new LSL2CSConverter();
@@ -45,7 +42,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
//private ICodeCompiler icc = codeProvider.CreateCompiler();
public string CompileFromFile(string LSOFileName)
{
switch (System.IO.Path.GetExtension(LSOFileName).ToLower())
switch (Path.GetExtension(LSOFileName).ToLower())
{
case ".txt":
case ".lsl":
@@ -58,6 +55,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
throw new Exception("Unknown script type.");
}
}
/// <summary>
/// Converts script from LSL to CS and calls CompileFromCSText
/// </summary>
@@ -67,13 +65,14 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
{
if (Script.Substring(0, 4).ToLower() == "//c#")
{
return CompileFromCSText( Script );
return CompileFromCSText(Script);
}
else
{
return CompileFromCSText(LSL_Converter.Convert(Script));
}
}
/// <summary>
/// Compile CS script to .Net assembly (.dll)
/// </summary>
@@ -81,14 +80,12 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
/// <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);
File.Delete(OutFile);
}
catch (Exception e)
{
@@ -99,12 +96,15 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
// DEBUG - write source to disk
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
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
CompilerParameters parameters = new CompilerParameters();
parameters.IncludeDebugInformation = true;
// Add all available assemblies
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
@@ -114,11 +114,11 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
}
string rootPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
string rootPathSE = Path.GetDirectoryName(this.GetType().Assembly.Location);
string rootPathSE = Path.GetDirectoryName(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;
@@ -129,13 +129,12 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
// 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";
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + "'\r\n";
}
throw new Exception(errtext);
}
@@ -143,6 +142,5 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
return OutFile;
}
}
}
}

View File

@@ -26,9 +26,7 @@
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
@@ -51,9 +49,8 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
dataTypes.Add("rotation", "LSL_Types.Quaternion");
dataTypes.Add("list", "list");
dataTypes.Add("null", "null");
}
public string Convert(string Script)
{
string Return = "";
@@ -81,7 +78,6 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
int quote_replaced_count = 0;
for (int p = 0; p < Script.Length; p++)
{
C = Script.Substring(p, 1);
while (true)
{
@@ -99,10 +95,13 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
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]);
_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);
quotes.Add(
quote_replacement_string +
quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
quote = "";
}
break;
@@ -112,7 +111,6 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
{
// We are not inside a quote
quote_replaced = false;
}
else
{
@@ -120,7 +118,8 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
if (!quote_replaced)
{
// Replace quote
_Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
_Script += quote_replacement_string +
quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
quote_replaced = true;
}
quote += C;
@@ -141,7 +140,6 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
//
//
// PROCESS STATES
// Remove state definitions and add state names to start of each event within state
@@ -170,7 +168,9 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
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);
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)
@@ -179,7 +179,11 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
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);
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 = "";
@@ -196,7 +200,11 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
// 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);
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;
@@ -216,32 +224,48 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
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);
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);
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);
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);
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);
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);
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
@@ -256,7 +280,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
// Add namespace, class name and inheritance
Return = "" +
"using OpenSim.Region.ScriptEngine.Common;";
"using OpenSim.Region.ScriptEngine.Common;";
//"using System; " +
//"using System.Collections.Generic; " +
//"using System.Text; " +
@@ -278,17 +302,15 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
Return += "" +
"namespace SecondLife { ";
Return += "" +
//"[Serializable] " +
"public class Script : OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass { ";
"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;
}
}
}
}