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