yog-runtime 0.102.2

Yog runtime — native library bridging the Fabric host to Rust mods via JNI.
Documentation
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
//! UI system JNI — layer management, show/hide, hit-test, event dispatch.

use crate::{UiLayer, handlers};
use jni::{JNIEnv, objects::JClass, objects::JString, sys::*};

use crate::jcall::jcall;
use std::cell::Cell;
use yog_abi::YogStr;

/// Sends `ev` to the `register_ui` handler of `id`, if any (result ignored).
fn dispatch(id: &str, ev: &str) {
    let h = handlers();
    let Some((module, ud, handler)) = h.ui_handlers.get(id).cloned() else {
        return;
    };
    if module.is_retiring() {
        return;
    }
    if let Some(_call) = module.enter() {
        unsafe {
            handler(ud, YogStr::from_str(id), YogStr::from_str(ev));
        }
    }
}

/// A Yog UI screen (or inventory screen) opened: a new top layer, and
/// `open:W:H` for its handler (yog-kit's `on_open`).
#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIShow<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    parent_id: JString<'l>,
    modal: jboolean,
    pause_game: jboolean,
    screen_w: jint,
    screen_h: jint,
) {
    let id = jstr!(env, ui_id);
    let parent = jstr!(env, parent_id);
    let parent = (!parent.is_empty()).then_some(parent);
    let h = handlers();
    let mut active = h.active_uis.lock().expect("active_uis");
    active.retain(|l| l.id != id);
    let layer = UiLayer {
        id: id.clone(),
        parent,
        modal: modal != 0,
        pause_game: pause_game != 0,
        visible: true,
        enabled: true,
        z_index: active.iter().map(|l| l.z_index).max().unwrap_or(0) + 1,
    };
    active.push(layer);
    active.sort_by_key(|l| l.z_index);
    yog_logging::info!(
        "UI show: {} modal={} pause={} layers={}",
        id,
        modal != 0,
        pause_game != 0,
        active.len()
    );
    drop(active);
    dispatch(&id, &format!("open:{screen_w}:{screen_h}"));
}

/// A Yog UI screen closed: it and its child layers go, each handler gets
/// `close` (yog-kit's `on_close`).
#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIHide<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
) {
    let id = jstr!(env, ui_id);
    let h = handlers();
    let mut active = h.active_uis.lock().expect("active_uis");
    let closed: Vec<String> = active
        .iter()
        .filter(|l| l.id == id || l.parent.as_deref() == Some(&id))
        .map(|l| l.id.clone())
        .collect();
    active.retain(|l| !closed.contains(&l.id));
    yog_logging::info!("UI hide: {} (layers left: {})", id, active.len());
    drop(active);
    for c in &closed {
        dispatch(c, "close");
    }
}

