Skip to content

Commit a523387

Browse files
committed
Remove IMainFormForApiInit. Fixes and largely reverts f902c10.
1 parent 2363d5e commit a523387

File tree

9 files changed

+65
-56
lines changed

9 files changed

+65
-56
lines changed

src/BizHawk.Client.Common/IMainFormForApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public interface IMainFormForApi
2222
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>
2323
bool IsSeeking { get; }
2424

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

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

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

6565
/// <remarks>only referenced from <see cref="EmuClientApi"/></remarks>

src/BizHawk.Client.EmuHawk/Api/ApiManager.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,21 @@ static ApiManager()
3434
private static ApiContainer Register(
3535
IEmulatorServiceProvider serviceProvider,
3636
Action<string> logCallback,
37-
IMainFormForApiInit mainForm,
37+
IMainFormForApi mainForm,
3838
DisplayManagerBase displayManager,
3939
InputManager inputManager,
4040
IMovieSession movieSession,
4141
ToolManager toolManager,
4242
Config config,
4343
IEmulator emulator,
44-
IGameInfo game)
44+
IGameInfo game,
45+
IDialogController dialogController)
4546
{
4647
var avail = new Dictionary<Type, object>
4748
{
4849
[typeof(Action<string>)] = logCallback,
4950
[typeof(IMainFormForApi)] = mainForm,
50-
[typeof(IDialogController)] = mainForm.DialogController,
51+
[typeof(IDialogController)] = dialogController,
5152
[typeof(DisplayManagerBase)] = displayManager,
5253
[typeof(InputManager)] = inputManager,
5354
[typeof(IMovieSession)] = movieSession,
@@ -69,34 +70,36 @@ private static ApiContainer Register(
6970

7071
public static IExternalApiProvider Restart(
7172
IEmulatorServiceProvider serviceProvider,
72-
IMainFormForApiInit mainForm,
73+
IMainFormForApi mainForm,
7374
DisplayManagerBase displayManager,
7475
InputManager inputManager,
7576
IMovieSession movieSession,
7677
ToolManager toolManager,
7778
Config config,
7879
IEmulator emulator,
79-
IGameInfo game)
80+
IGameInfo game,
81+
IDialogController dialogController)
8082
{
8183
_container?.Dispose();
82-
_container = Register(serviceProvider, Console.WriteLine, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game);
84+
_container = Register(serviceProvider, Console.WriteLine, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game, dialogController);
8385
return new BasicApiProvider(_container);
8486
}
8587

8688
public static ApiContainer RestartLua(
8789
IEmulatorServiceProvider serviceProvider,
8890
Action<string> logCallback,
89-
IMainFormForApiInit mainForm,
91+
IMainFormForApi mainForm,
9092
DisplayManagerBase displayManager,
9193
InputManager inputManager,
9294
IMovieSession movieSession,
9395
ToolManager toolManager,
9496
Config config,
9597
IEmulator emulator,
96-
IGameInfo game)
98+
IGameInfo game,
99+
IDialogController dialogController)
97100
{
98101
_luaContainer?.Dispose();
99-
_luaContainer = Register(serviceProvider, logCallback, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game);
102+
_luaContainer = Register(serviceProvider, logCallback, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game, dialogController);
100103
return _luaContainer;
101104
}
102105
}

src/BizHawk.Client.EmuHawk/Api/IMainFormForApiInit.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/BizHawk.Client.EmuHawk/IMainFormForTools.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public interface IMainFormForTools : IDialogController
2727
/// <remarks>only referenced from <see cref="BasicBot"/></remarks>
2828
bool InvisibleEmulation { get; set; }
2929

30+
/// <remarks>only referenced from <see cref="LuaConsole"/></remarks>
31+
bool IsTurboing { get; }
32+
3033
/// <remarks>only referenced from <see cref="TAStudio"/></remarks>
3134
bool IsFastForwarding { get; }
3235

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

