7
7
using Newtonsoft . Json ;
8
8
using Smod2 ;
9
9
using Smod2 . Attributes ;
10
+ using Smod2 . EventHandlers ;
10
11
using Smod2 . Events ;
11
12
using Smod2 . EventSystem . Events ;
12
13
@@ -153,22 +154,53 @@ class ProxyHandler : Plugin
153
154
public override void Register ( )
154
155
{
155
156
handler = this ;
156
- RegisterEvents . registerEvents ( ) ;
157
- AddEventHandlers ( new NewEventHandlers ( ) ) ;
157
+ Info ( "The ProxyHandler is Start!Please start the jsmod2 server" ) ;
158
158
reader . append ( "this.ip" , "127.0.0.1" )
159
159
. append ( "this.port" , "19938" )
160
160
. append ( "jsmod2.ip" , "127.0.0.1" )
161
161
. append ( "jsmod2.port" , "19935" )
162
162
. append ( "jsmod2.debug" , "false" )
163
163
. 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
+ }
164
180
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" ) ;
165
199
}
166
200
167
201
public override void OnEnable ( )
168
202
{
169
- Info ( "The ProxyHandler is Start!Please start the jsmod2 server" ) ;
170
- Thread thread = new Thread ( listenerThread ) ;
171
- thread . Start ( ) ;
203
+
172
204
}
173
205
174
206
public override void OnDisable ( )
@@ -206,6 +238,7 @@ public void listenerThread()
206
238
//在jsmod2的监听器执行完前不能停止,get为true,在read处阻塞
207
239
public void sendEventObject ( Event e , int id , IdMapping mapping )
208
240
{
241
+
209
242
sendObject ( "{}" , id , mapping , true ) ;
210
243
}
211
244
@@ -228,7 +261,7 @@ public void sendObject(TcpClient tcp,string json1, int id,IdMapping mapping,bool
228
261
{
229
262
//如何定位物品,并设置,通过itemMapping找到id归属对象(player这个字段就是id)
230
263
//然后通过id定位到物品,并设置
231
- send ( getProtocol ( json1 , id , mapping ) , tcp , get ) ;
264
+ send0 ( getProtocol ( json1 , id , mapping ) , tcp , get ) ;
232
265
}
233
266
234
267
//发送协议集合,用于传输物品数组,有多个不同的id
@@ -242,7 +275,7 @@ public void sendObjects(TcpClient client,JsonSetting[] settings)
242
275
all = all + "@!" + getProtocol ( JsonConvert . SerializeObject ( settings [ i ] . responseValue ) , settings [ i ] . id ,
243
276
settings [ i ] . idMapping ) ;
244
277
}
245
- send ( all , client ) ;
278
+ send0 ( all , client ) ;
246
279
}
247
280
248
281
@@ -251,13 +284,13 @@ public void sendObjects(JsonSetting[] settings)
251
284
sendObjects ( new TcpClient ( ) , settings ) ;
252
285
}
253
286
254
- private void send ( string jsons , TcpClient tcp , bool get )
287
+ private void send0 ( string jsons , TcpClient tcp , bool get )
255
288
{
256
289
try
257
290
{
258
291
int port ;
259
292
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 ) ) + ";" ) ;
261
294
if ( ! tcp . Connected )
262
295
{
263
296
tcp . Connect ( new IPEndPoint ( IPAddress . Parse ( reader . get ( "jsmod2.ip" ) ) , port ) ) ;
@@ -268,20 +301,17 @@ private void send(string jsons, TcpClient tcp, bool get)
268
301
{
269
302
tcp . GetStream ( ) . ReadByte ( ) ; //停止
270
303
}
304
+ tcp . Close ( ) ;
271
305
}
272
306
catch ( Exception e )
273
307
{
274
308
Error ( e . Message ) ;
275
309
}
276
- finally
277
- {
278
- tcp . Close ( ) ;
279
- }
280
310
}
281
- private void send ( string jsons , TcpClient tcp )
311
+ private void send0 ( string jsons , TcpClient tcp )
282
312
{
283
313
284
- send ( jsons , tcp , false ) ;
314
+ send0 ( jsons , tcp , false ) ;
285
315
286
316
}
287
317
0 commit comments