Skip to content

Commit 0c6ee12

Browse files
authored
Merge pull request #544 from starfi5h/pr-milestone
Add milestone syncing
2 parents 5ae3d30 + 47fe446 commit 0c6ee12

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace NebulaModel.Packets.Statistics
2+
{
3+
public class MilestoneUnlockPacket
4+
{
5+
public int Id { get; set; }
6+
public long UnlockTick { get; set; }
7+
public int PatternId { get; set; }
8+
public long[] Parameters { get; set; }
9+
10+
public MilestoneUnlockPacket() { }
11+
public MilestoneUnlockPacket(int id, long unlockTick, int patternId, long[] parameters)
12+
{
13+
Id = id;
14+
UnlockTick = unlockTick;
15+
PatternId = patternId;
16+
Parameters = parameters;
17+
}
18+
}
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using NebulaAPI;
2+
using NebulaModel.Networking;
3+
using NebulaModel.Packets;
4+
using NebulaModel.Packets.Statistics;
5+
using NebulaWorld;
6+
7+
namespace NebulaNetwork.PacketProcessors.Statistics
8+
{
9+
[RegisterPacketProcessor]
10+
internal class MilestoneUnlockProcessor : PacketProcessor<MilestoneUnlockPacket>
11+
{
12+
public override void ProcessPacket(MilestoneUnlockPacket packet, NebulaConnection conn)
13+
{
14+
IPlayerManager playerManager = Multiplayer.Session.Network.PlayerManager;
15+
bool valid = true;
16+
17+
if (IsHost)
18+
{
19+
INebulaPlayer player = playerManager.GetPlayer(conn);
20+
if (player != null)
21+
{
22+
playerManager.SendPacketToOtherPlayers(packet, player);
23+
}
24+
else
25+
{
26+
valid = false;
27+
}
28+
}
29+
30+
if (valid)
31+
{
32+
using (Multiplayer.Session.Statistics.IsIncomingRequest.On())
33+
{
34+
if (GameMain.data.milestoneSystem.milestoneDatas.TryGetValue(packet.Id, out MilestoneData milestoneData))
35+
{
36+
milestoneData.journalData.patternId = packet.PatternId;
37+
milestoneData.journalData.parameters = packet.Parameters;
38+
GameMain.data.milestoneSystem.UnlockMilestone(packet.Id, packet.UnlockTick);
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}

NebulaPatcher/Patches/Dynamic/GameData_Patch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NebulaModel.Networking;
55
using NebulaModel.Packets.Logistics;
66
using NebulaModel.Packets.Players;
7+
using NebulaModel.Packets.Statistics;
78
using NebulaPatcher.Patches.Transpilers;
89
using NebulaWorld;
910
using NebulaWorld.Warning;

NebulaPatcher/Patches/Dynamic/MilestoneSystem_Patch.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,29 @@ internal class MilestoneSystem_Patch
1212
[HarmonyPatch(nameof(MilestoneSystem.SetForNewGame))]
1313
public static void SetForNewGame_Postfix()
1414
{
15-
// Do not run if it is not multiplayer and the player is not a client
16-
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
15+
if (Multiplayer.IsActive && !Multiplayer.Session.LocalPlayer.IsHost)
16+
{
17+
// Request milestone data
18+
Log.Info($"Requesting MilestoneData from the server");
19+
Multiplayer.Session.Network.SendPacket(new MilestoneDataRequest());
20+
}
21+
}
22+
23+
[HarmonyPostfix]
24+
[HarmonyPatch(nameof(MilestoneSystem.UnlockMilestone))]
25+
public static void UnlockMilestone_Postfix(MilestoneSystem __instance, int id, long unlockTick)
26+
{
27+
if (!Multiplayer.IsActive || Multiplayer.Session.Statistics.IsIncomingRequest)
1728
{
1829
return;
1930
}
20-
// Request milestone data
21-
Log.Info($"Requesting MilestoneData from the server");
22-
Multiplayer.Session.Network.SendPacket(new MilestoneDataRequest());
31+
32+
if (__instance.milestoneDatas.TryGetValue(id, out MilestoneData milestoneData))
33+
{
34+
int patternId = milestoneData.journalData.patternId;
35+
long[] parameters = milestoneData.journalData.parameters;
36+
Multiplayer.Session.Network.SendPacket(new MilestoneUnlockPacket(id, unlockTick, patternId, parameters));
37+
}
2338
}
2439
}
2540
}

0 commit comments

Comments
 (0)