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.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 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); } } 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(); } } }