Skip to content

Commit 53bf264

Browse files
authored
Merge pull request #531 from starfi5h/pr-bugfix-0.8.2
Improve error log and bugfix
2 parents c0901a5 + ac81d2c commit 53bf264

17 files changed

+174
-100
lines changed

NebulaModel/Packets/Universe/DysonBlueprintPacket.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ public class DysonBlueprintPacket
55
public int StarIndex { get; set; }
66
public int LayerId { get; set; }
77
public EDysonBlueprintType BlueprintType {get; set; }
8-
public string StringData { get; set; }
8+
public char[] CharsData { get; set; }
99

1010
public DysonBlueprintPacket() { }
1111
public DysonBlueprintPacket(int starIndex, int layerId, EDysonBlueprintType blueprintType, string stringData)
1212
{
1313
StarIndex = starIndex;
1414
LayerId = layerId;
1515
BlueprintType = blueprintType;
16-
StringData = stringData;
16+
// because string length may exceed maxStringLength in NetSerializer, convert to char array here
17+
CharsData = stringData.ToCharArray();
1718
}
1819
}
1920
}

NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncProcessor.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ public class StationUIInitialSyncProcessor : PacketProcessor<StationUIInitialSyn
1717
public override void ProcessPacket(StationUIInitialSync packet, NebulaConnection conn)
1818
{
1919
StationComponent stationComponent = null;
20-
StationComponent[] gStationPool = GameMain.data.galacticTransport.stationPool;
2120
StationComponent[] stationPool = GameMain.data.galaxy.PlanetById(packet.PlanetId).factory.transport.stationPool;
22-
23-
stationComponent = packet.StationGId > 0 ? gStationPool[packet.StationGId] : stationPool?[packet.StationId];
21+
// Assume the requesting station is on a loaded planet
22+
stationComponent = stationPool?[packet.StationId];
2423

2524
if (stationComponent == null)
2625
{
2726
Log.Error($"StationUIInitialSyncProcessor: Unable to find requested station on planet {packet.PlanetId} with id {packet.StationId} and gid of {packet.StationGId}");
2827
return;
2928
}
29+
if (stationComponent.gid > 0 && stationComponent.gid != packet.StationGId)
30+
{
31+
Log.Error($"StationGid desync! Host:{packet.StationGId} Local:{stationComponent.gid}");
32+
}
3033

3134
stationComponent.tripRangeDrones = packet.TripRangeDrones;
3235
stationComponent.tripRangeShips = packet.TripRangeShips;

NebulaNetwork/PacketProcessors/Logistics/StationUIInitialSyncRequestProcessor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ public override void ProcessPacket(StationUIInitialSyncRequest packet, NebulaCon
2121
}
2222

2323
StationComponent stationComponent;
24-
StationComponent[] gStationPool = GameMain.data.galacticTransport.stationPool;
2524
StationComponent[] stationPool = GameMain.data.galaxy?.PlanetById(packet.PlanetId)?.factory?.transport?.stationPool;
2625

27-
stationComponent = packet.StationGId > 0 ? gStationPool[packet.StationGId] : stationPool?[packet.StationId];
26+
stationComponent = stationPool?[packet.StationId];
2827

2928
if (stationComponent == null)
3029
{
31-
Log.Error($"StationUIInitialSyncRequestProcessor: Unable to find requested station on planet {packet.PlanetId} with id {packet.StationId} and gid of {packet.StationGId}");
30+
Log.Error($"StationUIInitialSyncRequestProcessor: Unable to find requested station on planet {packet.PlanetId} with id {packet.StationId} and gid {packet.StationGId}");
3231
return;
3332
}
3433

NebulaNetwork/PacketProcessors/Logistics/StationUIProcessor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public override void ProcessPacket(StationUI packet, NebulaConnection conn)
2323
packet.ShouldRefund = false;
2424
Multiplayer.Session.StationsUI.UpdateStation(ref packet);
2525

26-
// broadcast to every clients that may have the station loaded.
27-
int starId = GameMain.galaxy.PlanetById(packet.PlanetId)?.star.id ?? -1;
28-
playerManager.SendPacketToStarExcept(packet, starId, conn);
26+
// broadcast to other clients
27+
INebulaPlayer player = playerManager.GetPlayer(conn);
28+
playerManager.SendPacketToOtherPlayers(packet, player);
2929

3030
// as we block the normal method for the client he must run it once he receives this packet.
3131
// but only the one issued the request should get items refund

NebulaNetwork/PacketProcessors/Logistics/StorageUIProcessor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public override void ProcessPacket(StorageUI packet, NebulaConnection conn)
2323
packet.ShouldRefund = false;
2424
Multiplayer.Session.StationsUI.UpdateStorage(packet);
2525

26-
// broadcast to every clients that may have the station loaded.
27-
int starId = GameMain.galaxy.PlanetById(packet.PlanetId)?.star.id ?? -1;
28-
playerManager.SendPacketToStarExcept(packet, starId, conn);
26+
// broadcast to other clients
27+
INebulaPlayer player = playerManager.GetPlayer(conn);
28+
playerManager.SendPacketToOtherPlayers(packet, player);
2929

3030
// as we block some methods for the client he must run it once he receives this packet.
3131
// but only the one issued the request should get items refund
Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NebulaAPI;
1+
using BepInEx;
2+
using NebulaAPI;
23
using NebulaModel.Logger;
34
using NebulaModel.Networking;
45
using NebulaModel.Packets;
@@ -21,50 +22,74 @@ public override void ProcessPacket(PlanetDataRequest packet, NebulaConnection co
2122

2223
foreach (int planetId in packet.PlanetIDs)
2324
{
24-
PlanetData planet = GameMain.galaxy.PlanetById(planetId);
25-
Log.Info($"Returning terrain for {planet.name}");
25+
ThreadingHelper.Instance.StartAsyncInvoke(() =>
26+
{
27+
PlanetCompute(planetId, planetDataToReturn);
28+
return () => Callback(conn, planetDataToReturn, packet.PlanetIDs.Length);
29+
});
30+
}
31+
}
2632

27-
// NOTE: The following has been picked-n-mixed from "PlanetModelingManager.PlanetComputeThreadMain()"
28-
// This method is **costly** - do not run it more than is required!
29-
// It generates the planet on the host and then sends it to the client
33+
public static void OnActivePlanetLoaded(PlanetData planet)
34+
{
35+
planet.Unload();
36+
planet.onLoaded -= OnActivePlanetLoaded;
37+
}
3038

31-
PlanetAlgorithm planetAlgorithm = PlanetModelingManager.Algorithm(planet);
39+
private static void PlanetCompute(int planetId, Dictionary<int, byte[]> planetDataToReturn)
40+
{
41+
PlanetData planet = GameMain.galaxy.PlanetById(planetId);
42+
HighStopwatch highStopwatch = new HighStopwatch();
43+
highStopwatch.Begin();
3244

33-
if (planet.data == null)
34-
{
35-
planet.data = new PlanetRawData(planet.precision);
36-
planet.modData = planet.data.InitModData(planet.modData);
37-
planet.data.CalcVerts();
38-
planet.aux = new PlanetAuxData(planet);
39-
planetAlgorithm.GenerateTerrain(planet.mod_x, planet.mod_y);
40-
planetAlgorithm.CalcWaterPercent();
45+
// NOTE: The following has been picked-n-mixed from "PlanetModelingManager.PlanetComputeThreadMain()"
46+
// This method is **costly** - do not run it more than is required!
47+
// It generates the planet on the host and then sends it to the client
48+
49+
PlanetAlgorithm planetAlgorithm = PlanetModelingManager.Algorithm(planet);
50+
51+
if (planet.data == null)
52+
{
53+
planet.data = new PlanetRawData(planet.precision);
54+
planet.modData = planet.data.InitModData(planet.modData);
55+
planet.data.CalcVerts();
56+
planet.aux = new PlanetAuxData(planet);
57+
planetAlgorithm.GenerateTerrain(planet.mod_x, planet.mod_y);
58+
planetAlgorithm.CalcWaterPercent();
4159

42-
//Load planet meshes and register callback to unload unneccessary stuff
43-
planet.wanted = true;
44-
planet.onLoaded += OnActivePlanetLoaded;
45-
PlanetModelingManager.modPlanetReqList.Enqueue(planet);
60+
//Load planet meshes and register callback to unload unneccessary stuff
61+
planet.wanted = true;
62+
planet.onLoaded += OnActivePlanetLoaded;
63+
PlanetModelingManager.modPlanetReqList.Enqueue(planet);
4664

47-
if (planet.type != EPlanetType.Gas)
48-
{
49-
planetAlgorithm.GenerateVegetables();
50-
planetAlgorithm.GenerateVeins(false);
51-
}
65+
if (planet.type != EPlanetType.Gas)
66+
{
67+
planetAlgorithm.GenerateVegetables();
68+
planetAlgorithm.GenerateVeins(false);
5269
}
70+
}
5371

54-
using (BinaryUtils.Writer writer = new BinaryUtils.Writer())
72+
using (BinaryUtils.Writer writer = new BinaryUtils.Writer())
73+
{
74+
planet.ExportRuntime(writer.BinaryWriter);
75+
lock (planetDataToReturn)
5576
{
56-
planet.ExportRuntime(writer.BinaryWriter);
5777
planetDataToReturn.Add(planetId, writer.CloseAndGetBytes());
5878
}
5979
}
60-
61-
conn.SendPacket(new PlanetDataResponse(planetDataToReturn));
80+
Log.Info($"Returning terrain for {planet.name}, time:{highStopwatch.duration:F5} s");
6281
}
6382

64-
public void OnActivePlanetLoaded(PlanetData planet)
83+
private static void Callback(NebulaConnection conn, Dictionary<int, byte[]> planetDataToReturn, int count)
6584
{
66-
planet.Unload();
67-
planet.onLoaded -= OnActivePlanetLoaded;
85+
lock (planetDataToReturn)
86+
{
87+
if (planetDataToReturn.Count == count)
88+
{
89+
conn.SendPacket(new PlanetDataResponse(planetDataToReturn));
90+
planetDataToReturn.Clear();
91+
}
92+
}
6893
}
6994
}
7095
}

