136 lines
3.5 KiB
C#
136 lines
3.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Il2CppPhoton.Bolt;
|
|
using MelonLoader;
|
|
using DevourClient.Localization;
|
|
|
|
namespace DevourClient.Network
|
|
{
|
|
// Network helper class
|
|
public static class NetworkHelper
|
|
{
|
|
// Check if current player is host
|
|
public static bool IsHost()
|
|
{
|
|
try
|
|
{
|
|
return BoltNetwork.IsServer;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Get network ping
|
|
public static float GetPing()
|
|
{
|
|
try
|
|
{
|
|
BoltConnection connection = BoltNetwork.Server;
|
|
if (connection != null)
|
|
{
|
|
float pingSeconds = connection.PingNetwork;
|
|
return (float)Math.Round(pingSeconds * 1000.0f);
|
|
}
|
|
return 0.0f;
|
|
}
|
|
catch
|
|
{
|
|
return 0.0f;
|
|
}
|
|
}
|
|
|
|
// Get local connection ID
|
|
public static uint GetConnectionID()
|
|
{
|
|
try
|
|
{
|
|
return NetworkIdAllocator.LocalConnectionId;
|
|
}
|
|
catch
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// Get player count
|
|
public static int GetPlayerCount()
|
|
{
|
|
try
|
|
{
|
|
if (BoltNetwork.IsRunning)
|
|
{
|
|
UnityEngine.GameObject[] players = UnityEngine.GameObject.FindGameObjectsWithTag("Player");
|
|
return players != null ? players.Length : 0;
|
|
}
|
|
return 0;
|
|
}
|
|
catch
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// Is network running
|
|
public static bool IsNetworkRunning()
|
|
{
|
|
try
|
|
{
|
|
return BoltNetwork.IsRunning;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Is client
|
|
public static bool IsClient()
|
|
{
|
|
try
|
|
{
|
|
return BoltNetwork.IsClient;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Get formatted string of current room information (multilanguage support)
|
|
public static string GetRoomInfo()
|
|
{
|
|
try
|
|
{
|
|
if (!IsNetworkRunning())
|
|
{
|
|
return MultiLanguageSystem.Translate("Not connected");
|
|
}
|
|
|
|
string roleText = MultiLanguageSystem.Translate("Role");
|
|
string role = IsHost() ? MultiLanguageSystem.Translate("Host") : MultiLanguageSystem.Translate("Client");
|
|
string playerCountText = MultiLanguageSystem.Translate("Player Count");
|
|
int playerCount = GetPlayerCount();
|
|
float ping = IsClient() ? GetPing() : 0;
|
|
|
|
if (IsHost())
|
|
{
|
|
return $"{roleText}: {role} | {playerCountText}: {playerCount}";
|
|
}
|
|
else
|
|
{
|
|
string pingText = MultiLanguageSystem.Translate("Ping");
|
|
return $"{roleText}: {role} | {playerCountText}: {playerCount} | {pingText}: {ping}ms";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MelonLogger.Error($"[NetworkHelper] Failed to get room info: {ex.Message}");
|
|
return MultiLanguageSystem.Translate("Error");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|