Created
June 15, 2015 11:53
-
-
Save xanathar/098bfa8d9c63f9602a75 to your computer and use it in GitHub Desktop.
MoonSharp_CustomDescritorForDictionaryAccess
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MoonSharp.Interpreter; | |
using MoonSharp.Interpreter.Interop; | |
namespace Playground | |
{ | |
class MyDictionaryDescriptor : StandardUserDataDescriptor | |
{ | |
public MyDictionaryDescriptor(Type type) | |
: base(type, InteropAccessMode.Default) | |
{ } | |
public override DynValue Index(Script script, object obj, DynValue index, bool isDirectIndexing) | |
{ | |
if (isDirectIndexing) | |
{ | |
string key = index.String; | |
if (key.StartsWith("_")) | |
index = DynValue.NewString(key.Substring(1)); | |
else | |
isDirectIndexing = false; | |
} | |
return base.Index(script, obj, index, isDirectIndexing); | |
} | |
public override bool SetIndex(Script script, object obj, DynValue index, DynValue value, bool isDirectIndexing) | |
{ | |
if (isDirectIndexing) | |
{ | |
string key = index.String; | |
if (key.StartsWith("_")) | |
index = DynValue.NewString(key.Substring(1)); | |
else | |
isDirectIndexing = false; | |
} | |
return base.SetIndex(script, obj, index, value, isDirectIndexing); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Dictionary<string, int> dic = new Dictionary<string, int>(); | |
dic["hp"] = 33; | |
UserData.RegisterType<Dictionary<string, int>>(new MyDictionaryDescriptor(typeof(Dictionary<string, int>))); | |
Script s = new Script(); | |
s.Globals["dic"] = dic; | |
try | |
{ | |
s.DoString("print(dic['hp'])"); | |
s.DoString("print(dic.hp)"); | |
s.DoString("print(dic._count)"); | |
} | |
catch (ScriptRuntimeException ex) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(ex.DecoratedMessage); | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment