2008-05-23 06:00:37 +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 ;
using System.Collections.Generic ;
using System.Globalization ;
using System.Text ;
using System.IO ;
using CodeImp.DoomBuilder.Data ;
using System.Diagnostics ;
2008-05-29 11:34:01 +00:00
using CodeImp.DoomBuilder.Actions ;
2008-05-23 06:00:37 +00:00
using System.Windows.Forms ;
#endregion
2008-09-07 17:52:16 +00:00
namespace CodeImp.DoomBuilder
2008-05-23 06:00:37 +00:00
{
internal class Launcher : IDisposable
{
#region = = = = = = = = = = = = = = = = = = Constants
2008-09-09 23:06:31 +00:00
private const string NUMBERS = "0123456789" ;
2008-05-23 06:00:37 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
private string tempwad ;
private bool isdisposed ;
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
public string TempWAD { get { return tempwad ; } }
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Destructor
// Constructor
public Launcher ( MapManager manager )
{
// Initialize
this . tempwad = General . MakeTempFilename ( manager . TempPath , "wad" ) ;
// Bind actions
General . Actions . BindMethods ( this ) ;
}
// Disposer
public void Dispose ( )
{
// Not yet disposed?
if ( ! isdisposed )
{
// Unbind actions
General . Actions . UnbindMethods ( this ) ;
// Done
isdisposed = true ;
}
}
#endregion
#region = = = = = = = = = = = = = = = = = = Parameters
// This takes the unconverted parameters (with placeholders) and converts it
// to parameters with full paths, names and numbers where placeholders were put.
// The tempfile must be the full path and filename to the PWAD file to test.
2009-02-04 15:54:15 +00:00
public string ConvertParameters ( string parameters , int skill , bool shortpaths )
2008-05-23 06:00:37 +00:00
{
string outp = parameters ;
DataLocation iwadloc ;
string p_wp = "" , p_wf = "" ;
string p_ap = "" , p_apq = "" ;
2008-09-09 23:06:31 +00:00
string p_l1 = "" , p_l2 = "" ;
2008-09-26 05:53:34 +00:00
string p_nm = "" ;
2009-02-04 15:54:15 +00:00
string f = tempwad ;
// Make short path if needed
if ( shortpaths ) f = General . GetShortFilePath ( f ) ;
2008-05-23 06:00:37 +00:00
// Find the first IWAD file
if ( General . Map . Data . FindFirstIWAD ( out iwadloc ) )
{
// %WP and %WF result in IWAD file
p_wp = iwadloc . location ;
p_wf = Path . GetFileName ( p_wp ) ;
2009-02-04 15:54:15 +00:00
if ( shortpaths )
{
p_wp = General . GetShortFilePath ( p_wp ) ;
p_wf = General . GetShortFilePath ( p_wf ) ;
}
2008-05-23 06:00:37 +00:00
}
// Make a list of all data locations, including map location
DataLocation maplocation = new DataLocation ( DataLocation . RESOURCE_WAD , General . Map . FilePathName , false , false ) ;
DataLocationList locations = new DataLocationList ( ) ;
locations . AddRange ( General . Map . Options . Resources ) ;
locations . Add ( maplocation ) ;
// Go for all data locations
foreach ( DataLocation dl in locations )
{
// Location not the IWAD file?
if ( ( dl . type = = DataLocation . RESOURCE_WAD ) & & ( dl . location ! = iwadloc . location ) )
{
// Add to string of files
2009-02-04 15:54:15 +00:00
if ( shortpaths )
{
p_ap + = General . GetShortFilePath ( dl . location ) + " " ;
p_apq + = "\"" + General . GetShortFilePath ( dl . location ) + "\" " ;
}
else
{
p_ap + = dl . location + " " ;
p_apq + = "\"" + dl . location + "\" " ;
}
2008-05-23 06:00:37 +00:00
}
}
// Trim last space from resource file locations
p_ap = p_ap . TrimEnd ( ' ' ) ;
p_apq = p_apq . TrimEnd ( ' ' ) ;
2008-09-09 23:06:31 +00:00
// Try finding the L1 and L2 numbers from the map name
string numstr = "" ;
bool first = true ;
foreach ( char c in General . Map . Options . CurrentName )
{
// Character is a number?
if ( NUMBERS . IndexOf ( c ) > - 1 )
{
// Include it
numstr + = c ;
}
else
{
// Store the number if we found one
if ( numstr . Length > 0 )
{
int num = 0 ;
int . TryParse ( numstr , out num ) ;
if ( first ) p_l1 = num . ToString ( ) ; else p_l2 = num . ToString ( ) ;
2009-01-14 12:57:35 +00:00
numstr = "" ;
2008-09-09 23:06:31 +00:00
first = false ;
}
}
}
// Store the number if we found one
if ( numstr . Length > 0 )
{
int num = 0 ;
int . TryParse ( numstr , out num ) ;
if ( first ) p_l1 = num . ToString ( ) ; else p_l2 = num . ToString ( ) ;
}
2008-09-26 05:53:34 +00:00
// No monsters?
if ( ! General . Settings . TestMonsters ) p_nm = "-nomonsters" ;
2008-05-23 06:00:37 +00:00
// Make sure all our placeholders are in uppercase
outp = outp . Replace ( "%f" , "%F" ) ;
outp = outp . Replace ( "%wp" , "%WP" ) ;
outp = outp . Replace ( "%wf" , "%WF" ) ;
outp = outp . Replace ( "%wP" , "%WP" ) ;
outp = outp . Replace ( "%wF" , "%WF" ) ;
outp = outp . Replace ( "%Wp" , "%WP" ) ;
outp = outp . Replace ( "%Wf" , "%WF" ) ;
2008-09-09 23:06:31 +00:00
outp = outp . Replace ( "%l1" , "%L1" ) ;
outp = outp . Replace ( "%l2" , "%L2" ) ;
2008-05-23 06:00:37 +00:00
outp = outp . Replace ( "%l" , "%L" ) ;
outp = outp . Replace ( "%ap" , "%AP" ) ;
outp = outp . Replace ( "%aP" , "%AP" ) ;
outp = outp . Replace ( "%Ap" , "%AP" ) ;
2008-09-09 23:06:31 +00:00
outp = outp . Replace ( "%s" , "%S" ) ;
2008-09-26 05:53:34 +00:00
outp = outp . Replace ( "%nM" , "%NM" ) ;
outp = outp . Replace ( "%Nm" , "%NM" ) ;
outp = outp . Replace ( "%nm" , "%NM" ) ;
2008-05-23 06:00:37 +00:00
// Replace placeholders with actual values
2009-02-23 14:26:01 +00:00
outp = outp . Replace ( "%F" , f ) ;
2008-05-23 06:00:37 +00:00
outp = outp . Replace ( "%WP" , p_wp ) ;
outp = outp . Replace ( "%WF" , p_wf ) ;
2008-09-09 23:06:31 +00:00
outp = outp . Replace ( "%L1" , p_l1 ) ;
outp = outp . Replace ( "%L2" , p_l2 ) ;
2008-05-23 06:00:37 +00:00
outp = outp . Replace ( "%L" , General . Map . Options . CurrentName ) ;
outp = outp . Replace ( "\"%AP\"" , p_apq ) ;
outp = outp . Replace ( "%AP" , p_ap ) ;
2008-09-09 23:06:31 +00:00
outp = outp . Replace ( "%S" , skill . ToString ( ) ) ;
2008-09-26 05:53:34 +00:00
outp = outp . Replace ( "%NM" , p_nm ) ;
2008-05-23 06:00:37 +00:00
// Return result
return outp ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Test
// This saves the map to a temporary file and launches a test
[BeginAction("testmap")]
public void Test ( )
2008-09-10 08:43:20 +00:00
{
TestAtSkill ( General . Map . ConfigSettings . TestSkill ) ;
}
// This saves the map to a temporary file and launches a test wit hthe given skill
public void TestAtSkill ( int skill )
2008-05-23 06:00:37 +00:00
{
Cursor oldcursor = Cursor . Current ;
ProcessStartInfo processinfo ;
Process process ;
TimeSpan deltatime ;
string args ;
// Check if configuration is OK
if ( ( General . Map . ConfigSettings . TestProgram = = "" ) | |
! File . Exists ( General . Map . ConfigSettings . TestProgram ) )
{
// Show message
Cursor . Current = Cursors . Default ;
DialogResult result = General . ShowWarningMessage ( "Your test program is not set for the current game configuration. Would you like to set up your test program now?" , MessageBoxButtons . YesNo ) ;
if ( result = = DialogResult . Yes )
{
// Show game configuration on the right page
General . MainWindow . ShowConfigurationPage ( 2 ) ;
}
return ;
}
2008-09-09 23:06:31 +00:00
// No custom parameters?
if ( ! General . Map . ConfigSettings . CustomParameters )
{
// Set parameters to the default ones
General . Map . ConfigSettings . TestParameters = General . Map . Config . TestParameters ;
2009-02-04 15:54:15 +00:00
General . Map . ConfigSettings . TestShortPaths = General . Map . Config . TestShortPaths ;
2008-09-09 23:06:31 +00:00
}
2008-05-23 06:00:37 +00:00
// Save map to temporary file
Cursor . Current = Cursors . WaitCursor ;
tempwad = General . MakeTempFilename ( General . Map . TempPath , "wad" ) ;
if ( General . Map . SaveMap ( tempwad , MapManager . SAVE_TEST ) )
{
// Make arguments
2009-02-04 15:54:15 +00:00
args = ConvertParameters ( General . Map . ConfigSettings . TestParameters , skill , General . Map . ConfigSettings . TestShortPaths ) ;
2008-05-23 06:00:37 +00:00
// Setup process info
processinfo = new ProcessStartInfo ( ) ;
processinfo . Arguments = args ;
processinfo . FileName = General . Map . ConfigSettings . TestProgram ;
processinfo . CreateNoWindow = false ;
processinfo . ErrorDialog = false ;
processinfo . UseShellExecute = true ;
processinfo . WindowStyle = ProcessWindowStyle . Normal ;
processinfo . WorkingDirectory = Path . GetDirectoryName ( processinfo . FileName ) ;
// Output info
General . WriteLogLine ( "Running test program: " + processinfo . FileName ) ;
General . WriteLogLine ( "Program parameters: " + processinfo . Arguments ) ;
2008-09-15 21:53:00 +00:00
// Disable interface
General . MainWindow . DisplayStatus ( "Waiting for game application to finish..." ) ;
2008-05-23 06:00:37 +00:00
try
{
// Start the program
process = Process . Start ( processinfo ) ;
// Wait for program to complete
2008-09-15 21:53:00 +00:00
while ( ! process . WaitForExit ( 10 ) )
{
General . MainWindow . Update ( ) ;
}
// Done
2008-05-23 06:00:37 +00:00
deltatime = TimeSpan . FromTicks ( process . ExitTime . Ticks - process . StartTime . Ticks ) ;
General . WriteLogLine ( "Test program has finished." ) ;
General . WriteLogLine ( "Run time: " + deltatime . TotalSeconds . ToString ( "###########0.00" ) + " seconds" ) ;
}
catch ( Exception e )
{
// Unable to start the program
General . ShowErrorMessage ( "Unable to start the test program, " + e . GetType ( ) . Name + ": " + e . Message , MessageBoxButtons . OK ) ; ;
}
}
// Remove temporary file
2008-09-10 08:43:20 +00:00
try { File . Delete ( tempwad ) ; }
catch ( Exception ) { }
2008-09-15 21:53:00 +00:00
// Done
General . MainWindow . DisplayReady ( ) ;
2008-12-08 06:29:30 +00:00
General . MainWindow . FocusDisplay ( ) ;
2008-05-23 06:00:37 +00:00
Cursor . Current = oldcursor ;
}
#endregion
}
}