Skip to content

Commit

Permalink
Merge pull request #660 from starfi5h/pr-fix
Browse files Browse the repository at this point in the history
Fix various bugs
  • Loading branch information
starfi5h authored Mar 14, 2024
2 parents ba73bb0 + 7962507 commit 39b7770
Show file tree
Hide file tree
Showing 18 changed files with 245 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ protected virtual SubscribeDelegate GetCallbackFromData(NetDataReader reader)

protected virtual void WriteHash<T>(NetDataWriter writer)
{
writer.Put(GetHash<T>());
var hash = GetHash<T>();
writer.Put(hash);
if (!_callbacks.ContainsKey(hash))
{
Log.WarnInform($"WriteHash for unregistered type: " + typeof(T).ToString());
}
}

/// <summary>
Expand Down
15 changes: 0 additions & 15 deletions NebulaModel/Packets/Factory/Belt/BeltReversePacket.cs

This file was deleted.

17 changes: 17 additions & 0 deletions NebulaModel/Packets/Factory/Belt/BeltReverseRequestPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace NebulaModel.Packets.Factory.Belt;

public class BeltReverseRequestPacket
{
public BeltReverseRequestPacket() { }

public BeltReverseRequestPacket(int beltId, int planetId, int authorId)
{
BeltId = beltId;
PlanetId = planetId;
AuthorId = authorId;
}

Check warning on line 12 in NebulaModel/Packets/Factory/Belt/BeltReverseRequestPacket.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check warning on line 12 in NebulaModel/Packets/Factory/Belt/BeltReverseRequestPacket.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check warning on line 12 in NebulaModel/Packets/Factory/Belt/BeltReverseRequestPacket.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check warning on line 12 in NebulaModel/Packets/Factory/Belt/BeltReverseRequestPacket.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

public int BeltId { get; set; }
public int PlanetId { get; set; }
public int AuthorId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public FoundationBuildUpdatePacket(Vector3 center, float radius, int reformSize,
ReformIndices = btr?.extraCircleIndices;
ExtraCenter = center.ToFloat3();
CirclePointCount = btr.circlePointCount;
IsCircle = true;
}
else //Normal reform
{
ReformIndices = btr?.cursorIndices;
ExtraCenter = new Float3(Vector3.zero);
CirclePointCount = 0;
IsCircle = false;
}
}