/// The cursor moved over a Yog UI screen — forwarded as `move:X:Y` (hover
/// states, wheel routing).
#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIMove<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    mx: jfloat,
    my: jfloat,
) {
    let id = jstr!(env, ui_id);
    dispatch(&id, &format!("move:{mx:.1}:{my:.1}"));
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeIsUIActive<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
) -> jboolean {
    let id = jstr!(env, ui_id);
    let h = handlers();
    let active = h.active_uis.lock().expect("active_uis");
    active.iter().any(|l| l.id == id && l.visible) as jboolean
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIClick<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    mx: jfloat,
    my: jfloat,
    button: jint,
) -> jboolean {
    let id = jstr!(env, ui_id);
    let h = handlers();
    let active = h.active_uis.lock().expect("active_uis");
    // Find topmost modal layer that contains this click
    let top_modal = active
        .iter()
        .rev()
        .find(|l| l.visible && l.modal)
        .map(|l| l.id.clone());
    // Only dispatch if click is on the topmost modal layer (or there's no modal)
    if let Some(ref modal_id) = top_modal
        && id != *modal_id
    {
        return 0;
    }
    drop(active);

    // Try book click first: hit-test against the book renderer's last layout.
    // Only block generic handler if the book actually has a rendered UI (ui.is_some()).
    {
        let mut renderers = h.book_renderers.lock().expect("book_renderers");
        if let Some(renderer) = renderers.get_mut(&id)
            && renderer.ui.is_some()
        {
            if let Some(ui) = &renderer.ui
                && let Some(hit) = yog_ui::layout::hit_test(&ui.layout_root, mx, my)
                && let Some(event) = &hit.on_click
            {
                let ev = event.clone();
                drop(renderers);
                if let Some(r) = h.book_renderers.lock().unwrap().get_mut(&id) {
                    r.handle_event(&ev);
                }
                yog_logging::info!("book click '{}' → '{}'", &id, &ev);
                return 1;
            }
            return 1; // book UI consumed the click (no hit on widget, but in book area)
        }
        // ui=None (JSON parse failed) → fall through to generic handler
    }

    // Generic UI handler: raw click coords, hit-tested by the mod. A click
    // it doesn't use (on an inventory screen's slot, say) goes on to the game.
    let _ = button;
    dispatch_consumable(&id, &format!("click:{mx:.1}:{my:.1}")) as jboolean
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIScroll<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    dy: jfloat,
) {
    let id = jstr!(env, ui_id);
    let h = handlers();
    // Forward as a generic UI event; the mod applies its own scroll offset.
    if let Some((module, ud, handler)) = h.ui_handlers.get(&id).cloned()
        && !module.is_retiring()
        && let Some(_call) = module.enter()
    {
        let ev = format!("scroll:{dy:.2}");
        unsafe {
            handler(ud, YogStr::from_str(&id), YogStr::from_str(&ev));
        }
    }
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIDrag<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    mx: jfloat,
    my: jfloat,
) {
    let id = jstr!(env, ui_id);
    let h = handlers();
    if let Some((module, ud, handler)) = h.ui_handlers.get(&id).cloned()
        && !module.is_retiring()
        && let Some(_call) = module.enter()
    {
        let ev = format!("drag:{mx:.1}:{my:.1}");
        unsafe {
            handler(ud, YogStr::from_str(&id), YogStr::from_str(&ev));
        }
    }
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIRelease<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    mx: jfloat,
    my: jfloat,
) {
    let id = jstr!(env, ui_id);
    let h = handlers();
    if let Some((module, ud, handler)) = h.ui_handlers.get(&id).cloned()
        && !module.is_retiring()
        && let Some(_call) = module.enter()
    {
        let ev = format!("release:{mx:.1}:{my:.1}");
        unsafe {
            handler(ud, YogStr::from_str(&id), YogStr::from_str(&ev));
        }
    }
}

thread_local! {
    /// Set by `YogApi::ui_consume_event` while a `key:`/`char:` event is
    /// being dispatched — the mod used the key.
    static CONSUMED: Cell<bool> = const { Cell::new(false) };
    /// A UTF-16 high surrogate waiting for its low half (`charTyped` hands
    /// characters outside the BMP over as two calls).
    static HIGH_SURROGATE: Cell<Option<u16>> = const { Cell::new(None) };
}

/// `YogApi::ui_consume_event`: marks the event being dispatched as used.
pub(crate) fn consume_event() {
    CONSUMED.with(|c| c.set(true));
}

/// Sends `ev` to the `register_ui` handler of `id`; `true` when the handler
/// consumed it (called `ui_consume_event`).
fn dispatch_consumable(id: &str, ev: &str) -> bool {
    let h = handlers();
    let Some((module, ud, handler)) = h.ui_handlers.get(id).cloned() else {
        return false;
    };
    if module.is_retiring() {
        return false;
    }
    let Some(_call) = module.enter() else {
        return false;
    };
    let before = CONSUMED.with(|c| c.replace(false));
    unsafe {
        handler(ud, YogStr::from_str(id), YogStr::from_str(ev));
    }
    CONSUMED.with(|c| c.replace(before))
}

/// A key pressed (or repeating) over a Yog UI screen — forwarded as
/// `key:KEY:MODS` (GLFW key code and modifier bits). Returns whether the mod
/// consumed it; if not, the host does its default (Esc closes the screen).
#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIKey<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    key: jint,
    _scan: jint,
    mods: jint,
    action: jint,
) -> jboolean {
    let id = jstr!(env, ui_id);
    if action == 0 {
        return 0;
    }
    dispatch_consumable(&id, &format!("key:{key}:{mods}")) as jboolean
}

/// A character typed over a Yog UI screen (one UTF-16 unit, as
/// `Screen.charTyped` gets it) — forwarded as `char:CODEPOINT` once a whole
/// character is known. Returns whether the mod consumed it.
#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIChar<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    chr: jchar,
    _mods: jint,
) -> jboolean {
    let id = jstr!(env, ui_id);
    let pending = HIGH_SURROGATE.with(std::cell::Cell::take);
    let c = match chr {
        0xD800..=0xDBFF => {
            HIGH_SURROGATE.with(|p| p.set(Some(chr)));
            return 1;
        }
        0xDC00..=0xDFFF => match pending {
            Some(hi) => char::decode_utf16([hi, chr])
                .next()
                .and_then(std::result::Result::ok),
            None => None,
        },
        _ => char::from_u32(chr as u32),
    };
    let Some(c) = c else { return 0 };
    dispatch_consumable(&id, &format!("char:{}", c as u32)) as jboolean
}

