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 ;
2014-01-17 11:51:52 +00:00
using System.Text.RegularExpressions ;
2014-01-30 22:32:29 +00:00
using System.Windows.Forms ;
2011-05-06 01:37:28 +00:00
2014-07-27 15:22:30 +00:00
using LuaInterface ;
2013-10-25 00:57:23 +00:00
using BizHawk.Client.Common ;
2013-11-04 01:39:19 +00:00
using BizHawk.Emulation.Common ;
2014-11-30 14:18:44 +00:00
using BizHawk.Emulation.Common.IEmulatorExtensions ;
2014-07-27 15:22:30 +00:00
using BizHawk.Client.EmuHawk.WinFormExtensions ;
2014-07-28 01:51:11 +00:00
using BizHawk.Client.EmuHawk.ToolExtensions ;
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
{
2015-01-01 17:48:25 +00:00
public partial class LuaConsole : Form , IToolFormAutoConfig
2011-06-16 02:39:35 +00:00
{
2013-12-19 01:17:53 +00:00
private readonly LuaFileList _luaList ;
2014-01-21 16:25:51 +00:00
private bool _sortReverse ;
private string _lastColumnSorted ;
2011-06-16 02:39:35 +00:00
2015-01-27 23:18:48 +00:00
private List < string > _consoleCommandHistory = new List < string > ( ) ;
private int _consoleCommandHistoryIndex = - 1 ;
2015-01-02 19:36:32 +00:00
2011-06-16 02:39:35 +00:00
public LuaConsole ( )
{
2014-01-21 16:25:51 +00:00
_sortReverse = false ;
2014-01-30 22:32:29 +00:00
_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 ) = >
{
2014-08-19 19:24:17 +00:00
if ( AskSaveChanges ( ) )
2013-11-25 23:38:10 +00:00
{
SaveConfigSettings ( ) ;
2014-08-16 20:09:57 +00:00
GlobalWin . DisplayManager . ClearLuaSurfaces ( ) ;
2013-11-25 23:38:10 +00:00
}
else
{
e . Cancel = true ;
}
} ;
2014-01-30 22:32:29 +00:00
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 ;
2015-01-02 19:36:32 +00:00
2015-01-27 23:18:48 +00:00
InputBox . AutoCompleteCustomSource . AddRange ( LuaImp . Docs . Select ( a = > a . Library + "." + a . Name ) . ToArray ( ) ) ;
2011-06-16 02:39:35 +00:00
}
2014-02-14 01:27:38 +00:00
public EmuLuaLibrary LuaImp { get ; set ; }
public bool UpdateBefore { get { return true ; } }
private IEnumerable < LuaFile > SelectedItems
{
2014-07-28 03:01:57 +00:00
get { return LuaListView . SelectedIndices ( ) . Select ( index = > _luaList [ index ] ) ; }
2014-02-14 01:27:38 +00:00
}
private IEnumerable < LuaFile > SelectedFiles
{
get { return SelectedItems . Where ( x = > ! x . IsSeparator ) ; }
}
2014-07-25 01:55:21 +00:00
public void UpdateValues ( )
{
// Do nothing
}
public void FastUpdate ( )
{
// Do nothing
}
2014-02-14 01:27:38 +00:00
public LuaConsole Get ( ) { return this ; }
public void ConsoleLog ( string message )
{
OutputBox . Text + = message + Environment . NewLine + Environment . NewLine ;
OutputBox . SelectionStart = OutputBox . Text . Length ;
OutputBox . ScrollToCaret ( ) ;
2014-11-18 01:09:06 +00:00
UpdateNumberOfScripts ( ) ;
2014-02-14 01:27:38 +00:00
}
2011-06-16 02:39:35 +00:00
private void LuaConsole_Load ( object sender , EventArgs e )
{
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
}
}
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
{
2014-03-23 14:44:18 +00:00
var processedPath = PathManager . TryMakeRelative ( path ) ;
if ( LuaAlreadyInSession ( processedPath ) = = false )
2012-03-20 21:44:34 +00:00
{
2014-03-23 14:44:18 +00:00
var luaFile = new LuaFile ( string . Empty , processedPath ) ;
2015-01-27 23:18:48 +00:00
2013-11-25 00:44:18 +00:00
_luaList . Add ( luaFile ) ;
2013-11-17 15:55:13 +00:00
LuaListView . ItemCount = _luaList . Count ;
2014-03-23 14:44:18 +00:00
Global . Config . RecentLua . Add ( processedPath ) ;
2015-01-27 23:18:48 +00:00
2012-03-26 23:31:21 +00:00
if ( ! Global . Config . DisableLuaScriptsOnLoad )
{
try
{
2014-03-23 14:44:18 +00:00
luaFile . Thread = LuaImp . SpawnCoroutine ( processedPath ) ;
2013-11-25 00:44:18 +00:00
luaFile . Enabled = true ;
2012-03-26 23:31:21 +00:00
}
catch ( Exception e )
{
2015-01-27 23:18:48 +00:00
if ( e . GetType ( ) = = typeof ( LuaInterface . LuaScriptException ) )
2012-03-26 23:31:21 +00:00
{
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
{
2014-03-23 14:44:18 +00:00
foreach ( var file in _luaList . Where ( file = > processedPath = = 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 )
{
2015-01-27 23:18:48 +00:00
if ( e is LuaScriptException )
2012-03-26 23:31:21 +00:00
{
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
}
}
}
private void SessionChangedCallback ( )
{
OutputMessages . Text =
2014-01-30 22:32:29 +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 )
{
2014-01-30 22:32:29 +00:00
text = string . Empty ;
2013-11-25 21:01:38 +00:00
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
{
2014-03-23 14:44:18 +00:00
text = DressUpRelative ( _luaList [ index ] . Path ) ;
2013-11-25 21:01:38 +00:00
}
}
2014-03-23 14:44:18 +00:00
private string DressUpRelative ( string path )
{
if ( path . StartsWith ( ".\\" ) )
{
return path . Replace ( ".\\" , string . Empty ) ;
}
return path ;
}
2015-01-01 17:48:25 +00:00
// TODO: Rename me
2013-11-25 21:01:38 +00:00
private void SaveConfigSettings ( )
{
LuaImp . Close ( ) ;
}
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 ( )
{
2014-01-30 22:32:29 +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 ( ( ) = >
{
2014-04-29 21:25:17 +00:00
OutputBox . Text + = message ;
2013-11-25 00:44:18 +00:00
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
{
2014-01-30 22:32:29 +00:00
OutputBox . Text = string . Empty ;
2013-11-25 00:44:18 +00:00
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 )
{
2014-02-14 00:55:18 +00:00
LuaImp . GuiLibrary . DrawNew ( "emu" ) ;
2013-11-25 00:44:18 +00:00
}
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 )
{
2014-06-03 02:39:15 +00:00
Environment . CurrentDirectory = PathManager . MakeAbsolutePath ( lf . CurrentDirectory , null ) ;
2013-11-25 00:44:18 +00:00
}
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 )
{
2014-05-26 03:08:16 +00:00
LuaImp . CallExitEvent ( lf . Thread ) ;
2013-11-26 01:21:24 +00:00
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 ;
}
}
}
2015-01-02 19:36:32 +00:00
catch ( Exception ex ) // TODO: it would be better to only catch LuaScriptException and LuaException and rethrow the generic ones
2013-11-25 00:44:18 +00:00
{
2014-05-04 14:10:28 +00:00
if ( ex is LuaScriptException | | ex is LuaException )
2013-11-25 00:44:18 +00:00
{
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
}
2014-06-03 02:39:15 +00:00
finally
{
// Restore the current directory
Environment . CurrentDirectory = oldcd ;
}
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 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
{
2014-02-14 00:55:18 +00:00
LuaImp . GuiLibrary . DrawNew ( "emu" ) ;
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
{
2014-02-14 00:55:18 +00:00
LuaImp . GuiLibrary . DrawFinish ( ) ;
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 ( ) ;
2014-01-30 22:32:29 +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
}
2014-11-30 18:44:20 +00:00
else if ( Global . Game ! = null )
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 )
{
2014-08-19 19:24:17 +00:00
doload = AskSaveChanges ( ) ;
2013-12-19 01:17:53 +00:00
}
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
{
2014-07-28 01:51:11 +00:00
Global . Config . RecentLuaSession . HandleLoadError ( path ) ;
2013-11-25 00:44:18 +00:00
}
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
}
}
2014-08-19 19:24:17 +00:00
public bool AskSaveChanges ( )
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
{
2014-01-30 22:32:29 +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 ;
}
2015-01-27 23:18:48 +00:00
2014-02-14 01:27:38 +00:00
if ( result = = DialogResult . No )
2013-11-25 00:44:18 +00:00
{
2013-11-25 23:38:10 +00:00
_luaList . Changes = false ;
2013-11-25 00:44:18 +00:00
return true ;
}
2015-01-27 23:18:48 +00:00
2014-02-14 01:27:38 +00:00
if ( result = = DialogResult . Cancel )
2013-11-25 00:44:18 +00:00
{
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
}
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
#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 (
2015-01-01 17:28:34 +00:00
Global . Config . RecentLuaSession . RecentMenu ( LoadSessionFromRecent , true ) ) ;
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 (
2014-07-28 01:51:11 +00:00
Global . Config . RecentLua . RecentMenu ( 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
{
2014-08-19 19:24:17 +00:00
var result = ! _luaList . Changes | | AskSaveChanges ( ) ;
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
{
2014-01-30 22:32:29 +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 =
2014-11-09 16:18:09 +00:00
DuplicateScriptMenuItem . Enabled =
2013-11-25 00:44:18 +00:00
MoveUpMenuItem . Enabled =
MoveDownMenuItem . Enabled =
2014-07-28 03:01:57 +00:00
LuaListView . SelectedIndices ( ) . Any ( ) ;
2013-11-25 00:44:18 +00:00
SelectAllMenuItem . Enabled = _luaList . Any ( ) ;
StopAllScriptsMenuItem . Enabled = _luaList . Any ( script = > script . Enabled ) ;
RegisteredFunctionsMenuItem . Enabled = GlobalWin . Tools . LuaConsole . LuaImp . RegisteredFunctions . Any ( ) ;
}
private void NewScriptMenuItem_Click ( object sender , EventArgs e )
{
2014-05-24 01:59:59 +00:00
var sfd = new SaveFileDialog
{
2015-01-27 23:18:48 +00:00
InitialDirectory = ! string . IsNullOrWhiteSpace ( _luaList . Filename ) ?
2014-05-24 01:59:59 +00:00
Path . GetDirectoryName ( _luaList . Filename ) :
PathManager . MakeAbsolutePath ( Global . Config . PathEntries . LuaPathFragment , null ) ,
DefaultExt = ".lua" ,
FileName = ! string . IsNullOrWhiteSpace ( _luaList . Filename ) ?
Path . GetFileNameWithoutExtension ( _luaList . Filename ) :
Path . GetFileNameWithoutExtension ( Global . Game . Name ) ,
2015-01-27 23:18:48 +00:00
OverwritePrompt = true ,
2014-05-24 01:59:59 +00:00
Filter = "Lua Scripts (*.lua)|*.lua|All Files (*.*)|*.*"
} ;
var result = sfd . ShowHawkDialog ( ) ;
if ( result = = DialogResult . OK
& & ! string . IsNullOrWhiteSpace ( sfd . FileName ) )
{
string defaultTemplate = "while true do\n\temu.frameadvance();\nend" ;
File . WriteAllText ( sfd . FileName , defaultTemplate ) ;
_luaList . Add ( new LuaFile ( Path . GetFileNameWithoutExtension ( sfd . FileName ) , sfd . FileName ) ) ;
UpdateDialog ( ) ;
System . Diagnostics . Process . Start ( sfd . FileName ) ;
}
2013-11-25 00:44:18 +00:00
}
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
{
2015-01-02 19:36:32 +00:00
if ( ex is LuaScriptException )
2013-11-25 00:44:18 +00:00
{
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
{
2014-05-26 03:08:16 +00:00
LuaImp . CallExitEvent ( item . Thread ) ;
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
2014-05-26 03:08:16 +00:00
LuaImp . CallExitEvent ( item . Thread ) ;
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-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 ( ) ;
2014-11-09 16:18:09 +00:00
}
}
private void DuplicateScriptMenuItem_Click ( object sender , EventArgs e )
{
if ( LuaListView . SelectedIndices ( ) . Any ( ) )
{
var script = SelectedFiles . First ( ) ;
var sfd = new SaveFileDialog
{
InitialDirectory = Path . GetDirectoryName ( script . Path ) ,
DefaultExt = ".lua" ,
FileName = Path . GetFileNameWithoutExtension ( script . Path ) + " (1)" ,
OverwritePrompt = true ,
Filter = "Lua Scripts (*.lua)|*.lua|All Files (*.*)|*.*"
} ;
if ( sfd . ShowDialog ( ) = = DialogResult . OK )
{
string text = File . ReadAllText ( script . Path ) ;
File . WriteAllText ( sfd . FileName , text ) ;
_luaList . Add ( new LuaFile ( Path . GetFileNameWithoutExtension ( sfd . FileName ) , sfd . FileName ) ) ;
UpdateDialog ( ) ;
System . Diagnostics . Process . Start ( sfd . FileName ) ;
}
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 )
{
2014-07-28 03:01:57 +00:00
var indices = LuaListView . SelectedIndices ( ) . ToList ( ) ;
2013-11-25 00:44:18 +00:00
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
{
2014-07-28 03:01:57 +00:00
var indices = LuaListView . SelectedIndices ( ) . ToList ( ) ;
2013-11-25 00:44:18 +00:00
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 )
{
2014-07-28 03:01:57 +00:00
var indices = LuaListView . SelectedIndices ( ) . ToList ( ) ;
2013-11-25 00:44:18 +00:00
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
{
2014-02-15 19:15:04 +00:00
LuaListView . SelectAll ( ) ;
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 )
{
2014-07-28 02:31:51 +00:00
new LuaRegisteredFunctionsList
{
StartLocation = this . ChildPointToScreen ( LuaListView )
} . 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
DisableScriptsOnLoadMenuItem . Checked = Global . Config . DisableLuaScriptsOnLoad ;
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 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
#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 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
2015-01-02 19:36:32 +00:00
#region Dialog , Listview , OutputBox , InputBox
2013-11-25 00:44:18 +00:00
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
{
2014-11-26 17:42:30 +00:00
if ( Path . GetExtension ( path ) . ToLower ( ) = = ".lua" | | Path . GetExtension ( path ) . ToLower ( ) = = ".txt" )
2013-11-10 22:05:03 +00:00
{
2013-11-25 00:44:18 +00:00
LoadLuaFile ( path ) ;
UpdateDialog ( ) ;
}
2014-11-26 17:42:30 +00:00
else if ( Path . GetExtension ( path ) . ToLower ( ) = = ".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 )
{
2015-01-27 23:18:48 +00:00
if ( ex is LuaScriptException | | ex is 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-30 22:32:29 +00:00
/// <summary>
2014-01-21 16:25:51 +00:00
/// Sorts the column Ascending on the first click and Descending on the second click.
/// </summary>
private void LuaListView_ColumnClick ( object sender , ColumnClickEventArgs e )
{
2014-01-30 22:32:29 +00:00
var columnToSort = LuaListView . Columns [ e . Column ] . Text ;
var luaListTemp = new List < LuaFile > ( ) ;
2014-01-21 16:25:51 +00:00
if ( columnToSort ! = _lastColumnSorted )
{
_sortReverse = false ;
}
2014-01-30 22:32:29 +00:00
// 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 ( var i = 0 ; i < _luaList . Count ; i + + )
2014-01-21 16:25:51 +00:00
{
2014-01-30 22:32:29 +00:00
var words = Regex . Split ( _luaList [ i ] . Path , ".lua" ) ;
var split = words [ 0 ] . Split ( Path . DirectorySeparatorChar ) ;
2014-01-21 16:25:51 +00:00
luaListTemp . Add ( _luaList [ i ] ) ;
luaListTemp [ i ] . Name = split [ split . Count ( ) - 1 ] ;
}
2014-01-30 22:32:29 +00:00
// Script, Path
2014-01-21 16:25:51 +00:00
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 ( ) ;
}
2014-01-30 22:32:29 +00:00
2014-01-21 16:25:51 +00:00
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 ( ) ;
}
2014-01-30 22:32:29 +00:00
2014-01-21 16:25:51 +00:00
break ;
}
2014-01-30 22:32:29 +00:00
for ( var i = 0 ; i < _luaList . Count ; i + + )
2014-01-21 16:25:51 +00:00
{
_luaList [ i ] = luaListTemp [ i ] ;
2014-01-30 22:32:29 +00:00
_luaList [ i ] . Name = string . Empty ;
2014-01-21 16:25:51 +00:00
}
2014-01-30 22:32:29 +00:00
2014-01-21 16:25:51 +00:00
UpdateDialog ( ) ;
_lastColumnSorted = columnToSort ;
_sortReverse = ! _sortReverse ;
}
2014-01-17 11:51:52 +00:00
2014-02-13 23:25:36 +00:00
private void RefreshScriptMenuItem_Click ( object sender , EventArgs e )
{
ToggleScriptMenuItem_Click ( sender , e ) ;
ToggleScriptMenuItem_Click ( sender , e ) ;
}
2015-01-27 23:18:48 +00:00
private void InputBox_KeyDown ( object sender , KeyEventArgs e )
{
if ( e . KeyCode = = Keys . Enter )
{
string consoleBeforeCall = OutputBox . Text ;
// TODO: Maybe make these try-catches more general
if ( InputBox . Text ! = "" )
{
try
{
LuaImp . ExecuteString ( InputBox . Text ) ;
// TODO: This isn't exactly optimal
if ( OutputBox . Text = = consoleBeforeCall )
ConsoleLog ( "Command successfully executed" ) ;
}
catch ( LuaScriptException ex )
{
2015-01-27 23:27:44 +00:00
try
{
LuaImp . ExecuteString ( string . Format ( "console.log({0})" , InputBox . Text ) ) ;
}
catch
{
ConsoleLog ( ex . ToString ( ) ) ;
}
2015-01-27 23:18:48 +00:00
}
_consoleCommandHistory . Insert ( 0 , InputBox . Text ) ;
_consoleCommandHistoryIndex = - 1 ;
InputBox . Clear ( ) ;
}
}
else if ( e . KeyCode = = Keys . Up )
{
if ( _consoleCommandHistoryIndex < _consoleCommandHistory . Count - 1 )
{
_consoleCommandHistoryIndex + + ;
InputBox . Text = _consoleCommandHistory [ _consoleCommandHistoryIndex ] ;
InputBox . Select ( InputBox . Text . Length , 0 ) ;
}
e . Handled = true ;
}
else if ( e . KeyCode = = Keys . Down )
{
if ( _consoleCommandHistoryIndex = = 0 )
{
_consoleCommandHistoryIndex - - ;
InputBox . Text = "" ;
}
else if ( _consoleCommandHistoryIndex > 0 )
{
_consoleCommandHistoryIndex - - ;
InputBox . Text = _consoleCommandHistory [ _consoleCommandHistoryIndex ] ;
InputBox . Select ( InputBox . Text . Length , 0 ) ;
}
e . Handled = true ;
}
else if ( e . KeyCode = = Keys . Tab )
{
this . ProcessTabKey ( false ) ;
e . Handled = true ;
}
}
protected override bool ProcessTabKey ( bool forward )
{
// TODO: Make me less dirty (please)
return false ;
}
2015-01-02 19:36:32 +00:00
2013-11-25 00:44:18 +00:00
#endregion
2014-04-15 21:46:18 +00:00
private void EraseToolbarItem_Click ( object sender , EventArgs e )
{
GlobalWin . DisplayManager . ClearLuaSurfaces ( ) ;
}
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
}