Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Lan alternative to Steam networking. #375

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/MultiplayerMod.Test/Environment/Network/CommandTools.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using MultiplayerMod.Multiplayer.Commands;
using MultiplayerMod.Platform.Steam.Network.Messaging.Surrogates;
using MultiplayerMod.Platform.Common.Network.Messaging.Surrogates;

namespace MultiplayerMod.Test.Environment.Network;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using MultiplayerMod.ModRuntime;
using MultiplayerMod.Multiplayer.Commands;
Expand Down Expand Up @@ -83,4 +83,8 @@ public void Receive(IMultiplayerCommand command) {
}
}

public void Tick()
{
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using MultiplayerMod.Core.Extensions;
Expand Down Expand Up @@ -113,4 +113,8 @@ public void Receive(TestMultiplayerClient source, IMultiplayerCommand command, M
}
}

public void Tick()
{
}

}
2 changes: 1 addition & 1 deletion src/MultiplayerMod.Test/Game/Chores/AbstractChoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using MultiplayerMod.Multiplayer.Commands;
using MultiplayerMod.Multiplayer.Objects.Extensions;
using MultiplayerMod.Network;
using MultiplayerMod.Platform.Steam.Network.Messaging;
using MultiplayerMod.Platform.Common.Network.Messaging;
using MultiplayerMod.Test.Environment.Patches;
using MultiplayerMod.Test.GameRuntime;
using MultiplayerMod.Test.GameRuntime.Patches;
Expand Down
2 changes: 1 addition & 1 deletion src/MultiplayerMod.Test/Multiplayer/Chores/ChoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using MultiplayerMod.Multiplayer.StateMachines;
using MultiplayerMod.Multiplayer.StateMachines.Configuration;
using MultiplayerMod.Network;
using MultiplayerMod.Platform.Steam.Network.Messaging;
using MultiplayerMod.Platform.Common.Network.Messaging;
using MultiplayerMod.Test.Environment;
using MultiplayerMod.Test.Environment.Network;
using MultiplayerMod.Test.Environment.Patches;
Expand Down
41 changes: 21 additions & 20 deletions src/MultiplayerMod.Test/MultiplayerMod.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MultiplayerMod\MultiplayerMod.csproj" PrivateAssets="All"/>
<ProjectReference Include="..\MultiplayerMod\MultiplayerMod.csproj" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="AssemblyExposer.MSBuild.Task" Version="0.1.5" Private="true"/>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="AssemblyExposer.MSBuild.Task" Version="0.1.5" Private="true" />
<PackageReference Include="WebSocketSharp" Version="1.0.3-rc11" />
</ItemGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp" HintPath="$(ExposedLibrariesPath)\Assembly-CSharp.dll"/>
<Reference Include="Assembly-CSharp-firstpass" HintPath="$(ExposedLibrariesPath)\Assembly-CSharp-firstpass.dll"/>
<Reference Include="UnityEngine.CoreModule" HintPath="$(ExposedLibrariesPath)\UnityEngine.CoreModule.dll"/>
<Reference Include="Assembly-CSharp" HintPath="$(ExposedLibrariesPath)\Assembly-CSharp.dll" />
<Reference Include="Assembly-CSharp-firstpass" HintPath="$(ExposedLibrariesPath)\Assembly-CSharp-firstpass.dll" />
<Reference Include="UnityEngine.CoreModule" HintPath="$(ExposedLibrariesPath)\UnityEngine.CoreModule.dll" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony" HintPath="$(ManagedPath)\0Harmony.dll"/>
<Reference Include="FMODUnity" HintPath="$(ManagedPath)\FMODUnity.dll"/>
<Reference Include="UnityEngine" HintPath="$(ManagedPath)\UnityEngine.dll"/>
<Reference Include="UnityEngine.UI" HintPath="$(ManagedPath)\UnityEngine.UI.dll"/>
<Reference Include="UnityEngine.ImageConversionModule" HintPath="$(ManagedPath)\UnityEngine.ImageConversionModule.dll"/>
<Reference Include="UnityEngine.IMGUIModule" HintPath="$(ManagedPath)\UnityEngine.IMGUIModule.dll"/>
<Reference Include="Unity.TextMeshPro" HintPath="$(ManagedPath)\Unity.TextMeshPro.dll"/>
<Reference Include="com.rlabrecque.steamworks.net" HintPath="$(ManagedPath)\com.rlabrecque.steamworks.net.dll"/>
<Reference Include="ImGui.NET" HintPath="$(ManagedPath)\ImGui.NET.dll"/>
<Reference Include="ImGui" HintPath="$(ManagedPath)\ImGui.dll"/>
<Reference Include="LibNoiseDotNet" HintPath="$(ManagedPath)\LibNoiseDotNet.dll"/>
<Reference Include="Ionic.Zip" HintPath="$(ManagedPath)\Ionic.Zip.dll"/>
<Reference Include="Newtonsoft.Json" HintPath="$(ManagedPath)\Newtonsoft.Json.dll"/>
<Reference Include="0Harmony" HintPath="$(ManagedPath)\0Harmony.dll" />
<Reference Include="FMODUnity" HintPath="$(ManagedPath)\FMODUnity.dll" />
<Reference Include="UnityEngine" HintPath="$(ManagedPath)\UnityEngine.dll" />
<Reference Include="UnityEngine.UI" HintPath="$(ManagedPath)\UnityEngine.UI.dll" />
<Reference Include="UnityEngine.ImageConversionModule" HintPath="$(ManagedPath)\UnityEngine.ImageConversionModule.dll" />
<Reference Include="UnityEngine.IMGUIModule" HintPath="$(ManagedPath)\UnityEngine.IMGUIModule.dll" />
<Reference Include="Unity.TextMeshPro" HintPath="$(ManagedPath)\Unity.TextMeshPro.dll" />
<Reference Include="com.rlabrecque.steamworks.net" HintPath="$(ManagedPath)\com.rlabrecque.steamworks.net.dll" />
<Reference Include="ImGui.NET" HintPath="$(ManagedPath)\ImGui.NET.dll" />
<Reference Include="ImGui" HintPath="$(ManagedPath)\ImGui.dll" />
<Reference Include="LibNoiseDotNet" HintPath="$(ManagedPath)\LibNoiseDotNet.dll" />
<Reference Include="Ionic.Zip" HintPath="$(ManagedPath)\Ionic.Zip.dll" />
<Reference Include="Newtonsoft.Json" HintPath="$(ManagedPath)\Newtonsoft.Json.dll" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions src/MultiplayerMod.Test/Network/CommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using MultiplayerMod.Multiplayer.Commands;
using MultiplayerMod.Network;
using MultiplayerMod.Platform.Steam.Network;
using MultiplayerMod.Platform.Steam.Network.Messaging;
using MultiplayerMod.Platform.Common;
using MultiplayerMod.Platform.Common.Network.Messaging;
using NUnit.Framework;

