129 lines
4.1 KiB
C#
Raw Normal View History

2020-07-28 15:59:24 +02:00
using QSB.Messaging;
2020-02-13 21:23:12 +01:00
using UnityEngine;
using System.Linq;
using QSB.Utility;
using UnityEngine.SceneManagement;
2020-02-13 20:23:26 +01:00
2020-02-21 23:36:07 +01:00
namespace QSB.TransformSync
2020-02-15 20:48:02 +01:00
{
2020-02-21 23:36:07 +01:00
public class SectorSync : MonoBehaviour
2020-02-15 20:48:02 +01:00
{
2020-02-21 23:36:07 +01:00
public static SectorSync Instance { get; private set; }
2020-02-21 23:36:07 +01:00
private Sector[] _allSectors;
private MessageHandler<SectorMessage> _sectorHandler;
2020-07-28 15:59:24 +02:00
private readonly Sector.Name[] _sectorWhitelist = {
Sector.Name.BrambleDimension,
Sector.Name.BrittleHollow,
Sector.Name.Comet,
Sector.Name.DarkBramble,
Sector.Name.EyeOfTheUniverse,
Sector.Name.GiantsDeep,
Sector.Name.HourglassTwin_A,
Sector.Name.HourglassTwin_B,
Sector.Name.OrbitalProbeCannon,
Sector.Name.QuantumMoon,
Sector.Name.SunStation,
Sector.Name.TimberHearth,
Sector.Name.TimberMoon,
Sector.Name.VolcanicMoon,
Sector.Name.WhiteHole
};
2020-02-13 21:23:12 +01:00
private void Awake()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
2020-02-15 20:48:02 +01:00
private void Start()
{
2020-02-21 23:36:07 +01:00
Instance = this;
2020-02-13 21:48:23 +01:00
2020-02-21 23:36:07 +01:00
_sectorHandler = new MessageHandler<SectorMessage>();
_sectorHandler.OnClientReceiveMessage += OnClientReceiveMessage;
_sectorHandler.OnServerReceiveMessage += OnServerReceiveMessage;
QSB.Helper.HarmonyHelper.AddPrefix<SectorDetector>("AddSector", typeof(Patches), "PreAddSector");
2020-02-13 21:23:12 +01:00
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
_allSectors = null;
}
2020-02-21 23:36:07 +01:00
public void SetSector(uint id, Sector.Name sectorName)
2020-02-15 20:48:02 +01:00
{
DebugLog.ToScreen("Gonna set sector");
PlayerRegistry.GetPlayer(id).ReferenceSector = FindSectorTransform(sectorName);
2020-02-15 20:48:02 +01:00
var msg = new SectorMessage
{
SectorId = (int)sectorName,
SenderId = id
};
2020-02-21 23:36:07 +01:00
_sectorHandler.SendToServer(msg);
}
2020-02-21 23:36:07 +01:00
private Transform FindSectorTransform(Sector.Name sectorName)
2020-02-15 20:48:02 +01:00
{
if (_allSectors == null)
{
_allSectors = FindObjectsOfType<Sector>();
}
2020-07-28 15:59:24 +02:00
return _allSectors
.Where(sector => sectorName == sector.GetName())
.Select(sector => sector.transform)
.FirstOrDefault();
2020-02-13 21:23:12 +01:00
}
2020-02-21 23:36:07 +01:00
private void OnClientReceiveMessage(SectorMessage message)
2020-02-15 20:48:02 +01:00
{
DebugLog.ToScreen("OnClientReceiveMessage SectorSync");
2020-02-13 21:23:12 +01:00
2020-02-21 23:36:07 +01:00
var sectorName = (Sector.Name)message.SectorId;
var sectorTransform = FindSectorTransform(sectorName);
2020-02-13 21:23:12 +01:00
2020-02-15 20:48:02 +01:00
if (sectorTransform == null)
{
DebugLog.ToScreen("Sector", sectorName, "not found");
2020-02-13 21:23:12 +01:00
return;
}
DebugLog.ToScreen("Found sector", sectorName, ", setting for", message.SenderId);
PlayerRegistry.GetPlayer(message.SenderId).ReferenceSector = sectorTransform;
2020-02-13 21:23:12 +01:00
}
2020-02-13 21:48:23 +01:00
2020-02-21 23:36:07 +01:00
private void OnServerReceiveMessage(SectorMessage message)
2020-02-15 20:48:02 +01:00
{
DebugLog.ToScreen("OnServerReceiveMessage SectorSync");
2020-02-21 23:36:07 +01:00
_sectorHandler.SendToAll(message);
2020-02-13 21:48:23 +01:00
}
2020-02-15 20:48:02 +01:00
private static class Patches
{
private static void PreAddSector(Sector sector, DynamicOccupant ____occupantType)
2020-02-15 20:48:02 +01:00
{
if (!Instance._sectorWhitelist.Contains(sector.GetName()))
2020-02-15 20:48:02 +01:00
{
return;
}
2020-02-21 23:36:07 +01:00
if (____occupantType == DynamicOccupant.Player && PlayerTransformSync.LocalInstance != null)
2020-02-15 20:48:02 +01:00
{
PlayerTransformSync.LocalInstance?.EnterSector(sector);
PlayerCameraSync.LocalInstance?.EnterSector(sector);
return;
}
if (____occupantType == DynamicOccupant.Ship && ShipTransformSync.LocalInstance != null)
{
ShipTransformSync.LocalInstance.EnterSector(sector);
2020-02-13 21:48:23 +01:00
}
}
}
2020-02-15 20:48:02 +01:00
2020-02-13 20:23:26 +01:00
}
}