Skip to main content

khora_core/runtime/
mod.rs

1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Runtime containers — engine-wide injection points for services,
16//! backends, and resources.
17//!
18//! Replaces the legacy `ServiceRegistry` fourre-tout with three distinct
19//! containers, each with a clear admission criterion:
20//!
21//! | Container | What lives here | Examples |
22//! |---|---|---|
23//! | [`Services`] | Concrete stateful objects with rich business APIs | `AssetService`, `SerializationService`, `TelemetryService`, `DccService` |
24//! | [`Backends`] | Concrete impls of abstract traits defined in `khora-core` | `dyn RenderSystem`, `dyn PhysicsProvider`, `dyn AudioDevice`, `dyn LayoutSystem` |
25//! | [`Resources`] | Long-lived shared state without a service-style API | `InputMap`, `EditorViewportOverride`, `GpuCache`, `UiAtlasMap` |
26//!
27//! Per-frame state (current viewport, frame deltas, lane outputs) does
28//! NOT belong in any of these containers — it flows through
29//! [`LaneContext`](crate::lane::LaneContext),
30//! [`LaneBus`](crate::lane::LaneBus), and
31//! [`OutputDeck`](crate::lane::OutputDeck).
32//!
33//! Game devs and plugins are free to register their own entries in any of
34//! the three containers, applying the same admission criteria.
35
36mod backends;
37mod resources;
38mod services;
39
40pub use backends::Backends;
41pub use resources::Resources;
42pub use services::Services;
43
44/// Bundle of the three runtime containers.
45///
46/// Engine init builds one `Runtime` (mutating its containers freely),
47/// then wraps it in an `Arc<Runtime>` and stores it in
48/// [`EngineContext`](crate::EngineContext). After that point the bundle
49/// is immutable for the rest of the engine lifetime.
50///
51/// Lanes, agents, flows, and data systems receive `&Runtime` and look up
52/// what they need with `runtime.services.get::<X>()`,
53/// `runtime.backends.get::<X>()`, or `runtime.resources.get::<X>()`.
54#[derive(Default, Debug)]
55pub struct Runtime {
56    /// Stateful business-logic objects (`AssetService`, …).
57    pub services: Services,
58    /// Concrete trait implementations (`RenderSystem`, …).
59    pub backends: Backends,
60    /// Long-lived shared state (`InputMap`, …).
61    pub resources: Resources,
62}
63
64impl Runtime {
65    /// Creates an empty bundle (all three containers empty).
66    #[must_use]
67    pub fn new() -> Self {
68        Self::default()
69    }
70}