Skip to content

Commit cba206e

Browse files
committed
Add IUserDataApi.Keys/userdata.get_keys
1 parent c2d5a9c commit cba206e

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/BizHawk.Client.Common/Api/Classes/UserDataApi.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24

35
namespace BizHawk.Client.Common
46
{
57
public sealed class UserDataApi : IUserDataApi
68
{
79
private readonly IMovieSession _movieSession;
810

11+
#if NET6_0
12+
public IReadOnlySet<string> Keys
13+
=> throw new NotImplementedException();
14+
#else
15+
public IReadOnlyCollection<string> Keys
16+
=> _movieSession.UserBag.Keys.ToList();
17+
#endif
18+
919
public UserDataApi(IMovieSession movieSession) => _movieSession = movieSession;
1020

1121
/// <exception cref="InvalidOperationException">type of <paramref name="value"/> cannot be used in userdata</exception>

src/BizHawk.Client.Common/Api/Interfaces/IUserDataApi.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
namespace BizHawk.Client.Common
1+
using System.Collections.Generic;
2+
3+
namespace BizHawk.Client.Common
24
{
35
public interface IUserDataApi : IExternalApi
46
{
7+
#if NET6_0
8+
IReadOnlySet<string> Keys { get; }
9+
#else
10+
IReadOnlyCollection<string> Keys { get; }
11+
#endif
12+
513
void Set(string name, object value);
614
object Get(string key);
715
void Clear();

src/BizHawk.Client.Common/lua/CommonLibs/UserDataLuaLibrary.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34

5+
using NLua;
6+
47
// ReSharper disable UnusedMember.Global
58
namespace BizHawk.Client.Common
69
{
@@ -37,5 +40,11 @@ public bool Remove([LuaArbitraryStringParam] string key)
3740
[LuaMethod("containskey", "returns whether or not there is an entry for the given key")]
3841
public bool ContainsKey([LuaArbitraryStringParam] string key)
3942
=> APIs.UserData.ContainsKey(key);
43+
44+
[LuaMethodExample("console.writeline(#userdata.get_keys());")]
45+
[LuaMethod("get_keys", "returns a list-like table of valid keys")]
46+
[return: LuaArbitraryStringParam]
47+
public LuaTable GetKeys()
48+
=> _th.ListToTable((List<string>) APIs.UserData.Keys); //HACK cast will succeed as long as impl. returns Dictionary<K, V>.Keys.ToList() as IROC<K>
4049
}
4150
}

0 commit comments

Comments
 (0)