2015-06-30 22:54:42 +00:00
using System ;
using System.ComponentModel ;
2017-07-10 04:51:02 +00:00
using NLua ;
2017-05-09 18:19:55 +00:00
2015-06-30 22:54:42 +00:00
using BizHawk.Client.Common ;
namespace BizHawk.Client.EmuHawk
{
[Description("A library for setting and retrieving dynamic data that will be saved and loaded with savestates")]
public sealed class UserDataLibrary : LuaLibraryBase
{
public UserDataLibrary ( Lua lua )
: base ( lua ) { }
public UserDataLibrary ( Lua lua , Action < string > logOutputCallback )
: base ( lua , logOutputCallback ) { }
2017-04-14 19:59:01 +00:00
public override string Name = > "userdata" ;
2015-06-30 22:54:42 +00:00
2019-11-04 01:55:38 +00:00
[LuaMethodExample("userdata.set(\"Unique key\", \"Current key data\");")]
2017-07-10 19:02:00 +00:00
[LuaMethod("set", "adds or updates the data with the given key with the given value")]
2015-06-30 22:54:42 +00:00
public void Set ( string name , object value )
2019-11-04 01:55:38 +00:00
{
if ( value ! = null )
{
var t = value . GetType ( ) ;
if ( ! t . IsPrimitive & & t ! = typeof ( string ) )
{
throw new InvalidOperationException ( "Invalid type for userdata" ) ;
}
2017-07-16 11:38:52 +00:00
}
2015-06-30 22:54:42 +00:00
Global . UserBag [ name ] = value ;
}
2019-11-04 01:55:38 +00:00
[LuaMethodExample("local obuseget = userdata.get( \"Unique key\" );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("get", "gets the data with the given key, if the key does not exist it will return nil")]
2015-06-30 22:54:42 +00:00
public object Get ( string key )
{
2019-11-16 06:52:32 +00:00
return Global . UserBag . ContainsKey ( key )
? Global . UserBag [ key ]
: null ;
2015-06-30 22:54:42 +00:00
}
2019-11-04 01:55:38 +00:00
[LuaMethodExample("userdata.clear( );")]
2017-07-10 19:02:00 +00:00
[LuaMethod("clear", "clears all user data")]
2015-06-30 22:54:42 +00:00
public void Clear ( )
{
Global . UserBag . Clear ( ) ;
}
2019-11-04 01:55:38 +00:00
[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;")]
2017-07-10 19:02:00 +00:00
[LuaMethod("remove", "remove the data with the given key. Returns true if the element is successfully found and removed; otherwise, false.")]
2015-06-30 22:54:42 +00:00
public bool Remove ( string key )
{
return Global . UserBag . Remove ( key ) ;
}
2015-07-02 18:29:19 +00:00
2019-11-04 01:55:38 +00:00
[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;")]
2017-07-10 19:02:00 +00:00
[LuaMethod("containskey", "returns whether or not there is an entry for the given key")]
2015-07-02 18:29:19 +00:00
public bool ContainsKey ( string key )
{
return Global . UserBag . ContainsKey ( key ) ;
}
2015-06-30 22:54:42 +00:00
}
}