namespace MultiplayerMod.Test.Network;
Expand Down
4 changes: 2 additions & 2 deletions src/MultiplayerMod/Core/Patch/PatchTargetResolver.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using MultiplayerMod.Core.Logging;
using MultiplayerMod.Platform.Steam.Network.Messaging.Surrogates;
using MultiplayerMod.Platform.Common.Network.Messaging.Surrogates;
using UnityEngine;

namespace MultiplayerMod.Core.Patch;
Expand Down
7 changes: 6 additions & 1 deletion src/MultiplayerMod/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<Copy SourceFiles="$(SourceDir)mod.yaml" DestinationFolder="$(TargetDir)"/>
</Target>

<Target Name="CopyLanConfigExample" AfterTargets="Build">
<Copy SourceFiles="$(SourceDir)lanconfig.example.txt" DestinationFolder="$(TargetDir)"/>
</Target>

<Target Name="GenerateModInfo" AfterTargets="Build">
<Message Text="Generating mod_info.yaml"/>
<ItemGroup>
Expand All @@ -62,10 +66,11 @@
<Target Name="CreateReleasePackage"
Condition="'$(Configuration)' == 'Publish'"
AfterTargets="Build"
DependsOnTargets="CopyModDescription; GenerateModInfo"
DependsOnTargets="CopyModDescription; CopyLanConfigExample; GenerateModInfo"
>
<ItemGroup>
<ModFiles Include="$(TargetDir)*.yaml"/>
<ModFiles Include="$(TargetDir)*.txt"/>
<ModFiles Include="$(TargetDir)*.dll"/>
<Scripts Include="$(SourceDir)..\..\scripts\*.*"/>
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/MultiplayerMod/Multiplayer/Commands/ArgumentUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Reflection;
using MultiplayerMod.Multiplayer.Objects.Extensions;
using MultiplayerMod.Multiplayer.Objects.Reference;
using MultiplayerMod.Platform.Steam.Network.Messaging.Surrogates;
using MultiplayerMod.Platform.Common.Network.Messaging.Surrogates;
using UnityEngine;

