From 447f41588853a9a187b542e85c2fa9d6f5083b41 Mon Sep 17 00:00:00 2001
From: Lukas Michalec <79051050+Baldie-dev@users.noreply.github.com>
Date: Wed, 14 Apr 2021 21:02:50 +0200
Subject: [PATCH 1/2] Unloading factories, loading planets, building on remote
star systems
---
.../Entity/BuildEntityRequestProcessor.cs | 8 +-
.../Planet/PlanetDataRequestProcessor.cs | 14 +++-
NebulaPatcher/NebulaPatcher.csproj | 1 +
.../Patches/Dynamic/GameData_Patch.cs | 21 +++++
.../Patches/Dynamic/PlanetData_Patch.cs | 76 +++++++++++++++++++
.../Transpilers/PlayerAction_Build_Patch.cs | 21 +++++
6 files changed, 139 insertions(+), 2 deletions(-)
create mode 100644 NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
diff --git a/NebulaHost/PacketProcessors/Factory/Entity/BuildEntityRequestProcessor.cs b/NebulaHost/PacketProcessors/Factory/Entity/BuildEntityRequestProcessor.cs
index 14fd0ab00..ddc121806 100644
--- a/NebulaHost/PacketProcessors/Factory/Entity/BuildEntityRequestProcessor.cs
+++ b/NebulaHost/PacketProcessors/Factory/Entity/BuildEntityRequestProcessor.cs
@@ -1,4 +1,5 @@
-using NebulaModel.Attributes;
+using HarmonyLib;
+using NebulaModel.Attributes;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets.Factory;
@@ -31,6 +32,11 @@ public void ProcessPacket(BuildEntityRequest packet, NebulaConnection conn)
planet.audio = new PlanetAudio(planet);
planet.audio.Init();
+
+ if (AccessTools.Field(typeof(CargoTraffic), "beltRenderingBatch").GetValue(planet.factory.cargoTraffic) == null)
+ {
+ planet.factory.cargoTraffic.CreateRenderingBatches();
+ }
}
planet.factory.BuildFinally(GameMain.mainPlayer, packet.PrebuildId);
diff --git a/NebulaHost/PacketProcessors/Planet/PlanetDataRequestProcessor.cs b/NebulaHost/PacketProcessors/Planet/PlanetDataRequestProcessor.cs
index 8139016e7..e79fbec8f 100644
--- a/NebulaHost/PacketProcessors/Planet/PlanetDataRequestProcessor.cs
+++ b/NebulaHost/PacketProcessors/Planet/PlanetDataRequestProcessor.cs
@@ -1,4 +1,5 @@
-using NebulaModel.Attributes;
+using HarmonyLib;
+using NebulaModel.Attributes;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets.Planet;
@@ -33,6 +34,11 @@ public void ProcessPacket(PlanetDataRequest packet, NebulaConnection conn)
planet.aux = new PlanetAuxData(planet);
planetAlgorithm.GenerateTerrain(planet.mod_x, planet.mod_y);
planetAlgorithm.CalcWaterPercent();
+
+ //Load planet meshes and register callback to unload unneccessary stuff
+ planet.wanted = true;
+ planet.onLoaded += OnActivePlanetLoaded;
+ PlanetModelingManager.modPlanetReqList.Enqueue(planet);
}
if (planet.factory == null)
@@ -53,5 +59,11 @@ public void ProcessPacket(PlanetDataRequest packet, NebulaConnection conn)
conn.SendPacket(new PlanetDataResponse(planetDataToReturn));
}
+
+ public void OnActivePlanetLoaded(PlanetData planet)
+ {
+ planet.Unload();
+ planet.onLoaded -= OnActivePlanetLoaded;
+ }
}
}
diff --git a/NebulaPatcher/NebulaPatcher.csproj b/NebulaPatcher/NebulaPatcher.csproj
index 12453fa77..f6d638a5e 100644
--- a/NebulaPatcher/NebulaPatcher.csproj
+++ b/NebulaPatcher/NebulaPatcher.csproj
@@ -46,6 +46,7 @@
+
diff --git a/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs b/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs
index 5b4d6b685..7e4a5b0a0 100644
--- a/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs
+++ b/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs
@@ -165,5 +165,26 @@ public static void OnDraw_Prefix()
SimulatedWorld.OnDronesDraw();
}
}
+
+ [HarmonyPrefix]
+ [HarmonyPatch("LeaveStar")]
+ public static void LeaveStar_Prefix(GameData __instance)
+ {
+ //Client should unload all factories once they leave the star system
+ if (SimulatedWorld.Initialized && !LocalPlayer.IsMasterClient)
+ {
+ for (int i = 0; i < __instance.localStar.planetCount; i++)
+ {
+ if (__instance.localStar.planets != null && __instance.localStar.planets[i] != null)
+ {
+ if (__instance.localStar.planets[i].factory != null)
+ {
+ __instance.localStar.planets[i].factory.Free();
+ __instance.localStar.planets[i].factory = null;
+ }
+ }
+ }
+ }
+ }
}
}
diff --git a/NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs b/NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
new file mode 100644
index 000000000..908b2f54f
--- /dev/null
+++ b/NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
@@ -0,0 +1,76 @@
+using HarmonyLib;
+using NebulaWorld;
+
+namespace NebulaPatcher.Patches.Dynamic
+{
+ [HarmonyPatch(typeof(PlanetData))]
+ class PlanetData_Patch
+ {
+ [HarmonyPrefix]
+ [HarmonyPatch("UnloadMeshes")]
+ public static bool UnloadMeshes_Prefix(PlanetData __instance)
+ {
+ //Host should not unload planet meshes, since he need to permorm all terrain operations
+ if (SimulatedWorld.Initialized && LocalPlayer.IsMasterClient)
+ {
+ //Do not unload meshes, just hide them so it is not visible
+ UnloadVisuals(__instance);
+ return false;
+ }
+
+ return true;
+ }
+
+ [HarmonyPrefix]
+ [HarmonyPatch("UnloadData")]
+ public static bool UnloadData_Prefix()
+ {
+ //TODO: FIX: This causes that atmosphere dissapear
+ //Host should not unload planet data, since he need to permorm all operations from users
+ if (SimulatedWorld.Initialized && LocalPlayer.IsMasterClient)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ public static void UnloadVisuals(PlanetData __instance)
+ {
+ if (__instance.gameObject != null)
+ {
+ __instance.gameObject.SetActive(false);
+ }
+ if (__instance.terrainMaterial != null)
+ {
+ UnityEngine.Object.Destroy(__instance.terrainMaterial);
+ __instance.terrainMaterial = null;
+ }
+ if (__instance.oceanMaterial != null)
+ {
+ UnityEngine.Object.Destroy(__instance.oceanMaterial);
+ __instance.oceanMaterial = null;
+ }
+ if (__instance.atmosMaterial != null)
+ {
+ UnityEngine.Object.Destroy(__instance.atmosMaterial);
+ __instance.atmosMaterial = null;
+ }
+ if (__instance.minimapMaterial != null)
+ {
+ UnityEngine.Object.Destroy(__instance.minimapMaterial);
+ __instance.minimapMaterial = null;
+ }
+ if (__instance.reformMaterial0 != null)
+ {
+ UnityEngine.Object.Destroy(__instance.reformMaterial0);
+ __instance.reformMaterial0 = null;
+ }
+ if (__instance.reformMaterial1 != null)
+ {
+ UnityEngine.Object.Destroy(__instance.reformMaterial1);
+ __instance.reformMaterial1 = null;
+ }
+ }
+ }
+}
diff --git a/NebulaPatcher/Patches/Transpilers/PlayerAction_Build_Patch.cs b/NebulaPatcher/Patches/Transpilers/PlayerAction_Build_Patch.cs
index 95d7fdb29..1892c599c 100644
--- a/NebulaPatcher/Patches/Transpilers/PlayerAction_Build_Patch.cs
+++ b/NebulaPatcher/Patches/Transpilers/PlayerAction_Build_Patch.cs
@@ -73,6 +73,27 @@ static IEnumerable CheckBuildConditions_Transpiler(ILGenerator
}
}
+ //Apply Ignoring if inserter match the planet grid check
+ for (i = 5; i < codes.Count - 2; i++)
+ {
+ if (codes[i].opcode == OpCodes.Brfalse &&
+ codes[i + 1].opcode == OpCodes.Ldloca_S &&
+ codes[i - 1].opcode == OpCodes.Ldloc_S &&
+ codes[i - 2].opcode == OpCodes.Stfld &&
+ codes[i - 3].opcode == OpCodes.Call &&
+ codes[i - 4].opcode == OpCodes.Ldfld &&
+ codes[i - 5].opcode == OpCodes.Ldloc_3 &&
+ codes[i + 2].opcode == OpCodes.Ldloc_3)
+ {
+ Label targetLabel = (Label)codes[i].operand;
+ codes.InsertRange(i+1, new CodeInstruction[] {
+ new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(FactoryManager), "get_IgnoreBasicBuildConditionChecks")),
+ new CodeInstruction(OpCodes.Brtrue_S, targetLabel),
+ });
+ break;
+ }
+ }
+
//Apply ignoring of 3 ground checks
for (i = 9; i < codes.Count - 4; i++)
{
From 222fd390ae4cbb2c580c30c1ebedf4f502436845 Mon Sep 17 00:00:00 2001
From: Lukas Michalec <79051050+Baldie-dev@users.noreply.github.com>
Date: Wed, 14 Apr 2021 21:15:27 +0200
Subject: [PATCH 2/2] deleting old comment
---
NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs | 1 -
1 file changed, 1 deletion(-)
diff --git a/NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs b/NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
index 908b2f54f..7ea26fe72 100644
--- a/NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
+++ b/NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
@@ -25,7 +25,6 @@ public static bool UnloadMeshes_Prefix(PlanetData __instance)
[HarmonyPatch("UnloadData")]
public static bool UnloadData_Prefix()
{
- //TODO: FIX: This causes that atmosphere dissapear
//Host should not unload planet data, since he need to permorm all operations from users
if (SimulatedWorld.Initialized && LocalPlayer.IsMasterClient)
{