From 6b7930104bdb845d3b9c085dc04f52b6446f23b1 Mon Sep 17 00:00:00 2001 From: lbsa71 Date: Tue, 24 Jun 2008 21:09:49 +0000 Subject: * Applied patch from Melanie, mantis issue #1581 - "Refactor LSL language, api and compiler out of XEngine" "First stage in a major Script Engine refactor, that will result in the LSL implementaions ebing reconverged. Not there yet, but one major part is done." Thank you, Melanie! --- .../ScriptEngine/Shared/CodeTools/Compiler.cs | 515 +++++++++++++++++++++ .../Shared/CodeTools/LSL2CSConverter.cs | 374 +++++++++++++++ .../Shared/CodeTools/YP2CSConverter.cs | 108 +++++ 3 files changed, 997 insertions(+) create mode 100644 OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs create mode 100644 OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSConverter.cs create mode 100644 OpenSim/Region/ScriptEngine/Shared/CodeTools/YP2CSConverter.cs (limited to 'OpenSim/Region/ScriptEngine/Shared/CodeTools') diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs new file mode 100644 index 0000000..2edcee0 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs @@ -0,0 +1,515 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using Microsoft.CSharp; +using Microsoft.JScript; +using Microsoft.VisualBasic; +using OpenSim.Region.Environment.Interfaces; +using OpenSim.Region.ScriptEngine.Interfaces; + +namespace OpenSim.Region.ScriptEngine.Shared.CodeTools +{ + public class Compiler + { + private static readonly log4net.ILog m_log + = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + + // * Uses "LSL2Converter" to convert LSL to C# if necessary. + // * Compiles C#-code into an assembly + // * Returns assembly name ready for AppDomain load. + // + // Assembly is compiled using LSL_BaseClass as base. Look at debug C# code file created when LSL script is compiled for full details. + // + + internal enum enumCompileType + { + lsl = 0, + cs = 1, + vb = 2, + js = 3 + } + + /// + /// This contains number of lines WE use for header when compiling script. User will get error in line x-LinesToRemoveOnError when error occurs. + /// + public int LinesToRemoveOnError = 3; + private enumCompileType DefaultCompileLanguage; + private bool WriteScriptSourceToDebugFile; + private bool CompileWithDebugInformation; + private Dictionary AllowedCompilers = new Dictionary(StringComparer.CurrentCultureIgnoreCase); + private Dictionary LanguageMapping = new Dictionary(StringComparer.CurrentCultureIgnoreCase); + + private string FilePrefix; + private string ScriptEnginesPath = "ScriptEngines"; + + private static LSL2CSConverter LSL_Converter = new LSL2CSConverter(); + private static CSharpCodeProvider CScodeProvider = new CSharpCodeProvider(); + private static VBCodeProvider VBcodeProvider = new VBCodeProvider(); + private static JScriptCodeProvider JScodeProvider = new JScriptCodeProvider(); + + private static int instanceID = new Random().Next(0, int.MaxValue); // Unique number to use on our compiled files + private static UInt64 scriptCompileCounter = 0; // And a counter + + public IScriptEngine m_scriptEngine; + public Compiler(IScriptEngine scriptEngine) + { + m_scriptEngine = scriptEngine; + ReadConfig(); + } + public bool in_startup = true; + public void ReadConfig() + { + + // Get some config + WriteScriptSourceToDebugFile = m_scriptEngine.Config.GetBoolean("WriteScriptSourceToDebugFile", true); + CompileWithDebugInformation = m_scriptEngine.Config.GetBoolean("CompileWithDebugInformation", true); + + // Get file prefix from scriptengine name and make it file system safe: + FilePrefix = "CommonCompiler"; + foreach (char c in Path.GetInvalidFileNameChars()) + { + FilePrefix = FilePrefix.Replace(c, '_'); + } + + // First time we start? Delete old files + if (in_startup) + { + in_startup = false; + DeleteOldFiles(); + } + + // Map name and enum type of our supported languages + LanguageMapping.Add(enumCompileType.cs.ToString(), enumCompileType.cs); + LanguageMapping.Add(enumCompileType.vb.ToString(), enumCompileType.vb); + LanguageMapping.Add(enumCompileType.lsl.ToString(), enumCompileType.lsl); + LanguageMapping.Add(enumCompileType.js.ToString(), enumCompileType.js); + + // Allowed compilers + string allowComp = m_scriptEngine.Config.GetString("AllowedCompilers", "lsl,cs,vb,js"); + AllowedCompilers.Clear(); + +#if DEBUG + m_scriptEngine.Log.Debug("[Compiler]: Allowed languages: " + allowComp); +#endif + + + foreach (string strl in allowComp.Split(',')) + { + string strlan = strl.Trim(" \t".ToCharArray()).ToLower(); + if (!LanguageMapping.ContainsKey(strlan)) + { + m_scriptEngine.Log.Error("[Compiler]: Config error. Compiler is unable to recognize language type \"" + strlan + "\" specified in \"AllowedCompilers\"."); + } + else + { +#if DEBUG + //m_scriptEngine.Log.Debug("[Compiler]: Config OK. Compiler recognized language type \"" + strlan + "\" specified in \"AllowedCompilers\"."); +#endif + } + AllowedCompilers.Add(strlan, true); + } + if (AllowedCompilers.Count == 0) + m_scriptEngine.Log.Error("[Compiler]: Config error. Compiler could not recognize any language in \"AllowedCompilers\". Scripts will not be executed!"); + + // Default language + string defaultCompileLanguage = m_scriptEngine.Config.GetString("DefaultCompileLanguage", "lsl").ToLower(); + + // Is this language recognized at all? + if (!LanguageMapping.ContainsKey(defaultCompileLanguage)) + { + m_scriptEngine.Log.Error("[Compiler]: " + + "Config error. Default language \"" + defaultCompileLanguage + "\" specified in \"DefaultCompileLanguage\" is not recognized as a valid language. Changing default to: \"lsl\"."); + defaultCompileLanguage = "lsl"; + } + + // Is this language in allow-list? + if (!AllowedCompilers.ContainsKey(defaultCompileLanguage)) + { + m_scriptEngine.Log.Error("[Compiler]: " + + "Config error. Default language \"" + defaultCompileLanguage + "\"specified in \"DefaultCompileLanguage\" is not in list of \"AllowedCompilers\". Scripts may not be executed!"); + } + else + { +#if DEBUG +// m_scriptEngine.Log.Debug("[Compiler]: " + +// "Config OK. Default language \"" + defaultCompileLanguage + "\" specified in \"DefaultCompileLanguage\" is recognized as a valid language."); +#endif + // LANGUAGE IS IN ALLOW-LIST + DefaultCompileLanguage = LanguageMapping[defaultCompileLanguage]; + } + + // We now have an allow-list, a mapping list, and a default language + + } + + /// + /// Delete old script files + /// + private void DeleteOldFiles() + { + + // CREATE FOLDER IF IT DOESNT EXIST + if (!Directory.Exists(ScriptEnginesPath)) + { + try + { + Directory.CreateDirectory(ScriptEnginesPath); + } + catch (Exception ex) + { + m_scriptEngine.Log.Error("[Compiler]: Exception trying to create ScriptEngine directory \"" + ScriptEnginesPath + "\": " + ex.ToString()); + } + } + + if (!Directory.Exists(Path.Combine(ScriptEnginesPath, + m_scriptEngine.World.RegionInfo.RegionID.ToString()))) + { + try + { + Directory.CreateDirectory(Path.Combine(ScriptEnginesPath, + m_scriptEngine.World.RegionInfo.RegionID.ToString())); + } + catch (Exception ex) + { + m_scriptEngine.Log.Error("[Compiler]: Exception trying to create ScriptEngine directory \"" + Path.Combine(ScriptEnginesPath, + m_scriptEngine.World.RegionInfo.RegionID.ToString())+ "\": " + ex.ToString()); + } + } + + foreach (string file in Directory.GetFiles(Path.Combine(ScriptEnginesPath, + m_scriptEngine.World.RegionInfo.RegionID.ToString()))) + { + //m_scriptEngine.Log.Error("[Compiler]: FILE FOUND: " + file); + + if (file.ToLower().StartsWith(FilePrefix + "_compiled_") || + file.ToLower().StartsWith(FilePrefix + "_source_")) + { + try + { + File.Delete(file); + } + catch (Exception ex) + { + m_scriptEngine.Log.Error("[Compiler]: Exception trying delete old script file \"" + file + "\": " + ex.ToString()); + } + + } + } + + } + + ////private ICodeCompiler icc = codeProvider.CreateCompiler(); + //public string CompileFromFile(string LSOFileName) + //{ + // switch (Path.GetExtension(LSOFileName).ToLower()) + // { + // case ".txt": + // case ".lsl": + // Common.ScriptEngineBase.Shared.SendToDebug("Source code is LSL, converting to CS"); + // return CompileFromLSLText(File.ReadAllText(LSOFileName)); + // case ".cs": + // Common.ScriptEngineBase.Shared.SendToDebug("Source code is CS"); + // return CompileFromCSText(File.ReadAllText(LSOFileName)); + // default: + // throw new Exception("Unknown script type."); + // } + //} + + /// + /// Converts script from LSL to CS and calls CompileFromCSText + /// + /// LSL script + /// Filename to .dll assembly + public string PerformScriptCompile(string Script, string asset) + { + string OutFile = Path.Combine(ScriptEnginesPath, Path.Combine( + m_scriptEngine.World.RegionInfo.RegionID.ToString(), + FilePrefix + "_compiled_" + asset + ".dll")); +// string OutFile = Path.Combine(ScriptEnginesPath, +// FilePrefix + "_compiled_" + asset + ".dll"); + + if (File.Exists(OutFile)) + { + m_scriptEngine.Log.DebugFormat("[Compiler] Returning existing assembly for {0}", asset); + return OutFile; + } + + if (!Directory.Exists(ScriptEnginesPath)) + { + try + { + Directory.CreateDirectory(ScriptEnginesPath); + } + catch (Exception ex) + { + } + } + + if (!Directory.Exists(Path.Combine(ScriptEnginesPath, + m_scriptEngine.World.RegionInfo.RegionID.ToString()))) + { + try + { + Directory.CreateDirectory(ScriptEnginesPath); + } + catch (Exception ex) + { + } + } + + enumCompileType l = DefaultCompileLanguage; + + if (Script.StartsWith("//c#", true, CultureInfo.InvariantCulture)) + l = enumCompileType.cs; + if (Script.StartsWith("//vb", true, CultureInfo.InvariantCulture)) + { + l = enumCompileType.vb; + // We need to remove //vb, it won't compile with that + + Script = Script.Substring(4, Script.Length - 4); + } + if (Script.StartsWith("//lsl", true, CultureInfo.InvariantCulture)) + l = enumCompileType.lsl; + + if (Script.StartsWith("//js", true, CultureInfo.InvariantCulture)) + l = enumCompileType.js; + + if (!AllowedCompilers.ContainsKey(l.ToString())) + { + // Not allowed to compile to this language! + string errtext = String.Empty; + errtext += "The compiler for language \"" + l.ToString() + "\" is not in list of allowed compilers. Script will not be executed!"; + throw new Exception(errtext); + } + + string compileScript = Script; + + if (l == enumCompileType.lsl) + { + // Its LSL, convert it to C# + compileScript = LSL_Converter.Convert(Script); + l = enumCompileType.cs; + } + + // Insert additional assemblies here + + //ADAM: Disabled for the moment until it's working right. + bool enableCommanderLSL = false; + + if (enableCommanderLSL == true && l == enumCompileType.cs) + { + foreach (KeyValuePair com + in m_scriptEngine.World.GetCommanders()) + { + compileScript = com.Value.GenerateRuntimeAPI() + compileScript; + } + } + + // End of insert + + switch (l) + { + case enumCompileType.cs: + compileScript = CreateCSCompilerScript(compileScript); + break; + case enumCompileType.vb: + compileScript = CreateVBCompilerScript(compileScript); + break; + case enumCompileType.js: + compileScript = CreateJSCompilerScript(compileScript); + break; + } + +// m_log.Debug("[ScriptEngine.DotNetEngine]: Preparing to compile the following LSL to C# translated code"); +// m_log.Debug(""); +// m_log.Debug(compileScript); + + return CompileFromDotNetText(compileScript, l, asset); + } + + private static string CreateJSCompilerScript(string compileScript) + { + compileScript = String.Empty + + "import OpenSim.Region.ScriptEngine.Shared; import System.Collections.Generic;\r\n" + + "package SecondLife {\r\n" + + "class Script extends OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" + + compileScript + + "} }\r\n"; + return compileScript; + } + + private static string CreateCSCompilerScript(string compileScript) + { + compileScript = String.Empty + + "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\r\n" + + String.Empty + "namespace SecondLife { " + + String.Empty + "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass { \r\n" + + @"public Script() { } " + + compileScript + + "} }\r\n"; + return compileScript; + } + + private static string CreateVBCompilerScript(string compileScript) + { + compileScript = String.Empty + + "Imports OpenSim.Region.ScriptEngine.Shared: Imports System.Collections.Generic: " + + String.Empty + "NameSpace SecondLife:" + + String.Empty + "Public Class Script: Inherits OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass: " + + "\r\nPublic Sub New()\r\nEnd Sub: " + + compileScript + + ":End Class :End Namespace\r\n"; + return compileScript; + } + + /// + /// Compile .NET script to .Net assembly (.dll) + /// + /// CS script + /// Filename to .dll assembly + internal string CompileFromDotNetText(string Script, enumCompileType lang, string asset) + { + string ext = "." + lang.ToString(); + + // Output assembly name + scriptCompileCounter++; + string OutFile = Path.Combine(ScriptEnginesPath, Path.Combine( + m_scriptEngine.World.RegionInfo.RegionID.ToString(), + FilePrefix + "_compiled_" + asset + ".dll")); +#if DEBUG +// m_scriptEngine.Log.Debug("[Compiler]: Starting compile of \"" + OutFile + "\"."); +#endif + try + { + File.Delete(OutFile); + } + catch (Exception e) // NOTLEGIT - Should be just catching FileIOException + { + //m_scriptEngine.Log.Error("[Compiler]: Unable to delete old existring script-file before writing new. Compile aborted: " + e.ToString()); + throw new Exception("Unable to delete old existring script-file before writing new. Compile aborted: " + e.ToString()); + } + //string OutFile = Path.Combine("ScriptEngines", "SecondLife.Script.dll"); + + // DEBUG - write source to disk + if (WriteScriptSourceToDebugFile) + { + string srcFileName = FilePrefix + "_source_" + Path.GetFileNameWithoutExtension(OutFile) + ext; + try + { + File.WriteAllText(Path.Combine(Path.Combine( + ScriptEnginesPath, + m_scriptEngine.World.RegionInfo.RegionID.ToString()), + srcFileName), Script); + } + catch (Exception ex) // NOTLEGIT - Should be just catching FileIOException + { + m_scriptEngine.Log.Error("[Compiler]: Exception while trying to write script source to file \"" + srcFileName + "\": " + ex.ToString()); + } + } + + // Do actual compile + 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(GetType().Assembly.Location); + //Console.WriteLine("Assembly location: " + rootPath); + parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll")); + parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll")); + + parameters.GenerateExecutable = false; + parameters.OutputAssembly = OutFile; + parameters.IncludeDebugInformation = CompileWithDebugInformation; + //parameters.WarningLevel = 1; // Should be 4? + parameters.TreatWarningsAsErrors = false; + +//Console.WriteLine(Script); + CompilerResults results; + switch (lang) + { + case enumCompileType.vb: + results = VBcodeProvider.CompileAssemblyFromSource(parameters, Script); + break; + case enumCompileType.cs: + results = CScodeProvider.CompileAssemblyFromSource(parameters, Script); + break; + case enumCompileType.js: + results = JScodeProvider.CompileAssemblyFromSource(parameters, Script); + break; + default: + throw new Exception("Compiler is not able to recongnize language type \"" + lang.ToString() + "\""); + } + + // Check result + // Go through errors + + // + // WARNINGS AND ERRORS + // + if (results.Errors.Count > 0) + { + string errtext = String.Empty; + foreach (CompilerError CompErr in results.Errors) + { + errtext += "Line number " + (CompErr.Line - LinesToRemoveOnError) + + ", Error Number: " + CompErr.ErrorNumber + + ", '" + CompErr.ErrorText + "'\r\n"; + } + if (!File.Exists(OutFile)) + { + throw new Exception(errtext); + } + } + + // + // NO ERRORS, BUT NO COMPILED FILE + // + if (!File.Exists(OutFile)) + { + string errtext = String.Empty; + errtext += "No compile error. But not able to locate compiled file."; + throw new Exception(errtext); + } + m_scriptEngine.Log.DebugFormat("[Compiler] Compiled new assembly for {0}", asset); + return OutFile; + } + } +} diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSConverter.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSConverter.cs new file mode 100644 index 0000000..380686e --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSConverter.cs @@ -0,0 +1,374 @@ +/* +* Copyright (c) Contributors, http://opensimulator.org/ +* See CONTRIBUTORS.TXT for a full list of copyright holders. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of the OpenSim Project nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace OpenSim.Region.ScriptEngine.Shared.CodeTools +{ + public class LSL2CSConverter + { + // Uses regex to convert LSL code to C# code. + + //private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled); + private Dictionary dataTypes = new Dictionary(); + private Dictionary quotes = new Dictionary(); + // c Style + private Regex cstylecomments = new Regex(@"/\*(.|[\r\n])*?\*/", RegexOptions.Compiled | RegexOptions.Multiline); + // c# one liners + private Regex nonCommentFwsl = new Regex("\"[a-zA-Z0-9.,:/\\n ]+//[^\"+]+([\\\\\\\"+]+)?(\\s+)?[\"+](\\s+)?(;)?", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline); + private Regex conelinecomments = new Regex(@"[^:].?([\/]{2}[^\n]*)|([\n]{1,}[\/]{2}[^\n]*)", RegexOptions.Compiled | RegexOptions.Multiline); + // ([^\"])((?:[a-zA-Z])\.[a-zA-Z].?)([^\"]) + + // value we're looking for: (?:[a-zA-Z])\.[a-zA-Z] + public LSL2CSConverter() + { + // Only the types we need to convert + dataTypes.Add("void", "void"); + dataTypes.Add("integer", "LSL_Types.LSLInteger"); + dataTypes.Add("float", "LSL_Types.LSLFloat"); + dataTypes.Add("string", "LSL_Types.LSLString"); + dataTypes.Add("key", "LSL_Types.LSLString"); + dataTypes.Add("vector", "LSL_Types.Vector3"); + dataTypes.Add("rotation", "LSL_Types.Quaternion"); + dataTypes.Add("list", "LSL_Types.list"); + dataTypes.Add("null", "null"); + } + + public string Convert(string Script) + { + quotes.Clear(); + string Return = String.Empty; + 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 quotes's + // TODO: Does this work on half-quotes in strings? ;) + string _Script = String.Empty; + string C; + bool in_quote = false; + bool quote_replaced = false; + string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_"; + string quote = String.Empty; + bool last_was_escape = false; + int quote_replaced_count = 0; + + string removefwnoncomments = nonCommentFwsl.Replace(Script, "\"\";"); + + string removecomments = conelinecomments.Replace(removefwnoncomments, ""); + removecomments = cstylecomments.Replace(removecomments, ""); + string[] localscript = removecomments.Split('"'); + string checkscript = String.Empty; + bool flip = true; + + for (int p = 0; p < localscript.Length; p++) + { + //if (localscript[p].Length >= 1) + //{ + if (!localscript[p].EndsWith(@"\")) + { + flip = !flip; + //System.Console.WriteLine("Flip:" + flip.ToString() + " - " + localscript[p] + " ! " + localscript[p].EndsWith(@"\").ToString()); + } + //} + //else + //{ + // flip = !flip; + // System.Console.WriteLine("Flip:" + flip.ToString() + " - " + localscript[p]); + //} + if (!flip) + checkscript += localscript[p]; + } + + //System.Console.WriteLine("SCRIPT:" + checkscript); + + // checks for alpha.alpha way of referring to objects in C# + // ignores alpha.x alpha.y, alpha.z for refering to vector components + Match SecurityM; + + // BROKEN: this check is very wrong. It block's any url in strings. + SecurityM = Regex.Match(checkscript, @"(?:[a-zA-Z])\.(?:[a-wA-Z]|[a-zA-Z][a-zA-Z])", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline); + + if (SecurityM.Success) + throw new Exception("CS0103: 'The . symbol cannot be used in LSL except in float values or vector components'. Detected around: " + SecurityM.Captures[0].Value); + + SecurityM = Regex.Match(checkscript, @"typeof\s", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline); + if (SecurityM.Success) + throw new Exception("CS0103: 'The object.typeof method isn't allowed in LSL'"); + + SecurityM = Regex.Match(checkscript, @"GetType\(", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline); + if (SecurityM.Success) + throw new Exception("CS0103: 'The object.GetType method isn't allowed in LSL'"); + + 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 == String.Empty) + { + // 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 = String.Empty; + } + 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.Empty; + string cache = String.Empty; + bool in_state = false; + string current_statename = String.Empty; + 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_\(\)]*{", + Regex.Match(cache, @"(?![a-zA-Z_]+)\s*(state\s+)?(?[a-zA-Z_][a-zA-Z_0-9]*)[^a-zA-Z_0-9\(\)]*{", + + 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["statename"].Captures[0].Value; + //Console.WriteLine("Current statename: " + current_statename); + cache = + //@"(?(?![a-zA-Z_]+)\s*)" + @"([a-zA-Z_]+)(?[^a-zA-Z_\(\)]*){", + Regex.Replace(cache, + @"(?(?![a-zA-Z_]+)\s*)" + @"(state\s+)?([a-zA-Z_][a-zA-Z_0-9]*)(?[^a-zA-Z_0-9\(\)]*){", + "${s1}${s2}", + RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase); + } + ret += cache; + cache = String.Empty; + } + if (ilevel == 0 && lastlevel == 1) + { + // 1 => 0: Remove last } + if (in_state == true) + { + cache = cache.Remove(cache.Length - 1, 1); + //cache = Regex.Replace(cache, "}$", String.Empty, 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" + "" + "$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline); + //Console.WriteLine("Replacing using statename: " + current_statename); + cache = + Regex.Replace(cache, + @"^(\s*)((?!(if|switch|for|while)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", + @"$1public " + current_statename + "_event_$2", + RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase); + } + + ret += cache; + cache = String.Empty; + in_state = true; + current_statename = String.Empty; + } + + break; + } + lastlevel = ilevel; + } + ret += cache; + cache = String.Empty; + + Script = ret; + ret = String.Empty; + + 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); + Script = + Regex.Replace(Script, @"(^|;|}|[\(,])(\s*)" + key + @"(\s*)[,]", @"$1$2" + val + "$3,", + RegexOptions.Compiled | RegexOptions.Multiline); + } + + // Change jumps into goto's and prefix its label + Script = + Regex.Replace(Script, + @"(\W)jump\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*;", + @"$1goto label_$2;", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline); + // and prefix labels so the do not clash with C#'s reserved words + Script = + Regex.Replace(Script, + @"@([a-zA-Z_][a-zA-Z_0-9]*)\s*;", + @"label_$1: ;", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline); + + // 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 and + 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, @"\[([^\]]*)\]", @"new LSL_Types.list($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 "state STATENAME" with "state("statename")" + Script = + Regex.Replace(Script, @"(state)\s+([^;\n\r]+)(;[\r\n\s])", "$1(\"$2\")$3", + RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase); + + // REPLACE BACK QUOTES + foreach (string key in quotes.Keys) + { + string val; + quotes.TryGetValue(key, out val); + Script = Script.Replace(key, "\"" + val + "\""); + } + + //System.Console.WriteLine(Script); + Return = String.Empty;// + + //"using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;"; + + //Return += String.Empty + + // "namespace SecondLife { "; + //Return += String.Empty + + // //"[Serializable] " + + // "public class Script : OpenSim.Region.ScriptEngine.Shared.LSL_BaseClass { "; + //Return += @"public Script() { } "; + Return += Script; + //Return += "} }\r\n"; + + quotes.Clear(); + + return Return; + } + } +} diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/YP2CSConverter.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/YP2CSConverter.cs new file mode 100644 index 0000000..54b4861 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/YP2CSConverter.cs @@ -0,0 +1,108 @@ +/* +* Copyright (c) Contributors, http://opensimulator.org/ +* See CONTRIBUTORS.TXT for a full list of copyright holders. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of the OpenSim Project nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System; +using System.IO; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; +using OpenSim.Region.ScriptEngine.Shared.YieldProlog; + +namespace OpenSim.Region.ScriptEngine.Shared.CodeTools +{ + public class YP2CSConverter + { + public YP2CSConverter() + { + } + + public string Convert(string Script) + { + string CS_code = GenCode(Script); + return CS_code; + } + + static string GenCode(string myCode) + { + Variable TermList = new Variable(); + Variable FunctionCode = new Variable(); + + string CS_code = ""; + + int cs_pointer = myCode.IndexOf("\n//cs"); + if (cs_pointer > 0) + { + CS_code = myCode.Substring(cs_pointer); // CS code comes after + myCode = myCode.Substring(0, cs_pointer); + } + myCode.Replace("//yp", "%YPCode"); + + StringWriter myCS_SW = new StringWriter(); + StringReader myCode_SR = new StringReader(" yp_nop_header_nop. \n "+myCode + "\n"); + + YP.see(myCode_SR); + YP.tell(myCS_SW); + + //Console.WriteLine("Mycode\n ===================================\n" + myCode+"\n"); + foreach (bool l1 in Parser.parseInput(TermList)) + { + foreach (bool l2 in YPCompiler.makeFunctionPseudoCode(TermList, FunctionCode)) + { + ListPair VFC = new ListPair(FunctionCode, new Variable()); + //Console.WriteLine("-------------------------") + //Console.WriteLine(FunctionCode.ToString()) + //Console.WriteLine("-------------------------") + YPCompiler.convertFunctionCSharp(FunctionCode); + //YPCompiler.convertStringCodesCSharp(VFC); + } + } + YP.seen(); + myCS_SW.Close(); + YP.told(); + StringBuilder bu = myCS_SW.GetStringBuilder(); + string finalcode = "//YPEncoded\n" + bu.ToString(); + // FIX script events (we're in the same script) + // 'YP.script_event(Atom.a(@"sayit"),' ==> 'sayit(' + finalcode = Regex.Replace(finalcode, + @"YP.script_event\(Atom.a\(\@\""(.*?)""\)\,", + @"this.$1(", + RegexOptions.Compiled | RegexOptions.Singleline); + finalcode = Regex.Replace(finalcode, + @" static ", + @" ", + RegexOptions.Compiled | RegexOptions.Singleline); + + finalcode = CS_code+"\n\r"+ finalcode; + finalcode = Regex.Replace(finalcode, + @"PrologCallback", + @"public IEnumerable ", + RegexOptions.Compiled | RegexOptions.Singleline); + return finalcode; + } + } +} -- cgit v1.1