Migrate UserDataLibrary to ApiHawk delegation

This commit is contained in:
YoshiRulz 2019-11-16 22:26:46 +10:00
parent 4a84e1a8a9
commit f4a229888f
1 changed files with 6 additions and 32 deletions

View File

@ -7,7 +7,7 @@ using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
[Description("A library for setting and retrieving dynamic data that will be saved and loaded with savestates")] [Description("A library for setting and retrieving dynamic data that will be saved and loaded with savestates")]
public sealed class UserDataLibrary : LuaLibraryBase public sealed class UserDataLibrary : DelegatingLuaLibrary
{ {
public UserDataLibrary(Lua lua) public UserDataLibrary(Lua lua)
: base(lua) { } : base(lua) { }
@ -19,48 +19,22 @@ namespace BizHawk.Client.EmuHawk
[LuaMethodExample("userdata.set(\"Unique key\", \"Current key data\");")] [LuaMethodExample("userdata.set(\"Unique key\", \"Current key data\");")]
[LuaMethod("set", "adds or updates the data with the given key with the given value")] [LuaMethod("set", "adds or updates the data with the given key with the given value")]
public void Set(string name, object value) public void Set(string name, object value) => APIs.UserData.Set(name, value);
{
if (value != null)
{
var t = value.GetType();
if (!t.IsPrimitive && t != typeof(string))
{
throw new InvalidOperationException("Invalid type for userdata");
}
}
Global.UserBag[name] = value;
}
[LuaMethodExample("local obuseget = userdata.get( \"Unique key\" );")] [LuaMethodExample("local obuseget = userdata.get( \"Unique key\" );")]
[LuaMethod("get", "gets the data with the given key, if the key does not exist it will return nil")] [LuaMethod("get", "gets the data with the given key, if the key does not exist it will return nil")]
public object Get(string key) public object Get(string key) => APIs.UserData.Get(key);
{
return Global.UserBag.ContainsKey(key)
? Global.UserBag[key]
: null;
}
[LuaMethodExample("userdata.clear( );")] [LuaMethodExample("userdata.clear( );")]
[LuaMethod("clear", "clears all user data")] [LuaMethod("clear", "clears all user data")]
public void Clear() public void Clear() => APIs.UserData.Clear();
{
Global.UserBag.Clear();
}
[LuaMethodExample("if ( userdata.remove( \"Unique key\" ) ) then\r\n\tconsole.log( \"remove the data with the given key.Returns true if the element is successfully found and removed; otherwise, false.\" );\r\nend;")] [LuaMethodExample("if ( userdata.remove( \"Unique key\" ) ) then\r\n\tconsole.log( \"remove the data with the given key.Returns true if the element is successfully found and removed; otherwise, false.\" );\r\nend;")]
[LuaMethod("remove", "remove the data with the given key. Returns true if the element is successfully found and removed; otherwise, false.")] [LuaMethod("remove", "remove the data with the given key. Returns true if the element is successfully found and removed; otherwise, false.")]
public bool Remove(string key) public bool Remove(string key) => APIs.UserData.Remove(key);
{
return Global.UserBag.Remove(key);
}
[LuaMethodExample("if ( userdata.containskey( \"Unique key\" ) ) then\r\n\tconsole.log( \"returns whether or not there is an entry for the given key\" );\r\nend;")] [LuaMethodExample("if ( userdata.containskey( \"Unique key\" ) ) then\r\n\tconsole.log( \"returns whether or not there is an entry for the given key\" );\r\nend;")]
[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(string key) public bool ContainsKey(string key) => APIs.UserData.ContainsKey(key);
{
return Global.UserBag.ContainsKey(key);
}
} }
} }