Skip to content

Commit

Permalink
Hide Stacktrace Directory; Resolve NetworkObject List Errors (#24)
Browse files Browse the repository at this point in the history
* Hide Stacktrace Directory; Resolve NetworkObject List Errors

* Fix path & netvar set error
  • Loading branch information
Xilophor authored Feb 11, 2024
1 parent dd8a96b commit d585b21
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 27 deletions.
10 changes: 6 additions & 4 deletions LethalNetworkAPI/LethalNetworkAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
<None Include="$(ProjectDir)../README.md" Pack="true" PackagePath="/"/>
</ItemGroup>

<!-- Embedded Debug -->
<PropertyGroup>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<PathMap>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))=./</PathMap>
</PropertyGroup>

<ItemGroup>
<None Include="$(ProjectDir)../README.md" Pack="true" PackagePath="/"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="BepInEx.Analyzers" Version="1.*" PrivateAssets="all"/>
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.1" PrivateAssets="all"/>
Expand Down
21 changes: 4 additions & 17 deletions LethalNetworkAPI/Serializable/LethalNetworkSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,19 @@ internal static class LethalNetworkSerializer
internal static byte[] Serialize<T>(T value)
{
#if NETSTANDARD2_1
return value switch
{
GameObject gameObject => SerializationUtility.SerializeValue((NetworkObjectReference)gameObject, DataFormat.Binary),
NetworkObject networkObject => SerializationUtility.SerializeValue((NetworkObjectReference)networkObject, DataFormat.Binary),
NetworkBehaviour gameObject => SerializationUtility.SerializeValue((NetworkBehaviourReference)gameObject, DataFormat.Binary),
_ => SerializationUtility.SerializeValue(value, DataFormat.Binary)
};
return SerializationUtility.SerializeValue(value, DataFormat.Binary);
#else
return [];
return [];
#endif
}

internal static T Deserialize<T>(byte[] data)
{
#if NETSTANDARD2_1
T type = default!;
return type switch
{
GameObject => (T)(object)(GameObject)SerializationUtility.DeserializeValue<NetworkObjectReference>(data, DataFormat.Binary),
NetworkObject => (T)(object)(NetworkObject)SerializationUtility.DeserializeValue<NetworkObjectReference>(data, DataFormat.Binary),
NetworkBehaviour => (T)(object)(NetworkBehaviour)SerializationUtility.DeserializeValue<NetworkBehaviourReference>(data, DataFormat.Binary),
_ => SerializationUtility.DeserializeValue<T>(data, DataFormat.Binary)
};
return SerializationUtility.DeserializeValue<T>(data, DataFormat.Binary);
#else
T test = default!;
return test;
return test;
#endif
}
}
29 changes: 29 additions & 0 deletions LethalNetworkAPI/Serializable/NetworkBehaviourFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using LethalNetworkAPI.Serializable;

#if NETSTANDARD2_1
using OdinSerializer;

[assembly: RegisterFormatter(typeof(NetworkBehaviourFormatter))]

namespace LethalNetworkAPI.Serializable;


/// <summary>
/// Custom formatter for the <see cref="NetworkBehaviour"/> type.
/// </summary>
public class NetworkBehaviourFormatter : MinimalBaseFormatter<NetworkBehaviour>
{
private static readonly Serializer<NetworkBehaviourReference> NetworkBehaviourReferenceSerializer = Serializer.Get<NetworkBehaviourReference>();

protected override void Read(ref NetworkBehaviour value, IDataReader reader)
{
value = NetworkBehaviourReferenceSerializer.ReadValue(reader);
}

protected override void Write(ref NetworkBehaviour value, IDataWriter writer)
{
NetworkBehaviourReferenceSerializer.WriteValue(value, writer);
}
}

#endif
47 changes: 47 additions & 0 deletions LethalNetworkAPI/Serializable/NetworkObjectFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using LethalNetworkAPI.Serializable;

#if NETSTANDARD2_1
using OdinSerializer;

[assembly: RegisterFormatter(typeof(NetworkObjectFormatter))]
[assembly: RegisterFormatter(typeof(GameObjectFormatter))]

namespace LethalNetworkAPI.Serializable;

/// <summary>
/// Custom formatter for the <see cref="NetworkObject"/> type.
/// </summary>
public class NetworkObjectFormatter : MinimalBaseFormatter<NetworkObject>
{
private static readonly Serializer<NetworkObjectReference> NetworkObjectReferenceSerializer = Serializer.Get<NetworkObjectReference>();

protected override void Read(ref NetworkObject value, IDataReader reader)
{
value = NetworkObjectReferenceSerializer.ReadValue(reader);
}

protected override void Write(ref NetworkObject value, IDataWriter writer)
{
NetworkObjectReferenceSerializer.WriteValue(value, writer);
}
}

/// <summary>
/// Custom formatter for the <see cref="GameObject"/> type.
/// </summary>
public class GameObjectFormatter : MinimalBaseFormatter<GameObject>
{
private static readonly Serializer<NetworkObjectReference> NetworkObjectReferenceSerializer = Serializer.Get<NetworkObjectReference>();

protected override void Read(ref GameObject value, IDataReader reader)
{
value = NetworkObjectReferenceSerializer.ReadValue(reader);
}

protected override void Write(ref GameObject value, IDataWriter writer)
{
NetworkObjectReferenceSerializer.WriteValue(value, writer);
}
}

#endif
11 changes: 5 additions & 6 deletions LethalNetworkAPI/Variable/LethalNetworkVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ public TData Value
get => _value;
set
{
if (!(
_public ||
(_ownerObject is null && NetworkManager.Singleton.IsServer) ||
_ownerObject is null ||
_ownerObject.OwnerClientId == NetworkManager.Singleton.LocalClientId
)) return;
if (!(_public ||
NetworkManager.Singleton == null ||
_ownerObject is null && NetworkManager.Singleton.IsServer ||
_ownerObject is not null && _ownerObject.OwnerClientId == NetworkManager.Singleton.LocalClientId)
) return;

if (value is null) return;

Expand Down

0 comments on commit d585b21

Please sign in to comment.