2009-04-19 18:07:22 +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.Generic ;
using System.Globalization ;
using System.Diagnostics ;
using System.Reflection ;
using System.IO ;
using CodeImp.DoomBuilder.Config ;
2013-04-11 11:04:16 +00:00
2009-04-19 18:07:22 +00:00
#endregion
namespace CodeImp.DoomBuilder.Compilers
{
public abstract class Compiler : IDisposable
{
#region = = = = = = = = = = = = = = = = = = Variables
// Parameters
2014-05-15 13:16:30 +00:00
protected readonly CompilerInfo info ;
2009-04-19 18:07:22 +00:00
protected string parameters ;
protected string workingdir ;
protected string sourcefile ;
protected string outputfile ;
protected string inputfile ;
// Files
2014-05-15 13:16:30 +00:00
protected readonly DirectoryInfo tempdir ;
2009-04-19 18:07:22 +00:00
// Errors
2014-05-15 13:16:30 +00:00
private readonly List < CompilerError > errors ;
2009-04-19 18:07:22 +00:00
// 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
2015-12-17 10:07:28 +00:00
protected Compiler ( CompilerInfo info , bool copyrequiredfiles )
2009-04-19 18:07:22 +00:00
{
// Initialize
this . info = info ;
this . errors = new List < CompilerError > ( ) ;
2016-02-22 08:04:06 +00:00
General . WriteLogLine ( "Creating compiler \"" + info . Name + "\" on interface \"" + this . GetType ( ) . Name + "\"..." ) ;
2009-04-19 18:07:22 +00:00
// Create temporary directory
tempdir = Directory . CreateDirectory ( General . MakeTempDirname ( ) ) ;
workingdir = tempdir . FullName ;
2015-12-17 10:07:28 +00:00
//mxd. ACC compiler itself is not copied to tempdir anymore, so we don't need to move it's include files
//but we still need tempdir to compile SCRIPTS lump.
if ( copyrequiredfiles )
{
// Copy required files to the temp directory
General . WriteLogLine ( "Copying required files for compiler..." ) ;
CopyRequiredFiles ( ) ;
}
2009-04-19 18:07:22 +00:00
}
// Disposer
public virtual void Dispose ( )
{
if ( ! isdisposed )
{
2014-02-21 14:42:12 +00:00
Exception deleteerror ;
2016-03-14 00:01:21 +00:00
long starttime = Clock . CurrentTime ;
2009-04-19 18:07:22 +00:00
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
2014-05-20 09:09:28 +00:00
if ( ( Clock . CurrentTime - starttime ) > 2000 ) break ;
2009-04-19 18:07:22 +00:00
}
while ( deleteerror ! = null ) ;
2009-05-10 19:04:47 +00:00
// 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 ) ;
}
2009-04-19 18:07:22 +00:00
// 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 )
{
2015-11-30 14:18:42 +00:00
string srcfile = Path . Combine ( info . Path , f ) ;
if ( ! File . Exists ( srcfile ) )
2014-12-03 23:15:26 +00:00
{
2016-02-22 08:04:06 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "The file \"" + f + "\" required by the \"" + info . Name + "\" compiler is missing. According to the compiler configuration in \"" + info . FileName + "\", it was expected to be found here: \"" + info . Path + "\"" ) ;
2014-12-03 23:15:26 +00:00
}
else
{
2015-11-30 14:18:42 +00:00
string tgtfile = Path . Combine ( tempdir . FullName , f ) ;
File . Copy ( srcfile , tgtfile , true ) ;
2014-05-15 13:16:30 +00:00
}
2009-04-19 18:07:22 +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>
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
2014-12-03 23:15:26 +00:00
foreach ( Assembly a in asms )
{
2009-04-19 18:07:22 +00:00
// Find the class
2014-05-15 13:16:30 +00:00
Type [ ] types = ( Equals ( a , General . ThisAssembly ) ? a . GetTypes ( ) : a . GetExportedTypes ( ) ) ;
2009-04-19 18:07:22 +00:00
foreach ( Type t in types )
{
if ( t . IsSubclassOf ( typeof ( Compiler ) ) & & ( t . Name = = info . ProgramInterface ) )
{
// Create instance
2014-02-18 14:04:14 +00:00
Compiler result = ( Compiler ) a . CreateInstance ( t . FullName , false , BindingFlags . Default ,
null , args , CultureInfo . CurrentCulture , new object [ 0 ] ) ;
2009-04-19 18:07:22 +00:00
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
}
}