summaryrefslogtreecommitdiff
path: root/CompileScript.cs
diff options
context:
space:
mode:
Diffstat (limited to 'CompileScript.cs')
-rw-r--r--CompileScript.cs105
1 files changed, 105 insertions, 0 deletions
diff --git a/CompileScript.cs b/CompileScript.cs
new file mode 100644
index 0000000..cbdb5b8
--- /dev/null
+++ b/CompileScript.cs
@@ -0,0 +1,105 @@
+// SIDFX
+// Copyright (C) 2004 Ian Cowburn
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// $Id$
+//
+using System;
+using Microsoft.CSharp;
+using System.CodeDom;
+using System.CodeDom.Compiler;
+using System.Reflection;
+
+namespace SIDFX
+{
+ /// <summary>
+ /// Description of CompileScript.
+ /// </summary>
+ public class CompileScript
+ {
+ // -------------------------------------------------------
+ // PUBLIC
+ // -------------------------------------------------------
+
+ public CompileScript(string script)
+ {
+ try
+ {
+ script="using System;using SIDFX;\nusing System.Threading;\nclass Script { public void Main() {\n"+script;
+ script=script+"}}";
+
+ m_provider=new CSharpCodeProvider();
+ m_compiler=m_provider.CreateCompiler();
+ m_param=new CompilerParameters();
+
+ m_param.GenerateExecutable=false;
+ m_param.GenerateInMemory=true;
+ m_param.OutputAssembly=null;
+ m_param.MainClass="Script.Main";
+ m_param.IncludeDebugInformation=false;
+
+ foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
+ m_param.ReferencedAssemblies.Add(a.Location);
+ }
+
+ m_results=m_compiler.CompileAssemblyFromSource(m_param,script);
+ }
+ catch
+ {
+ throw;
+ }
+ }
+
+ public bool HasErrors
+ {
+ get
+ {
+ if (m_results!=null)
+ {
+ return m_results.Errors.Count>0;
+ }
+ else
+ {
+ return true;
+ }
+ }
+ }
+
+ public CompilerErrorCollection Errors
+ {
+ get {return m_results.Errors;}
+ }
+
+ public void Run()
+ {
+ if (!HasErrors)
+ {
+ object o = m_results.CompiledAssembly.CreateInstance("Script");
+ Type type = o.GetType();
+ MethodInfo m = type.GetMethod("Main");
+ m.Invoke(o, null);
+ }
+ }
+
+ // -------------------------------------------------------
+ // PRIVATE
+ // -------------------------------------------------------
+ private CSharpCodeProvider m_provider=null;
+ private ICodeCompiler m_compiler=null;
+ private CompilerParameters m_param=null;
+ private CompilerResults m_results=null;
+ }
+}