forked from rwmt/Multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSyncAction.cs
More file actions
163 lines (133 loc) · 5.76 KB
/
SyncAction.cs
File metadata and controls
163 lines (133 loc) · 5.76 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using HarmonyLib;
using Multiplayer.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Multiplayer.Client.Util;
using Verse;
namespace Multiplayer.Client
{
public delegate ref Action ActionGetter<in T>(T t);
public delegate Action ActionWrapper<in T, in A, in B, in C>(T instance, A a, B b, C c, Action original, Action syncAction);
public interface ISyncAction
{
IEnumerable DoSync(object target, object arg0, object arg1);
}
public class SyncAction<T, A, B, C> : SyncHandler, ISyncAction
{
private Func<A, B, C, IEnumerable<T>> func;
private ActionGetter<T> actionGetter;
private ActionWrapper<T, A, B, C> actionWrapper;
public SyncAction(Func<A, B, C, IEnumerable<T>> func, ActionGetter<T> actionGetter, ActionWrapper<T, A, B, C> actionWrapper = null)
{
this.func = func;
this.actionGetter = actionGetter;
this.actionWrapper = actionWrapper ?? ((_, _, _, _, _, _) => null);
}
public IEnumerable<T> DoSync(A target, B arg0, C arg1)
{
SyncActions.wantOriginal = true;
try
{
int i = 0;
foreach (T t in func(target, arg0, arg1))
{
int j = i;
i++;
var original = actionGetter(t);
var sync = () => ActualSync(target, arg0, arg1, original);
var wrapper = actionWrapper(t, target, arg0, arg1, original, sync);
actionGetter(t) = wrapper ?? sync;
yield return t;
}
}
finally
{
SyncActions.wantOriginal = false;
}
}
public IEnumerable DoSync(object target, object arg0, object arg1)
{
return DoSync((A)target, (B)arg0, (C)arg1);
}
private void ActualSync(A target, B arg0, C arg1, Action original)
{
LoggingByteWriter writer = new LoggingByteWriter();
MpContext context = writer.MpContext();
writer.Log.Node("Sync action");
writer.WriteInt32(syncId);
SyncSerialization.WriteSync(writer, target);
SyncSerialization.WriteSync(writer, arg0);
SyncSerialization.WriteSync(writer, arg1);
var methodDesc = original.Method.MethodDesc();
writer.Log.Node($"Method desc: {methodDesc}");
writer.WriteInt32(GenText.StableStringHash(methodDesc));
// If target is null then just sync the hash for null
var typeDesc = (original.Target?.GetType()).FullDescription();
writer.Log.Node($"Type desc: {typeDesc}");
writer.WriteInt32(GenText.StableStringHash(typeDesc));
int mapId = writer.MpContext().map?.uniqueID ?? -1;
writer.Log.Node($"Map id: {mapId}");
Multiplayer.WriterLog.AddCurrentNode(writer);
SendSyncCommand(mapId, writer);
}
public override void Handle(ByteReader data)
{
A target = SyncSerialization.ReadSync<A>(data);
B arg0 = SyncSerialization.ReadSync<B>(data);
C arg1 = SyncSerialization.ReadSync<C>(data);
int methodDescHash = data.ReadInt32();
int typeDescHash = data.ReadInt32();
var action = func(target, arg0, arg1)
.Where(t =>
{
var a = actionGetter(t);
// Match both the method description and target type description (including generics), or "null" string for the type
return GenText.StableStringHash(a.Method.MethodDesc()) == methodDescHash &&
GenText.StableStringHash((a.Target?.GetType()).FullDescription()) == typeDescHash;
})
.Select(t =>
{
var a = actionGetter(t);
var w = actionWrapper(t, target, arg0, arg1, a, null);
// Return the wrapper (if present) or the action itself
return w ?? a;
})
.FirstOrDefault();
action?.Invoke();
}
public void PatchAll(string methodName)
{
foreach (var type in typeof(A).AllSubtypesAndSelf())
{
if (type.IsAbstract) continue;
foreach (var method in type.GetDeclaredMethods().Where(m => m.Name == methodName))
{
HarmonyMethod prefix = new HarmonyMethod(typeof(SyncActions), nameof(SyncActions.SyncAction_Prefix));
prefix.priority = MpPriority.MpFirst;
HarmonyMethod postfix;
if (method.GetParameters().Length == 1)
postfix = new HarmonyMethod(typeof(SyncActions), nameof(SyncActions.SyncAction1_Postfix));
else if (method.GetParameters().Length == 2)
postfix = new HarmonyMethod(typeof(SyncActions), nameof(SyncActions.SyncAction2_Postfix));
else
throw new Exception($"Too many arguments to patch {method.FullDescription()}");
postfix.priority = MpPriority.MpLast;
Multiplayer.harmony.PatchMeasure(method, prefix, postfix);
SyncActions.syncActions[method] = this;
}
}
}
public override void Validate()
{
ValidateType("Target type", typeof(A));
ValidateType("Arg 0 type", typeof(B));
ValidateType("Arg 1 type", typeof(C));
}
public override string ToString()
{
return "SyncAction";
}
}
}