Skip to main content

yog_api/
lib.rs

1//! Yog API — the single crate mod authors depend on.
2//!
3//! A facade that re-exports every Yog domain plus the central [`Registry`] hub.
4//! Add a new domain crate, re-export it here, and mods pick it up via
5//! `yog_api::*`. Items are available both flat (`yog_api::Registry`) and
6//! namespaced by domain (`yog_api::world::World`).
7
8mod registry;
9
10pub use registry::{CServer, Mod, Registry};
11pub use yog_gfx::{GfxContext, core as gfx_core, gl as gfx_gl, draw2d as gfx_draw2d};
12
13/// Stable C ABI — re-exported so mods don't need a direct `yog-abi` dependency.
14pub use yog_abi::{ABI_VERSION, YogApi};
15
16#[doc(hidden)]
17pub use std::os::raw::c_void as __c_void;
18
19/// Export a [`Mod`] as a dynamically loadable Yog mod.
20///
21/// Generates the two C-ABI entry points the runtime looks up:
22/// - `yog_abi_version() -> u32`  — version check before loading
23/// - `yog_mod_register(*const YogApi, *mut c_void)` — registration entry point
24///
25/// Put this once at the crate root of a `cdylib` mod:
26///
27/// ```ignore
28/// yog_api::export_mod!(MyMod);
29/// ```
30#[macro_export]
31macro_rules! export_mod {
32    ($mod_ty:ty) => {
33        #[no_mangle]
34        pub extern "C" fn yog_abi_version() -> u32 {
35            $crate::ABI_VERSION
36        }
37
38        #[no_mangle]
39        pub unsafe extern "C" fn yog_mod_register(
40            api: *const $crate::YogApi,
41            _ctx: *mut $crate::__c_void,
42        ) {
43            // Catch panics so they never unwind across this `extern "C"` boundary
44            // back into the runtime (which would be undefined behaviour).
45            let outcome = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
46                // SAFETY: the runtime passes a valid YogApi pointer, verified via
47                // yog_abi_version() and abi_version/size checks before this call.
48                let mut registry = unsafe { $crate::Registry::from_raw(api) };
49                <$mod_ty as $crate::Mod>::register(&mut registry);
50            }));
51            if outcome.is_err() {
52                $crate::error!("mod {} panicked during register", ::core::stringify!($mod_ty));
53            }
54        }
55    };
56}
57
58pub use yog_command::CommandContext;
59pub use yog_core::{BlockPos, Server};
60pub use yog_event::{
61    AdvancementEvent, AttackEntityEvent, BlockBreakEvent, ChatEvent, ClientTickEvent,
62    ContainerCloseEvent, ContainerOpenEvent, CraftEvent, EntityDamageEvent, EntityDeathEvent,
63    EntityInteractEvent, EntitySpawnEvent, EventPhase, ExplosionEvent,
64    ItemPickupEvent, KeyPressEvent, PlaceBlockEvent, PlayerDeathEvent, PlayerJoinEvent,
65    PlayerLeaveEvent, PlayerMoveEvent, PlayerRespawnEvent, ProjectileHitEvent, ScreenEvent,
66    UseBlockEvent, UseItemEvent,
67};
68pub use yog_entity::Entity;
69pub use yog_network::{Packet, PacketEvent, PacketField};
70#[doc(inline)]
71pub use yog_network::packet;
72pub use yog_player::Player;
73pub use yog_registry::{BlockDef, FoodDef, FurnaceRecipe, ItemDef, ShapedRecipe, ShapelessRecipe, BookRecipe, ItemModifier, AdvancementReward};
74pub use yog_config::Config;
75pub use yog_storage::{Storage, StorageScope, Value};
76pub use yog_world::World;
77pub use yog_book::{Book, BookCategory, BookEntry, BookPage, BookMacro, BookRegistry};
78pub use yog_book::{text_page, spotlight_page, crafting_page, smelting_page, image_page, entity_page, relations_page, pattern_page};
79
80/// Logging macros (`yog_api::info!`, `warn!`, `error!`).
81pub use yog_logging::{error, info, warn};
82
83/// Core types and handles.
84pub mod core {
85    pub use yog_core::*;
86}
87
88/// Events and the subscription registry.
89pub mod event {
90    pub use yog_event::*;
91}
92
93/// World access (block get/set, dimensions).
94pub mod world {
95    pub use yog_world::*;
96}
97
98/// Entity access (teleport, position, health, ... by UUID).
99pub mod entity {
100    pub use yog_entity::*;
101}
102
103/// Player access (give item, teleport).
104pub mod player {
105    pub use yog_player::*;
106}
107
108/// Content registration (custom items / blocks / food).
109pub mod content {
110    pub use yog_registry::*;
111}
112
113/// Networking (raw-byte packets over channels).
114pub mod network {
115    pub use yog_network::*;
116}
117
118/// Commands.
119pub mod command {
120    pub use yog_command::*;
121}
122
123/// Persistent key-value storage for mod data.
124pub mod storage {
125    pub use yog_storage::*;
126}
127
128/// Mod configuration (typed key/value files).
129pub mod config {
130    pub use yog_config::*;
131}
132
133/// In-game book/documentation system (Patchouli-like).
134pub mod book {
135    pub use yog_book::*;
136}