fix null reference exception when LuaSandbox attempts to use the Logger

This commit is contained in:
adelikat 2017-05-18 14:27:22 -05:00
parent 70baf9d306
commit 4c48542b98
3 changed files with 13 additions and 19 deletions

View File

@ -7,22 +7,16 @@ namespace BizHawk.Client.Common
{ {
public unsafe class LuaSandbox public unsafe class LuaSandbox
{ {
protected static Action<string> Logger; private static readonly System.Runtime.CompilerServices.ConditionalWeakTable<Lua, LuaSandbox> SandboxForThread = new System.Runtime.CompilerServices.ConditionalWeakTable<Lua, LuaSandbox>();
static System.Runtime.CompilerServices.ConditionalWeakTable<Lua, LuaSandbox> SandboxForThread = new System.Runtime.CompilerServices.ConditionalWeakTable<Lua, LuaSandbox>(); public static Action<string> DefaultLogger { get; set; }
public static Action<string> DefaultLogger;
public void SetLogger(Action<string> logger)
{
Logger = logger;
}
public void SetSandboxCurrentDirectory(string dir) public void SetSandboxCurrentDirectory(string dir)
{ {
CurrentDirectory = dir; _currentDirectory = dir;
} }
private string CurrentDirectory; private string _currentDirectory;
#if WINDOWS #if WINDOWS
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
@ -31,9 +25,9 @@ namespace BizHawk.Client.Common
static extern uint GetCurrentDirectoryW(uint nBufferLength, byte* pBuffer); static extern uint GetCurrentDirectoryW(uint nBufferLength, byte* pBuffer);
#endif #endif
bool CoolSetCurrentDirectory(string path, string currDirSpeedHack = null) private bool CoolSetCurrentDirectory(string path, string currDirSpeedHack = null)
{ {
string target = CurrentDirectory + "\\"; string target = _currentDirectory + "\\";
// first we'll bypass it with a general hack: dont do any setting if the value's already there (even at the OS level, setting the directory can be slow) // first we'll bypass it with a general hack: dont do any setting if the value's already there (even at the OS level, setting the directory can be slow)
// yeah I know, not the smoothest move to compare strings here, in case path normalization is happening at some point // yeah I know, not the smoothest move to compare strings here, in case path normalization is happening at some point
@ -91,9 +85,9 @@ namespace BizHawk.Client.Common
{ {
savedEnvironmentCurrDir = Environment.CurrentDirectory; savedEnvironmentCurrDir = Environment.CurrentDirectory;
if (CurrentDirectory != null) if (_currentDirectory != null)
{ {
CoolSetCurrentDirectory(CurrentDirectory, savedEnvironmentCurrDir); CoolSetCurrentDirectory(_currentDirectory, savedEnvironmentCurrDir);
} }
EnvironmentSandbox.Sandbox(callback); EnvironmentSandbox.Sandbox(callback);
@ -101,12 +95,12 @@ namespace BizHawk.Client.Common
catch (LuaException ex) catch (LuaException ex)
{ {
Console.WriteLine(ex); Console.WriteLine(ex);
Logger(ex.ToString()); DefaultLogger(ex.ToString());
exceptionCallback?.Invoke(); exceptionCallback?.Invoke();
} }
finally finally
{ {
if (CurrentDirectory != null) if (_currentDirectory != null)
{ {
CoolSetCurrentDirectory(savedEnvironmentCurrDir); CoolSetCurrentDirectory(savedEnvironmentCurrDir);
} }
@ -118,7 +112,6 @@ namespace BizHawk.Client.Common
var sandbox = new LuaSandbox(); var sandbox = new LuaSandbox();
SandboxForThread.Add(thread, sandbox); SandboxForThread.Add(thread, sandbox);
sandbox.SetSandboxCurrentDirectory(initialDirectory); sandbox.SetSandboxCurrentDirectory(initialDirectory);
sandbox.SetLogger(DefaultLogger);
return sandbox; return sandbox;
} }

View File

@ -23,7 +23,7 @@ namespace BizHawk.Client.EmuHawk
private bool _sortReverse; private bool _sortReverse;
private string _lastColumnSorted; private string _lastColumnSorted;
private List<string> _consoleCommandHistory = new List<string>(); private readonly List<string> _consoleCommandHistory = new List<string>();
private int _consoleCommandHistoryIndex = -1; private int _consoleCommandHistoryIndex = -1;
public bool IsRebootingCore; public bool IsRebootingCore;
@ -82,7 +82,7 @@ namespace BizHawk.Client.EmuHawk
LuaListView.QueryItemIndent += LuaListView_QueryItemIndent; LuaListView.QueryItemIndent += LuaListView_QueryItemIndent;
LuaListView.VirtualMode = true; LuaListView.VirtualMode = true;
//this is bad, in case we ever have more than one gui part running lua.. not sure how much other badness there is like that // this is bad, in case we ever have more than one gui part running lua.. not sure how much other badness there is like that
LuaSandbox.DefaultLogger = ConsoleLog; LuaSandbox.DefaultLogger = ConsoleLog;
} }

View File

@ -43,6 +43,7 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StyleCop_002ESA1202/@EntryIndexedValue">DO_NOT_SHOW</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StyleCop_002ESA1202/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StyleCop_002ESA1210/@EntryIndexedValue">DO_NOT_SHOW</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StyleCop_002ESA1210/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedMemberInSuper_002ELocal/@EntryIndexedValue">HINT</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedMemberInSuper_002ELocal/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedMethodReturnValue_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AF/@EntryIndexedValue">AF</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AF/@EntryIndexedValue">AF</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ARGB/@EntryIndexedValue">ARGB</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ARGB/@EntryIndexedValue">ARGB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AV/@EntryIndexedValue">AV</s:String> <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AV/@EntryIndexedValue">AV</s:String>