Skip to content

Commit c4793b3

Browse files
authored
Merge pull request #723 from starfi5h/pr-refactor
Bugfix and refactor (2025-1-25)
2 parents d61defa + 2ce1c73 commit c4793b3

File tree

20 files changed

+227
-152
lines changed

20 files changed

+227
-152
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
## Changelog
22

3+
0.9.14:
4+
- @starfi5h: Add `Enable Other Player Sounds` config settings in multiplayer general tab
5+
- @starfi5h: Fix headless server error
6+
- @starfi5h: When GalacticScale is present, hide New Game (Host) button due to compatibility
7+
- @starfi5h: Fix lastSaveTime initialization and the issue of hearing other player audio on the other planet
8+
39
0.9.13:
410
- Compatible with game version 0.10.32.25552
511
- @starfi5h: Sync ejector auto orbit and blueprint paste foundation
6-
- @starfi5h: Force enable drone and shield brust for client
12+
- @starfi5h: Force enable drone and shield burst for client
713
- Note: The dashboard is not synced. Clients will lose their custom stats when they leave the star system
814

915
0.9.12:

NebulaModel/MultiplayerOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public bool StreamerMode
124124
[Description("Toggle to enable achievement in multiplayer game")]
125125
public bool EnableAchievement { get; set; } = true;
126126

127+
[DisplayName("Enable Other Player Sounds")]
128+
public bool EnableOtherPlayerSounds { get; set; } = true;
129+
127130
[DisplayName("Chat Hotkey")]
128131
[Category("Chat")]
129132
[Description("Keyboard shortcut to toggle the chat window")]

NebulaModel/Networking/Serialization/NebulaNetPacketProcessor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public class NebulaNetPacketProcessor : NetPacketProcessor, INetPacketProcessor
1616
private readonly List<DelayedPacket> delayedPackets = [];
1717
private readonly Queue<PendingPacket> pendingPackets = new();
1818

19+
#if DEBUG
1920
private readonly Random simulationRandom = new();
2021
private readonly int SimulatedMaxLatency = 50;
2122
private readonly int SimulatedMinLatency = 20;
23+
#endif
2224

2325
public bool SimulateLatency { get; set; } = false;
2426

NebulaModel/Packets/Factory/Belt/BeltReverseRequestPacket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public BeltReverseRequestPacket(int beltId, int planetId, int authorId)
99
BeltId = beltId;
1010
PlanetId = planetId;
1111
AuthorId = authorId;
12-
}
12+
}
1313

1414
public int BeltId { get; set; }
1515
public int PlanetId { get; set; }

NebulaNetwork/PacketProcessors/GameStates/GameStateSaveInfoProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public class GameStateSaveInfoProcessor : PacketProcessor<GameStateSaveInfoPacke
1919
protected override void ProcessPacket(GameStateSaveInfoPacket packet, NebulaConnection conn)
2020
{
2121
GameStatesManager.LastSaveTime = packet.LastSaveTime;
22-
Log.Info("LastSaveTime: " + DateTimeOffset.FromUnixTimeSeconds(packet.LastSaveTime).ToString());
22+
Log.Info("LastSaveTime: " + DateTimeOffset.FromUnixTimeSeconds(packet.LastSaveTime).LocalDateTime.ToString());
2323
}
2424
}
Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
#region
22

3-
using System;
43
using NebulaAPI.Packets;
5-
using NebulaModel;
6-
using NebulaModel.Logger;
74
using NebulaModel.Networking;
85
using NebulaModel.Packets;
96
using NebulaModel.Packets.GameStates;
107
using NebulaWorld;
11-
using NebulaWorld.GameStates;
12-
using UnityEngine;
138

149
#endregion
1510

