UltimateZoneBuilder/Source/Core/Compilers/Compiler.cs
MaxED 592887a086 Configurations: increased game configuration loading speed (in previous builds it took ~650 ms. to load a single game configuration, now it takes ~120 ms. to load all 64 of them). As a side effect, New\Open Map Options, Map Options and Game Configurations windows are now opened noticeably faster. The editor starts up a bit faster as well.
Configurations: all 64 game configuration are now available by default.
Game Configurations window: game configurations can now be disabled. This setting is mostly cosmetic. When a game configuration is disabled, it won't be shown in "game configuration" dropdowns in New\Open Map Options and Map Options windows. If a map's .dbs file specifies a disabled configuration, it will be picked as a map configuration anyway.
Linedefs mode: vertex insert preview logic used Highlight range instead of Stitch range (which is used when draw mode engages).
Visual mode: double-sided middle textures were not selected when using "Select" action with "with same texture" modifier.
Textures: some optimizations in patch blending code.
ZDoom ACS script configuration: added definitions for StrLeft, StrMid and StrRight functions.
2014-02-18 14:04:14 +00:00

212 lines
5.9 KiB
C#

#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.Generic;
using System.Globalization;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using CodeImp.DoomBuilder.Config;
#endregion
namespace CodeImp.DoomBuilder.Compilers
{
public abstract class Compiler : IDisposable
{
#region ================== Variables
// Parameters
protected CompilerInfo info;
protected string parameters;
protected string workingdir;
protected string sourcefile;
protected string outputfile;
protected string inputfile;
// Files
protected DirectoryInfo tempdir;
// Errors
private List<CompilerError> errors;
// Disposing
protected bool isdisposed;
#endregion
#region ================== Properties
public string Parameters { get { return parameters; } set { parameters = value; } }
public string WorkingDirectory { get { return workingdir; } set { workingdir = value; } }
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; } }
public CompilerError[] Errors { get { return errors.ToArray(); } }
#endregion
#region ================== Constructor / Disposer
// Constructor
public Compiler(CompilerInfo info)
{
// Initialize
this.info = info;
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)
{
Exception deleteerror = null;
float starttime = General.Clock.CurrentTime;
do
{
try
{
// Remove temporary directory
General.WriteLogLine("Removing temporary compiler files...");
tempdir.Delete(true);
deleteerror = null;
}
catch(Exception e)
{
deleteerror = e;
}
// Bail out when it takes too long
if((General.Clock.CurrentTime - starttime) > 2000) break;
}
while(deleteerror != null);
// Report error if we have one
if(deleteerror != null)
{
General.ErrorLogger.Add(ErrorType.Error, "Unable to remove temporary compiler files. " + deleteerror.GetType().Name + ": " + deleteerror.Message);
General.WriteLogLine(deleteerror.StackTrace);
}
// Disposed
isdisposed = true;
}
}
#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(info.Path, 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. According to the compiler configuration in '" + info.FileName + "', the was expected to be found in the following path: " + info.Path);
File.Copy(sourcefile, targetfile, true);
}
}
/// <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>
protected void ReportError(CompilerError err)
{
this.errors.Add(err);
}
// This creates a compiler by interface name
internal static Compiler Create(CompilerInfo info)
{
// 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;
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
Compiler result = (Compiler)a.CreateInstance(t.FullName, false, BindingFlags.Default,
null, args, CultureInfo.CurrentCulture, new object[0]);
return result;
}
}
}
}
// 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;
}
#endregion
}
}