Skip to content

Commit

Permalink
Merge pull request #144 from Baldie-dev/master
Browse files Browse the repository at this point in the history
Building on Remote Star system + unloading factories when client leaves star system
  • Loading branch information
hubastard authored Apr 14, 2021
2 parents 7f2167d + 222fd39 commit c442237
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NebulaModel.Attributes;
using HarmonyLib;
using NebulaModel.Attributes;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets.Factory;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 13 additions & 1 deletion NebulaHost/PacketProcessors/Planet/PlanetDataRequestProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NebulaModel.Attributes;
using HarmonyLib;
using NebulaModel.Attributes;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets.Planet;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}
}
1 change: 1 addition & 0 deletions NebulaPatcher/NebulaPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Compile Include="Patches\Dynamic\GameSave_Patch.cs" />
<Compile Include="Patches\Dynamic\GameScenarioLogic_Patch.cs" />
<Compile Include="Patches\Dynamic\GameStatData_Patch.cs" />
<Compile Include="Patches\Dynamic\PlanetData_Patch.cs" />
<Compile Include="Patches\Dynamic\PlanetFactory_Patch.cs" />
<Compile Include="Patches\Dynamic\Debugging.cs" />
<Compile Include="Patches\Dynamic\ArriveLeavePlanet_Patch.cs" />
Expand Down
21 changes: 21 additions & 0 deletions NebulaPatcher/Patches/Dynamic/GameData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
}
}
}
75 changes: 75 additions & 0 deletions NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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()
{
//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;
}
}
}
}
21 changes: 21 additions & 0 deletions NebulaPatcher/Patches/Transpilers/PlayerAction_Build_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ static IEnumerable<CodeInstruction> 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++)
{
Expand Down

0 comments on commit c442237

Please sign in to comment.