Skip to content

Commit 03a7382

Browse files
committed
feat: tick callbacks
1 parent 0c194ac commit 03a7382

File tree

11 files changed

+102
-44
lines changed

11 files changed

+102
-44
lines changed

src-test/pc/test/PrimordialCoreTest.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "ll/api/mod/RegisterHelper.h"
66

7-
namespace primordial_core {
7+
namespace pc {
88

99
void Hooked_OriginalFunction(CallbackManager& mgr, int damage) {
1010
Game::EventScope scope;
@@ -109,6 +109,10 @@ bool PrimordialCoreTest::load() {
109109
getLogger().info("Outer scope prevented: {}", scope.isPrevented() ? "Yes" : "No");
110110
}
111111

112+
//mgr.addCallback("OnServerLevelTick", [](uint64 tick) { getLogger().info("OnServerLevelTick: {}", tick); });
113+
//mgr.addCallback("OnClientLevelTick", [](uint64 tick) { getLogger().info("OnClientLevelTick: {}", tick); });
114+
//mgr.addCallback("tick", []() { getLogger().info("tick"); });
115+
112116
return true;
113117
}
114118

@@ -130,6 +134,6 @@ bool PrimordialCoreTest::unload() {
130134
return true;
131135
}
132136

133-
} // namespace primordial_core
137+
} // namespace pc
134138

135-
LL_REGISTER_MOD(primordial_core::PrimordialCoreTest, primordial_core::PrimordialCoreTest::getInstance());
139+
LL_REGISTER_MOD(pc::PrimordialCoreTest, pc::PrimordialCoreTest::getInstance());

src-test/pc/test/PrimordialCoreTest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "ll/api/mod/NativeMod.h"
44

5-
namespace primordial_core {
5+
namespace pc {
66

77
class PrimordialCoreTest {
88

@@ -31,4 +31,4 @@ class PrimordialCoreTest {
3131
ll::mod::NativeMod& mSelf;
3232
};
3333

34-
} // namespace primordial_core
34+
} // namespace pc

src/pc/Macro.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#ifdef PC_EXPORT
4+
#define PCAPI __declspec(dllexport)
5+
#else
6+
#define PCAPI __declspec(dllimport)
7+
#endif
8+
9+
#define PCNDAPI [[nodiscard]] PCAPI
10+
11+
#if defined(LL_PLAT_S)
12+
#define PCAPI_C [[deprecated("Client API not available on SERVER")]] PCAPI
13+
#define PCAPI_S PCAPI
14+
#define PCNDAPI_C [[deprecated("Client API not available on SERVER")]] PCNDAPI
15+
#define PCNDAPI_S PCNDAPI
16+
#elif defined(LL_PLAT_C)
17+
#define PCAPI_C PCAPI
18+
#define PCAPI_S [[deprecated("Server API not available on CLIENT")]] PCAPI
19+
#define PCNDAPI_C PCNDAPI
20+
#define PCNDAPI_S [[deprecated("Server API not available on CLIENT")]] PCNDAPI
21+
#endif

src/pc/callback/CallbackManager.hpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@
1212
#include <type_traits>
1313
#include <vector>
1414

15-
#ifdef PC_EXPORT
16-
#define PCAPI __declspec(dllexport)
17-
#define PCAPI_TEMPLATE
18-
#else
19-
#define PCAPI __declspec(dllimport)
20-
#define PCAPI_TEMPLATE extern
21-
#endif
15+
#include "pc/Macro.h"
16+
#include "pc/mod/PrimordialCore.h"
2217

23-
namespace primordial_core {
18+
namespace pc {
2419

2520
template <typename T>
2621
struct function_traits;
@@ -121,7 +116,7 @@ class CallbackManager {
121116
std::shared_lock lock(mutex);
122117
auto it = events.find(name);
123118
if (it == events.end()) {
124-
std::cerr << "Warning: Event '" << name << "' not found\n";
119+
PrimordialCore::getLogger().debug("No valid callback is registered for '{}'", name);
125120
return;
126121
}
127122

@@ -131,9 +126,9 @@ class CallbackManager {
131126
const auto& container = std::any_cast<const ListT&>(it->second);
132127
container.invoke(std::forward<Args>(args)...);
133128
} catch (const std::bad_any_cast&) {
134-
std::cerr << "Error: Invoke arguments do not match stored signature for '" << name << "'\n";
129+
PrimordialCore::getLogger().error("Invoke callback arguments do not match stored signature for '{}'", name);
135130
}
136131
}
137132
};
138133

139-
} // namespace primordial_core
134+
} // namespace pc

src/pc/callback/ItemCallbacks.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
#include "ll/api/memory/Hook.h"
2-
3-
#include "mc/world/gamemode/InteractionResult.h"
1+
#include "mc/world/gamemode/InteractionResult.h"
42
#include "mc/world/item/Item.h"
53
#include "mc/world/item/ItemUsedOnEventContext.h"
4+
#include "mc/world/actor/Actor.h"
5+
#include "mc/world/level/Level.h"
6+
#include "mc/world/level/Tick.h"
7+
8+
#include "ll/api/memory/Hook.h"
9+
10+
#include "pc/mod/PrimordialCore.h"
11+
#include "pc/callback/CallbackManager.hpp"
12+
13+
using namespace pc;
614

