-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGhostNetFrame.cs
More file actions
199 lines (173 loc) · 6.93 KB
/
Copy pathGhostNetFrame.cs
File metadata and controls
199 lines (173 loc) · 6.93 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using Celeste.Mod.Helpers;
using FMOD.Studio;
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using YamlDotNet.Serialization;
namespace Celeste.Mod.Ghost.Net {
public delegate void GhostNetFrameHandler(GhostNetConnection con, GhostNetFrame frame);
public delegate IChunk GhostNetChunkParser(BinaryReader reader);
/// <summary>
/// A GhostNetFrame is a collection of many individual Chunks, which can have an individual or combined meaning.
/// </summary>
public sealed class GhostNetFrame : ICloneable, IEnumerable<IChunk> {
private static IDictionary<string, GhostNetChunkParser> ChunkParsers = new Dictionary<string, GhostNetChunkParser>();
private static IDictionary<Type, string> ChunkIDs = new Dictionary<Type, string>();
// TODO: Wrapper for objects that don't natively implement IChunk, but implement its members.
public static void RegisterChunk(Type type, string id, Func<BinaryReader, object> parser)
=> RegisterChunk(type, id, reader => parser(reader) as IChunk);
public static void RegisterChunk(Type type, string id, GhostNetChunkParser parser) {
ChunkIDs[type] = id;
ChunkParsers[id] = parser;
}
public static string GetChunkID(Type type) {
string id;
if (ChunkIDs.TryGetValue(type, out id))
return id;
ChunkAttribute chunkInfo = type.GetCustomAttribute<ChunkAttribute>();
if (chunkInfo != null)
return ChunkIDs[type] = chunkInfo.ID;
throw new InvalidDataException("Unregistered chunk type");
}
public static void RegisterChunksFromModule(EverestModule module) {
foreach (Type type in module.GetType().Assembly.GetTypes()) {
ChunkAttribute chunkInfo = type.GetCustomAttribute<ChunkAttribute>();
if (chunkInfo == null)
continue;
// TODO: Can be optimized. Who wants to write a DynamicMethod generator for this? :^)
RegisterChunk(type, chunkInfo.ID, reader => {
IChunk chunk = (IChunk) Activator.CreateInstance(type);
chunk.Read(reader);
return chunk;
});
}
}
/// <summary>
/// Server-internal field. Should the frame be propagated after handling?
/// </summary>
public bool PropagateM;
/// <summary>
/// Server-internal field. Should the frame be propagated after handling?
/// </summary>
public bool PropagateU;
public IDictionary<Type, IChunk> ChunkMap = new Dictionary<Type, IChunk>();
#region Standard Chunks
// Head chunk, added by server.
public ChunkHHead HHead {
get {
return Get<ChunkHHead>();
}
set {
Add(value);
}
}
public ChunkMPlayer MPlayer {
get {
return Get<ChunkMPlayer>();
}
set {
Add(value);
}
}
public ChunkUUpdate UUpdate {
get {
return Get<ChunkUUpdate>();
}
set {
Add(value);
}
}
#endregion
// Unparsed chunks, modifyable by mods.
public byte[] Extra;
public void Read(BinaryReader reader) {
string id;
// The last "chunk" type, \r\n (Windows linebreak), doesn't contain a length.
using (MemoryStream extraBuffer = new MemoryStream())
using (BinaryWriter extraWriter = new BinaryWriter(extraBuffer)) {
while ((id = reader.ReadNullTerminatedString()) != "\r\n") {
uint length = reader.ReadUInt32();
GhostNetChunkParser parser;
if (ChunkParsers.TryGetValue(id, out parser)) {
IChunk chunk = parser(reader);
if (chunk != null && chunk.IsValid) {
lock (ChunkMap) {
ChunkMap[chunk.GetType()] = chunk;
}
}
} else {
// Store any unknown chunks.
extraWriter.WriteNullTerminatedString(id);
extraWriter.Write(length);
extraWriter.Write(reader.ReadBytes((int) length));
break;
}
}
extraWriter.Flush();
Extra = extraBuffer.ToArray();
}
}
public void Write(BinaryWriter writer) {
lock (ChunkMap) {
foreach (IChunk chunk in ChunkMap.Values)
if (chunk != null && chunk.IsValid && chunk.IsSendable)
GhostFrame.WriteChunk(writer, chunk.Write, GetChunkID(chunk.GetType()));
}
if (Extra != null)
writer.Write(Extra);
writer.WriteNullTerminatedString(GhostFrame.End);
}
public GhostNetFrame Add<T>(T chunk) where T : IChunk
=> Add(typeof(T), chunk);
public GhostNetFrame Add(Type t, IChunk chunk) {
// Assume that chunk is t for performance reasons.
lock (ChunkMap) {
ChunkMap[t] = chunk;
}
return this;
}
public void Remove<T>() where T : IChunk
=> Remove(typeof(T));
public void Remove(Type t) {
lock (ChunkMap) {
ChunkMap[t] = null;
}
}
public void Get<T>(out T chunk) where T : IChunk
=> chunk = (T) Get(typeof(T));
public T Get<T>() where T : IChunk
=> (T) Get(typeof(T));
public IChunk Get(Type t) {
IChunk chunk;
if (ChunkMap.TryGetValue(t, out chunk) && chunk != null && chunk.IsValid)
return chunk;
return null;
}
public bool Has<T>() where T : IChunk
=> Has(typeof(T));
public bool Has(Type t)
=> Get(t) != null;
public object Clone() {
GhostNetFrame clone = new GhostNetFrame();
lock (ChunkMap) {
foreach (KeyValuePair<Type, IChunk> entry in ChunkMap)
if (entry.Value != null && entry.Value.IsValid && entry.Value.IsSendable)
clone.ChunkMap[entry.Key] = (IChunk) entry.Value.Clone();
}
return clone;
}
public IEnumerator<IChunk> GetEnumerator() {
return ChunkMap.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return ChunkMap.Values.GetEnumerator();
}
}
}