From eb19c01c2debdf32a9d1adde1d207bd8749cae80 Mon Sep 17 00:00:00 2001 From: kebiao <380000937@qq.com> Date: Mon, 1 Apr 2019 11:08:14 +0800 Subject: [PATCH] up --- .../kbengine_unity3d_plugins/AccountBase.cs | 8 + .../kbengine_unity3d_plugins/AvatarBase.cs | 14 ++ .../ClientSDKUpdater.cs | 158 ++++++++++++++++++ .../ClientSDKUpdater.cs.meta | 8 + .../kbengine_unity3d_plugins/Entity.cs | 12 ++ .../EntityComponent.cs | 8 + .../kbengine_unity3d_plugins/GateBase.cs | 8 + .../kbengine_unity3d_plugins/KBEngine.cs | 6 +- .../MessageReaderTCP.cs | 1 + .../kbengine_unity3d_plugins/MonsterBase.cs | 8 + .../kbengine_unity3d_plugins/NPCBase.cs | 8 + .../PacketSenderTCP.cs | 47 ++++-- .../ServerErrorDescrs.cs | 2 +- .../kbengine_unity3d_plugins/SpaceBase.cs | 8 + .../SpaceDuplicateBase.cs | 8 + .../kbengine_unity3d_plugins/SpacesBase.cs | 8 + .../SpawnPointBase.cs | 8 + 17 files changed, 300 insertions(+), 20 deletions(-) create mode 100644 Assets/Plugins/kbengine/kbengine_unity3d_plugins/ClientSDKUpdater.cs create mode 100644 Assets/Plugins/kbengine/kbengine_unity3d_plugins/ClientSDKUpdater.cs.meta diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/AccountBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/AccountBase.cs index d2de8757..41e1e72e 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/AccountBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/AccountBase.cs @@ -30,6 +30,14 @@ public AccountBase() { } + public override void onComponentsEnterworld() + { + } + + public override void onComponentsLeaveworld() + { + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_AccountBase(id, className); diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/AvatarBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/AvatarBase.cs index 9f0edac9..a73b9570 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/AvatarBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/AvatarBase.cs @@ -109,6 +109,20 @@ public AvatarBase() } + public override void onComponentsEnterworld() + { + component1.onEnterworld(); + component2.onEnterworld(); + component3.onEnterworld(); + } + + public override void onComponentsLeaveworld() + { + component1.onLeaveworld(); + component2.onLeaveworld(); + component3.onLeaveworld(); + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_AvatarBase(id, className); diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ClientSDKUpdater.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ClientSDKUpdater.cs new file mode 100644 index 00000000..c96e3b7d --- /dev/null +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ClientSDKUpdater.cs @@ -0,0 +1,158 @@ +namespace KBEngine +{ +#if UNITY_EDITOR + using UnityEngine; + using UnityEditor; + using System.Collections; + using System; + using System.IO; + using KBEngine; + + public class ClientSDKUpdater : MonoBehaviour + { + string warnUpdateSDK = ""; + MemoryStream sdkFileStream = null; + int downloadFiles = 0; + string sdkPath = ""; + string sdkTempPath = ""; + string sdkBakPath = ""; + + void Start() + { + string kbengineCoreFile = "KBEngine.cs"; + string[] res = System.IO.Directory.GetFiles(Application.dataPath, kbengineCoreFile, SearchOption.AllDirectories); + sdkPath = res[0].Replace(kbengineCoreFile, "").Replace("\\", "/"); + sdkPath = sdkPath.Remove(sdkPath.Length - 1, 1); + + sdkTempPath = sdkPath + "_temp"; + sdkBakPath = sdkPath + "_bak"; + + warnUpdateSDK = "Version does not match the server.\nClick to update KBEnginePlugin!\nPull from: " + KBEngineApp.app.getInitArgs().ip + ":" + KBEngineApp.app.getInitArgs().port; + installEvents(); + + GameObject[] objs = FindObjectsOfType(typeof(GameObject)) as GameObject[]; + foreach (GameObject child in objs) + { + if (!child.gameObject.GetComponent() && + !child.gameObject.GetComponent() && + !child.gameObject.GetComponent()) + { + child.gameObject.SetActive(false); + } + } + } + + public virtual void installEvents() + { + Event.registerIn("onImportClientSDK", this, "onImportClientSDK"); + } + + protected virtual void OnDestroy() + { + KBEngine.Event.deregisterOut(this); + } + + public void onImportClientSDK(int remainingFiles, string fileName, int fileSize, byte[] fileDatas) + { + if (sdkFileStream == null) + sdkFileStream = MemoryStream.createObject(); + + sdkFileStream.append(fileDatas, (uint)sdkFileStream.rpos, (uint)fileDatas.Length); + + warnUpdateSDK = "Download:" + fileName + " -> " + sdkFileStream.length() + "/" + fileSize + "bytes! " + (int)(((float)downloadFiles / (float)(downloadFiles + remainingFiles)) * 100) + "%"; + Debug.Log(warnUpdateSDK); + + if (sdkFileStream.length() == fileSize) + { + Debug.Log("onImportClientSDK: " + fileName + "->" + fileSize + "bytes success!"); + + string path = Path.GetDirectoryName(sdkTempPath + "//" + fileName); + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + + StreamWriter sw; + FileInfo t = new FileInfo(sdkTempPath + "//" + fileName); + string data = System.Text.Encoding.UTF8.GetString(sdkFileStream.data(), 0, fileSize); + sw = t.CreateText(); + sw.WriteLine(data); + sw.Close(); + sw.Dispose(); + + sdkFileStream.reclaimObject(); + sdkFileStream = null; + downloadFiles += 1; + + if (remainingFiles == 0) + { + warnUpdateSDK = ""; + downloadFiles = 0; + replaceNewSDK(); + } + } + } + + void downloadSDKFromServer() + { + downloadFiles = 0; + + if (Directory.Exists(sdkTempPath)) + Directory.Delete(sdkTempPath, true); + + Directory.CreateDirectory(sdkTempPath); + + if (sdkFileStream != null) + { + sdkFileStream.reclaimObject(); + sdkFileStream = null; + } + + // kbcmd options + string tool_options = "Unity"; + string callbackIP = ""; + UInt16 callbackPort = 0; + int clientWindowSize = (int)KBEngineApp.app.getInitArgs().TCP_RECV_BUFFER_MAX; + + Bundle bundle = Bundle.createObject(); + bundle.newMessage(Messages.messages["Loginapp_importClientSDK"]); + bundle.writeString(tool_options); + bundle.writeInt32(clientWindowSize); + bundle.writeString(callbackIP); + bundle.writeUint16(callbackPort); + bundle.send(KBEngineApp.app.networkInterface()); + } + + void replaceNewSDK() + { + System.IO.Directory.Move(sdkPath, sdkBakPath); + System.IO.Directory.Move(sdkTempPath, sdkPath); + + // 删除旧的SKD文件夹 + Directory.Delete(sdkBakPath, true); + + EditorApplication.isPlaying = false; + AssetDatabase.Refresh(); + } + + void OnGUI() + { + if (warnUpdateSDK.Length > 0) + { + GUI.contentColor = Color.red; + GUI.backgroundColor = Color.red; + + if (GUI.Button(new Rect(Screen.width * 0.25f, Screen.height * 0.4f, Screen.width * 0.5f, Screen.height * 0.2f), warnUpdateSDK)) + { + // 从服务器下载新的SDK + downloadSDKFromServer(); + } + } + } + + void Update() + { + + + } + } +#endif +} diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ClientSDKUpdater.cs.meta b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ClientSDKUpdater.cs.meta new file mode 100644 index 00000000..024f9661 --- /dev/null +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ClientSDKUpdater.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 33c007f050604fb44ab0afb745771062 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/Entity.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/Entity.cs index 981b795c..0d93b225 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/Entity.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/Entity.cs @@ -89,6 +89,16 @@ public virtual void onLoseCell() // 动态生成 } + public virtual void onComponentsEnterworld() + { + // 动态生成, 通知组件onEnterworld + } + + public virtual void onComponentsLeaveworld() + { + // 动态生成, 通知组件onLeaveworld + } + public virtual EntityCall getBaseEntityCall() { // 动态生成 @@ -257,6 +267,7 @@ public void enterWorld() try{ onEnterWorld(); + onComponentsEnterworld(); } catch (Exception e) { @@ -277,6 +288,7 @@ public void leaveWorld() try{ onLeaveWorld(); + onComponentsLeaveworld(); } catch (Exception e) { diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/EntityComponent.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/EntityComponent.cs index 683d1d7d..1e8fbdfa 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/EntityComponent.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/EntityComponent.cs @@ -26,6 +26,14 @@ public virtual void onDetached(Entity ownerEntity) } + public virtual void onEnterworld() + { + } + + public virtual void onLeaveworld() + { + } + public virtual ScriptModule getScriptModule() { // 动态生成 diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/GateBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/GateBase.cs index ef926823..8879de69 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/GateBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/GateBase.cs @@ -37,6 +37,14 @@ public GateBase() { } + public override void onComponentsEnterworld() + { + } + + public override void onComponentsLeaveworld() + { + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_GateBase(id, className); diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/KBEngine.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/KBEngine.cs index a3025081..7538382d 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/KBEngine.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/KBEngine.cs @@ -88,10 +88,10 @@ public enum NETWORK_ENCRYPT_TYPE // 服务端与客户端的版本号以及协议MD5 public string serverVersion = ""; - public string clientVersion = "2.4.2"; + public string clientVersion = "2.4.4"; public string serverScriptVersion = ""; public string clientScriptVersion = "0.1.0"; - public string serverProtocolMD5 = "78D6E7A3B539900D86F0C2145E44AEB3"; + public string serverProtocolMD5 = "0CDB82520874ED30FA3D6BFE658116D0"; public string serverEntitydefMD5 = "90AA620FCF194B85FBE7A8E4F4F8F938"; // 当前玩家的实体id与实体类别 @@ -327,7 +327,7 @@ public void sendTick() // 更新玩家的位置与朝向到服务端 updatePlayerToServer(); - if(span.Seconds > _args.serverHeartbeatTick) + if(_args.serverHeartbeatTick > 0 && span.Seconds > _args.serverHeartbeatTick) { span = _lastTickCBTime - _lastTickTime; diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/MessageReaderTCP.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/MessageReaderTCP.cs index d79dcb17..ae12f5cf 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/MessageReaderTCP.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/MessageReaderTCP.cs @@ -162,6 +162,7 @@ public override void process(byte[] datas, MessageLengthEx offset, MessageLength #endif msg.handleMessage(stream); + #if UNITY_EDITOR Dbg.profileEnd(msg.name); #endif diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/MonsterBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/MonsterBase.cs index 42330dae..e3f30302 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/MonsterBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/MonsterBase.cs @@ -54,6 +54,14 @@ public MonsterBase() { } + public override void onComponentsEnterworld() + { + } + + public override void onComponentsLeaveworld() + { + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_MonsterBase(id, className); diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/NPCBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/NPCBase.cs index 26a8330e..135c3522 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/NPCBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/NPCBase.cs @@ -39,6 +39,14 @@ public NPCBase() { } + public override void onComponentsEnterworld() + { + } + + public override void onComponentsLeaveworld() + { + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_NPCBase(id, className); diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/PacketSenderTCP.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/PacketSenderTCP.cs index 7f7cf621..d9ba8a89 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/PacketSenderTCP.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/PacketSenderTCP.cs @@ -17,22 +17,22 @@ 包发送模块(与服务端网络部分的名称对应) 处理网络数据的发送 */ - public class PacketSenderTCP : PacketSenderBase - { + public class PacketSenderTCP : PacketSenderBase + { private byte[] _buffer; int _wpos = 0; // 写入的数据位置 int _spos = 0; // 发送完毕的数据位置 - int _sending = 0; + Boolean _sending = false; - public PacketSenderTCP(NetworkInterfaceBase networkInterface) : base(networkInterface) - { - _buffer = new byte[KBEngineApp.app.getInitArgs().TCP_SEND_BUFFER_MAX]; + public PacketSenderTCP(NetworkInterfaceBase networkInterface) : base(networkInterface) + { + _buffer = new byte[KBEngineApp.app.getInitArgs().TCP_SEND_BUFFER_MAX]; _wpos = 0; _spos = 0; - _sending = 0; - } + _sending = false; + } ~PacketSenderTCP() { @@ -45,7 +45,8 @@ public override bool send(MemoryStream stream) if (dataLength <= 0) return true; - if (0 == Interlocked.Add(ref _sending, 0)) + Monitor.Enter(_sending); + if (!_sending) { if (_wpos == _spos) { @@ -54,7 +55,7 @@ public override bool send(MemoryStream stream) } } - int t_spos = Interlocked.Add(ref _spos, 0); + int t_spos =_spos; int space = 0; int tt_wpos = _wpos % _buffer.Length; int tt_spos = t_spos % _buffer.Length; @@ -84,12 +85,19 @@ public override bool send(MemoryStream stream) Array.Copy(stream.data(), stream.rpos + remain, _buffer, 0, expect_total - _buffer.Length); } - Interlocked.Add(ref _wpos, dataLength); + _wpos += dataLength; - if (Interlocked.CompareExchange(ref _sending, 1, 0) == 0) + if (!_sending) { + _sending = true; + Monitor.Exit(_sending); + _startSend(); } + else + { + Monitor.Exit(_sending); + } return true; } @@ -106,7 +114,9 @@ protected override void _asyncSend() while (true) { - int sendSize = Interlocked.Add(ref _wpos, 0) - _spos; + Monitor.Enter(_sending); + + int sendSize = _wpos - _spos; int t_spos = _spos % _buffer.Length; if (t_spos == 0) t_spos = sendSize; @@ -123,17 +133,22 @@ protected override void _asyncSend() { Dbg.ERROR_MSG(string.Format("PacketSenderTCP::_asyncSend(): send data error, disconnect from '{0}'! error = '{1}'", socket.RemoteEndPoint, se)); Event.fireIn("_closeNetwork", new object[] { _networkInterface }); + + Monitor.Exit(_sending); return; } - int spos = Interlocked.Add(ref _spos, bytesSent); + _spos += bytesSent; // 所有数据发送完毕了 - if (spos == Interlocked.Add(ref _wpos, 0)) + if (_spos == _wpos) { - Interlocked.Exchange(ref _sending, 0); + _sending = false; + Monitor.Exit(_sending); return; } + + Monitor.Exit(_sending); } } } diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ServerErrorDescrs.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ServerErrorDescrs.cs index 4fce984f..0f574e4e 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ServerErrorDescrs.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/ServerErrorDescrs.cs @@ -93,7 +93,7 @@ public ServerErrorDescrs() ServerErr e; e.id = 7; e.name = "SERVER_ERR_ACCOUNT_CREATE_FAILED"; - e.descr = "创建账号失败(已经存在一个相同的账号)。"; + e.descr = "创建账号失败。"; serverErrs.Add(e.id, e); } diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpaceBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpaceBase.cs index b7fec4e1..e8e9603b 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpaceBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpaceBase.cs @@ -35,6 +35,14 @@ public SpaceBase() { } + public override void onComponentsEnterworld() + { + } + + public override void onComponentsLeaveworld() + { + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_SpaceBase(id, className); diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpaceDuplicateBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpaceDuplicateBase.cs index 552df9b9..fe4b831a 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpaceDuplicateBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpaceDuplicateBase.cs @@ -35,6 +35,14 @@ public SpaceDuplicateBase() { } + public override void onComponentsEnterworld() + { + } + + public override void onComponentsLeaveworld() + { + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_SpaceDuplicateBase(id, className); diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpacesBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpacesBase.cs index 3854fe02..84f8e09e 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpacesBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpacesBase.cs @@ -35,6 +35,14 @@ public SpacesBase() { } + public override void onComponentsEnterworld() + { + } + + public override void onComponentsLeaveworld() + { + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_SpacesBase(id, className); diff --git a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpawnPointBase.cs b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpawnPointBase.cs index 7e04b2a7..63a9b715 100644 --- a/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpawnPointBase.cs +++ b/Assets/Plugins/kbengine/kbengine_unity3d_plugins/SpawnPointBase.cs @@ -35,6 +35,14 @@ public SpawnPointBase() { } + public override void onComponentsEnterworld() + { + } + + public override void onComponentsLeaveworld() + { + } + public override void onGetBase() { baseEntityCall = new EntityBaseEntityCall_SpawnPointBase(id, className);