-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRemoteFuncManager.cs
More file actions
57 lines (51 loc) · 1.89 KB
/
RemoteFuncManager.cs
File metadata and controls
57 lines (51 loc) · 1.89 KB
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
using SkylinesPythonShared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace PythonConsole
{
public class RemoteFuncManager
{
private TcpClient client;
private delegate object CallMethod(object msg);
private Dictionary<string,TargetInfo> funcDict = new Dictionary<string, TargetInfo>();
public RemoteFuncManager(TcpClient client)
{
this.client = client;
foreach (var field in typeof(Contracts).GetFields(BindingFlags.Static | BindingFlags.Public)) {
Contract contract = (Contract)field.GetValue(null);
MethodInfo method = typeof(GameAPI).GetMethod(contract.FuncName, BindingFlags.Public | BindingFlags.Static);
CallMethod del = (CallMethod)Delegate.CreateDelegate(typeof(CallMethod), method);
funcDict.Add("c_callfunc_" + contract.FuncName, new TargetInfo(del, contract));
}
}
public void HandleAPICall(object msg, string type, long requestId)
{
object retVal = null;
TargetInfo info = funcDict[type];
try
{
retVal = info.method(msg);
if (info.contract.RetType != null) {
client.SendMsg(retVal, "s_ret_" + info.contract.RetType, requestId);
}
}
catch(Exception ex)
{
client.SendMsg(ex.Message + " (source: " + info.contract.FuncName + ")", "s_exception", requestId);
}
}
private struct TargetInfo
{
internal TargetInfo(CallMethod method, Contract contract)
{
this.method = method;
this.contract = contract;
}
internal CallMethod method;
internal Contract contract;
}
}
}