Skip to content

Commit

Permalink
Merge pull request #586 from starfi5h/pr-bugfix0.8.11
Browse files Browse the repository at this point in the history
Bugfix for nebula 0.8.11
  • Loading branch information
starfi5h authored Oct 19, 2022
2 parents 4cbbfd3 + 394b343 commit ee63b66
Show file tree
Hide file tree
Showing 19 changed files with 296 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .github/scripts/thunderstore_bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function generateManifest() {
BEPINEX_DEPENDENCY,
`nebula-${apiPluginInfo.name}-${apiPluginInfo.version}`,
"PhantomGamers-IlLine-1.0.0",
"CommonAPI-CommonAPI-1.5.5",
"CommonAPI-CommonAPI-1.5.6",
"starfi5h-BulletTime-1.2.9",
],
website_url: "https://github.com/hubastard/nebula"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

0.8.12:

- @PhantomGamers: Remove exe targeting to support game pass version
- @starfi5h: Fix errors about logistic bots
- @starfi5h: Add -load-latest launch option for dedicated server

0.8.11:

- @starfi5h: Added support for DSP 0.9.27 along with syncing for the new logistics distribution system
Expand Down
4 changes: 3 additions & 1 deletion NebulaModel/Packets/Logistics/DispenserCourierPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
{
public class DispenserCourierPacket
{
public int PlanetId { get; set; }
public int PlayerId { get; set; }
public int DispenserId { get; set; }
public int ItemId { get; set; }
public int ItemCount { get; set; }

public DispenserCourierPacket() { }
public DispenserCourierPacket(int playerId, int dispenserId, int itemId, int itemCount)
public DispenserCourierPacket(int planetId, int playerId, int dispenserId, int itemId, int itemCount)
{
PlanetId = planetId;
PlayerId = playerId;
DispenserId = dispenserId;
ItemId = itemId;
Expand Down
50 changes: 44 additions & 6 deletions NebulaModel/Utils/IPUtils.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using BepInEx;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using static NebulaModel.Utils.IPUtils;

namespace NebulaModel.Utils
{
Expand Down Expand Up @@ -121,11 +120,50 @@ public static async Task<string> GetPortStatus(ushort port)
{
string response = await client.GetStringAsync($"https://ifconfig.co/port/{port}");
Dictionary<string, object> jObject = MiniJson.Deserialize(response) as Dictionary<string, object>;
if (!IsIPv4((string)jObject["ip"]))
if (IsIPv4((string)jObject["ip"]))
{
return Status.Unsupported.ToString();
return (bool)jObject["reachable"] ? PortStatus.Open.ToString() : PortStatus.Closed.ToString() + "(IPv4)";
}
else
{
// if client has IPv6, extra test for IPv4 port status
string result = ((bool)jObject["reachable"] ? PortStatus.Open.ToString() : PortStatus.Closed.ToString()) + "(IPv6) ";
try
{
IPAddress iPv4Address = null;
foreach (IPAddress ip in Dns.GetHostEntry(string.Empty).AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
string str = ip.ToString();
if (!str.StartsWith("127.0") && !str.StartsWith("192.168"))
{
iPv4Address = ip;
break;
}
}
}
if (iPv4Address != null)
{
// TODO: More respect about rate limit?
HttpWebRequest httpWebRequest = HttpWebRequest.Create($"https://ifconfig.co/port/{port}") as HttpWebRequest;
httpWebRequest.Timeout = 5000;
httpWebRequest.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) => new IPEndPoint(iPv4Address, 0);

using WebResponse webResponse = await httpWebRequest.GetResponseAsync();
using Stream stream = webResponse.GetResponseStream();
using StreamReader readStream = new(stream, Encoding.UTF8);
response = readStream.ReadToEnd();
jObject = MiniJson.Deserialize(response) as Dictionary<string, object>;
result += ((bool)jObject["reachable"] ? PortStatus.Open.ToString() : PortStatus.Closed.ToString()) + "(IPv4)";
}
}
catch(Exception e)
{
Logger.Log.Warn(e);
}
return result;
}
return (bool)jObject["reachable"] ? PortStatus.Open.ToString() : PortStatus.Closed.ToString();
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using NebulaAPI;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Logistics;
using NebulaWorld;
using System;

namespace NebulaNetwork.PacketProcessors.Logistics
{
Expand All @@ -15,7 +13,11 @@ public override void ProcessPacket(DispenserCourierPacket packet, NebulaConnecti
{
PlanetFactory factory = GameMain.mainPlayer.factory;
DispenserComponent[] pool = factory?.transport.dispenserPool;
if (pool != null && packet.DispenserId > 0 && packet.DispenserId < pool.Length && pool[packet.DispenserId].id == packet.DispenserId)
if (GameMain.mainPlayer.planetId != packet.PlanetId || pool == null)
{
return;
}
if (packet.DispenserId > 0 && packet.DispenserId < pool.Length && pool[packet.DispenserId] != null)
{
DispenserComponent dispenser = pool[packet.DispenserId];
Multiplayer.Session.Couriers.AddCourier(packet.PlayerId, factory.entityPool[dispenser.entityId].pos, packet.ItemId, packet.ItemCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,9 @@ namespace NebulaNetwork.PacketProcessors.Logistics
[RegisterPacketProcessor]
internal class ILSUpdateSlotDataProcessor : PacketProcessor<ILSUpdateSlotData>
{
private readonly IPlayerManager playerManager;
public ILSUpdateSlotDataProcessor()
{
playerManager = Multiplayer.Session.Network.PlayerManager;
}

public override void ProcessPacket(ILSUpdateSlotData packet, NebulaConnection conn)
{
if (IsHost)
{
PlanetData pData = null;
// PLS
if (packet.StationGId == 0)
{
pData = GameMain.galaxy.PlanetById(packet.PlanetId);
}
else // ILS
{
if (packet.StationGId < GameMain.data.galacticTransport.stationPool.Length)
{
StationComponent stationComponent = GameMain.data.galacticTransport.stationPool[packet.StationGId];
if (stationComponent != null)
{
pData = GameMain.galaxy.PlanetById(stationComponent.planetId);
}
}
}

if (pData != null)
{
playerManager.SendPacketToStar(packet, pData.star.id);
}

Multiplayer.Session.Ships.UpdateSlotData(packet);
}

if (IsClient)
{
Multiplayer.Session.Ships.UpdateSlotData(packet);
}
Multiplayer.Session.Ships.UpdateSlotData(packet);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Planet;
using System;
using System.Threading.Tasks;

namespace NebulaNetwork.PacketProcessors.Planet
Expand All @@ -22,30 +23,38 @@ public override void ProcessPacket(PlanetDetailRequest packet, NebulaConnection
planetData.calculating = true;
Task.Run(() =>
{
// Modify from PlanetModelingManager.PlanetCalculateThreadMain()
HighStopwatch highStopwatch = new HighStopwatch();
highStopwatch.Begin();
planetData.data = new PlanetRawData(planetData.precision);
planetData.modData = planetData.data.InitModData(planetData.modData);
planetData.data.CalcVerts();
planetData.aux = new PlanetAuxData(planetData);
PlanetAlgorithm planetAlgorithm = PlanetModelingManager.Algorithm(planetData);
planetAlgorithm.GenerateTerrain(planetData.mod_x, planetData.mod_y);
planetAlgorithm.CalcWaterPercent();
if (planetData.type != EPlanetType.Gas)
try
{
planetAlgorithm.GenerateVegetables();
planetAlgorithm.GenerateVeins();
// Modify from PlanetModelingManager.PlanetCalculateThreadMain()
HighStopwatch highStopwatch = new HighStopwatch();
highStopwatch.Begin();
planetData.data = new PlanetRawData(planetData.precision);
planetData.modData = planetData.data.InitModData(planetData.modData);
planetData.data.CalcVerts();
planetData.aux = new PlanetAuxData(planetData);
PlanetAlgorithm planetAlgorithm = PlanetModelingManager.Algorithm(planetData);
planetAlgorithm.GenerateTerrain(planetData.mod_x, planetData.mod_y);
planetAlgorithm.CalcWaterPercent();
if (planetData.type != EPlanetType.Gas)
{
planetAlgorithm.GenerateVegetables();
planetAlgorithm.GenerateVeins();
}
planetData.CalculateVeinGroups();
planetData.GenBirthPoints();
planetData.NotifyCalculated();
conn.SendPacket(new PlanetDetailResponse(planetData.id, planetData.runtimeVeinGroups ?? Array.Empty<VeinGroup>(), planetData.landPercent));
Log.Info($"PlanetCalculateThread:{planetData.displayName} time:{highStopwatch.duration:F4}s");
}
catch (Exception e)
{
Log.Warn($"Error when calculating {planetData.displayName}");
Log.Warn(e);
}
planetData.CalculateVeinGroups();
planetData.GenBirthPoints();
planetData.NotifyCalculated();
conn.SendPacket(new PlanetDetailResponse(planetData.id, planetData.runtimeVeinGroups, planetData.landPercent));
Log.Info($"PlanetCalculateThread:{planetData.displayName} time:{highStopwatch.duration:F4}s");
});
return;
}
conn.SendPacket(new PlanetDetailResponse(planetData.id, planetData.runtimeVeinGroups, planetData.landPercent));
conn.SendPacket(new PlanetDetailResponse(planetData.id, planetData.runtimeVeinGroups ?? Array.Empty<VeinGroup>(), planetData.landPercent));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ public override void ProcessPacket(PlanetDetailResponse packet, NebulaConnection
planet = GameMain.galaxy.PlanetById(packet.PlanetDataID);
}

if (planet.veinGroups == null || planet.veinGroups.Length != packet.VeinCounts.Length)
if (packet.VeinCounts.Length > 0)
{
planet.veinGroups = new VeinGroup[packet.VeinCounts.Length];
}
for (int i = 1; i < planet.veinGroups.Length; i++)
{
planet.veinGroups[i].type = (EVeinType)packet.VeinTypes[i];
planet.veinGroups[i].count = packet.VeinCounts[i];
planet.veinGroups[i].amount = packet.VeinAmounts[i];
if (planet.veinGroups == null || planet.veinGroups.Length != packet.VeinCounts.Length)
{
planet.veinGroups = new VeinGroup[packet.VeinCounts.Length];
}
for (int i = 1; i < planet.veinGroups.Length; i++)
{
planet.veinGroups[i].type = (EVeinType)packet.VeinTypes[i];
planet.veinGroups[i].count = packet.VeinCounts[i];
planet.veinGroups[i].amount = packet.VeinAmounts[i];
}
}
planet.landPercent = packet.LandPercent;
planet.landPercentDirty = false;
Expand Down
2 changes: 1 addition & 1 deletion NebulaNetwork/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void SendRawPacketToPlanet(byte[] rawPacket, int planetId, INebulaConnect
foreach (KeyValuePair<INebulaConnection, INebulaPlayer> kvp in connectedPlayers)
{
INebulaPlayer player = kvp.Value;
if (!!player.Connection.Equals(exclude))
if (!player.Connection.Equals(exclude))
{
player.SendPacket(packet);
}
Expand Down
34 changes: 30 additions & 4 deletions NebulaPatcher/NebulaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
using NebulaWorld.SocialIntegration;
using System;
using System.Net;
#if DEBUG
using System.IO;
#endif
using System.Reflection;
using UnityEngine;

namespace NebulaPatcher
{
[BepInPlugin(PluginInfo.PLUGIN_ID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
[BepInProcess("DSPGAME.exe")]
[BepInDependency(CommonAPIPlugin.GUID)]
[CommonAPISubmoduleDependency(nameof(ProtoRegistry), nameof(CustomKeyBindSystem))]
public class NebulaPlugin : BaseUnityPlugin, IMultiplayerMod
Expand Down Expand Up @@ -63,6 +60,28 @@ private void Awake()
}
}

if (args[i] == "-load-latest")
{
loadArgExists = true;
string[] files = Directory.GetFiles(GameConfig.gameSaveFolder, "*" + GameSave.saveExt, SearchOption.TopDirectoryOnly);
long[] times = new long[files.Length];
string[] names = new string[files.Length];
for (int j = 0; j < files.Length; j++)
{
FileInfo fileInfo = new(files[j]);
times[j] = fileInfo.LastWriteTime.ToFileTime();
names[j] = fileInfo.Name.Substring(0, fileInfo.Name.Length - GameSave.saveExt.Length);
}
if (files.Length > 0)
{
Array.Sort(times, names);
saveName = names[files.Length - 1];
Log.Info($">> Loading save {saveName}");
NebulaWorld.GameStates.GameStatesManager.ImportedSaveName = saveName;
didLoad = true;
}
}

if (args[i] == "-ups" && i + 1 < args.Length)
{
if (int.TryParse(args[i + 1], out int value))
Expand All @@ -81,7 +100,14 @@ private void Awake()
{
if (loadArgExists)
{
Log.Error($">> Can't find save with name {saveName}! Exiting...");
if (saveName != string.Empty)
{
Log.Error($">> Can't find save with name {saveName}! Exiting...");
}
else
{
Log.Error($">> Can't find any save in the folder! Exiting...");
}
}
else
{
Expand Down
10 changes: 10 additions & 0 deletions NebulaPatcher/Patches/Dynamic/Dedicated_Server_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,15 @@ public static void DysonSwarmSetSailCapacity_Prefix(DysonSwarm __instance)
__instance.swarmInfoBuffer = null;
}
}


// From user report
[HarmonyPrefix]
[HarmonyPatch(typeof(UnityEngine.UI.Graphic), nameof(UnityEngine.UI.Graphic.DoMeshGeneration))]
[HarmonyPatch(typeof(UnityEngine.UI.Graphic), nameof(UnityEngine.UI.Graphic.DoLegacyMeshGeneration))]
public static bool DoMeshGeneration_Prefix()
{
return false;
}
}
}
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/GameData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public static void GameTick_Postfix(GameData __instance, long time)
return;
}

Multiplayer.Session.Couriers.GameTick(time);
Multiplayer.Session.Couriers.GameTick();

if (Multiplayer.Session.LocalPlayer.IsHost)
{
Expand Down
8 changes: 8 additions & 0 deletions NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public static void _OnOpen_Postfix()
AddMultiplayerJoinMenu();
}

[HarmonyPostfix]
[HarmonyPatch(nameof(UIMainMenu.OnUpdateLogButtonClick))]
public static void OnUpdateLogButtonClick_Postfix(UIMainMenu __instance)
{
// Return to main menu when update log is opened
OnMultiplayerBackButtonClick();
}

// Main Menu
private static void AddMultiplayerButton()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public static void IdleCourierToWork(DispenserComponent dispenser)
if (Multiplayer.IsActive)
{
Multiplayer.Session.Network.SendPacketToLocalPlanet(
new DispenserCourierPacket(Multiplayer.Session.LocalPlayer.Id,
new DispenserCourierPacket(GameMain.mainPlayer.planetId,
Multiplayer.Session.LocalPlayer.Id,
dispenser.id,
dispenser.workCourierDatas[dispenser.workCourierCount].itemId,
dispenser.workCourierDatas[dispenser.workCourierCount].itemCount));
Expand Down
Loading

0 comments on commit ee63b66

Please sign in to comment.