Skip to content

Commit

Permalink
Did some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NullShock78 committed Apr 20, 2021
1 parent c558db2 commit 9869505
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 380 deletions.
6 changes: 3 additions & 3 deletions Source/LunarBind/Attributes/LunarBindHideAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
//TODO: Implement
using System;
/// <summary>
/// Hide constructor from being added
/// Hide from being added
/// </summary>
[System.AttributeUsage(AttributeTargets.Constructor, Inherited = false, AllowMultiple = false)]
internal sealed class LunarBindHideAttribute : Attribute
[System.AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Struct | AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class LunarBindHideAttribute : Attribute
{
public LunarBindHideAttribute()
{
Expand Down
2 changes: 1 addition & 1 deletion Source/LunarBind/Attributes/LunarBindTypeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;

[System.AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
internal sealed class LunarBindTypeAttribute : Attribute
internal class LunarBindTypeAttribute : Attribute
{
readonly string name;
readonly bool newable;
Expand Down
7 changes: 4 additions & 3 deletions Source/LunarBind/Binding/BindEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

internal class BindEnum : BindItem
{
public string Name { get; private set; }
private List<KeyValuePair<string, int>> enumVals = new List<KeyValuePair<string, int>>();
public BindEnum(string name, Type e)
{
Name = name;
var fields = e.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
foreach (var field in fields)
{
var attr = (LunarBindHideAttribute)Attribute.GetCustomAttribute(field, typeof(LunarBindHideAttribute));
if(attr == null)
var hidden = (LunarBindHideAttribute)Attribute.GetCustomAttribute(field, typeof(LunarBindHideAttribute)) != null ||
(MoonSharpHiddenAttribute)Attribute.GetCustomAttribute(field, typeof(MoonSharpHiddenAttribute)) != null;

if(!hidden)
{
enumVals.Add(new KeyValuePair<string, int>(field.Name, (int)field.GetValue(null)));
}
Expand Down
16 changes: 12 additions & 4 deletions Source/LunarBind/Binding/BindFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ internal class BindFunc : BindItem
//TODO: create convention for coroutine yield
const string COROUTINE_YIELD_ = "COROUTINE_YIELD_";

public BindFunc(string name, Delegate callback, string documentation = "", string example = "")
public BindFunc(string name, Delegate callback, bool autoYield = true, string documentation = "", string example = "")
{
this.Callback = callback;
this.Documentation = documentation;
this.Example = example;
IsYieldable = typeof(Yielder).IsAssignableFrom(callback.Method.ReturnType);
if (IsYieldable && GlobalScriptBindings.AutoYield) { Name = COROUTINE_YIELD_ + name; }
else { Name = name; }
if (autoYield)
{
IsYieldable = typeof(Yielder).IsAssignableFrom(callback.Method.ReturnType);
if (IsYieldable && GlobalScriptBindings.AutoYield) { Name = COROUTINE_YIELD_ + name; }
else { Name = name; }
}
else
{
IsYieldable = false;
Name = name;
}
YieldableString = "";
}

Expand Down
1 change: 1 addition & 0 deletions Source/LunarBind/Binding/BindItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace LunarBind
internal abstract class BindItem
{
public string Name { get; internal protected set; }
//Todo: limit to table and function
public string YieldableString { get; internal protected set; }
internal abstract void AddToScript(Script script);
}
Expand Down
109 changes: 52 additions & 57 deletions Source/LunarBind/Binding/GlobalScriptBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,36 @@ private static void InitializeYielders()
//Other internal yielder types are not meant to be created in Lua
}


/// <summary>
/// Initializes a script with C# callback functions, types, newable types, enums, and yieldables
/// </summary>
/// <param name="lua"></param>
public static void Initialize(Script lua)
{
foreach (var item in bindItems.Values)
{
item.AddToScript(lua);
}

foreach (var type in staticTypes)
{
lua.Globals[type.Key] = type.Value;
}

InitializeNewables(lua);
InitializeYieldables(lua);

if (CustomInitializerString != null)
{
lua.DoString(CustomInitializerString);
}
}

public static void AddYieldableType<T>(string name = null) where T : Yielder
{
if (name == null) { name = typeof(T).Name; }
RegisterUserDataType(typeof(T));
//yieldableTypes[TypePrefix + name] = typeof(T);
yieldableTypes[name] = typeof(T);
BakeYieldableNewables();
}
Expand All @@ -75,15 +100,13 @@ public static void AddYieldableType<T>(string name = null) where T : Yielder
public static void AddNewableType(string name, Type t)
{
RegisterUserDataType(t);
//newableTypes[TypePrefix + name] = t;
newableTypes[name] = t;
BakeNewables();
}

public static void AddNewableType(Type t)
{
RegisterUserDataType(t);
//newableTypes[TypePrefix + t.Name] = t;
newableTypes[t.Name] = t;
BakeNewables();
}
Expand Down Expand Up @@ -136,37 +159,37 @@ public static void AddGlobalObject(string path, object o)
}

/// <summary>
/// Call this to register all the callback functions in all assemblies in the current app domain. Not recommended.
/// Bind all static functions with the [<see cref="LunarBindFunctionAttribute"/>] attribute on each type in each assembly
/// </summary>
public static void AddAllAssemblies()
/// <param name="assemblies"></param>
public static void BindAssemblyFuncs(params Assembly[] assemblies)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
RegisterAssemblyFuncs(assembly);
UserData.RegisterAssembly(assembly);
}
}


//TODO: rename to differentiate from the AddGlobalType, etc
/// <summary>
/// Register all the callback functions for specific assemblies only.
/// Use <see cref="BindTypeFuncs(Type[])"/> instead
/// </summary>
/// <param name="assemblies"></param>
public static void AddAssemblies(params Assembly[] assemblies)
[Obsolete]
public static void AddTypes(params Type[] types)
{
foreach (var assembly in assemblies)
foreach (var type in types)
{
RegisterAssemblyFuncs(assembly);
RegisterTypeFuncs(type);
}
}


//TODO: rename to differentiate from the AddStaticType, etc
/// <summary>
/// Automatically register all the static functions with the <see cref="LunarBindFunctionAttribute"/> for specific types
/// Bind all static functions with the [<see cref="LunarBindFunctionAttribute"/>] attribute on each type
/// </summary>
/// <param name="assemblies"></param>
public static void AddTypes(params Type[] types)
/// <param name="types"></param>
public static void BindTypeFuncs(params Type[] types)
{
foreach (var type in types)
{
Expand Down Expand Up @@ -227,10 +250,11 @@ private static void RegisterTypeFuncs(Type type)
var example = (LunarBindExampleAttribute)Attribute.GetCustomAttribute(mi, typeof(LunarBindExampleAttribute));
var del = BindingHelpers.CreateDelegate(mi);
string name = attr.Name ?? mi.Name;
BindingHelpers.CreateBindFunction(bindItems, name, del, documentation?.Data ?? "", example?.Data ?? "");

BindingHelpers.CreateBindFunction(bindItems, name, del, attr.AutoYield, documentation?.Data ?? "", example?.Data ?? "");
//callbackItems[name] = new CallbackFunc(name, del, documentation?.Data ?? "", example?.Data ?? "");
}
}
}
}

//public static void HookActionProps<T0>(Type type)
Expand Down Expand Up @@ -263,7 +287,7 @@ private static void RegisterTypeFuncs(Type type)
/// <param name="example"></param>
public static void AddAction(string name, Action action, string documentation = "", string example = "")
{
BindingHelpers.CreateBindFunction(bindItems, name, action, documentation ?? "", example ?? "");
BindingHelpers.CreateBindFunction(bindItems, name, action, false, documentation ?? "", example ?? "");
}
/// <summary>
/// Add a specific <see cref="Action"/> to the bindings, using the method's Name as the name
Expand All @@ -273,7 +297,7 @@ public static void AddAction(string name, Action action, string documentation =
/// <param name="example"></param>
public static void AddAction(Action action, string documentation = "", string example = "")
{
BindingHelpers.CreateBindFunction(bindItems, action.Method.Name, action, documentation ?? "", example ?? "");
BindingHelpers.CreateBindFunction(bindItems, action.Method.Name, action, false, documentation ?? "", example ?? "");
//callbackItems[action.Method.Name] = new CallbackFunc(action.Method.Name, action, documentation, example);
}

Expand All @@ -285,7 +309,7 @@ public static void AddActions(params Action[] actions)
{
foreach (var action in actions)
{
BindingHelpers.CreateBindFunction(bindItems, action.Method.Name, action, "", "");
BindingHelpers.CreateBindFunction(bindItems, action.Method.Name, action, false, "", "");
//callbackItems[action.Method.Name] = new CallbackFunc(action.Method.Name, action);
}
}
Expand All @@ -299,7 +323,7 @@ public static void AddActions(params Action[] actions)
/// <param name="example"></param>
public static void AddDelegate(string name, Delegate del, string documentation = "", string example = "")
{
BindingHelpers.CreateBindFunction(bindItems, name, del, documentation ?? "", example ?? "");
BindingHelpers.CreateBindFunction(bindItems, name, del, false, documentation ?? "", example ?? "");
//callbackItems[name] = new CallbackFunc(name, del, documentation, example);
}

Expand All @@ -312,7 +336,7 @@ public static void AddDelegate(string name, Delegate del, string documentation =
/// <param name="example"></param>
public static void AddDelegate(Delegate del, string documentation = "", string example = "")
{
BindingHelpers.CreateBindFunction(bindItems, del.Method.Name, del, documentation ?? "", example ?? "");
BindingHelpers.CreateBindFunction(bindItems, del.Method.Name, del, false, documentation ?? "", example ?? "");
//callbackItems[del.Method.Name] = new CallbackFunc(del.Method.Name, del, documentation, example);
}

Expand All @@ -327,12 +351,12 @@ public static void AddDelegates(params Delegate[] dels)
{
foreach (var del in dels)
{
BindingHelpers.CreateBindFunction(bindItems, del.Method.Name, del, "", "");
BindingHelpers.CreateBindFunction(bindItems, del.Method.Name, del, false, "", "");
//callbackItems[del.Method.Name] = new CallbackFunc(del.Method.Name, del);
}
}

static void RegisterAssemblyFuncs(Assembly assembly)
private static void RegisterAssemblyFuncs(Assembly assembly)
{
Type[] types = assembly.GetTypes();
foreach (var type in types)
Expand All @@ -341,35 +365,6 @@ static void RegisterAssemblyFuncs(Assembly assembly)
}
}

/// <summary>
/// Initializes a script with C# callback functions, static types, newable types, and yieldables
/// </summary>
/// <param name="lua"></param>
public static void Initialize(Script lua)
{
//TODO: more stuff
foreach (var item in bindItems.Values)
{
item.AddToScript(lua);
//lua.Globals[func.Value.Name] = func.Value.Callback;
//if (func.Value.IsYieldable)
//{
// lua.DoString(func.Value.YieldableString);
//}
}
foreach (var type in staticTypes)
{
lua.Globals[type.Key] = type.Value;
}

InitializeNewables(lua);
InitializeYieldables(lua);

if(CustomInitializerString != null)
{
lua.DoString(CustomInitializerString);
}
}


private static void BakeNewables()
Expand All @@ -382,7 +377,7 @@ private static void BakeYieldableNewables()
bakedYieldableNewableTypeString = BakeNewableTypeString(yieldableTypes);
}

private static string BakeNewableTypeString(Dictionary<string,Type> source)
private static string BakeNewableTypeString(Dictionary<string, Type> source)
{
StringBuilder s = new StringBuilder();

Expand Down Expand Up @@ -443,7 +438,7 @@ private static void InitializeNewables(Script lua)
lua.DoString(bakedNewableTypeString);
}
}

/// <summary>
/// Initializes a script with the yielder types
/// </summary>
Expand Down Expand Up @@ -488,4 +483,4 @@ internal static void InitializeYieldables(Script lua)

}

}
}
Loading

0 comments on commit 9869505

Please sign in to comment.