2020-02-18 20:32:26 +00:00
|
|
|
|
using QSB.Messaging;
|
|
|
|
|
using System.Collections.Generic;
|
2020-02-13 20:23:12 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
2020-02-13 19:23:26 +00:00
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
namespace QSB
|
|
|
|
|
{
|
|
|
|
|
public class SectorSync : MessageHandler
|
|
|
|
|
{
|
|
|
|
|
protected override MessageType Type => MessageType.Sector;
|
2020-02-14 21:14:24 +00:00
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
private static Dictionary<uint, Transform> _playerSectors;
|
|
|
|
|
private static Sector[] _allSectors;
|
2020-02-13 20:23:12 +00:00
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2020-02-14 21:14:24 +00:00
|
|
|
|
DebugLog.Screen("Start SectorSync");
|
2020-02-15 19:48:02 +00:00
|
|
|
|
_playerSectors = new Dictionary<uint, Transform>();
|
2020-02-13 20:48:23 +00:00
|
|
|
|
|
2020-02-21 20:51:58 +00:00
|
|
|
|
QSB.Helper.HarmonyHelper.AddPrefix<SectorDetector>("AddSector", typeof(Patches), "PreAddSector");
|
2020-02-13 20:23:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
public static void SetSector(uint id, Transform sectorTransform)
|
|
|
|
|
{
|
|
|
|
|
_playerSectors[id] = sectorTransform;
|
2020-02-14 21:14:24 +00:00
|
|
|
|
}
|
2020-02-13 21:18:41 +00:00
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
public static void SetSector(uint id, Sector.Name sectorName)
|
|
|
|
|
{
|
2020-02-14 21:14:24 +00:00
|
|
|
|
DebugLog.Screen("Gonna set sector");
|
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
_playerSectors[id] = FindSectorTransform(sectorName);
|
2020-02-14 21:14:24 +00:00
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
var msg = new SectorMessage
|
|
|
|
|
{
|
|
|
|
|
SectorId = (int)sectorName,
|
|
|
|
|
SenderId = id
|
|
|
|
|
};
|
|
|
|
|
NetworkManager.singleton.client.Send((short)MessageType.Sector, msg);
|
2020-02-13 21:11:20 +00:00
|
|
|
|
|
2020-02-13 20:34:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
public static Transform GetSector(uint id)
|
|
|
|
|
{
|
|
|
|
|
return _playerSectors[id];
|
2020-02-13 20:34:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
private static Transform FindSectorTransform(Sector.Name sectorName)
|
|
|
|
|
{
|
|
|
|
|
if (_allSectors == null)
|
|
|
|
|
{
|
2020-02-14 21:14:24 +00:00
|
|
|
|
_allSectors = FindObjectsOfType<Sector>();
|
|
|
|
|
}
|
2020-02-15 19:48:02 +00:00
|
|
|
|
foreach (var sector in _allSectors)
|
|
|
|
|
{
|
|
|
|
|
if (sectorName == sector.GetName())
|
|
|
|
|
{
|
2020-02-13 20:23:12 +00:00
|
|
|
|
return sector.transform;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
protected override void OnClientReceiveMessage(NetworkMessage netMsg)
|
|
|
|
|
{
|
2020-02-14 21:14:24 +00:00
|
|
|
|
DebugLog.Screen("OnClientReceiveMessage SectorSync");
|
2020-02-15 19:48:02 +00:00
|
|
|
|
var msg = netMsg.ReadMessage<SectorMessage>();
|
2020-02-13 20:23:12 +00:00
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
var sectorName = (Sector.Name)msg.SectorId;
|
2020-02-14 21:14:24 +00:00
|
|
|
|
var sectorTransform = FindSectorTransform(sectorName);
|
2020-02-13 20:23:12 +00:00
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
if (sectorTransform == null)
|
|
|
|
|
{
|
2020-02-14 21:14:24 +00:00
|
|
|
|
DebugLog.Screen("Sector", sectorName, "not found");
|
2020-02-13 20:23:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
DebugLog.Screen("Found sector", sectorName, ", setting for", msg.SenderId);
|
|
|
|
|
_playerSectors[msg.SenderId] = sectorTransform;
|
2020-02-13 20:23:12 +00:00
|
|
|
|
}
|
2020-02-13 20:48:23 +00:00
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
protected override void OnServerReceiveMessage(NetworkMessage netMsg)
|
|
|
|
|
{
|
2020-02-14 21:14:24 +00:00
|
|
|
|
DebugLog.Screen("OnServerReceiveMessage SectorSync");
|
2020-02-15 19:48:02 +00:00
|
|
|
|
var msg = netMsg.ReadMessage<SectorMessage>();
|
|
|
|
|
NetworkServer.SendToAll((short)MessageType.Sector, msg);
|
2020-02-13 20:48:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 19:48:02 +00:00
|
|
|
|
private static class Patches
|
|
|
|
|
{
|
2020-02-21 20:51:58 +00:00
|
|
|
|
private static void PreAddSector(Sector sector, DynamicOccupant ____occupantType)
|
2020-02-15 19:48:02 +00:00
|
|
|
|
{
|
|
|
|
|
if (sector.GetName() == Sector.Name.Unnamed || sector.GetName() == Sector.Name.Ship || sector.GetName() == Sector.Name.Sun || sector.GetName() == Sector.Name.HourglassTwins)
|
|
|
|
|
{
|
2020-02-14 21:14:24 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 20:51:58 +00:00
|
|
|
|
if (____occupantType == DynamicOccupant.Player && NetworkPlayer.LocalInstance != null)
|
2020-02-15 19:48:02 +00:00
|
|
|
|
{
|
|
|
|
|
NetworkPlayer.LocalInstance.EnterSector(sector);
|
2020-02-21 20:51:58 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (____occupantType == DynamicOccupant.Ship && ShipTransformSync.LocalInstance != null)
|
|
|
|
|
{
|
|
|
|
|
ShipTransformSync.LocalInstance.EnterSector(sector);
|
2020-02-13 20:48:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-15 19:48:02 +00:00
|
|
|
|
|
2020-02-13 19:23:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|