-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
HostWrapper.cs
98 lines (81 loc) · 2.94 KB
/
HostWrapper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
namespace AltV.Net
{
//TODO: support own object based event names in altv with a custom object to eventName resolver as well
//TODO: like Alt.On(VehicleEvents.VehicleLock)
//TODO: Alt.EventNameResolver = (eventName) {
/// <summary>
/// Receives host delegates from host when starting the resource to communicate with host
/// </summary>
internal static class HostWrapper
{
public delegate bool ImportDelegate(string resourceName, string key, out object value);
private static Action<string, string> _startTracing;
private static Action _stopTracing;
private static ImportDelegate _import;
private static Action<string, object> _export;
private static readonly Action<long> OnTraceSizeChange = OnTraceSizeChangeDelegate;
internal static readonly HashSet<Action<long>> OnTraceFileSizeChangeEventHandlers = new HashSet<Action<long>>();
private static void OnTraceSizeChangeDelegate(long size)
{
lock (OnTraceFileSizeChangeEventHandlers)
{
foreach (var onTraceFileSizeChange in OnTraceFileSizeChangeEventHandlers)
{
onTraceFileSizeChange(size);
}
}
}
public static Action<long> GetTraceSizeChangeDelegate()
{
return OnTraceSizeChange;
}
public static void SetStartTracingDelegate(Action<string, string> startTracing)
{
_startTracing = startTracing;
}
public static void SetStopTracingDelegate(Action stopTracing)
{
_stopTracing = stopTracing;
}
public static void SetImportDelegate(Delegate import)
{
_import = new ImportDelegate((string name, string key, out object value) =>
{
var parameters = new object[] {name, key, null};
var result = (bool) import.DynamicInvoke(parameters);
value = parameters[2];
return result;
});
}
public static void SetExportDelegate(Action<string, object> export)
{
_export = export;
}
public static void StartTracing(string traceFileName, string traceFileFormatName)
{
_startTracing?.Invoke(traceFileName, traceFileFormatName);
}
public static void StopTracing()
{
_stopTracing?.Invoke();
}
public static bool Import(string resourceName, string key, out object value)
{
if (_import != null)
{
return _import.Invoke(resourceName, key, out value);
}
else
{
value = default;
return false;
}
}
public static void Export(string key, object value)
{
_export?.Invoke(key, value);
}
}
}