From 9c92e33daf1ee62cfc520f1bc91007d6c74e6706 Mon Sep 17 00:00:00 2001 From: HungDK <> Date: Fri, 3 Oct 2025 15:40:37 +0700 Subject: [PATCH] Add request pickup item method --- .../Scripts/Network/UnityGameSession.cs | 417 +++++++++--------- 1 file changed, 211 insertions(+), 206 deletions(-) diff --git a/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs b/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs index 55c721e7a8..1408fc5b05 100644 --- a/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs +++ b/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs @@ -1,207 +1,212 @@ -using BrewMonster; -using CSNetwork; -using CSNetwork.Protocols; -using CSNetwork.Protocols.RPCData; -using CSNetwork.Security; -using ModelRenderer.Scripts.Common; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using UnityEngine; -using UnityEngine.Rendering; -using UnityEngine.SceneManagement; - -namespace BrewMonster.Network -{ - // How to connect to the server: - // 1. Set the connection info - // 2. Login - public class UnityGameSession : MonoSingleton - { - private GameSession _gameSession; - - private bool _isInitialized = false; - private string _ip = ""; - private int _port = 0; - private string _username = ""; - private string _password = ""; - - protected override void Awake() - { - Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); - base.Awake(); - } - /// - /// Send a - /// - /// - /// - public static void SendProtocol(Protocol protocol, Action complete = null) - { - if (!Instance._isInitialized) - { - return; - } - Instance._gameSession.SendProtocol(protocol, complete); - } - - /// Set the connection info. This MUST be called call before login - public static void SetConnectionInfo(string ip, int port) - { - Logger.Log($"Set connection info {ip} {port}"); - Instance._ip = ip; - Instance._port = port; - } - - public static async Task Login(string username, string password, Action onLoginComplete = null) - { - Instance._username = username; - Instance._password = password; - - if (Instance._ip == "" || Instance._port == 0) - { - Logger.LogError($"IP or port is not set {Instance._ip} {Instance._port}"); - onLoginComplete?.Invoke(false); - return; - } - - await Instance.ConnectAsync(Instance._ip, Instance._port); - - if (!Instance._gameSession.IsConnected) - { - Logger.LogError($"Failed to connect to {Instance._ip} {Instance._port}"); - onLoginComplete?.Invoke(false); - return; - } - - Instance._gameSession.LoginAsync(username, password, onLoginComplete); - } - public void c2s_SendCmdStopMove(in Vector3 vDest, float fSpeed, int iMoveMode, - byte byDir, ushort wStamp, int iTime) - { - Debug.LogWarning("HoangDev : c2s_SendCmdStopMove"); - Instance._gameSession.c2s_SendCmdStopMove(EC_Utility.ToNumerics(vDest), fSpeed, iMoveMode, byDir, wStamp, iTime); - } - public void c2s_CmdPlayerMove(in Vector3 vCurPos, in Vector3 vDest, - int iTime, float fSpeed, int iMoveMode, ushort wStamp) - { - Instance._gameSession.c2s_CmdPlayerMove(EC_Utility.ToNumerics(vCurPos), EC_Utility.ToNumerics(vDest), iTime, fSpeed, iMoveMode, wStamp); - } - protected override void Initialize() - { - BaseSecurity.Initizalize(); - ProtocolFactory.RegisterAllProtocols(); - _gameSession = new GameSession(); - _isInitialized = true; - - DontDestroyOnLoad(gameObject); - } - - /// Make sure username and password is set before calling this method - private async Task ConnectAsync(string ip, int port) - { - if (!Instance._isInitialized) - { - Logger.LogError("GameSession is not initialized"); - return; - } - await Instance._gameSession.ConnectAsync(ip, port); - } - - /// Get the list of created characters - public static void GetRoleListAsync(Action> callback = null) - { - Instance._gameSession.GetRoleListAsync(callback); - } - - public static void SelectRoleAsync(RoleInfo roleInfo, Action callback = null) - { - Instance._gameSession.SelectRoleAsync(roleInfo, callback); - } - public static void EnterWorldAsync(RoleInfo roleInfo, Action callback = null) - { - Debug.Log("EnterWorldAsync !!!!! nay "); - Instance._gameSession.EnterWorldAsync(roleInfo, callback); - } - public static void SendChatData(byte cChannel, in string szMsg, int iPack, int iSlot) - { - - Instance._gameSession.SendChatData(cChannel, szMsg, iPack, iSlot); - } - public static void RequestInventoryAsync(byte byPackage, Action callback = null) - { - Instance._gameSession.RequestInventoryAsync(byPackage, callback); - } - - public static void RequestEquipItemAsync(byte iIvtrIdx, byte iEquipIdx, Action callback = null) - { - Instance._gameSession.RequestEquipItem(iIvtrIdx, iEquipIdx, callback); - } - public static void RequestDropIvrtItem(byte index, int amount) - { - Instance._gameSession.RequestDropIvtrItem(index, amount); - } - - public static void RequestDropEquipItem(byte index) - { - Instance._gameSession.RequestDropEquipItem(index); - } - public static void RequestAllInventoriesAsync(Action callback = null, params byte[] packages) - { - if (packages == null || packages.Length == 0) - { - packages = new byte[] { 0, 1, 2 }; - } - - int remaining = packages.Length; - Action onOneDone = () => - { - remaining--; - if (remaining <= 0) - { - callback?.Invoke(); - } - }; - - foreach (var p in packages) - { - RequestInventoryAsync(p, onOneDone); - } - } - - #region Task - public static void c2s_CmdGetAllData(bool byPack, bool byEquip, bool byTask) - { - Debug.Log("[Dat]- SendCmdGetAllData"); - Instance._gameSession.c2s_SendCmdGetAllData(byPack, byEquip, byTask); - } - #endregion - - public void LoadScene(string sceneName, LoadSceneMode mode, Action actDone) - { - StartCoroutine(LoadSceneCoroutine(sceneName, mode, actDone)); - } - - private IEnumerator LoadSceneCoroutine(string sceneName, LoadSceneMode mode, Action actDone) - { - AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName, mode); - asyncLoad.allowSceneActivation = false; - while (!asyncLoad.isDone) - { - if (asyncLoad.progress >= 0.9f) - { - asyncLoad.allowSceneActivation = true; - } - yield return null; - } - actDone?.Invoke(true); - } - - void OnDestroy() - { - _gameSession.Disconnect(); - } - } +using BrewMonster; +using CSNetwork; +using CSNetwork.Protocols; +using CSNetwork.Protocols.RPCData; +using CSNetwork.Security; +using ModelRenderer.Scripts.Common; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using UnityEngine.Rendering; +using UnityEngine.SceneManagement; + +namespace BrewMonster.Network +{ + // How to connect to the server: + // 1. Set the connection info + // 2. Login + public class UnityGameSession : MonoSingleton + { + private GameSession _gameSession; + + private bool _isInitialized = false; + private string _ip = ""; + private int _port = 0; + private string _username = ""; + private string _password = ""; + + protected override void Awake() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + base.Awake(); + } + /// + /// Send a + /// + /// + /// + public static void SendProtocol(Protocol protocol, Action complete = null) + { + if (!Instance._isInitialized) + { + return; + } + Instance._gameSession.SendProtocol(protocol, complete); + } + + /// Set the connection info. This MUST be called call before login + public static void SetConnectionInfo(string ip, int port) + { + Logger.Log($"Set connection info {ip} {port}"); + Instance._ip = ip; + Instance._port = port; + } + + public static async Task Login(string username, string password, Action onLoginComplete = null) + { + Instance._username = username; + Instance._password = password; + + if (Instance._ip == "" || Instance._port == 0) + { + Logger.LogError($"IP or port is not set {Instance._ip} {Instance._port}"); + onLoginComplete?.Invoke(false); + return; + } + + await Instance.ConnectAsync(Instance._ip, Instance._port); + + if (!Instance._gameSession.IsConnected) + { + Logger.LogError($"Failed to connect to {Instance._ip} {Instance._port}"); + onLoginComplete?.Invoke(false); + return; + } + + Instance._gameSession.LoginAsync(username, password, onLoginComplete); + } + public void c2s_SendCmdStopMove(in Vector3 vDest, float fSpeed, int iMoveMode, + byte byDir, ushort wStamp, int iTime) + { + Debug.LogWarning("HoangDev : c2s_SendCmdStopMove"); + Instance._gameSession.c2s_SendCmdStopMove(EC_Utility.ToNumerics(vDest), fSpeed, iMoveMode, byDir, wStamp, iTime); + } + public void c2s_CmdPlayerMove(in Vector3 vCurPos, in Vector3 vDest, + int iTime, float fSpeed, int iMoveMode, ushort wStamp) + { + Instance._gameSession.c2s_CmdPlayerMove(EC_Utility.ToNumerics(vCurPos), EC_Utility.ToNumerics(vDest), iTime, fSpeed, iMoveMode, wStamp); + } + protected override void Initialize() + { + BaseSecurity.Initizalize(); + ProtocolFactory.RegisterAllProtocols(); + _gameSession = new GameSession(); + _isInitialized = true; + + DontDestroyOnLoad(gameObject); + } + + /// Make sure username and password is set before calling this method + private async Task ConnectAsync(string ip, int port) + { + if (!Instance._isInitialized) + { + Logger.LogError("GameSession is not initialized"); + return; + } + await Instance._gameSession.ConnectAsync(ip, port); + } + + /// Get the list of created characters + public static void GetRoleListAsync(Action> callback = null) + { + Instance._gameSession.GetRoleListAsync(callback); + } + + public static void SelectRoleAsync(RoleInfo roleInfo, Action callback = null) + { + Instance._gameSession.SelectRoleAsync(roleInfo, callback); + } + public static void EnterWorldAsync(RoleInfo roleInfo, Action callback = null) + { + Debug.Log("EnterWorldAsync !!!!! nay "); + Instance._gameSession.EnterWorldAsync(roleInfo, callback); + } + public static void SendChatData(byte cChannel, in string szMsg, int iPack, int iSlot) + { + + Instance._gameSession.SendChatData(cChannel, szMsg, iPack, iSlot); + } + public static void RequestInventoryAsync(byte byPackage, Action callback = null) + { + Instance._gameSession.RequestInventoryAsync(byPackage, callback); + } + + public static void RequestEquipItemAsync(byte iIvtrIdx, byte iEquipIdx, Action callback = null) + { + Instance._gameSession.RequestEquipItem(iIvtrIdx, iEquipIdx, callback); + } + public static void RequestDropIvrtItem(byte index, int amount) + { + Instance._gameSession.RequestDropIvtrItem(index, amount); + } + + public static void RequestDropEquipItem(byte index) + { + Instance._gameSession.RequestDropEquipItem(index); + } + + public static void RequestPickupItem(int idItem, int tid) + { + Instance._gameSession.RequestPickupItem(idItem, tid); + } + public static void RequestAllInventoriesAsync(Action callback = null, params byte[] packages) + { + if (packages == null || packages.Length == 0) + { + packages = new byte[] { 0, 1, 2 }; + } + + int remaining = packages.Length; + Action onOneDone = () => + { + remaining--; + if (remaining <= 0) + { + callback?.Invoke(); + } + }; + + foreach (var p in packages) + { + RequestInventoryAsync(p, onOneDone); + } + } + + #region Task + public static void c2s_CmdGetAllData(bool byPack, bool byEquip, bool byTask) + { + Debug.Log("[Dat]- SendCmdGetAllData"); + Instance._gameSession.c2s_SendCmdGetAllData(byPack, byEquip, byTask); + } + #endregion + + public void LoadScene(string sceneName, LoadSceneMode mode, Action actDone) + { + StartCoroutine(LoadSceneCoroutine(sceneName, mode, actDone)); + } + + private IEnumerator LoadSceneCoroutine(string sceneName, LoadSceneMode mode, Action actDone) + { + AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName, mode); + asyncLoad.allowSceneActivation = false; + while (!asyncLoad.isDone) + { + if (asyncLoad.progress >= 0.9f) + { + asyncLoad.allowSceneActivation = true; + } + yield return null; + } + actDone?.Invoke(true); + } + + void OnDestroy() + { + _gameSession.Disconnect(); + } + } } \ No newline at end of file