From cba206efeca261f3b26d51aaeb87fa634ca6b08c Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 4 Dec 2022 04:06:58 +1000 Subject: [PATCH] Add `IUserDataApi.Keys`/`userdata.get_keys` --- src/BizHawk.Client.Common/Api/Classes/UserDataApi.cs | 10 ++++++++++ .../Api/Interfaces/IUserDataApi.cs | 10 +++++++++- .../lua/CommonLibs/UserDataLuaLibrary.cs | 9 +++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/BizHawk.Client.Common/Api/Classes/UserDataApi.cs b/src/BizHawk.Client.Common/Api/Classes/UserDataApi.cs index 794351e479..91e1d0e45f 100644 --- a/src/BizHawk.Client.Common/Api/Classes/UserDataApi.cs +++ b/src/BizHawk.Client.Common/Api/Classes/UserDataApi.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; namespace BizHawk.Client.Common { @@ -6,6 +8,14 @@ namespace BizHawk.Client.Common { private readonly IMovieSession _movieSession; +#if NET6_0 + public IReadOnlySet Keys + => throw new NotImplementedException(); +#else + public IReadOnlyCollection Keys + => _movieSession.UserBag.Keys.ToList(); +#endif + public UserDataApi(IMovieSession movieSession) => _movieSession = movieSession; /// type of cannot be used in userdata diff --git a/src/BizHawk.Client.Common/Api/Interfaces/IUserDataApi.cs b/src/BizHawk.Client.Common/Api/Interfaces/IUserDataApi.cs index 840c894078..497aff23b2 100644 --- a/src/BizHawk.Client.Common/Api/Interfaces/IUserDataApi.cs +++ b/src/BizHawk.Client.Common/Api/Interfaces/IUserDataApi.cs @@ -1,7 +1,15 @@ -namespace BizHawk.Client.Common +using System.Collections.Generic; + +namespace BizHawk.Client.Common { public interface IUserDataApi : IExternalApi { +#if NET6_0 + IReadOnlySet Keys { get; } +#else + IReadOnlyCollection Keys { get; } +#endif + void Set(string name, object value); object Get(string key); void Clear(); diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs index 3ae6ba90c8..59b9dd4136 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; using System.ComponentModel; +using NLua; + // ReSharper disable UnusedMember.Global namespace BizHawk.Client.Common { @@ -37,5 +40,11 @@ namespace BizHawk.Client.Common [LuaMethod("containskey", "returns whether or not there is an entry for the given key")] public bool ContainsKey([LuaArbitraryStringParam] string key) => APIs.UserData.ContainsKey(key); + + [LuaMethodExample("console.writeline(#userdata.get_keys());")] + [LuaMethod("get_keys", "returns a list-like table of valid keys")] + [return: LuaArbitraryStringParam] + public LuaTable GetKeys() + => _th.ListToTable((List) APIs.UserData.Keys); //HACK cast will succeed as long as impl. returns Dictionary.Keys.ToList() as IROC } }