#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeUIRender<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    ui_id: JString<'l>,
    screen_w: jint,
    screen_h: jint,
) {
    let id = jstr!(env, ui_id);

    // Build a fresh snapshot of the currently-open inventory screen's slots
    // and push it into every loaded mod (see `yog_inventory::current_slot`)
    // — a plain Rust `static` in `yog-inventory` can't cross the cdylib
    // boundary between this runtime and a mod's own dynamic library, so each
    // mod gets the data via its own exported `yog_inventory_set_snapshot`
    // symbol instead, resolved the same way `yog_mod_register` is.
    // `slot_item_ids` and `snapshot` must outlive the `render_cbs` loop below
    // (not just this `if`) — mods read through the pointer we hand them via
    // `push_inventory_snapshot` for the rest of this function's duration.
    let n = (inv_slot_count(&mut env).max(0) as usize).min(yog_inventory::MAX_SNAPSHOT_SLOTS);
    let mut slot_item_ids: Vec<String> = Vec::with_capacity(n);
    let mut snapshot = yog_inventory::YogInvSnapshotRaw::EMPTY;
    if n > 0 {
        snapshot.slot_count = n as u32;
        for i in 0..n {
            let item = inv_slot_item(&mut env, i as i32);
            let pos = inv_slot_pos(&mut env, i as i32).unwrap_or((0, 0));
            slot_item_ids.push(item.as_ref().map(|(id, _)| id.clone()).unwrap_or_default());
            snapshot.slots[i].count = item.map(|(_, c)| c as u32).unwrap_or(0);
            snapshot.slots[i].x = pos.0;
            snapshot.slots[i].y = pos.1;
        }
        for (slot, id) in snapshot.slots.iter_mut().zip(&slot_item_ids) {
            slot.item_id = YogStr::from_str(id);
        }
        push_inventory_snapshot(&snapshot);
    }

    let h = crate::handlers();
    let mut gfx = crate::GFX_FN_TABLE;
    gfx.screen_w = screen_w;
    gfx.screen_h = screen_h;
    gfx.delta_tick = 1.0;
    gfx.scale_factor = 1.0;
    let ctx = unsafe { yog_gfx::GfxContext::from_raw(&gfx as *const _) };
    let sw = screen_w as f32;
    let sh = screen_h as f32;

    // Book renderer path (books registered via register_book).
    // Only use if the book has a successfully parsed layout (ui.is_some()).
    {
        let mut book_renderers = h.book_renderers.lock().expect("book_renderers");
        if let Some(book_renderer) = book_renderers.get_mut(&id)
            && book_renderer.ui.is_some()
        {
            // Pictures of `drawn` pages: the mod that registered the drawer draws them.
            let gfx_ptr = &gfx as *const _;
            book_renderer.render_with(&ctx, sw, sh, &mut |_, drawer, data, [x, y, w, bh]| {
                let Some((module, ud, f)) = h.page_drawers.get(drawer).cloned() else {
                    return;
                };
                if module.is_retiring() {
                    return;
                }
                let Some(_call) = module.enter() else {
                    return;
                };
                let bytes = yog_data::binary::encode(data);
                unsafe { f(ud, gfx_ptr, bytes.as_ptr(), bytes.len(), x, y, w, bh) };
            });
            return;
        }
        // ui=None → fall through to on_ui_render callbacks
    }

    // Generic on_ui_render callbacks — run AFTER renderBackground(), so they appear
    // on top of the screen darkening (unlike on_hud_render which runs before screens).
    let render_cbs: Vec<_> = h.ui_render_handlers.get(&id).cloned().unwrap_or_default();
    for (module, ud, f) in render_cbs {
        if module.is_retiring() {
            continue;
        }
        let Some(_call) = module.enter() else {
            continue;
        };
        unsafe { f(ud, &gfx as *const _) };
    }
}

/// Hand `snapshot` to every loaded mod's own copy of `yog-inventory`'s
/// module-level static, via each mod's exported `yog_inventory_set_snapshot`
/// symbol (present only in mods that depend on `yog-inventory`; missing on
/// others, which is fine — just skip them).
fn push_inventory_snapshot(snapshot: &yog_inventory::YogInvSnapshotRaw) {
    type SetSnapshotFn = unsafe extern "C" fn(*const yog_inventory::YogInvSnapshotRaw);
    for (_id, module) in crate::hot_reloader().all_active() {
        if module.is_retiring() {
            continue;
        }
        let Some(_call) = module.enter() else {
            continue;
        };
        module.with_library(|lib| unsafe {
            if let Ok(f) = lib.get::<SetSnapshotFn>(b"yog_inventory_set_snapshot") {
                f(snapshot as *const _);
            }
        });
    }
}