57+
/// <remarks>only referenced from <see cref="LuaConsole"/></remarks>
58+
/// <param name="forceWindowResize">Override <see cref="Common.Config.ResizeWithFramebuffer"/></param>
59+
void FrameBufferResized(bool forceWindowResize = false);
60+
5461
/// <remarks>only referenced from <see cref="BasicBot"/></remarks>
5562
bool LoadQuickSave(int slot, bool suppressOSD = false);
5663

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
namespace BizHawk.Client.EmuHawk
4646
{
4747
public partial class MainForm : FormBase, IDialogParent,
48-
IMainFormForApiInit, IMainFormForRetroAchievements, IMainFormForTools
48+
IMainFormForApi, IMainFormForRetroAchievements, IMainFormForTools
4949
{
5050
private const string FMT_STR_DUMP_STATUS_MENUITEM_LABEL = "Dump Status Report{0}...";
5151

@@ -1227,7 +1227,7 @@ private set
12271227

12281228
internal readonly ExternalToolManager ExtToolManager;
12291229

1230-
public ToolManager Tools { get; }
1230+
private readonly ToolManager Tools;
12311231

12321232
private IControlMainform ToolControllingSavestates => Tools.FirstOrNull<IControlMainform>(tool => tool.WantsToControlSavestates);
12331233
private IControlMainform ToolControllingRewind => Tools.FirstOrNull<IControlMainform>(tool => tool.WantsToControlRewind);

src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public sealed class FormsLuaLibrary : LuaLibraryBase
2727
public FormsLuaLibrary(ILuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
2828
: base(luaLibsImpl, apiContainer, logOutputCallback) {}
2929

30-
public IDialogParent MainForm { get; set; }
30+
public IDialogParent OwnerForm { get; set; }
3131

3232
public override string Name => "forms";
3333

@@ -284,6 +284,9 @@ public long NewForm(
284284
string title = null,
285285
LuaFunction onClose = null)
286286
{
287+
if (OwnerForm is not IWin32Window ownerForm)
288+
throw new Exception("IDialogParent must implement IWin32Window");
289+
287290
var form = new LuaWinform(CurrentFile, WindowClosed);
288291
_luaForms.Add(form);
289292
if (width.HasValue && height.HasValue)
@@ -295,8 +298,7 @@ public long NewForm(
295298
form.MaximizeBox = false;
296299
form.FormBorderStyle = FormBorderStyle.FixedDialog;
297300
form.Icon = SystemIcons.Application;
298-
form.Owner = (Form) MainForm;
299-
form.Show();
301+
form.Show(ownerForm);
300302

301303
form.FormClosed += (o, e) =>
302304
{
@@ -325,7 +327,7 @@ public string OpenFile(
325327
string filter = null)
326328
{
327329
if (initialDirectory is null && fileName is not null) initialDirectory = Path.GetDirectoryName(fileName);
328-
var result = MainForm.ShowFileOpenDialog(
330+
var result = OwnerForm.ShowFileOpenDialog(
329331
filterStr: filter ?? FilesystemFilter.AllFilesEntry,
330332
initDir: initialDirectory ?? PathEntries.LuaAbsolutePath(),
331333
initFileName: fileName);

src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static Icon ToolIcon
5050

5151
public ToolDialogSettings.ColumnList Columns { get; set; }
5252

53-
internal new IMainFormForApiInit MainForm { get; set; }
53+
internal IMainFormForApi MainFormForApi { get; set; }
5454

5555
public class LuaConsoleSettings
5656
{
@@ -190,13 +190,26 @@ public override void Restart()
190190
{
191191
List<LuaFile> runningScripts = new();
192192

193+
ApiContainer apiContainer = ApiManager.RestartLua(
194+
Emulator.ServiceProvider,
195+
WriteToOutputWindow,
196+
MainFormForApi,
197+
DisplayManager,
198+
InputManager,
199+
MovieSession,
200+
Tools,
201+
Config,
202+
Emulator,
203+
Game,
204+
DialogController);
205+
193206
// Things we need to do with the existing LuaImp before we can make a new one
194207
if (LuaImp is not null)
195208
{
196209
if (LuaImp.IsRebootingCore)
197210
{
198211
// Even if the lua console is self-rebooting from client.reboot_core() we still want to re-inject dependencies
199-
LuaImp.Restart(Emulator.ServiceProvider, Config, Emulator, Game);
212+
LuaImp.Restart(Emulator.ServiceProvider, Config, apiContainer);
200213
return;
201214
}
202215

@@ -216,12 +229,11 @@ public override void Restart()
216229
newScripts,
217230
registeredFuncList,
218231
Emulator.ServiceProvider,
219-
MainForm,
220-
DisplayManager,
221-
InputManager,
232+
MainFormForApi,
222233
Config,
223-
Emulator,
224-
Game);
234+
Tools,
235+
this,
236+
apiContainer);
225237

226238
InputBox.AutoCompleteCustomSource.Clear();
227239
InputBox.AutoCompleteCustomSource.AddRange(LuaImp.Docs.Where(static f => f.SuggestInREPL)

src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ public LuaLibraries(
2323
LuaFileList scriptList,
2424
LuaFunctionList registeredFuncList,
2525
IEmulatorServiceProvider serviceProvider,
26-
IMainFormForApiInit mainForm,
27-
DisplayManagerBase displayManager,
28-
InputManager inputManager,
26+
IMainFormForApi mainForm,
2927
Config config,
30-
IEmulator emulator,
31-
IGameInfo game)
28+
ToolManager toolManager,
29+
IDialogParent dialogParent,
30+
ApiContainer apiContainer)
3231
{
3332
if (!IsAvailable)
3433
{
@@ -57,15 +56,13 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
5756
}
5857

5958
_th = new NLuaTableHelper(_lua, LogToLuaConsole);
60-
_displayManager = displayManager;
61-
_inputManager = inputManager;
6259
_mainForm = mainForm;
6360
LuaWait = new AutoResetEvent(false);
6461
PathEntries = config.PathEntries;
6562
RegisteredFunctions = registeredFuncList;
6663
ScriptList = scriptList;
6764
Docs.Clear();
68-
_apiContainer = ApiManager.RestartLua(serviceProvider, LogToLuaConsole, _mainForm, _displayManager, _inputManager, _mainForm.MovieSession, _mainForm.Tools, config, emulator, game);
65+
_apiContainer = apiContainer;
6966

7067
// Register lua libraries
7168
foreach (var lib in ReflectionCache_Biz_Cli_Com.Types.Concat(ReflectionCache.Types)
@@ -94,7 +91,7 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
9491
else if (instance is ConsoleLuaLibrary consoleLib)
9592
{
9693
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
97-
consoleLib.Tools = _mainForm.Tools;
94+
consoleLib.Tools = toolManager;
9895
_logToLuaConsoleCallback = consoleLib.Log;
9996
}
10097
else if (instance is DoomLuaLibrary doomLib)
@@ -108,7 +105,8 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
108105
}
109106
else if (instance is FormsLuaLibrary formsLib)
110107
{
111-
formsLib.MainForm = _mainForm;
108+
109+
formsLib.OwnerForm = dialogParent;
112110
}
113111
else if (instance is GuiLuaLibrary guiLib)
114112
{
@@ -122,7 +120,7 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
122120
}
123121
else if (instance is TAStudioLuaLibrary tastudioLib)
124122
{
125-
tastudioLib.Tools = _mainForm.Tools;
123+
tastudioLib.Tools = toolManager;
126124
}
127125

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

160158
private ApiContainer _apiContainer;
161159

162-
private readonly DisplayManagerBase _displayManager;
163-
164160
private GuiApi GuiAPI => (GuiApi)_apiContainer.Gui;
165161

166-
private readonly InputManager _inputManager;
167-
168-
private readonly IMainFormForApiInit _mainForm;
162+
private readonly IMainFormForApi _mainForm;
169163

170164
private Lua _lua = new();
171165
private LuaThread _currThread;
@@ -201,10 +195,9 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
201195
public void Restart(
202196
IEmulatorServiceProvider newServiceProvider,
203197
Config config,
204-
IEmulator emulator,
205-
IGameInfo game)
198+
ApiContainer apiContainer)
206199
{
207-
_apiContainer = ApiManager.RestartLua(newServiceProvider, LogToLuaConsole, _mainForm, _displayManager, _inputManager, _mainForm.MovieSession, _mainForm.Tools, config, emulator, game);
200+
_apiContainer = apiContainer;
208201
PathEntries = config.PathEntries;
209202
foreach (var lib in Libraries.Values)
210203
{

src/BizHawk.Client.EmuHawk/tools/ToolManager.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ private IExternalApiProvider GetOrInitApiProvider()
6868
this,
6969
_config,
7070
_emulator,
71-
_game);
71+
_game,
72+
_owner.DialogController);
7273

7374
/// <summary>
7475
/// 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
@@ -96,7 +97,9 @@ private void SetBaseProperties(IToolForm form)
9697
f.Config = _config;
9798
if (form is not ToolFormBase tool) return;
9899
tool.SetToolFormBaseProps(_displayManager, _inputManager, _owner, _movieSession, this, _game);
99-
if (form is LuaConsole luaConsole) luaConsole.MainForm = _owner; // could go via ServiceProvider but nah
100+
101+
// Lua is a special case, being the only tool that currently uses this.
102+
if (form is LuaConsole luaConsole) luaConsole.MainFormForApi = _owner;
100103
}
101104

102105
/// <summary>

0 commit comments

Comments
 (0)