213 lines
7.3 KiB
C#
213 lines
7.3 KiB
C#
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<UnityGameSession>
|
|
{
|
|
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();
|
|
}
|
|
private void Start()
|
|
{
|
|
CECNPC.InitStaticRes();
|
|
}
|
|
/// <summary>
|
|
/// Send a
|
|
/// </summary>
|
|
/// <param name="protocol"></param>
|
|
/// <param name="complete"></param>
|
|
public static void SendProtocol(Protocol protocol, Action complete = null)
|
|
{
|
|
if (!Instance._isInitialized)
|
|
{
|
|
return;
|
|
}
|
|
Instance._gameSession.SendProtocol(protocol, complete);
|
|
}
|
|
|
|
/// <summary>Set the connection info. This MUST be called call before login</summary>
|
|
public static void SetConnectionInfo(string ip, int port)
|
|
{
|
|
BMLogger.Log($"Set connection info {ip} {port}");
|
|
Instance._ip = ip;
|
|
Instance._port = port;
|
|
}
|
|
|
|
public static async Task Login(string username, string password, Action<bool> onLoginComplete = null)
|
|
{
|
|
Instance._username = username;
|
|
Instance._password = password;
|
|
|
|
if (Instance._ip == "" || Instance._port == 0)
|
|
{
|
|
BMLogger.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)
|
|
{
|
|
BMLogger.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);
|
|
}
|
|
|
|
/// <summary>Make sure username and password is set before calling this method</summary>
|
|
private async Task ConnectAsync(string ip, int port)
|
|
{
|
|
if (!Instance._isInitialized)
|
|
{
|
|
BMLogger.LogError("GameSession is not initialized");
|
|
return;
|
|
}
|
|
await Instance._gameSession.ConnectAsync(ip, port);
|
|
}
|
|
|
|
/// <summary>Get the list of created characters</summary>
|
|
public static void GetRoleListAsync(Action<List<RoleInfo>> callback = null)
|
|
{
|
|
Instance._gameSession.GetRoleListAsync(callback);
|
|
}
|
|
|
|
public static void SelectRoleAsync(RoleInfo roleInfo, Action<RoleInfo> 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 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 static void c2s_CmdNPCSevHello(int nid)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdNPCSevHello(nid);
|
|
}
|
|
|
|
public static void c2s_CmdNormalAttack(byte byPVPMask)
|
|
{
|
|
Instance._gameSession.c2s_CmdNormalAttack(byPVPMask);
|
|
}
|
|
|
|
#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<bool> actDone)
|
|
{
|
|
StartCoroutine(LoadSceneCoroutine(sceneName, mode, actDone));
|
|
}
|
|
|
|
private IEnumerator LoadSceneCoroutine(string sceneName, LoadSceneMode mode, Action<bool> 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();
|
|
CECNPC.ReleaseStaticRes();
|
|
}
|
|
}
|
|
} |