1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00
azalea/azalea-brigadier/src/tree/mod.rs
mat a5672815cc
Use an ECS (#52)
* add EntityData::kind

* start making metadata use hecs

* make entity codegen generate ecs stuff

* fix registry codegen

* get rid of worldhaver

it's not even used

* add bevy_ecs to deps

* rename Component to FormattedText

also start making the metadata use bevy_ecs but bevy_ecs doesn't let you query on Bundles so it's annoying

* generate metadata.rs correctly for bevy_ecs

* start switching more entity stuff to use ecs

* more ecs stuff for entity storage

* ok well it compiles but

it definitely doesn't work

* random fixes

* change a bunch of entity things to use the components

* some ecs stuff in az-client

* packet handler uses the ecs now

and other fun changes

i still need to make ticking use the ecs but that's tricker, i'm considering using bevy_ecs systems for those

bevy_ecs systems can't be async but the only async things in ticking is just sending packets which can just be done as a tokio task so that's not a big deal

* start converting some functions in az-client into systems

committing because i'm about to try something that might go horribly wrong

* start splitting client

i'm probably gonna change it so azalea entity ids are separate from minecraft entity ids next (so stuff like player ids can be consistent and we don't have to wait for the login packet)

* separate minecraft entity ids from azalea entity ids + more ecs stuff

i guess i'm using bevy_app now too huh
it's necessary for plugins and it lets us control the tick rate anyways so it's fine i think

i'm still not 100% sure how packet handling that interacts with the world will work, but i think if i can sneak the ecs world into there it'll be fine. Can't put packet handling in the schedule because that'd make it tick-bound, which it's not (technically it'd still work but it'd be wrong and anticheats might realize).

* packet handling

now it runs the schedule only when we get a tick or packet 😄

also i systemified some more functions and did other random fixes so az-world and az-physics compile

making azalea-client use the ecs is almost done! all the hard parts are done now i hope, i just have to finish writing all the code so it actually works

* start figuring out how functions in Client will work

generally just lifetimes being annoying but i think i can get it all to work

* make writing packets work synchronously*

* huh az-client compiles

* start fixing stuff

* start fixing some packets

* make packet handler work

i still haven't actually tested any of this yet lol but in theory it should all work

i'll probably either actually test az-client and fix all the remaining issues or update the azalea crate next

ok also one thing that i'm not particularly happy with is how the packet handlers are doing ugly queries like
```rs
let local_player = ecs
    .query::<&LocalPlayer>()
    .get_mut(ecs, player_entity)
    .unwrap();
```
i think the right way to solve it would be by putting every packet handler in its own system but i haven't come up with a way to make that not be really annoying yet

* fix warnings

* ok what if i just have a bunch of queries and a single packet handler system

* simple example for azalea-client

* 🐛

* maybe fix deadlock idk

can't test it rn lmao

* make physicsstate its own component

* use the default plugins

* azalea compiles lol

* use systemstate for packet handler

* fix entities

basically moved some stuff from being in the world to just being components

* physics (ticking) works

* try to add a .entity_by function

still doesn't work because i want to make the predicate magic

* try to make entity_by work

well it does work but i couldn't figure out how to make it look not terrible. Will hopefully change in the future

* everything compiles

* start converting swarm to use builder

* continue switching swarm to builder and fix stuff

* make swarm use builder

still have to fix some stuff and make client use builder

* fix death event

* client builder

* fix some warnings

* document plugins a bit

* start trying to fix tests

* azalea-ecs

* azalea-ecs stuff compiles

* az-physics tests pass 🎉

* fix all the tests

* clippy on azalea-ecs-macros

* remove now-unnecessary trait_upcasting feature

* fix some clippy::pedantic warnings lol

* why did cargo fmt not remove the trailing spaces

* FIX ALL THE THINGS

* when i said 'all' i meant non-swarm bugs

* start adding task pool

* fix entity deduplication

* fix pathfinder not stopping

* fix some more random bugs

* fix panic that sometimes happens in swarms

* make pathfinder run in task

* fix some tests

* fix doctests and clippy

* deadlock

* fix systems running in wrong order

* fix non-swarm bots
2023-02-04 19:32:27 -06:00

263 lines
8.8 KiB
Rust
Executable file

