quantum-space-buddies/QSB/SectorSync.cs

113 lines
3.5 KiB
C#
Raw Normal View History

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-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()
{
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
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-13 21:18:41 +00:00
2020-02-15 19:48:02 +00:00
public static void SetSector(uint id, Sector.Name sectorName)
{
DebugLog.Screen("Gonna set sector");
2020-02-15 19:48:02 +00:00
_playerSectors[id] = FindSectorTransform(sectorName);
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-15 19:48:02 +00:00
public static Transform GetSector(uint id)
{
return _playerSectors[id];
}
2020-02-15 19:48:02 +00:00
private static Transform FindSectorTransform(Sector.Name sectorName)
{
if (_allSectors == null)
{
_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)
{
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;
var sectorTransform = FindSectorTransform(sectorName);
2020-02-13 20:23:12 +00:00
2020-02-15 19:48:02 +00:00
if (sectorTransform == null)
{
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)
{
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
{
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)
{
return;
}
if (____occupantType == DynamicOccupant.Player && NetworkPlayer.LocalInstance != null)
2020-02-15 19:48:02 +00:00
{
NetworkPlayer.LocalInstance.EnterSector(sector);
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
}
}