Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/BizHawk.Client.Common/IMainFormForApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface IMainFormForApi
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
bool IsSeeking { get; }

/// <remarks>referenced from <see cref="EmuClientApi"/> and <c>LuaConsole</c></remarks>
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
bool IsTurboing { get; }

/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
Expand Down Expand Up @@ -59,7 +59,7 @@ public interface IMainFormForApi
void FrameAdvance(bool discardApiHawkSurfaces = true);

/// <param name="forceWindowResize">Override <see cref="Common.Config.ResizeWithFramebuffer"/></param>
/// <remarks>referenced from <see cref="EmuClientApi"/> and <c>LuaConsole</c></remarks>
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
void FrameBufferResized(bool forceWindowResize = false);

/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
Expand Down
21 changes: 12 additions & 9 deletions src/BizHawk.Client.EmuHawk/Api/ApiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ static ApiManager()
private static ApiContainer Register(
IEmulatorServiceProvider serviceProvider,
Action<string> logCallback,
IMainFormForApiInit mainForm,
IMainFormForApi mainForm,
DisplayManagerBase displayManager,
InputManager inputManager,
IMovieSession movieSession,
ToolManager toolManager,
Config config,
IEmulator emulator,
IGameInfo game)
IGameInfo game,
IDialogController dialogController)
{
var avail = new Dictionary<Type, object>
{
[typeof(Action<string>)] = logCallback,
[typeof(IMainFormForApi)] = mainForm,
[typeof(IDialogController)] = mainForm.DialogController,
[typeof(IDialogController)] = dialogController,
[typeof(DisplayManagerBase)] = displayManager,
[typeof(InputManager)] = inputManager,
[typeof(IMovieSession)] = movieSession,
Expand All @@ -69,34 +70,36 @@ private static ApiContainer Register(

public static IExternalApiProvider Restart(
IEmulatorServiceProvider serviceProvider,
IMainFormForApiInit mainForm,
IMainFormForApi mainForm,
DisplayManagerBase displayManager,
InputManager inputManager,
IMovieSession movieSession,
ToolManager toolManager,
Config config,
IEmulator emulator,
IGameInfo game)
IGameInfo game,
IDialogController dialogController)
{
_container?.Dispose();
_container = Register(serviceProvider, Console.WriteLine, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game);
_container = Register(serviceProvider, Console.WriteLine, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game, dialogController);
return new BasicApiProvider(_container);
}

public static ApiContainer RestartLua(
IEmulatorServiceProvider serviceProvider,
Action<string> logCallback,
IMainFormForApiInit mainForm,
IMainFormForApi mainForm,
DisplayManagerBase displayManager,
InputManager inputManager,
IMovieSession movieSession,
ToolManager toolManager,
Config config,
IEmulator emulator,
IGameInfo game)
IGameInfo game,
IDialogController dialogController)
{
_luaContainer?.Dispose();
_luaContainer = Register(serviceProvider, logCallback, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game);
_luaContainer = Register(serviceProvider, logCallback, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game, dialogController);
return _luaContainer;
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/BizHawk.Client.EmuHawk/Api/IMainFormForApiInit.cs

This file was deleted.

7 changes: 7 additions & 0 deletions src/BizHawk.Client.EmuHawk/IMainFormForTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public interface IMainFormForTools : IDialogController
/// <remarks>only referenced from <see cref="BasicBot"/></remarks>
bool InvisibleEmulation { get; set; }

/// <remarks>only referenced from <see cref="LuaConsole"/></remarks>
bool IsTurboing { get; }

/// <remarks>only referenced from <see cref="TAStudio"/></remarks>
bool IsFastForwarding { get; }

Expand All @@ -51,6 +54,10 @@ public interface IMainFormForTools : IDialogController
/// <remarks>only referenced from <see cref="TAStudio"/></remarks>
void FrameAdvance(bool discardApiHawkSurfaces = true);

/// <remarks>only referenced from <see cref="LuaConsole"/></remarks>
/// <param name="forceWindowResize">Override <see cref="Common.Config.ResizeWithFramebuffer"/></param>
void FrameBufferResized(bool forceWindowResize = false);

/// <remarks>only referenced from <see cref="BasicBot"/></remarks>
bool LoadQuickSave(int slot, bool suppressOSD = false);

Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.Client.EmuHawk/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
namespace BizHawk.Client.EmuHawk
{
public partial class MainForm : FormBase, IDialogParent,
IMainFormForApiInit, IMainFormForRetroAchievements, IMainFormForTools
IMainFormForApi, IMainFormForRetroAchievements, IMainFormForTools
{
private const string FMT_STR_DUMP_STATUS_MENUITEM_LABEL = "Dump Status Report{0}...";

Expand Down Expand Up @@ -1227,7 +1227,7 @@ private set

internal readonly ExternalToolManager ExtToolManager;

public ToolManager Tools { get; }
private readonly ToolManager Tools;

private IControlMainform ToolControllingSavestates => Tools.FirstOrNull<IControlMainform>(tool => tool.WantsToControlSavestates);
private IControlMainform ToolControllingRewind => Tools.FirstOrNull<IControlMainform>(tool => tool.WantsToControlRewind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed class FormsLuaLibrary : LuaLibraryBase
public FormsLuaLibrary(ILuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
: base(luaLibsImpl, apiContainer, logOutputCallback) {}

public IDialogParent MainForm { get; set; }
public IDialogParent OwnerForm { get; set; }

public override string Name => "forms";

Expand Down Expand Up @@ -284,6 +284,9 @@ public long NewForm(
string title = null,
LuaFunction onClose = null)
{
if (OwnerForm is not IWin32Window ownerForm)
throw new Exception("IDialogParent must implement IWin32Window");

var form = new LuaWinform(CurrentFile, WindowClosed);
_luaForms.Add(form);
if (width.HasValue && height.HasValue)
Expand All @@ -295,8 +298,7 @@ public long NewForm(
form.MaximizeBox = false;
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.Icon = SystemIcons.Application;
form.Owner = (Form) MainForm;
form.Show();
form.Show(ownerForm);

form.FormClosed += (o, e) =>
{
Expand Down Expand Up @@ -325,7 +327,7 @@ public string OpenFile(
string filter = null)
{
if (initialDirectory is null && fileName is not null) initialDirectory = Path.GetDirectoryName(fileName);
var result = MainForm.ShowFileOpenDialog(
var result = OwnerForm.ShowFileOpenDialog(
filterStr: filter ?? FilesystemFilter.AllFilesEntry,
initDir: initialDirectory ?? PathEntries.LuaAbsolutePath(),
initFileName: fileName);
Expand Down
26 changes: 19 additions & 7 deletions src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static Icon ToolIcon

public ToolDialogSettings.ColumnList Columns { get; set; }

internal new IMainFormForApiInit MainForm { get; set; }
internal IMainFormForApi MainFormForApi { get; set; }

public class LuaConsoleSettings
{
Expand Down Expand Up @@ -190,13 +190,26 @@ public override void Restart()
{
List<LuaFile> runningScripts = new();

ApiContainer apiContainer = ApiManager.RestartLua(
Emulator.ServiceProvider,
WriteToOutputWindow,
MainFormForApi,
DisplayManager,
InputManager,
MovieSession,
Tools,
Config,
Emulator,
Game,
DialogController);

// Things we need to do with the existing LuaImp before we can make a new one
if (LuaImp is not null)
{
if (LuaImp.IsRebootingCore)
{
// Even if the lua console is self-rebooting from client.reboot_core() we still want to re-inject dependencies
LuaImp.Restart(Emulator.ServiceProvider, Config, Emulator, Game);
LuaImp.Restart(Emulator.ServiceProvider, Config, apiContainer);
return;
}

Expand All @@ -216,12 +229,11 @@ public override void Restart()
newScripts,
registeredFuncList,
Emulator.ServiceProvider,
MainForm,
DisplayManager,
InputManager,
MainFormForApi,
Config,
Emulator,
Game);
Tools,
this,
apiContainer);

InputBox.AutoCompleteCustomSource.Clear();
InputBox.AutoCompleteCustomSource.AddRange(LuaImp.Docs.Where(static f => f.SuggestInREPL)
Expand Down
31 changes: 12 additions & 19 deletions src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ public LuaLibraries(
LuaFileList scriptList,
LuaFunctionList registeredFuncList,
IEmulatorServiceProvider serviceProvider,
IMainFormForApiInit mainForm,
DisplayManagerBase displayManager,
InputManager inputManager,
IMainFormForApi mainForm,
Config config,
IEmulator emulator,
IGameInfo game)
ToolManager toolManager,
IDialogParent dialogParent,
ApiContainer apiContainer)
{
if (!IsAvailable)
{
Expand Down Expand Up @@ -57,15 +56,13 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
}

_th = new NLuaTableHelper(_lua, LogToLuaConsole);
_displayManager = displayManager;
_inputManager = inputManager;
_mainForm = mainForm;
LuaWait = new AutoResetEvent(false);
PathEntries = config.PathEntries;
RegisteredFunctions = registeredFuncList;
ScriptList = scriptList;
Docs.Clear();
_apiContainer = ApiManager.RestartLua(serviceProvider, LogToLuaConsole, _mainForm, _displayManager, _inputManager, _mainForm.MovieSession, _mainForm.Tools, config, emulator, game);
_apiContainer = apiContainer;

// Register lua libraries
foreach (var lib in ReflectionCache_Biz_Cli_Com.Types.Concat(ReflectionCache.Types)
Expand Down Expand Up @@ -94,7 +91,7 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
else if (instance is ConsoleLuaLibrary consoleLib)
{
consoleLib.AllAPINames = new(() => string.Join("\n", Docs.Select(static lf => lf.Name)) + "\n"); // Docs may not be fully populated now, depending on order of ReflectionCache.Types, but definitely will be when this is read
consoleLib.Tools = _mainForm.Tools;
consoleLib.Tools = toolManager;
_logToLuaConsoleCallback = consoleLib.Log;
}
else if (instance is DoomLuaLibrary doomLib)
Expand All @@ -108,7 +105,8 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
}
else if (instance is FormsLuaLibrary formsLib)
{
formsLib.MainForm = _mainForm;

formsLib.OwnerForm = dialogParent;
}
else if (instance is GuiLuaLibrary guiLib)
{
Expand All @@ -122,7 +120,7 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
}
else if (instance is TAStudioLuaLibrary tastudioLib)
{
tastudioLib.Tools = _mainForm.Tools;
tastudioLib.Tools = toolManager;
}

EnumerateLuaFunctions(instance.Name, lib, instance);
Expand Down Expand Up @@ -159,13 +157,9 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)

private ApiContainer _apiContainer;

private readonly DisplayManagerBase _displayManager;

private GuiApi GuiAPI => (GuiApi)_apiContainer.Gui;

private readonly InputManager _inputManager;

private readonly IMainFormForApiInit _mainForm;
private readonly IMainFormForApi _mainForm;

private Lua _lua = new();
private LuaThread _currThread;
Expand Down Expand Up @@ -201,10 +195,9 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
public void Restart(
IEmulatorServiceProvider newServiceProvider,
Config config,
IEmulator emulator,
IGameInfo game)
ApiContainer apiContainer)
{
_apiContainer = ApiManager.RestartLua(newServiceProvider, LogToLuaConsole, _mainForm, _displayManager, _inputManager, _mainForm.MovieSession, _mainForm.Tools, config, emulator, game);
_apiContainer = apiContainer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this is an interesting idea. I'm not sure if it's semantically sound, but the call order seems the same so it should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced that the restart logic is correct, but yeah this should be equivalent to what is already there. And I see no reason why LuaLibraries would need to be responsible for constructing the API instances or ApiContainer.

Btw this is something my other PR does too.

PathEntries = config.PathEntries;
foreach (var lib in Libraries.Values)
{
Expand Down
7 changes: 5 additions & 2 deletions src/BizHawk.Client.EmuHawk/tools/ToolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ private IExternalApiProvider GetOrInitApiProvider()
this,
_config,
_emulator,
_game);
_game,
_owner.DialogController);

/// <summary>
/// Loads the tool dialog T (T must implements <see cref="IToolForm"/>) , if it does not exist it will be created, if it is already open, it will be focused
Expand Down Expand Up @@ -96,7 +97,9 @@ private void SetBaseProperties(IToolForm form)
f.Config = _config;
if (form is not ToolFormBase tool) return;
tool.SetToolFormBaseProps(_displayManager, _inputManager, _owner, _movieSession, this, _game);
if (form is LuaConsole luaConsole) luaConsole.MainForm = _owner; // could go via ServiceProvider but nah

// Lua is a special case, being the only tool that currently uses this.
if (form is LuaConsole luaConsole) luaConsole.MainFormForApi = _owner;
}

/// <summary>
Expand Down