2011-05-06 01:37:28 +00:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Drawing ;
2013-12-19 01:17:53 +00:00
using System.IO ;
2011-05-06 01:37:28 +00:00
using System.Linq ;
using System.Windows.Forms ;
2014-01-17 11:51:52 +00:00
using System.Text.RegularExpressions ;
2011-05-06 01:37:28 +00:00
2013-10-25 00:57:23 +00:00
using BizHawk.Client.Common ;
2013-11-04 01:39:19 +00:00
using BizHawk.Emulation.Common ;
2013-10-25 00:57:23 +00:00
2013-11-03 03:54:37 +00:00
namespace BizHawk.Client.EmuHawk
2011-05-06 01:37:28 +00:00
{
2013-11-03 01:02:17 +00:00
public partial class LuaConsole : Form , IToolForm
2011-06-16 02:39:35 +00:00
{
2013-12-19 01:17:53 +00:00
// TODO:
// remember column widths and restore column width on restore default settings
// column reorder
public EmuLuaLibrary LuaImp { get ; set ; }
2011-06-16 02:39:35 +00:00
2013-12-19 01:17:53 +00:00
private readonly LuaFileList _luaList ;
2013-11-25 21:01:38 +00:00
private int _defaultWidth ;
2013-11-17 15:55:13 +00:00
private int _defaultHeight ;
2014-01-21 16:25:51 +00:00
private bool _sortReverse ;
private string _lastColumnSorted ;
2011-06-16 02:39:35 +00:00
2013-11-03 01:02:17 +00:00
public bool UpdateBefore { get { return true ; } }
public void UpdateValues ( ) { }
2013-11-17 15:55:13 +00:00
public LuaConsole Get ( ) { return this ; }
2011-06-16 02:39:35 +00:00
2013-11-25 21:01:38 +00:00
public void ConsoleLog ( string message )
2011-06-16 02:39:35 +00:00
{
2013-11-25 21:01:38 +00:00
OutputBox . Text + = message + Environment . NewLine + Environment . NewLine ;
2013-11-17 15:55:13 +00:00
OutputBox . SelectionStart = OutputBox . Text . Length ;
OutputBox . ScrollToCaret ( ) ;
2011-06-16 02:39:35 +00:00
}
public LuaConsole ( )
{
2014-01-21 16:25:51 +00:00
_sortReverse = false ;
_lastColumnSorted = String . Empty ;
2013-11-25 23:24:26 +00:00
_luaList = new LuaFileList
2013-11-25 21:01:38 +00:00
{
ChangedCallback = SessionChangedCallback ,
LoadCallback = ClearOutputWindow
} ;
2011-06-16 02:39:35 +00:00
InitializeComponent ( ) ;
2014-01-21 16:31:12 +00:00
LuaImp = new EmuLuaLibrary ( this ) ;
2013-11-25 23:38:10 +00:00
Closing + = ( o , e ) = >
{
if ( AskSave ( ) )
{
SaveConfigSettings ( ) ;
}
else
{
e . Cancel = true ;
}
} ;
2013-04-14 23:56:45 +00:00
LuaListView . QueryItemText + = LuaListView_QueryItemText ;
LuaListView . QueryItemBkColor + = LuaListView_QueryItemBkColor ;
2011-06-16 02:39:35 +00:00
LuaListView . VirtualMode = true ;
}
private void LuaConsole_Load ( object sender , EventArgs e )
{
LoadConfigSettings ( ) ;
2013-09-07 03:15:29 +00:00
if ( Global . Config . RecentLuaSession . AutoLoad )
2012-07-25 13:55:25 +00:00
{
2013-07-16 01:59:59 +00:00
if ( ! Global . Config . RecentLuaSession . Empty )
2012-07-25 13:55:25 +00:00
{
2013-09-06 21:23:59 +00:00
LoadSessionFromRecent ( Global . Config . RecentLuaSession [ 0 ] ) ;
2012-07-25 13:55:25 +00:00
}
}
2012-07-25 22:53:11 +00:00
2013-11-25 00:44:18 +00:00
NewScriptToolbarItem . Visible = VersionInfo . INTERIM ;
NewScriptMenuItem . Visible = VersionInfo . INTERIM ;
2011-06-16 02:39:35 +00:00
}
2011-06-26 02:09:06 +00:00
public void Restart ( )
{
2013-11-25 23:45:59 +00:00
UpdateDialog ( ) ;
2011-06-16 02:39:35 +00:00
}
2012-06-07 03:41:45 +00:00
public void LoadLuaFile ( string path )
2011-06-16 02:39:35 +00:00
{
2012-03-20 21:44:34 +00:00
if ( LuaAlreadyInSession ( path ) = = false )
{
2013-11-25 02:08:45 +00:00
var luaFile = new LuaFile ( String . Empty , path ) ;
2013-11-25 00:44:18 +00:00
_luaList . Add ( luaFile ) ;
2013-11-17 15:55:13 +00:00
LuaListView . ItemCount = _luaList . Count ;
2012-03-26 23:31:21 +00:00
Global . Config . RecentLua . Add ( path ) ;
if ( ! Global . Config . DisableLuaScriptsOnLoad )
{
try
{
2013-11-25 00:44:18 +00:00
luaFile . Thread = LuaImp . SpawnCoroutine ( path ) ;
luaFile . Enabled = true ;
2012-03-26 23:31:21 +00:00
}
catch ( Exception e )
{
if ( e . ToString ( ) . Substring ( 0 , 32 ) = = "LuaInterface.LuaScriptException:" )
{
2013-11-25 00:44:18 +00:00
luaFile . Enabled = false ;
2013-11-25 21:01:38 +00:00
ConsoleLog ( e . Message ) ;
2012-03-26 23:31:21 +00:00
}
2013-12-19 01:17:53 +00:00
else
{
MessageBox . Show ( e . ToString ( ) ) ;
}
2012-03-26 23:31:21 +00:00
}
}
2013-12-19 01:17:53 +00:00
else
{
luaFile . Enabled = false ;
}
2013-12-19 00:54:35 +00:00
2013-11-25 00:44:18 +00:00
luaFile . Paused = false ;
2012-03-20 21:44:34 +00:00
}
else
{
2013-11-26 01:21:24 +00:00
foreach ( var file in _luaList . Where ( file = > path = = file . Path & & file . Enabled = = false & & ! Global . Config . DisableLuaScriptsOnLoad ) )
2012-03-20 21:44:34 +00:00
{
2013-11-26 01:21:24 +00:00
file . Toggle ( ) ;
break ;
2012-03-20 21:44:34 +00:00
}
2013-11-25 21:01:38 +00:00
RunLuaScripts ( ) ;
2012-03-20 21:44:34 +00:00
}
2013-12-19 00:54:35 +00:00
UpdateDialog ( ) ;
2011-06-16 02:39:35 +00:00
}
2013-11-25 00:44:18 +00:00
public void UpdateDialog ( )
2011-06-26 02:09:06 +00:00
{
2013-11-17 15:55:13 +00:00
LuaListView . ItemCount = _luaList . Count ;
2013-11-26 01:21:24 +00:00
LuaListView . Refresh ( ) ;
2013-11-25 21:01:38 +00:00
UpdateNumberOfScripts ( ) ;
2013-12-19 00:54:35 +00:00
UpdateRegisteredFunctionsDialog ( ) ;
2011-06-26 02:09:06 +00:00
}
2012-03-25 15:34:09 +00:00
public void RunLuaScripts ( )
2012-01-21 20:05:53 +00:00
{
2013-11-26 01:21:24 +00:00
foreach ( var file in _luaList )
2012-03-26 23:31:21 +00:00
{
2013-11-26 01:21:24 +00:00
if ( file . Enabled & & file . Thread = = null )
2012-03-26 23:31:21 +00:00
{
try
{
2013-11-26 01:21:24 +00:00
file . Thread = LuaImp . SpawnCoroutine ( file . Path ) ;
2012-03-26 23:31:21 +00:00
}
catch ( Exception e )
{
if ( e . ToString ( ) . Substring ( 0 , 32 ) = = "LuaInterface.LuaScriptException:" )
{
2013-11-26 01:21:24 +00:00
file . Enabled = false ;
2013-11-25 21:01:38 +00:00
ConsoleLog ( e . Message ) ;
2012-03-26 23:31:21 +00:00
}
2013-12-19 01:17:53 +00:00
else
{
MessageBox . Show ( e . ToString ( ) ) ;
}
2012-03-26 23:31:21 +00:00
}
}
else
{
2013-11-26 01:21:24 +00:00
file . Stop ( ) ;
2013-11-25 21:01:38 +00:00
_luaList . Changes = true ;
}
}
}
private void SessionChangedCallback ( )
{
OutputMessages . Text =
2014-01-21 16:25:51 +00:00
( _luaList . Changes ? "* " : String . Empty ) +
2013-11-25 21:01:38 +00:00
Path . GetFileName ( _luaList . Filename ) ;
}
private void LuaListView_QueryItemBkColor ( int index , int column , ref Color color )
{
if ( column = = 0 )
{
if ( _luaList [ index ] . IsSeparator )
{
color = BackColor ;
}
else if ( _luaList [ index ] . Enabled & & ! _luaList [ index ] . Paused )
{
color = Color . LightCyan ;
}
else if ( _luaList [ index ] . Enabled & & _luaList [ index ] . Paused )
{
color = Color . IndianRed ;
2012-03-26 23:31:21 +00:00
}
}
2013-11-25 21:01:38 +00:00
UpdateNumberOfScripts ( ) ;
}
private void LuaListView_QueryItemText ( int index , int column , out string text )
{
text = String . Empty ;
if ( column = = 0 )
{
2013-12-19 01:17:53 +00:00
text = Path . GetFileNameWithoutExtension ( _luaList [ index ] . Path ) ; // TODO: how about allow the user to name scripts?
2013-11-25 21:01:38 +00:00
}
2013-12-19 01:17:53 +00:00
else if ( column = = 1 )
2013-11-25 21:01:38 +00:00
{
text = _luaList [ index ] . Path ;
}
}
private void SaveConfigSettings ( )
{
LuaImp . Close ( ) ;
Global . Config . LuaConsoleWndx = Location . X ;
Global . Config . LuaConsoleWndy = Location . Y ;
Global . Config . LuaConsoleWidth = Right - Left ;
Global . Config . LuaConsoleHeight = Bottom - Top ;
}
private void LoadConfigSettings ( )
{
2013-12-19 01:17:53 +00:00
_defaultWidth = Size . Width ;
2013-11-25 21:01:38 +00:00
_defaultHeight = Size . Height ;
2013-12-19 01:17:53 +00:00
if ( Global . Config . LuaConsoleSaveWindowPosition & & Global . Config . LuaConsoleWndx > = 0
2014-01-21 16:25:51 +00:00
& & Global . Config . LuaConsoleWndy > = 0 )
2013-12-19 01:17:53 +00:00
{
2013-11-25 21:01:38 +00:00
Location = new Point ( Global . Config . LuaConsoleWndx , Global . Config . LuaConsoleWndy ) ;
2013-12-19 01:17:53 +00:00
}
2013-11-25 21:01:38 +00:00
if ( Global . Config . LuaConsoleWidth > = 0 & & Global . Config . LuaConsoleHeight > = 0 )
{
Size = new Size ( Global . Config . LuaConsoleWidth , Global . Config . LuaConsoleHeight ) ;
}
}
2013-11-25 23:24:26 +00:00
private static FileInfo GetFileFromUser ( string filter )
2013-11-25 21:01:38 +00:00
{
2013-11-25 23:24:26 +00:00
var ofd = new OpenFileDialog
{
2014-01-21 16:25:51 +00:00
InitialDirectory = PathManager . GetLuaPath ( ) ,
Filter = filter ,
RestoreDirectory = true
2013-11-25 23:24:26 +00:00
} ;
2013-11-25 21:01:38 +00:00
if ( ! Directory . Exists ( ofd . InitialDirectory ) )
2013-11-25 23:24:26 +00:00
{
2013-11-25 21:01:38 +00:00
Directory . CreateDirectory ( ofd . InitialDirectory ) ;
2013-11-25 23:24:26 +00:00
}
2013-11-25 21:01:38 +00:00
2013-11-28 22:39:00 +00:00
var result = ofd . ShowHawkDialog ( ) ;
2013-11-25 23:24:26 +00:00
return result = = DialogResult . OK ? new FileInfo ( ofd . FileName ) : null ;
2011-06-16 02:39:35 +00:00
}
private void UpdateNumberOfScripts ( )
{
2013-11-25 02:08:45 +00:00
var message = String . Empty ;
2013-12-19 01:17:53 +00:00
var total = SelectedFiles . Count ( ) ;
var active = _luaList . Count ( file = > file . Enabled ) ;
var paused = _luaList . Count ( file = > file . Enabled & & file . Paused ) ;
2011-06-16 02:39:35 +00:00
2013-12-19 01:17:53 +00:00
if ( total = = 1 )
2013-11-25 00:44:18 +00:00
{
2013-12-19 01:17:53 +00:00
message + = total + " script (" + active + " active, " + paused + " paused)" ;
2013-11-25 00:44:18 +00:00
}
2013-12-19 01:17:53 +00:00
else if ( total = = 0 )
2013-11-25 00:44:18 +00:00
{
2013-12-19 01:17:53 +00:00
message + = total + " scripts" ;
2013-11-25 00:44:18 +00:00
}
2011-06-16 02:39:35 +00:00
else
2013-11-25 00:44:18 +00:00
{
2013-12-19 01:17:53 +00:00
message + = total + " scripts (" + active + " active, " + paused + " paused)" ;
2013-11-25 00:44:18 +00:00
}
2011-06-16 02:39:35 +00:00
NumberOfScripts . Text = message ;
}
2013-11-25 00:44:18 +00:00
private void LoadLuaFromRecent ( string path )
2011-06-16 02:39:35 +00:00
{
2013-11-25 00:44:18 +00:00
LoadLuaFile ( path ) ;
2011-06-16 02:39:35 +00:00
}
2013-11-25 00:44:18 +00:00
private bool LuaAlreadyInSession ( string path )
2011-06-16 02:39:35 +00:00
{
2013-11-25 00:44:18 +00:00
return _luaList . Any ( t = > path = = t . Path ) ;
2011-06-16 02:39:35 +00:00
}
2011-06-26 02:09:06 +00:00
2013-11-25 00:44:18 +00:00
public void WriteToOutputWindow ( string message )
2011-06-26 02:09:06 +00:00
{
2013-11-25 00:44:18 +00:00
if ( ! OutputBox . IsHandleCreated | | OutputBox . IsDisposed )
2013-12-19 01:17:53 +00:00
{
2013-11-25 00:44:18 +00:00
return ;
2013-12-19 01:17:53 +00:00
}
2013-11-25 00:44:18 +00:00
OutputBox . Invoke ( ( ) = >
{
OutputBox . Text + = message + "\n\n" ;
OutputBox . SelectionStart = OutputBox . Text . Length ;
OutputBox . ScrollToCaret ( ) ;
} ) ;
2012-01-22 23:56:49 +00:00
}
2013-11-25 00:44:18 +00:00
public void ClearOutputWindow ( )
2012-01-22 23:56:49 +00:00
{
2013-11-25 00:44:18 +00:00
if ( ! OutputBox . IsHandleCreated | | OutputBox . IsDisposed )
2013-12-19 01:17:53 +00:00
{
2013-11-25 00:44:18 +00:00
return ;
2013-12-19 01:17:53 +00:00
}
2012-03-17 22:23:52 +00:00
2013-11-25 00:44:18 +00:00
OutputBox . Invoke ( ( ) = >
2012-03-17 22:23:52 +00:00
{
2013-11-25 00:44:18 +00:00
OutputBox . Text = String . Empty ;
OutputBox . Refresh ( ) ;
} ) ;
2011-06-26 02:09:06 +00:00
}
2013-11-25 00:44:18 +00:00
public bool LoadLuaSession ( string path )
2011-06-26 02:09:06 +00:00
{
2013-11-25 21:01:38 +00:00
return _luaList . LoadLuaSession ( path ) ;
2011-06-26 03:03:15 +00:00
}
2013-11-25 00:44:18 +00:00
/// <summary>
2013-12-19 01:17:53 +00:00
/// resumes suspended Co-routines
2013-11-25 00:44:18 +00:00
/// </summary>
/// <param name="includeFrameWaiters">should frame waiters be waken up? only use this immediately before a frame of emulation</param>
public void ResumeScripts ( bool includeFrameWaiters )
2011-06-26 03:03:15 +00:00
{
2013-11-27 21:03:48 +00:00
if ( _luaList . Any ( ) )
2011-06-26 03:03:15 +00:00
{
2013-11-25 00:44:18 +00:00
if ( LuaImp . GuiLibrary . SurfaceIsNull )
{
LuaImp . GuiLibrary . DrawNewEmu ( ) ;
}
2013-12-19 01:17:53 +00:00
2013-11-25 00:44:18 +00:00
foreach ( var lf in _luaList )
{
2013-12-19 01:17:53 +00:00
var oldcd = Environment . CurrentDirectory ; // Save old current directory before this lua thread clobbers it for the .net thread
2011-06-26 03:03:15 +00:00
2013-11-25 00:44:18 +00:00
try
{
2013-12-19 01:17:53 +00:00
if ( lf . Enabled & & lf . Thread ! = null & & ! lf . Paused )
2013-11-25 00:44:18 +00:00
{
2013-11-25 02:08:45 +00:00
var prohibit = lf . FrameWaiting & & ! includeFrameWaiters ;
2013-11-25 00:44:18 +00:00
if ( ! prohibit )
{
2013-12-19 01:17:53 +00:00
// Restore this lua thread's preferred current directory
2013-11-25 00:44:18 +00:00
if ( lf . CurrentDirectory ! = null )
{
Environment . CurrentDirectory = lf . CurrentDirectory ;
}
2013-12-19 01:17:53 +00:00
2013-11-25 00:44:18 +00:00
var result = LuaImp . ResumeScript ( lf . Thread ) ;
2013-11-26 01:21:24 +00:00
if ( result . Terminated )
{
lf . Stop ( ) ;
}
2013-12-19 01:17:53 +00:00
2013-11-25 00:44:18 +00:00
lf . FrameWaiting = result . WaitForFrame ;
2011-06-26 03:03:15 +00:00
2013-12-19 01:17:53 +00:00
// If the lua thread changed its current directory, capture that here
2013-11-25 00:44:18 +00:00
lf . CurrentDirectory = Environment . CurrentDirectory ;
}
}
}
catch ( Exception ex )
{
if ( ex is LuaInterface . LuaScriptException | | ex is LuaInterface . LuaException )
{
lf . Enabled = false ;
lf . Thread = null ;
2013-11-25 21:01:38 +00:00
ConsoleLog ( ex . ToString ( ) ) ;
2013-11-25 00:44:18 +00:00
}
2013-12-19 01:17:53 +00:00
else
{
MessageBox . Show ( ex . ToString ( ) ) ;
}
2013-11-25 00:44:18 +00:00
}
2011-06-26 03:03:15 +00:00
2013-12-19 01:17:53 +00:00
// Restore the current directory
2013-11-25 00:44:18 +00:00
Environment . CurrentDirectory = oldcd ;
}
}
2011-06-26 03:03:15 +00:00
}
2013-11-25 00:44:18 +00:00
public void StartLuaDrawing ( )
2011-06-26 03:03:15 +00:00
{
2013-12-19 01:17:53 +00:00
if ( _luaList . Any ( ) & & LuaImp . GuiLibrary . SurfaceIsNull )
2011-06-26 03:03:15 +00:00
{
2013-12-19 01:17:53 +00:00
LuaImp . GuiLibrary . DrawNewEmu ( ) ;
2011-06-26 03:03:15 +00:00
}
2013-11-25 00:44:18 +00:00
}
2011-06-26 03:03:15 +00:00
2013-11-25 00:44:18 +00:00
public void EndLuaDrawing ( )
{
2013-11-25 21:01:38 +00:00
if ( _luaList . Any ( ) )
2013-04-14 23:56:45 +00:00
{
2013-11-25 00:44:18 +00:00
LuaImp . GuiLibrary . DrawFinishEmu ( ) ;
2013-04-14 23:56:45 +00:00
}
2011-06-26 03:03:15 +00:00
}
2012-01-10 03:12:01 +00:00
2013-11-25 00:44:18 +00:00
public bool WaitOne ( int timeout )
2012-01-10 03:12:01 +00:00
{
2013-11-25 00:44:18 +00:00
if ( ! IsHandleCreated | | IsDisposed )
{
return true ;
}
2012-01-10 03:12:01 +00:00
2013-11-25 00:44:18 +00:00
return LuaImp . LuaWait . WaitOne ( timeout ) ;
2012-01-10 03:12:01 +00:00
}
2013-11-25 00:44:18 +00:00
private FileInfo GetSaveFileFromUser ( )
2012-03-20 21:44:34 +00:00
{
2013-11-25 00:44:18 +00:00
var sfd = new SaveFileDialog ( ) ;
2013-11-25 21:01:38 +00:00
if ( ! String . IsNullOrWhiteSpace ( _luaList . Filename ) )
2012-01-11 02:29:50 +00:00
{
2013-11-25 21:01:38 +00:00
sfd . FileName = Path . GetFileNameWithoutExtension ( _luaList . Filename ) ;
sfd . InitialDirectory = Path . GetDirectoryName ( _luaList . Filename ) ;
2012-01-10 03:12:01 +00:00
}
2013-11-25 00:44:18 +00:00
else if ( ! ( Global . Emulator is NullEmulator ) )
2012-07-22 19:59:33 +00:00
{
2013-11-25 00:44:18 +00:00
sfd . FileName = PathManager . FilesystemSafeName ( Global . Game ) ;
sfd . InitialDirectory = PathManager . GetLuaPath ( ) ;
2012-07-22 19:59:33 +00:00
}
else
{
2013-11-25 00:44:18 +00:00
sfd . FileName = "NULL" ;
sfd . InitialDirectory = PathManager . GetLuaPath ( ) ;
2012-07-22 19:59:33 +00:00
}
2013-12-19 01:17:53 +00:00
2013-11-25 00:44:18 +00:00
sfd . Filter = "Lua Session Files (*.luases)|*.luases|All Files|*.*" ;
sfd . RestoreDirectory = true ;
2013-11-28 22:39:00 +00:00
var result = sfd . ShowHawkDialog ( ) ;
2013-11-25 00:44:18 +00:00
if ( result ! = DialogResult . OK )
2013-11-28 22:39:00 +00:00
{
2013-11-25 00:44:18 +00:00
return null ;
2013-11-28 22:39:00 +00:00
}
return new FileInfo ( sfd . FileName ) ;
2012-01-11 02:29:50 +00:00
}
2013-11-25 00:44:18 +00:00
private void SaveSessionAs ( )
2012-01-22 03:14:31 +00:00
{
2013-11-25 00:44:18 +00:00
var file = GetSaveFileFromUser ( ) ;
if ( file ! = null )
2012-03-17 23:40:30 +00:00
{
2013-11-25 21:01:38 +00:00
_luaList . SaveSession ( file . FullName ) ;
OutputMessages . Text = Path . GetFileName ( _luaList . Filename ) + " saved." ;
2013-11-25 00:44:18 +00:00
}
2012-01-22 03:14:31 +00:00
}
2012-01-22 22:42:40 +00:00
2013-11-25 00:44:18 +00:00
public void LoadSessionFromRecent ( string path )
2012-01-22 23:56:49 +00:00
{
2013-11-25 02:08:45 +00:00
var doload = true ;
2013-12-19 01:17:53 +00:00
if ( _luaList . Changes )
{
doload = AskSave ( ) ;
}
2013-11-25 00:44:18 +00:00
if ( doload )
2012-01-22 23:56:49 +00:00
{
2013-11-25 21:01:38 +00:00
if ( ! _luaList . LoadLuaSession ( path ) )
2013-11-25 00:44:18 +00:00
{
ToolHelpers . HandleLoadError ( Global . Config . RecentLuaSession , path ) ;
}
else
{
RunLuaScripts ( ) ;
UpdateDialog ( ) ;
2013-11-25 21:01:38 +00:00
_luaList . Changes = false ;
2013-11-25 00:44:18 +00:00
}
2012-01-22 23:56:49 +00:00
}
}
2013-11-25 00:44:18 +00:00
public bool AskSave ( )
2012-01-22 23:56:49 +00:00
{
2013-11-25 21:01:38 +00:00
if ( _luaList . Changes )
2012-10-06 16:13:45 +00:00
{
2013-11-25 00:44:18 +00:00
GlobalWin . Sound . StopSound ( ) ;
2013-11-25 02:08:45 +00:00
var result = MessageBox . Show ( "Save changes to session?" , "Lua Console" , MessageBoxButtons . YesNoCancel , MessageBoxIcon . Question , MessageBoxDefaultButton . Button3 ) ;
2013-11-25 00:44:18 +00:00
GlobalWin . Sound . StartSound ( ) ;
if ( result = = DialogResult . Yes )
2012-03-26 23:31:21 +00:00
{
2013-11-25 21:01:38 +00:00
if ( ! String . IsNullOrWhiteSpace ( _luaList . Filename ) )
2012-03-26 23:31:21 +00:00
{
2013-11-25 21:01:38 +00:00
_luaList . SaveSession ( ) ;
2012-03-26 23:31:21 +00:00
}
2013-11-25 00:44:18 +00:00
else
2012-03-27 07:25:36 +00:00
{
2013-11-25 21:01:38 +00:00
SaveSessionAs ( ) ;
2012-03-27 07:25:36 +00:00
}
2012-11-29 18:42:13 +00:00
2013-11-25 00:44:18 +00:00
return true ;
}
else if ( result = = DialogResult . No )
{
2013-11-25 23:38:10 +00:00
_luaList . Changes = false ;
2013-11-25 00:44:18 +00:00
return true ;
}
else if ( result = = DialogResult . Cancel )
{
return false ;
2013-04-14 23:56:45 +00:00
}
2012-03-23 19:48:45 +00:00
}
2013-12-19 01:17:53 +00:00
2013-11-25 00:44:18 +00:00
return true ;
2012-10-06 16:13:45 +00:00
}
2014-01-24 12:44:55 +00:00
private void OpenLuaWriter ( string path )
2012-10-06 16:13:45 +00:00
{
2014-01-24 12:44:55 +00:00
var writer = new LuaWriter ( this ) { CurrentFile = path } ;
2013-11-25 00:44:18 +00:00
writer . Show ( ) ;
2012-03-23 18:24:29 +00:00
}
2013-11-25 00:44:18 +00:00
private Point GetPromptPoint ( )
2012-01-28 22:00:51 +00:00
{
2013-11-25 02:08:45 +00:00
return PointToScreen (
new Point ( LuaListView . Location . X + 30 , LuaListView . Location . Y + 30 )
) ;
2012-01-28 22:00:51 +00:00
}
2012-03-05 14:27:29 +00:00
2013-11-26 01:21:24 +00:00
private static void UpdateRegisteredFunctionsDialog ( )
{
foreach ( var form in Application . OpenForms . OfType < LuaRegisteredFunctionsList > ( ) )
{
form . UpdateValues ( ) ;
}
}
2013-11-25 00:44:18 +00:00
private IEnumerable < int > SelectedIndices
2012-03-05 14:27:29 +00:00
{
2013-11-25 00:55:56 +00:00
get { return LuaListView . SelectedIndices . Cast < int > ( ) ; }
2012-03-05 14:27:29 +00:00
}
2012-03-13 22:39:07 +00:00
2013-11-25 00:44:18 +00:00
private IEnumerable < LuaFile > SelectedItems
2012-03-17 12:14:59 +00:00
{
2013-11-25 00:55:56 +00:00
get { return SelectedIndices . Select ( index = > _luaList [ index ] ) ; }
2012-03-17 12:14:59 +00:00
}
2013-11-25 00:44:18 +00:00
private IEnumerable < LuaFile > SelectedFiles
2012-03-17 12:19:30 +00:00
{
2013-11-25 00:44:18 +00:00
get { return SelectedItems . Where ( x = > ! x . IsSeparator ) ; }
2012-03-17 12:19:30 +00:00
}
2013-11-25 00:44:18 +00:00
#region Events
2012-03-17 21:31:48 +00:00
2013-11-25 00:44:18 +00:00
#region File Menu
private void FileSubMenu_DropDownOpened ( object sender , EventArgs e )
2012-03-17 21:31:48 +00:00
{
2013-11-25 21:01:38 +00:00
SaveSessionMenuItem . Enabled = _luaList . Changes ;
2012-03-17 21:31:48 +00:00
}
2013-11-25 00:44:18 +00:00
private void RecentSessionsSubMenu_DropDownOpened ( object sender , EventArgs e )
2012-03-17 21:31:48 +00:00
{
2013-11-25 00:44:18 +00:00
RecentSessionsSubMenu . DropDownItems . Clear ( ) ;
RecentSessionsSubMenu . DropDownItems . AddRange (
ToolHelpers . GenerateRecentMenu ( Global . Config . RecentLuaSession , LoadSessionFromRecent )
) ;
2012-03-17 21:31:48 +00:00
}
2013-11-25 00:44:18 +00:00
private void RecentScriptsSubMenu_DropDownOpened ( object sender , EventArgs e )
2012-03-17 21:31:48 +00:00
{
2013-11-25 00:44:18 +00:00
RecentScriptsSubMenu . DropDownItems . Clear ( ) ;
RecentScriptsSubMenu . DropDownItems . AddRange (
ToolHelpers . GenerateRecentMenu ( Global . Config . RecentLua , LoadLuaFromRecent )
) ;
2012-03-17 21:31:48 +00:00
}
2013-11-25 00:44:18 +00:00
private void NewSessionMenuItem_Click ( object sender , EventArgs e )
2012-03-17 21:31:48 +00:00
{
2013-11-25 21:01:38 +00:00
var result = ! _luaList . Changes | | AskSave ( ) ;
2013-11-25 00:44:18 +00:00
if ( result )
{
_luaList . Clear ( ) ;
2013-11-25 21:01:38 +00:00
ClearOutputWindow ( ) ;
2013-11-25 00:44:18 +00:00
UpdateDialog ( ) ;
}
2012-03-17 21:31:48 +00:00
}
2012-03-17 21:59:56 +00:00
2013-11-25 00:44:18 +00:00
private void OpenSessionMenuItem_Click ( object sender , EventArgs e )
2012-03-17 21:59:56 +00:00
{
2013-11-25 00:44:18 +00:00
var file = GetFileFromUser ( "Lua Session Files (*.luases)|*.luases|All Files|*.*" ) ;
if ( file ! = null )
{
2013-11-25 21:01:38 +00:00
_luaList . LoadLuaSession ( file . FullName ) ;
2013-11-25 00:44:18 +00:00
RunLuaScripts ( ) ;
UpdateDialog ( ) ;
2013-11-25 21:01:38 +00:00
_luaList . Changes = false ;
2013-11-25 00:44:18 +00:00
}
2012-03-17 21:59:56 +00:00
}
2013-11-25 00:44:18 +00:00
private void SaveSessionMenuItem_Click ( object sender , EventArgs e )
2012-03-17 21:59:56 +00:00
{
2013-11-25 21:01:38 +00:00
if ( _luaList . Changes )
2012-03-17 21:59:56 +00:00
{
2013-11-25 21:01:38 +00:00
if ( ! String . IsNullOrWhiteSpace ( _luaList . Filename ) )
2012-03-17 21:59:56 +00:00
{
2013-11-25 21:01:38 +00:00
_luaList . SaveSession ( ) ;
2013-09-09 21:36:26 +00:00
}
else
{
2013-11-25 21:01:38 +00:00
SaveSessionAs ( ) ;
2012-03-17 21:59:56 +00:00
}
2013-11-25 00:44:18 +00:00
2013-11-25 21:01:38 +00:00
OutputMessages . Text = Path . GetFileName ( _luaList . Filename ) + " saved." ;
2012-03-17 21:59:56 +00:00
}
}
2013-11-25 00:44:18 +00:00
private void SaveSessionAsMenuItem_Click ( object sender , EventArgs e )
2012-03-17 21:59:56 +00:00
{
2013-11-25 00:44:18 +00:00
SaveSessionAs ( ) ;
}
private void ExitMenuItem_Click ( object sender , EventArgs e )
{
Close ( ) ;
}
#endregion
#region Script
private void ScriptSubMenu_DropDownOpened ( object sender , EventArgs e )
{
ToggleScriptMenuItem . Enabled =
PauseScriptMenuItem . Enabled =
EditScriptMenuItem . Enabled =
SelectedFiles . Any ( ) ;
RemoveScriptMenuItem . Enabled =
MoveUpMenuItem . Enabled =
MoveDownMenuItem . Enabled =
SelectedIndices . Any ( ) ;
SelectAllMenuItem . Enabled = _luaList . Any ( ) ;
StopAllScriptsMenuItem . Enabled = _luaList . Any ( script = > script . Enabled ) ;
RegisteredFunctionsMenuItem . Enabled = GlobalWin . Tools . LuaConsole . LuaImp . RegisteredFunctions . Any ( ) ;
NewScriptMenuItem . Visible = VersionInfo . INTERIM ;
}
private void NewScriptMenuItem_Click ( object sender , EventArgs e )
{
OpenLuaWriter ( null ) ;
}
private void OpenScriptMenuItem_Click ( object sender , EventArgs e )
{
var file = GetFileFromUser ( "Lua Scripts (*.lua)|*.lua|Text (*.text)|*.txt|All Files|*.*" ) ;
if ( file ! = null )
2012-07-10 17:22:23 +00:00
{
2013-11-25 00:44:18 +00:00
LoadLuaFile ( file . FullName ) ;
UpdateDialog ( ) ;
2012-07-10 17:22:23 +00:00
}
2013-11-25 00:44:18 +00:00
}
2012-07-10 17:22:23 +00:00
2013-11-25 00:44:18 +00:00
private void ToggleScriptMenuItem_Click ( object sender , EventArgs e )
{
2013-11-25 21:01:38 +00:00
foreach ( var item in SelectedFiles )
2012-03-17 21:59:56 +00:00
{
2013-11-25 21:01:38 +00:00
item . Toggle ( ) ;
2013-11-25 00:44:18 +00:00
if ( item . Enabled & & item . Thread = = null )
{
try
2012-03-17 21:59:56 +00:00
{
2013-11-25 00:44:18 +00:00
item . Thread = LuaImp . SpawnCoroutine ( item . Path ) ;
2012-03-17 21:59:56 +00:00
}
2013-11-25 00:44:18 +00:00
catch ( Exception ex )
2013-11-17 17:17:18 +00:00
{
2013-11-25 00:44:18 +00:00
if ( ex . ToString ( ) . Substring ( 0 , 32 ) = = "LuaInterface.LuaScriptException:" )
{
item . Enabled = false ;
2013-11-25 21:01:38 +00:00
ConsoleLog ( ex . Message ) ;
2013-11-25 00:44:18 +00:00
}
else
{
MessageBox . Show ( ex . ToString ( ) ) ;
}
2013-11-17 17:17:18 +00:00
}
2012-03-17 21:59:56 +00:00
}
2013-11-25 00:44:18 +00:00
else if ( ! item . Enabled & & item . Thread ! = null )
2013-11-17 17:17:18 +00:00
{
2013-12-19 00:12:46 +00:00
var items = SelectedItems . ToList ( ) ;
foreach ( var sitem in items )
{
var temp = sitem ;
var functions = LuaImp . RegisteredFunctions . Where ( x = > x . Lua = = temp . Thread ) . ToList ( ) ;
foreach ( var function in functions )
{
LuaImp . RegisteredFunctions . Remove ( function ) ;
}
2013-12-19 01:17:53 +00:00
2013-12-19 00:12:46 +00:00
UpdateRegisteredFunctionsDialog ( ) ;
}
2013-12-19 00:54:35 +00:00
2013-11-25 00:44:18 +00:00
item . Stop ( ) ;
2013-11-17 17:17:18 +00:00
}
2012-03-17 21:59:56 +00:00
}
2013-11-25 00:44:18 +00:00
2013-11-25 21:01:38 +00:00
_luaList . Changes = true ;
2013-12-19 00:54:35 +00:00
UpdateDialog ( ) ;
2012-03-17 21:59:56 +00:00
}
2012-03-17 22:23:52 +00:00
2013-11-25 00:44:18 +00:00
private void PauseScriptMenuItem_Click ( object sender , EventArgs e )
2012-03-17 22:23:52 +00:00
{
2013-11-25 00:44:18 +00:00
SelectedFiles . ToList ( ) . ForEach ( x = > x . TogglePause ( ) ) ;
2013-12-19 00:54:35 +00:00
UpdateDialog ( ) ;
2012-03-17 22:23:52 +00:00
}
2013-11-25 00:44:18 +00:00
private void EditScriptMenuItem_Click ( object sender , EventArgs e )
2012-03-17 22:23:52 +00:00
{
2013-11-25 00:44:18 +00:00
SelectedFiles . ToList ( ) . ForEach ( file = > System . Diagnostics . Process . Start ( file . Path ) ) ;
2012-03-17 22:23:52 +00:00
}
2012-03-17 23:16:11 +00:00
2013-11-25 00:44:18 +00:00
private void RemoveScriptMenuItem_Click ( object sender , EventArgs e )
2012-03-17 23:16:11 +00:00
{
2013-11-25 23:38:10 +00:00
var items = SelectedItems . ToList ( ) ;
if ( items . Any ( ) )
2012-03-17 23:16:11 +00:00
{
2013-11-25 23:38:10 +00:00
foreach ( var item in items )
2013-04-14 23:56:45 +00:00
{
2013-11-26 01:21:24 +00:00
var temp = item ;
var functions = LuaImp . RegisteredFunctions . Where ( x = > x . Lua = = temp . Thread ) . ToList ( ) ;
foreach ( var function in functions )
{
LuaImp . RegisteredFunctions . Remove ( function ) ;
}
2013-11-25 00:44:18 +00:00
_luaList . Remove ( item ) ;
2013-04-14 23:56:45 +00:00
}
2013-11-26 01:21:24 +00:00
UpdateRegisteredFunctionsDialog ( ) ;
2013-11-25 00:44:18 +00:00
UpdateDialog ( ) ;
2012-03-17 23:16:11 +00:00
}
2013-11-25 00:44:18 +00:00
}
2012-03-17 23:16:11 +00:00
2013-11-25 00:44:18 +00:00
private void InsertSeparatorMenuItem_Click ( object sender , EventArgs e )
{
var indices = SelectedIndices . ToList ( ) ;
if ( indices . Any ( ) & & indices . Last ( ) < _luaList . Count )
2012-03-17 23:16:11 +00:00
{
2013-11-25 00:44:18 +00:00
_luaList . Insert ( indices . Last ( ) , LuaFile . SeparatorInstance ) ;
2012-03-17 23:16:11 +00:00
}
else
{
2013-11-25 00:44:18 +00:00
_luaList . Add ( LuaFile . SeparatorInstance ) ;
2012-03-17 23:16:11 +00:00
}
2013-11-25 00:44:18 +00:00
UpdateDialog ( ) ;
2012-03-17 23:16:11 +00:00
}
2013-11-25 00:44:18 +00:00
private void MoveUpMenuItem_Click ( object sender , EventArgs e )
2012-03-17 23:16:11 +00:00
{
2013-11-25 00:44:18 +00:00
var indices = SelectedIndices . ToList ( ) ;
if ( indices . Count = = 0 | | indices [ 0 ] = = 0 )
2012-03-17 23:16:11 +00:00
{
2013-11-25 00:44:18 +00:00
return ;
2012-03-17 23:16:11 +00:00
}
2013-11-25 02:08:45 +00:00
foreach ( var index in indices )
2012-03-17 23:16:11 +00:00
{
2013-11-25 00:44:18 +00:00
var file = _luaList [ index ] ;
_luaList . Remove ( file ) ;
_luaList . Insert ( index - 1 , file ) ;
}
2012-03-17 23:16:11 +00:00
2013-11-25 00:44:18 +00:00
var newindices = indices . Select ( t = > t - 1 ) . ToList ( ) ;
LuaListView . SelectedIndices . Clear ( ) ;
2013-11-25 02:08:45 +00:00
foreach ( var newi in newindices )
2013-11-25 00:44:18 +00:00
{
LuaListView . SelectItem ( newi , true ) ;
2012-03-17 23:16:11 +00:00
}
2013-11-25 00:44:18 +00:00
UpdateDialog ( ) ;
}
private void MoveDownMenuItem_Click ( object sender , EventArgs e )
{
var indices = SelectedIndices . ToList ( ) ;
if ( indices . Count = = 0 | | indices . Last ( ) = = _luaList . Count - 1 )
2012-03-17 23:16:11 +00:00
{
2013-11-25 00:44:18 +00:00
return ;
2012-03-17 23:16:11 +00:00
}
2013-11-25 23:24:26 +00:00
for ( var i = indices . Count - 1 ; i > = 0 ; i - - )
2012-03-17 23:16:11 +00:00
{
2013-11-25 23:24:26 +00:00
var file = _luaList [ indices [ i ] ] ;
2013-11-25 00:44:18 +00:00
_luaList . Remove ( file ) ;
2013-11-25 23:24:26 +00:00
_luaList . Insert ( indices [ i ] + 1 , file ) ;
2012-03-17 23:16:11 +00:00
}
2013-11-25 00:44:18 +00:00
var newindices = indices . Select ( t = > t + 1 ) . ToList ( ) ;
LuaListView . SelectedIndices . Clear ( ) ;
2013-11-25 02:08:45 +00:00
foreach ( var newi in newindices )
2012-03-17 23:16:11 +00:00
{
2013-11-25 00:44:18 +00:00
LuaListView . SelectItem ( newi , true ) ;
2012-03-17 23:16:11 +00:00
}
2013-11-25 00:44:18 +00:00
UpdateDialog ( ) ;
2012-03-17 23:16:11 +00:00
}
2012-03-19 16:19:31 +00:00
2013-11-25 00:44:18 +00:00
private void SelectAllMenuItem_Click ( object sender , EventArgs e )
2012-03-19 16:19:31 +00:00
{
2013-11-25 02:08:45 +00:00
for ( var i = 0 ; i < _luaList . Count ; i + + )
2013-11-25 00:44:18 +00:00
{
2013-11-25 02:08:45 +00:00
LuaListView . SelectItem ( i , true ) ;
2013-11-25 00:44:18 +00:00
}
2012-03-19 16:19:31 +00:00
}
2012-03-19 16:49:47 +00:00
2013-11-25 00:44:18 +00:00
private void StopAllScriptsMenuItem_Click ( object sender , EventArgs e )
2012-03-19 16:49:47 +00:00
{
2013-11-25 21:01:38 +00:00
_luaList . StopAllScripts ( ) ;
2012-03-19 16:49:47 +00:00
}
2012-03-24 10:53:26 +00:00
2013-11-25 00:44:18 +00:00
private void RegisteredFunctionsMenuItem_Click ( object sender , EventArgs e )
2012-03-26 23:31:21 +00:00
{
2013-11-25 00:44:18 +00:00
if ( LuaImp . RegisteredFunctions . Any ( ) )
2012-03-26 23:31:21 +00:00
{
2013-11-25 02:08:45 +00:00
var alreadyOpen = false ;
2013-11-25 00:44:18 +00:00
foreach ( Form form in Application . OpenForms )
2012-03-26 23:31:21 +00:00
{
2013-11-25 00:44:18 +00:00
if ( form is LuaRegisteredFunctionsList )
{
alreadyOpen = true ;
form . Focus ( ) ;
}
}
if ( ! alreadyOpen )
{
2013-12-19 01:17:53 +00:00
var form = new LuaRegisteredFunctionsList { StartLocation = GetPromptPoint ( ) } ;
2013-11-25 00:44:18 +00:00
form . Show ( ) ;
2012-03-26 23:31:21 +00:00
}
}
}
2013-11-25 00:44:18 +00:00
#endregion
#region Options
private void OptionsSubMenu_DropDownOpened ( object sender , EventArgs e )
2012-03-26 23:31:21 +00:00
{
2013-11-25 00:44:18 +00:00
SaveWindowPositionMenuItem . Checked = Global . Config . LuaConsoleSaveWindowPosition ;
AutoloadConsoleMenuItem . Checked = Global . Config . AutoLoadLuaConsole ;
AutoloadSessionMenuItem . Checked = Global . Config . RecentLuaSession . AutoLoad ;
DisableScriptsOnLoadMenuItem . Checked = Global . Config . DisableLuaScriptsOnLoad ;
2012-03-26 23:31:21 +00:00
}
2013-11-25 00:44:18 +00:00
private void SaveWindowPositionMenuItem_Click ( object sender , EventArgs e )
2012-03-26 23:31:21 +00:00
{
2013-11-25 00:44:18 +00:00
Global . Config . LuaConsoleSaveWindowPosition ^ = true ;
2012-03-26 23:31:21 +00:00
}
2013-11-25 00:44:18 +00:00
private void AutoloadConsoleMenuItem_Click ( object sender , EventArgs e )
2012-03-26 23:31:21 +00:00
{
2013-11-25 00:44:18 +00:00
Global . Config . AutoLoadLuaConsole ^ = true ;
2012-03-26 23:31:21 +00:00
}
2012-03-28 20:49:58 +00:00
2013-11-25 00:44:18 +00:00
private void AutoloadSessionMenuItem_Click ( object sender , EventArgs e )
2012-03-28 20:49:58 +00:00
{
2013-11-25 00:44:18 +00:00
Global . Config . RecentLuaSession . AutoLoad ^ = true ;
2012-03-28 20:49:58 +00:00
}
2012-07-19 04:19:24 +00:00
2013-11-25 00:44:18 +00:00
private void DisableScriptsOnLoadMenuItem_Click ( object sender , EventArgs e )
2012-07-22 20:25:53 +00:00
{
2013-11-25 00:44:18 +00:00
Global . Config . DisableLuaScriptsOnLoad ^ = true ;
}
2012-07-22 20:25:53 +00:00
2013-11-25 00:44:18 +00:00
private void RestoreDefaultSettingsMenuItem_Click ( object sender , EventArgs e )
{
Size = new Size ( _defaultWidth , _defaultHeight ) ;
2012-07-22 20:25:53 +00:00
}
2013-11-25 00:44:18 +00:00
#endregion
#region Help
private void FunctionsListMenuItem_Click ( object sender , EventArgs e )
2012-07-19 04:19:24 +00:00
{
2013-11-25 00:44:18 +00:00
new LuaFunctionsForm ( ) . Show ( ) ;
2012-07-19 04:19:24 +00:00
}
2012-07-25 17:36:26 +00:00
2013-11-25 00:44:18 +00:00
private void OnlineDocsMenuItem_Click ( object sender , EventArgs e )
2012-07-25 17:36:26 +00:00
{
2013-11-25 00:44:18 +00:00
System . Diagnostics . Process . Start ( "http://tasvideos.org/BizHawk/LuaFunctions.html" ) ;
2012-07-25 17:36:26 +00:00
}
2013-11-25 00:44:18 +00:00
#endregion
#region Toolbar and Context Menu
private void EditToolbarItem_Click ( object sender , EventArgs e )
2012-07-25 17:36:26 +00:00
{
2013-11-25 00:44:18 +00:00
if ( VersionInfo . INTERIM )
{
2013-11-25 02:08:45 +00:00
SelectedFiles . ToList ( ) . ForEach ( x = > OpenLuaWriter ( x . Path ) ) ;
2013-11-25 00:44:18 +00:00
}
else
{
EditScriptMenuItem_Click ( sender , e ) ;
}
2012-07-25 17:36:26 +00:00
}
2013-11-25 00:44:18 +00:00
private void ScriptListContextMenu_Opening ( object sender , CancelEventArgs e )
2012-07-25 17:36:26 +00:00
{
2013-11-25 00:44:18 +00:00
ToggleScriptContextItem . Enabled =
PauseScriptContextItem . Enabled =
EditScriptContextItem . Enabled =
SelectedFiles . Any ( ) ;
StopAllScriptsContextItem . Visible =
ScriptContextSeparator . Visible =
_luaList . Any ( file = > file . Enabled ) ;
2012-07-25 17:36:26 +00:00
}
2013-08-05 00:26:19 +00:00
2013-11-25 00:44:18 +00:00
private void ConsoleContextMenu_Opening ( object sender , CancelEventArgs e )
2013-11-10 23:16:18 +00:00
{
2013-11-25 00:44:18 +00:00
RegisteredFunctionsContextItem . Enabled = LuaImp . RegisteredFunctions . Any ( ) ;
2013-11-10 23:16:18 +00:00
}
2013-11-25 00:44:18 +00:00
private void ClearConsoleContextItem_Click ( object sender , EventArgs e )
2013-08-05 00:26:19 +00:00
{
2013-11-25 21:01:38 +00:00
ClearOutputWindow ( ) ;
2013-11-25 00:44:18 +00:00
}
#endregion
#region Dialog , Listview , OutputBox
private void LuaConsole_DragDrop ( object sender , DragEventArgs e )
{
2013-11-25 02:08:45 +00:00
var filePaths = ( string [ ] ) e . Data . GetData ( DataFormats . FileDrop ) ;
2013-11-25 00:44:18 +00:00
try
2013-08-05 00:26:19 +00:00
{
2013-11-25 02:08:45 +00:00
foreach ( var path in filePaths )
2013-11-10 22:05:03 +00:00
{
2013-12-19 01:17:53 +00:00
if ( Path . GetExtension ( path ) = = ".lua" | | Path . GetExtension ( path ) = = ".txt" )
2013-11-10 22:05:03 +00:00
{
2013-11-25 00:44:18 +00:00
LoadLuaFile ( path ) ;
UpdateDialog ( ) ;
}
2013-12-19 01:17:53 +00:00
else if ( Path . GetExtension ( path ) = = ".luases" )
2013-11-25 00:44:18 +00:00
{
2013-11-25 21:01:38 +00:00
_luaList . LoadLuaSession ( path ) ;
2013-11-25 00:44:18 +00:00
RunLuaScripts ( ) ;
2013-11-25 21:01:38 +00:00
UpdateDialog ( ) ;
_luaList . Changes = false ;
2013-11-25 00:44:18 +00:00
return ;
2013-11-10 22:05:03 +00:00
}
}
2013-11-25 00:44:18 +00:00
}
catch ( Exception ex )
{
if ( ex . ToString ( ) . Substring ( 0 , 32 ) = = "LuaInterface.LuaScriptException:" | | ex . ToString ( ) . Substring ( 0 , 26 ) = = "LuaInterface.LuaException:" )
2013-11-10 22:05:03 +00:00
{
2013-11-25 21:01:38 +00:00
ConsoleLog ( ex . Message ) ;
2013-11-10 22:05:03 +00:00
}
2013-11-25 00:44:18 +00:00
else
{
MessageBox . Show ( ex . Message ) ;
}
}
}
private void LuaConsole_DragEnter ( object sender , DragEventArgs e )
{
e . Effect = e . Data . GetDataPresent ( DataFormats . FileDrop ) ? DragDropEffects . Copy : DragDropEffects . None ;
}
private void LuaListView_KeyDown ( object sender , KeyEventArgs e )
{
if ( e . KeyCode = = Keys . Delete & & ! e . Control & & ! e . Alt & & ! e . Shift )
{
RemoveScriptMenuItem_Click ( null , null ) ;
}
2013-12-19 01:17:53 +00:00
else if ( e . KeyCode = = Keys . A & & e . Control & & ! e . Alt & & ! e . Shift ) // Select All
2013-11-25 00:44:18 +00:00
{
SelectAllMenuItem_Click ( null , null ) ;
}
2013-12-19 01:17:53 +00:00
else if ( e . KeyCode = = Keys . F12 & & ! e . Control & & ! e . Alt & & ! e . Shift ) // F12
2013-11-25 00:44:18 +00:00
{
RegisteredFunctionsMenuItem_Click ( null , null ) ;
2013-08-05 00:26:19 +00:00
}
}
2013-08-05 00:45:41 +00:00
2013-11-25 00:44:18 +00:00
private void LuaListView_ItemActivate ( object sender , EventArgs e )
2013-08-05 00:45:41 +00:00
{
2013-11-25 00:44:18 +00:00
ToggleScriptMenuItem_Click ( sender , e ) ;
2013-08-05 00:45:41 +00:00
}
2013-11-17 16:58:24 +00:00
private void OutputBox_KeyDown ( object sender , KeyEventArgs e )
{
2013-12-19 01:17:53 +00:00
if ( e . KeyCode = = Keys . F12 & & ! e . Control & & ! e . Alt & & ! e . Shift ) // F12
2013-11-17 16:58:24 +00:00
{
2013-11-25 00:44:18 +00:00
RegisteredFunctionsMenuItem_Click ( null , null ) ;
2013-11-17 16:58:24 +00:00
}
}
2013-11-25 00:44:18 +00:00
2014-01-21 16:25:51 +00:00
/// <summary> -MightyMar
/// Sorts the column Ascending on the first click and Descending on the second click.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LuaListView_ColumnClick ( object sender , ColumnClickEventArgs e )
{
String columnToSort = LuaListView . Columns [ e . Column ] . Text ;
List < LuaFile > luaListTemp = new List < LuaFile > ( ) ;
if ( columnToSort ! = _lastColumnSorted )
{
_sortReverse = false ;
}
//For getting the name of the .lua file, for some reason this field is kept blank in LuaFile.cs?
//The Name variable gets emptied again near the end just in case it would break something.
for ( int i = 0 ; i < _luaList . Count ; i + + )
{
String [ ] words = Regex . Split ( _luaList [ i ] . Path , ".lua" ) ;
String [ ] split = words [ 0 ] . Split ( '\\' ) ;
luaListTemp . Add ( _luaList [ i ] ) ;
luaListTemp [ i ] . Name = split [ split . Count ( ) - 1 ] ;
}
//Script, Path
switch ( columnToSort )
{
case "Script" :
if ( _sortReverse )
{
luaListTemp = luaListTemp . OrderByDescending ( x = > x . Name ) . ThenBy ( x = > x . Path ) . ToList ( ) ;
}
else
{
luaListTemp = luaListTemp . OrderBy ( x = > x . Name ) . ThenBy ( x = > x . Path ) . ToList ( ) ;
}
break ;
case "Path" :
if ( _sortReverse )
{
luaListTemp = luaListTemp . OrderByDescending ( x = > x . Path ) . ThenBy ( x = > x . Name ) . ToList ( ) ;
}
else
{
luaListTemp = luaListTemp . OrderBy ( x = > x . Path ) . ThenBy ( x = > x . Name ) . ToList ( ) ;
}
break ;
}
for ( int i = 0 ; i < _luaList . Count ; i + + )
{
_luaList [ i ] = luaListTemp [ i ] ;
_luaList [ i ] . Name = String . Empty ;
}
UpdateDialog ( ) ;
_lastColumnSorted = columnToSort ;
_sortReverse = ! _sortReverse ;
}
2014-01-17 11:51:52 +00:00
2013-11-25 00:44:18 +00:00
#endregion
2014-01-17 11:51:52 +00:00
2013-11-25 00:44:18 +00:00
#endregion
2011-06-16 02:39:35 +00:00
}
2011-05-06 01:37:28 +00:00
}