-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSyncWorker.cs
More file actions
151 lines (121 loc) · 5.22 KB
/
Copy pathSyncWorker.cs
File metadata and controls
151 lines (121 loc) · 5.22 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
using System;
namespace Multiplayer.API;
/// <summary>
/// An abstract class that can be both a reader and a writer depending on implementation.
/// </summary>
/// <remarks>See <see cref="ISynchronizable"/> and <see cref="SyncWorkerAttribute"/> for usage examples.</remarks>
public abstract class SyncWorker
{
/// <summary><see langword="true"/> if is currently writing.</summary>
public readonly bool isWriting;
protected SyncWorker(bool isWriting)
{
this.isWriting = isWriting;
}
public void Write<T>(T obj, SyncType type)
{
if (isWriting)
{
Bind(ref obj, type);
}
}
/// <summary>
/// Write the specified obj, only active during writing.
/// </summary>
/// <param name="obj">Object to write.</param>
/// <typeparam name="T">Type to write.</typeparam>
public void Write<T>(T obj) {
if (isWriting) {
Bind(ref obj);
}
}
public T Read<T>(SyncType type)
{
T obj = default(T);
if (isWriting)
{
return obj;
}
Bind(ref obj, type);
return obj;
}
/// <summary>
/// Read the specified Type from the memory stream, only active during reading.
/// </summary>
/// <returns>The requested Type object. Null if writing.</returns>
/// <typeparam name="T">The Type to read.</typeparam>
public T Read<T>() {
T obj = default(T);
if (isWriting) {
return obj;
}
Bind(ref obj);
return obj;
}
public abstract void Bind<T>(ref T obj, SyncType type);
/// <summary>Reads or writes a <see cref="Type"/> referenced by <paramref name="type"/>.</summary>
/// <typeparam name="T">Base type that <paramref name="type"/> derives from.</typeparam>
/// <param name="type">type to bind</param>
public abstract void BindType<T>(ref Type type);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref byte obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref sbyte obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref short obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref ushort obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref int obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref uint obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref long obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref ulong obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref float obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref double obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref string obj);
/// <summary>Reads or writes an object referenced by <paramref name="obj"/>.</summary>
/// <param name="obj">object to bind</param>
public abstract void Bind(ref bool obj);
/// <summary>
/// Reads or writes an object referenced by <paramref name="obj"/>
/// </summary>
/// <remarks>Can read/write types using user defined syncers, <see cref="ISynchronizable"/>s and readers/writers implemented by the multiplayer mod.</remarks>
/// <typeparam name="T">type of the object to bind</typeparam>
/// <param name="obj">object to bind</param>
public abstract void Bind<T>(ref T obj);
/// <summary>
/// Uses reflection to bind a field or property
/// </summary>
/// <param name="obj">
/// <para>object where the field or property can be found</para>
/// <para>if null, <paramref name="name"/> will point at field from the global namespace</para>
/// </param>
/// <param name="name">path to the field or property</param>
public abstract void Bind(object obj, string name);
/// <summary>
/// Reads or writes an object inheriting <see cref="ISynchronizable"/> interface.
/// </summary>
/// <remarks>Does not create a new object.</remarks>
/// <param name="obj">object to bind</param>
public void Bind(ref ISynchronizable obj)
{
obj.Sync(this);
}
}