From 73af92b57939d09fd83fee466a6c8f7c2d5973ea Mon Sep 17 00:00:00 2001
From: YoshiRulz <OSSYoshiRulz@gmail.com>
Date: Sat, 23 Oct 2021 04:38:51 +1000
Subject: [PATCH] Refactor `IGameInfoApi` (expose GameInfo as its read-only
 interface)

also enabled NRTs
---
 .../Api/Classes/GameInfoApi.cs                | 29 +++++++++----------
 .../Api/Interfaces/IGameInfoApi.cs            | 16 +++++-----
 .../lua/CommonLibs/GameInfoLuaLibrary.cs      | 24 ++++++++++-----
 3 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/src/BizHawk.Client.Common/Api/Classes/GameInfoApi.cs b/src/BizHawk.Client.Common/Api/Classes/GameInfoApi.cs
index a74d9a527a..da982efee7 100644
--- a/src/BizHawk.Client.Common/Api/Classes/GameInfoApi.cs
+++ b/src/BizHawk.Client.Common/Api/Classes/GameInfoApi.cs
@@ -1,4 +1,6 @@
-using System.Collections.Generic;
+#nullable enable
+
+using System.Collections.Generic;
 
 using BizHawk.Emulation.Common;
 
@@ -7,27 +9,22 @@ namespace BizHawk.Client.Common
 	public sealed class GameInfoApi : IGameInfoApi
 	{
 		[OptionalService]
-		private IBoardInfo BoardInfo { get; set; }
+		public IBoardInfo? _boardInfo { get; set; }
 
-		private readonly IGameInfo _game;
+		private readonly IGameInfo? _game;
 
-		public GameInfoApi(IGameInfo game) => _game = game;
+		public GameInfoApi(IGameInfo? game)
+			=> _game = game;
 
-		public string GetRomName() => _game?.Name ?? "";
+		public string GetBoardType()
+			=> _boardInfo?.BoardName ?? string.Empty;
 
-		public string GetRomHash() => _game?.Hash ?? "";
+		public IGameInfo? GetGameInfo()
+			=> _game;
 
-		public bool InDatabase() => _game?.NotInDatabase == false;
-
-		public string GetStatus() => _game?.Status.ToString();
-
-		public bool IsStatusBad() => _game?.IsRomStatusBad() != false;
-
-		public string GetBoardType() => BoardInfo?.BoardName ?? "";
-
-		public IReadOnlyDictionary<string, string> GetOptions()
+		public IReadOnlyDictionary<string, string?> GetOptions()
 		{
-			var options = new Dictionary<string, string>();
+			var options = new Dictionary<string, string?>();
 			if (_game == null) return options;
 			foreach (var option in ((GameInfo) _game).GetOptions()) options[option.Key] = option.Value;
 			return options;
diff --git a/src/BizHawk.Client.Common/Api/Interfaces/IGameInfoApi.cs b/src/BizHawk.Client.Common/Api/Interfaces/IGameInfoApi.cs
index bbc7e92d96..59ed328ffa 100644
--- a/src/BizHawk.Client.Common/Api/Interfaces/IGameInfoApi.cs
+++ b/src/BizHawk.Client.Common/Api/Interfaces/IGameInfoApi.cs
@@ -1,15 +1,17 @@
-using System.Collections.Generic;
+#nullable enable
+
+using System.Collections.Generic;
+
+using BizHawk.Emulation.Common;
 
 namespace BizHawk.Client.Common
 {
 	public interface IGameInfoApi : IExternalApi
 	{
-		string GetRomName();
-		string GetRomHash();
-		bool InDatabase();
-		string GetStatus();
-		bool IsStatusBad();
 		string GetBoardType();
-		IReadOnlyDictionary<string, string> GetOptions();
+
+		IGameInfo? GetGameInfo();
+
+		IReadOnlyDictionary<string, string?> GetOptions();
 	}
 }
diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/GameInfoLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/GameInfoLuaLibrary.cs
index b02a0110d4..46db66bb30 100644
--- a/src/BizHawk.Client.Common/lua/CommonLibs/GameInfoLuaLibrary.cs
+++ b/src/BizHawk.Client.Common/lua/CommonLibs/GameInfoLuaLibrary.cs
@@ -1,4 +1,7 @@
 using System;
+
+using BizHawk.Emulation.Common;
+
 using NLua;
 
 // ReSharper disable UnusedMember.Global
@@ -14,30 +17,37 @@ namespace BizHawk.Client.Common
 
 		[LuaMethodExample("local stgamget = gameinfo.getromname( );")]
 		[LuaMethod("getromname", "returns the name of the currently loaded rom, if a rom is loaded")]
-		public string GetRomName() => APIs.GameInfo.GetRomName();
+		public string GetRomName()
+			=> APIs.GameInfo.GetGameInfo()?.Name ?? string.Empty;
 
 		[LuaMethodExample("local stgamget = gameinfo.getromhash( );")]
 		[LuaMethod("getromhash", "returns the hash of the currently loaded rom, if a rom is loaded")]
-		public string GetRomHash() => APIs.GameInfo.GetRomHash();
+		public string GetRomHash()
+			=> APIs.GameInfo.GetGameInfo()?.Hash ?? string.Empty;
 
 		[LuaMethodExample("if ( gameinfo.indatabase( ) ) then\r\n\tconsole.log( \"returns whether or not the currently loaded rom is in the game database\" );\r\nend;")]
 		[LuaMethod("indatabase", "returns whether or not the currently loaded rom is in the game database")]
-		public bool InDatabase() => APIs.GameInfo.InDatabase();
+		public bool InDatabase()
+			=> APIs.GameInfo.GetGameInfo()?.NotInDatabase is false;
 
 		[LuaMethodExample("local stgamget = gameinfo.getstatus( );")]
 		[LuaMethod("getstatus", "returns the game database status of the currently loaded rom. Statuses are for example: GoodDump, BadDump, Hack, Unknown, NotInDatabase")]
-		public string GetStatus() => APIs.GameInfo.GetStatus();
+		public string GetStatus()
+			=> (APIs.GameInfo.GetGameInfo()?.Status)?.ToString();
 
 		[LuaMethodExample("if ( gameinfo.isstatusbad( ) ) then\r\n\tconsole.log( \"returns the currently loaded rom's game database status is considered 'bad'\" );\r\nend;")]
 		[LuaMethod("isstatusbad", "returns the currently loaded rom's game database status is considered 'bad'")]
-		public bool IsStatusBad() => APIs.GameInfo.IsStatusBad();
+		public bool IsStatusBad()
+			=> APIs.GameInfo.GetGameInfo()?.IsRomStatusBad() is true or null;
 
 		[LuaMethodExample("local stgamget = gameinfo.getboardtype( );")]
 		[LuaMethod("getboardtype", "returns identifying information about the 'mapper' or similar capability used for this game.  empty if no such useful distinction can be drawn")]
-		public string GetBoardType() => APIs.GameInfo.GetBoardType();
+		public string GetBoardType()
+			=> APIs.GameInfo.GetBoardType();
 
 		[LuaMethodExample("local nlgamget = gameinfo.getoptions( );")]
 		[LuaMethod("getoptions", "returns the game options for the currently loaded rom. Options vary per platform")]
-		public LuaTable GetOptions() => _th.DictToTable(APIs.GameInfo.GetOptions());
+		public LuaTable GetOptions()
+			=> _th.DictToTable(APIs.GameInfo.GetOptions());
 	}
 }