NebulaNetwork/PacketProcessors/Universe/DysonBlueprintProcessor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public override void ProcessPacket(DysonBlueprintPacket packet, NebulaConnection
2020
using (Multiplayer.Session.DysonSpheres.IsIncomingRequest.On())
2121
{
2222
DysonSphereLayer layer = sphere.GetLayer(packet.LayerId);
23-
DysonBlueprintDataIOError err = new DysonBlueprintData().ContentFromBase64String(packet.StringData, packet.BlueprintType, sphere, layer);
23+
string str64Data = new string(packet.CharsData);
24+
DysonBlueprintDataIOError err = new DysonBlueprintData().FromBase64String(str64Data, packet.BlueprintType, sphere, layer);
2425
if (err != DysonBlueprintDataIOError.OK)
2526
{
2627
Log.Warn($"DysonBlueprintData IO error: {err}");

NebulaNetwork/PacketProcessors/Universe/DysonSphereDataProcessor.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,15 @@ public override void ProcessPacket(DysonSphereData packet, NebulaConnection conn
4141

4242
case DysonSphereRespondEvent.Load:
4343
//Failsafe, if client does not have instantiated sphere for the star, it will create dummy one that will be replaced during import
44-
if (GameMain.data.dysonSpheres[packet.StarIndex] == null)
44+
GameMain.data.dysonSpheres[packet.StarIndex] = new DysonSphere();
45+
GameMain.data.statistics.production.Init(GameMain.data);
46+
//Another failsafe, DysonSphere import requires initialized factory statistics
47+
if (GameMain.data.statistics.production.factoryStatPool[0] == null)
4548
{
46-
GameMain.data.dysonSpheres[packet.StarIndex] = new DysonSphere();
47-
GameMain.data.statistics.production.Init(GameMain.data);
48-
//Another failsafe, DysonSphere import requires initialized factory statistics
49-
if (GameMain.data.statistics.production.factoryStatPool[0] == null)
50-
{
51-
GameMain.data.statistics.production.factoryStatPool[0] = new FactoryProductionStat();
52-
GameMain.data.statistics.production.factoryStatPool[0].Init();
53-
}
54-
GameMain.data.dysonSpheres[packet.StarIndex].Init(GameMain.data, GameMain.data.galaxy.stars[packet.StarIndex]);
49+
GameMain.data.statistics.production.factoryStatPool[0] = new FactoryProductionStat();
50+
GameMain.data.statistics.production.factoryStatPool[0].Init();
5551
}
52+
GameMain.data.dysonSpheres[packet.StarIndex].Init(GameMain.data, GameMain.data.galaxy.stars[packet.StarIndex]);
5653

5754
using (BinaryUtils.Reader reader = new BinaryUtils.Reader(packet.BinaryData))
5855
{

NebulaPatcher/Patches/Dynamic/DESelection_Patch.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ public static bool SetViewStar_Prefix(DESelection __instance, ref StarData starD
4343
}
4444
if (starData != null && GameMain.data.dysonSpheres[starData.index] == null)
4545
{
46-
if (Multiplayer.Session.DysonSpheres.RequestingIndex != -1)
46+
if (Multiplayer.Session.DysonSpheres.RequestingIndex == -1)
4747
{
48-
InGamePopup.ShowInfo("Loading", $"Loading Dyson sphere {starData.displayName}, please wait...", "OK");
49-
return false;
48+
Multiplayer.Session.DysonSpheres.RequestDysonSphere(starData.index);
49+
}
50+
else
51+
{
52+
InGamePopup.ShowInfo("Loading", $"Loading Dyson sphere {starData.displayName}, please wait...", null);
5053
}
51-
Multiplayer.Session.DysonSpheres.RequestingIndex = starData.index;
52-
Log.Info($"Requesting DysonSphere for system {starData.displayName} (Index: {starData.index})");
53-
Multiplayer.Session.Network.SendPacket(new DysonSphereLoadRequest(starData.index, DysonSphereRequestEvent.Load));
54-
__instance.ClearAllSelection();
55-
InGamePopup.ShowInfo("Loading", $"Loading Dyson sphere {starData.displayName}, please wait...", "OK");
5654
// Restore comboBox back to original star
5755
UIComboBox dysonBox = UIRoot.instance.uiGame.dysonEditor.controlPanel.topFunction.dysonBox;
5856
int index = dysonBox.ItemsData.FindIndex(x => x == UIRoot.instance.uiGame.dysonEditor.selection.viewStar?.index);

NebulaPatcher/Patches/Dynamic/DysonBlueprintData_Patch.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace NebulaPatcher.Patches.Dynamic
99
internal class DysonBlueprintData_Patch
1010
{
1111
[HarmonyPrefix]
12-
[HarmonyPatch(nameof(DysonBlueprintData.ContentFromBase64String))]
13-
public static void ContentFromBase64String_Prefix()
12+
[HarmonyPatch(nameof(DysonBlueprintData.FromBase64String))]
13+
public static void FromBase64String_Prefix()
1414
{
1515
if (Multiplayer.IsActive)
1616
{
@@ -20,17 +20,17 @@ public static void ContentFromBase64String_Prefix()
2020

2121
#pragma warning disable Harmony003
2222
[HarmonyPostfix]
23-
[HarmonyPatch(nameof(DysonBlueprintData.ContentFromBase64String))]
24-
public static void ContentFromBase64String_Postfix(DysonBlueprintDataIOError __result, string __0, EDysonBlueprintType __1, DysonSphere __2, DysonSphereLayer __3)
23+
[HarmonyPatch(nameof(DysonBlueprintData.FromBase64String))]
24+
public static void FromBase64String_Postfix(DysonBlueprintDataIOError __result, string str64Data, EDysonBlueprintType requestType, DysonSphere sphere, DysonSphereLayer layer)
2525
{
2626
if (Multiplayer.IsActive)
2727
{
2828
Multiplayer.Session.DysonSpheres.InBlueprint = false;
2929
if (!Multiplayer.Session.DysonSpheres.IsIncomingRequest && __result == DysonBlueprintDataIOError.OK)
3030
{
31-
int starIndex = __2.starData.index;
32-
int layerId = __3?.id ?? -1;
33-
Multiplayer.Session.Network.SendPacket(new DysonBlueprintPacket(starIndex, layerId, __1, __0));
31+
int starIndex = sphere.starData.index;
32+
int layerId = layer?.id ?? -1;
33+
Multiplayer.Session.Network.SendPacket(new DysonBlueprintPacket(starIndex, layerId, requestType, str64Data));
3434
}
3535
}
3636
}

0 commit comments

Comments
 (0)