456 lines
16 KiB
C#
456 lines
16 KiB
C#
using BrewMonster;
|
|
using BrewMonster.Common;
|
|
using CSNetwork;
|
|
using CSNetwork.C2SCommand;
|
|
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.IO;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using BrewMonster.Scripts.Task;
|
|
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 = "";
|
|
|
|
CECStubbornFactionInfoSender m_stubbornFactionInfoSender;
|
|
public GameSession GameSession { get => _gameSession; }
|
|
public CECC2SCmdCache GetC2SCmdCache() { return _gameSession.CmdCache; }
|
|
#if UNITY_EDITOR
|
|
public bool isDebg;
|
|
private bool lastDebug;
|
|
|
|
|
|
public void OnValidate()
|
|
{
|
|
if (_gameSession != null && isDebg != lastDebug)
|
|
{
|
|
_gameSession.IsDebug = isDebg;
|
|
lastDebug = isDebg;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
protected override void Awake()
|
|
{
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
GameSession.Context = SynchronizationContext.Current;
|
|
|
|
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 void c2s_CmdCastSkill(int idSkill, byte byPVPMask, int iNumTarget, int[] aTargets)
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdCastSkill(idSkill, byPVPMask, iNumTarget, aTargets);
|
|
}
|
|
|
|
public static void c2s_CmdCastInstantSkill(int idSkill, byte byPVPMask, int iNumTarget, int[] aTargets)
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdCastInstantSkill(idSkill, byPVPMask, iNumTarget, aTargets);
|
|
}
|
|
|
|
public static void c2s_CmdCastPosSkill(int idSkill, Vector3 vDest, byte byPVPMask, int iNumTarget, int aTargets)
|
|
{
|
|
Instance._gameSession.c2s_CmdCastPosSkill(idSkill, vDest, byPVPMask, iNumTarget, aTargets);
|
|
}
|
|
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;
|
|
}
|
|
|
|
BMLogger.Log( $"Connecting to {Instance._ip} {Instance._port}...");
|
|
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(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(vCurPos, vDest, iTime, fSpeed, iMoveMode, wStamp);
|
|
}
|
|
protected override void Initialize()
|
|
{
|
|
BaseSecurity.Initizalize();
|
|
ProtocolFactory.RegisterAllProtocols();
|
|
_gameSession = new GameSession();
|
|
#if UNITY_EDITOR
|
|
var path = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("Assets"));
|
|
#else
|
|
var path = Application.persistentDataPath;
|
|
#endif
|
|
_gameSession.SetLogPath(Path.Combine(path, "Logs", "GameSession.log"));
|
|
_isInitialized = true;
|
|
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
public RoleInfo GetRoleInfo()
|
|
{
|
|
return _gameSession.GetRoleInfo();
|
|
}
|
|
/// <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.c2s_SendCmdGetIvtrDetailData(byPackage, callback);
|
|
}
|
|
public static void RequesrQueryPlayerCash()
|
|
{
|
|
Instance._gameSession.c2s_SendCmdQueryCashInfo();
|
|
}
|
|
public static void RequestDropEquipItem(byte index)
|
|
{
|
|
Instance._gameSession.RequestDropEquipItem(index);
|
|
}
|
|
public static void RequestEquipItemAsync(byte iIvtrIdx, byte iEquipIdx, Action callback = null)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdEquipItem(iIvtrIdx, iEquipIdx, callback);
|
|
}
|
|
public static void RequestPickupItem(int idItem, int tid)
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdPickUp(idItem, tid);
|
|
}
|
|
public static void RequestDropIvrtItem(byte index, int amount)
|
|
{
|
|
Instance._gameSession.RequestDropIvtrItem(index, amount);
|
|
}
|
|
public static void LoadConfigData()
|
|
{
|
|
Instance._gameSession.LoadConfigData();
|
|
}
|
|
public static void RequestCheckSecurityPassWd(string password)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdOpenFashionTrash(password);
|
|
}
|
|
public static void c2s_SendCmdContinueAction()
|
|
{
|
|
Instance._gameSession.c2s_SendCmdContinueAction();
|
|
}
|
|
public static void c2s_CmdReviveVillage()
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdReviveVillage();
|
|
}
|
|
public static void c2s_CmdReviveItem()
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdReviveItem();
|
|
}
|
|
public static void RequestReviveByPlayer()
|
|
{
|
|
Instance._gameSession.RequestReviveByPlayer();
|
|
}
|
|
|
|
public void RequestMallShopping(uint count, int good_id, int good_index, int good_pos)
|
|
{
|
|
var goods = new CMD_MallShopping.goods[]
|
|
{
|
|
new CMD_MallShopping.goods
|
|
{
|
|
goods_id = good_id,
|
|
goods_index = good_index,
|
|
goods_pos = good_pos
|
|
}
|
|
};
|
|
Instance._gameSession.c2s_SendCmdMallShopping(count, goods);
|
|
}
|
|
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_SendCmdNPCSevLearnSkill(int idSkill)
|
|
{
|
|
BMLogger.LogError("c2s_SendCmdNPCSevLearnSkill");
|
|
Instance._gameSession.c2s_SendCmdNPCSevLearnSkill(idSkill);
|
|
}
|
|
public static void c2s_CmdNPCSevHello(int nid)
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdNPCSevHello(nid);
|
|
}
|
|
|
|
public static void c2s_CmdNormalAttack(byte byPVPMask)
|
|
{
|
|
Instance._gameSession.c2s_CmdNormalAttack(byPVPMask);
|
|
}
|
|
|
|
public static void c2s_CmdCancelAction()
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdCancelAction();
|
|
}
|
|
|
|
public static void c2s_CmdUnselect()
|
|
{
|
|
Instance._gameSession.c2s_CmdUnselect();
|
|
}
|
|
|
|
public static void c2s_CmdSelectTarget(int idTarget)
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdSelectTarget(idTarget);
|
|
}
|
|
public static void c2s_CmdNPCSevWaypoint()
|
|
{
|
|
Instance._gameSession.c2s_SendCmdNPCSevWaypoint();
|
|
}
|
|
|
|
public static void c2s_CmdNPCSevMakeItem(int idSkill, int idItem, uint dwCount)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdNPCSevMakeItem(idSkill, idItem, dwCount);
|
|
}
|
|
|
|
public void GetFactionInfo(int iNumFaction, int[] aFactinoIDs)
|
|
{
|
|
m_stubbornFactionInfoSender.Add(iNumFaction, aFactinoIDs);
|
|
}
|
|
public static void c2s_CmdSendEnterPKPrecinct()
|
|
{
|
|
Instance._gameSession.c2s_CmdSendEnterPKPrecinctint();
|
|
}
|
|
|
|
public static void c2s_CmdNPCSevHeal()
|
|
{
|
|
|
|
}
|
|
public static void c2s_CmdNPCSevAcceptTask(int idTask,int idStorage,int idRefreshItem)
|
|
{
|
|
Instance._gameSession.c2s_CmdNPCSevAcceptTask(idTask, idStorage, idRefreshItem);
|
|
}
|
|
|
|
public static void c2s_CmdNPCSevReturnTask(int idTask, int iChoice)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdNPCSevReturnTask(idTask, iChoice);
|
|
}
|
|
|
|
public static void c2s_CmdNPCSevTaskMatter(int idTask)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdNPCSevTaskMatter(idTask);
|
|
}
|
|
|
|
public static void c2s_CmdNPCSevBuy(int itemNum, npc_trade_item[] items)
|
|
{
|
|
if (items == null || itemNum <= 0)
|
|
return;
|
|
Instance._gameSession.c2s_SendCmdNPCSevBuy(itemNum, items);
|
|
}
|
|
|
|
public static void c2s_CmdNPCSevSell(int itemNum, npc_sell_item[] items)
|
|
{
|
|
if (items == null || itemNum <= 0)
|
|
return;
|
|
Instance._gameSession.c2s_SendCmdNPCSevSell(itemNum, items);
|
|
}
|
|
public static void c2s_CmdStandUp()
|
|
{
|
|
Instance._gameSession.c2s_SendCmdStandUp();
|
|
}
|
|
#region Task
|
|
public static void c2s_CmdGetAllData(bool byPack, bool byEquip, bool byTask)
|
|
{
|
|
//Debug.Log("[Dat]- SendCmdGetAllData");
|
|
Instance._gameSession.c2s_SendCmdGetAllData(byPack, byEquip, byTask);
|
|
}
|
|
|
|
public static void c2s_CmdEmoteAction(uint wPose)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdEmoteAction(wPose);
|
|
}
|
|
public static void c2s_CmdTaskNotify( byte[] pBuf, uint sz)
|
|
{
|
|
if (Instance != null && Instance._gameSession != null)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdTaskNotify( pBuf, sz);
|
|
}
|
|
}
|
|
|
|
public static void c2s_CmdAutoTeamSetGoal(int type, int goal_id, int op)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdAutoTeamSetGoal(type, goal_id, op);//{ ::c2s_SendCmdAutoTeamSetGoal(type, goal_id, op); }
|
|
}
|
|
public static void c2s_CmdGatherMaterial(int idMatter, int iToolPack, int idToolIndex, int idTool, int idTask)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdGatherMaterial(idMatter, iToolPack, idToolIndex, idTool, idTask);
|
|
}
|
|
#endregion
|
|
|
|
public static void GetRoleBaseInfo(int iNumRole, List<int> aRoleIDs)
|
|
{
|
|
Instance._gameSession.GetRoleBaseInfo(iNumRole, aRoleIDs);
|
|
}
|
|
|
|
public static void GetRoleCustomizeData(int iNumRole, List<int> aRoleIDs)
|
|
{
|
|
Instance._gameSession.GetRoleCustomizeData(iNumRole, aRoleIDs);
|
|
}
|
|
|
|
public void LoadScene(string sceneName, LoadSceneMode mode, Action<bool> actDone)
|
|
{
|
|
// SceneLoadService.Load(sceneName, mode, 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();
|
|
_gameSession.Dispose();
|
|
CECNPC.ReleaseStaticRes();
|
|
}
|
|
|
|
public static void c2s_CmdGoto(float x, float y, float z)
|
|
{
|
|
Instance._gameSession.c2s_CmdGoto(x, y, z);
|
|
}
|
|
|
|
public static void c2s_SendCmdUseItem(byte byPackage, byte bySlot, int tid, byte byCount)
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdUseItem(byPackage, bySlot, tid, byCount);
|
|
}
|
|
|
|
// Send C2S::GET_EXT_PROP commadn data
|
|
public static void c2s_SendCmdGetExtProps()
|
|
{
|
|
Instance._gameSession.CmdCache.SendCmdExtProps();
|
|
}
|
|
|
|
public static void c2s_SendCmdGivePresent(int roleid, int mail_id, int goods_id, int goods_index, int goods_slot)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdGivePresent(roleid, mail_id, goods_id, goods_index, goods_slot);
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
_gameSession?.CmdCache?.Tick(Time.deltaTime);
|
|
}
|
|
|
|
public static void c2s_CmdPetCtrl(int idTarget, int cmd, object pParamBuf, int iParamLen)
|
|
{
|
|
Instance._gameSession.SendCmdPetCtrl(idTarget, cmd, pParamBuf, iParamLen);
|
|
}
|
|
|
|
// Pet commands ...
|
|
public static void c2s_CmdPetSummon(int iPetIdx)
|
|
{
|
|
Instance._gameSession.c2s_SendCmdPetSummon(iPetIdx);
|
|
}
|
|
}
|
|
} |