Skip to content

Commit eebcab0

Browse files
authored
Simplify API implementations by removing LiteNetLib dependency (#312)
- Removed the LiteNetLib from CSM.API so implementing mods don't throw errors if CSM is not available. This is realized by removing the NetPeer field from the Player class. - Also explain how the bundled protobuf-net.dll is created - Update changelog panel * Fix transport line synchronization, remove transport tool sync for now (which was broken anyway)
1 parent 29272d3 commit eebcab0

File tree

13 files changed

+75
-31
lines changed

13 files changed

+75
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,4 @@ __pycache__/
264264
.vscode/
265265
assemblies/*
266266
!assemblies/protobuf-net.dll
267+
!assemblies/LICENSE.txt

assemblies/LICENSE.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
protobuf-net.dll was built from https://github.com/protobuf-net/protobuf-net on tag 2.4.8
2+
with the following config changes:
3+
FeatureServiceModel = false
4+
FeatureServiceModelConfiguration = false
5+
PlatformXmlSerializer = false
6+
PlatformBinaryFormatter = false
7+
8+
protobuf-net is licensed under the Apache License, Version 2.0.
9+
See https://github.com/protobuf-net/protobuf-net/blob/main/Licence.txt

examples/SampleExternalMod/SampleExternalMod.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@
4646
<ItemGroup>
4747
<PackageReference Include="CitiesHarmony.API">
4848
<Version>2.0.0</Version>
49+
<IncludeAssets>compile</IncludeAssets>
4950
</PackageReference>
5051
<PackageReference Include="protobuf-net">
5152
<Version>2.4.6</Version>
53+
<IncludeAssets>compile</IncludeAssets>
5254
</PackageReference>
5355
<PackageReference Include="CitiesSkylinesMultiplayer.API">
5456
<Version>0.9.0</Version>
@@ -58,10 +60,12 @@
5860
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
5961
<SpecificVersion>False</SpecificVersion>
6062
<HintPath>..\..\assemblies\Assembly-CSharp.dll</HintPath>
63+
<Private>False</Private>
6164
</Reference>
6265
<Reference Include="ICities, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
6366
<SpecificVersion>False</SpecificVersion>
6467
<HintPath>..\..\assemblies\ICities.dll</HintPath>
68+
<Private>False</Private>
6569
</Reference>
6670
<Reference Include="System" />
6771
<Reference Include="System.Configuration" />

src/api/CSM.API.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454
<SpecificVersion>False</SpecificVersion>
5555
<HintPath>..\..\assemblies\protobuf-net.dll</HintPath>
5656
</Reference>
57-
<PackageReference Include="LiteNetLib">
58-
<Version>0.9.4</Version>
59-
</PackageReference>
6057
</ItemGroup>
6158
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6259
</Project>

src/api/Networking/Player.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
using CSM.API.Networking.Status;
2-
using LiteNetLib;
32

43
namespace CSM.API.Networking
54
{
65
public class Player
76
{
87
public string Username { get; set; }
98

10-
public NetPeer NetPeer { get; set; }
11-
129
public long Latency { get; set; }
1310

1411
public ClientStatus Status { get; set; }
1512

16-
public Player(NetPeer peer, string username)
13+
public Player(string username)
1714
{
1815
Username = username;
19-
NetPeer = peer;
2016
Latency = -1;
2117
}
2218

23-
public Player(string username) : this(null, username)
24-
{
25-
}
26-
27-
public Player() : this(null, null)
19+
public Player()
2820
{
2921
}
3022
}

src/basegame/Injections/Tools/TransportToolHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ public class PlayerTransportToolCommandHandler : BaseToolCommandHandler<PlayerTr
7676
{
7777
protected override void Configure(TransportTool tool, ToolController toolController, PlayerTransportToolCommand command) {
7878
// TODO: somehow force the rendering to occur even when clients aren't viewing the transport layer
79-
tool.m_prefab = PrefabCollection<TransportInfo>.GetPrefab(command.TransportInfo);
79+
// TODO: Implement this correctly, the following breaks our transport line synchronisation
80+
/*tool.m_prefab = PrefabCollection<TransportInfo>.GetPrefab(command.TransportInfo);
8081
ReflectionHelper.SetAttr(tool, "m_lastEditLine", command.LastEditLine);
8182
ReflectionHelper.SetAttr(tool, "m_hoverStopIndex", command.HoverStopIndex);
8283
ReflectionHelper.SetAttr(tool, "m_hoverSegmentIndex", command.HoverSegmentIndex);
83-
ReflectionHelper.SetAttr(tool, "m_hitPosition", command.HitPosition);
84+
ReflectionHelper.SetAttr(tool, "m_hitPosition", command.HitPosition);*/
8485
}
8586

8687
protected override CursorInfo GetCursorInfo(TransportTool tool)

src/csm/CSM.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
<Compile Include="Networking\Config\ClientConfig.cs" />
148148
<Compile Include="Networking\Config\ConfigData.cs" />
149149
<Compile Include="Networking\Config\ServerConfig.cs" />
150+
<Compile Include="Networking\CSMPlayer.cs" />
150151
<Compile Include="Networking\IpAddress.cs" />
151152
<Compile Include="Networking\MultiplayerManager.cs" />
152153
<Compile Include="Networking\Server.cs" />

src/csm/Commands/CommandInternal.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ public void SendToClient(NetPeer peer, CommandBase command)
5151
/// <param name="command">The command to send.</param>
5252
public void SendToClient(Player player, CommandBase command)
5353
{
54-
SendToClient(player.NetPeer, command);
54+
if (player is CSMPlayer csmPlayer)
55+
{
56+
SendToClient(csmPlayer.NetPeer, command);
57+
}
58+
else
59+
{
60+
Log.Warn("Trying to send packet to non-csm player, ignoring.");
61+
}
5562
}
5663

5764
/// <summary>
@@ -78,7 +85,7 @@ public void SendToClients(CommandBase command)
7885
/// <param name="exclude">The player to not send the packet to.</param>
7986
public void SendToOtherClients(CommandBase command, Player exclude)
8087
{
81-
foreach (Player player in MultiplayerManager.Instance.CurrentServer.ConnectedPlayers.Values)
88+
foreach (CSMPlayer player in MultiplayerManager.Instance.CurrentServer.ConnectedPlayers.Values)
8289
{
8390
if (player.Equals(exclude))
8491
continue;

src/csm/Commands/Handler/Internal/ClientDisonnectHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ protected override void Handle(ClientDisconnectCommand command)
3636

3737
public override void OnClientDisconnect(Player player)
3838
{
39+
int clientId = player is CSMPlayer csmPlayer ? csmPlayer.NetPeer.Id : -2;
3940
Command.SendToClients(new ClientDisconnectCommand
4041
{
4142
Username = player.Username,
42-
ClientId = player.NetPeer.Id
43+
ClientId = clientId
4344
});
4445
}
4546
}

src/csm/Commands/Handler/Internal/ConnectionRequestHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void HandleOnServer(ConnectionRequestCommand command, NetPeer peer)
152152
}
153153

154154
// Add the new player as a connected player
155-
Player newPlayer = new Player(peer, command.Username);
155+
CSMPlayer newPlayer = new CSMPlayer(peer, command.Username);
156156
MultiplayerManager.Instance.CurrentServer.ConnectedPlayers[peer.Id] = newPlayer;
157157

158158
// Send the result command

0 commit comments

Comments
 (0)