Skip to content
This repository was archived by the owner on Oct 22, 2023. It is now read-only.

Commit 994423c

Browse files
authored
Merge pull request #21 from proepkes/develop
Develop
2 parents 3e1a018 + f15426b commit 994423c

File tree

104 files changed

+1432
-935
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1432
-935
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,6 @@ Unity/Assembly-CSharp-Editor\.csproj
271271
/Unity/Assets/Integration/FixMath.NET.pdb.meta
272272
/Unity/Assets/Integration/ECS.pdb.meta
273273
/Unity/Assets/Integration/BEPUutilities.pdb.meta
274+
/Unity/Assets/Editor Default Resources
275+
/Unity/Assets/Editor Default Resources.meta
276+
/Unity/Assets/Debug.meta

Engine/Client/Client.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
<ProjectReference Include="..\Network\Network.csproj" />
1212
</ItemGroup>
1313

14+
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
15+
<Exec Command="xcopy /d /y &quot;$(TargetDir)Lockstep.*.dll&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;&#xD;&#xA;xcopy /d /y &quot;$(TargetDir)Lockstep.*.pdb&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;&#xD;&#xA;xcopy /d /y &quot;$(TargetDir)Lockstep.*.json&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;" />
16+
</Target>
17+
1418
</Project>

Engine/Client/CommandBuffer.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Lockstep.Core.Data;
5+
using Lockstep.Core.Interfaces;
6+
7+
namespace Lockstep.Client.Implementations
8+
{
9+
public class CommandBuffer : ICommandBuffer
10+
{
11+
private readonly Dictionary<long, List<ICommand>> _commands = new Dictionary<long, List<ICommand>>();
12+
13+
public event Action<long, ICommand> Inserted;
14+
15+
public long Count
16+
{
17+
get
18+
{
19+
lock (_commands)
20+
{
21+
return _commands.LongCount();
22+
}
23+
}
24+
}
25+
26+
public long ItemIndex { get; private set; }
27+
28+
public long Remaining => Count - ItemIndex;
29+
30+
public virtual void Insert(long frameNumber, ICommand command)
31+
{
32+
lock (_commands)
33+
{
34+
if (!_commands.ContainsKey(frameNumber))
35+
{
36+
_commands.Add(frameNumber, new List<ICommand>(10));
37+
}
38+
39+
_commands[frameNumber].Add(command);
40+
41+
Inserted?.Invoke(frameNumber, command);
42+
}
43+
}
44+
45+
public ICommand[] GetNext()
46+
{
47+
lock (_commands)
48+
{
49+
//If no commands were inserted then return an empty list
50+
if (!_commands.ContainsKey(ItemIndex))
51+
{
52+
_commands[ItemIndex] = new List<ICommand>();
53+
}
54+
55+
return _commands[ItemIndex++].ToArray();
56+
57+
}
58+
}
59+
}
60+
}

Engine/Client/FrameBuffer.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

Engine/Client/Implementations/LocalDataReceiver.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Engine/Client/Implementations/NetworkedDataReceiver.cs

Lines changed: 0 additions & 90 deletions
This file was deleted.

Engine/Client/Interfaces/IDataReceiver.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

Engine/Client/Interfaces/INetwork.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public interface INetwork
99
/// <summary>
1010
/// Send data reliable ordered
1111
/// </summary>
12-
void Send(byte[] data, int length);
12+
void Send(byte[] data);
1313
}
1414
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Lockstep.Client.Implementations;
5+
using Lockstep.Client.Interfaces;
6+
using Lockstep.Core.Data;
7+
using Lockstep.Network;
8+
using Lockstep.Network.Messages;
9+
using Lockstep.Network.Utils;
10+
11+
namespace Lockstep.Client
12+
{
13+
public class NetworkCommandBuffer : CommandBuffer
14+
{
15+
public event Action<Init> InitReceived;
16+
17+
private readonly INetwork _network;
18+
private readonly IDictionary<ushort, Func<ISerializableCommand>> _commandFactories = new Dictionary<ushort, Func<ISerializableCommand>>();
19+
20+
public NetworkCommandBuffer(INetwork network)
21+
{
22+
_network = network;
23+
_network.DataReceived += OnDataReceived;
24+
}
25+
26+
public void RegisterCommand(Func<ISerializableCommand> commandFactory)
27+
{
28+
var tag = commandFactory.Invoke().Tag;
29+
if (_commandFactories.ContainsKey(tag))
30+
{
31+
throw new InvalidDataException("The command tag " + tag + " is already registered. Every command tag must be unique.");
32+
}
33+
_commandFactories.Add(tag, commandFactory);
34+
}
35+
36+
public override void Insert(long frameNumber, ICommand command)
37+
{
38+
if (command is ISerializableCommand serializable)
39+
{
40+
//Tell the server
41+
var writer = new Serializer();
42+
writer.Put((byte)MessageTag.Input);
43+
writer.Put(frameNumber);
44+
writer.Put(serializable.Tag);
45+
serializable.Serialize(writer);
46+
47+
_network.Send(Compressor.Compress(writer));
48+
}
49+
}
50+
51+
private void OnDataReceived(byte[] data)
52+
{
53+
data = Compressor.Decompress(data);
54+
55+
var reader = new Deserializer(data);
56+
var messageTag = (MessageTag)reader.GetByte();
57+
switch (messageTag)
58+
{
59+
case MessageTag.StartSimulation:
60+
var init = new Init();
61+
init.Deserialize(reader);
62+
InitReceived?.Invoke(init);
63+
break;
64+
case MessageTag.Input:
65+
var frameNumber = reader.GetLong();
66+
var tag = reader.GetUShort();
67+
68+
if (_commandFactories.ContainsKey(tag))
69+
{
70+
var newCommand = _commandFactories[tag].Invoke();
71+
newCommand.Deserialize(reader);
72+
73+
74+
base.Insert(frameNumber, newCommand);
75+
}
76+
77+
break;
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)