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.IO ;
2015-11-30 14:18:42 +00:00
using System.Windows.Forms ;
2009-04-19 18:07:22 +00:00
using CodeImp.DoomBuilder.Compilers ;
2015-11-30 14:18:42 +00:00
using CodeImp.DoomBuilder.Config ;
Added, Sector Edit window, UDMF: added UI for sector damage-realted properties.
Added, DECORATE parser: damage types are now parsed.
Added: the editor now reports duplicate textures/flats/patches/sprites/colormaps/voxels in the loaded wads.
Added, all text parsers: added #region/#endregion support.
Added TERRAIN parser.
Added, Script Editor: added special handling for DECORATE special comments.
Added, Sector Edit window, UDMF: Soundsequence value was setup incorrectly when showing the window for multiple sectors with mixed Soundsequence value.
Fixed, Map Options window: "Strictly load patches between P_START and P_END" was not applied when applying the changes.
Fixed, MAPINFO parser: MapInfo should be treated as defined when a map MAPINFO block corresponding to current map is encountered even if it doesn't define any properties recognized by the editor.
Fixed, all text parsers: in some cases error line was calculated incorrectly when reporting an error detected by a text parser.
Cosmetic: changed ' to " in the rest of Error and Warning messages.
Internal: added text resource tracking.
Updated ZDoom_DECORATE.cfg.
Updated documentation ("Game Configuration - Basic Settings" page).
2016-02-22 12:33:19 +00:00
using CodeImp.DoomBuilder.Data ;
2016-04-27 09:13:07 +00:00
using CodeImp.DoomBuilder.ZDoom.Scripting ;
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 ( ) ;
2016-01-27 14:08:15 +00:00
editor . FunctionBar . Enabled = ( config . ScriptType ! = ScriptType . UNKNOWN ) ; //mxd
2009-04-19 18:07:22 +00:00
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
// This compiles the script file
public override void Compile ( )
{
2015-11-30 14:18:42 +00:00
//mxd. ACS requires special handling...
if ( config . ScriptType = = ScriptType . ACS )
2015-11-13 21:42:41 +00:00
{
2015-11-30 14:18:42 +00:00
CompileACS ( ) ;
2015-11-13 21:42:41 +00:00
return ;
}
2009-04-19 18:07:22 +00:00
Compiler compiler ;
2015-11-30 14:18:42 +00:00
List < CompilerError > errors = new List < CompilerError > ( ) ;
2009-04-19 18:07:22 +00:00
try
{
// Initialize compiler
compiler = config . Compiler . Create ( ) ;
}
catch ( Exception e )
{
// Fail
errors . Add ( new CompilerError ( "Unable to initialize compiler. " + e . GetType ( ) . Name + ": " + e . Message ) ) ;
2015-11-30 14:18:42 +00:00
panel . ShowErrors ( errors ) ; //mxd
2009-04-19 18:07:22 +00:00
return ;
}
// Copy the source file into the temporary directory
2015-11-13 21:42:41 +00:00
string inputfile = Path . Combine ( compiler . Location , Path . GetFileName ( filepathname ) ) ;
2009-04-19 18:07:22 +00:00
File . Copy ( filepathname , inputfile ) ;
// Make random output filename
2015-11-13 21:42:41 +00:00
string outputfile = General . MakeTempFilename ( compiler . Location , "tmp" ) ;
2009-04-19 18:07:22 +00:00
// 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 ) ;
}
}
// Dispose compiler
compiler . Dispose ( ) ;
2015-11-13 21:42:41 +00:00
//mxd. Update script navigator
Added, Sector Edit window, UDMF: added UI for sector damage-realted properties.
Added, DECORATE parser: damage types are now parsed.
Added: the editor now reports duplicate textures/flats/patches/sprites/colormaps/voxels in the loaded wads.
Added, all text parsers: added #region/#endregion support.
Added TERRAIN parser.
Added, Script Editor: added special handling for DECORATE special comments.
Added, Sector Edit window, UDMF: Soundsequence value was setup incorrectly when showing the window for multiple sectors with mixed Soundsequence value.
Fixed, Map Options window: "Strictly load patches between P_START and P_END" was not applied when applying the changes.
Fixed, MAPINFO parser: MapInfo should be treated as defined when a map MAPINFO block corresponding to current map is encountered even if it doesn't define any properties recognized by the editor.
Fixed, all text parsers: in some cases error line was calculated incorrectly when reporting an error detected by a text parser.
Cosmetic: changed ' to " in the rest of Error and Warning messages.
Internal: added text resource tracking.
Updated ZDoom_DECORATE.cfg.
Updated documentation ("Game Configuration - Basic Settings" page).
2016-02-22 12:33:19 +00:00
errors . AddRange ( UpdateNavigator ( ) ) ;
2009-04-19 18:07:22 +00:00
// Feed errors to panel
panel . ShowErrors ( errors ) ;
}
2015-11-30 14:18:42 +00:00
//mxd. ACS requires special handling...
private void CompileACS ( )
{
Compiler compiler ;
List < CompilerError > errors = new List < CompilerError > ( ) ;
string inputfile = Path . GetFileName ( filepathname ) ;
// Which compiler to use?
ScriptConfiguration scriptconfig ;
if ( ! string . IsNullOrEmpty ( General . Map . Options . ScriptCompiler ) )
{
// Boilderplate
if ( ! General . CompiledScriptConfigs . ContainsKey ( General . Map . Options . ScriptCompiler ) )
{
2016-02-08 21:51:03 +00:00
General . ShowErrorMessage ( "Unable to compile \"" + inputfile + "\". Unable to find required script compiler configuration (\"" + General . Map . Options . ScriptCompiler + "\")." , MessageBoxButtons . OK ) ;
2015-11-30 14:18:42 +00:00
return ;
}
scriptconfig = General . CompiledScriptConfigs [ General . Map . Options . ScriptCompiler ] ;
}
else
{
scriptconfig = config ;
}
// Initialize compiler
try
{
compiler = scriptconfig . Compiler . Create ( ) ;
}
catch ( Exception e )
{
// Fail
errors . Add ( new CompilerError ( "Unable to initialize compiler. " + e . GetType ( ) . Name + ": " + e . Message ) ) ;
panel . ShowErrors ( errors ) ;
return ;
}
// Preprocess the file
Added, Sector Edit window, UDMF: added UI for sector damage-realted properties.
Added, DECORATE parser: damage types are now parsed.
Added: the editor now reports duplicate textures/flats/patches/sprites/colormaps/voxels in the loaded wads.
Added, all text parsers: added #region/#endregion support.
Added TERRAIN parser.
Added, Script Editor: added special handling for DECORATE special comments.
Added, Sector Edit window, UDMF: Soundsequence value was setup incorrectly when showing the window for multiple sectors with mixed Soundsequence value.
Fixed, Map Options window: "Strictly load patches between P_START and P_END" was not applied when applying the changes.
Fixed, MAPINFO parser: MapInfo should be treated as defined when a map MAPINFO block corresponding to current map is encountered even if it doesn't define any properties recognized by the editor.
Fixed, all text parsers: in some cases error line was calculated incorrectly when reporting an error detected by a text parser.
Cosmetic: changed ' to " in the rest of Error and Warning messages.
Internal: added text resource tracking.
Updated ZDoom_DECORATE.cfg.
Updated documentation ("Game Configuration - Basic Settings" page).
2016-02-22 12:33:19 +00:00
AcsParserSE parser = new AcsParserSE
{
OnInclude = delegate ( AcsParserSE se , string includefile , AcsParserSE . IncludeType includetype )
{
2016-02-28 22:15:17 +00:00
TextResourceData data = General . Map . Data . GetTextResourceData ( includefile ) ;
Added, Sector Edit window, UDMF: added UI for sector damage-realted properties.
Added, DECORATE parser: damage types are now parsed.
Added: the editor now reports duplicate textures/flats/patches/sprites/colormaps/voxels in the loaded wads.
Added, all text parsers: added #region/#endregion support.
Added TERRAIN parser.
Added, Script Editor: added special handling for DECORATE special comments.
Added, Sector Edit window, UDMF: Soundsequence value was setup incorrectly when showing the window for multiple sectors with mixed Soundsequence value.
Fixed, Map Options window: "Strictly load patches between P_START and P_END" was not applied when applying the changes.
Fixed, MAPINFO parser: MapInfo should be treated as defined when a map MAPINFO block corresponding to current map is encountered even if it doesn't define any properties recognized by the editor.
Fixed, all text parsers: in some cases error line was calculated incorrectly when reporting an error detected by a text parser.
Cosmetic: changed ' to " in the rest of Error and Warning messages.
Internal: added text resource tracking.
Updated ZDoom_DECORATE.cfg.
Updated documentation ("Game Configuration - Basic Settings" page).
2016-02-22 12:33:19 +00:00
if ( data = = null )
{
// Fial
errors . Add ( new CompilerError ( "Unable to find include file \"" + includefile + "\"" ) ) ;
panel . ShowErrors ( errors ) ;
}
else
{
se . Parse ( data , true , includetype , false ) ;
}
}
} ;
2015-11-30 14:18:42 +00:00
using ( FileStream stream = File . OpenRead ( filepathname ) )
{
Added, Sector Edit window, UDMF: added UI for sector damage-realted properties.
Added, DECORATE parser: damage types are now parsed.
Added: the editor now reports duplicate textures/flats/patches/sprites/colormaps/voxels in the loaded wads.
Added, all text parsers: added #region/#endregion support.
Added TERRAIN parser.
Added, Script Editor: added special handling for DECORATE special comments.
Added, Sector Edit window, UDMF: Soundsequence value was setup incorrectly when showing the window for multiple sectors with mixed Soundsequence value.
Fixed, Map Options window: "Strictly load patches between P_START and P_END" was not applied when applying the changes.
Fixed, MAPINFO parser: MapInfo should be treated as defined when a map MAPINFO block corresponding to current map is encountered even if it doesn't define any properties recognized by the editor.
Fixed, all text parsers: in some cases error line was calculated incorrectly when reporting an error detected by a text parser.
Cosmetic: changed ' to " in the rest of Error and Warning messages.
Internal: added text resource tracking.
Updated ZDoom_DECORATE.cfg.
Updated documentation ("Game Configuration - Basic Settings" page).
2016-02-22 12:33:19 +00:00
TextResourceData data = new TextResourceData ( stream , new DataLocation ( ) , filepathname , false ) ;
if ( ! parser . Parse ( data , scriptconfig . Compiler . Files , true , AcsParserSE . IncludeType . NONE , false ) )
2015-11-30 14:18:42 +00:00
{
// Check for errors
if ( parser . HasError )
{
errors . Add ( new CompilerError ( parser . ErrorDescription , parser . ErrorSource , parser . ErrorLine ) ) ;
panel . ShowErrors ( errors ) ;
}
2015-12-17 10:07:28 +00:00
compiler . Dispose ( ) ;
return ;
2015-11-30 14:18:42 +00:00
}
}
2015-12-17 10:07:28 +00:00
//mxd. Only works for libraries
2015-11-30 14:18:42 +00:00
if ( ! parser . IsLibrary )
{
2015-12-17 10:07:28 +00:00
errors . Add ( new CompilerError ( "External ACS files can only be compiled as libraries!" , filepathname ) ) ;
2015-11-30 14:18:42 +00:00
panel . ShowErrors ( errors ) ;
compiler . Dispose ( ) ;
return ;
}
// Make random output filename
string outputfile = General . MakeTempFilename ( compiler . Location , "tmp" ) ;
// Run compiler
compiler . Parameters = config . Parameters ;
compiler . InputFile = inputfile ;
compiler . OutputFile = outputfile ;
compiler . SourceFile = filepathname ;
compiler . WorkingDirectory = Path . GetDirectoryName ( filepathname ) ;
2016-03-25 14:06:00 +00:00
compiler . Includes = parser . GetIncludes ( ) ;
2015-11-30 14:18:42 +00:00
compiler . CopyIncludesToWorkingDirectory = false ;
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 ) ;
}
// No errors and output file exists?
if ( compiler . Errors . Length = = 0 )
{
// Output file exists?
if ( ! File . Exists ( outputfile ) )
{
// Fail
compiler . Dispose ( ) ;
2016-02-08 21:51:03 +00:00
errors . Add ( new CompilerError ( "Output file \"" + outputfile + "\" doesn't exist." ) ) ;
2015-11-30 14:18:42 +00:00
panel . ShowErrors ( errors ) ;
return ;
}
// Rename and copy to source file directory
string targetfilename = Path . Combine ( Path . GetDirectoryName ( filepathname ) , parser . LibraryName + ".o" ) ;
try
{
File . Copy ( outputfile , targetfilename , true ) ;
}
catch ( Exception e )
{
// Fail
compiler . Dispose ( ) ;
2016-02-08 21:51:03 +00:00
errors . Add ( new CompilerError ( "Unable to create library file \"" + targetfilename + "\". " + e . GetType ( ) . Name + ": " + e . Message ) ) ;
2015-11-30 14:18:42 +00:00
panel . ShowErrors ( errors ) ;
return ;
}
}
}
// Dispose compiler
compiler . Dispose ( ) ;
// Update script navigator
Added, Sector Edit window, UDMF: added UI for sector damage-realted properties.
Added, DECORATE parser: damage types are now parsed.
Added: the editor now reports duplicate textures/flats/patches/sprites/colormaps/voxels in the loaded wads.
Added, all text parsers: added #region/#endregion support.
Added TERRAIN parser.
Added, Script Editor: added special handling for DECORATE special comments.
Added, Sector Edit window, UDMF: Soundsequence value was setup incorrectly when showing the window for multiple sectors with mixed Soundsequence value.
Fixed, Map Options window: "Strictly load patches between P_START and P_END" was not applied when applying the changes.
Fixed, MAPINFO parser: MapInfo should be treated as defined when a map MAPINFO block corresponding to current map is encountered even if it doesn't define any properties recognized by the editor.
Fixed, all text parsers: in some cases error line was calculated incorrectly when reporting an error detected by a text parser.
Cosmetic: changed ' to " in the rest of Error and Warning messages.
Internal: added text resource tracking.
Updated ZDoom_DECORATE.cfg.
Updated documentation ("Game Configuration - Basic Settings" page).
2016-02-22 12:33:19 +00:00
errors . AddRange ( UpdateNavigator ( ) ) ;
2015-11-30 14:18:42 +00:00
// Feed errors to panel
panel . ShowErrors ( errors ) ;
}
2009-04-19 18:07:22 +00:00
// 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
2016-02-08 21:51:03 +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
2016-01-27 14:08:15 +00:00
editor . SetSavePoint ( ) ; //mxd
2015-11-30 14:18:42 +00:00
UpdateTitle ( ) ; //mxd
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
2016-01-27 14:08:15 +00:00
editor . Text = File . ReadAllText ( filepathname ) ; //mxd
2009-04-19 18:07:22 +00:00
}
catch ( Exception e )
{
// Failed
2016-02-08 21:51:03 +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 ;
editor . ClearUndoRedo ( ) ;
2015-11-30 14:18:42 +00:00
SetTitle ( Path . GetFileName ( filepathname ) ) ;
Added, Sector Edit window, UDMF: added UI for sector damage-realted properties.
Added, DECORATE parser: damage types are now parsed.
Added: the editor now reports duplicate textures/flats/patches/sprites/colormaps/voxels in the loaded wads.
Added, all text parsers: added #region/#endregion support.
Added TERRAIN parser.
Added, Script Editor: added special handling for DECORATE special comments.
Added, Sector Edit window, UDMF: Soundsequence value was setup incorrectly when showing the window for multiple sectors with mixed Soundsequence value.
Fixed, Map Options window: "Strictly load patches between P_START and P_END" was not applied when applying the changes.
Fixed, MAPINFO parser: MapInfo should be treated as defined when a map MAPINFO block corresponding to current map is encountered even if it doesn't define any properties recognized by the editor.
Fixed, all text parsers: in some cases error line was calculated incorrectly when reporting an error detected by a text parser.
Cosmetic: changed ' to " in the rest of Error and Warning messages.
Internal: added text resource tracking.
Updated ZDoom_DECORATE.cfg.
Updated documentation ("Game Configuration - Basic Settings" page).
2016-02-22 12:33:19 +00:00
panel . ShowErrors ( UpdateNavigator ( ) ) ; //mxd
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 )
{
this . config = newconfig ;
editor . SetupStyles ( config ) ;
if ( filepathname . Length = = 0 )
{
2015-11-30 14:18:42 +00:00
string ext = ( config . Extensions . Length > 0 ? "." + config . Extensions [ 0 ] : "" ) ;
2009-04-19 18:07:22 +00:00
SetTitle ( "Untitled" + ext ) ;
}
2015-12-17 10:07:28 +00:00
else
{
UpdateTitle ( ) ; //mxd
}
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
}
}