2020-03-04 21:46:16 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2020-03-06 19:01:16 +01:00
|
|
|
|
using OWML.ModHelper.Events;
|
2020-03-04 21:46:16 +01:00
|
|
|
|
using QSB.Animation;
|
|
|
|
|
using QSB.Events;
|
2020-02-24 19:55:16 +01:00
|
|
|
|
using QSB.TimeSync;
|
2020-02-21 23:36:07 +01:00
|
|
|
|
using QSB.TransformSync;
|
|
|
|
|
using UnityEngine;
|
2020-02-13 20:23:26 +01:00
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
2020-02-15 20:48:02 +01:00
|
|
|
|
namespace QSB
|
|
|
|
|
{
|
|
|
|
|
public class QSBNetworkManager : NetworkManager
|
|
|
|
|
{
|
2020-03-02 17:25:37 +01:00
|
|
|
|
private const int MaxConnections = 128;
|
|
|
|
|
|
2020-02-21 23:36:07 +01:00
|
|
|
|
private AssetBundle _assetBundle;
|
|
|
|
|
private GameObject _shipPrefab;
|
|
|
|
|
|
2020-03-04 21:46:16 +01:00
|
|
|
|
private readonly string[] _defaultNames = {
|
|
|
|
|
"Arkose",
|
|
|
|
|
"Chert",
|
|
|
|
|
"Esker",
|
|
|
|
|
"Hal",
|
|
|
|
|
"Hornfels",
|
|
|
|
|
"Feldspar",
|
|
|
|
|
"Gabbro",
|
|
|
|
|
"Galena",
|
|
|
|
|
"Gneiss",
|
|
|
|
|
"Gossan",
|
|
|
|
|
"Marl",
|
|
|
|
|
"Mica",
|
|
|
|
|
"Moraine",
|
|
|
|
|
"Porphy",
|
|
|
|
|
"Riebeck",
|
|
|
|
|
"Rutile",
|
|
|
|
|
"Slate",
|
|
|
|
|
"Spinel",
|
|
|
|
|
"Tektite",
|
|
|
|
|
"Tephra",
|
|
|
|
|
"Tuff"
|
|
|
|
|
};
|
|
|
|
|
private string _playerName;
|
2020-03-06 19:01:16 +01:00
|
|
|
|
private bool _canEditName;
|
2020-03-04 21:46:16 +01:00
|
|
|
|
|
2020-02-15 20:48:02 +01:00
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2020-02-21 21:51:58 +01:00
|
|
|
|
_assetBundle = QSB.Helper.Assets.LoadBundle("assets/network");
|
|
|
|
|
playerPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkplayer.prefab");
|
2020-02-21 23:36:07 +01:00
|
|
|
|
playerPrefab.AddComponent<PlayerTransformSync>();
|
2020-02-18 21:39:18 +01:00
|
|
|
|
playerPrefab.AddComponent<AnimationSync>();
|
2020-02-24 19:55:16 +01:00
|
|
|
|
playerPrefab.AddComponent<WakeUpSync>();
|
2020-03-06 05:16:41 +01:00
|
|
|
|
playerPrefab.AddComponent<NetPlayer>();
|
2020-02-21 21:51:58 +01:00
|
|
|
|
|
|
|
|
|
_shipPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkship.prefab");
|
|
|
|
|
_shipPrefab.AddComponent<ShipTransformSync>();
|
|
|
|
|
spawnPrefabs.Add(_shipPrefab);
|
2020-03-02 17:25:37 +01:00
|
|
|
|
|
2020-03-02 20:44:44 +01:00
|
|
|
|
ConfigureNetworkManager();
|
2020-03-04 21:46:16 +01:00
|
|
|
|
|
2020-03-06 19:01:16 +01:00
|
|
|
|
_playerName = GetPlayerName();
|
|
|
|
|
_canEditName = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetPlayerName()
|
|
|
|
|
{
|
|
|
|
|
var profileManager = StandaloneProfileManager.SharedInstance;
|
|
|
|
|
profileManager.Initialize();
|
|
|
|
|
var profile = profileManager.GetValue<StandaloneProfileManager.ProfileData>("_currentProfile");
|
|
|
|
|
var profileName = profile?.profileName;
|
|
|
|
|
if (!string.IsNullOrEmpty(profileName))
|
|
|
|
|
{
|
|
|
|
|
return profileName;
|
|
|
|
|
}
|
|
|
|
|
return _defaultNames.OrderBy(x => Guid.NewGuid()).First();
|
2020-03-02 20:44:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConfigureNetworkManager()
|
|
|
|
|
{
|
2020-03-02 17:34:01 +01:00
|
|
|
|
networkAddress = QSB.DefaultServerIP;
|
2020-03-02 17:25:37 +01:00
|
|
|
|
maxConnections = MaxConnections;
|
|
|
|
|
customConfig = true;
|
|
|
|
|
connectionConfig.AddChannel(QosType.Reliable);
|
|
|
|
|
connectionConfig.AddChannel(QosType.Unreliable);
|
|
|
|
|
channels.Add(QosType.Reliable);
|
|
|
|
|
channels.Add(QosType.Unreliable);
|
2020-03-06 19:00:24 +01:00
|
|
|
|
|
|
|
|
|
QSB.Helper.HarmonyHelper.EmptyMethod<NetworkManagerHUD>("Update");
|
2020-02-21 21:51:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
|
|
|
|
|
{
|
|
|
|
|
base.OnServerAddPlayer(conn, playerControllerId);
|
|
|
|
|
|
|
|
|
|
NetworkServer.SpawnWithClientAuthority(Instantiate(_shipPrefab), conn);
|
2020-02-13 20:23:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 20:48:02 +01:00
|
|
|
|
public override void OnClientConnect(NetworkConnection conn)
|
|
|
|
|
{
|
2020-02-14 22:14:24 +01:00
|
|
|
|
base.OnClientConnect(conn);
|
2020-02-13 20:23:26 +01:00
|
|
|
|
|
2020-02-14 22:14:24 +01:00
|
|
|
|
DebugLog.Screen("OnClientConnect");
|
2020-02-13 21:23:12 +01:00
|
|
|
|
gameObject.AddComponent<SectorSync>();
|
2020-03-04 21:46:16 +01:00
|
|
|
|
gameObject.AddComponent<PlayerJoin>().Join(_playerName);
|
2020-03-06 19:01:16 +01:00
|
|
|
|
|
|
|
|
|
_canEditName = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnStopClient()
|
|
|
|
|
{
|
|
|
|
|
DebugLog.Screen("OnStopClient");
|
2020-03-07 16:40:17 +01:00
|
|
|
|
Destroy(GetComponent<SectorSync>());
|
|
|
|
|
Destroy(GetComponent<PlayerJoin>());
|
|
|
|
|
PlayerTransformSync.LocalInstance.gameObject.GetComponent<AnimationSync>().Reset();
|
|
|
|
|
|
2020-03-06 19:01:16 +01:00
|
|
|
|
_canEditName = true;
|
2020-03-04 21:46:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 19:03:35 +01:00
|
|
|
|
public override void OnServerDisconnect(NetworkConnection conn)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.Screen("OnServerDisconnect");
|
|
|
|
|
|
|
|
|
|
var playerId = conn.playerControllers[0].gameObject.GetComponent<NetPlayer>().netId.Value;
|
|
|
|
|
GetComponent<PlayerJoin>().Leave(playerId);
|
|
|
|
|
|
|
|
|
|
base.OnServerDisconnect(conn);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 21:46:16 +01:00
|
|
|
|
private void OnGUI()
|
|
|
|
|
{
|
|
|
|
|
GUI.Label(new Rect(10, 10, 200f, 20f), "Name:");
|
2020-03-06 19:01:16 +01:00
|
|
|
|
if (_canEditName)
|
|
|
|
|
{
|
|
|
|
|
_playerName = GUI.TextField(new Rect(60, 10, 145, 20f), _playerName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GUI.Label(new Rect(60, 10, 145, 20f), _playerName);
|
|
|
|
|
}
|
2020-02-13 20:23:26 +01:00
|
|
|
|
}
|
2020-02-23 18:31:38 +01:00
|
|
|
|
|
2020-02-13 20:23:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|