Separate handler for client and server

This commit is contained in:
Ricardo Lopes 2020-02-13 21:48:23 +01:00
parent 22b6692642
commit d6e1f4c2a4
3 changed files with 20 additions and 14 deletions

View File

@ -6,10 +6,11 @@ namespace QSB {
protected abstract short type { get; }
public MessageHandler () {
NetworkServer.RegisterHandler(SectorMessage.Type, OnReceiveMessage);
NetworkManager.singleton.client.RegisterHandler(SectorMessage.Type, OnReceiveMessage);
NetworkServer.RegisterHandler(SectorMessage.Type, OnServerReceiveMessage);
NetworkManager.singleton.client.RegisterHandler(SectorMessage.Type, OnClientReceiveMessage);
}
protected abstract void OnReceiveMessage (NetworkMessage netMsg);
protected abstract void OnClientReceiveMessage (NetworkMessage netMsg);
protected abstract void OnServerReceiveMessage (NetworkMessage netMsg);
}
}

View File

@ -26,8 +26,6 @@ namespace QSB {
gameObject.AddComponent<QSBNetworkManager>();
gameObject.AddComponent<NetworkManagerHUD>();
ModHelper.HarmonyHelper.AddPrefix<PlayerSectorDetector>("OnAddSector", typeof(Patches), "OnAddSector");
}
static string JoinAll (params object[] logObjects) {
@ -51,13 +49,5 @@ namespace QSB {
NotificationData data = new NotificationData(NotificationTarget.Player, JoinAll(logObjects), 5f, true);
NotificationManager.SharedInstance.PostNotification(data, false);
}
static class Patches {
static void OnAddSector (Sector sector, PlayerSectorDetector __instance) {
if (NetworkPlayer.localInstance != null) {
NetworkPlayer.localInstance.EnterSector(sector);
}
}
}
}
}

View File

@ -11,6 +11,8 @@ namespace QSB {
void Awake () {
playerSectors = new Dictionary<uint, Transform>();
_allSectors = FindObjectsOfType<Sector>();
QSB.Helper.HarmonyHelper.AddPrefix<PlayerSectorDetector>("OnAddSector", typeof(Patches), "PreAddSector");
}
public static void SetSector (NetworkInstanceId netId, Sector.Name sectorName) {
@ -30,7 +32,7 @@ namespace QSB {
return null;
}
protected override void OnReceiveMessage (NetworkMessage netMsg) {
protected override void OnClientReceiveMessage (NetworkMessage netMsg) {
SectorMessage msg = netMsg.ReadMessage<SectorMessage>();
var sectorName = (Sector.Name) msg.sectorId;
@ -44,5 +46,18 @@ namespace QSB {
QSB.LogToScreen("Found sector", sectorName, ", setting for", msg.senderId);
playerSectors[msg.senderId] = sectorTransform;
}
protected override void OnServerReceiveMessage (NetworkMessage netMsg) {
SectorMessage msg = netMsg.ReadMessage<SectorMessage>();
NetworkServer.SendToAll(MessageType.Sector, msg);
}
static class Patches {
static void PreAddSector (Sector sector, PlayerSectorDetector __instance) {
if (NetworkPlayer.localInstance != null) {
NetworkPlayer.localInstance.EnterSector(sector);
}
}
}
}
}