try to 'sandbox' lua files with their own current directory.

This commit is contained in:
zeromus 2012-11-29 18:42:13 +00:00
parent 0e5ef426d4
commit 31faf359e9
4 changed files with 48 additions and 22 deletions

View File

@ -212,6 +212,11 @@ namespace BizHawk.MultiClient
return false;
}
public static string GetLuaPath()
{
return MakeAbsolutePath(Global.Config.LuaPath, "");
}
public static string GetRomsPath(string sysID)
{
string path = "";

View File

@ -165,7 +165,7 @@ namespace BizHawk.MultiClient
var ofd = new OpenFileDialog();
if (lastLuaFile.Length > 0)
ofd.FileName = Path.GetFileNameWithoutExtension(lastLuaFile);
ofd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, "");
ofd.InitialDirectory = PathManager.GetLuaPath();
ofd.Filter = filter;
ofd.RestoreDirectory = true;
@ -848,21 +848,32 @@ namespace BizHawk.MultiClient
}
for (int i = 0; i < luaList.Count; i++)
{
var lf = luaList[i];
//save old current directory before this lua thread clobbers it for the .net thread
string oldcd = Environment.CurrentDirectory;
try
{
//LuaImp.gui_clearGraphics();
if (luaList[i].Enabled && luaList[i].Thread != null && !(luaList[i].Paused))
if (lf.Enabled && lf.Thread != null && !(lf.Paused))
{
bool prohibit = false;
if (luaList[i].FrameWaiting && !includeFrameWaiters)
if (lf.FrameWaiting && !includeFrameWaiters)
{
prohibit = true;
}
if (!prohibit)
{
var result = LuaImp.ResumeScript(luaList[i].Thread);
if (result.Terminated) luaList[i].Stop();
luaList[i].FrameWaiting = result.WaitForFrame;
//restore this lua thread's preferred current directory
Environment.CurrentDirectory = lf.CurrentDirectory;
var result = LuaImp.ResumeScript(lf.Thread);
if (result.Terminated) lf.Stop();
lf.FrameWaiting = result.WaitForFrame;
//if the lua thread changed its current directory, capture that here
lf.CurrentDirectory = Environment.CurrentDirectory;
}
}
}
@ -870,13 +881,17 @@ namespace BizHawk.MultiClient
{
if (ex is LuaInterface.LuaScriptException || ex is LuaInterface.LuaException)
{
luaList[i].Enabled = false;
luaList[i].Thread = null;
lf.Enabled = false;
lf.Thread = null;
AddText(ex.ToString());
}
else MessageBox.Show(ex.ToString());
}
}
//restore the current directory
Environment.CurrentDirectory = oldcd;
} //loop across luaList
}
//LuaImp.gui_drawFinishEmu();
}
@ -942,13 +957,13 @@ namespace BizHawk.MultiClient
else if (!(Global.Emulator is NullEmulator))
{
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, "");
sfd.InitialDirectory = PathManager.GetLuaPath();
}
else
{
sfd.FileName = "NULL";
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, "");
sfd.InitialDirectory = PathManager.GetLuaPath();
}
sfd.Filter = "Lua Session Files (*.luases)|*.luases|All Files|*.*";
sfd.RestoreDirectory = true;

View File

@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -10,17 +11,18 @@ namespace BizHawk.MultiClient
public string Name;
public string Path;
public bool Enabled;
public bool Paused;
public bool Paused;
public bool IsSeparator;
public LuaInterface.Lua Thread;
public bool FrameWaiting;
public string CurrentDirectory;
public LuaFile(string path)
{
Name = "";
Path = path;
Enabled = true;
Paused = false;
Paused = false;
FrameWaiting = false;
}
@ -35,6 +37,9 @@ namespace BizHawk.MultiClient
Name = name;
Path = path;
IsSeparator = false;
//the current directory for the lua task will start off wherever the lua file is located
CurrentDirectory = new FileInfo(path).Directory.FullName;
}
public LuaFile(bool isSeparator)
@ -50,20 +55,21 @@ namespace BizHawk.MultiClient
Name = l.Name;
Path = l.Path;
Enabled = l.Enabled;
Paused = l.Paused;
Paused = l.Paused;
IsSeparator = l.IsSeparator;
CurrentDirectory = l.CurrentDirectory;
}
public void Toggle()
{
Enabled ^= true;
if (Enabled)
Paused = false;
if (Enabled)
Paused = false;
}
public void TogglePause()
{
Paused ^= true;
}
public void TogglePause()
{
Paused ^= true;
}
}
}

View File

@ -577,12 +577,12 @@ namespace BizHawk.MultiClient
else if (!(Global.Emulator is NullEmulator))
{
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, "");
sfd.InitialDirectory = PathManager.GetLuaPath();
}
else
{
sfd.FileName = "NULL";
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, "");
sfd.InitialDirectory = PathManager.GetLuaPath();
}
sfd.Filter = "Watch Files (*.lua)|*.lua|All Files|*.*";
sfd.RestoreDirectory = true;