diff --git a/Fika.Core/Coop/BotClasses/CoopBotInventoryController.cs b/Fika.Core/Coop/BotClasses/CoopBotInventoryController.cs index 4fb28c44..3dd9336a 100644 --- a/Fika.Core/Coop/BotClasses/CoopBotInventoryController.cs +++ b/Fika.Core/Coop/BotClasses/CoopBotInventoryController.cs @@ -30,8 +30,7 @@ public override void Execute(GClass2837 operation, [CanBeNull] Callback callback packet.ItemControllerExecutePacket = new() { CallbackId = operation.Id, - OperationBytes = opBytes, - InventoryId = ID + OperationBytes = opBytes }; CoopBot.PacketSender?.InventoryPackets?.Enqueue(packet); diff --git a/Fika.Core/Coop/ClientClasses/CoopClientInventoryController.cs b/Fika.Core/Coop/ClientClasses/CoopClientInventoryController.cs index ffcaf129..355164a4 100644 --- a/Fika.Core/Coop/ClientClasses/CoopClientInventoryController.cs +++ b/Fika.Core/Coop/ClientClasses/CoopClientInventoryController.cs @@ -66,8 +66,7 @@ public override void Execute(GClass2837 operation, [CanBeNull] Callback callback packet.ItemControllerExecutePacket = new() { CallbackId = operation.Id, - OperationBytes = opBytes, - InventoryId = ID + OperationBytes = opBytes }; CoopPlayer.PacketSender?.InventoryPackets?.Enqueue(packet); @@ -79,8 +78,6 @@ public override void Execute(GClass2837 operation, [CanBeNull] Callback callback } else if (MatchmakerAcceptPatches.IsClient) { - ConsoleScreen.Log(operation.GetType().ToString()); - // Do not replicate picking up quest items, throws an error on the other clients if (operation is GClass2839 pickupOperation) { @@ -91,12 +88,6 @@ public override void Execute(GClass2837 operation, [CanBeNull] Callback callback } } - if (operation is GClass2870) - { - base.Execute(operation, callback); - return; - } - InventoryPacket packet = new() { HasItemControllerExecutePacket = true @@ -119,8 +110,7 @@ public override void Execute(GClass2837 operation, [CanBeNull] Callback callback packet.ItemControllerExecutePacket = new() { CallbackId = operationNum, - OperationBytes = opBytes, - InventoryId = ID + OperationBytes = opBytes }; CoopPlayer.PacketSender?.InventoryPackets?.Enqueue(packet); @@ -165,24 +155,27 @@ public void HandleResult(Result result) clientOperationManager = this, result = result }; - if (callbackManager.result.Succeed && callbackManager.result.Value != EOperationStatus.Failed) + + if (callbackManager.result.Succeed) { EOperationStatus value = callbackManager.result.Value; if (value == EOperationStatus.Started) { localOperationStatus = EOperationStatus.Started; serverOperationStatus = EOperationStatus.Started; - operation.vmethod_0(new Callback(callbackManager.HandleResult), false); + operation.vmethod_0(new Callback(callbackManager.HandleResult), true); return; } - /*if (value == EOperationStatus.Finished) + if (value == EOperationStatus.Finished) { serverOperationStatus = EOperationStatus.Finished; - localOperationStatus = EOperationStatus.Finished; - operation.Dispose(); - callback.Succeed(); - return; - }*/ + if (localOperationStatus == serverOperationStatus) + { + operation.Dispose(); + callback.Succeed(); + return; + } + } } else { @@ -202,33 +195,29 @@ private class ClientInventoryCallbackManager public void HandleResult(IResult executeResult) { - if (!executeResult.Succeed) + if (!executeResult.Succeed && (executeResult.Error is not "skipped skippable" or "skipped _completed")) { FikaPlugin.Instance.FikaLogger.LogError($"{clientOperationManager.inventoryController.ID} - Client operation critical failure: {clientOperationManager.inventoryController.ID} - {clientOperationManager.operation}\r\nError: {executeResult.Error}"); } - clientOperationManager.operation.Dispose(); - clientOperationManager.callback.Invoke(result); + clientOperationManager.localOperationStatus = EOperationStatus.Finished; - /*clientOperationManager.localOperationStatus = EOperationStatus.Finished; - EOperationStatus? serverOperationStatus = clientOperationManager.serverOperationStatus; - EOperationStatus? eoperationStatus = clientOperationManager.localOperationStatus; - if ((serverOperationStatus.GetValueOrDefault() == eoperationStatus.GetValueOrDefault()) & (serverOperationStatus != null == (eoperationStatus != null))) + if (clientOperationManager.localOperationStatus == clientOperationManager.serverOperationStatus) { clientOperationManager.operation.Dispose(); clientOperationManager.callback.Invoke(result); return; - }*/ - /*if (clientOperationManager.serverOperationStatus != null) + } + + if (clientOperationManager.serverOperationStatus != null) { - eoperationStatus = clientOperationManager.serverOperationStatus; - if ((eoperationStatus.GetValueOrDefault() == EOperationStatus.Failed) & (eoperationStatus != null)) + if (clientOperationManager.serverOperationStatus == EOperationStatus.Failed) { clientOperationManager.operation.Dispose(); clientOperationManager.callback.Invoke(result); } - }*/ + } } } } -} +} \ No newline at end of file diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index 36640187..ec873685 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -677,7 +677,7 @@ public override async Task vmethod_2(int playerId, Vector3 position { myPlayer.HealthController.DisableMetabolism(); NotificationManagerClass.DisplayMessageNotification("Metabolism disabled", iconType: EFT.Communications.ENotificationIconType.Alert); - } + } } CoopPlayer coopPlayer = (CoopPlayer)myPlayer; diff --git a/Fika.Core/Coop/ObservedClasses/ObservedInventoryController.cs b/Fika.Core/Coop/ObservedClasses/ObservedInventoryController.cs index b59e514c..a0c3852f 100644 --- a/Fika.Core/Coop/ObservedClasses/ObservedInventoryController.cs +++ b/Fika.Core/Coop/ObservedClasses/ObservedInventoryController.cs @@ -1,9 +1,6 @@ // © 2024 Lacyway All Rights Reserved -using Comfort.Common; -using Diz.LanguageExtensions; using EFT; -using EFT.InventoryLogic; namespace Fika.Core.Coop.ObservedClasses { @@ -37,7 +34,7 @@ public override bool IsInventoryBlocked() return false; } - public override void InProcess(TraderControllerClass executor, Item item, ItemAddress to, bool succeed, GInterface338 operation, Callback callback) + /*public override void InProcess(TraderControllerClass executor, Item item, ItemAddress to, bool succeed, GInterface338 operation, Callback callback) { if (!succeed) { @@ -50,6 +47,6 @@ public override void InProcess(TraderControllerClass executor, Item item, ItemAd return; } callback.Succeed(); - } + }*/ } } diff --git a/Fika.Core/Coop/Players/CoopPlayer.cs b/Fika.Core/Coop/Players/CoopPlayer.cs index e3445048..4485135b 100644 --- a/Fika.Core/Coop/Players/CoopPlayer.cs +++ b/Fika.Core/Coop/Players/CoopPlayer.cs @@ -1052,6 +1052,32 @@ public virtual void DoObservedVault(VaultPacket vaultPacket) } + public void HandleCallbackFromServer(in OperationCallbackPacket operationCallbackPacket) + { + if (OperationCallbacks.TryGetValue(operationCallbackPacket.CallbackId, out Callback callback)) + { + if (operationCallbackPacket.OperationStatus != EOperationStatus.Started) + { + OperationCallbacks.Remove(operationCallbackPacket.CallbackId); + } + if (operationCallbackPacket.OperationStatus != EOperationStatus.Failed) + { + callback(new Result(operationCallbackPacket.OperationStatus)); + } + else + { + callback(new Result(EOperationStatus.Failed) + { + Error = operationCallbackPacket.Error + }); + } + } + else + { + FikaPlugin.Instance.FikaLogger.LogError($"Could not find CallbackId {operationCallbackPacket.CallbackId}!"); + } + } + public virtual void HandleInventoryPacket(in InventoryPacket packet) { if (packet.HasItemControllerExecutePacket) @@ -1064,36 +1090,9 @@ public virtual void HandleInventoryPacket(in InventoryPacket packet) { GStruct411 result = ToInventoryOperation(binaryReader.ReadPolymorph()); - if (IsYourPlayer) - { - if (OperationCallbacks.TryGetValue(packet.ItemControllerExecutePacket.CallbackId, out Callback callback)) - { - OperationCallbacks.Remove(packet.ItemControllerExecutePacket.CallbackId); - if (!result.Failed) - { - callback(new Result(EOperationStatus.Started)); - } - else - { - callback(new Result(EOperationStatus.Failed) - { - Error = result.Error.ToString() - }); - } - } - else - { - FikaPlugin.Instance.FikaLogger.LogError($"Could not find CallbackId {packet.ItemControllerExecutePacket.CallbackId}!"); - } - return; - } - InventoryOperationHandler opHandler = new() { - opResult = result, - inventoryId = _inventoryController.ID, - operationId = packet.ItemControllerExecutePacket.CallbackId, - netId = NetId + opResult = result }; opHandler.opResult.Value.vmethod_0(new Callback(opHandler.HandleResult), false); @@ -1144,7 +1143,7 @@ public virtual void HandleInventoryPacket(in InventoryPacket packet) } } - // Fix for folding not replicating + /*// Fix for folding not replicating if (result.Value is GClass2858 foldOperation) { if (HandsController is CoopObservedFirearmController observedFirearmController) @@ -1154,14 +1153,14 @@ public virtual void HandleInventoryPacket(in InventoryPacket packet) observedFirearmController.InitiateOperation().Start(foldOperation, null); } } - } + }*/ } catch (Exception exception) { FikaPlugin.Instance.FikaLogger.LogError($"ItemControllerExecutePacket::Exception thrown: {exception}"); if (MatchmakerAcceptPatches.IsServer) { - OperationCallbackPacket callbackPacket = new(NetId, packet.ItemControllerExecutePacket.CallbackId, false); + OperationCallbackPacket callbackPacket = new(NetId, packet.ItemControllerExecutePacket.CallbackId, EOperationStatus.Failed); Singleton.Instance.SendDataToAll(new(), ref callbackPacket, LiteNetLib.DeliveryMethod.ReliableOrdered); } } @@ -1171,7 +1170,7 @@ public virtual void HandleInventoryPacket(in InventoryPacket packet) FikaPlugin.Instance.FikaLogger.LogError("ItemControllerExecutePacket: inventory was null!"); if (MatchmakerAcceptPatches.IsServer) { - OperationCallbackPacket callbackPacket = new(NetId, packet.ItemControllerExecutePacket.CallbackId, false); + OperationCallbackPacket callbackPacket = new(NetId, packet.ItemControllerExecutePacket.CallbackId, EOperationStatus.Failed); Singleton.Instance.SendDataToAll(new(), ref callbackPacket, LiteNetLib.DeliveryMethod.ReliableOrdered); } } @@ -1351,7 +1350,6 @@ public virtual void HandleDamagePacket(DamagePacket packet) } } - //ClientApplyDamageInfo(damageInfo, packet.ApplyDamageInfo.BodyPartType, packet.ApplyDamageInfo.ColliderType, packet.ApplyDamageInfo.Absorbed); ClientApplyShot(damageInfo, packet.DamageInfo.BodyPartType, packet.DamageInfo.ColliderType, packet.DamageInfo.ArmorPlateCollider); } @@ -1384,7 +1382,7 @@ public virtual void SetupDogTag() dogtagComponent.Level = Profile.Info.Experience > 0 ? Profile.Info.Level : 1; dogtagComponent.Time = DateTime.Now; dogtagComponent.Status = "Killed by "; - dogtagComponent.WeaponName = LastDamageInfo.Weapon.Name; + dogtagComponent.WeaponName = LastDamageInfo.Weapon != null ? LastDamageInfo.Weapon.Name : "Unknown"; dogtagComponent.GroupId = GroupId; } } @@ -1470,9 +1468,6 @@ internal void HandleKeyEvent() private class InventoryOperationHandler() { public GStruct411 opResult; - public string inventoryId; - public uint operationId; - public int netId; internal void HandleResult(IResult result) { @@ -1480,26 +1475,6 @@ internal void HandleResult(IResult result) { FikaPlugin.Instance.FikaLogger.LogError($"Error in operation: {result.Error}"); } - if (MatchmakerAcceptPatches.IsServer) - { - InventoryPacket packet = new(netId) - { - HasItemControllerExecutePacket = true - }; - - using MemoryStream memoryStream = new(); - using BinaryWriter binaryWriter = new(memoryStream); - binaryWriter.WritePolymorph(GClass1632.FromInventoryOperation(opResult.Value, false)); - byte[] opBytes = memoryStream.ToArray(); - packet.ItemControllerExecutePacket = new() - { - CallbackId = operationId, - OperationBytes = opBytes, - InventoryId = inventoryId - }; - - Singleton.Instance.SendDataToAll(new(), ref packet, LiteNetLib.DeliveryMethod.ReliableOrdered); - } } } diff --git a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs index e69a00c7..983545eb 100644 --- a/Fika.Core/Coop/Players/ObservedCoopPlayer.cs +++ b/Fika.Core/Coop/Players/ObservedCoopPlayer.cs @@ -150,7 +150,7 @@ public static async Task CreateObservedPlayer( player.IsYourPlayer = false; - InventoryControllerClass inventoryController = new ObservedInventoryController(player, profile, false); + InventoryControllerClass inventoryController = new ObservedInventoryController(player, profile, true); PlayerHealthController tempController = new(profile.Health, player, inventoryController, profile.Skills, aiControl); byte[] healthBytes = tempController.SerializeState(); diff --git a/Fika.Core/FikaPlugin.cs b/Fika.Core/FikaPlugin.cs index 5c35e104..f08d005b 100644 --- a/Fika.Core/FikaPlugin.cs +++ b/Fika.Core/FikaPlugin.cs @@ -27,6 +27,7 @@ using Fika.Core.UI.Models; using Fika.Core.UI.Patches; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; @@ -49,26 +50,20 @@ public class FikaPlugin : BaseUnityPlugin /// Stores the Instance of this Plugin /// public static FikaPlugin Instance; - public static InternalBundleLoader BundleLoaderPlugin { get; private set; } - /// /// If any mod dependencies fail, show an error. This is a flag to say it has occurred. /// private bool ShownDependencyError { get; set; } - /// /// This is the Official EFT Version defined by BSG /// public static string EFTVersionMajor { get; internal set; } - public static string[] LoadedPlugins { get; private set; } - public ManualLogSource FikaLogger { get => Logger; } - public BotDifficulties BotDifficulties; - public string Locale { get; private set; } = "en"; + public string[] LocalIPs; public static Dictionary RespectedPlayersList = new() { @@ -356,7 +351,7 @@ private void SetupConfig() ForceIP = Config.Bind("Network", "Force IP", "", new ConfigDescription("Forces the server when hosting to use this IP when broadcasting to the backend instead of automatically trying to fetch it. Leave empty to disable.", tags: new ConfigurationManagerAttributes() { Order = 6 })); - ForceBindIP = Config.Bind("Network", "Force Bind IP", "", new ConfigDescription("Forces the server when hosting to use this local IP when starting the server. Useful if you are hosting on a VPN.", new AcceptableValueList(GetLocalIPAddress()), new ConfigurationManagerAttributes() { Order = 5 })); + ForceBindIP = Config.Bind("Network", "Force Bind IP", "", new ConfigDescription("Forces the server when hosting to use this local IP when starting the server. Useful if you are hosting on a VPN.", new AcceptableValueList(GetLocalAddresses()), new ConfigurationManagerAttributes() { Order = 5 })); AutoRefreshRate = Config.Bind("Network", "Auto Server Refresh Rate", 10f, new ConfigDescription("Every X seconds the client will ask the server for the list of matches while at the lobby screen.", new AcceptableValueRange(3f, 60f), new ConfigurationManagerAttributes() { Order = 4 })); @@ -373,7 +368,7 @@ private void SetupConfig() ArmpitDamageMultiplier = Config.Bind("Gameplay", "Armpit Damage Multiplier", 1f, new ConfigDescription("X multiplier to damage taken on the armpits collider. 0.2 = 20%", new AcceptableValueRange(0.2f, 1f), new ConfigurationManagerAttributes() { Order = 1 })); } - private string[] GetLocalIPAddress() + private string[] GetLocalAddresses() { IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); List ips = []; @@ -385,6 +380,8 @@ private string[] GetLocalIPAddress() ips.Add(ip.ToString()); } } + + LocalIPs = ips.Skip(1).ToArray(); return [.. ips]; } diff --git a/Fika.Core/Networking/FikaClient.cs b/Fika.Core/Networking/FikaClient.cs index 962378a2..240d94ec 100644 --- a/Fika.Core/Networking/FikaClient.cs +++ b/Fika.Core/Networking/FikaClient.cs @@ -58,7 +58,6 @@ public NetManager NetClient protected void Start() { - packetProcessor.SubscribeNetSerializable(OnPlayerStatePacketReceived); packetProcessor.SubscribeNetSerializable(OnGameTimerPacketReceived); packetProcessor.SubscribeNetSerializable(OnFirearmPacketReceived); @@ -81,7 +80,7 @@ protected void Start() packetProcessor.SubscribeNetSerializable(OnSendCharacterPacketReceived); packetProcessor.SubscribeNetSerializable(OnAssignNetIdPacketReceived); packetProcessor.SubscribeNetSerializable(OnSyncNetIdPacketReceived); - packetProcessor.SubscribeNetSerializable(OnOperationCallbackPacketReceived); + packetProcessor.SubscribeNetSerializable(OnOperationCallbackPacketReceived); packetProcessor.SubscribeNetSerializable(OnSessionSettingsPacketReceived); _netClient = new NetManager(this) @@ -129,21 +128,11 @@ private void OnSessionSettingsPacketReceived(SessionSettingsPacket packet, NetPe } } - private void OnOperationCallbackPacketReceived(OperationCallbackPacket packet, NetPeer peer) + private void OnOperationCallbackPacketReceived(OperationCallbackPacket packet) { if (Players.TryGetValue(packet.NetId, out CoopPlayer player) && player.IsYourPlayer) { - if (player.OperationCallbacks.TryGetValue(packet.CallbackId, out Callback callback)) - { - callback(new Result(EOperationStatus.Failed) - { - Error = "CRITICAL ERROR DURING PROCESSING OF PACKET" - }); - } - else - { - FikaPlugin.Instance.FikaLogger.LogError($"OnOperationCallbackPacketReceived: Could not find CallbackId {packet.CallbackId}!"); - } + player.HandleCallbackFromServer(in packet); } } @@ -654,7 +643,7 @@ protected void Awake() CoopHandler = CoopHandler.CoopHandlerParent.GetComponent(); } - void Update() + protected void Update() { _netClient.PollEvents(); @@ -664,7 +653,7 @@ void Update() } } - void OnDestroy() + protected void OnDestroy() { _netClient?.Stop(); FikaEventDispatcher.DispatchEvent(new FikaClientDestroyedEvent(this)); @@ -704,7 +693,7 @@ public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketRead public void OnNetworkLatencyUpdate(NetPeer peer, int latency) { Ping = latency; - NetworkGameSession.RTT = Ping; + NetworkGameSession.RTT = peer.RoundTripTime; NetworkGameSession.LossPercent = (int)NetClient.Statistics.PacketLossPercent; } diff --git a/Fika.Core/Networking/FikaSerialization.cs b/Fika.Core/Networking/FikaSerialization.cs index 9b7a57e7..709c3f49 100644 --- a/Fika.Core/Networking/FikaSerialization.cs +++ b/Fika.Core/Networking/FikaSerialization.cs @@ -701,14 +701,12 @@ public struct ItemControllerExecutePacket { public uint CallbackId; public byte[] OperationBytes; - public string InventoryId; public static ItemControllerExecutePacket Deserialize(NetDataReader reader) { ItemControllerExecutePacket packet = new() { CallbackId = reader.GetUInt(), OperationBytes = reader.GetByteArray(), - InventoryId = reader.GetString() }; return packet; } @@ -716,7 +714,6 @@ public static void Serialize(NetDataWriter writer, ItemControllerExecutePacket p { writer.Put(packet.CallbackId); writer.PutByteArray(packet.OperationBytes); - writer.Put(packet.InventoryId); } } diff --git a/Fika.Core/Networking/FikaServer.cs b/Fika.Core/Networking/FikaServer.cs index 3c49f205..54c419cd 100644 --- a/Fika.Core/Networking/FikaServer.cs +++ b/Fika.Core/Networking/FikaServer.cs @@ -5,6 +5,7 @@ using EFT; using EFT.AssetsManager; using EFT.Interactive; +using EFT.InventoryLogic; using EFT.UI; using Fika.Core.Coop.Components; using Fika.Core.Coop.GameMode; @@ -15,11 +16,13 @@ using Fika.Core.Networking.Http; using Fika.Core.Networking.Http.Models; using Fika.Core.Networking.Packets.GameWorld; +using Fika.Core.Networking.Packets.Player; using LiteNetLib; using LiteNetLib.Utils; using Open.Nat; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; @@ -504,11 +507,91 @@ private void OnInventoryPacketReceived(InventoryPacket packet, NetPeer peer) { if (Players.TryGetValue(packet.NetId, out CoopPlayer playerToApply)) { - playerToApply?.PacketReceiver?.InventoryPackets?.Enqueue(packet); - } + using MemoryStream memoryStream = new(packet.ItemControllerExecutePacket.OperationBytes); + using BinaryReader binaryReader = new(memoryStream); + try + { + GStruct411 result = playerToApply.ToInventoryOperation(binaryReader.ReadPolymorph()); + + InventoryOperationHandler opHandler = new() + { + opResult = result, + operationId = packet.ItemControllerExecutePacket.CallbackId, + netId = playerToApply.NetId, + peer = peer, + server = this + }; - /*_dataWriter.Reset(); - SendDataToAll(_dataWriter, ref packet, DeliveryMethod.ReliableOrdered, peer);*/ + OperationCallbackPacket operationCallbackPacket = new(playerToApply.NetId, packet.ItemControllerExecutePacket.CallbackId, EOperationStatus.Started); + SendDataToPeer(peer, new(), ref operationCallbackPacket, DeliveryMethod.ReliableOrdered); + + opHandler.opResult.Value.vmethod_0(new Callback(opHandler.HandleResult), false); + + // TODO: Hacky workaround to fix errors due to each client generating new IDs. Might need to find a more 'elegant' solution later. + // Unknown what problems this might cause so far. + if (result.Value is GClass2861 unloadOperation) + { + if (unloadOperation.InternalOperation is GClass2872 internalSplitOperation) + { + Item item = internalSplitOperation.To.Item; + if (item != null) + { + if (item.Id != internalSplitOperation.CloneId && item.TemplateId == internalSplitOperation.Item.TemplateId) + { + item.Id = internalSplitOperation.CloneId; + } + else + { + FikaPlugin.Instance.FikaLogger.LogWarning($"Matching failed: ItemID: {item.Id}, SplitOperationItemID: {internalSplitOperation.To.Item.Id}"); + } + } + else + { + FikaPlugin.Instance.FikaLogger.LogError("Split: Item was null"); + } + } + } + + // TODO: Same as above. + if (result.Value is GClass2872 splitOperation) + { + Item item = splitOperation.To.Item; + if (item != null) + { + if (item.Id != splitOperation.CloneId && item.TemplateId == splitOperation.Item.TemplateId) + { + item.Id = splitOperation.CloneId; + } + else + { + FikaPlugin.Instance.FikaLogger.LogWarning($"Matching failed: ItemID: {item.Id}, SplitOperationItemID: {splitOperation.To.Item.Id}"); + } + } + else + { + FikaPlugin.Instance.FikaLogger.LogError("Split: Item was null"); + } + } + + /*// Fix for folding not replicating + if (result.Value is GClass2858 foldOperation) + { + if (playerToApply.HandsController is CoopObservedFirearmController observedFirearmController) + { + if (observedFirearmController.Weapon != null && observedFirearmController.Weapon.Foldable != null) + { + observedFirearmController.InitiateOperation().Start(foldOperation, null); + } + } + }*/ + } + catch (Exception exception) + { + FikaPlugin.Instance.FikaLogger.LogError($"ItemControllerExecutePacket::Exception thrown: {exception}"); + OperationCallbackPacket callbackPacket = new(playerToApply.NetId, packet.ItemControllerExecutePacket.CallbackId, EOperationStatus.Failed); + SendDataToAll(new(), ref callbackPacket, LiteNetLib.DeliveryMethod.ReliableOrdered); + } + } } private void OnDamagePacketReceived(DamagePacket packet, NetPeer peer) @@ -677,5 +760,51 @@ public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelN { packetProcessor.ReadAllPackets(reader, peer); } + + private class InventoryOperationHandler + { + public GStruct411 opResult; + public uint operationId; + public int netId; + public NetPeer peer; + public FikaServer server; + + internal void HandleResult(IResult result) + { + NetDataWriter writer = new(); + OperationCallbackPacket operationCallbackPacket; + + if (!result.Succeed) + { + FikaPlugin.Instance.FikaLogger.LogError($"Error in operation: {result.Error ?? "An unknown error has occured"}"); + operationCallbackPacket = new(netId, operationId, EOperationStatus.Failed, result.Error ?? "An unknown error has occured"); + writer.Reset(); + server.SendDataToPeer(peer, writer, ref operationCallbackPacket, DeliveryMethod.ReliableOrdered); + + return; + } + + InventoryPacket packet = new(netId) + { + HasItemControllerExecutePacket = true + }; + + using MemoryStream memoryStream = new(); + using BinaryWriter binaryWriter = new(memoryStream); + binaryWriter.WritePolymorph(GClass1632.FromInventoryOperation(opResult.Value, false)); + byte[] opBytes = memoryStream.ToArray(); + packet.ItemControllerExecutePacket = new() + { + CallbackId = operationId, + OperationBytes = opBytes + }; + + server.SendDataToAll(writer, ref packet, DeliveryMethod.ReliableOrdered, peer); + + operationCallbackPacket = new(netId, operationId, EOperationStatus.Finished); + writer.Reset(); + server.SendDataToPeer(peer, writer, ref operationCallbackPacket, DeliveryMethod.ReliableOrdered); + } + } } } diff --git a/Fika.Core/Networking/Packets/Backend/SessionSettingsPacket.cs b/Fika.Core/Networking/Packets/Backend/SessionSettingsPacket.cs index cc01818f..d71661c5 100644 --- a/Fika.Core/Networking/Packets/Backend/SessionSettingsPacket.cs +++ b/Fika.Core/Networking/Packets/Backend/SessionSettingsPacket.cs @@ -12,7 +12,7 @@ public void Deserialize(NetDataReader reader) IsRequest = reader.GetBool(); if (!IsRequest) { - MetabolismDisabled = reader.GetBool(); + MetabolismDisabled = reader.GetBool(); } } @@ -21,7 +21,7 @@ public void Serialize(NetDataWriter writer) writer.Put(IsRequest); if (!IsRequest) { - writer.Put(MetabolismDisabled); + writer.Put(MetabolismDisabled); } } } diff --git a/Fika.Core/Networking/Packets/Player/OperationCallbackPacket.cs b/Fika.Core/Networking/Packets/Player/OperationCallbackPacket.cs index 8f44c714..527007fd 100644 --- a/Fika.Core/Networking/Packets/Player/OperationCallbackPacket.cs +++ b/Fika.Core/Networking/Packets/Player/OperationCallbackPacket.cs @@ -1,25 +1,35 @@ -using LiteNetLib.Utils; +using EFT; +using LiteNetLib.Utils; namespace Fika.Core.Networking.Packets.Player { - public struct OperationCallbackPacket(int netId, uint callbackId, bool success) : INetSerializable + public struct OperationCallbackPacket(int netId, uint callbackId, EOperationStatus operationStatus, string error = null) : INetSerializable { public int NetId = netId; public uint CallbackId = callbackId; - public bool Success = success; + public EOperationStatus OperationStatus = operationStatus; + public string Error = error; public void Deserialize(NetDataReader reader) { NetId = reader.GetInt(); CallbackId = reader.GetUInt(); - Success = reader.GetBool(); + OperationStatus = (EOperationStatus)reader.GetInt(); + if (OperationStatus == EOperationStatus.Failed) + { + Error = reader.GetString(); + } } public void Serialize(NetDataWriter writer) { writer.Put(NetId); writer.Put(CallbackId); - writer.Put(Success); + writer.Put((int)OperationStatus); + if (OperationStatus == EOperationStatus.Failed) + { + writer.Put(Error); + } } } } diff --git a/Fika.Core/UI/Custom/MatchMakerUIScript.cs b/Fika.Core/UI/Custom/MatchMakerUIScript.cs index 8c8ec3b6..e8989ec5 100644 --- a/Fika.Core/UI/Custom/MatchMakerUIScript.cs +++ b/Fika.Core/UI/Custom/MatchMakerUIScript.cs @@ -193,16 +193,16 @@ private IEnumerator JoinMatch(string profileId, string serverId, Button button) if (pingingClient.Init()) { int attempts = 0; - bool success = false; + bool success; do { attempts++; - if (pingingClient.PingEndPoint()) - { - pingingClient.NetClient.PollEvents(); - success = pingingClient.Received; - } + + pingingClient.PingEndPoint(); + pingingClient.NetClient.PollEvents(); + success = pingingClient.Received; + yield return new WaitForSeconds(0.1f); } while (!success && attempts < 50);