/// <summary> /// Executes the given script. /// </summary> /// <param name="p_bteScript">The bytes of the assembly containing the script to execute.</param> /// <returns><c>true</c> if the script completes successfully; /// <c>false</c> otherwise.</returns> public bool Execute(byte[] p_bteScript) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Control.CheckForIllegalCrossThreadCalls = false; Assembly asmScript = Assembly.Load(p_bteScript); object s = asmScript.CreateInstance("Script"); //s = new Script(); if (s == null) { m_csfFunctions.ExtendedMessageBox("C# Script did not contain a 'Script' class in the root namespace.", "Error", null); return(false); } try { MethodInfo mifMethod = null; for (Type tpeScriptType = s.GetType(); mifMethod == null; tpeScriptType = tpeScriptType.BaseType) { mifMethod = tpeScriptType.GetMethod("Setup", new Type[] { typeof(CSharpScriptFunctionProxy) }); } mifMethod.Invoke(s, new object[] { m_csfFunctions }); return((bool)s.GetType().GetMethod("OnActivate").Invoke(s, null)); } catch (Exception ex) { StringBuilder stbException = new StringBuilder(ex.ToString()); while (ex.InnerException != null) { ex = ex.InnerException; stbException.AppendLine().AppendLine().Append(ex.ToString()); } string strMessage = "An exception occured in the script."; m_csfFunctions.ExtendedMessageBox(strMessage, "Error", stbException.ToString()); return(false); } }
/// <summary> /// Compiles the given C# script code. /// </summary> /// <remarks> /// The compiled script is not loaded into the current domain. /// </remarks> /// <param name="p_strCode">The code to compile.</param> /// <returns>The bytes of the assembly containing the script to execute.</returns> protected byte[] Compile(string p_strCode) { CSharpScriptCompiler sccCompiler = new CSharpScriptCompiler(); CompilerErrorCollection cecErrors = null; string strBaseScriptClassName = m_regScriptClass.Match(p_strCode).Groups[2].ToString(); string strCode = m_regScriptClass.Replace(p_strCode, "using " + BaseScriptType.Namespace + ";\r\n$1" + BaseScriptType.Name); Regex regOtherScriptClasses = new Regex(string.Format(@"(class\s+\S+\s*:.*?)(?<!\w){0}", strBaseScriptClassName)); strCode = regOtherScriptClasses.Replace(strCode, "$1" + BaseScriptType.Name); strCode = m_regFommUsing.Replace(strCode, ""); byte[] bteAssembly = sccCompiler.Compile(strCode, BaseScriptType, out cecErrors); if (cecErrors != null) { StringBuilder stbErrors = new StringBuilder(); if (cecErrors.HasErrors) { stbErrors.Append("<h3 style='color:red'>Errors</h3><ul>"); foreach (CompilerError cerError in cecErrors) { if (!cerError.IsWarning) { stbErrors.AppendFormat("<li><b>{0},{1}:</b> {2} <i>(Error {3})</i></li>", cerError.Line, cerError.Column, cerError.ErrorText, cerError.ErrorNumber); } } stbErrors.Append("</ul>"); } if (cecErrors.HasWarnings) { stbErrors.Append("<h3 style='color:#ffd700;'>Warnings</h3><ul>"); foreach (CompilerError cerError in cecErrors) { if (cerError.IsWarning) { stbErrors.AppendFormat("<li><b>{0},{1}:</b> {2} <i>(Error {3})</i></li>", cerError.Line, cerError.Column, cerError.ErrorText, cerError.ErrorNumber); } } stbErrors.Append("</ul>"); } if (cecErrors.HasErrors) { string strMessage = "Could not compile script; errors were found."; m_csfFunctions.ExtendedMessageBox(strMessage, "Error", stbErrors.ToString()); return(null); } } return(bteAssembly); }
/// <summary> /// Executes the given script. /// </summary> /// <param name="p_bteScript">The bytes of the assembly containing the script to execute.</param> /// <returns><c>true</c> if the script completes successfully; /// <c>false</c> otherwise.</returns> public bool Execute(byte[] p_bteScript) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Control.CheckForIllegalCrossThreadCalls = false; Assembly asmScript = Assembly.Load(p_bteScript); object s = asmScript.CreateInstance("Script"); //s = new Script(); if (s == null) { m_csfFunctions.ExtendedMessageBox("C# Script did not contain a 'Script' class in the root namespace.", "Error", null); return(false); } try { MethodInfo mifMethod = null; try { for (Type tpeScriptType = s.GetType(); mifMethod == null; tpeScriptType = tpeScriptType.BaseType) { mifMethod = tpeScriptType.GetMethod("Setup", new Type[] { typeof(CSharpScriptFunctionProxy) }); } mifMethod.Invoke(s, new object[] { m_csfFunctions }); return((bool)s.GetType().GetMethod("OnActivate").Invoke(s, null)); } catch (System.Reflection.TargetInvocationException ex) { throw ex.InnerException; } } catch (System.Security.SecurityException ex) { string strMessage = "Mod installer incompatible."; StringBuilder stbException = new StringBuilder(); stbException .Append("The mod installer attempted to access a location outside the mod directory.") .AppendLine() .Append("While this is likely to be harmless, due to technical and security reasons we cannot ensure " + "compatibility with this type of installer.") .AppendLine() .Append("We ask you to instead install the mod manually and apologize for the inconvenience.") .AppendLine() .AppendLine() .Append(ex.ToString()); m_csfFunctions.ExtendedMessageBox(strMessage, "Error", stbException.ToString()); return(false); } catch (Exception ex) { StringBuilder stbException = new StringBuilder(ex.ToString()); while (ex.InnerException != null) { ex = ex.InnerException; stbException.AppendLine().AppendLine().Append(ex.ToString()); } string strMessage = "An exception occurred in the script."; m_csfFunctions.ExtendedMessageBox(strMessage, "Error", stbException.ToString()); return(false); } }