// ── Inventory slot queries (Rust → Java, called during yog-ui render) ──────

/// Number of slots in the currently open inventory screen (0 if none).
pub fn inv_slot_count(env: &mut JNIEnv) -> i32 {
    unsafe { jcall!(env, "dev/yog/NativeBridge", "getSlotCount", []) }.unwrap_or(0)
}

/// Query one slot: returns `Some((item_id, count))` or `None` if empty.
/// `item_id` is a namespaced string like `"minecraft:diamond"`.
pub fn inv_slot_item(env: &mut JNIEnv, index: i32) -> Option<(String, i32)> {
    let s: String = unsafe { jcall!(env, "dev/yog/NativeBridge", "getSlotItem", [I(index)]) }?;
    let (id, count) = s.split_once('\t')?;
    Some((id.to_owned(), count.parse().ok()?))
}

/// Pixel position of a slot (relative to screen, already including guiLeft/guiTop).
pub fn inv_slot_pos(env: &mut JNIEnv, index: i32) -> Option<(i32, i32)> {
    let s: String = unsafe { jcall!(env, "dev/yog/NativeBridge", "getSlotPos", [I(index)]) }?;
    let [x, y] = crate::parse_tsv(&s)?;
    Some((x, y))
}

/// Return tab+newline-encoded menu entries: `label\tui_id\n...`
/// Java side parses this and injects buttons into vanilla screens.
#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeMenuEntries<'l>(
    env: JNIEnv<'l>,
    _class: JClass<'l>,
) -> jstring {
    let h = handlers();
    let lines: Vec<String> = h
        .menu_entries
        .iter()
        .map(|(label, ui_id)| format!("{label}\t{ui_id}"))
        .collect();
    let out = lines.join("\n");
    env.new_string(out).unwrap().into_raw()
}

// ── Pictures in item tooltips (ABI 2.2) ─────────────────────────────────────

/// The mod's picture for a stack of `item` (custom data `snbt`): which
/// entry, and its size.
fn tooltip_of(h: &crate::RuntimeHandlers, item: &str, snbt: &str) -> Option<(crate::TooltipEntry, f32, f32)> {
    for entry in h.item_tooltips.get(item)? {
        let (module, ud, size, _) = entry.clone();
        if module.is_retiring() {
            continue;
        }
        let Some(_call) = module.enter() else {
            continue;
        };
        let (mut w, mut hh) = (0.0f32, 0.0f32);
        if unsafe { size(ud, YogStr::from_str(item), YogStr::from_str(snbt), &mut w, &mut hh) } && w > 0.0 && hh > 0.0 {
            return Some((entry.clone(), w, hh));
        }
    }
    None
}

/// `float[]{w, h}` of a stack's tooltip picture, or null for none.
#[unsafe(no_mangle)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeTooltipSize<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    item: JString<'l>,
    snbt: JString<'l>,
) -> jfloatArray {
    let item = jstr!(env, item, std::ptr::null_mut());
    let snbt = jstr!(env, snbt, std::ptr::null_mut());
    let h = handlers();
    let Some((_, w, hh)) = tooltip_of(&h, &item, &snbt) else {
        return std::ptr::null_mut();
    };
    let Ok(arr) = env.new_float_array(2) else {
        return std::ptr::null_mut();
    };
    let _ = env.set_float_array_region(&arr, 0, &[w, hh]);
    arr.into_raw()
}

/// Draws a stack's tooltip picture in `x, y, w, h` (GUI pixels).
#[unsafe(no_mangle)]
#[allow(clippy::too_many_arguments)]
pub extern "system" fn Java_dev_yog_NativeBridge_nativeTooltipDraw<'l>(
    mut env: JNIEnv<'l>,
    _class: JClass<'l>,
    item: JString<'l>,
    snbt: JString<'l>,
    x: jint,
    y: jint,
    w: jint,
    hh: jint,
    screen_w: jint,
    screen_h: jint,
) {
    let item = jstr!(env, item);
    let snbt = jstr!(env, snbt);
    let h = handlers();
    let Some(((module, ud, _, draw), ..)) = tooltip_of(&h, &item, &snbt) else {
        return;
    };
    let Some(_call) = module.enter() else {
        return;
    };
    let mut gfx = crate::GFX_FN_TABLE;
    gfx.screen_w = screen_w;
    gfx.screen_h = screen_h;
    gfx.delta_tick = 1.0;
    gfx.scale_factor = 1.0;
    unsafe { draw(ud, &gfx as *const _, YogStr::from_str(&item), YogStr::from_str(&snbt), x as f32, y as f32, w as f32, hh as f32) };
}