Skip to content

Commit

Permalink
V1.0.0 (#5)
Browse files Browse the repository at this point in the history
* Add to readme, start github action

* Add to readme, starting on github action

* some spring cleaning

* some spring cleaning

* Update README; Add to workflow

* Added Docs & Support for net472; Updated README; Thunderstore Stuff

I shoulda committed sooner
  • Loading branch information
Xilophor authored Jan 8, 2024
1 parent 4be81ba commit 18e74d5
Show file tree
Hide file tree
Showing 2,332 changed files with 785,691 additions and 258 deletions.
25 changes: 25 additions & 0 deletions .github/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: on-push

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: pull-repo
uses: actions@checkoutv4
with:
fetch-depth: 0
filter: tree:0

- name: Setup .NET environment
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.0.100"
50 changes: 50 additions & 0 deletions .github/workflows/github-pages-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Deploy to GitHub Pages

on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
# Build job
build:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
- name: Install dependencies
run: npm install
- name: Build
run: npm run docs:build
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
path: ./.vitepress/dist
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

## v1.0.0 - ***Initial Release!***

Added:

- Network Messages
- Network Events
- Network Variables
- Network Utils/Extensions
6 changes: 3 additions & 3 deletions LethalNetworkAPI.csproj.user template.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LC_PLUGIN_DIR>Plugin Dir</LC_PLUGIN_DIR>
<LC_DEPS_DIR>Deps Dir</LC_DEPS_DIR>
<NETCODE_PATCHER>NetcodePatcher Dir</NETCODE_PATCHER>
<PluginsDirectory>Plugin Dir</PluginsDirectory>
<DependenciesDirectory>Deps Dir</DependenciesDirectory>
<NetcodePatcherDirectory>NetcodePatcher Dir</NetcodePatcherDirectory>
</PropertyGroup>
</Project>
```
6 changes: 4 additions & 2 deletions LethalNetworkAPI/Event/LethalClientEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;

// ReSharper disable InvalidXmlDocComment

Expand Down Expand Up @@ -105,6 +103,8 @@ public void InvokeAllClientsSynced()
public event Action<ulong>? OnReceivedFromClient;

#endregion

#region Private Methods and Fields

private void ReceiveClientEvent(string identifier, ulong originatorClientId)
{
Expand Down Expand Up @@ -149,4 +149,6 @@ private IEnumerator WaitAndInvokeEvent(float timeToWait, ulong originatorClientI
}

private readonly string _eventIdentifier;

#endregion
}
6 changes: 5 additions & 1 deletion LethalNetworkAPI/Event/LethalServerEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LethalNetworkAPI;

public class LethalServerEvent
{
#region Public Constructors
#region Public Constructors
/// <summary>
/// Create a new network event for the server.
/// </summary>
Expand Down Expand Up @@ -147,6 +147,8 @@ public void InvokeAllClients(bool receiveOnHost = true)

#endregion

#region Private Methods and Fields

private void ReceiveServerEvent(string identifier, ulong originClientId)
{
if (identifier != _eventIdentifier) return;
Expand Down Expand Up @@ -191,4 +193,6 @@ private IEnumerator WaitAndInvokeEvent(float timeToWait, ulong clientId)
}

private readonly string _eventIdentifier;

#endregion
}
14 changes: 7 additions & 7 deletions LethalNetworkAPI/LethalNetworkAPI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.1;net472</TargetFrameworks>
<AssemblyName>LethalNetworkAPI</AssemblyName>
<Description>Easily create networked mods.</Description>
<Version>1.0.0</Version>
Expand All @@ -27,13 +27,13 @@

<ItemGroup>
<Reference Include="Assembly-CSharp" Publicize="true" PrivateAssets="all">
<HintPath>$(LC_DEPS_DIR)\Assembly-CSharp.dll</HintPath>
<HintPath>$(DependenciesDirectory)\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json" PrivateAssets="all">
<HintPath>$(LC_DEPS_DIR)\Newtonsoft.Json.dll</HintPath>
<HintPath>$(DependenciesDirectory)\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Unity.Netcode.Runtime" PrivateAssets="all">
<HintPath>$(LC_DEPS_DIR)\Unity.Netcode.Runtime.dll</HintPath>
<HintPath>$(DependenciesDirectory)\Unity.Netcode.Runtime.dll</HintPath>
</Reference>
</ItemGroup>

Expand All @@ -49,11 +49,11 @@
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="cd $(NETCODE_PATCHER)&#xA;NetcodePatcher.dll $(TargetDir) deps/" />
<Exec Command="cd $(NetcodePatcherDirectory)&#xA;NetcodePatcher.dll $(TargetDir) deps/" />
</Target>

<Target Name="CopyFiles" AfterTargets="PostBuildEvent">
<Copy SourceFiles="$(TargetPath)" DestinationFolder="$(LC_PLUGIN_DIR)" />
<Copy SourceFiles="$(TargetDir)\$(TargetName).pdb" DestinationFolder="$(LC_PLUGIN_DIR)" />
<Copy SourceFiles="$(TargetPath)" DestinationFolder="$(PluginsDirectory)" />
<Copy SourceFiles="$(TargetDir)\$(TargetName).pdb" DestinationFolder="$(PluginsDirectory)" />
</Target>
</Project>
2 changes: 1 addition & 1 deletion LethalNetworkAPI/LethalNetworkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class LethalNetworkExtensions
/// <param name="clientId">(<see cref="UInt64">ulong</see>) The client id. </param>
/// <returns>(<see cref="PlayerControllerB">PlayerControllerB?</see>) The player controller component.</returns>
/// <remarks>Will return <c>null</c> if the controller is not found.</remarks>
public static PlayerControllerB? GetPlayerFromId(ulong clientId)
public static PlayerControllerB? GetPlayerFromId(this ulong clientId)
{
return StartOfRound.Instance.allPlayerScripts[StartOfRound.Instance.ClientPlayerList[clientId]];
}
Expand Down
15 changes: 13 additions & 2 deletions LethalNetworkAPI/Networking/NetworkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public override void OnNetworkSpawn()

NetworkSpawn?.Invoke();
NetworkManager.NetworkTickSystem.Tick += NetworkTick;
NetworkManager.Singleton.OnClientConnectedCallback += OnPlayerJoin;
}

public override void OnNetworkDespawn()
Expand Down Expand Up @@ -126,7 +127,7 @@ internal void UpdateVariableServerRpc(string identifier, string data, ServerRpcP
}

[ClientRpc]
private void UpdateVariableClientRpc(string identifier, string data, ClientRpcParams clientRpcParams = default)
internal void UpdateVariableClientRpc(string identifier, string data, ClientRpcParams clientRpcParams = default)
{
OnVariableUpdate?.Invoke(identifier, data);
clientRpcParams.Send.TargetClientIdsNativeArray?.Dispose();
Expand All @@ -135,8 +136,16 @@ private void UpdateVariableClientRpc(string identifier, string data, ClientRpcPa
Plugin.Logger.LogDebug($"Received variable with identifier: {identifier}");
#endif
}




[ServerRpc(RequireOwnership = false)]
internal void GetVariableValueServerRpc(string identifier, ServerRpcParams serverRpcParams = default)
{
GetVariableValue?.Invoke(identifier, serverRpcParams.Receive.SenderClientId);
}



[ServerRpc(RequireOwnership = false)]
internal void UpdateOwnershipServerRpc(string identifier, ulong newClientId, ServerRpcParams serverRpcParams = default)
Expand All @@ -157,11 +166,13 @@ internal void UpdateOwnershipClientRpc(string identifier, ulong[] clientIds)
internal static event Action? NetworkSpawn;
internal static event Action? NetworkDespawn;
internal static event Action? NetworkTick;
internal static event Action<ulong>? OnPlayerJoin;

internal static event Action<string, string, ulong>? OnServerMessage; // identifier, data, originatorClientId
internal static event Action<string, string, ulong>? OnClientMessage; // identifier, data, originatorClientId

internal static event Action<string, string>? OnVariableUpdate; // identifier, data
internal static event Action<string, ulong>? GetVariableValue; // identifier, connectedClientId
internal static event Action<string, ulong[]>? OnOwnershipChange; // identifier, clientIds

internal static event Action<string, ulong>? OnServerEvent; // identifier, originatorClientId
Expand Down
2 changes: 0 additions & 2 deletions LethalNetworkAPI/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ private void Awake()
NetcodePatcher();

_harmony.PatchAll(typeof(NetworkObjectManager));

new LethalServerMessage<string>("").SendAllClients("");

Logger.LogDebug("LethalNetworkAPI Patches Applied");
}
Expand Down
64 changes: 63 additions & 1 deletion LethalNetworkAPI/Variable/LethalNetworkVariable.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Unity.Collections;

namespace LethalNetworkAPI;

/// <typeparam name="TData">The <a href="https://docs.unity3d.com/2022.3/Documentation/Manual/script-Serialization.html#SerializationRules">serializable data type</a> of the message.</typeparam>
Expand All @@ -15,10 +17,49 @@ public LethalNetworkVariable(string identifier)
NetworkHandler.OnVariableUpdate += ReceiveUpdate;
NetworkHandler.OnOwnershipChange += OwnershipChange;
NetworkHandler.NetworkTick += OnNetworkTick;
NetworkHandler.NetworkSpawn += () => {
if (NetworkHandler.Instance != null &&
(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost))
{
// Send variable data when a player joins (if variable is created outside of playtime (in main menu))
NetworkHandler.OnPlayerJoin += OnPlayerJoin;
}
};
// Send variable data when a variable is initialized during playtime (in lobby)
NetworkHandler.GetVariableValue += (id, clientId) =>
{
if (id != _variableIdentifier) return;

if (NetworkHandler.Instance == null)
{
Plugin.Logger.LogError(string.Format(TextDefinitions.NetworkHandlerDoesNotExist));
return;
}

if (!(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost)) return;

NetworkHandler.Instance.UpdateVariableClientRpc(_variableIdentifier,
Serializer.Serialize<TData>(_value!), new ClientRpcParams
{
Send = { TargetClientIdsNativeArray = new NativeArray<ulong>(new[] { clientId }, Allocator.Persistent) }
});
};

if (typeof(LethalNetworkVariable<TData>).GetCustomAttributes(typeof(LethalNetworkProtectedAttribute), true).Any())
_protect = true;


if (NetworkHandler.Instance != null &&
!(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost))
{
NetworkHandler.Instance.GetVariableValueServerRpc(_variableIdentifier);
}
else if (NetworkHandler.Instance != null &&
(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost))
{
// Send variable data when a player joins (if variable is created during playtime (in lobby))
NetworkHandler.OnPlayerJoin += OnPlayerJoin;
}

#if DEBUG
Plugin.Logger.LogDebug($"NetworkVariable with identifier \"{_variableIdentifier}\" has been created.");
#endif
Expand Down Expand Up @@ -89,6 +130,27 @@ public TData Value

#region Private Methods

private void OnPlayerJoin(ulong clientId)
{
if (NetworkHandler.Instance == null)
{
Plugin.Logger.LogError(string.Format(TextDefinitions.NetworkHandlerDoesNotExist));
return;
}

if (!(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost)) return;

#if DEBUG
Plugin.Logger.LogDebug($"Player Joined! Sending {_variableIdentifier}'s value.");
#endif

NetworkHandler.Instance.UpdateVariableClientRpc(_variableIdentifier,
Serializer.Serialize<TData>(_value!), new ClientRpcParams
{
Send = { TargetClientIdsNativeArray = new NativeArray<ulong>(new[] { clientId }, Allocator.Persistent) }
});
}

private void SendUpdate()
{
if (NetworkHandler.Instance == null)
Expand Down
Loading

0 comments on commit 18e74d5

Please sign in to comment.