Expand All @@ -48,4 +50,5 @@ public FoundationBuildUpdatePacket(Vector3 center, float radius, int reformSize,
public Float3 GroundTestPos { get; set; }
public Float3 ExtraCenter { get; set; }
public int CirclePointCount { get; set; }
public bool IsCircle { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NebulaModel.Packets;
using NebulaModel.Packets.Combat.GroundEnemy;
using NebulaWorld;
using NebulaWorld.Combat;

#endregion

Expand All @@ -28,18 +29,7 @@ protected override void ProcessPacket(DFGLaunchAssaultPacket packet, NebulaConne
using (Multiplayer.Session.Combat.IsIncomingRequest.On())
{
// Set enemyRecycle pool to make enemyId stay in sync
factory.enemyCursor = packet.EnemyCursor;
var capacity = factory.enemyCapacity;
while (capacity <= factory.enemyCursor)
{
capacity *= 2;
}
if (capacity > factory.enemyCapacity)
{
factory.SetEnemyCapacity(capacity);
}
factory.enemyRecycleCursor = packet.EnemyRecyle.Length;
Array.Copy(packet.EnemyRecyle, factory.enemyRecycle, packet.EnemyRecyle.Length);
EnemyManager.SetPlanetFactoryRecycle(factory, packet.EnemyCursor, packet.EnemyRecyle);

var dFBase = factory.enemySystem.bases.buffer[packet.BaseId];
dFBase.turboTicks = 60;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected override void ProcessPacket(DFGUpdateBaseStatusPacket packet, NebulaCo
if (factory == null) return;

var dFBase = factory.enemySystem.bases.buffer[packet.BaseId];
if (dFBase == null) return;
ref var evolveData = ref dFBase.evolve;
evolveData.threat = packet.Threat;
if (evolveData.level != packet.Level)
Expand Down
42 changes: 31 additions & 11 deletions NebulaNetwork/PacketProcessors/Factory/Belt/BeltReverseProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
#region

using System;
using NebulaAPI;
using NebulaAPI.Networking;
using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Packets.Factory.Belt;
using NebulaWorld;

#endregion

namespace NebulaNetwork.PacketProcessors.Factory.Belt;

[RegisterPacketProcessor]
internal class BeltReverseProcessor : BasePacketProcessor<BeltReversePacket>
internal class BeltReverseProcessor : BasePacketProcessor<BeltReverseRequestPacket>
{
public override void ProcessPacket(BeltReversePacket packet, INebulaConnection conn)
public override void ProcessPacket(BeltReverseRequestPacket packet, INebulaConnection conn)
{
var factory = GameMain.galaxy.PlanetById(packet.PlanetId).factory;
if (factory == null)
{
return;
}

if (IsHost)
{
var starId = packet.PlanetId / 100;
Multiplayer.Session.Server.SendPacketToStar(packet, starId);
}

using (NebulaModAPI.MultiplayerSession.Factories.IsIncomingRequest.On())
{
NebulaModAPI.MultiplayerSession.Factories.EventFactory = factory;
NebulaModAPI.MultiplayerSession.Factories.TargetPlanet = packet.PlanetId;
if (NebulaModAPI.MultiplayerSession.LocalPlayer.IsHost)
NebulaModAPI.MultiplayerSession.Factories.PacketAuthor = packet.AuthorId;
if (IsHost)
{
// Load planet model
NebulaModAPI.MultiplayerSession.Factories.AddPlanetTimer(packet.PlanetId);
}

var beltWindow = UIRoot.instance.uiGame.beltWindow;
beltWindow._Close(); // close the window first to avoid changing unwant variable when setting beltId
var tmpFactory = beltWindow.factory;
var tmpBeltId = beltWindow.beltId;
beltWindow.factory = factory;
beltWindow.beltId = packet.BeltId;
beltWindow.OnReverseButtonClick(0);
beltWindow.factory = tmpFactory;
beltWindow.beltId = tmpBeltId;
try
{
beltWindow._Close(); // close the window first to avoid changing unwant variable when setting beltId
beltWindow.factory = factory;
beltWindow.traffic = factory.cargoTraffic;
beltWindow.player = GameMain.mainPlayer;
beltWindow.beltId = packet.BeltId;
beltWindow.OnReverseButtonClick(0);
beltWindow._Close();
}
catch (Exception e)
{
Log.Warn(e);
beltWindow._tmp_ids.Clear();
beltWindow._tmp_cargos.Clear();
beltWindow._Close();
}

NebulaModAPI.MultiplayerSession.Factories.EventFactory = null;
NebulaModAPI.MultiplayerSession.Factories.TargetPlanet = NebulaModAPI.PLANET_NONE;
NebulaModAPI.MultiplayerSession.Factories.PacketAuthor = NebulaModAPI.AUTHOR_NONE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void ProcessPacket(FoundationBuildUpdatePacket packet, Nebula
//Perform terrain operation
var center = packet.ExtraCenter.ToVector3();
var area = packet.CirclePointCount;
if (packet.CirclePointCount == 0) //Normal reform
if (!packet.IsCircle) //Normal reform
{
var reformPointsCount = factory.planet.aux.ReformSnap(packet.GroundTestPos.ToVector3(), packet.ReformSize,
packet.ReformType, packet.ReformColor, reformPoints, packet.ReformIndices, factory.platformSystem,
Expand Down
31 changes: 31 additions & 0 deletions NebulaPatcher/Patches/Dynamic/CombatModuleComponent_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#region

using System;
using HarmonyLib;
using NebulaModel.Logger;

#endregion

namespace NebulaPatcher.Patches.Dynamic;

[HarmonyPatch(typeof(CombatModuleComponent))]
internal class CombatModuleComponent_Patch
{
static int s_errorCount = 0;

[HarmonyFinalizer]
[HarmonyPatch(typeof(CombatModuleComponent), nameof(CombatModuleComponent.GameTick))]
public static Exception GameTick_Finalizer(Exception __exception)
{
if (__exception != null)
{
// After 10 exception triggered, suppress the following messages
if (s_errorCount++ < 10)
{
var msg = "GameTick_Finalizer suppressed exception: \n" + __exception.ToString();
Log.Error(msg);
}
}
return null;
}
}
6 changes: 6 additions & 0 deletions NebulaPatcher/Patches/Dynamic/Dedicated_Server_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public static void GameMainBegin_Postfix()
{
GameMain.Pause();
}

if (GameMain.mainPlayer != null)
{
// Don't let the player of dedicated server to interact with enemies
GameMain.mainPlayer.isAlive = false;
}
}

// Stop game rendering
Expand Down
16 changes: 13 additions & 3 deletions NebulaPatcher/Patches/Dynamic/PowerSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ internal class PowerSystem_Patch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(PowerSystem.GameTick))]
public static void PowerSystem_GameTick_Prefix(long time, ref bool isActive)
public static void PowerSystem_GameTick_Prefix(PowerSystem __instance, long time, ref bool isActive)
{
//Enable signType update on remote planet every 64 tick
if ((time & 63) == 0 && Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
if (!Multiplayer.IsActive) return;

// Enable signType update on remote planet every 64 tick
if ((time & 63) == 0 && Multiplayer.Session.IsServer)
{
isActive = true;
}

// Temporary fix NRE of factoryStatPool in client (note: try to find the root cause in the future)
var factoryIndex = __instance.factory.index;
if (GameMain.statistics.production.factoryStatPool[factoryIndex] == null)
{
GameMain.statistics.production.factoryStatPool[factoryIndex] = new FactoryProductionStat();
GameMain.statistics.production.factoryStatPool[factoryIndex].Init();
}
}

[HarmonyPrefix]
Expand Down
22 changes: 22 additions & 0 deletions NebulaPatcher/Patches/Dynamic/PropertyLogic_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#region

using HarmonyLib;
using NebulaWorld;

#endregion

namespace NebulaPatcher.Patches.Dynamic;

[HarmonyPatch(typeof(PropertyLogic))]
internal class PropertyLogic_Patch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(PropertyLogic.GameTick))]
public static bool PrepareTick_Prefix()
{
if (!Multiplayer.IsActive) return true;

// Disable UpdateProduction in client to prevent errror
return Multiplayer.Session.IsServer;
}
}
21 changes: 16 additions & 5 deletions NebulaPatcher/Patches/Dynamic/UIBeltWindow_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,26 @@ namespace NebulaPatcher.Patches.Dynamic;
[HarmonyPatch(typeof(UIBeltWindow))]
internal class UIBeltWindow_Patch
{
[HarmonyPostfix]
[HarmonyPrefix]
[HarmonyPatch(typeof(UIBeltWindow), nameof(UIBeltWindow.OnReverseButtonClick))]
public static void OnReverseButtonClick_Postfix(UIBeltWindow __instance)
public static bool OnReverseButtonClick_Prefix(UIBeltWindow __instance)
{
if (!Multiplayer.IsActive) return true;
if (Multiplayer.Session.Factories.IsIncomingRequest.Value) return true;

// Notify others about belt direction reverse
if (Multiplayer.IsActive && !Multiplayer.Session.Factories.IsIncomingRequest.Value)
var packet = new BeltReverseRequestPacket(__instance.beltId, __instance.factory.planetId, Multiplayer.Session.LocalPlayer.Id);
if (Multiplayer.Session.IsServer)
{
var starId = __instance.factory.planetId / 100;
Multiplayer.Session.Server.SendPacketToStar(packet, starId);
return true;
}
else
{
Multiplayer.Session.Network.SendPacketToLocalStar(new BeltReversePacket(__instance.beltId,
__instance.factory.planetId));
// Request reverse change and wait for server to approve
Multiplayer.Session.Client.SendPacket(packet);
return false;
}
}
}
4 changes: 2 additions & 2 deletions NebulaPatcher/Patches/Dynamic/UIGalaxySelect_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static void SetStarmapGalaxy_Prefix()
[HarmonyPatch(nameof(UIGalaxySelect.UpdateParametersUIDisplay))]
public static void UpdateParametersUIDisplay_Postfix(UIGalaxySelect __instance)
{
if (!Multiplayer.IsInMultiplayerMenu || !Multiplayer.Session.LocalPlayer.IsHost)
if (!Multiplayer.IsInMultiplayerMenu || !Multiplayer.IsActive || !Multiplayer.Session.LocalPlayer.IsHost)
{
return;
}
Expand All @@ -201,7 +201,7 @@ public static void UpdateParametersUIDisplay_Postfix(UIGalaxySelect __instance)
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Original Function Name")]
public static void _OnUpdate_Postfix(UIGalaxySelect __instance)
{
if (!Multiplayer.IsInMultiplayerMenu)
if (!Multiplayer.IsInMultiplayerMenu || !Multiplayer.IsActive || !Multiplayer.Session.IsInLobby)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ static int AddUnit(EnemyFormation enemyFormation, DFGBaseComponent gbase, int fo
}

var portId = enemyFormation.AddUnit();
var packet = new DFGFormationAddUnitPacket(gbase.groundSystem.planet.id, gbase.id, formId, portId);
Multiplayer.Session.Network.SendPacketToStar(packet, gbase.groundSystem.planet.star.id);
if (portId > 0)
{
// Only broadcast if add unit success (vacancyCursor > 0)
var packet = new DFGFormationAddUnitPacket(gbase.groundSystem.planet.id, gbase.id, formId, portId);
Multiplayer.Session.Server.SendPacketToStar(packet, gbase.groundSystem.planet.star.id);
}
return 0; // Skip the following call to InitiateUnitDeferred
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ static int AddUnit(EnemyFormation enemyFormation, EnemyDFHiveSystem hive, int fo
}

var portId = enemyFormation.AddUnit();
var packet = new DFSFormationAddUnitPacket(hive.hiveAstroId, formId, portId);
Multiplayer.Session.Network.SendPacket(packet);
if (portId > 0)
{
// Only broadcast if add unit success (vacancyCursor > 0)
var packet = new DFSFormationAddUnitPacket(hive.hiveAstroId, formId, portId);
Multiplayer.Session.Server.SendPacket(packet);
}
return 0; // Skip the following call to InitiateUnitDeferred
}
}
Loading

0 comments on commit 39b7770

Please sign in to comment.