2013-07-25 12:39:40 +00:00
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.IO ;
using System.Windows.Forms ;
2014-02-18 14:04:14 +00:00
using CodeImp.DoomBuilder.Config ;
2013-07-25 12:39:40 +00:00
using CodeImp.DoomBuilder.IO ;
using CodeImp.DoomBuilder.Map ;
namespace CodeImp.DoomBuilder.Windows
{
public partial class ChangeMapForm : DelayedForm
{
2014-11-25 11:52:01 +00:00
private MapOptions options ;
private Configuration mapsettings ;
2013-07-25 12:39:40 +00:00
private readonly string filepathname ;
public MapOptions Options { get { return options ; } }
2014-12-03 09:06:05 +00:00
public ChangeMapForm ( string filepathname , MapOptions options )
{
2013-07-25 12:39:40 +00:00
InitializeComponent ( ) ;
2020-09-11 23:25:59 +00:00
CodeImp . DoomBuilder . General . ApplyMonoListViewFix ( mapslist ) ;
2013-07-25 12:39:40 +00:00
this . options = options ;
this . filepathname = filepathname ;
}
2014-12-03 09:06:05 +00:00
private void LoadSettings ( )
{
2013-07-25 12:39:40 +00:00
// Check if the file exists
if ( ! File . Exists ( filepathname ) )
{
// WAD file does not exist
2015-12-28 15:01:53 +00:00
MessageBox . Show ( this , "Could not open the WAD file. The file does not exist." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2013-07-25 12:39:40 +00:00
this . DialogResult = DialogResult . Cancel ;
this . Close ( ) ;
return ;
}
2015-12-28 15:01:53 +00:00
// Busy
Cursor . Current = Cursors . WaitCursor ;
WAD wadfile ;
2013-07-25 12:39:40 +00:00
try
{
// Open the WAD file
wadfile = new WAD ( filepathname , true ) ;
}
catch ( Exception )
{
// Unable to open WAD file (or its config)
MessageBox . Show ( this , "Could not open the WAD file for reading. Please make sure the file you selected is valid and is not in use by any other application." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
this . DialogResult = DialogResult . Cancel ;
this . Close ( ) ;
return ;
}
// Make an array for the map names
List < ListViewItem > mapnames = new List < ListViewItem > ( ) ;
2014-11-25 11:52:01 +00:00
// Open the Map Settings configuration
string dbsfile = filepathname . Substring ( 0 , filepathname . Length - 4 ) + ".dbs" ;
2015-12-28 15:01:53 +00:00
if ( File . Exists ( dbsfile ) )
2014-11-25 11:52:01 +00:00
{
try
{
mapsettings = new Configuration ( dbsfile , true ) ;
}
2016-02-01 22:04:00 +00:00
catch ( Exception )
2014-11-25 11:52:01 +00:00
{
mapsettings = new Configuration ( true ) ;
}
}
else
{
mapsettings = new Configuration ( true ) ;
}
2014-02-18 14:04:14 +00:00
//mxd. Get Proper configuration
ConfigurationInfo ci = General . GetConfigurationInfo ( options . ConfigFile ) ;
2014-11-25 11:52:01 +00:00
2013-07-25 12:39:40 +00:00
// Get the map lump names
2014-11-25 11:52:01 +00:00
IDictionary maplumpnames = ci . Configuration . ReadSetting ( "maplumpnames" , new Hashtable ( ) ) ;
2013-07-25 12:39:40 +00:00
// Count how many required lumps we have to find
2015-12-28 15:01:53 +00:00
int lumpsrequired = 0 ;
2014-12-03 09:06:05 +00:00
foreach ( DictionaryEntry ml in maplumpnames )
{
2013-07-25 12:39:40 +00:00
// Ignore the map header (it will not be found because the name is different)
2014-12-03 09:06:05 +00:00
if ( ml . Key . ToString ( ) ! = MapManager . CONFIG_MAP_HEADER )
{
2013-07-25 12:39:40 +00:00
// Read lump setting and count it
2014-11-25 11:52:01 +00:00
if ( ci . Configuration . ReadSetting ( "maplumpnames." + ml . Key + ".required" , false ) )
2013-07-25 12:39:40 +00:00
lumpsrequired + + ;
}
}
// Go for all the lumps in the wad
2015-12-28 15:01:53 +00:00
for ( int scanindex = 0 ; scanindex < ( wadfile . Lumps . Count - 1 ) ; scanindex + + )
2014-12-03 09:06:05 +00:00
{
2013-07-25 12:39:40 +00:00
// Make sure this lump is not part of the map
2014-12-03 09:06:05 +00:00
if ( ! maplumpnames . Contains ( wadfile . Lumps [ scanindex ] . Name ) )
{
2013-07-25 12:39:40 +00:00
// Reset check
2015-12-28 15:01:53 +00:00
int lumpsfound = 0 ;
int checkoffset = 1 ;
2013-07-25 12:39:40 +00:00
// Continue while still within bounds and lumps are still recognized
while ( ( ( scanindex + checkoffset ) < wadfile . Lumps . Count ) & &
2014-12-03 09:06:05 +00:00
maplumpnames . Contains ( wadfile . Lumps [ scanindex + checkoffset ] . Name ) )
{
2013-07-25 12:39:40 +00:00
// Count the lump when it is marked as required
2015-12-28 15:01:53 +00:00
string lumpname = wadfile . Lumps [ scanindex + checkoffset ] . Name ;
2014-11-25 11:52:01 +00:00
if ( ci . Configuration . ReadSetting ( "maplumpnames." + lumpname + ".required" , false ) )
2013-07-25 12:39:40 +00:00
lumpsfound + + ;
// Check the next lump
checkoffset + + ;
}
// Map found? Then add it to the list
if ( lumpsfound > = lumpsrequired )
mapnames . Add ( new ListViewItem ( wadfile . Lumps [ scanindex ] . Name ) ) ;
}
}
wadfile . Dispose ( ) ;
// Clear the list and add the new map names
mapslist . BeginUpdate ( ) ;
mapslist . Items . Clear ( ) ;
mapslist . Items . AddRange ( mapnames . ToArray ( ) ) ;
mapslist . Sort ( ) ;
//select current map
2014-12-03 09:06:05 +00:00
foreach ( ListViewItem item in mapslist . Items )
{
2013-07-25 12:39:40 +00:00
// Was this item previously selected?
2014-12-03 09:06:05 +00:00
if ( item . Text = = options . LevelName )
{
2013-07-25 12:39:40 +00:00
// Select it again
item . Selected = true ;
2015-07-30 23:48:16 +00:00
item . EnsureVisible ( ) ;
2013-07-25 12:39:40 +00:00
break ;
}
}
mapslist . EndUpdate ( ) ;
2015-07-30 23:48:16 +00:00
// Do some focus managing
if ( mapslist . SelectedItems . Count > 0 )
{
mapslist . FocusedItem = mapslist . SelectedItems [ 0 ] ;
}
2013-07-25 12:39:40 +00:00
// Done
Cursor . Current = Cursors . Default ;
}
2014-12-03 09:06:05 +00:00
private void ChangeMapForm_Shown ( object sender , EventArgs e )
{
2013-07-25 12:39:40 +00:00
LoadSettings ( ) ;
}
2014-12-03 09:06:05 +00:00
private void mapslist_DoubleClick ( object sender , EventArgs e )
{
2013-07-25 12:39:40 +00:00
// Click OK
if ( mapslist . SelectedItems . Count > 0 ) apply . PerformClick ( ) ;
}
2014-12-03 09:06:05 +00:00
private void apply_Click ( object sender , EventArgs e )
{
2014-12-29 21:44:25 +00:00
// No map selected?
2015-12-28 15:01:53 +00:00
if ( mapslist . SelectedItems . Count = = 0 )
2014-11-03 13:58:19 +00:00
{
2014-12-29 21:44:25 +00:00
MessageBox . Show ( this , "Please select a map to load for editing." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
mapslist . Focus ( ) ;
2014-11-03 13:58:19 +00:00
return ;
}
2014-12-29 21:44:25 +00:00
// Current map is already loaded
2015-12-28 15:01:53 +00:00
if ( mapslist . SelectedItems [ 0 ] . Text = = options . LevelName )
2014-11-03 14:03:47 +00:00
{
2016-03-08 20:41:06 +00:00
MessageBox . Show ( this , "Map \"" + options . LevelName + "\" is already loaded." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
2014-12-29 21:44:25 +00:00
mapslist . Focus ( ) ;
return ;
}
// Just NO...
2014-12-30 13:21:01 +00:00
if ( ! General . Map . ConfigSettings . ValidateMapName ( mapslist . SelectedItems [ 0 ] . Text . ToUpperInvariant ( ) ) )
2014-12-29 21:44:25 +00:00
{
2014-12-30 13:21:01 +00:00
MessageBox . Show ( this , "Selected map name conflicts with a lump name defined for current map format.\nPlease rename the map and try again." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
2014-12-29 21:44:25 +00:00
mapslist . Focus ( ) ;
2014-11-03 14:03:47 +00:00
return ;
}
2014-11-25 11:52:01 +00:00
2014-12-03 09:06:05 +00:00
// Create new map options, pass settings which should stay unchanged
//TODO: are there other settings which should stay unchanged?..
2015-02-19 13:00:19 +00:00
MapOptions newoptions = new MapOptions ( mapsettings , mapslist . SelectedItems [ 0 ] . Text , options . UseLongTextureNames ) ;
2014-12-03 09:06:05 +00:00
newoptions . ConfigFile = options . ConfigFile ;
2015-09-16 12:10:43 +00:00
newoptions . ScriptCompiler = options . ScriptCompiler ;
2016-01-18 11:39:59 +00:00
newoptions . CopyResources ( options . Resources ) ;
2014-12-03 09:06:05 +00:00
options = newoptions ;
2013-07-25 12:39:40 +00:00
// Hide window
this . DialogResult = DialogResult . OK ;
this . Close ( ) ;
}
2014-12-03 09:06:05 +00:00
private void cancel_Click ( object sender , EventArgs e )
{
// Just hide the window
2013-07-25 12:39:40 +00:00
this . DialogResult = DialogResult . Cancel ;
this . Close ( ) ;
}
}
}