use crate::{
builder::{
argument_builder::ArgumentBuilderType, literal_argument_builder::Literal,
required_argument_builder::Argument,
},
context::{CommandContext, CommandContextBuilder, ParsedArgument, StringRange},
exceptions::{BuiltInExceptions, CommandSyntaxException},
modifier::RedirectModifier,
string_reader::StringReader,
};
use std::{cell::RefCell, collections::HashMap, fmt::Debug, hash::Hash, ptr, rc::Rc};
pub type Command<S> = Option<Rc<dyn Fn(&CommandContext<S>) -> i32>>;
/// An ArgumentBuilder that has been built.
#[non_exhaustive]
pub struct CommandNode<S> {
pub value: ArgumentBuilderType,
pub children: HashMap<String, Rc<RefCell<CommandNode<S>>>>,
pub literals: HashMap<String, Rc<RefCell<CommandNode<S>>>>,
pub arguments: HashMap<String, Rc<RefCell<CommandNode<S>>>>,
pub command: Command<S>,
pub requirement: Rc<dyn Fn(Rc<S>) -> bool>,
pub redirect: Option<Rc<RefCell<CommandNode<S>>>>,
pub forks: bool,
pub modifier: Option<Rc<RedirectModifier<S>>>,
}
impl<S> Clone for CommandNode<S> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
children: self.children.clone(),
literals: self.literals.clone(),
arguments: self.arguments.clone(),
command: self.command.clone(),
requirement: self.requirement.clone(),
redirect: self.redirect.clone(),
forks: self.forks,
modifier: self.modifier.clone(),
}
}
}
impl<S> CommandNode<S> {
/// Gets the literal, or panics. You should use match if you're not certain
/// about the type.
pub fn literal(&self) -> &Literal {
match self.value {
ArgumentBuilderType::Literal(ref literal) => literal,
_ => panic!("CommandNode::literal() called on non-literal node"),
}
}
/// Gets the argument, or panics. You should use match if you're not certain
/// about the type.
pub fn argument(&self) -> &Argument {
match self.value {
ArgumentBuilderType::Argument(ref argument) => argument,
_ => panic!("CommandNode::argument() called on non-argument node"),
}
}
pub fn get_relevant_nodes(&self, input: &mut StringReader) -> Vec<Rc<RefCell<CommandNode<S>>>> {
let literals = &self.literals;
if literals.is_empty() {
self.arguments.values().cloned().collect()
} else {
let cursor = input.cursor();
while input.can_read() && input.peek() != ' ' {
input.skip();
}
let text: String = input
.string()
.chars()
.skip(cursor)
.take(input.cursor() - cursor)
.collect();
input.cursor = cursor;
let literal = literals.get(&text);
if let Some(literal) = literal {
vec![literal.clone()]
} else {
self.arguments.values().cloned().collect()
}
}
}
pub fn can_use(&self, source: Rc<S>) -> bool {
(self.requirement)(source)
}
pub fn add_child(&mut self, node: &Rc<RefCell<CommandNode<S>>>) {
let child = self.children.get(node.borrow().name());
if let Some(child) = child {
// We've found something to merge onto
if let Some(command) = &node.borrow().command {
child.borrow_mut().command = Some(command.clone());
}
for grandchild in node.borrow().children.values() {
child.borrow_mut().add_child(grandchild);
}
} else {
self.children
.insert(node.borrow().name().to_string(), node.clone());
match &node.borrow().value {
ArgumentBuilderType::Literal(literal) => {
self.literals.insert(literal.value.clone(), node.clone());
}
ArgumentBuilderType::Argument(argument) => {
self.arguments.insert(argument.name.clone(), node.clone());
}
}
}
}
pub fn name(&self) -> &str {
match &self.value {
ArgumentBuilderType::Argument(argument) => &argument.name,
ArgumentBuilderType::Literal(literal) => &literal.value,
}
}
pub fn child(&self, name: &str) -> Option<Rc<RefCell<CommandNode<S>>>> {
self.children.get(name).cloned()
}
pub fn parse_with_context(
&self,
reader: &mut StringReader,
context_builder: &mut CommandContextBuilder<S>,
) -> Result<(), CommandSyntaxException> {
match self.value {
ArgumentBuilderType::Argument(ref argument) => {
let start = reader.cursor();
let result = argument.parse(reader)?;
let parsed = ParsedArgument {
range: StringRange::between(start, reader.cursor()),
result,
};
context_builder.with_argument(&argument.name, parsed.clone());
context_builder.with_node(Rc::new(RefCell::new(self.clone())), parsed.range);
Ok(())
}
ArgumentBuilderType::Literal(ref literal) => {
let start = reader.cursor();
let end = self.parse(reader);
if let Some(end) = end {
context_builder.with_node(
Rc::new(RefCell::new(self.clone())),
StringRange::between(start, end),
);
return Ok(());
}
Err(BuiltInExceptions::LiteralIncorrect {
expected: literal.value.clone(),
}
.create_with_context(reader))
}
}
}
fn parse(&self, reader: &mut StringReader) -> Option<usize> {
match self.value {
ArgumentBuilderType::Argument(_) => {
panic!("Can't parse argument.")
}
ArgumentBuilderType::Literal(ref literal) => {
let start = reader.cursor();
if reader.can_read_length(literal.value.len()) {
let end = start + literal.value.len();
if reader
.string()
.get(start..end)
.expect("Couldn't slice reader correctly?")
== literal.value
{
reader.cursor = end;
if !reader.can_read() || reader.peek() == ' ' {
return Some(end);
} else {
reader.cursor = start;
}
}
}
}
}
None
}
}
impl<S> Debug for CommandNode<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommandNode")
.field("value", &self.value)
.field("children", &self.children)
.field("command", &self.command.is_some())
// .field("requirement", &self.requirement)
.field("redirect", &self.redirect)
.field("forks", &self.forks)
// .field("modifier", &self.modifier)
.finish()
}
}
impl<S> Default for CommandNode<S> {
fn default() -> Self {
Self {
value: ArgumentBuilderType::Literal(Literal::default()),
children: HashMap::new(),
literals: HashMap::new(),
arguments: HashMap::new(),
command: None,
requirement: Rc::new(|_| true),
redirect: None,
forks: false,
modifier: None,
}
}
}
impl<S> Hash for CommandNode<S> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
// hash the children
for (k, v) in &self.children {
k.hash(state);
v.borrow().hash(state);
}
// i hope this works because if doesn't then that'll be a problem
ptr::hash(&self.command, state);
}
}
impl<S> PartialEq for CommandNode<S> {
fn eq(&self, other: &Self) -> bool {
if self.children != other.children {
return false;
}
if let Some(selfexecutes) = &self.command {
// idk how to do this better since we can't compare `dyn Fn`s
if let Some(otherexecutes) = &other.command {
#[allow(clippy::vtable_address_comparisons)]
if !Rc::ptr_eq(selfexecutes, otherexecutes) {
return false;
}
} else {
return false;
}
} else if other.command.is_some() {
return false;
}
true
}
}
impl<S> Eq for CommandNode<S> {}