////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."); // } //} /// <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 PerformScriptCompile(string Script, string asset) { m_positionMap = null; m_warnings.Clear(); 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 (!Directory.Exists(ScriptEnginesPath)) { try { Directory.CreateDirectory(ScriptEnginesPath); } catch (Exception) { } } if (!Directory.Exists(Path.Combine(ScriptEnginesPath, m_scriptEngine.World.RegionInfo.RegionID.ToString()))) { try { Directory.CreateDirectory(ScriptEnginesPath); } catch (Exception) { } } if (Script == String.Empty) { if (File.Exists(OutFile)) { return(OutFile); } throw new Exception("Cannot find script assembly and no script text present"); } // Don't recompile if we already have it // if (File.Exists(OutFile) && File.Exists(OutFile + ".text") && File.Exists(OutFile + ".map")) { ReadMapFile(OutFile + ".map"); return(OutFile); } 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 (Script.StartsWith("//yp", true, CultureInfo.InvariantCulture)) { l = enumCompileType.yp; } 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# LSL_Converter = (ICodeConverter) new CSCodeGenerator(); compileScript = LSL_Converter.Convert(Script); // copy converter warnings into our warnings. foreach (string warning in LSL_Converter.GetWarnings()) { AddWarning(warning); } m_positionMap = ((CSCodeGenerator)LSL_Converter).PositionMap; } if (l == enumCompileType.yp) { // Its YP, convert it to C# compileScript = YP_Converter.Convert(Script); } switch (l) { case enumCompileType.cs: case enumCompileType.lsl: compileScript = CreateCSCompilerScript(compileScript); break; case enumCompileType.vb: compileScript = CreateVBCompilerScript(compileScript); break; case enumCompileType.js: compileScript = CreateJSCompilerScript(compileScript); break; case enumCompileType.yp: compileScript = CreateYPCompilerScript(compileScript); break; } return(CompileFromDotNetText(compileScript, l, asset)); }
/// <summary> /// Converts script from LSL to CS and calls CompileFromCSText /// </summary> /// <param name="Script">LSL script</param> /// <returns>Filename to .dll assembly</returns> public void PerformScriptCompile(string Script, string asset, UUID ownerUUID, out string assembly, out Dictionary <KeyValuePair <int, int>, KeyValuePair <int, int> > linemap) { // m_log.DebugFormat("[Compiler]: Compiling script\n{0}", Script); IScriptModuleComms comms = m_scriptEngine.World.RequestModuleInterface <IScriptModuleComms>(); linemap = null; m_warnings.Clear(); assembly = GetCompilerOutput(asset); if (!Directory.Exists(ScriptEnginesPath)) { try { Directory.CreateDirectory(ScriptEnginesPath); } catch (Exception) { } } if (!Directory.Exists(Path.Combine(ScriptEnginesPath, m_scriptEngine.World.RegionInfo.RegionID.ToString()))) { try { Directory.CreateDirectory(ScriptEnginesPath); } catch (Exception) { } } // Don't recompile if we already have it // Performing 3 file exists tests for every script can still be slow if (File.Exists(assembly) && File.Exists(assembly + ".text") && File.Exists(assembly + ".map")) { // If we have already read this linemap file, then it will be in our dictionary. // Don't build another copy of the dictionary (saves memory) and certainly // don't keep reading the same file from disk multiple times. if (!m_lineMaps.ContainsKey(assembly)) { m_lineMaps[assembly] = ReadMapFile(assembly + ".map"); } linemap = m_lineMaps[assembly]; return; } if (Script == String.Empty) { throw new Exception("Cannot find script assembly and no script text present"); } enumCompileType language = DefaultCompileLanguage; if (Script.StartsWith("//c#", true, CultureInfo.InvariantCulture)) { language = enumCompileType.cs; } if (Script.StartsWith("//vb", true, CultureInfo.InvariantCulture)) { language = 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)) { language = enumCompileType.lsl; } if (Script.StartsWith("//js", true, CultureInfo.InvariantCulture)) { language = enumCompileType.js; } if (Script.StartsWith("//yp", true, CultureInfo.InvariantCulture)) { language = enumCompileType.yp; } // m_log.DebugFormat("[Compiler]: Compile language is {0}", language); if (!AllowedCompilers.ContainsKey(language.ToString())) { // Not allowed to compile to this language! string errtext = String.Empty; errtext += "The compiler for language \"" + language.ToString() + "\" is not in list of allowed compilers. Script will not be executed!"; throw new Exception(errtext); } if (m_scriptEngine.World.Permissions.CanCompileScript(ownerUUID, (int)language) == false) { // Not allowed to compile to this language! string errtext = String.Empty; errtext += ownerUUID + " is not in list of allowed users for this scripting language. Script will not be executed!"; throw new Exception(errtext); } string compileScript = Script; if (language == enumCompileType.lsl) { // Its LSL, convert it to C# LSL_Converter = (ICodeConverter) new CSCodeGenerator(comms, m_insertCoopTerminationCalls); compileScript = LSL_Converter.Convert(Script); // copy converter warnings into our warnings. foreach (string warning in LSL_Converter.GetWarnings()) { AddWarning(warning); } linemap = ((CSCodeGenerator)LSL_Converter).PositionMap; // Write the linemap to a file and save it in our dictionary for next time. m_lineMaps[assembly] = linemap; WriteMapFile(assembly + ".map", linemap); } switch (language) { case enumCompileType.cs: case enumCompileType.lsl: compileScript = CreateCSCompilerScript( compileScript, m_scriptEngine.ScriptClassName, m_scriptEngine.ScriptBaseClassName, m_scriptEngine.ScriptBaseClassParameters); break; case enumCompileType.vb: compileScript = CreateVBCompilerScript( compileScript, m_scriptEngine.ScriptClassName, m_scriptEngine.ScriptBaseClassName); break; } assembly = CompileFromDotNetText(compileScript, language, asset, assembly); }