Add `IUserDataApi.Keys`/`userdata.get_keys`

This commit is contained in:
YoshiRulz 2022-12-04 04:06:58 +10:00
parent c2d5a9c931
commit cba206efec
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 28 additions and 1 deletions

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
@ -6,6 +8,14 @@ namespace BizHawk.Client.Common
{ {
private readonly IMovieSession _movieSession; private readonly IMovieSession _movieSession;
#if NET6_0
public IReadOnlySet<string> Keys
=> throw new NotImplementedException();
#else
public IReadOnlyCollection<string> Keys
=> _movieSession.UserBag.Keys.ToList();
#endif
public UserDataApi(IMovieSession movieSession) => _movieSession = movieSession; public UserDataApi(IMovieSession movieSession) => _movieSession = movieSession;
/// <exception cref="InvalidOperationException">type of <paramref name="value"/> cannot be used in userdata</exception> /// <exception cref="InvalidOperationException">type of <paramref name="value"/> cannot be used in userdata</exception>

View File

@ -1,7 +1,15 @@
namespace BizHawk.Client.Common using System.Collections.Generic;
namespace BizHawk.Client.Common
{ {
public interface IUserDataApi : IExternalApi public interface IUserDataApi : IExternalApi
{ {
#if NET6_0
IReadOnlySet<string> Keys { get; }
#else
IReadOnlyCollection<string> Keys { get; }
#endif
void Set(string name, object value); void Set(string name, object value);
object Get(string key); object Get(string key);
void Clear(); void Clear();

View File

@ -1,6 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using NLua;
// ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common 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")] [LuaMethod("containskey", "returns whether or not there is an entry for the given key")]
public bool ContainsKey([LuaArbitraryStringParam] string key) public bool ContainsKey([LuaArbitraryStringParam] string key)
=> APIs.UserData.ContainsKey(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<string>) APIs.UserData.Keys); //HACK cast will succeed as long as impl. returns Dictionary<K, V>.Keys.ToList() as IROC<K>
} }
} }