forked from Semaeopus/Unity-Lua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LuaAPIBase.cs
68 lines (60 loc) · 1.49 KB
/
LuaAPIBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using MoonSharp.Interpreter;
/// <summary>
/// Base class for all lua api systems
/// </summary>
public abstract class LuaAPIBase
{
/// <summary>
/// The table containing the API functions and variables
/// </summary>
protected Table m_ApiTable;
/// <summary>
/// The luaVM the api is attached to
/// </summary>
protected LuaVM m_ParentVM;
/// <summary>
/// The name of the Lua API, this will be used for the api name in Lua
/// E.G
///
/// C#:
/// m_APIName = "ExampleAPI";
///
/// Lua:
/// ExampleAPI.ExampleFunction()
/// </summary>
private readonly string m_APIName;
/// <summary>
/// Derived types must provide their name
/// </summary>
/// <param name="APIName">API name.</param>
protected LuaAPIBase(string APIName)
{
m_APIName = APIName;
}
/// <summary>
/// Derived types must create a function that fills in the api table
/// </summary>
protected abstract void InitialiseAPITable();
/// <summary>
/// Adds the API to lua instance.
/// </summary>
/// <param name="luaInstance">Lua instance.</param>
public void AddAPIToLuaInstance(LuaVM luaInstance)
{
// Set our parent
m_ParentVM = luaInstance;
// Make a new table
Table apiTable = m_ParentVM.AddGlobalTable (m_APIName);
if (apiTable != null)
{
// Set the api table
m_ApiTable = apiTable;
// Hand over to the API derived type to fill in the table
InitialiseAPITable();
}
else
{
LuaLogger.Log (Channel.Lua, "Failed to Initilise API {0}", m_APIName);
}
}
}