715
LL_AUTO_TYPE_INSTANCE_HOOK(
816
ItemOnUseHook,
917
HookPriority::Normal,
10-
Item,
11-
&Item::useOn,
18+
::Item,
19+
&::Item::useOn,
1220
::InteractionResult,
1321
::ItemStack& item,
1422
::Actor& entity,
@@ -19,5 +27,6 @@ LL_AUTO_TYPE_INSTANCE_HOOK(
1927
::Vec3 const& clickPos,
2028
::ItemUsedOnEventContext itemUsedOnEventContext
2129
) {
30+
PrimordialCore::getLogger().info("Current Tick: {}", entity.getLevel().getCurrentServerTick().tickID);
2231
return origin(item, entity, x, y, z, face, clickPos, itemUsedOnEventContext);
2332
}

src/pc/callback/LevelCallbacks.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "mc/world/level/Level.h"
2+
#include "mc/world/level/Tick.h"
3+
4+
#include "ll/api/memory/Hook.h"
5+
6+
#include "pc/mod/PrimordialCore.h"
7+
#include "pc/callback/CallbackManager.hpp"
8+
#include "pc/world/Game.h"
9+
10+
using namespace pc;
11+
12+
LL_AUTO_TYPE_INSTANCE_HOOK(
13+
LevelTickHook,
14+
HookPriority::Normal,
15+
::Level,
16+
&::Level::$tick,
17+
void) {
18+
{
19+
Game::EventScope scope;
20+
if (isClientSide()) {
21+
CallbackManager::getInstance().invokeCallback("OnClientLevelTick", getCurrentTick().tickID);
22+
#if defined(LL_PLAT_C)
23+
CallbackManager::getInstance().invokeCallback("tick");
24+
#endif
25+
} else {
26+
CallbackManager::getInstance().invokeCallback("OnServerLevelTick", getCurrentTick().tickID);
27+
#if defined(LL_PLAT_S)
28+
CallbackManager::getInstance().invokeCallback("tick");
29+
#endif
30+
}
31+
if (scope.isPrevented()) {
32+
PrimordialCore::getLogger().debug("Level::tick was prevented by a callback!");
33+
return;
34+
}
35+
}
36+
return origin();
37+
}

src/pc/mod/PrimordialCore.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#include "pc/mod/PrimordialCore.h"
2-
#include "pc/callback/CallbackManager.hpp"
3-
#include "pc/world/Game.h"
1+
#include "ll/api/mod/RegisterHelper.h"
42

5-
#include "ll/api/mod/RegisterHelper.h"
3+
#include "pc/mod/PrimordialCore.h"
64

7-
namespace primordial_core {
5+
namespace pc {
86

97
PrimordialCore& PrimordialCore::getInstance() {
108
static PrimordialCore instance;
@@ -35,6 +33,6 @@ bool PrimordialCore::unload() {
3533
return true;
3634
}
3735

38-
} // namespace primordial_core
36+
} // namespace pc
3937

40-
LL_REGISTER_MOD(primordial_core::PrimordialCore, primordial_core::PrimordialCore::getInstance());
38+
LL_REGISTER_MOD(pc::PrimordialCore, pc::PrimordialCore::getInstance());

src/pc/mod/PrimordialCore.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "ll/api/mod/NativeMod.h"
44

5-
namespace primordial_core {
5+
namespace pc {
66

77
class PrimordialCore {
88

@@ -31,4 +31,4 @@ class PrimordialCore {
3131
ll::mod::NativeMod& mSelf;
3232
};
3333

34-
} // namespace primordial_core
34+
} // namespace pc

src/pc/world/Game.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "pc/world/Game.h"
1+
#include "pc/world/Game.h"
22

3-
namespace primordial_core {
3+
namespace pc {
44

55
thread_local std::vector<bool> Game::prevent_stack;
66

@@ -26,4 +26,4 @@ Game::EventScope::~EventScope() { Game::prevent_stack.pop_back(); }
2626

2727
bool Game::EventScope::isPrevented() const { return Game::getInstance()._isDefaultPrevented(); }
2828

29-
} // namespace primordial_core
29+
} // namespace pc

src/pc/world/Game.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
#include <vector>
44

5-
#ifdef PC_EXPORT
6-
#define PCAPI __declspec(dllexport)
7-
#else
8-
#define PCAPI __declspec(dllimport)
9-
#endif
5+
#include "pc/Macro.h"
106

11-
namespace primordial_core {
7+
namespace pc {
128

139
class Game {
1410
private:
@@ -37,4 +33,4 @@ class Game {
3733
};
3834
};
3935

40-
} // namespace primordial_core
36+
} // namespace pc

0 commit comments

Comments
 (0)