Skip to content

Commit 84c423e

Browse files
committed
up
1 parent ea60fd1 commit 84c423e

File tree

14 files changed

+2665
-462
lines changed

14 files changed

+2665
-462
lines changed

Assets/Plugins/kbengine/kbengine_unity3d_plugins/Blowfish.cs

Lines changed: 531 additions & 0 deletions
Large diffs are not rendered by default.

Assets/Plugins/kbengine/kbengine_unity3d_plugins/Bundle.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,20 @@ public Bundle()
2525

2626
public void clear()
2727
{
28-
stream = MemoryStream.createObject();
29-
streamList = new List<MemoryStream>();
28+
// 把不用的MemoryStream放回缓冲池,以减少垃圾回收的消耗
29+
for (int i = 0; i < streamList.Count; ++i)
30+
{
31+
if (stream != streamList[i])
32+
streamList[i].reclaimObject();
33+
}
34+
35+
streamList.Clear();
36+
37+
if(stream != null)
38+
stream.clear();
39+
else
40+
stream = MemoryStream.createObject();
41+
3042
numMessage = 0;
3143
messageLength = 0;
3244
msgtype = null;
@@ -110,21 +122,12 @@ public void send(NetworkInterface networkInterface)
110122
Dbg.ERROR_MSG("Bundle::send: networkInterface invalid!");
111123
}
112124

113-
// 把不用的MemoryStream放回缓冲池,以减少垃圾回收的消耗
114-
for (int i = 0; i < streamList.Count; ++i)
115-
{
116-
streamList[i].reclaimObject();
117-
}
118-
119-
streamList.Clear();
120-
stream.clear();
121-
122125
// 我们认为,发送完成,就视为这个bundle不再使用了,
123126
// 所以我们会把它放回对象池,以减少垃圾回收带来的消耗,
124127
// 如果需要继续使用,应该重新Bundle.createObject(),
125128
// 如果外面不重新createObject()而直接使用,就可能会出现莫名的问题,
126129
// 仅以此备注,警示使用者。
127-
Bundle.reclaimObject(this);
130+
reclaimObject();
128131
}
129132

