UltimateZoneBuilder/Source/Compilers/Compiler.cs

193 lines
5.2 KiB
C#
Raw Normal View History

2008-11-11 16:19:50 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* 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.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
2008-11-13 16:21:11 +00:00
using System.Reflection;
using System.IO;
using CodeImp.DoomBuilder.Config;
using CodeImp.DoomBuilder.IO;
2008-11-11 16:19:50 +00:00
#endregion
2008-11-13 16:21:11 +00:00
namespace CodeImp.DoomBuilder.Compilers
2008-11-11 16:19:50 +00:00
{
public abstract class Compiler : IDisposable
2008-11-11 16:19:50 +00:00
{
#region ================== Variables
// Parameters
protected CompilerInfo info;
protected string parameters;
protected string workingdir;
2009-01-16 12:25:47 +00:00
protected string sourcefile;
protected string outputfile;
protected string inputfile;
// Files
protected DirectoryInfo tempdir;
2008-11-11 16:19:50 +00:00
// Errors
private List<CompilerError> errors;
// Disposing
protected bool isdisposed;
2008-11-11 16:19:50 +00:00
#endregion
#region ================== Properties
public string Parameters { get { return parameters; } set { parameters = value; } }
public string WorkingDirectory { get { return workingdir; } set { workingdir = value; } }
2009-01-16 12:25:47 +00:00
public string SourceFile { get { return sourcefile; } set { sourcefile = value; } }
public string InputFile { get { return inputfile; } set { inputfile = value; } }
public string OutputFile { get { return outputfile; } set { outputfile = value; } }
public string Location { get { return tempdir.FullName; } }
public bool IsDisposed { get { return isdisposed; } }
2008-11-11 16:19:50 +00:00
public CompilerError[] Errors { get { return errors.ToArray(); } }
#endregion
#region ================== Constructor / Disposer
2008-11-11 16:19:50 +00:00
// Constructor
public Compiler(CompilerInfo info)
2008-11-11 16:19:50 +00:00
{
// Initialize
this.info = info;
2008-11-11 16:19:50 +00:00
this.errors = new List<CompilerError>();
General.WriteLogLine("Creating compiler '" + info.Name + "' on interface '" + this.GetType().Name + "'...");
// Create temporary directory
tempdir = Directory.CreateDirectory(General.MakeTempDirname());
workingdir = tempdir.FullName;
// Copy required files to the temp directory
General.WriteLogLine("Copying required files for compiler...");
CopyRequiredFiles();
}
// Disposer
public virtual void Dispose()
{
if(!isdisposed)
{
// Remove temporary directory
General.WriteLogLine("Removing temporary compiler files...");
tempdir.Delete(true);
// Disposed
isdisposed = true;
}
2008-11-11 16:19:50 +00:00
}
#endregion
#region ================== Methods
// This copies all compiler files to a given destination
private void CopyRequiredFiles()
{
// Copy files
foreach(string f in info.Files)
{
string sourcefile = Path.Combine(General.CompilersPath, f);
string targetfile = Path.Combine(tempdir.FullName, f);
if(!File.Exists(sourcefile)) General.ErrorLogger.Add(ErrorType.Error, "The file '" + f + "' required by the '" + info.Name + "' compiler is missing!");
File.Copy(sourcefile, targetfile, true);
}
}
2008-11-11 16:19:50 +00:00
/// <summary>
/// This runs the compiler.
/// </summary>
/// <returns>Returns false when failed to start.</returns>
public virtual bool Run() { return false; }
/// <summary>
/// Use this to report an error.
/// </summary>
2008-11-11 16:19:50 +00:00
protected void ReportError(CompilerError err)
{
this.errors.Add(err);
}
2008-11-13 16:21:11 +00:00
// This creates a compiler by interface name
internal static Compiler Create(CompilerInfo info)
2008-11-13 16:21:11 +00:00
{
Compiler result;
2008-11-13 16:21:11 +00:00
// Make list of assemblies to search in
List<Assembly> asms = General.Plugins.GetPluginAssemblies();
asms.Add(General.ThisAssembly);
// Constructor arguments
object[] args = new object[1];
args[0] = info;
2008-11-13 16:21:11 +00:00
try
{
// Go for all assemblies
foreach(Assembly a in asms)
{
Type[] types;
// Find the class
if(a == General.ThisAssembly)
types = a.GetTypes();
else
types = a.GetExportedTypes();
foreach(Type t in types)
{
if(t.IsSubclassOf(typeof(Compiler)) && (t.Name == info.ProgramInterface))
{
// Create instance
result = (Compiler)a.CreateInstance(t.FullName, false, BindingFlags.Default,
null, args, CultureInfo.CurrentCulture, new object[0]);
return result;
}
}
}
2008-11-13 16:21:11 +00:00
}
// Catch errors
catch(TargetInvocationException e)
{
// Throw the actual exception
Debug.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
Debug.WriteLine(e.InnerException.Source + " throws " + e.InnerException.GetType().Name + ":");
Debug.WriteLine(e.InnerException.Message);
Debug.WriteLine(e.InnerException.StackTrace);
throw e.InnerException;
}
// No such compiler
return null;
2008-11-13 16:21:11 +00:00
}
2008-11-11 16:19:50 +00:00
#endregion
}
}