/// <summary> /// Does the compilation of the script into an assembly. The assembly is stored together with /// the read-only source code and returned as result. As list of compiled source codes is maintained by this class. /// If you provide a text that was already compiled before, the already compiled assembly is returned instead /// of a freshly compiled assembly. /// </summary> /// <returns>True if successfully compiles, otherwise false.</returns> public static IScriptCompilerResult Compile(string[] scriptText, out string[] errors) { ScriptCompilerResult result; string scriptTextHash = ScriptCompilerResult.ComputeScriptTextHash(scriptText); if (_compilerResults.TryGetValue(scriptTextHash, out result)) { errors = null; return result; } Dictionary<string, string> providerOptions = new Dictionary<string, string>(); providerOptions.Add("CompilerVersion", "v4.0"); Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions); // For Visual Basic Compiler try this : //Microsoft.VisualBasic.VBCodeProvider System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.IncludeDebugInformation = true; // parameters.OutputAssembly = this.ScriptName; // Add available assemblies including the application itself foreach (string loc in Settings.Scripting.ReferencedAssemblies.AllLocations) parameters.ReferencedAssemblies.Add(loc); CompilerResults results; if (scriptText.Length == 1) results = codeProvider.CompileAssemblyFromSource(parameters, scriptText[0]); else results = codeProvider.CompileAssemblyFromSource(parameters, scriptText); if (results.Errors.Count > 0) { errors = new string[results.Errors.Count]; int i = 0; foreach (CompilerError err in results.Errors) { errors[i++] = err.ToString(); } return null; } else { result = new ScriptCompilerResult(scriptText, scriptTextHash, results.CompiledAssembly); _compilerResults.TryAdd(result); errors = null; return result; } }
internal bool TryGetValue(string scriptTextHash, out ScriptCompilerResult result) { if (string.IsNullOrEmpty(scriptTextHash)) throw new ArgumentNullException("scriptTextHash"); _lock.EnterReadLock(); try { return _compilerResultsByTextHash.TryGetValue(scriptTextHash, out result); } finally { _lock.ExitReadLock(); } }
internal bool TryGetValue(Assembly assembly, out ScriptCompilerResult result) { if (null == assembly) throw new ArgumentNullException("assembly"); _lock.EnterReadLock(); try { return _compilerResultsByAssembly.TryGetValue(assembly, out result); } finally { _lock.ExitReadLock(); } }
internal bool TryAdd(ScriptCompilerResult result) { if (null == result) throw new ArgumentNullException("result"); _lock.EnterUpgradeableReadLock(); try { if (_compilerResultsByTextHash.ContainsKey(result.ScriptTextHash)) { return false; } else { _lock.EnterWriteLock(); try { _compilerResultsByTextHash.Add(result.ScriptTextHash, result); _compilerResultsByAssembly.Add(result.ScriptAssembly, result); return true; } finally { _lock.ExitWriteLock(); } } } finally { _lock.ExitUpgradeableReadLock(); } }
/// <summary> /// Does the compilation of the script into an assembly. The assembly is stored together with /// the read-only source code and returned as result. As list of compiled source codes is maintained by this class. /// If you provide a text that was already compiled before, the already compiled assembly is returned instead /// of a freshly compiled assembly. /// </summary> /// <returns>True if successfully compiles, otherwise false.</returns> public static IScriptCompilerResult Compile(string[] scriptText, out string[] errors) { string scriptTextHash = ScriptCompilerResult.ComputeScriptTextHash(scriptText); if (_compilerResultsByTextHash.Contains(scriptTextHash)) { errors = null; return((ScriptCompilerResult)_compilerResultsByTextHash[scriptTextHash]); } Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); // For Visual Basic Compiler try this : //Microsoft.VisualBasic.VBCodeProvider System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.IncludeDebugInformation = true; // parameters.OutputAssembly = this.ScriptName; // Add available assemblies including the application itself foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { if (!(asm is System.Reflection.Emit.AssemblyBuilder) && asm.Location != null && asm.Location != String.Empty) { parameters.ReferencedAssemblies.Add(asm.Location); } } CompilerResults results; if (scriptText.Length == 1) { results = codeProvider.CompileAssemblyFromSource(parameters, scriptText[0]); } else { results = codeProvider.CompileAssemblyFromSource(parameters, scriptText); } if (results.Errors.Count > 0) { errors = new string[results.Errors.Count]; int i = 0; foreach (CompilerError err in results.Errors) { errors[i++] = err.ToString(); } return(null); } else { ScriptCompilerResult result = new ScriptCompilerResult(scriptText, scriptTextHash, results.CompiledAssembly); _compilerResultsByTextHash.Add(scriptTextHash, result); _compilerResultsByAssembly.Add(result.ScriptAssembly, result); errors = null; return(result); } }
/// <summary> /// Computes the Script text hash of a single script text. /// </summary> /// <param name="scriptText">The script text.</param> /// <returns>A hash string which unique identifies the script text.</returns> public static string ComputeScriptTextHash(string scriptText) { return(ScriptCompilerResult.ComputeScriptTextHash(new string[] { scriptText })); }
private void Compile() { try { if (!string.IsNullOrEmpty(SourcePath)) { _scriptCompilerResult = ScriptCompiler.TryCompile(Name, SourcePath, Script); if (_scriptCompilerResult != null && _scriptCompilerResult.CompilerResult == CompilerResult.AssemblyExists) _assembly = _scriptCompilerResult.Assembly; return; } } catch (Exception e) { Log.Editor.WriteError("Error trying to compile script {0}.Message {1} \n {2}", Name, e.Message, e.StackTrace); } Log.Editor.WriteWarning("The script resource '{0}' has no SourcePath and can't be compiled.", Name); }
/// <summary> /// Does the compilation of the script into an assembly. The assembly is stored together with /// the read-only source code and returned as result. As list of compiled source codes is maintained by this class. /// If you provide a text that was already compiled before, the already compiled assembly is returned instead /// of a freshly compiled assembly. /// </summary> /// <returns>True if successfully compiles, otherwise false.</returns> public static IScriptCompilerResult Compile(string[] scriptText, out string[] errors) { string scriptTextHash = ScriptCompilerResult.ComputeScriptTextHash(scriptText); if(_compilerResultsByTextHash.Contains(scriptTextHash)) { errors = null; return (ScriptCompilerResult)_compilerResultsByTextHash[scriptTextHash]; } Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); // For Visual Basic Compiler try this : //Microsoft.VisualBasic.VBCodeProvider System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.IncludeDebugInformation = true; // parameters.OutputAssembly = this.ScriptName; // Add available assemblies including the application itself foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { if(!(asm is System.Reflection.Emit.AssemblyBuilder) && asm.Location!=null && asm.Location!=String.Empty) parameters.ReferencedAssemblies.Add(asm.Location); } CompilerResults results; if(scriptText.Length==1) results = codeProvider.CompileAssemblyFromSource(parameters, scriptText[0]); else results = codeProvider.CompileAssemblyFromSource(parameters, scriptText); if (results.Errors.Count > 0) { errors = new string[results.Errors.Count]; int i=0; foreach (CompilerError err in results.Errors) { errors[i++] = err.ToString(); } return null; } else { ScriptCompilerResult result = new ScriptCompilerResult(scriptText, scriptTextHash, results.CompiledAssembly); _compilerResultsByTextHash.Add(scriptTextHash,result); _compilerResultsByAssembly.Add(result.ScriptAssembly,result); errors=null; return result; } }