Skip to content

Commit c442237

Browse files
authored
Merge pull request #144 from Baldie-dev/master
Building on Remote Star system + unloading factories when client leaves star system
2 parents 7f2167d + 222fd39 commit c442237

6 files changed

Lines changed: 138 additions & 2 deletions

File tree

NebulaHost/PacketProcessors/Factory/Entity/BuildEntityRequestProcessor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NebulaModel.Attributes;
1+
using HarmonyLib;
2+
using NebulaModel.Attributes;
23
using NebulaModel.Logger;
34
using NebulaModel.Networking;
45
using NebulaModel.Packets.Factory;
@@ -31,6 +32,11 @@ public void ProcessPacket(BuildEntityRequest packet, NebulaConnection conn)
3132

3233
planet.audio = new PlanetAudio(planet);
3334
planet.audio.Init();
35+
36+
if (AccessTools.Field(typeof(CargoTraffic), "beltRenderingBatch").GetValue(planet.factory.cargoTraffic) == null)
37+
{
38+
planet.factory.cargoTraffic.CreateRenderingBatches();
39+
}
3440
}
3541

3642
planet.factory.BuildFinally(GameMain.mainPlayer, packet.PrebuildId);

NebulaHost/PacketProcessors/Planet/PlanetDataRequestProcessor.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NebulaModel.Attributes;
1+
using HarmonyLib;
2+
using NebulaModel.Attributes;
23
using NebulaModel.Logger;
34
using NebulaModel.Networking;
45
using NebulaModel.Packets.Planet;
@@ -33,6 +34,11 @@ public void ProcessPacket(PlanetDataRequest packet, NebulaConnection conn)
3334
planet.aux = new PlanetAuxData(planet);
3435
planetAlgorithm.GenerateTerrain(planet.mod_x, planet.mod_y);
3536
planetAlgorithm.CalcWaterPercent();
37+
38+
//Load planet meshes and register callback to unload unneccessary stuff
39+
planet.wanted = true;
40+
planet.onLoaded += OnActivePlanetLoaded;
41+
PlanetModelingManager.modPlanetReqList.Enqueue(planet);
3642
}
3743

3844
if (planet.factory == null)
@@ -53,5 +59,11 @@ public void ProcessPacket(PlanetDataRequest packet, NebulaConnection conn)
5359

5460
conn.SendPacket(new PlanetDataResponse(planetDataToReturn));
5561
}
62+
63+
public void OnActivePlanetLoaded(PlanetData planet)
64+
{
65+
planet.Unload();
66+
planet.onLoaded -= OnActivePlanetLoaded;
67+
}
5668
}
5769
}

NebulaPatcher/NebulaPatcher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Compile Include="Patches\Dynamic\GameSave_Patch.cs" />
4747
<Compile Include="Patches\Dynamic\GameScenarioLogic_Patch.cs" />
4848
<Compile Include="Patches\Dynamic\GameStatData_Patch.cs" />
49+
<Compile Include="Patches\Dynamic\PlanetData_Patch.cs" />
4950
<Compile Include="Patches\Dynamic\PlanetFactory_Patch.cs" />
5051
<Compile Include="Patches\Dynamic\Debugging.cs" />
5152
<Compile Include="Patches\Dynamic\ArriveLeavePlanet_Patch.cs" />

