|
1 | | -using Microsoft.Xna.Framework; |
2 | | -using OTAPI; |
| 1 | +using OTAPI; |
3 | 2 |
|
4 | | -namespace TerrariaApi.Server.Hooking |
| 3 | +namespace TerrariaApi.Server.Hooking; |
| 4 | + |
| 5 | +internal static class GameHooks |
5 | 6 | { |
6 | | - internal static class GameHooks |
| 7 | + private static HookManager _hookManager; |
| 8 | + |
| 9 | + /// <summary> |
| 10 | + /// Attaches any of the OTAPI Game hooks to the existing <see cref="HookManager"/> implementation |
| 11 | + /// </summary> |
| 12 | + /// <param name="hookManager">HookManager instance which will receive the events</param> |
| 13 | + public static void AttachTo(HookManager hookManager) |
7 | 14 | { |
8 | | - private static HookManager _hookManager; |
| 15 | + _hookManager = hookManager; |
9 | 16 |
|
10 | | - /// <summary> |
11 | | - /// Attaches any of the OTAPI Game hooks to the existing <see cref="HookManager"/> implementation |
12 | | - /// </summary> |
13 | | - /// <param name="hookManager">HookManager instance which will receive the events</param> |
14 | | - public static void AttachTo(HookManager hookManager) |
15 | | - { |
16 | | - _hookManager = hookManager; |
| 17 | + HookEvents.Terraria.Main.Update += OnUpdate; |
| 18 | + HookEvents.Terraria.Main.Initialize += OnInitialize; |
| 19 | + HookEvents.Terraria.Netplay.StartServer += OnStartServer; |
17 | 20 |
|
18 | | - On.Terraria.Main.Update += OnUpdate; |
19 | | - On.Terraria.Main.Initialize += OnInitialize; |
20 | | - On.Terraria.Netplay.StartServer += OnStartServer; |
| 21 | + Hooks.WorldGen.HardmodeTilePlace += OnHardmodeTilePlace; |
| 22 | + Hooks.WorldGen.HardmodeTileUpdate += OnHardmodeTileUpdate; |
| 23 | + Hooks.Item.MechSpawn += OnItemMechSpawn; |
| 24 | + Hooks.NPC.MechSpawn += OnNpcMechSpawn; |
| 25 | + } |
21 | 26 |
|
22 | | - Hooks.WorldGen.HardmodeTilePlace += OnHardmodeTilePlace; |
23 | | - Hooks.WorldGen.HardmodeTileUpdate += OnHardmodeTileUpdate; |
24 | | - Hooks.Item.MechSpawn += OnItemMechSpawn; |
25 | | - Hooks.NPC.MechSpawn += OnNpcMechSpawn; |
26 | | - } |
| 27 | + private static void OnUpdate(Terraria.Main instance, HookEvents.Terraria.Main.UpdateEventArgs args) |
| 28 | + { |
| 29 | + if (!args.ContinueExecution) return; |
| 30 | + args.ContinueExecution = false; |
| 31 | + _hookManager.InvokeGameUpdate(); |
| 32 | + args.OriginalMethod(args.gameTime); |
| 33 | + _hookManager.InvokeGamePostUpdate(); |
| 34 | + } |
27 | 35 |
|
28 | | - private static void OnUpdate(On.Terraria.Main.orig_Update orig, Terraria.Main instance, GameTime gameTime) |
| 36 | + private static void OnHardmodeTileUpdate(object sender, Hooks.WorldGen.HardmodeTileUpdateEventArgs e) |
| 37 | + { |
| 38 | + if (e.Result == HookResult.Cancel) |
29 | 39 | { |
30 | | - _hookManager.InvokeGameUpdate(); |
31 | | - orig(instance, gameTime); |
32 | | - _hookManager.InvokeGamePostUpdate(); |
| 40 | + return; |
33 | 41 | } |
34 | | - |
35 | | - private static void OnHardmodeTileUpdate(object sender, Hooks.WorldGen.HardmodeTileUpdateEventArgs e) |
| 42 | + if (_hookManager.InvokeGameHardmodeTileUpdate(e.X, e.Y, e.Type)) |
36 | 43 | { |
37 | | - if (e.Result == HookResult.Cancel) |
38 | | - { |
39 | | - return; |
40 | | - } |
41 | | - if (_hookManager.InvokeGameHardmodeTileUpdate(e.X, e.Y, e.Type)) |
42 | | - { |
43 | | - e.Result = HookResult.Cancel; |
44 | | - } |
| 44 | + e.Result = HookResult.Cancel; |
45 | 45 | } |
| 46 | + } |
46 | 47 |
|
47 | | - private static void OnHardmodeTilePlace(object sender, Hooks.WorldGen.HardmodeTilePlaceEventArgs e) |
| 48 | + private static void OnHardmodeTilePlace(object sender, Hooks.WorldGen.HardmodeTilePlaceEventArgs e) |
| 49 | + { |
| 50 | + if (e.Result == HardmodeTileUpdateResult.Cancel) |
48 | 51 | { |
49 | | - if (e.Result == HardmodeTileUpdateResult.Cancel) |
50 | | - { |
51 | | - return; |
52 | | - } |
53 | | - if (_hookManager.InvokeGameHardmodeTileUpdate(e.X, e.Y, e.Type)) |
54 | | - { |
55 | | - e.Result = HardmodeTileUpdateResult.Cancel; |
56 | | - } |
| 52 | + return; |
57 | 53 | } |
58 | | - |
59 | | - private static void OnInitialize(On.Terraria.Main.orig_Initialize orig, Terraria.Main instance) |
| 54 | + if (_hookManager.InvokeGameHardmodeTileUpdate(e.X, e.Y, e.Type)) |
60 | 55 | { |
61 | | - HookManager.InitialiseAPI(); |
62 | | - _hookManager.InvokeGameInitialize(); |
63 | | - orig(instance); |
| 56 | + e.Result = HardmodeTileUpdateResult.Cancel; |
64 | 57 | } |
| 58 | + } |
65 | 59 |
|
66 | | - private static void OnStartServer(On.Terraria.Netplay.orig_StartServer orig) |
| 60 | + private static void OnInitialize(Terraria.Main instance, HookEvents.Terraria.Main.InitializeEventArgs args) |
| 61 | + { |
| 62 | + if (!args.ContinueExecution) return; |
| 63 | + HookManager.InitialiseAPI(); |
| 64 | + _hookManager.InvokeGameInitialize(); |
| 65 | + } |
| 66 | + |
| 67 | + private static void OnStartServer(object? sender, HookEvents.Terraria.Netplay.StartServerEventArgs args) |
| 68 | + { |
| 69 | + if (!args.ContinueExecution) return; |
| 70 | + _hookManager.InvokeGamePostInitialize(); |
| 71 | + } |
| 72 | + |
| 73 | + private static void OnItemMechSpawn(object sender, Hooks.Item.MechSpawnEventArgs e) |
| 74 | + { |
| 75 | + if (e.Result == HookResult.Cancel) |
67 | 76 | { |
68 | | - _hookManager.InvokeGamePostInitialize(); |
69 | | - orig(); |
| 77 | + return; |
70 | 78 | } |
71 | | - |
72 | | - private static void OnItemMechSpawn(object sender, Hooks.Item.MechSpawnEventArgs e) |
| 79 | + if (!_hookManager.InvokeGameStatueSpawn(e.Num2, e.Num3, e.Num, (int)(e.X / 16f), (int)(e.Y / 16f), e.Type, false)) |
73 | 80 | { |
74 | | - if (e.Result == HookResult.Cancel) |
75 | | - { |
76 | | - return; |
77 | | - } |
78 | | - if (!_hookManager.InvokeGameStatueSpawn(e.Num2, e.Num3, e.Num, (int)(e.X / 16f), (int)(e.Y / 16f), e.Type, false)) |
79 | | - { |
80 | | - e.Result = HookResult.Cancel; |
81 | | - } |
| 81 | + e.Result = HookResult.Cancel; |
82 | 82 | } |
| 83 | + } |
83 | 84 |
|
84 | | - private static void OnNpcMechSpawn(object sender, Hooks.NPC.MechSpawnEventArgs e) |
| 85 | + private static void OnNpcMechSpawn(object sender, Hooks.NPC.MechSpawnEventArgs e) |
| 86 | + { |
| 87 | + if (e.Result == HookResult.Cancel) |
| 88 | + { |
| 89 | + return; |
| 90 | + } |
| 91 | + if (!_hookManager.InvokeGameStatueSpawn(e.Num2, e.Num3, e.Num, (int)(e.X / 16f), (int)(e.Y / 16f), e.Type, true)) |
85 | 92 | { |
86 | | - if (e.Result == HookResult.Cancel) |
87 | | - { |
88 | | - return; |
89 | | - } |
90 | | - if (!_hookManager.InvokeGameStatueSpawn(e.Num2, e.Num3, e.Num, (int)(e.X / 16f), (int)(e.Y / 16f), e.Type, true)) |
91 | | - { |
92 | | - e.Result = HookResult.Cancel; |
93 | | - } |
| 93 | + e.Result = HookResult.Cancel; |
94 | 94 | } |
95 | 95 | } |
96 | 96 | } |
0 commit comments