This repository has been archived by the owner on Oct 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuaS.cs
821 lines (671 loc) · 37.9 KB
/
LuaS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/*
* This plugin will let you write MCGalaxy plugins and commands using Lua.
*
* Plugins written in Lua contain many caveats and work-arounds
* because of how MCGalaxy's and NLua's APIs are made.
*
* What you would usually use as a reference/pointer is
* a C# class because NLua does not support references and needs
* "containers". To edit the container values you access the container's
* .contained property.
*
* Lua functions are not C# functions and therefore cannot be used to
* register event functions. The work-around for that is to define functions
* for each plugin. They follow the C# API's naming with the exception of them
* using camelCase. So for example, OnLevelSave becomes onLevelSave.
*
* Lua plugin speed compared to C# plugin speed will be slower, however,
* the cost of the speed is repaid by the time it takes to write
* a plugin with Lua's simplicity (and less special character spam).
*
*
*/
//reference System.dll
//reference System.Net.Sockets.dll
//reference NLua.dll
//reference KeraLua.dll
using MCGalaxy.Games;
using MCGalaxy.Network;
using MCGalaxy.Blocks.Physics;
using MCGalaxy.Events;
using MCGalaxy.Events.EconomyEvents;
using MCGalaxy.Events.EntityEvents;
using MCGalaxy.Events.GameEvents;
using MCGalaxy.Events.GroupEvents;
using MCGalaxy.Events.LevelEvents;
using MCGalaxy.Events.PlayerDBEvents;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Events.ServerEvents;
using BlockID = System.UInt16;
using System;
using System.IO;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text;
using NLua;
namespace MCGalaxy {
public class LuaContainer : object {
public object contained;
public LuaContainer (object contained) {
this.contained = contained;
}
}
public class LuaS : Plugin {
public override string name { get { return "LuaS"; } }
public override string MCGalaxy_Version { get { return "1.9.4.8"; } }
public override string welcome { get { return "LuaS loaded."; } }
public override string creator { get { return "someone and cry in a corner because you can't do anything"; } }
public override int build { get { return 1; } }
public static string prefix = "plugins/";
private Lua state;
private List<LuaTable> plugins;
public override void Load(bool startup) {
Directory.CreateDirectory(prefix + "lua");
Directory.CreateDirectory(prefix + "lua/plugins");
Directory.CreateDirectory(prefix + "lua/commands");
#region Economy register
//OnMoneyChangedEvent.Register(HandleMoneyChanged, Priority.Critical);
//OnEcoTransactionEvent.Register(HandleEcoTransaction, Priority.Critical);
#endregion Economy register
#region Entitiy register
//OnTabListEntryAddedEvent.Register(HandleTabListEntryAdded, Priority.Critical);
//OnTabListEntryRemovedEvent.Register(HandleTabListEntryRemoved, Priority.Critical);
//OnEntitySpawnedEvent.Register(HandleEntitySpawned, Priority.Critical);
//OnEntityDespawnedEvent.Register(HandleEntityDespawned, Priority.Critical);
//OnSendingModelEvent.Register(HandleSendingModel, Priority.Critical);
//OnGettingCanSeeEntityEvent.Register(HandleGettingCanSeeEntity, Priority.Critical);
#endregion Entity register
#region Games register
//OnStateChangedEvent.Register(HandleStateChanged, Priority.Critical);
//OnMapsChangedEvent.Register(HandleMapsChanged, Priority.Critical);
#endregion Games register
#region Group register
//OnGroupLoadedEvent.Register(HandleGroupLoaded, Priority.Critical);
//OnGroupLoadEvent.Register(HandleGroupLoad, Priority.Critical);
//OnGroupSaveEvent.Register(HandleGroupSave, Priority.Critical);
//OnChangingGroupEvent.Register(HandleChangingGroup, Priority.Critical);
#endregion Group register
#region Level register
//OnLevelLoadedEvent.Register(HandleLevelLoaded, Priority.Critical);
//OnLevelLoadEvent.Register(HandleLevelLoad, Priority.Critical);
//OnLevelSaveEvent.Register(HandleLevelSave, Priority.Critical);
//OnLevelUnloadEvent.Register(HandleLevelUnload, Priority.Critical);
//OnLevelAddedEvent.Register(HandleLevelAdded, Priority.Critical);
//OnLevelRemovedEvent.Register(HandleLevelRemoved, Priority.Critical);
//OnPhysicsStateChangedEvent.Register(HandlePhysicsStateChanged, Priority.Critical);
//OnPhysicsUpdateEvent.Register(HandlePhysicsUpdate, Priority.Critical);
//OnLevelRenamedEvent.Register(HandleLevelRenamed, Priority.Critical);
//OnLevelCopiedEvent.Register(HandleLevelCopied, Priority.Critical);
//OnLevelDeletedEvent.Register(HandleLevelDeleted, Priority.Critical);
//OnBlockHandlersUpdatedEvent.Register(HandleBlockHandlersUpdated, Priority.Critical);
//OnMainLevelChangingEvent.Register(HandleMainLevelChanging, Priority.Critical); //TODO: Once MCGalaxy upgrades, update this event
#endregion Level register
#region ModAction register
//OnModActionEvent.Register(HandleModAction, Priority.Critical);
#endregion ModAction register
#region PlayerDB register
//OnInfoSaveEvent.Register(HandleInfoSave, Priority.Critical);
//OnInfoSwapEvent.Register(HandleInfoSwap, Priority.Critical);
#endregion PlayerDB register
#region Player register
OnPlayerChatEvent.Register(HandlePlayerChat, Priority.Critical);
//OnPlayerMoveEvent.Register(HandlePlayerMove, Priority.Critical);
OnPlayerCommandEvent.Register(HandlePlayerCommand, Priority.Critical);
// Connecting
//OnPlayerConnectEvent.Register(HandlePlayerConnect, Priority.Critical);
//OnPlayerStartConnectingEvent.Register(HandlePlayerStartConnecting, Priority.Critical);
OnPlayerFinishConnectingEvent.Register(HandlePlayerFinishConnecting, Priority.Critical);
// Death
//OnPlayerDyingEvent.Register(HandlePlayerDying, Priority.Critical);
//OnPlayerDiedEvent.Register(HandlePlayerDied, Priority.Critical);
//OnPlayerDisconnectEvent.Register(HandlePlayerDisconnect, Priority.Critical);
//OnBlockChangingEvent.Register(HandleBlockChanging, Priority.Critical);
//OnBlockChangedEvent.Register(HandleBlockChanged, Priority.Critical);
//OnPlayerClickEvent.Register(HandlePlayerClick, Priority.Critical);
//OnMessageRecievedEvent.Register(HandleMessageReceived, Priority.Critical);
//OnSentMapEvent.Register(HandleSentMap, Priority.Critical);
//OnJoiningLevelEvent.Register(HandleJoiningLevel, Priority.Critical);
//OnJoinedLevelEvent.Register(HandleJoinedLevel, Priority.Critical);
//OnPlayerActionEvent.Register(HandlePlayerAction, Priority.Critical);
//OnSettingPrefixEvent.Register(HandleSettingPrefix, Priority.Critical);
OnGettingMotdEvent.Register(HandleGettingMotd, Priority.Critical);
OnSendingMotdEvent.Register(HandleSendingMotd, Priority.Critical);
//OnPlayerSpawningEvent.Register(HandlePlayerSpawning, Priority.Critical);
//OnChangedZoneEvent.Register(HandleChangedZone, Priority.Critical);
//OnGettingCanSeeEvent.Register(HandleGettingCanSee, Priority.Critical);
#endregion Player register
#region Server register
//OnSendingHeartbeatEvent.Register(HandleSendingHeartbeat, Priority.Critical);
//OnShuttingDownEvent.Register(HandleShuttingDown, Priority.Critical);
//OnConfigUpdatedEvent.Register(HandleConfigUpdated, Priority.Critical);
//OnConnectionReceivedEvent.Register(HandleConnectionReceived, Priority.Critical);
OnChatSysEvent.Register(HandleChatSys, Priority.Critical);
OnChatFromEvent.Register(HandleChatFrom, Priority.Critical);
OnChatEvent.Register(HandleChat, Priority.Critical);
//OnPluginMessageReceivedEvent.Register(HandlePluginMessageReceived, Priority.Critical);
#endregion Server register
this.plugins = new List<LuaTable>();
this.state = new Lua();
this.state.State.Encoding = Encoding.UTF8;
this.state.LoadCLRPackage();
this.state.DoString(@"
-- Initiate the MCGalaxy namespace.
MCGalaxy = import (""MCGalaxy_"", ""MCGalaxy"")
function Plugin(name)
return {__pluginname = name}
end
");
Command.Register(new CmdRunLua());
Command.Register(new CmdLua());
string[] paths = Directory.GetFiles(prefix + "lua/plugins", "*.lua", SearchOption.TopDirectoryOnly);
foreach (string i in paths) {
object[] rvalue = this.state.DoFile(i);
if (rvalue.Length > 0 && rvalue[0].ToString() == "table") {
Logger.Log(LogType.SystemActivity, "LuaS: Loaded plugin " + i);
LuaTable plug = (LuaTable) rvalue[0];
this.plugins.Add(plug);
}
else {
Logger.Log(
LogType.Warning,
"LuaS: The plugin "
+ i
+ " could not be loaded because it has an invalid structure and does not return a table."
);
}
}
this.Call("load", startup);
}
#region Economy
void HandleMoneyChanged(Player p) {
this.Call("onMoneyChanged", p);
}
void HandleEcoTransaction(EcoTransaction transaction) {
this.Call("onEcoTransaction", transaction);
}
#endregion Economy
#region Entity
void HandleTabListEntryAdded(Entity e, ref string tabName, ref string tabGroup, Player dst) {
LuaContainer tabName_c = new LuaContainer((object) tabName);
LuaContainer tabGroup_c = new LuaContainer((object) tabGroup);
this.Call("onTabListEntryAdded", e, tabName_c, tabGroup_c, dst);
tabName = (string) tabName_c.contained;
tabGroup = (string) tabGroup_c.contained;
}
void HandleTabListEntryRemoved(Entity e, Player dst) {
this.Call("onTabListEntryRemoved", e, dst);
}
void HandleEntitySpawned(Entity e, ref string name, ref string skin, ref string model, Player dst) {
LuaContainer name_c = new LuaContainer((object) name);
LuaContainer skin_c = new LuaContainer((object) skin);
LuaContainer model_c = new LuaContainer((object) model);
this.Call("onEntitySpawned", e, name_c, skin_c, model_c, dst);
name = (string) name_c.contained;
skin = (string) skin_c.contained;
model = (string) model_c.contained;
}
void HandleEntityDespawned(Entity e, Player dst) {
this.Call("onEntityDespawned", e, dst);
}
void HandleSendingModel(Entity e, ref string model, Player dst) {
LuaContainer model_c = new LuaContainer((object) model);
this.Call("onSendingModel", e, model_c, dst);
model = (string) model_c.contained;
}
void HandleGettingCanSeeEntity(Player p, ref bool canSee, Entity target) {
LuaContainer canSee_c = new LuaContainer((object) canSee);
this.Call("onGettingCanSeeEntity", p, canSee_c, target);
canSee = (bool) canSee_c.contained;
}
#endregion Entity
#region Game
void HandleStateChanged(IGame game) {
this.Call("onStateChanged", game);
}
void HandleMapsChanged(RoundsGame game) {
this.Call("onMapsChanged", game);
}
#endregion Game
#region Group
void HandleGroupLoaded(Group g) {
this.Call("groupLoaded", g);
}
void HandleGroupLoad() {
this.Call("groupLoad");
}
void HandleGroupSave() {
this.Call("groupSave");
}
void HandleChangingGroup(string player, Group curRank, Group newRank, ref bool cancel) {
LuaContainer cancel_c = new LuaContainer((object) cancel);
this.Call("onChangingGroup", player, curRank, newRank, cancel_c);
cancel = (bool) cancel_c.contained;
}
#endregion Group
#region Level
void HandleLevelLoaded(Level lvl) {
this.Call("onLevelLoaded", lvl);
}
void HandleLevelLoad(string name, string path, ref bool cancel) {
LuaContainer cancel_c = new LuaContainer((object) cancel);
this.Call("onLevelLoad", name, path, cancel_c);
cancel = (bool) cancel_c.contained;
}
void HandleLevelSave(Level lvl, ref bool cancel) {
LuaContainer cancel_c = new LuaContainer((object) cancel);
this.Call("onLevelSave", lvl, cancel_c);
cancel = (bool) cancel_c.contained;
}
void HandleLevelUnload(Level lvl, ref bool cancel) {
LuaContainer cancel_c = new LuaContainer((object) cancel);
this.Call("onLevelUnload", lvl, cancel_c);
cancel = (bool) cancel_c.contained;
}
void HandleLevelAdded(Level lvl) {
this.Call("onLevelAdded", lvl);
}
void HandleLevelRemoved(Level lvl) {
this.Call("onLevelRemoved", lvl);
}
void HandlePhysicsStateChanged(Level lvl, PhysicsState state) {
this.Call("onPhysicsStateChanged", lvl, state);
}
void HandlePhysicsLevelChanged(Level lvl, int level) {
this.Call("onPhysicsLevelChanged", lvl, level);
}
void HandlePhysicsUpdate(ushort x, ushort y, ushort z, PhysicsArgs args, Level lvl) {
this.Call("onPhysicsUpdate", x, y, z, args, lvl);
}
void HandleLevelRenamed(string srcMap, string dstMap) {
this.Call("onLevelRenamed", srcMap, dstMap);
}
void HandleLevelCopied(string srcMap, string dstMap) {
this.Call("onLevelCopied", srcMap, dstMap);
}
void HandleLevelDeleted(string map) {
this.Call("onLevelDeleted", map);
}
void HandleBlockHandlersUpdated(Level lvl, BlockID block) {
this.Call("onBlockHandlersUpdated", lvl, block);
}
void HandleMainLevelChanging(ref string map) {
LuaContainer map_c = new LuaContainer((object) map);
this.Call("onMainLevelChanging", map_c);
map = (string) map_c.contained;
}
#endregion Level
#region ModAction
void HandleModAction(ModAction e) {
this.Call("onModAction", e);
}
#endregion ModAction
#region PlayerDB
void HandleInfoSave(Player p, ref bool cancel) {
LuaContainer cancel_c = new LuaContainer((object) cancel);
this.Call("onInfoSave", p, cancel_c);
cancel = (bool) cancel_c.contained;
}
void HandleInfoSwap(string src, string dst) {
this.Call("onInfoSwap", src, dst);
}
#endregion PlayerDB
#region Player
void HandlePlayerChat(Player p, string message) {
this.Call("onPlayerChat", p, message);
}
void HandlePlayerMove(Player p, Position next, byte yaw, byte pitch, ref bool cancel) {
LuaContainer cancel_c = new LuaContainer((object)cancel);
this.Call("onPlayerMove", p, next, yaw, pitch, cancel_c);
cancel = (bool) cancel_c.contained;
}
void HandlePlayerCommand(Player p, string cmd, string args, CommandData data) {
this.Call("onPlayerCommand", p, cmd, args, data);
RunCmd(this.state, p,cmd,args,data);
if (File.Exists(prefix + "lua/commands/" + cmd.ToLower() + ".lua")){
Logger.Log(LogType.SystemActivity, p.truename + " used /" + cmd.ToLower() + " " + args);
}
}
void HandlePlayerConnect(Player p) {
this.Call("onPlayerConnect", p);
}
void HandlePlayerStartConnecting(Player p, string mppass) {
this.Call("onPlayerStartConnecting", p, mppass);
}
void HandlePlayerFinishConnecting(Player p) {
this.Call("onPlayerFinishConnecting", p);
}
void HandlePlayerDying(Player p, BlockID cause, ref bool cancel) {
LuaContainer cancel_c = new LuaContainer((object) cancel);
this.Call("onPlayerDying", p, cause, cancel_c);
cancel = (bool) cancel_c.contained;
}
void HandlePlayerDied(Player p, BlockID cause, ref TimeSpan cooldown) {
LuaContainer cooldown_c = new LuaContainer((object) cooldown);
this.Call("onPlayerDied", p, cause, cooldown_c);
cooldown = (TimeSpan) cooldown_c.contained;
}
void HandlePlayerDisconnect(Player p, string reason) {
this.Call("onPlayerDisconnect", p, reason);
}
void HandleBlockChanging(Player p, ushort x, ushort y, ushort z, BlockID block, bool placing, ref bool cancel) {
LuaContainer cancel_c = new LuaContainer((object) cancel);
this.Call("onBlockChanging", p, x, y, z, block, placing, cancel_c);
cancel = (bool) cancel_c.contained;
}
void HandleBlockChanged(Player p, ushort x, ushort y, ushort z, ChangeResult result) {
this.Call("onBlockChanged", p, x, y, z, result);
}
void HandlePlayerClick(Player p, MouseButton button, MouseAction action, ushort yaw, ushort pitch, byte entity, ushort x, ushort y, ushort z, TargetBlockFace face) {
this.Call("onPlayerClick", p, button, action, yaw, pitch, entity, x, y, z, face);
}
void HandleMessageReceived(Player p, ref string message, ref bool cancel) {
LuaContainer message_c = new LuaContainer((object) message);
LuaContainer cancel_c = new LuaContainer((object) cancel);
this.Call("onMessageReceived", p, message_c, cancel_c);
message = (string) message_c.contained;
cancel = (bool) cancel_c.contained;
}
void HandleSentMap(Player p, Level prevLevel, Level level) {
this.Call("onSentMap", p, prevLevel, level);
}
void HandleJoiningLevel(Player p, Level lvl, ref bool canJoin) {
LuaContainer canJoin_c = new LuaContainer((object) canJoin);
this.Call("onJoiningLevel", p, lvl, canJoin_c);
canJoin = (bool) canJoin_c.contained;
}
void HandleJoinedLevel(Player p, Level prevLevel, Level level, ref bool announce) {
LuaContainer announce_c = new LuaContainer((object) announce);
this.Call("onJoinedLevel", p, prevLevel, level, announce_c);
announce = (bool) announce_c.contained;
}
void HandlePlayerAction(Player p, PlayerAction action, string message, bool stealth) {
this.Call("onPlayerAction", p, action, message, stealth);
}
void HandleSettingPrefix(Player p, List<string> prefixes) {
this.Call("onSettingPrefix", p, prefixes);
}
void HandleSettingColor(Player p, ref string color) {
LuaContainer color_c = new LuaContainer((object) color);
this.Call("onSettingColor", p, color_c);
color = (string) color_c.contained;
}
void HandleGettingMotd(Player p, ref string motd) {
LuaContainer motd_c = new LuaContainer((object) motd);
this.Call("onGettingMotd", p, motd_c);
motd = (string) motd_c.contained;
}
void HandleSendingMotd(Player p, ref string motd) {
LuaContainer motd_c = new LuaContainer((object) motd);
this.Call("onSendingMotd", p, motd_c);
motd = (string) motd_c.contained;
}
void HandlePlayerSpawning(Player p, ref Position pos, ref byte yaw, ref byte pitch, bool respawning) {
LuaContainer pos_c = new LuaContainer((object) pos);
LuaContainer yaw_c = new LuaContainer((object) yaw);
LuaContainer pitch_c = new LuaContainer((object) pitch);
this.Call("onPlayerSpawning", p, pos_c, yaw_c, pitch_c, respawning);
pos = (Position) pos_c.contained;
yaw = (byte) yaw_c.contained;
pitch = (byte) pitch_c.contained;
}
void HandleChangedZone(Player p) {
this.Call("onChangedZone", p);
}
void HandleGettingCanSee(Player p, LevelPermission plRank, ref bool canSee, Player target) {
LuaContainer canSee_c = new LuaContainer((object) canSee);
this.Call("onChangedZone", p, plRank, canSee_c, target);
canSee = (bool) canSee_c.contained;
}
#endregion Player
#region Server
void HandleSendingHeartbeat(Heartbeat service, ref string name) {
LuaContainer name_c = new LuaContainer((object) name);
this.Call("onSendingHeartbeat", service, name_c);
name = (string) name_c.contained;
}
void HandleShuttingDown(bool restarting, string reason) {
this.Call("onShuttingDown", restarting, reason);
}
void HandleConfigUpdated() {
this.Call("onConfigUpdated");
}
void HandleConnectionReceived(Socket s, ref bool cancel, ref bool announce) {
LuaContainer cancel_c = new LuaContainer((object) cancel);
LuaContainer announce_c = new LuaContainer((object) announce);
this.Call("onConnectionReceived", s, cancel_c, announce_c);
cancel = (bool) cancel_c.contained;
announce = (bool) announce_c.contained;
}
void HandleChatSys(ChatScope scope, string msg, object arg, ref ChatMessageFilter filter, bool relay) {
LuaContainer filter_c = new LuaContainer((object) filter);
this.Call("onChatSys", scope, msg, arg, filter, relay);
filter = (ChatMessageFilter) filter_c.contained;
}
void HandleChatFrom(ChatScope scope, Player source, string msg, object arg, ref ChatMessageFilter filter, bool relay) {
LuaContainer filter_c = new LuaContainer((object) filter);
this.Call("onChatFrom", scope, source, msg, arg, filter_c, relay);
filter = (ChatMessageFilter) filter_c.contained;
}
void HandleChat(ChatScope scope, Player source, string msg, object arg, ref ChatMessageFilter filter, bool relay) {
LuaContainer filter_c = new LuaContainer((object) filter);
this.Call("onChat", scope, source, msg, arg, filter_c, relay);
filter = (ChatMessageFilter) filter_c.contained;
}
void HandlePluginMessageReceived(Player p, byte channel, byte[] data) {
this.Call("onPluginMessageReceived", p, channel, data);
}
#endregion Server
public override void Unload(bool shutdown) {
Command.Unregister(Command.Find("RunLua"));
Command.Unregister(Command.Find("Lua"));
#region Economy unregister
OnMoneyChangedEvent.Unregister(HandleMoneyChanged);
OnEcoTransactionEvent.Unregister(HandleEcoTransaction);
#endregion Economy unregister
#region Entitiy unregister
OnTabListEntryAddedEvent.Unregister(HandleTabListEntryAdded);
OnTabListEntryRemovedEvent.Unregister(HandleTabListEntryRemoved);
OnEntitySpawnedEvent.Unregister(HandleEntitySpawned);
OnEntityDespawnedEvent.Unregister(HandleEntityDespawned);
OnSendingModelEvent.Unregister(HandleSendingModel);
OnGettingCanSeeEntityEvent.Unregister(HandleGettingCanSeeEntity);
#endregion Entity unregister
#region Games unregister
OnStateChangedEvent.Unregister(HandleStateChanged);
OnMapsChangedEvent.Unregister(HandleMapsChanged);
#endregion Games unregister
#region Group unregister
OnGroupLoadedEvent.Unregister(HandleGroupLoaded);
OnGroupLoadEvent.Unregister(HandleGroupLoad);
OnGroupSaveEvent.Unregister(HandleGroupSave);
OnChangingGroupEvent.Unregister(HandleChangingGroup);
#endregion Group unregister
#region Level unregister
OnLevelLoadedEvent.Unregister(HandleLevelLoaded);
OnLevelLoadEvent.Unregister(HandleLevelLoad);
OnLevelSaveEvent.Unregister(HandleLevelSave);
OnLevelUnloadEvent.Unregister(HandleLevelUnload);
OnLevelAddedEvent.Unregister(HandleLevelAdded);
OnLevelRemovedEvent.Unregister(HandleLevelRemoved);
OnPhysicsStateChangedEvent.Unregister(HandlePhysicsStateChanged);
OnPhysicsUpdateEvent.Unregister(HandlePhysicsUpdate);
OnLevelRenamedEvent.Unregister(HandleLevelRenamed);
OnLevelCopiedEvent.Unregister(HandleLevelCopied);
OnLevelDeletedEvent.Unregister(HandleLevelDeleted);
OnBlockHandlersUpdatedEvent.Unregister(HandleBlockHandlersUpdated);
//OnMainLevelChangingEvent.Unregister(HandleMainLevelChanging); //TODO: Once MCGalaxy upgrades, update this event
#endregion Level unregister
#region ModAction unregister
OnModActionEvent.Unregister(HandleModAction);
#endregion ModAction unregister
#region PlayerDB unregister
OnInfoSaveEvent.Unregister(HandleInfoSave);
OnInfoSwapEvent.Unregister(HandleInfoSwap);
#endregion PlayerDB unregister
#region Player unregister
OnPlayerChatEvent.Unregister(HandlePlayerChat);
OnPlayerMoveEvent.Unregister(HandlePlayerMove);
OnPlayerCommandEvent.Unregister(HandlePlayerCommand);
// Connecting events
OnPlayerConnectEvent.Unregister(HandlePlayerConnect);
OnPlayerStartConnectingEvent.Unregister(HandlePlayerStartConnecting);
OnPlayerFinishConnectingEvent.Unregister(HandlePlayerFinishConnecting);
// Death
OnPlayerDyingEvent.Unregister(HandlePlayerDying);
OnPlayerDiedEvent.Unregister(HandlePlayerDied);
OnPlayerDisconnectEvent.Unregister(HandlePlayerDisconnect);
OnBlockChangingEvent.Unregister(HandleBlockChanging);
OnBlockChangedEvent.Unregister(HandleBlockChanged);
OnPlayerClickEvent.Unregister(HandlePlayerClick);
OnMessageRecievedEvent.Unregister(HandleMessageReceived);
OnSentMapEvent.Unregister(HandleSentMap);
OnJoiningLevelEvent.Unregister(HandleJoiningLevel);
OnJoinedLevelEvent.Unregister(HandleJoinedLevel);
OnPlayerActionEvent.Unregister(HandlePlayerAction);
OnSettingPrefixEvent.Unregister(HandleSettingPrefix);
OnGettingMotdEvent.Unregister(HandleGettingMotd);
OnSendingMotdEvent.Unregister(HandleSendingMotd);
OnPlayerSpawningEvent.Unregister(HandlePlayerSpawning);
OnChangedZoneEvent.Unregister(HandleChangedZone);
OnGettingCanSeeEvent.Unregister(HandleGettingCanSee);
#endregion Player unregister
#region Server unregister
OnSendingHeartbeatEvent.Unregister(HandleSendingHeartbeat);
OnShuttingDownEvent.Unregister(HandleShuttingDown);
OnConfigUpdatedEvent.Unregister(HandleConfigUpdated);
OnConnectionReceivedEvent.Unregister(HandleConnectionReceived);
OnChatSysEvent.Unregister(HandleChatSys);
OnChatFromEvent.Unregister(HandleChatFrom);
OnChatEvent.Unregister(HandleChat);
OnPluginMessageReceivedEvent.Unregister(HandlePluginMessageReceived);
#endregion Server unregister
this.Call("unload", shutdown);
this.state.Close();
}
public override void Help(Player p) {
p.Message("This plugin allows you to run Lua scripts on the MCGalaxy server.");
}
private void Call(string name, params object[] args) {
try {
foreach(LuaTable plug in this.plugins) {
if (plug[name] != null && plug[name].ToString() == "function") {
((LuaFunction) plug[name]).Call(args);
}
}
}
catch (NullReferenceException) {}
}
public static void RunCmd(Lua s, Player p, string cmd, string args, CommandData data, bool cancel = true) {
string fullpath = prefix + "lua/commands/" + cmd.ToLower() + ".lua";
if (File.Exists(fullpath)) {
LuaFunction func = (LuaFunction) s.DoFile(fullpath)[0];
func.Call(p, cmd, args, data);
if (cancel) {
p.cancelcommand = true;
}
}
}
}
public class CmdRunLua : Command {
public override string name { get { return "RunLua"; } }
public override LevelPermission defaultRank { get { return LevelPermission.Admin; }}
public override string type { get { return "Scripting"; } }
public override void Use (Player p, string args) {
Lua state = new Lua();
state.State.Encoding = Encoding.UTF8;
state.LoadCLRPackage();
state.DoString(@"
MCGalaxy = import (""MCGalaxy_"", ""MCGalaxy"")
");
object[] rvals = state.DoString(args);
p.Message("Returned values:");
foreach (object o in rvals) {
p.Message(o.ToString());
}
state.Close();
}
public override void Help (Player p) {
p.Message("Runs Lua code. /RunLua function returnV(v) if v == 1 then return \"v\"; else return \"no\"; end end return returnV(1);");
}
}
public class CmdLua : Command {
public override string name { get { return "Lua"; } }
public override LevelPermission defaultRank { get { return LevelPermission.Admin; }}
public override string type { get { return "Scripting"; } }
public override void Use (Player p, string args) {
if (args.ToLower().StartsWith("new")) {
string[] words = args.Split(' ');
if (words.Length <= 1)
return;
if (File.Exists(LuaS.prefix + "lua/plugins/" + words[1].ToLower() + "_plugin.lua")) {
p.Message("Unfortunately that file already exists. Try another file name!");
}
else {
Util.TextFile file = new Util.TextFile(LuaS.prefix + "lua/plugins/" + words[1].ToLower() + "_plugin.lua",
String.Format(@"--[[
Lua plugin skeleton.
Please read the LuaS readme for the details
on Lua APIs.
]]
-- You are advised to make every
-- plugin table local as all of
-- the plugins share the same
-- Lua state/context
local {0}Plugin = Plugin ""{0}""
function {0}Plugin.load(startup)
-- Put what you would put into the C# Load method here.
if startup then
-- All of MCGalaxy's API is accessible
-- through the MCGalaxy namespace.
MCGalaxy.Logger.Log(
MCGalaxy.LogType.Warning,
""This plugin loaded when you started up the server""
)
else
MCGalaxy.Logger.Log(
MCGalaxy.LogType.Warning,
""This plugin got loaded in manually.""
)
end
end
function {0}Plugin.unload(shutdown)
-- Same deal here. put what you would put into
-- the C# Unload method here.
end
-- The Plugin API expects you to
-- return the plugin table.
return {0}Plugin ",
words[1]
)
);
file.EnsureExists();
p.Message("Saved the file into lua/" + words[1].ToLower() + "_plugin.lua");
}
}
else if (args.ToLower().StartsWith("run")) {
string[] words = args.Split(' ');
string cmdargs = "";
for (int k = 2; k < words.Length; k++) {
cmdargs += words[k];
if (!(k +1 == words.Length))
cmdargs += " ";
}
Lua state = new Lua();
state.State.Encoding = Encoding.UTF8;
state.LoadCLRPackage();
state.DoString(@"
-- Initiate the MCGalaxy namespace.
MCGalaxy = import (""MCGalaxy_"", ""MCGalaxy"")
function Plugin(name)
return {__pluginname = name}
end
consoleSandbox = true
");
LuaS.RunCmd(state, p, words[1], cmdargs, new CommandData(), false);
}
}
public override void Help (Player p) {
p.Message(@"Useless for now, just creates plugin skeletons.");
}
}
}