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.\n Click to update KBEnginePlugin!\n Pull 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
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.\n Click to update KBEnginePlugin!\n Pull 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
158
}
0 commit comments