Added Furnance and smelting
This commit is contained in:
@@ -12,9 +12,18 @@ using Unity.Services.Relay.Models;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public enum NetworkConnectionMode
|
||||
{
|
||||
None,
|
||||
Relay,
|
||||
Lan
|
||||
}
|
||||
|
||||
[RequireComponent(typeof(NetworkManager), typeof(UnityTransport))]
|
||||
public sealed class NetworkSessionController : MonoBehaviour
|
||||
{
|
||||
public const ushort DefaultLanPort = 7777;
|
||||
|
||||
private const string PlayerNamePreferenceKey = "player_name";
|
||||
private const string RelayCodeDataKey = "relay_code";
|
||||
|
||||
@@ -26,9 +35,12 @@ public sealed class NetworkSessionController : MonoBehaviour
|
||||
[SerializeField, Min(1)] private int maxClientCount = 3;
|
||||
[SerializeField] private bool useSecureRelay = false;
|
||||
|
||||
public string Status { get; private set; } = "Relay is not initialized.";
|
||||
public string Status { get; private set; } = "Ready.";
|
||||
public string JoinCode { get; private set; } = string.Empty;
|
||||
public string LocalPlayerName { get; private set; }
|
||||
public string LanAddress { get; private set; } = "127.0.0.1";
|
||||
public ushort LanPort { get; private set; } = DefaultLanPort;
|
||||
public NetworkConnectionMode ConnectionMode { get; private set; }
|
||||
public bool IsBusy { get; private set; }
|
||||
public bool IsOnline => NetworkManager.Singleton != null && NetworkManager.Singleton.IsListening;
|
||||
public bool IsHost => networkManager != null && networkManager.IsHost;
|
||||
@@ -85,12 +97,15 @@ public sealed class NetworkSessionController : MonoBehaviour
|
||||
allocation.AllocationIdBytes, allocation.Key, allocation.ConnectionData, useSecureRelay);
|
||||
JoinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
|
||||
await PublishLobbyAsync();
|
||||
ConnectionMode = NetworkConnectionMode.Relay;
|
||||
loadFactoryAfterServerStart = true;
|
||||
if (!networkManager.StartHost()) throw new InvalidOperationException("Netcode host could not start.");
|
||||
Status = "Host started.";
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ConnectionMode = NetworkConnectionMode.None;
|
||||
loadFactoryAfterServerStart = false;
|
||||
Status = $"Relay host error: {exception.Message}";
|
||||
Debug.LogException(exception);
|
||||
}
|
||||
@@ -108,17 +123,93 @@ public sealed class NetworkSessionController : MonoBehaviour
|
||||
JoinAllocation allocation = await RelayService.Instance.JoinAllocationAsync(joinCode.Trim().ToUpperInvariant());
|
||||
transport.SetClientRelayData(allocation.RelayServer.IpV4, (ushort)allocation.RelayServer.Port,
|
||||
allocation.AllocationIdBytes, allocation.Key, allocation.ConnectionData, allocation.HostConnectionData, useSecureRelay);
|
||||
ConnectionMode = NetworkConnectionMode.Relay;
|
||||
if (!networkManager.StartClient()) throw new InvalidOperationException("Netcode client could not start.");
|
||||
Status = "Connecting to host...";
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ConnectionMode = NetworkConnectionMode.None;
|
||||
Status = $"Relay join error: {exception.Message}";
|
||||
Debug.LogException(exception);
|
||||
}
|
||||
finally { IsBusy = false; }
|
||||
}
|
||||
|
||||
public bool StartLanHost(ushort port = DefaultLanPort)
|
||||
{
|
||||
if (IsBusy || IsOnline)
|
||||
return false;
|
||||
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
JoinCode = string.Empty;
|
||||
LanAddress = "127.0.0.1";
|
||||
LanPort = port;
|
||||
ConnectionMode = NetworkConnectionMode.Lan;
|
||||
transport.SetConnectionData(LanAddress, LanPort, "0.0.0.0");
|
||||
loadFactoryAfterServerStart = true;
|
||||
|
||||
if (!networkManager.StartHost())
|
||||
throw new InvalidOperationException("Netcode LAN host could not start.");
|
||||
|
||||
Status = $"LAN host started on port {LanPort}.";
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ConnectionMode = NetworkConnectionMode.None;
|
||||
loadFactoryAfterServerStart = false;
|
||||
Status = $"LAN host error: {exception.Message}";
|
||||
Debug.LogException(exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool StartLanClient(string address, ushort port = DefaultLanPort)
|
||||
{
|
||||
if (IsBusy || IsOnline)
|
||||
return false;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(address))
|
||||
{
|
||||
Status = "LAN address is empty.";
|
||||
return false;
|
||||
}
|
||||
|
||||
IsBusy = true;
|
||||
try
|
||||
{
|
||||
JoinCode = string.Empty;
|
||||
LanAddress = address.Trim();
|
||||
LanPort = port;
|
||||
ConnectionMode = NetworkConnectionMode.Lan;
|
||||
transport.SetConnectionData(LanAddress, LanPort);
|
||||
|
||||
if (!networkManager.StartClient())
|
||||
throw new InvalidOperationException("Netcode LAN client could not start.");
|
||||
|
||||
Status = $"Connecting directly to {LanAddress}:{LanPort}...";
|
||||
return true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ConnectionMode = NetworkConnectionMode.None;
|
||||
Status = $"LAN join error: {exception.Message}";
|
||||
Debug.LogException(exception);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RefreshPublicLobbiesAsync()
|
||||
{
|
||||
if (IsBusy) return;
|
||||
|
||||
Reference in New Issue
Block a user