NebulaPatcher/Patches/Dynamic/GameData_Patch.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,26 @@ public static void OnDraw_Prefix()
165165
SimulatedWorld.OnDronesDraw();
166166
}
167167
}
168+
169+
[HarmonyPrefix]
170+
[HarmonyPatch("LeaveStar")]
171+
public static void LeaveStar_Prefix(GameData __instance)
172+
{
173+
//Client should unload all factories once they leave the star system
174+
if (SimulatedWorld.Initialized && !LocalPlayer.IsMasterClient)
175+
{
176+
for (int i = 0; i < __instance.localStar.planetCount; i++)
177+
{
178+
if (__instance.localStar.planets != null && __instance.localStar.planets[i] != null)
179+
{
180+
if (__instance.localStar.planets[i].factory != null)
181+
{
182+
__instance.localStar.planets[i].factory.Free();
183+
__instance.localStar.planets[i].factory = null;
184+
}
185+
}
186+
}
187+
}
188+
}
168189
}
169190
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using HarmonyLib;
2+
using NebulaWorld;
3+
4+
namespace NebulaPatcher.Patches.Dynamic
5+
{
6+
[HarmonyPatch(typeof(PlanetData))]
7+
class PlanetData_Patch
8+
{
9+
[HarmonyPrefix]
10+
[HarmonyPatch("UnloadMeshes")]
11+
public static bool UnloadMeshes_Prefix(PlanetData __instance)
12+
{
13+
//Host should not unload planet meshes, since he need to permorm all terrain operations
14+
if (SimulatedWorld.Initialized && LocalPlayer.IsMasterClient)
15+
{
16+
//Do not unload meshes, just hide them so it is not visible
17+
UnloadVisuals(__instance);
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
[HarmonyPrefix]
25+
[HarmonyPatch("UnloadData")]
26+
public static bool UnloadData_Prefix()
27+
{
28+
//Host should not unload planet data, since he need to permorm all operations from users
29+
if (SimulatedWorld.Initialized && LocalPlayer.IsMasterClient)
30+
{
31+
return false;
32+
}
33+
34+
return true;
35+
}
36+
37+
public static void UnloadVisuals(PlanetData __instance)
38+
{
39+
if (__instance.gameObject != null)
40+
{
41+
__instance.gameObject.SetActive(false);
42+
}
43+
if (__instance.terrainMaterial != null)
44+
{
45+
UnityEngine.Object.Destroy(__instance.terrainMaterial);
46+
__instance.terrainMaterial = null;
47+
}
48+
if (__instance.oceanMaterial != null)
49+
{
50+
UnityEngine.Object.Destroy(__instance.oceanMaterial);
51+
__instance.oceanMaterial = null;
52+
}
53+
if (__instance.atmosMaterial != null)
54+
{
55+
UnityEngine.Object.Destroy(__instance.atmosMaterial);
56+
__instance.atmosMaterial = null;
57+
}
58+
if (__instance.minimapMaterial != null)
59+
{
60+
UnityEngine.Object.Destroy(__instance.minimapMaterial);
61+
__instance.minimapMaterial = null;
62+
}
63+
if (__instance.reformMaterial0 != null)
64+
{
65+
UnityEngine.Object.Destroy(__instance.reformMaterial0);
66+
__instance.reformMaterial0 = null;
67+
}
68+
if (__instance.reformMaterial1 != null)
69+
{
70+
UnityEngine.Object.Destroy(__instance.reformMaterial1);
71+
__instance.reformMaterial1 = null;
72+
}
73+
}
74+
}
75+
}

NebulaPatcher/Patches/Transpilers/PlayerAction_Build_Patch.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ static IEnumerable<CodeInstruction> CheckBuildConditions_Transpiler(ILGenerator
7373
}
7474
}
7575

76+
//Apply Ignoring if inserter match the planet grid check
77+
for (i = 5; i < codes.Count - 2; i++)
78+
{
79+
if (codes[i].opcode == OpCodes.Brfalse &&
80+
codes[i + 1].opcode == OpCodes.Ldloca_S &&
81+
codes[i - 1].opcode == OpCodes.Ldloc_S &&
82+
codes[i - 2].opcode == OpCodes.Stfld &&
83+
codes[i - 3].opcode == OpCodes.Call &&
84+
codes[i - 4].opcode == OpCodes.Ldfld &&
85+
codes[i - 5].opcode == OpCodes.Ldloc_3 &&
86+
codes[i + 2].opcode == OpCodes.Ldloc_3)
87+
{
88+
Label targetLabel = (Label)codes[i].operand;
89+
codes.InsertRange(i+1, new CodeInstruction[] {
90+
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(FactoryManager), "get_IgnoreBasicBuildConditionChecks")),
91+
new CodeInstruction(OpCodes.Brtrue_S, targetLabel),
92+
});
93+
break;
94+
}
95+
}
96+
7697
//Apply ignoring of 3 ground checks
7798
for (i = 9; i < codes.Count - 4; i++)
7899
{

0 commit comments

Comments
 (0)