Skip to content

Commit

Permalink
Merge pull request NebulaModTeam#710 from starfi5h/pr-bugfix
Browse files Browse the repository at this point in the history
Bugfix 2024-09-14
  • Loading branch information
starfi5h authored Sep 15, 2024
2 parents 0af58c9 + 34143fe commit 4e345d3
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

0.9.11:
- @starfi5h: Fix half-growth dark fog bases keep regenerating
- @starfi5h: Fix combat drones doesn't increase ground base threat
- @starfi5h: Fix errors in NgrokManager.IsNgrokActive and SpaceSector.RemoveEnemyWithComponents

0.9.10:
- @fakeboboliu: Support to connect server with WSS by specifying wss:// in the connect Uri
- @starfi5h: Sync Logistics Control Panel (I) entry list
Expand Down
8 changes: 6 additions & 2 deletions NebulaModel/Logger/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ public static void Error(string message)
{
// ShowError has Unity API and needs to call on the main thread
BepInEx.ThreadingHelper.Instance.StartSyncInvoke(() =>
UIFatalErrorTip.instance.ShowError("[Nebula Error] " + message, "")
);
{
// We just want to use the window to show the error message, so leave GameMain.errored as the original value
var tmp = GameMain.errored;
UIFatalErrorTip.instance.ShowError("[Nebula Error] " + message, "");
GameMain.errored = tmp;
});
return;
}
UIFatalErrorTip.instance.ShowError("[Nebula Error] " + message, "");
Expand Down
14 changes: 4 additions & 10 deletions NebulaNetwork/Ngrok/NgrokManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ private bool StartNgrok()
// This links the process as a child process by attaching a null debugger thus ensuring that the process is killed when its parent dies.
_ = new ChildProcessLinker(_ngrokProcess, _ =>
{
Log.Warn(
Log.WarnInform(
"Failed to link Ngrok process to DSP process as a child! (This might result in a left over ngrok process if the DSP process uncleanly killed)");
});
}
else
{
Log.Error("Failed to start Ngrok process!");
Log.WarnInform("Failed to start Ngrok process!");
}