@@ -19,91 +14,8 @@ namespace NebulaNetwork.PacketProcessors.GameStates;
1914
// ReSharper disable once UnusedType.Global
2015
public class GameStateUpdateProcessor : PacketProcessor<GameStateUpdate>
2116
{
22-
private readonly float BUFFERING_TICK = 60f;
23-
private readonly float BUFFERING_TIME = 30f;
24-
private float averageUPS = 60f;
25-
26-
private int averageRTT;
27-
private bool hasChanged;
28-
2917
protected override void ProcessPacket(GameStateUpdate packet, NebulaConnection conn)
3018
{
31-
var rtt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - packet.SentTime;
32-
averageRTT = (int)(averageRTT * 0.8 + rtt * 0.2);
33-
averageUPS = averageUPS * 0.8f + packet.UnitsPerSecond * 0.2f;
34-
Multiplayer.Session.World.UpdatePingIndicator($"Ping: {averageRTT}ms");
35-
36-
// We offset the tick received to account for the time it took to receive the packet
37-
var tickOffsetSinceSent = (long)Math.Round(packet.UnitsPerSecond * rtt / 2 / 1000);
38-
var currentGameTick = packet.GameTick + tickOffsetSinceSent;
39-
var diff = currentGameTick - GameMain.gameTick;
40-
41-
// Discard abnormal packet (usually after host saving the file)
42-
if (rtt > 2 * averageRTT || averageUPS - packet.UnitsPerSecond > 15)
43-
{
44-
// Initial connection
45-
if (GameMain.gameTick < 1200L)
46-
{
47-
averageRTT = (int)rtt;
48-
GameMain.gameTick = currentGameTick;
49-
}
50-
Log.Debug(
51-
$"GameStateUpdate unstable. RTT:{rtt}(avg{averageRTT}) UPS:{packet.UnitsPerSecond:F2}(avg{averageUPS:F2})");
52-
return;
53-
}
54-
55-
if (!Config.Options.SyncUps)
56-
{
57-
// We allow for a small drift of 5 ticks since the tick offset using the ping is only an approximation
58-
if (GameMain.gameTick > 0 && Mathf.Abs(diff) > 5)
59-
{
60-
Log.Debug($"Game Tick desync. {GameMain.gameTick} skip={diff} UPS:{packet.UnitsPerSecond:F2}(avg{averageUPS:F2})");
61-
GameMain.gameTick = currentGameTick;
62-
}
63-
// Reset FixUPS when user turns off the option
64-
if (!hasChanged)
65-
{
66-
return;
67-
}
68-
FPSController.SetFixUPS(0);
69-
hasChanged = false;
70-
return;
71-
}
72-
73-
// Adjust client's UPS to match game tick with server, range 30~120 UPS
74-
var ups = diff / 1f + averageUPS;
75-
long skipTick = 0;
76-
switch (ups)
77-
{
78-
case > GameStatesManager.MaxUPS:
79-
{
80-
// Try to distribute game tick difference into BUFFERING_TIME (seconds)
81-
if (diff / BUFFERING_TIME + averageUPS > GameStatesManager.MaxUPS)
82-
{
83-
// The difference is too large, need to skip ticks to catch up
84-
skipTick = (long)(ups - GameStatesManager.MaxUPS);
85-
}
86-
ups = GameStatesManager.MaxUPS;
87-
break;
88-
}
89-
case < GameStatesManager.MinUPS:
90-
{
91-
if (diff + averageUPS - GameStatesManager.MinUPS < -BUFFERING_TICK)
92-
{
93-
skipTick = (long)(ups - GameStatesManager.MinUPS);
94-
}
95-
ups = GameStatesManager.MinUPS;
96-
break;
97-
}
98-
}
99-
if (skipTick != 0)
100-
{
101-
Log.Debug($"Game Tick desync. skip={skipTick} diff={diff,2}, RTT={rtt}ms, UPS={packet.UnitsPerSecond:F2}(avg{averageUPS:F2})");
102-
GameMain.gameTick += skipTick;
103-
}
104-
FPSController.SetFixUPS(ups);
105-
hasChanged = true;
106-
// Tick difference in the next second. Expose for other mods
107-
GameStatesManager.NotifyTickDifference(diff / 1f + averageUPS - ups);
19+
Multiplayer.Session.State.ProcessGameStateUpdatePacket(packet.SentTime, packet.GameTick, packet.UnitsPerSecond);
10820
}
10921
}

NebulaNetwork/PacketProcessors/Planet/VegeMinedProcessor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#region
22

3+
using NebulaAPI;
34
using NebulaAPI.Packets;
45
using NebulaModel.Networking;
56
using NebulaModel.Packets;
@@ -24,6 +25,7 @@ protected override void ProcessPacket(VegeMinedPacket packet, NebulaConnection c
2425
}
2526
using (Multiplayer.Session.Planets.IsIncomingRequest.On())
2627
{
28+
Multiplayer.Session.Factories.TargetPlanet = packet.PlanetId;
2729
if (packet.Amount == 0)
2830
{
2931
if (packet.IsVein)
@@ -68,6 +70,7 @@ protected override void ProcessPacket(VegeMinedPacket packet, NebulaConnection c
6870
factory.veinPool[packet.VegeId].amount = packet.Amount;
6971
veinGroups[groupIndex].amount -= 1L;
7072
}
73+
Multiplayer.Session.Factories.TargetPlanet = NebulaModAPI.PLANET_NONE;
7174
}
7275
}
7376
}

NebulaNetwork/PacketProcessors/Session/GlobalGameDataResponseProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using NebulaModel.Networking;
55
using NebulaModel.Packets;
66
using NebulaModel.Packets.Session;
7-
using NebulaWorld.GameStates;
7+
using NebulaWorld;
88

99
#endregion
1010

@@ -18,6 +18,6 @@ protected override void ProcessPacket(GlobalGameDataResponse packet, NebulaConne
1818
if (IsHost) return;
1919

2020
// Store the binary data in GameStatesManager then later overwrite those system in GameData.NewGame
21-
GameStatesManager.ImportGlobalGameData(packet);
21+
Multiplayer.Session.State.ImportGlobalGameData(packet);
2222
}
2323
}

NebulaPatcher/GlobalSuppressions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[assembly:
88
SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression",
99
Justification = "If warnings are suppressed it is necessary")]
10+
[assembly:
11+
SuppressMessage("Style", "IDE1006:Naming Styles",
12+
Justification = "HarmonyX parameters use underscore prefix")]

NebulaPatcher/Patches/Dynamic/GameData_Patch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public static void NewGame_Postfix(GameData __instance)
311311
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost) return;
312312

313313
// Overwrite from binaryData in GlobalGameDataResponse
314-
GameStatesManager.OverwriteGlobalGameData(__instance);
314+
Multiplayer.Session.State.OverwriteGlobalGameData(__instance);
315315
}
316316

317317
[HarmonyPostfix]
@@ -399,7 +399,7 @@ private static void InitLandingPlace(GameData gameData, PlanetData planet)
399399

400400
[HarmonyPrefix]
401401
[HarmonyPatch(nameof(GameData.LeaveStar))]
402-
public static void LeaveStar_Prefix(GameData __instance)
402+
public static void LeaveStar_Prefix()
403403
{
404404
//Client should unload all factories once they leave the star system
405405
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)

0 commit comments

Comments
 (0)