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.Windows.Forms ;
using CodeImp.DoomBuilder.Config ;
using System.IO ;
using CodeImp.DoomBuilder.Compilers ;
2013-04-11 11:04:16 +00:00
using CodeImp.DoomBuilder.GZBuilder.Data ; //mxd
2009-04-19 18:07:22 +00:00
#endregion
namespace CodeImp.DoomBuilder.Controls
{
internal sealed class ScriptFileDocumentTab : ScriptDocumentTab
{
#region = = = = = = = = = = = = = = = = = = Constants
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
private string filepathname ;
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
public override bool IsSaveAsRequired { get { return ( filepathname . Length = = 0 ) ; } }
public override string Filename { get { return filepathname ; } }
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
public ScriptFileDocumentTab ( ScriptEditorPanel panel , ScriptConfiguration config ) : base ( panel )
{
string ext = "" ;
// Initialize
this . filepathname = "" ;
this . config = config ;
editor . SetupStyles ( config ) ;
if ( config . Extensions . Length > 0 ) ext = "." + config . Extensions [ 0 ] ;
SetTitle ( "Untitled" + ext ) ;
editor . ClearUndoRedo ( ) ;
2013-09-11 09:47:53 +00:00
//mxd
2014-07-11 10:13:26 +00:00
navigator . Enabled = ( config . ScriptType ! = ScriptType . UNKNOWN ) ;
2009-04-19 18:07:22 +00:00
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
// This compiles the script file
public override void Compile ( )
{
string inputfile , outputfile ;
Compiler compiler ;
// 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 ;
}
// Copy the source file into the temporary directory
inputfile = Path . Combine ( compiler . Location , Path . GetFileName ( filepathname ) ) ;
File . Copy ( filepathname , inputfile ) ;
// Make random output filename
outputfile = General . MakeTempFilename ( compiler . Location , "tmp" ) ;
// Run compiler
compiler . Parameters = config . Parameters ;
compiler . InputFile = Path . GetFileName ( inputfile ) ;
compiler . OutputFile = Path . GetFileName ( outputfile ) ;
compiler . SourceFile = filepathname ;
compiler . WorkingDirectory = Path . GetDirectoryName ( inputfile ) ;
if ( compiler . Run ( ) )
{
// Fetch errors
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 ) ;
}
2012-07-23 21:28:23 +00:00
2013-09-11 09:47:53 +00:00
//mxd. Should be called only if script is compiled successfully
2014-12-03 23:15:26 +00:00
if ( compiler . Errors . Length = = 0 & & config . ScriptType = = ScriptType . ACS )
2013-09-11 09:47:53 +00:00
General . Map . UpdateScriptNames ( ) ;
2009-04-19 18:07:22 +00:00
}
// Dispose compiler
compiler . Dispose ( ) ;
// 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 ) ;
}
// This saves the document (used for both explicit and implicit)
// Return true when successfully saved
public override bool Save ( )
{
try
{
// Write the file
File . WriteAllBytes ( filepathname , editor . GetText ( ) ) ;
}
catch ( Exception e )
{
// Failed
2009-05-10 16:02:08 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "Cannot open file '" + filepathname + "' for writing. Make sure the path exists and that the file is not in use by another application." ) ;
2009-04-19 18:07:22 +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
2009-05-10 19:04:47 +00:00
editor . IsChanged = false ;
2009-04-19 18:07:22 +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 ( ) )
{
SetTitle ( Path . GetFileName ( filepathname ) ) ;
return true ;
}
else
{
this . filepathname = oldfilename ;
return false ;
}
}
// This opens a file and returns true when successful
public bool Open ( string filepathname )
{
2013-09-11 09:47:53 +00:00
try
2009-04-19 18:07:22 +00:00
{
// Read the file
2013-09-11 09:47:53 +00:00
editor . SetText ( File . ReadAllBytes ( filepathname ) ) ;
2009-04-19 18:07:22 +00:00
}
catch ( Exception e )
{
// Failed
2009-05-10 16:02:08 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "Cannot open file '" + filepathname + "' for reading. Make sure the path exists and that the file is not in use by another application." ) ;
2009-04-19 18:07:22 +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 ( ) ;
2012-07-16 09:45:21 +00:00
2009-04-19 18:07:22 +00:00
return true ;
}
// 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 ) ;
}
2012-07-12 22:34:12 +00:00
2013-09-11 09:47:53 +00:00
//mxd
base . ChangeScriptConfig ( newconfig ) ;
2009-04-19 18:07:22 +00:00
}
#endregion
#region = = = = = = = = = = = = = = = = = = Events
#endregion
}
}