Skip to content

Commit 1ce7c03

Browse files
committed
♻️ move resource manager creation out of lua env to be able to initialize plugins
1 parent 4adfbc0 commit 1ce7c03

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

runtime/src/game.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use vectarine_plugin_sdk::plugininterface::PluginInterface;
77

88
use crate::{
99
console::print_warn,
10-
game_resource::{Resource, ResourceId, Status, script_resource::ScriptResource},
10+
game_resource::{
11+
Resource, ResourceId, ResourceManager, Status, script_resource::ScriptResource,
12+
},
1113
graphics::batchdraw::BatchDraw2d,
1214
io::{fs::ReadOnlyFileSystem, process_events},
1315
lua_env::{LuaEnvironment, lua_screen, print_lua_error_from_error},
@@ -27,6 +29,8 @@ pub struct Game {
2729
pub main_script_path: String,
2830

2931
pub metrics_holder: Rc<RefCell<MetricsHolder>>,
32+
33+
pub plugin_env: PluginEnvironment,
3034
}
3135

3236
impl Game {
@@ -58,27 +62,39 @@ impl Game {
5862
project_info.default_screen_height,
5963
);
6064

65+
// Create all the things we need for a game
6166
let batch = BatchDraw2d::new(&gl).expect("Failed to create batch 2d");
6267
let metrics = Rc::new(RefCell::new(MetricsHolder::new()));
63-
let lua_env = LuaEnvironment::new(batch, file_system, project_dir, metrics.clone());
64-
let mut game = Game::from_lua(&gl, lua_env, project_info.main_script_path.clone(), metrics);
65-
game.load(video, window);
68+
let resources = Rc::new(ResourceManager::new(file_system, project_dir));
69+
let plugin_env = PluginEnvironment::load_plugins(&project_info.plugins, &resources);
70+
let lua_env = LuaEnvironment::new(batch, metrics.clone(), resources);
6671

67-
let plugin_env =
68-
PluginEnvironment::load_plugins(&project_info.plugins, &game.lua_env.resources);
69-
plugin_env.init(PluginInterface {
72+
// Make the game!
73+
let mut game = Game::from_lua(
74+
&gl,
75+
lua_env,
76+
project_info.main_script_path.clone(),
77+
metrics,
78+
plugin_env,
79+
);
80+
81+
game.load(video, window);
82+
game.plugin_env.init(PluginInterface {
7083
lua: &game.lua_env.lua,
7184
});
7285

86+
// Load the starting script
7387
let path = Path::new(&game.main_script_path);
7488
game.lua_env.resources.load_resource::<ScriptResource>(
7589
path,
7690
gl,
7791
game.lua_env.lua.clone(),
7892
game.lua_env.default_events.resource_loaded_event.clone(),
7993
);
80-
// New game means new sounds, so we discard the previous ones.
94+
95+
// New game means new sounds, so we discard the previous ones (this is useful only for the editor).
8196
sound::flush_all_samples();
97+
8298
callback(Ok(game));
8399
}
84100

@@ -87,13 +103,15 @@ impl Game {
87103
lua_env: LuaEnvironment,
88104
main_script_path: String,
89105
metrics_holder: Rc<RefCell<MetricsHolder>>,
106+
plugin_env: PluginEnvironment,
90107
) -> Self {
91108
Game {
92109
gl: gl.clone(),
93110
lua_env,
94111
was_main_script_executed: false,
95112
main_script_path,
96113
metrics_holder,
114+
plugin_env,
97115
}
98116
}
99117

runtime/src/lua_env.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ impl LuaEnvironment {
5353
#[allow(clippy::unwrap_used)]
5454
pub fn new(
5555
batch: BatchDraw2d,
56-
file_system: Box<dyn ReadOnlyFileSystem>,
57-
base_path: &Path,
5856
metrics: Rc<RefCell<MetricsHolder>>,
57+
resources: Rc<ResourceManager>,
5958
) -> Self {
6059
let batch = Rc::new(RefCell::new(batch));
6160
let lua_options = vectarine_plugin_sdk::mlua::LuaOptions::default();
@@ -83,7 +82,6 @@ impl LuaEnvironment {
8382
.raw_set(UNSAFE_INTERNALS_KEY, lua.create_table().unwrap())
8483
.unwrap();
8584

86-
let resources = Rc::new(ResourceManager::new(file_system, base_path));
8785
let env_state = Rc::new(RefCell::new(IoEnvState::default()));
8886

8987
let persist_module = lua_persist::setup_persist_api(&lua).unwrap();

0 commit comments

Comments
 (0)