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, StartupGrant};
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::{BookRenderer, BookFontRegistry};
79pub use yog_book::{text_page, text_page_titled, spotlight_page, crafting_page, smelting_page, image_page, entity_page, relations_page, pattern_page};
80pub use yog_ui::{UiRoot, LayoutNode, widget, Align, FlexDir, Dock, FocusStyle};
81
82/// Logging macros (`yog_api::info!`, `warn!`, `error!`).
83pub use yog_logging::{error, info, warn};
84
85/// Core types and handles.
86pub mod core {
87    pub use yog_core::*;
88}
89
90/// Events and the subscription registry.
91pub mod event {
92    pub use yog_event::*;
93}
94
95/// World access (block get/set, dimensions).
96pub mod world {
97    pub use yog_world::*;
98}
99
100/// Entity access (teleport, position, health, ... by UUID).
101pub mod entity {
102    pub use yog_entity::*;
103}
104
105/// Player access (give item, teleport).
106pub mod player {
107    pub use yog_player::*;
108}
109
110/// Content registration (custom items / blocks / food).
111pub mod content {
112    pub use yog_registry::*;
113}
114
115/// Networking (raw-byte packets over channels).
116pub mod network {
117    pub use yog_network::*;
118}
119
120/// Commands.
121pub mod command {
122    pub use yog_command::*;
123}
124
125/// Persistent key-value storage for mod data.
126pub mod storage {
127    pub use yog_storage::*;
128}
129
130/// Mod configuration (typed key/value files).
131pub mod config {
132    pub use yog_config::*;
133}
134
135/// In-game book/documentation system (Patchouli-like).
136pub mod book {
137    pub use yog_book::*;
138}
139
140/// UI framework — flexbox layout + widgets on top of yog-gfx.
141pub mod ui {
142    pub use yog_ui::*;
143}