engine: load lua from code, driveby stuff
parent
05dd81fa4f
commit
10e513dff4
|
@ -13,7 +13,11 @@ http_archive(
|
|||
)
|
||||
|
||||
load("@rules_rust//rust:repositories.bzl", "rust_repositories")
|
||||
rust_repositories()
|
||||
rust_repositories(
|
||||
version = "nightly",
|
||||
iso_date = "2020-12-31",
|
||||
edition = "2018",
|
||||
)
|
||||
|
||||
load("//third_party/shaderc:deps.bzl", "shaderc_deps")
|
||||
shaderc_deps()
|
||||
|
|
|
@ -9,6 +9,8 @@ rust_binary(
|
|||
"-C", "overflow-checks=n",
|
||||
"-C", "force-frame-pointers=y",
|
||||
"-C", "lto=off",
|
||||
"-Zpolonius",
|
||||
"-Zborrowck=mir",
|
||||
],
|
||||
srcs = [
|
||||
"src/main.rs",
|
||||
|
@ -60,10 +62,11 @@ rust_binary(
|
|||
"//lib/ecs_macros",
|
||||
],
|
||||
data = [
|
||||
"//engine/shaders:forward_vert",
|
||||
"//engine/shaders:forward_frag",
|
||||
"//assets:test-128px.png",
|
||||
"//assets:test-128px-roughness.png",
|
||||
"//engine/shaders:forward_vert",
|
||||
"//engine/shaders:forward_frag",
|
||||
":init.lua",
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
print("Hello, Lua2!")
|
||||
--for k,v in pairs(components) do
|
||||
-- print("Component", k, v)
|
||||
--end
|
||||
|
||||
local sent = {}
|
||||
sent.register = function (name, cls)
|
||||
if cls.__sent_class_id ~= nil then
|
||||
print("Attempting to re-register " .. name)
|
||||
return
|
||||
end
|
||||
local sent_class_id = __sent_register(name, cls)
|
||||
cls.__sent_class_id = sent_class_id
|
||||
cls.new = function(...)
|
||||
local arg = {...}
|
||||
local res = __sent_new(sent_class_id)
|
||||
if res.init ~= nil then
|
||||
res:init(unpack(arg))
|
||||
end
|
||||
return res
|
||||
end
|
||||
end
|
||||
|
||||
local Test = {}
|
||||
|
||||
function Test:init(val)
|
||||
self.val = val
|
||||
end
|
||||
|
||||
function Test:tick()
|
||||
print("tick! " .. tostring(self.val))
|
||||
end
|
||||
|
||||
sent.register("Test", Test)
|
||||
local t1 = Test.new(123)
|
||||
t1:tick()
|
||||
local t2 = Test.new(234)
|
||||
t2:tick()
|
|
@ -220,46 +220,8 @@ fn main() {
|
|||
let main = Main::new(&mut world, &mut renderer);
|
||||
|
||||
let context = scripting::WorldContext::new(&world);
|
||||
context.eval_init(r#"
|
||||
print("Hello, Lua!")
|
||||
--for k,v in pairs(components) do
|
||||
-- print("Component", k, v)
|
||||
--end
|
||||
|
||||
local sent = {}
|
||||
sent.register = function (name, cls)
|
||||
if cls.__sent_class_id ~= nil then
|
||||
print("Attempting to re-register " .. name)
|
||||
return
|
||||
end
|
||||
local sent_class_id = __sent_register(name, cls)
|
||||
cls.__sent_class_id = sent_class_id
|
||||
cls.new = function(...)
|
||||
local arg = {...}
|
||||
local res = __sent_new(sent_class_id)
|
||||
if res.init ~= nil then
|
||||
res:init(unpack(arg))
|
||||
end
|
||||
return res
|
||||
end
|
||||
end
|
||||
|
||||
local Test = {}
|
||||
|
||||
function Test:init(val)
|
||||
self.val = val
|
||||
end
|
||||
|
||||
function Test:tick()
|
||||
print("tick! " .. tostring(self.val))
|
||||
end
|
||||
|
||||
sent.register("Test", Test)
|
||||
local t1 = Test.new(123)
|
||||
t1:tick()
|
||||
local t2 = Test.new(234)
|
||||
t2:tick()
|
||||
"#).unwrap();
|
||||
let init = util::file::resource("//engine/init.lua").unwrap().string().unwrap();
|
||||
context.eval_init(init).unwrap();
|
||||
|
||||
log::info!("Starting...");
|
||||
|
||||
|
@ -275,6 +237,7 @@ fn main() {
|
|||
});
|
||||
world.set_global(input::Input::new());
|
||||
loop {
|
||||
//world.queue_drain();
|
||||
world.global_mut::<Time>().get().now = time::Instant::now();
|
||||
|
||||
p.run();
|
||||
|
|
|
@ -141,12 +141,14 @@ impl WorldContext {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn eval_init(&self, val: &str) -> mlua::Result<()>
|
||||
pub fn eval_init<T>(&self, val: T) -> mlua::Result<()>
|
||||
where
|
||||
T: Into<String>
|
||||
{
|
||||
let val: String = val.into();
|
||||
self.lua.scope(|scope| {
|
||||
self.scope_sent(scope)?;
|
||||
self.lua.load(val).exec()
|
||||
self.lua.load(&val).exec()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU General Public License along with
|
||||
// Abrasion. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use std::path;
|
||||
use std::io::Read;
|
||||
|
||||
use runfiles::Runfiles;
|
||||
|
||||
|
@ -32,6 +32,14 @@ pub enum Resource {
|
|||
File(std::io::BufReader<std::fs::File>),
|
||||
}
|
||||
|
||||
impl Resource {
|
||||
pub fn string(&mut self) -> std::io::Result<String> {
|
||||
let mut contents = String::new();
|
||||
self.read_to_string(&mut contents);
|
||||
Ok(contents)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::io::Read for Resource {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
|
@ -61,8 +69,11 @@ impl std::io::Seek for Resource {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn resource(name: String) -> Result<Resource>
|
||||
pub fn resource<T>(name: T) -> Result<Resource>
|
||||
where
|
||||
T: Into<String>
|
||||
{
|
||||
let name: String = name.into();
|
||||
// Ensure name has //-prefix.
|
||||
let rel = name.strip_prefix("//").ok_or(ResourceError::InvalidPath)?;
|
||||
// Ensure no / prefix or suffix.
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::collections::BTreeMap;
|
||||
use std::marker::PhantomData;
|
||||
use std::iter::Iterator;
|
||||
use std::cell::RefCell;
|
||||
|
||||
use crate::componentmap::{
|
||||
AccessError,
|
||||
|
@ -147,6 +148,7 @@ pub struct World {
|
|||
components: BTreeMap<component::ID, ComponentMap>,
|
||||
component_by_idstr: BTreeMap<&'static str, component::ID>,
|
||||
component_lua_bindings: BTreeMap<component::ID, Box<dyn component::LuaBindings>>,
|
||||
component_queue: RefCell<Vec<(component::ID, Box<dyn component::Component>, entity::Entity)>>,
|
||||
globals: GlobalMap,
|
||||
next_id: entity::ID,
|
||||
}
|
||||
|
@ -157,6 +159,7 @@ impl World {
|
|||
components: BTreeMap::new(),
|
||||
component_by_idstr: BTreeMap::new(),
|
||||
component_lua_bindings: BTreeMap::new(),
|
||||
component_queue: RefCell::new(Vec::new()),
|
||||
globals: GlobalMap::new(),
|
||||
next_id: 1u64,
|
||||
}
|
||||
|
@ -190,6 +193,23 @@ impl World {
|
|||
map.insert(e.id(), c).unwrap();
|
||||
}
|
||||
|
||||
pub fn enqueue_register_component_entity(
|
||||
&self,
|
||||
cid: component::ID,
|
||||
c: Box<dyn component::Component>,
|
||||
e: entity::Entity
|
||||
) {
|
||||
self.component_queue.borrow_mut().push((cid, c, e));
|
||||
}
|
||||
|
||||
pub fn queue_drain(
|
||||
&mut self,
|
||||
) {
|
||||
for (cid, c, e) in self.component_queue.replace(Vec::new()).into_iter() {
|
||||
self.register_component_entity(cid, c, e);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn components<'a, T: component::Component>(&'a self) -> ReadComponent<'a, T> {
|
||||
ReadComponent {
|
||||
world: self,
|
||||
|
|
Loading…
Reference in New Issue