2008-11-05 22:20:52 +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.ComponentModel ;
using System.Drawing ;
using System.Text ;
using System.Windows.Forms ;
using Microsoft.Win32 ;
using System.Diagnostics ;
using CodeImp.DoomBuilder.Data ;
using CodeImp.DoomBuilder.Map ;
using CodeImp.DoomBuilder.Config ;
using CodeImp.DoomBuilder.Types ;
using CodeImp.DoomBuilder.IO ;
2008-11-06 15:00:01 +00:00
using System.IO ;
2008-11-14 16:19:25 +00:00
using CodeImp.DoomBuilder.Compilers ;
2008-11-05 22:20:52 +00:00
#endregion
namespace CodeImp.DoomBuilder.Controls
{
internal sealed class ScriptFileDocumentTab : ScriptDocumentTab
{
#region = = = = = = = = = = = = = = = = = = Constants
#endregion
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#region = = = = = = = = = = = = = = = = = = Variables
2008-11-06 15:00:01 +00:00
private string filepathname ;
2008-11-05 22:20:52 +00:00
#endregion
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#region = = = = = = = = = = = = = = = = = = Properties
2008-11-06 15:00:01 +00:00
public override bool IsSaveAsRequired { get { return ( filepathname . Length = = 0 ) ; } }
2008-11-11 06:43:33 +00:00
public override string Filename { get { return filepathname ; } }
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#endregion
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
// Constructor
2008-11-15 13:12:09 +00:00
public ScriptFileDocumentTab ( ScriptEditorPanel panel , ScriptConfiguration config ) : base ( panel )
2008-11-05 22:20:52 +00:00
{
2008-11-06 15:00:01 +00:00
string ext = "" ;
2008-11-05 22:20:52 +00:00
// Initialize
2008-11-06 15:00:01 +00:00
this . filepathname = "" ;
2008-11-09 17:59:13 +00:00
this . config = config ;
2008-11-06 15:00:01 +00:00
editor . SetupStyles ( config ) ;
if ( config . Extensions . Length > 0 ) ext = "." + config . Extensions [ 0 ] ;
SetTitle ( "Untitled" + ext ) ;
editor . ClearUndoRedo ( ) ;
2008-11-05 22:20:52 +00:00
}
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
// Disposer
protected override void Dispose ( bool disposing )
{
base . Dispose ( disposing ) ;
}
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#endregion
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#region = = = = = = = = = = = = = = = = = = Methods
2008-11-06 15:00:01 +00:00
2008-11-14 16:19:25 +00:00
// This compiles the script file
public override void Compile ( )
{
string inputfile , outputfile ;
2008-11-15 13:12:09 +00:00
Compiler compiler ;
2008-11-14 16:19:25 +00:00
// List of errors
List < CompilerError > errors = new List < CompilerError > ( ) ;
try
{
// Initialize compiler
compiler = config . Compiler . Create ( ) ;
}
catch ( Exception e )
{
// Fail
errors . Add ( new CompilerError ( "Unable to initialize compiler. " + e . GetType ( ) . Name + ": " + e . Message ) ) ;
return ;
}
2008-11-15 13:12:09 +00:00
// Copy the source file into the temporary directory
inputfile = Path . Combine ( compiler . Location , Path . GetFileName ( filepathname ) ) ;
File . Copy ( filepathname , inputfile ) ;
2008-11-14 16:19:25 +00:00
// Make random output filename
outputfile = General . MakeTempFilename ( compiler . Location , "tmp" ) ;
2009-01-16 12:25:47 +00:00
2008-11-14 16:19:25 +00:00
// Run compiler
compiler . Parameters = config . Parameters ;
2008-11-15 13:12:09 +00:00
compiler . InputFile = Path . GetFileName ( inputfile ) ;
2008-11-14 16:19:25 +00:00
compiler . OutputFile = Path . GetFileName ( outputfile ) ;
2009-01-16 12:25:47 +00:00
compiler . SourceFile = filepathname ;
2008-11-15 13:12:09 +00:00
compiler . WorkingDirectory = Path . GetDirectoryName ( inputfile ) ;
2008-11-14 16:19:25 +00:00
if ( compiler . Run ( ) )
{
// Fetch errors
2008-11-15 13:12:09 +00:00
foreach ( CompilerError e in compiler . Errors )
{
CompilerError newerr = e ;
// If the error's filename equals our temporary file,
// replace it with the original source filename
if ( string . Compare ( e . filename , inputfile , true ) = = 0 )
newerr . filename = filepathname ;
errors . Add ( newerr ) ;
}
2008-11-14 16:19:25 +00:00
}
// Dispose compiler
compiler . Dispose ( ) ;
2008-11-15 13:12:09 +00:00
// Feed errors to panel
panel . ShowErrors ( errors ) ;
}
// This checks if a script error applies to this script
public override bool VerifyErrorForScript ( CompilerError e )
{
return ( string . Compare ( e . filename , filepathname , true ) = = 0 ) ;
2008-11-14 16:19:25 +00:00
}
2008-11-06 15:00:01 +00:00
// This saves the document (used for both explicit and implicit)
// Return true when successfully saved
public override bool Save ( )
{
try
{
// Write the file
2009-02-14 08:18:17 +00:00
File . WriteAllBytes ( filepathname , editor . GetText ( ) ) ;
2008-11-06 15:00:01 +00:00
}
catch ( Exception e )
{
// Failed
2009-03-01 09:15:58 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "Cannot open file '" + filepathname + "' for writing" ) ;
2008-11-06 15:00:01 +00:00
General . WriteLogLine ( e . GetType ( ) . Name + ": " + e . Message ) ;
General . ShowErrorMessage ( "Unable to open file \"" + filepathname + "\" for writing. Make sure the path exists and that the file is not in use by another application." , MessageBoxButtons . OK ) ;
return false ;
}
// Done
2008-11-09 17:59:13 +00:00
editor . ClearUndoRedo ( ) ;
2008-11-06 15:00:01 +00:00
return true ;
}
// This saves the document to a new file
// Return true when successfully saved
public override bool SaveAs ( string filename )
{
string oldfilename = filepathname ;
filepathname = filename ;
if ( this . Save ( ) )
{
2008-11-09 17:59:13 +00:00
SetTitle ( Path . GetFileName ( filepathname ) ) ;
2008-11-06 15:00:01 +00:00
return true ;
}
else
{
this . filepathname = oldfilename ;
return false ;
}
}
// This opens a file and returns true when successful
public bool Open ( string filepathname )
{
try
{
// Read the file
2009-02-14 08:18:17 +00:00
editor . SetText ( File . ReadAllBytes ( filepathname ) ) ;
2008-11-06 15:00:01 +00:00
}
catch ( Exception e )
{
// Failed
2009-03-01 09:15:58 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "Cannot open file '" + filepathname + "' for reading" ) ;
2008-11-06 15:00:01 +00:00
General . WriteLogLine ( e . GetType ( ) . Name + ": " + e . Message ) ;
General . ShowErrorMessage ( "Unable to open file \"" + filepathname + "\" for reading. Make sure the path exists and that the file is not in use by another application." , MessageBoxButtons . OK ) ;
return false ;
}
// Setup
this . filepathname = filepathname ;
SetTitle ( Path . GetFileName ( filepathname ) ) ;
editor . ClearUndoRedo ( ) ;
return true ;
}
2008-11-09 17:59:13 +00:00
// This changes the script configurations
public override void ChangeScriptConfig ( ScriptConfiguration newconfig )
{
string ext = "" ;
this . config = newconfig ;
editor . SetupStyles ( config ) ;
if ( filepathname . Length = = 0 )
{
if ( config . Extensions . Length > 0 ) ext = "." + config . Extensions [ 0 ] ;
SetTitle ( "Untitled" + ext ) ;
}
}
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#endregion
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#region = = = = = = = = = = = = = = = = = = Events
2008-11-06 15:00:01 +00:00
2008-11-05 22:20:52 +00:00
#endregion
}
}