Skip to content

Commit c99881d

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

File tree

9 files changed

+72
-58
lines changed

9 files changed

+72
-58
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IMainFormForTools : IDialogController
1212
/// <remarks>referenced by 3 or more tools</remarks>
1313
string CurrentlyOpenRom { get; }
1414

15-
/// <remarks>referenced from <see cref="HexEditor"/> and RetroAchievements</remarks>
15+
/// <remarks>only referenced from <see cref="HexEditor"/></remarks>
1616
LoadRomArgs CurrentlyOpenRomArgs { get; }
1717

1818
/// <remarks>only referenced from <see cref="TAStudio"/></remarks>
@@ -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,10 +54,14 @@ 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

57-
/// <remarks>referenced from <see cref="MultiDiskBundler"/> and RetroAchievements</remarks>
64+
/// <remarks>only referenced from <see cref="MultiDiskBundler"/></remarks>
5865
bool LoadRom(string path, LoadRomArgs args);
5966

6067
/// <remarks>only referenced from <see cref="BookmarksBranchesBox"/></remarks>
@@ -96,7 +103,7 @@ public interface IMainFormForTools : IDialogController
96103
/// <remarks>only referenced from <see cref="BookmarksBranchesBox"/></remarks>
97104
void UpdateStatusSlots();
98105

99-
/// <remarks>referenced from <see cref="TAStudio"/> and RetroAchievements</remarks>
106+
/// <remarks>only referenced from <see cref="TAStudio"/></remarks>
100107
void UpdateWindowTitle();
101108
}
102109
}

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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ 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 DialogParent { get; set; }
31+
32+
public Form OwnerForm { get; set; }
3133

3234
public override string Name => "forms";
3335

@@ -295,7 +297,7 @@ public long NewForm(
295297
form.MaximizeBox = false;
296298
form.FormBorderStyle = FormBorderStyle.FixedDialog;
297299
form.Icon = SystemIcons.Application;
298-
form.Owner = (Form) MainForm;
300+
form.Owner = OwnerForm;
299301
form.Show();
300302

301303
form.FormClosed += (o, e) =>
@@ -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 = DialogParent.ShowFileOpenDialog(
329331
filterStr: filter ?? FilesystemFilter.AllFilesEntry,
330332
initDir: initialDirectory ?? PathEntries.LuaAbsolutePath(),
331333
initFileName: fileName);

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

Lines changed: 18 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,10 @@ 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+
apiContainer);
225236

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

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ 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+
ApiContainer apiContainer)
3230
{
3331
if (!IsAvailable)
3432
{
@@ -57,15 +55,13 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
5755
}
5856

5957
_th = new NLuaTableHelper(_lua, LogToLuaConsole);
60-
_displayManager = displayManager;
61-
_inputManager = inputManager;
6258
_mainForm = mainForm;
6359
LuaWait = new AutoResetEvent(false);
6460
PathEntries = config.PathEntries;
6561
RegisteredFunctions = registeredFuncList;
6662
ScriptList = scriptList;
6763
Docs.Clear();
68-
_apiContainer = ApiManager.RestartLua(serviceProvider, LogToLuaConsole, _mainForm, _displayManager, _inputManager, _mainForm.MovieSession, _mainForm.Tools, config, emulator, game);
64+
_apiContainer = apiContainer;
6965

7066
// Register lua libraries
7167
foreach (var lib in ReflectionCache_Biz_Cli_Com.Types.Concat(ReflectionCache.Types)
@@ -94,7 +90,7 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
9490
else if (instance is ConsoleLuaLibrary consoleLib)
9591
{
9692
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;
93+
consoleLib.Tools = toolManager;
9894
_logToLuaConsoleCallback = consoleLib.Log;
9995
}
10096
else if (instance is DoomLuaLibrary doomLib)
@@ -108,7 +104,15 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
108104
}
109105
else if (instance is FormsLuaLibrary formsLib)
110106
{
111-
formsLib.MainForm = _mainForm;
107+
// We want to not give tools/apis explicit references to Windows Forms.
108+
// But this Lua API actually needs one, because it creates Windows Forms.
109+
// If we ever have another frontend framework, this library should not be used. So we check its type.
110+
if (mainForm is MainForm mainAsForm)
111+
{
112+
formsLib.DialogParent = mainAsForm;
113+
formsLib.OwnerForm = mainAsForm;
114+
}
115+
else continue;
112116
}
113117
else if (instance is GuiLuaLibrary guiLib)
114118
{
@@ -122,7 +126,7 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
122126
}
123127
else if (instance is TAStudioLuaLibrary tastudioLib)
124128
{
125-
tastudioLib.Tools = _mainForm.Tools;
129+
tastudioLib.Tools = toolManager;
126130
}
127131

128132
EnumerateLuaFunctions(instance.Name, lib, instance);
@@ -159,13 +163,9 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
159163

160164
private ApiContainer _apiContainer;
161165

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

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

170170
private Lua _lua = new();
171171
private LuaThread _currThread;
@@ -201,10 +201,9 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
201201
public void Restart(
202202
IEmulatorServiceProvider newServiceProvider,
203203
Config config,
204-
IEmulator emulator,
205-
IGameInfo game)
204+
ApiContainer apiContainer)
206205
{
207-
_apiContainer = ApiManager.RestartLua(newServiceProvider, LogToLuaConsole, _mainForm, _displayManager, _inputManager, _mainForm.MovieSession, _mainForm.Tools, config, emulator, game);
206+
_apiContainer = apiContainer;
208207
PathEntries = config.PathEntries;
209208
foreach (var lib in Libraries.Values)
210209
{

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)