blob: 36ff97d1a4efb7e9d0c6172257354c9cea74d33d (
plain)
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
|
using System.Buffers.Binary;
namespace SarahEngine;
public interface INetworkable<T>
{
void ToBytes(Span<byte> span);
static abstract T FromBytes(ReadOnlySpan<byte> span);
static int SizeOf {get;}
}
public struct NetInt(int i) : INetworkable<NetInt>
{
public int Value = i;
public static int SizeOf => sizeof(int);
public readonly void ToBytes(Span<byte> span) => BinaryPrimitives.WriteInt32LittleEndian(span, Value);
public static NetInt FromBytes(ReadOnlySpan<byte> span) => BinaryPrimitives.ReadInt32LittleEndian(span);
public static implicit operator NetInt(int i) => new(i);
public static implicit operator int(NetInt i) => i.Value;
}
public struct NetSingle(float f) : INetworkable<NetSingle>
{
public float Value = f;
public static int SizeOf => sizeof(float);
public readonly void ToBytes(Span<byte> span) => BinaryPrimitives.WriteSingleLittleEndian(span, Value);
public static NetSingle FromBytes(ReadOnlySpan<byte> span) => new(BinaryPrimitives.ReadSingleLittleEndian(span));
public static implicit operator NetSingle(float f) => new(f);
public static implicit operator float(NetSingle f) => f.Value;
}
public abstract class RPCBase
{
static readonly List<RPCBase> AllRPCs = new();
protected abstract void FromBytes(Span<byte> span);
}
public class RPC<T>(Action<T> method) : RPCBase where T : struct, INetworkable<T>
{
readonly Action<T> Method = method;
public void SendRPC(T param)
{
//blah blah blah, networking
}
protected override void FromBytes(Span<byte> span) => Method?.Invoke(T.FromBytes(span));
}
public class TestingRPC
{
static void DoStuff(NetInt testInt) {}
RPC<NetInt> Taco = new(DoStuff);
public void OtherTest(int blah) {
Taco.SendRPC(blah);
}
}
|