Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Update to 0.9.1 #38

Merged
merged 11 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bevy_mod_scripting_rhai = { path = "languages/bevy_mod_scripting_rhai", version
bevy_script_api = { path = "bevy_script_api", version = "0.1.1", optional = true }

[dev-dependencies]
bevy = { version = "0.8.0"}
bevy = { version = "0.9.1"}
rand = "0.8.5"


Expand All @@ -62,7 +62,7 @@ rand = "0.8.5"
# bevy_mod_scripting_lua = {path="languages/bevy_mod_scripting_lua", version="0.1.1", features=["lua54"]}
# bevy_mod_scripting_lua_derive = {path="languages/bevy_mod_scripting_lua_derive", version="0.1.1"}

# bevy = { version = "0.8.0"}
# bevy = { version = "0.9.1"}
# serde = "1.0.137"
# criterion = "0.3"

Expand All @@ -89,7 +89,7 @@ opt-level = 1
opt-level = 3


# needs bevy 0.8 support from console
# needs bevy 0.9 support from console
# [[example]]
# name = "console_integration_lua"
# path = "examples/console_integration_lua.rs"
Expand Down
19 changes: 7 additions & 12 deletions api_gen_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{script_ref::{ReflectedValue,ValueIndex},
use std::sync::Mutex;
use bevy_mod_scripting_core::prelude::*;
use bevy::prelude::App;
use bevy::reflect::Enum;
"""

other = """
Expand Down Expand Up @@ -156,10 +157,6 @@ source="bevy_ui"
type="Style"
source="bevy_ui"

[[types]]
type="UiColor"
source="bevy_ui"

[[types]]
type="UiImage"
source="bevy_ui"
Expand Down Expand Up @@ -398,9 +395,7 @@ source="bevy_sprite"
type="Sprite"
source="bevy_sprite"

[[types]]
type="Rect"
source="bevy_sprite"


## BEVY_RENDER

Expand Down Expand Up @@ -486,10 +481,6 @@ traits=[
{name="CameraProjection", import_path="bevy::render::camera::CameraProjection"}
]

[[types]]
type="DepthCalculation"
source="bevy_render"

[[types]]
type="CameraRenderGraph"
source="bevy_render"
Expand Down Expand Up @@ -915,4 +906,8 @@ import_path="glam::f64::DQuat"
[[types]]
type="EulerRot"
source="bevy_math"
import_path="glam::EulerRot"
import_path="glam::EulerRot"

[[types]]
type="Rect"
source="bevy_math"
4 changes: 2 additions & 2 deletions assets/scripts/game_of_life.tl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
math.randomseed(os.time())

function init()
global function init()

local LifeState = world:get_type_by_name("LifeState")
local life_state = world:get_component(entity,LifeState) as types.LuaLifeState
Expand All @@ -13,7 +13,7 @@ function init()
end
end

function on_update()
global function on_update()
local LifeState = world:get_type_by_name("LifeState")
local Settings = world:get_type_by_name("Settings")

Expand Down
2 changes: 1 addition & 1 deletion bevy_api_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "src/main.rs"


[dependencies]
rustdoc-types = "0.11.0"
rustdoc-types = "0.19.0"
clap = { version = "3.2.6", features = ["derive"] }
serde_json = "1.0.81"
toml = "0.5.9"
Expand Down
8 changes: 4 additions & 4 deletions bevy_api_gen/src/arg_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ impl TryFrom<&Type> for ArgType {

fn try_from(value: &Type) -> Result<Self, Self::Error> {
match value {
Type::ResolvedPath { name, args, .. } => {
Type::ResolvedPath(path) => {
let mut processed_args = Vec::default();

for a in args {
for a in &path.args {
if let GenericArgs::AngleBracketed { args, bindings } = a.as_ref() {
for generic in args {
match generic {
Expand All @@ -89,7 +89,7 @@ impl TryFrom<&Type> for ArgType {
return Err("Parenthesised generics are not supported".to_owned());
}
}
let base = Type::Primitive(name.to_string()).try_into()?;
let base = Type::Primitive(path.name.to_string()).try_into()?;
if let base @ ArgType::Base(_) = base {
if !processed_args.is_empty() {
Ok(Self::Generic {
Expand Down Expand Up @@ -174,7 +174,7 @@ impl ArgWrapperType {
let base_ident = type_.base_ident().unwrap_or(self_type);
type_
.is_self()
.then(|| ArgWrapperType::None)
.then_some(ArgWrapperType::None)
.or_else(|| {
config
.primitives
Expand Down
76 changes: 76 additions & 0 deletions bevy_api_gen/src/cratepath.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use rustdoc_types::{Crate, Id, ItemEnum, Visibility};

pub(crate) fn get_path(id: &Id, source: &Crate) -> Option<Vec<Id>> {
match source.paths.get(id) {
Some(_) => return Some(vec![id.to_owned()]),
None => {
let ind = source.index.get(id)?;
if let Visibility::Restricted { parent, .. } = &ind.visibility {
if let Some(p_path) = get_path(parent, source) {
return Some(p_path);
}
}
let parents = source.index.iter().filter(|(_, p_item)| {
if let Some(name) = &ind.name {
if p_item.links.contains_key(name) {
return true;
}
}
if let ItemEnum::Impl(p_impl) = &p_item.inner {
return p_impl.items.contains(id);
}
if let ItemEnum::Import(p_import) = &p_item.inner {
if let Some(p_inner) = &p_import.id {
return p_inner == id;
}
return false;
}
if let ItemEnum::Module(p_mod) = &p_item.inner {
return p_mod.items.contains(id);
}
false
});

for (parent, _) in parents {
let path_o = get_path(parent, source);
if let Some(mut path) = path_o {
path.push(id.to_owned());
return Some(path);
}
}
}
};
None
}

pub(crate) fn path_to_import(path: Vec<Id>, source: &Crate) -> Vec<String> {
path.iter()
.rev()
.enumerate()
.rev()
.enumerate()
.map(|(starti, (endi, id))| {
let ind = source.index.get(id).unwrap();
if starti == 0 {
return source.paths.get(id).unwrap().path.clone();
} else if endi == 0 {
if let Some(name) = &ind.name {
return vec![name.to_owned()];
}
} else if let Visibility::Restricted { parent: _, path } = &ind.visibility {
return path[2..].split("::").map(|x| x.to_string()).collect();
} else if let ItemEnum::Module(module) = &ind.inner {
if !module.is_stripped {
return vec![source.index.get(id).unwrap().name.clone().unwrap()];
} else {
return vec![];
}
}
vec![]
})
.reduce(|mut x, y| {
x.extend(y);
x
})
.unwrap()
}
7 changes: 1 addition & 6 deletions bevy_api_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ pub use {arg_validator::*, config::*, wrapper::*, writer::*};
/// Currently only used for stringifying simple trait names
pub fn stringify_type(type_: &Type) -> Option<String> {
match type_ {
Type::ResolvedPath {
name,
id: _,
args: _,
param_names: _,
} => Some(name.to_owned()),
Type::ResolvedPath(path) => Some(path.name.to_owned()),
Type::Generic(s) | Type::Primitive(s) => Some(s.to_owned()),
Type::QualifiedPath {
name,
Expand Down
20 changes: 15 additions & 5 deletions bevy_api_gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use bevy_api_gen_lib::{stringify_type, Args, Config, PrettyWriter, WrappedItem, WRAPPER_PREFIX};
pub mod cratepath;

use bevy_api_gen_lib::{Args, Config, PrettyWriter, WrappedItem, WRAPPER_PREFIX};

use clap::Parser;
use cratepath::{get_path, path_to_import};
use indexmap::{IndexMap, IndexSet};
use rustdoc_types::{Crate, Impl, Item, ItemEnum};
use serde_json::from_reader;
use std::{
borrow::Cow,
collections::HashSet,
fs::{read_to_string, File},
io::{self, BufReader},
Expand Down Expand Up @@ -70,7 +74,7 @@ pub(crate) fn generate_macros(
if let ItemEnum::Impl(i) = &source.index.get(id).unwrap().inner {
match &i.trait_ {
Some(t) => {
stringify_type(t).map(|str_| implemented_traits.insert(str_));
implemented_traits.insert(t.name.to_owned());
}
None => self_impl = Some(i),
}
Expand All @@ -89,14 +93,20 @@ pub(crate) fn generate_macros(

let config = config.types.get(item.name.as_ref().unwrap()).unwrap();

let path_components = &source.paths.get(id).unwrap().path;
//let path_components = &source.paths.get(id).unwrap().path;
let path_components = get_path(id, source).unwrap_or_else(|| {
panic!("path not found for {:?} in {:?}", id, source.root)
});
//eprintln!("{:?}", path_components);
let path_components = path_to_import(path_components, source);
//eprintln!("{:?}", path_components);

let wrapper_name = format!("{WRAPPER_PREFIX}{}", item.name.as_ref().unwrap());
let wrapped_type = item.name.as_ref().unwrap();
WrappedItem {
wrapper_name,
wrapped_type,
path_components,
path_components: Cow::Owned(path_components),
source,
config,
item,
Expand Down Expand Up @@ -333,7 +343,7 @@ pub fn main() -> Result<(), io::Error> {
.json
.iter()
.map(|json| {
let f = File::open(&json).unwrap_or_else(|_| panic!("Could not open {}", &json));
let f = File::open(json).unwrap_or_else(|_| panic!("Could not open {}", &json));
let rdr = BufReader::new(f);
from_reader(rdr)
})
Expand Down
Loading