Make Lua scripts sharing global scope optional.

This commit is contained in:
SuuperW 2023-10-03 01:20:11 -05:00
parent 39d37a389f
commit a63f4fae98
5 changed files with 46 additions and 20 deletions

View File

@ -48,13 +48,13 @@ namespace BizHawk.Client.Common
bool RemoveNamedFunctionMatching(Func<INamedLuaFunction, bool> predicate);
void SpawnAndSetFileThread(LuaFile lf);
void SpawnAndSetFileThread(LuaFile lf, bool shareGlobals);
void ExecuteString(string command, LuaFile lf = null);
(bool WaitForFrame, bool Terminated) ResumeScript(LuaFile lf);
void EnableLuaFile(LuaFile item);
void EnableLuaFile(LuaFile item, bool shareGlobals);
void DisableLuaScript(LuaFile file);

View File

@ -331,22 +331,27 @@ namespace BizHawk.Client.Common
return ret;
}
public void SpawnAndSetFileThread(LuaFile lf)
public void SpawnAndSetFileThread(LuaFile lf, bool shareGlobals)
{
if (_activeLuas.ContainsKey(lf))
if (_activeLuas.TryGetValue(lf, out Lua al) && al != _luaWithoutFile)
_activeLuas[lf].Dispose();
Lua lua = new();
UpdatePackageTable(lua);
lua.RegisterFunction("print", this, typeof(LuaLibrariesBase).GetMethod(nameof(Print)));
// We cannot copy tables from one Lua to another, unfortunately. Directly assigning them doesn't work at all.
// Transferring each individual value to new tables mostly works, but not always.
// For example event.onframeend would receive bad LuaFunction references.
foreach (string name in _tablesForFunctions)
lua.NewTable(name);
foreach (var item in _functionsToRegister)
lua.RegisterFunction(item.Key, item.Value.Lib, item.Value.Func);
_activeLuas[lf] = lua;
if (shareGlobals)
_activeLuas[lf] = _luaWithoutFile;
else
{
Lua lua = new();
UpdatePackageTable(lua);
lua.RegisterFunction("print", this, typeof(LuaLibrariesBase).GetMethod(nameof(Print)));
// We cannot copy tables from one Lua to another, unfortunately. Directly assigning them doesn't work at all.
// Transferring each individual value to new tables mostly works, but not always.
// For example event.onframeend would receive bad LuaFunction references.
foreach (string name in _tablesForFunctions)
lua.NewTable(name);
foreach (var item in _functionsToRegister)
lua.RegisterFunction(item.Key, item.Value.Lib, item.Value.Func);
_activeLuas[lf] = lua;
}
lf.Thread = SpawnCoroutine(lf);
}
@ -428,11 +433,11 @@ namespace BizHawk.Client.Common
}
}
public void EnableLuaFile(LuaFile item)
public void EnableLuaFile(LuaFile item, bool shareGlobals)
{
LuaSandbox.Sandbox(null, () =>
{
SpawnAndSetFileThread(item);
SpawnAndSetFileThread(item, shareGlobals);
LuaSandbox.CreateSandbox(item.Thread, Path.GetDirectoryName(item.Path));
}, () =>
{

View File

@ -73,6 +73,7 @@ namespace BizHawk.Client.EmuHawk
this.DisableScriptsOnLoadMenuItem = new BizHawk.WinForms.Controls.ToolStripMenuItemEx();
this.ReturnAllIfNoneSelectedMenuItem = new BizHawk.WinForms.Controls.ToolStripMenuItemEx();
this.ReloadWhenScriptFileChangesMenuItem = new BizHawk.WinForms.Controls.ToolStripMenuItemEx();
this.ShareGlobalsMenuItem = new BizHawk.WinForms.Controls.ToolStripMenuItemEx();
this.toolStripSeparator4 = new BizHawk.WinForms.Controls.ToolStripSeparatorEx();
this.RegisterToTextEditorsSubMenu = new BizHawk.WinForms.Controls.ToolStripMenuItemEx();
this.RegisterSublimeText2MenuItem = new BizHawk.WinForms.Controls.ToolStripMenuItemEx();
@ -349,6 +350,7 @@ namespace BizHawk.Client.EmuHawk
this.DisableScriptsOnLoadMenuItem,
this.ReturnAllIfNoneSelectedMenuItem,
this.ReloadWhenScriptFileChangesMenuItem,
this.ShareGlobalsMenuItem,
this.toolStripSeparator4,
this.RegisterToTextEditorsSubMenu});
this.SettingsSubMenu.Text = "&Settings";
@ -369,6 +371,11 @@ namespace BizHawk.Client.EmuHawk
this.ReloadWhenScriptFileChangesMenuItem.Text = "Reload When Script File Changes";
this.ReloadWhenScriptFileChangesMenuItem.Click += new System.EventHandler(this.ReloadWhenScriptFileChangesMenuItem_Click);
//
// ReloadWhenScriptFileChangesMenuItem
//
this.ShareGlobalsMenuItem.Text = "Scripts Share Global Scope";
this.ShareGlobalsMenuItem.Click += new System.EventHandler(this.ShareGlobalsMenuItem_Click);
//
// RegisterToTextEditorsSubMenu
//
this.RegisterToTextEditorsSubMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
@ -790,6 +797,7 @@ namespace BizHawk.Client.EmuHawk
private System.Windows.Forms.SplitContainer splitContainer1;
private BizHawk.WinForms.Controls.ToolStripMenuItemEx ReturnAllIfNoneSelectedMenuItem;
private BizHawk.WinForms.Controls.ToolStripMenuItemEx ReloadWhenScriptFileChangesMenuItem;
private BizHawk.WinForms.Controls.ToolStripMenuItemEx ShareGlobalsMenuItem;
private BizHawk.WinForms.Controls.ToolStripSeparatorEx toolStripSeparator4;
private BizHawk.WinForms.Controls.ToolStripMenuItemEx RegisterToTextEditorsSubMenu;
private BizHawk.WinForms.Controls.ToolStripMenuItemEx RegisterSublimeText2MenuItem;

View File

@ -72,6 +72,8 @@ namespace BizHawk.Client.EmuHawk
public bool DisableLuaScriptsOnLoad { get; set; }
public bool WarnedOnceOnOverwrite { get; set; }
public bool ScriptsShareGlobals { get; set; } = true;
}
[ConfigPersist]
@ -228,7 +230,7 @@ namespace BizHawk.Client.EmuHawk
{
LuaSandbox.Sandbox(file.Thread, () =>
{
LuaImp.SpawnAndSetFileThread(file);
LuaImp.SpawnAndSetFileThread(file, Settings.ScriptsShareGlobals);
LuaSandbox.CreateSandbox(file.Thread, Path.GetDirectoryName(file.Path));
file.State = LuaFile.RunState.Running;
}, () =>
@ -899,7 +901,7 @@ namespace BizHawk.Client.EmuHawk
{
try
{
LuaImp.EnableLuaFile(item);
LuaImp.EnableLuaFile(item, Settings.ScriptsShareGlobals);
}
catch (IOException)
{
@ -1089,6 +1091,7 @@ namespace BizHawk.Client.EmuHawk
DisableScriptsOnLoadMenuItem.Checked = Settings.DisableLuaScriptsOnLoad;
ReturnAllIfNoneSelectedMenuItem.Checked = Settings.ToggleAllIfNoneSelected;
ReloadWhenScriptFileChangesMenuItem.Checked = Settings.ReloadOnScriptFileChange;
ShareGlobalsMenuItem.Checked = Settings.ScriptsShareGlobals;
}
private void DisableScriptsOnLoadMenuItem_Click(object sender, EventArgs e)
@ -1115,6 +1118,16 @@ namespace BizHawk.Client.EmuHawk
}
}
private void ShareGlobalsMenuItem_Click(object sender, EventArgs e)
{
Settings.ScriptsShareGlobals ^= true;
if (LuaImp.RegisteredFunctions.Any() || LuaImp.ScriptList.Any((lf) => lf.Enabled))
{
MessageBox.Show("All currently running scripts and registered functions will keep their current global scope.\n"
+ "To make them respect the changed setting, restart them.");
}
}
private void RegisterToTextEditorsSubMenu_DropDownOpened(object sender, EventArgs e)
{
// Hide until this one is implemented

View File

@ -42,7 +42,7 @@ namespace BizHawk.Tests.Client.Common.Lua
{
LuaFile luaFile = new LuaFile("", path);
luaLibraries.ScriptList.Add(luaFile);
luaLibraries.EnableLuaFile(luaFile);
luaLibraries.EnableLuaFile(luaFile, false);
if (autoStart)
luaLibraries.ResumeScript(luaFile);