From 7d505e2750fef13035c9e0fc4be9d9c872b705e7 Mon Sep 17 00:00:00 2001 From: starfish Date: Thu, 23 Dec 2021 05:40:29 +0800 Subject: [PATCH 1/4] Sync dyson sphere status sync dysonSphere.energyReqCurrentTick and other parameters so that ray receivers on client side can have the correct output --- .../Universe/DysonSphereStatusPacket.cs | 19 +++++++++ .../Universe/DysonSphereStatusProcessor.cs | 23 +++++++++++ NebulaNetwork/Server.cs | 19 ++++++++- .../Patches/Dynamic/DysonSphere_Patch.cs | 41 +++++++++++++++++++ .../Transpilers/PowerSystem_Transpiler.cs | 32 +++++++++++++++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 NebulaModel/Packets/Universe/DysonSphereStatusPacket.cs create mode 100644 NebulaNetwork/PacketProcessors/Universe/DysonSphereStatusProcessor.cs diff --git a/NebulaModel/Packets/Universe/DysonSphereStatusPacket.cs b/NebulaModel/Packets/Universe/DysonSphereStatusPacket.cs new file mode 100644 index 000000000..503b1d8cd --- /dev/null +++ b/NebulaModel/Packets/Universe/DysonSphereStatusPacket.cs @@ -0,0 +1,19 @@ +namespace NebulaModel.Packets.Universe +{ + public class DysonSphereStatusPacket + { + public int StarIndex { get; set; } + public float GrossRadius { get; set; } + public long EnergyReqCurrentTick { get; set; } + public long EnergyGenCurrentTick { get; set; } + + public DysonSphereStatusPacket() {} + public DysonSphereStatusPacket(DysonSphere dysonSphere) + { + StarIndex = dysonSphere.starData.index; + GrossRadius = dysonSphere.grossRadius; + EnergyReqCurrentTick = dysonSphere.energyReqCurrentTick; + EnergyGenCurrentTick = dysonSphere.energyGenCurrentTick; + } + } +} diff --git a/NebulaNetwork/PacketProcessors/Universe/DysonSphereStatusProcessor.cs b/NebulaNetwork/PacketProcessors/Universe/DysonSphereStatusProcessor.cs new file mode 100644 index 000000000..664230bd2 --- /dev/null +++ b/NebulaNetwork/PacketProcessors/Universe/DysonSphereStatusProcessor.cs @@ -0,0 +1,23 @@ +using NebulaAPI; +using NebulaModel.Networking; +using NebulaModel.Packets; +using NebulaModel.Packets.Universe; + +namespace NebulaNetwork.PacketProcessors.Universe +{ + [RegisterPacketProcessor] + internal class DysonSphereStatusProcessor : PacketProcessor + { + public override void ProcessPacket(DysonSphereStatusPacket packet, NebulaConnection conn) + { + DysonSphere dysonSphere = GameMain.data.dysonSpheres[packet.StarIndex]; + if (IsHost || dysonSphere == null) + { + return; + } + dysonSphere.grossRadius = packet.GrossRadius; + dysonSphere.energyReqCurrentTick = packet.EnergyReqCurrentTick; + dysonSphere.energyGenCurrentTick = packet.EnergyGenCurrentTick; + } + } +} \ No newline at end of file diff --git a/NebulaNetwork/Server.cs b/NebulaNetwork/Server.cs index 8d211c358..bf6ad22cd 100644 --- a/NebulaNetwork/Server.cs +++ b/NebulaNetwork/Server.cs @@ -6,6 +6,7 @@ using NebulaModel.Networking.Serialization; using NebulaModel.Packets.GameHistory; using NebulaModel.Packets.GameStates; +using NebulaModel.Packets.Universe; using NebulaModel.Utils; using NebulaWorld; using System.Net.Sockets; @@ -22,11 +23,13 @@ public class Server : NetworkProvider private const float GAME_RESEARCH_UPDATE_INTERVAL = 2; private const float STATISTICS_UPDATE_INTERVAL = 1; private const float LAUNCH_UPDATE_INTERVAL = 2; + private const float DYSONSPHERE_UPDATE_INTERVAL = 5; private float gameStateUpdateTimer = 0; private float gameResearchHashUpdateTimer = 0; private float productionStatisticsUpdateTimer = 0; - private float dysonLaunchUpateTimer = 0; + private float dysonLaunchUpateTimer = 1; + private float dysonSphereUpdateTimer = 0; private WebSocketServer socket; @@ -125,6 +128,7 @@ public override void Update() gameResearchHashUpdateTimer += Time.deltaTime; productionStatisticsUpdateTimer += Time.deltaTime; dysonLaunchUpateTimer += Time.deltaTime; + dysonSphereUpdateTimer += Time.deltaTime; if (gameStateUpdateTimer > GAME_STATE_UPDATE_INTERVAL) { @@ -154,6 +158,19 @@ public override void Update() Multiplayer.Session.Launch.SendBroadcastIfNeeded(); } + if (dysonSphereUpdateTimer > DYSONSPHERE_UPDATE_INTERVAL) + { + dysonSphereUpdateTimer = 0; + DysonSphere[] dysonSpheres = GameMain.data.dysonSpheres; + for (int i = 0; i < dysonSpheres.Length; i++) + { + if (dysonSpheres[i] != null) + { + SendPacketToStar(new DysonSphereStatusPacket(dysonSpheres[i]), dysonSpheres[i].starData.id); + } + } + } + PacketProcessor.ProcessPacketQueue(); } diff --git a/NebulaPatcher/Patches/Dynamic/DysonSphere_Patch.cs b/NebulaPatcher/Patches/Dynamic/DysonSphere_Patch.cs index 012339d89..2f1091ec6 100644 --- a/NebulaPatcher/Patches/Dynamic/DysonSphere_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/DysonSphere_Patch.cs @@ -9,6 +9,47 @@ namespace NebulaPatcher.Patches.Dynamic [HarmonyPatch(typeof(DysonSphere))] internal class DysonSphere_Patch { + [HarmonyPrefix] + [HarmonyPatch(nameof(DysonSphere.BeforeGameTick))] + public static bool BeforeGameTick_Prefix(DysonSphere __instance, long times) + { + if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost) + { + return true; + } + //Update swarm and layer energy generation stats every 120 frames + if (times % 120 == 0) + { + __instance.swarm.energyGenCurrentTick = __instance.swarm.sailCount * __instance.energyGenPerSail; + for (int i = 0; i < 10; i++) + { + DysonSphereLayer dysonSphereLayer = __instance.layersSorted[i]; + if (dysonSphereLayer != null) + { + dysonSphereLayer.energyGenCurrentTick = 0L; + DysonNode[] nodePool = dysonSphereLayer.nodePool; + DysonShell[] shellPool = dysonSphereLayer.shellPool; + for (int j = 1; j < dysonSphereLayer.nodeCursor; j++) + { + if (nodePool[j] != null && nodePool[j].id == j) + { + dysonSphereLayer.energyGenCurrentTick += nodePool[j].EnergyGenCurrentTick(__instance.energyGenPerNode, __instance.energyGenPerFrame, 0L); + } + } + for (int k = 1; k < dysonSphereLayer.shellCursor; k++) + { + if (shellPool[k] != null && shellPool[k].id == k) + { + dysonSphereLayer.energyGenCurrentTick += shellPool[k].cellPoint * __instance.energyGenPerShell; + } + } + } + } + } + //Sync other Dyson sphere status related to ray receivers on client side by DysonSphereStatusPacket + return false; + } + [HarmonyPrefix] [HarmonyPatch(nameof(DysonSphere.AddLayer))] public static bool AddLayer_Prefix(DysonSphere __instance, float orbitRadius, Quaternion orbitRotation, float orbitAngularSpeed) diff --git a/NebulaPatcher/Patches/Transpilers/PowerSystem_Transpiler.cs b/NebulaPatcher/Patches/Transpilers/PowerSystem_Transpiler.cs index cef6345eb..3cbbd6d73 100644 --- a/NebulaPatcher/Patches/Transpilers/PowerSystem_Transpiler.cs +++ b/NebulaPatcher/Patches/Transpilers/PowerSystem_Transpiler.cs @@ -1,6 +1,7 @@ using HarmonyLib; using NebulaModel.Packets.Factory.PowerTower; using NebulaWorld; +using System; using System.Collections.Generic; using System.Reflection.Emit; @@ -126,5 +127,36 @@ public static IEnumerable PowerSystem_GameTick_Transpiler(IEnum }) .InstructionEnumeration(); } + + [HarmonyTranspiler] + [HarmonyPatch(nameof(PowerSystem.RequestDysonSpherePower))] + public static IEnumerable PowerSystem_RequestDysonSpherePower_Transpiler(IEnumerable instructions) + { + //Prevent dysonSphere.energyReqCurrentTick from changing on the client side + //Change: if (this.dysonSphere != null) + //To: if (this.dysonSphere != null && (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)) + try + { + CodeMatcher codeMatcher = new CodeMatcher(instructions) + .MatchForward(true, + new CodeMatch(OpCodes.Ldarg_0), + new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(PowerSystem), "dysonSphere")), + new CodeMatch(OpCodes.Brfalse) //IL #93 + ); + object label = codeMatcher.Instruction.operand; + codeMatcher.Advance(1) + .InsertAndAdvance(HarmonyLib.Transpilers.EmitDelegate>(() => + { + return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost; + })) + .InsertAndAdvance(new CodeInstruction(OpCodes.Brfalse_S, label)); + return codeMatcher.InstructionEnumeration(); + } + catch + { + NebulaModel.Logger.Log.Error("PowerSystem.RequestDysonSpherePower_Transpiler failed. Mod version not compatible with game version."); + return instructions; + } + } } } From 777bd5333fed9bbcd4f24bc4d7c5cdda4aa288aa Mon Sep 17 00:00:00 2001 From: starfish Date: Thu, 23 Dec 2021 07:03:06 +0800 Subject: [PATCH 2/4] Fix AnimateRenderers, remove obsolete class NebulaAnimationState --- .../DataStructures/DataStructureExtensions.cs | 17 ------------ .../DataStructures/NebulaAnimationState.cs | 26 ------------------- .../Remote/RemotePlayerAnimation.cs | 2 ++ 3 files changed, 2 insertions(+), 43 deletions(-) delete mode 100644 NebulaModel/DataStructures/DataStructureExtensions.cs delete mode 100644 NebulaModel/DataStructures/NebulaAnimationState.cs diff --git a/NebulaModel/DataStructures/DataStructureExtensions.cs b/NebulaModel/DataStructures/DataStructureExtensions.cs deleted file mode 100644 index 2552f8104..000000000 --- a/NebulaModel/DataStructures/DataStructureExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using UnityEngine; - -namespace NebulaModel.DataStructures -{ - public static class AnimationStateExtensions - { - public static NebulaAnimationState ToNebula(this AnimationState state) - { - return new NebulaAnimationState() - { - Enabled = state.enabled, - Weight = state.weight, - Speed = state.speed, - }; - } - } -} diff --git a/NebulaModel/DataStructures/NebulaAnimationState.cs b/NebulaModel/DataStructures/NebulaAnimationState.cs deleted file mode 100644 index 574cb2d21..000000000 --- a/NebulaModel/DataStructures/NebulaAnimationState.cs +++ /dev/null @@ -1,26 +0,0 @@ -using NebulaAPI; - -namespace NebulaModel.DataStructures -{ - [RegisterNestedType] - public struct NebulaAnimationState : INetSerializable - { - public float Weight { get; set; } - public float Speed { get; set; } - public bool Enabled { get; set; } - - public void Serialize(INetDataWriter writer) - { - writer.Put(Weight); - writer.Put(Speed); - writer.Put(Enabled); - } - - public void Deserialize(INetDataReader reader) - { - Weight = reader.GetFloat(); - Speed = reader.GetFloat(); - Enabled = reader.GetBool(); - } - } -} diff --git a/NebulaWorld/MonoBehaviours/Remote/RemotePlayerAnimation.cs b/NebulaWorld/MonoBehaviours/Remote/RemotePlayerAnimation.cs index f107322d9..5acf4a81b 100644 --- a/NebulaWorld/MonoBehaviours/Remote/RemotePlayerAnimation.cs +++ b/NebulaWorld/MonoBehaviours/Remote/RemotePlayerAnimation.cs @@ -128,6 +128,8 @@ public void AnimateSailState(PlayerAnimator animator) public void AnimateRenderers(PlayerAnimator animator) { animator.player.mechaArmorModel.inst_armor_mat.SetVector("_InitPositionSet", transform.position); + animator.player.mechaArmorModel.inst_part_ar_mat.SetVector("_InitPositionSet", transform.position); + animator.player.mechaArmorModel.inst_part_sk_mat.SetVector("_InitPositionSet", transform.position); } } } From 6a52fb37acc8eae72f5f64f3a94ff4cf458d56b5 Mon Sep 17 00:00:00 2001 From: starfish Date: Thu, 23 Dec 2021 07:13:21 +0800 Subject: [PATCH 3/4] Fix item duplication when canceling manual research --- .../Patches/Dynamic/GameHistoryData_Patch.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/NebulaPatcher/Patches/Dynamic/GameHistoryData_Patch.cs b/NebulaPatcher/Patches/Dynamic/GameHistoryData_Patch.cs index 95fa29c15..98552ff59 100644 --- a/NebulaPatcher/Patches/Dynamic/GameHistoryData_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/GameHistoryData_Patch.cs @@ -148,18 +148,5 @@ public static bool UnlockTech_Prefix() //Wait for the authoritative packet for unlocking tech features in multiplayer for clients return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.History.IsIncomingRequest; } - - [HarmonyPrefix] - [HarmonyPatch(nameof(GameHistoryData.RemoveTechInQueue))] - public static void RemoveTechInQueue_Prefix(int index, out int __state) - { - __state = GameMain.history.techQueue[index]; - if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost) - { - //we need to know which itemtypes are currently needed for refunds, so trigger refund before cancelling own research - Multiplayer.Session.Network.PlayerManager.SendTechRefundPackagesToClients(__state); - } - Log.Info($"RemoveTechInQueue: remove tech at index {index} with techId { __state}"); - } } } \ No newline at end of file From 48420dd1837fb4b6df2e2d0012714fa9991863da Mon Sep 17 00:00:00 2001 From: Chris Yeninas <844685+PhantomGamers@users.noreply.github.com> Date: Sat, 25 Dec 2021 13:30:20 -0500 Subject: [PATCH 4/4] 0.7.6 --- CHANGELOG.md | 6 ++++++ version.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17bd22aa5..6de39c7fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ ## Changelog +0.7.6: + +- @starfi5h: Added syncing of ray receiver output +- @starfi5h: Fixed lighting of remote players +- @starfi5h: Fixed clients receiving duplicate items when cancelling manual research + 0.7.5: - @sp00ktober: Fixed error caused by warning system introduced in previous update diff --git a/version.json b/version.json index bd07bfffc..12508e25a 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "0.7.5", + "version": "0.7.6", "assemblyVersion": { "precision": "build" },