70 lines
2.9 KiB
C#
70 lines
2.9 KiB
C#
#if UNITY_NETCODE || NETCODE_PRESENT
|
|
using System;
|
|
using System.Reflection;
|
|
using UnityEngine;
|
|
using Unity.Netcode;
|
|
|
|
namespace MegaKoop.Networking
|
|
{
|
|
/// <summary>
|
|
/// Reflection-based adapter that tries to configure your Steam transport (attached to NetworkManager)
|
|
/// for host/client using provided SteamIDs. If it can't find a known method, it logs a clear instruction.
|
|
/// Replace this with a strongly typed adapter when you pick a specific Steam transport.
|
|
/// </summary>
|
|
public class SteamNGOAdapter : MonoBehaviour, ISteamNGOAdapter
|
|
{
|
|
private object Transport => NetworkManager.Singleton ? NetworkManager.Singleton.NetworkConfig.NetworkTransport : null;
|
|
|
|
public void ConfigureHost(ulong hostSteamId)
|
|
{
|
|
var t = Transport;
|
|
if (t == null)
|
|
{
|
|
Debug.LogWarning("[SteamNGOAdapter] NetworkTransport not found on NetworkManager.");
|
|
return;
|
|
}
|
|
if (TryInvoke(t, "ConfigureHost", hostSteamId)) return;
|
|
if (TryInvoke(t, "SetHostSteamId", hostSteamId)) return;
|
|
if (TryInvoke(t, "SetServerSteamId", hostSteamId)) return;
|
|
if (TryInvoke(t, "InitAsServer", hostSteamId)) return;
|
|
Debug.LogWarning("[SteamNGOAdapter] Could not configure host on transport via reflection. Please implement host configuration for your transport.");
|
|
}
|
|
|
|
public void ConfigureClient(ulong hostSteamId)
|
|
{
|
|
var t = Transport;
|
|
if (t == null)
|
|
{
|
|
Debug.LogWarning("[SteamNGOAdapter] NetworkTransport not found on NetworkManager.");
|
|
return;
|
|
}
|
|
if (TryInvoke(t, "ConfigureClient", hostSteamId)) return;
|
|
if (TryInvoke(t, "SetClientSteamId", hostSteamId)) return;
|
|
if (TryInvoke(t, "ConnectToSteamId", hostSteamId)) return;
|
|
if (TryInvoke(t, "InitAsClient", hostSteamId)) return;
|
|
Debug.LogWarning("[SteamNGOAdapter] Could not configure client on transport via reflection. Please implement client configuration for your transport.");
|
|
}
|
|
|
|
private bool TryInvoke(object target, string methodName, ulong steamId)
|
|
{
|
|
var mi = target.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
|
|
if (mi == null) return false;
|
|
try
|
|
{
|
|
if (mi.IsStatic)
|
|
mi.Invoke(null, new object[] { steamId });
|
|
else
|
|
mi.Invoke(target, new object[] { steamId });
|
|
Debug.Log($"[SteamNGOAdapter] Invoked {target.GetType().Name}.{methodName}({steamId})");
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning($"[SteamNGOAdapter] Invoke {methodName} failed: {e.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|