130133
public void checkStream(int v)
@@ -239,5 +242,20 @@ public void writeVector4(Vector4 v)
239242
checkStream(16);
240243
stream.writeVector4(v);
241244
}
245+
246+
public void writeEntitycall(byte[] v)
247+
{
248+
checkStream(16);
249+
250+
UInt64 cid = 0;
251+
Int32 id = 0;
252+
UInt16 type = 0;
253+
UInt16 utype = 0;
254+
255+
stream.writeUint64(cid);
256+
stream.writeInt32(id);
257+
stream.writeUint16(type);
258+
stream.writeUint16(utype);
259+
}
242260
}
243261
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
namespace KBEngine
2+
{
3+
#if UNITY_EDITOR
4+
using UnityEngine;
5+
using UnityEditor;
6+
using System.Collections;
7+
using System;
8+
using System.IO;
9+
using KBEngine;
10+
11+
public class ClientSDKUpdater : MonoBehaviour
12+
{
13+
string warnUpdateSDK = "";
14+
MemoryStream sdkFileStream = null;
15+
int downloadFiles = 0;
16+
string sdkPath = "";
17+
string sdkTempPath = "";
18+
string sdkBakPath = "";
19+
20+
void Start()
21+
{
22+
string kbengineCoreFile = "KBEngine.cs";
23+
string[] res = System.IO.Directory.GetFiles(Application.dataPath, kbengineCoreFile, SearchOption.AllDirectories);
24+
sdkPath = res[0].Replace(kbengineCoreFile, "").Replace("\\", "/");
25+
sdkPath = sdkPath.Remove(sdkPath.Length - 1, 1);
26+
27+
sdkTempPath = sdkPath + "_temp";
28+
sdkBakPath = sdkPath + "_bak";
29+
30+
warnUpdateSDK = "Version does not match the server.\nClick to update KBEnginePlugin!\nPull from: " + KBEngineApp.app.getInitArgs().ip + ":" + KBEngineApp.app.getInitArgs().port;
31+
installEvents();
32+
33+
GameObject[] objs = FindObjectsOfType(typeof(GameObject)) as GameObject[];
34+
foreach (GameObject child in objs)
35+
{
36+
if (!child.gameObject.GetComponent<Camera>() &&
37+
!child.gameObject.GetComponent<KBEMain>() &&
38+
!child.gameObject.GetComponent<ClientSDKUpdater>())
39+
{
40+
child.gameObject.SetActive(false);
41+
}
42+
}
43+
}
44+
45+
public virtual void installEvents()
46+
{
47+
Event.registerIn("onImportClientSDK", this, "onImportClientSDK");
48+
}
49+
50+
protected virtual void OnDestroy()
51+
{
52+
KBEngine.Event.deregisterOut(this);
53+
}
54+
55+
public void onImportClientSDK(int remainingFiles, string fileName, int fileSize, byte[] fileDatas)
56+
{
57+
if (sdkFileStream == null)
58+
sdkFileStream = MemoryStream.createObject();
59+
60+
sdkFileStream.append(fileDatas, (uint)sdkFileStream.rpos, (uint)fileDatas.Length);
61+
62+
warnUpdateSDK = "Download:" + fileName + " -> " + sdkFileStream.length() + "/" + fileSize + "bytes! " + (int)(((float)downloadFiles / (float)(downloadFiles + remainingFiles)) * 100) + "%";
63+
Debug.Log(warnUpdateSDK);
64+
65+
if (sdkFileStream.length() == fileSize)
66+
{
67+
Debug.Log("onImportClientSDK: " + fileName + "->" + fileSize + "bytes success!");
68+
69+
string path = Path.GetDirectoryName(sdkTempPath + "//" + fileName);
70+
if (!Directory.Exists(path))
71+
Directory.CreateDirectory(path);
72+
73+
StreamWriter sw;
74+
FileInfo t = new FileInfo(sdkTempPath + "//" + fileName);
75+
string data = System.Text.Encoding.UTF8.GetString(sdkFileStream.data(), 0, fileSize);
76+
sw = t.CreateText();
77+
sw.WriteLine(data);
78+
sw.Close();
79+
sw.Dispose();
80+
81+
sdkFileStream.reclaimObject();
82+
sdkFileStream = null;
83+
downloadFiles += 1;
84+
85+
if (remainingFiles == 0)
86+
{
87+
warnUpdateSDK = "";
88+
downloadFiles = 0;
89+
replaceNewSDK();
90+
}
91+
}
92+
}
93+
94+
void downloadSDKFromServer()
95+
{
96+
downloadFiles = 0;
97+
98+
if (Directory.Exists(sdkTempPath))
99+
Directory.Delete(sdkTempPath, true);
100+
101+
Directory.CreateDirectory(sdkTempPath);
102+
103+
if (sdkFileStream != null)
104+
{
105+
sdkFileStream.reclaimObject();
106+
sdkFileStream = null;
107+
}
108+
109+
// kbcmd options
110+
string tool_options = "Unity";
111+
string callbackIP = "";
112+
UInt16 callbackPort = 0;
113+
int clientWindowSize = (int)KBEngineApp.app.getInitArgs().RECV_BUFFER_MAX;
114+
115+
Bundle bundle = Bundle.createObject();
116+
bundle.newMessage(Messages.messages["Loginapp_importClientSDK"]);
117+
bundle.writeString(tool_options);
118+
bundle.writeInt32(clientWindowSize);
119+
bundle.writeString(callbackIP);
120+
bundle.writeUint16(callbackPort);
121+
bundle.send(KBEngineApp.app.networkInterface());
122+
}
123+
124+
void replaceNewSDK()
125+
{
126+
System.IO.Directory.Move(sdkPath, sdkBakPath);
127+
System.IO.Directory.Move(sdkTempPath, sdkPath);
128+
129+
// 删除旧的SKD文件夹
130+
Directory.Delete(sdkBakPath, true);
131+
132+
EditorApplication.isPlaying = false;
133+
AssetDatabase.Refresh();
134+
}
135+
136+
void OnGUI()
137+
{
138+
if (warnUpdateSDK.Length > 0)
139+
{
140+
GUI.contentColor = Color.red;
141+
GUI.backgroundColor = Color.red;
142+
143+
if (GUI.Button(new Rect(Screen.width * 0.25f, Screen.height * 0.4f, Screen.width * 0.5f, Screen.height * 0.2f), warnUpdateSDK))
144+
{
145+
// 从服务器下载新的SDK
146+
downloadSDKFromServer();
147+
}
148+
}
149+
}
150+
151+
void Update()
152+
{
153+
154+
155+
}
156+
}
157+
#endif
158+
}

0 commit comments

Comments
 (0)