_ngrokProcess?.BeginOutputReadLine();
Expand Down Expand Up @@ -286,7 +286,7 @@ public void StopNgrok()
}
catch (Exception e)
{
Log.Error(e);
Log.WarnInform(e.ToString());
}
_ngrokProcess.Close();
_ngrokProcess = null;
Expand All @@ -301,7 +301,7 @@ public bool IsNgrokActive()
}
catch (Exception e)
{
Log.Error(e);
Log.WarnInform(e.ToString());
return false;
}
}
Expand All @@ -310,12 +310,6 @@ public Task<string> GetNgrokAddressAsync()
{
return Task.Run(() =>
{
if (!IsNgrokActive())
{
throw new Exception(
$"Not able to get Ngrok tunnel address because Ngrok is not started (or exited prematurely)! LastErrorCode: {NgrokLastErrorCode} {NgrokLastErrorCodeDesc}");
}

if (!_ngrokAddressObtainedSource.Task.Wait(TimeSpan.FromSeconds(15)))
{
throw new TimeoutException(
Expand Down
20 changes: 15 additions & 5 deletions NebulaNetwork/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public void Start()
}
catch (InvalidOperationException e)
{
Log.Warn(e.ToString());
InGamePopup.ShowError("Error", "An error occurred while hosting the game: ".Translate() + e.Message,
"Close".Translate());
Stop();
Expand All @@ -267,13 +268,22 @@ public void Start()

Task.Run(async () =>
{
if (ngrokManager.IsNgrokActive())
if (ngrokManager.NgrokEnabled)
{
var ip = await ngrokManager.GetNgrokAddressAsync();
DiscordManager.UpdateRichPresence(ip, updateTimestamp: true);
if (Multiplayer.IsDedicated)
try
{
// Wait up to 15s for ngrok to start, then get the address
var ip = await ngrokManager.GetNgrokAddressAsync();
DiscordManager.UpdateRichPresence(ip, updateTimestamp: true);
if (Multiplayer.IsDedicated)
{
Log.Info($">> Ngrok address: {ip}");
}
return;
}
catch (Exception e)
{
Log.Info($">> Ngrok address: {ip}");
Log.Warn(e);
}
}
else
Expand Down
47 changes: 22 additions & 25 deletions NebulaPatcher/Patches/Dynamic/EnemyDFGroundSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,6 @@ public static bool ExecuteDeferredUnitFormation_Prefix(EnemyDFGroundSystem __ins
return true;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(EnemyDFGroundSystem.CanEraseBase))]
public static bool CanEraseBase_Prefix(DFGBaseComponent _base, ref bool __result)
{
if (!Multiplayer.IsActive) return true;

if (_base == null || _base.id == 0)
{
__result = true;
return false;
}
// Skip __instance.builders.buffer[_base.builderId].sp check as it may have different value
var pbuilders = _base.pbuilders;
for (var i = 2; i < pbuilders.Length; i++)
{
if (pbuilders[i].instId > 0)
{
__result = false;
return false;
}
}
__result = true;
return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(EnemyDFGroundSystem.NotifyEnemyKilled))]
public static bool NotifyEnemyKilled_Prefix()
Expand Down Expand Up @@ -231,4 +206,26 @@ public static void KeyTickLogic_Prefix(EnemyDFGroundSystem __instance)
}
}

[HarmonyPostfix]
[HarmonyPatch(nameof(EnemyDFGroundSystem.KeyTickLogic))]
public static void KeyTickLogic_Postfix(EnemyDFGroundSystem __instance)
{
if (!Multiplayer.IsActive || Multiplayer.Session.IsServer) return;

var cursor = __instance.bases.cursor;
var baseBuffer = __instance.bases.buffer;
var enemyPool = __instance.factory.enemyPool;
for (var baseId = 1; baseId < cursor; baseId++)
{
var dfgbaseComponent = baseBuffer[baseId];
if (dfgbaseComponent == null || dfgbaseComponent.id != baseId) continue;
if (dfgbaseComponent.enemyId != 0 && enemyPool[dfgbaseComponent.enemyId].id == 0)
{
// Note: isInvincible in enemy is used by Nebula client to note if the enemy is pending to get killed
// isInvincible will get set back to true in EnemyDFGroundSystem.KeyTickLogic when base sp > 0
// So we'll need to set isInvincible = true to let host's incoming KillEnemyFinally packet get executed
enemyPool[dfgbaseComponent.enemyId].isInvincible = true;
}
}
}
}
21 changes: 21 additions & 0 deletions NebulaPatcher/Patches/Dynamic/SkillSystem_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,25 @@ public static void DamageObject_Prefix(int damage, int slice, ref SkillTarget ta
Multiplayer.Session.Network.SendPacketToLocalPlanet(packet);
}
}

[HarmonyPrefix]
[HarmonyPatch(nameof(SkillSystem.DamageGroundObjectByLocalCaster))]
public static void DamageGroundObjectByLocalCaster_Prefix(PlanetFactory factory, int damage, int slice, ref SkillTarget target, ref SkillTarget caster)
{
if (caster.type != ETargetType.Craft
|| target.type != ETargetType.Enemy
|| !Multiplayer.IsActive || Multiplayer.Session.Combat.IsIncomingRequest.Value) return;

if (factory == GameMain.localPlanet?.factory) // Sync for local planet combat drones
{
target.astroId = caster.astroId = GameMain.localPlanet.astroId;
var packet = new CombatStatDamagePacket(damage, slice, in target, in caster)
{
// Change the caster to player as craft (space fleet) is not sync yet
CasterType = (short)ETargetType.Player,
CasterId = Multiplayer.Session.LocalPlayer.Id
};
Multiplayer.Session.Network.SendPacketToLocalPlanet(packet);
}
}
}
16 changes: 16 additions & 0 deletions NebulaPatcher/Patches/Dynamic/SpaceSector_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ public static bool KillEnemyFinal_Prefix(SpaceSector __instance, int enemyId)
return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(SpaceSector.RemoveEnemyWithComponents))]
public static void RemoveEnemyWithComponents_Prefix(SpaceSector __instance, int id)
{
// Fix IndexOutOfRangeException in SpaceSector.RemoveEnemyWithComponents IL_026A
// This is due to combatStats is not sync in client
if (id != 0 && __instance.enemyPool[id].id != 0)
{
if (__instance.enemyPool[id].combatStatId != 0)
{
if (__instance.enemyPool[id].combatStatId >= __instance.skillSystem.combatStats.cursor)
__instance.enemyPool[id].combatStatId = 0;
}
}
}

[HarmonyPrefix]
[HarmonyPatch(nameof(SpaceSector.TryCreateNewHive))]
public static bool TryCreateNewHive_Prefix(StarData star)
Expand Down
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/UIFatalErrorTip_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private static void OnCopyClick(int id)
}

// Nebula only: skip the message after PacketProcessor
if (str.StartsWith("NebulaModel.Packets.PacketProcessor"))
if (str.StartsWith("NebulaModel.Packets.PacketProcessor") || str.StartsWith(" at NebulaModel.Packets.PacketProcessor"))
{
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ public static IEnumerable<CodeInstruction> UpdateFactoryThreat_Transpiler(IEnume

static bool LaunchCondition(DFGBaseComponent @this)
{
if (!Multiplayer.IsActive || @this.groundSystem.local_player_alive == true) return @this.groundSystem.local_player_alive;
if (!Multiplayer.IsActive) return @this.groundSystem.local_player_grounded_alive;

// In MP, replace local_player_grounded_alive flag with the following condition
var planetId = @this.groundSystem.planet.id;
var players = Multiplayer.Session.Combat.Players;
for (var i = 0; i < players.Length; i++)
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "0.9.10",
"version": "0.9.11",
"assemblyVersion": {
"precision": "build"
},
Expand Down

0 comments on commit 4e345d3

Please sign in to comment.