namespace MultiplayerMod.Multiplayer.Commands;
Expand Down
1 change: 1 addition & 0 deletions src/MultiplayerMod/MultiplayerMod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
<PackageReference Include="AssemblyExposer.MSBuild.Task" Version="0.1.5" Private="true" />
<PackageReference Include="AttributeProcessor.Annotations" Version="0.1.0" ExcludeAssets="runtime"/>
<PackageReference Include="AttributeProcessor.MSBuild.Task" Version="0.1.3" />
<PackageReference Include="WebSocketSharp" Version="1.0.3-rc11" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion src/MultiplayerMod/Network/IMultiplayerClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using MultiplayerMod.Multiplayer.Commands;

namespace MultiplayerMod.Network;
Expand All @@ -11,6 +11,7 @@ public interface IMultiplayerClient {
void Disconnect();

void Send(IMultiplayerCommand command, MultiplayerCommandOptions options = MultiplayerCommandOptions.None);
void Tick();

event Action<MultiplayerClientState> StateChanged;
event Action<IMultiplayerCommand> CommandReceived;
Expand Down
3 changes: 2 additions & 1 deletion src/MultiplayerMod/Network/IMultiplayerServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using MultiplayerMod.Multiplayer.Commands;

Expand All @@ -14,6 +14,7 @@ public interface IMultiplayerServer {

void Send(IMultiplayerClientId clientId, IMultiplayerCommand command);
void Send(IMultiplayerCommand command, MultiplayerCommandOptions options = MultiplayerCommandOptions.None);
void Tick();

event Action<MultiplayerServerState> StateChanged;
event Action<IMultiplayerClientId> ClientConnected;
Expand Down
18 changes: 18 additions & 0 deletions src/MultiplayerMod/Platform/Common/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using MultiplayerMod.Platform.Common.Network.Messaging;
using MultiplayerMod.Platform.Lan.Network;
using System;

namespace MultiplayerMod.Platform.Common;

public static class Configuration
{
public const int MaxMessageSize = 524288; // 512 KiB
public static readonly int MaxFragmentDataSize = GetFragmentDataSize();

private static int GetFragmentDataSize() {
using var serialized = NetworkSerializer.Serialize(new NetworkMessageFragment(0, Array.Empty<byte>()));
return MaxMessageSize - (int) serialized.Size;
}

public static bool useSteam { get { return !LanConfiguration.instance.isConfigured; } }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MultiplayerMod.Core.Dependency;
using MultiplayerMod.Core.Unity;
using MultiplayerMod.Platform.Common.Network;

// ReSharper disable FieldCanBeMadeReadOnly.Local

namespace MultiplayerMod.Platform.Common.Network.Components;

public class ClientComponent : MultiplayerMonoBehaviour
{

[InjectDependency]
private SharedClient client = null!;

private void Update() => client.Tick();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MultiplayerMod.Core.Dependency;
using MultiplayerMod.Core.Unity;
using MultiplayerMod.Platform.Common.Network;

// ReSharper disable FieldCanBeMadeReadOnly.Local

namespace MultiplayerMod.Platform.Common.Network.Components;

public class ServerComponent : MultiplayerMonoBehaviour
{

[InjectDependency]
private SharedServer server = null!;

private void Update() => server.Tick();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace MultiplayerMod.Platform.Common.Network.Messaging;

public interface INetworkMessage { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace MultiplayerMod.Platform.Common.Network.Messaging;

public interface INetworkMessageHandle : IDisposable
{
public IntPtr Pointer { get; }
public uint Size { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using MultiplayerMod.Multiplayer.Commands;
using MultiplayerMod.Network;
using MultiplayerMod.Platform.Common.Network.Messaging.Surrogates;

namespace MultiplayerMod.Platform.Common.Network.Messaging;

[Serializable]
public class NetworkMessage : INetworkMessage
{
private static BinaryFormatter formatter = new BinaryFormatter() { SurrogateSelector = SerializationSurrogates.Selector };

public IMultiplayerCommand Command { get; }
public MultiplayerCommandOptions Options { get; }

public NetworkMessage(IMultiplayerCommand command, MultiplayerCommandOptions options)
{
Command = command;
Options = options;
}

public byte[] toBytes() {
using (var memoryStream = new MemoryStream()) {
formatter.Serialize(memoryStream, this);
byte[] data = new byte[memoryStream.Length];
Array.Copy(memoryStream.GetBuffer(), 0, data, 0, data.Length);
return data;
}
}

public static NetworkMessage from(byte[] rawData) {
using (var memoryStream = new MemoryStream(rawData)) {
var message = (NetworkMessage) formatter.Deserialize(memoryStream);
return message;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using System;
using System.Collections.Generic;
using MultiplayerMod.Multiplayer.Commands;
using MultiplayerMod.Network;
using static MultiplayerMod.Platform.Steam.Network.Configuration;
using static MultiplayerMod.Platform.Common.Configuration;

namespace MultiplayerMod.Platform.Steam.Network.Messaging;
namespace MultiplayerMod.Platform.Common.Network.Messaging;

public class NetworkMessageFactory {
public class NetworkMessageFactory
{

public IEnumerable<INetworkMessageHandle> Create(IMultiplayerCommand command, MultiplayerCommandOptions options) {
public IEnumerable<INetworkMessageHandle> Create(IMultiplayerCommand command, MultiplayerCommandOptions options)
{
using var message = NetworkSerializer.Serialize(new NetworkMessage(command, options));
if (message.Size <= MaxMessageSize) {
if (message.Size <= MaxMessageSize)
{
yield return message;
yield break;
}
Expand All @@ -20,7 +23,8 @@ public IEnumerable<INetworkMessageHandle> Create(IMultiplayerCommand command, Mu
var serializedHeader = NetworkSerializer.Serialize(header);
yield return serializedHeader;

for (var i = 0; i < fragmentsCount; i++) {
for (var i = 0; i < fragmentsCount; i++)
{
var offset = i * MaxFragmentDataSize;
var bufferSize = Math.Min(Math.Max((int) message.Size - offset, 0), MaxFragmentDataSize);
var data = new byte[bufferSize];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace MultiplayerMod.Platform.Common.Network.Messaging;

[Serializable]
public class NetworkMessageFragment : INetworkMessage
{

public int MessageId { get; }
public byte[] Data { get; }

public NetworkMessageFragment(int messageId, byte[] data)
{
MessageId = messageId;
Data = data;
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System;
using System.Threading;

namespace MultiplayerMod.Platform.Steam.Network.Messaging;
namespace MultiplayerMod.Platform.Common.Network.Messaging;

[Serializable]
public class NetworkMessageFragmentsHeader : INetworkMessage {
public class NetworkMessageFragmentsHeader : INetworkMessage
{

public int MessageId { get; }
public int FragmentsCount { get; }

private static int uniqueMessageId;

public NetworkMessageFragmentsHeader(int fragmentsCount) {
public NetworkMessageFragmentsHeader(int fragmentsCount)
{
MessageId = Interlocked.Increment(ref uniqueMessageId);
FragmentsCount = fragmentsCount;
}
Expand Down
Loading