77using Newtonsoft . Json ;
88using Smod2 ;
99using Smod2 . Attributes ;
10+ using Smod2 . EventHandlers ;
1011using Smod2 . Events ;
1112using Smod2 . EventSystem . Events ;
1213
@@ -153,22 +154,53 @@ class ProxyHandler : Plugin
153154 public override void Register ( )
154155 {
155156 handler = this ;
156- RegisterEvents . registerEvents ( ) ;
157- AddEventHandlers ( new NewEventHandlers ( ) ) ;
157+ Info ( "The ProxyHandler is Start!Please start the jsmod2 server" ) ;
158158 reader . append ( "this.ip" , "127.0.0.1" )
159159 . append ( "this.port" , "19938" )
160160 . append ( "jsmod2.ip" , "127.0.0.1" )
161161 . append ( "jsmod2.port" , "19935" )
162162 . append ( "jsmod2.debug" , "false" )
163163 . create ( Server . GetAppFolder ( ) + "/jsmod2.conf" ) ;
164+ Thread thread = new Thread ( listenerThread ) ;
165+ thread . Start ( ) ;
166+ Info ( "ProxyHandler config:" + Server . GetAppFolder ( ) + "/jsmod2.conf" ) ;
167+ TcpClient client = new TcpClient ( ) ;
168+ int port = Lib . getInt ( reader . get ( "jsmod2.port" ) ) ;
169+ Info ( "Connecting the Jsmod2...." ) ;
170+ while ( ! client . Connected )
171+ {
172+ try
173+ {
174+ client . Connect ( new IPEndPoint ( IPAddress . Parse ( reader . get ( "jsmod2.ip" ) ) , 20003 ) ) ;
175+ }
176+ catch ( Exception e )
177+ {
178+
179+ }
164180
181+ }
182+ client . Close ( ) ;
183+ Info ( "Connect Successfully" ) ;
184+ RegisterEvents . registerEvents ( ) ;
185+ Info ( "registered events" ) ;
186+ Type type = typeof ( NewEventHandlers ) ;
187+ NewEventHandlers handlers = new NewEventHandlers ( ) ;
188+ Type [ ] types = type . GetInterfaces ( ) ;
189+ foreach ( var t in types )
190+ {
191+ if ( t != typeof ( IEventHandlerSetConfig ) )
192+ {
193+ AddEventHandler ( t , handlers ) ;
194+ }
195+
196+ }
197+ AddEventHandler ( typeof ( IEventHandlerSetConfig ) , handlers ) ;
198+ Info ( "registered event handlers" ) ;
165199 }
166200
167201 public override void OnEnable ( )
168202 {
169- Info ( "The ProxyHandler is Start!Please start the jsmod2 server" ) ;
170- Thread thread = new Thread ( listenerThread ) ;
171- thread . Start ( ) ;
203+
172204 }
173205
174206 public override void OnDisable ( )
@@ -206,6 +238,7 @@ public void listenerThread()
206238 //在jsmod2的监听器执行完前不能停止,get为true,在read处阻塞
207239 public void sendEventObject ( Event e , int id , IdMapping mapping )
208240 {
241+
209242 sendObject ( "{}" , id , mapping , true ) ;
210243 }
211244
@@ -228,7 +261,7 @@ public void sendObject(TcpClient tcp,string json1, int id,IdMapping mapping,bool
228261 {
229262 //如何定位物品,并设置,通过itemMapping找到id归属对象(player这个字段就是id)
230263 //然后通过id定位到物品,并设置
231- send ( getProtocol ( json1 , id , mapping ) , tcp , get ) ;
264+ send0 ( getProtocol ( json1 , id , mapping ) , tcp , get ) ;
232265 }
233266
234267 //发送协议集合,用于传输物品数组,有多个不同的id
@@ -242,7 +275,7 @@ public void sendObjects(TcpClient client,JsonSetting[] settings)
242275 all = all + "@!" + getProtocol ( JsonConvert . SerializeObject ( settings [ i ] . responseValue ) , settings [ i ] . id ,
243276 settings [ i ] . idMapping ) ;
244277 }
245- send ( all , client ) ;
278+ send0 ( all , client ) ;
246279 }
247280
248281
@@ -251,13 +284,13 @@ public void sendObjects(JsonSetting[] settings)
251284 sendObjects ( new TcpClient ( ) , settings ) ;
252285 }
253286
254- private void send ( string jsons , TcpClient tcp , bool get )
287+ private void send0 ( string jsons , TcpClient tcp , bool get )
255288 {
256289 try
257290 {
258291 int port ;
259292 int . TryParse ( reader . get ( "jsmod2.port" ) , out port ) ;
260- byte [ ] bytes = utf8WithoutBom . GetBytes ( Convert . ToBase64String ( utf8WithoutBom . GetBytes ( jsons ) ) ) ;
293+ byte [ ] bytes = utf8WithoutBom . GetBytes ( Convert . ToBase64String ( utf8WithoutBom . GetBytes ( jsons ) ) + ";" ) ;
261294 if ( ! tcp . Connected )
262295 {
263296 tcp . Connect ( new IPEndPoint ( IPAddress . Parse ( reader . get ( "jsmod2.ip" ) ) , port ) ) ;
@@ -268,20 +301,17 @@ private void send(string jsons, TcpClient tcp, bool get)
268301 {
269302 tcp . GetStream ( ) . ReadByte ( ) ; //停止
270303 }
304+ tcp . Close ( ) ;
271305 }
272306 catch ( Exception e )
273307 {
274308 Error ( e . Message ) ;
275309 }
276- finally
277- {
278- tcp . Close ( ) ;
279- }
280310 }
281- private void send ( string jsons , TcpClient tcp )
311+ private void send0 ( string jsons , TcpClient tcp )
282312 {
283313
284- send ( jsons , tcp , false ) ;
314+ send0 ( jsons , tcp , false ) ;
285315
286316 }
287317
0 commit comments