only prefix commands with '$' that actually use macros
This commit is contained in:
parent
8f05fef703
commit
588bc3f464
|
@ -7,7 +7,7 @@ use std::{
|
||||||
use crate::{
|
use crate::{
|
||||||
prelude::Command,
|
prelude::Command,
|
||||||
util::{
|
util::{
|
||||||
compile::{CompileOptions, FunctionCompilerState, MutCompilerState},
|
compile::{CompileOptions, CompiledCommand, FunctionCompilerState, MutCompilerState},
|
||||||
MacroString,
|
MacroString,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -16,22 +16,25 @@ use super::Execute;
|
||||||
|
|
||||||
/// Compile an if condition command.
|
/// Compile an if condition command.
|
||||||
/// The first tuple element is a boolean indicating if the prefix should be used for that command.
|
/// The first tuple element is a boolean indicating if the prefix should be used for that command.
|
||||||
|
#[expect(clippy::too_many_arguments)]
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
pub fn compile_if_cond(
|
pub fn compile_if_cond(
|
||||||
cond: &Condition,
|
cond: &Condition,
|
||||||
then: &Execute,
|
then: &Execute,
|
||||||
el: Option<&Execute>,
|
el: Option<&Execute>,
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
|
prefix_contains_macros: bool,
|
||||||
options: &CompileOptions,
|
options: &CompileOptions,
|
||||||
global_state: &MutCompilerState,
|
global_state: &MutCompilerState,
|
||||||
function_state: &FunctionCompilerState,
|
function_state: &FunctionCompilerState,
|
||||||
) -> Vec<(bool, String)> {
|
) -> Vec<CompiledCommand> {
|
||||||
if options.pack_format < 20 {
|
if options.pack_format < 20 {
|
||||||
compile_pre_20_format(
|
compile_pre_20_format(
|
||||||
cond,
|
cond,
|
||||||
then,
|
then,
|
||||||
el,
|
el,
|
||||||
prefix,
|
prefix,
|
||||||
|
prefix_contains_macros,
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
|
@ -42,6 +45,7 @@ pub fn compile_if_cond(
|
||||||
then,
|
then,
|
||||||
el,
|
el,
|
||||||
prefix,
|
prefix,
|
||||||
|
prefix_contains_macros,
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
|
@ -49,16 +53,18 @@ pub fn compile_if_cond(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_lines)]
|
#[expect(clippy::too_many_lines, clippy::too_many_arguments)]
|
||||||
fn compile_pre_20_format(
|
fn compile_pre_20_format(
|
||||||
cond: &Condition,
|
cond: &Condition,
|
||||||
then: &Execute,
|
then: &Execute,
|
||||||
el: Option<&Execute>,
|
el: Option<&Execute>,
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
|
prefix_contains_macros: bool,
|
||||||
options: &CompileOptions,
|
options: &CompileOptions,
|
||||||
global_state: &MutCompilerState,
|
global_state: &MutCompilerState,
|
||||||
function_state: &FunctionCompilerState,
|
function_state: &FunctionCompilerState,
|
||||||
) -> Vec<(bool, String)> {
|
) -> Vec<CompiledCommand> {
|
||||||
|
let contains_macro = prefix_contains_macros || cond.contains_macro();
|
||||||
let then_count = then.get_count(options);
|
let then_count = then.get_count(options);
|
||||||
|
|
||||||
let str_cond = cond.clone().compile(options, global_state, function_state);
|
let str_cond = cond.clone().compile(options, global_state, function_state);
|
||||||
|
@ -93,14 +99,21 @@ fn compile_pre_20_format(
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
if allows_prefix {
|
if allows_prefix {
|
||||||
(true, "run ".to_string() + s)
|
s.clone().apply_prefix("run ")
|
||||||
} else {
|
} else {
|
||||||
(false, s.clone())
|
s.clone()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
then.compile_internal(String::new(), false, options, global_state, function_state)
|
then.compile_internal(
|
||||||
|
String::new(),
|
||||||
|
false,
|
||||||
|
contains_macro,
|
||||||
|
options,
|
||||||
|
global_state,
|
||||||
|
function_state,
|
||||||
|
)
|
||||||
};
|
};
|
||||||
// if the conditions have multiple parts joined by a disjunction, commands need to be grouped
|
// if the conditions have multiple parts joined by a disjunction, commands need to be grouped
|
||||||
let each_or_cmd = (str_cond.len() > 1).then(|| {
|
let each_or_cmd = (str_cond.len() > 1).then(|| {
|
||||||
|
@ -112,10 +125,10 @@ fn compile_pre_20_format(
|
||||||
format!("data modify storage shulkerbox:cond {success_uid} set value true"),
|
format!("data modify storage shulkerbox:cond {success_uid} set value true"),
|
||||||
combine_conditions_commands(
|
combine_conditions_commands(
|
||||||
str_cond.clone(),
|
str_cond.clone(),
|
||||||
&[(
|
&[CompiledCommand::new(format!(
|
||||||
true,
|
"run data modify storage shulkerbox:cond {success_uid} set value true"
|
||||||
format!("run data modify storage shulkerbox:cond {success_uid} set value true"),
|
))
|
||||||
)],
|
.or_contains_macros(contains_macro)],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
@ -148,6 +161,7 @@ fn compile_pre_20_format(
|
||||||
let el = el.compile_internal(
|
let el = el.compile_internal(
|
||||||
String::new(),
|
String::new(),
|
||||||
else_cond.len() > 1,
|
else_cond.len() > 1,
|
||||||
|
contains_macro,
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
|
@ -162,10 +176,10 @@ fn compile_pre_20_format(
|
||||||
tracing::error!("No success_uid found for each_or_cmd, using default");
|
tracing::error!("No success_uid found for each_or_cmd, using default");
|
||||||
"if_success"
|
"if_success"
|
||||||
});
|
});
|
||||||
Some((
|
Some(
|
||||||
false,
|
CompiledCommand::new(format!("data remove storage shulkerbox:cond {success_uid}"))
|
||||||
format!("data remove storage shulkerbox:cond {success_uid}"),
|
.with_forbid_prefix(true),
|
||||||
))
|
)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
@ -178,26 +192,22 @@ fn compile_pre_20_format(
|
||||||
.chain(then_commands)
|
.chain(then_commands)
|
||||||
.chain(el_commands)
|
.chain(el_commands)
|
||||||
.chain(reset_success_storage)
|
.chain(reset_success_storage)
|
||||||
.map(|(use_prefix, cmd)| {
|
.map(|cmd| cmd.apply_prefix(prefix))
|
||||||
let cmd = if use_prefix {
|
|
||||||
prefix.to_string() + &cmd
|
|
||||||
} else {
|
|
||||||
cmd
|
|
||||||
};
|
|
||||||
(use_prefix, cmd)
|
|
||||||
})
|
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[expect(clippy::too_many_arguments)]
|
||||||
fn compile_since_20_format(
|
fn compile_since_20_format(
|
||||||
cond: &Condition,
|
cond: &Condition,
|
||||||
then: &Execute,
|
then: &Execute,
|
||||||
el: Option<&Execute>,
|
el: Option<&Execute>,
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
|
prefix_contains_macros: bool,
|
||||||
options: &CompileOptions,
|
options: &CompileOptions,
|
||||||
global_state: &MutCompilerState,
|
global_state: &MutCompilerState,
|
||||||
function_state: &FunctionCompilerState,
|
function_state: &FunctionCompilerState,
|
||||||
) -> Vec<(bool, String)> {
|
) -> Vec<CompiledCommand> {
|
||||||
|
let contains_macros = prefix_contains_macros || cond.contains_macro();
|
||||||
let then_count = then.get_count(options);
|
let then_count = then.get_count(options);
|
||||||
|
|
||||||
let str_cond = cond
|
let str_cond = cond
|
||||||
|
@ -216,12 +226,14 @@ fn compile_since_20_format(
|
||||||
function_state,
|
function_state,
|
||||||
);
|
);
|
||||||
let group = Command::Group(group_cmds);
|
let group = Command::Group(group_cmds);
|
||||||
let allows_prefix = !group.forbid_prefix();
|
let cmds = group.compile(options, global_state, function_state);
|
||||||
group
|
if contains_macros {
|
||||||
.compile(options, global_state, function_state)
|
cmds.into_iter()
|
||||||
.into_iter()
|
.map(|cmd| cmd.or_contains_macros(true))
|
||||||
.map(|s| (allows_prefix, s))
|
.collect()
|
||||||
.collect()
|
} else {
|
||||||
|
cmds
|
||||||
|
}
|
||||||
} else if then_count > 1 {
|
} else if then_count > 1 {
|
||||||
let then_cmd = match then.clone() {
|
let then_cmd = match then.clone() {
|
||||||
Execute::Run(cmd) => vec![*cmd],
|
Execute::Run(cmd) => vec![*cmd],
|
||||||
|
@ -239,30 +251,30 @@ fn compile_since_20_format(
|
||||||
};
|
};
|
||||||
combine_conditions_commands_concat(str_cond, &then_cmd)
|
combine_conditions_commands_concat(str_cond, &then_cmd)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|cmd| {
|
.flat_map(|cmd| {
|
||||||
(
|
cmd.compile(options, global_state, function_state)
|
||||||
cmd.forbid_prefix(),
|
.into_iter()
|
||||||
cmd.compile(options, global_state, function_state),
|
.map(move |compiled_cmd| {
|
||||||
)
|
compiled_cmd
|
||||||
})
|
.apply_prefix(prefix)
|
||||||
.flat_map(|(forbid_prefix, cmds)| {
|
.or_contains_macros(contains_macros)
|
||||||
cmds.into_iter()
|
})
|
||||||
.map(move |cmd| (!forbid_prefix, prefix.to_string() + &cmd))
|
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
str_cond
|
str_cond
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|cond| {
|
.flat_map(|cond| {
|
||||||
then.compile_internal(String::new(), false, options, global_state, function_state)
|
then.compile_internal(
|
||||||
.into_iter()
|
String::new(),
|
||||||
.map(move |(require_prefix, cmd)| {
|
false,
|
||||||
if require_prefix {
|
contains_macros,
|
||||||
(true, prefix.to_string() + &cond.compile() + " " + &cmd)
|
options,
|
||||||
} else {
|
global_state,
|
||||||
(false, cmd)
|
function_state,
|
||||||
}
|
)
|
||||||
})
|
.into_iter()
|
||||||
|
.map(move |cmd| cmd.apply_prefix(prefix.to_string() + &cond.compile() + " "))
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
@ -270,19 +282,15 @@ fn compile_since_20_format(
|
||||||
|
|
||||||
fn combine_conditions_commands(
|
fn combine_conditions_commands(
|
||||||
conditions: Vec<String>,
|
conditions: Vec<String>,
|
||||||
commands: &[(bool, String)],
|
commands: &[CompiledCommand],
|
||||||
) -> Vec<(bool, String)> {
|
) -> Vec<CompiledCommand> {
|
||||||
conditions
|
conditions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|cond| {
|
.flat_map(|cond| {
|
||||||
commands.iter().map(move |(use_prefix, cmd)| {
|
let prefix = cond + " ";
|
||||||
|
commands.iter().map(move |cmd| {
|
||||||
// combine the condition with the command if it uses a prefix
|
// combine the condition with the command if it uses a prefix
|
||||||
let cmd = if *use_prefix {
|
cmd.clone().apply_prefix(&prefix)
|
||||||
cond.clone() + " " + cmd
|
|
||||||
} else {
|
|
||||||
cmd.clone()
|
|
||||||
};
|
|
||||||
(*use_prefix, cmd)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
|
@ -652,18 +660,21 @@ mod tests {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(str::to_string)
|
.map(str::to_string)
|
||||||
.collect();
|
.collect();
|
||||||
let commands = &[(true, "1".to_string()), (false, "2".to_string())];
|
let commands = &[
|
||||||
|
CompiledCommand::new("1".to_string()),
|
||||||
|
CompiledCommand::new("2".to_string()).with_forbid_prefix(true),
|
||||||
|
];
|
||||||
|
|
||||||
let combined = combine_conditions_commands(conditions, commands);
|
let combined = combine_conditions_commands(conditions, commands);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
combined,
|
combined,
|
||||||
vec![
|
vec![
|
||||||
(true, "a 1".to_string()),
|
CompiledCommand::new("a 1".to_string()),
|
||||||
(false, "2".to_string()),
|
CompiledCommand::new("2".to_string()).with_forbid_prefix(true),
|
||||||
(true, "b 1".to_string()),
|
CompiledCommand::new("b 1".to_string()),
|
||||||
(false, "2".to_string()),
|
CompiledCommand::new("2".to_string()).with_forbid_prefix(true),
|
||||||
(true, "c 1".to_string()),
|
CompiledCommand::new("c 1".to_string()),
|
||||||
(false, "2".to_string())
|
CompiledCommand::new("2".to_string()).with_forbid_prefix(true)
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::{collections::HashSet, ops::RangeInclusive, string::ToString};
|
||||||
|
|
||||||
use super::Command;
|
use super::Command;
|
||||||
use crate::util::{
|
use crate::util::{
|
||||||
compile::{CompileOptions, FunctionCompilerState, MutCompilerState},
|
compile::{CompileOptions, CompiledCommand, FunctionCompilerState, MutCompilerState},
|
||||||
ExtendableQueue, MacroString,
|
ExtendableQueue, MacroString,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ impl Execute {
|
||||||
options: &CompileOptions,
|
options: &CompileOptions,
|
||||||
global_state: &MutCompilerState,
|
global_state: &MutCompilerState,
|
||||||
function_state: &FunctionCompilerState,
|
function_state: &FunctionCompilerState,
|
||||||
) -> Vec<String> {
|
) -> Vec<CompiledCommand> {
|
||||||
// Directly compile the command if it is a run command, skipping the execute part
|
// Directly compile the command if it is a run command, skipping the execute part
|
||||||
// Otherwise, compile the execute command using internal function
|
// Otherwise, compile the execute command using internal function
|
||||||
match self {
|
match self {
|
||||||
|
@ -48,30 +48,29 @@ impl Execute {
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|c| c.compile(options, global_state, function_state))
|
.flat_map(|c| c.compile(options, global_state, function_state))
|
||||||
.collect(),
|
.collect(),
|
||||||
_ => self
|
_ => self.compile_internal(
|
||||||
.compile_internal(
|
String::from("execute "),
|
||||||
String::from("execute "),
|
false,
|
||||||
false,
|
false,
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
)
|
),
|
||||||
.into_iter()
|
|
||||||
.map(|(_, cmd)| cmd)
|
|
||||||
.collect(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile the execute command into strings with the given prefix.
|
/// Compile the execute command into strings with the given prefix.
|
||||||
/// Each first tuple element is a boolean indicating if the prefix should be used for that command.
|
/// Each first tuple element is a boolean indicating if the prefix should be used for that command.
|
||||||
|
#[expect(clippy::too_many_lines)]
|
||||||
fn compile_internal(
|
fn compile_internal(
|
||||||
&self,
|
&self,
|
||||||
prefix: String,
|
prefix: String,
|
||||||
require_grouping: bool,
|
require_grouping: bool,
|
||||||
|
prefix_contains_macros: bool,
|
||||||
options: &CompileOptions,
|
options: &CompileOptions,
|
||||||
global_state: &MutCompilerState,
|
global_state: &MutCompilerState,
|
||||||
function_state: &FunctionCompilerState,
|
function_state: &FunctionCompilerState,
|
||||||
) -> Vec<(bool, String)> {
|
) -> Vec<CompiledCommand> {
|
||||||
match self {
|
match self {
|
||||||
Self::Align(arg, next)
|
Self::Align(arg, next)
|
||||||
| Self::Anchored(arg, next)
|
| Self::Anchored(arg, next)
|
||||||
|
@ -89,6 +88,7 @@ impl Execute {
|
||||||
arg = arg.compile()
|
arg = arg.compile()
|
||||||
),
|
),
|
||||||
require_grouping,
|
require_grouping,
|
||||||
|
prefix_contains_macros || arg.contains_macro(),
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
|
@ -99,6 +99,7 @@ impl Execute {
|
||||||
selector = selector.compile()
|
selector = selector.compile()
|
||||||
),
|
),
|
||||||
require_grouping,
|
require_grouping,
|
||||||
|
prefix_contains_macros || selector.contains_macro(),
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
|
@ -108,6 +109,7 @@ impl Execute {
|
||||||
then.as_ref(),
|
then.as_ref(),
|
||||||
el.as_deref(),
|
el.as_deref(),
|
||||||
&prefix,
|
&prefix,
|
||||||
|
prefix_contains_macros,
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
|
@ -119,6 +121,7 @@ impl Execute {
|
||||||
arg = arg.compile()
|
arg = arg.compile()
|
||||||
),
|
),
|
||||||
true,
|
true,
|
||||||
|
prefix_contains_macros || arg.contains_macro(),
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
|
@ -127,6 +130,7 @@ impl Execute {
|
||||||
Command::Execute(ex) => ex.compile_internal(
|
Command::Execute(ex) => ex.compile_internal(
|
||||||
prefix,
|
prefix,
|
||||||
require_grouping,
|
require_grouping,
|
||||||
|
prefix_contains_macros,
|
||||||
options,
|
options,
|
||||||
global_state,
|
global_state,
|
||||||
function_state,
|
function_state,
|
||||||
|
@ -134,36 +138,38 @@ impl Execute {
|
||||||
command => command
|
command => command
|
||||||
.compile(options, global_state, function_state)
|
.compile(options, global_state, function_state)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|c| map_run_cmd(command.forbid_prefix(), c, &prefix))
|
.map(|c| {
|
||||||
|
map_run_cmd(command.forbid_prefix(), c, &prefix)
|
||||||
|
.or_contains_macros(prefix_contains_macros)
|
||||||
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
},
|
||||||
Self::Runs(commands) if !require_grouping => commands
|
Self::Runs(commands) if !require_grouping => commands
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|c| {
|
.flat_map(|c| match c {
|
||||||
let forbid_prefix = c.forbid_prefix();
|
Command::Execute(ex) => ex.compile_internal(
|
||||||
match c {
|
prefix.clone(),
|
||||||
Command::Execute(ex) => ex.compile_internal(
|
require_grouping,
|
||||||
prefix.clone(),
|
prefix_contains_macros,
|
||||||
require_grouping,
|
options,
|
||||||
options,
|
global_state,
|
||||||
global_state,
|
function_state,
|
||||||
function_state,
|
),
|
||||||
),
|
command => command.compile(options, global_state, function_state),
|
||||||
command => command
|
})
|
||||||
.compile(options, global_state, function_state)
|
.map(|cmd| {
|
||||||
.into_iter()
|
map_run_cmd(false, cmd, &prefix).or_contains_macros(prefix_contains_macros)
|
||||||
.map(move |c| (!forbid_prefix, c))
|
|
||||||
.collect(),
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.map(|(require_prefix, c)| map_run_cmd(!require_prefix, c, &prefix))
|
|
||||||
.collect(),
|
.collect(),
|
||||||
Self::Runs(commands) => {
|
Self::Runs(commands) => {
|
||||||
let group = Command::Group(commands.clone());
|
let group = Command::Group(commands.clone());
|
||||||
group
|
group
|
||||||
.compile(options, global_state, function_state)
|
.compile(options, global_state, function_state)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|c| map_run_cmd(group.forbid_prefix(), c, &prefix))
|
.map(|c| {
|
||||||
|
map_run_cmd(group.forbid_prefix(), c, &prefix)
|
||||||
|
.or_contains_macros(prefix_contains_macros)
|
||||||
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,6 +185,7 @@ impl Execute {
|
||||||
self.compile_internal(
|
self.compile_internal(
|
||||||
String::new(),
|
String::new(),
|
||||||
false,
|
false,
|
||||||
|
false,
|
||||||
options,
|
options,
|
||||||
&global_state,
|
&global_state,
|
||||||
&function_state,
|
&function_state,
|
||||||
|
@ -313,12 +320,11 @@ where
|
||||||
|
|
||||||
/// Combine command parts, respecting if the second part is a comment
|
/// Combine command parts, respecting if the second part is a comment
|
||||||
/// The first tuple element is a boolean indicating if the prefix should be used
|
/// The first tuple element is a boolean indicating if the prefix should be used
|
||||||
fn map_run_cmd(forbid_prefix: bool, cmd: String, prefix: &str) -> (bool, String) {
|
fn map_run_cmd(forbid_prefix: bool, cmd: CompiledCommand, prefix: &str) -> CompiledCommand {
|
||||||
if forbid_prefix || cmd.is_empty() || cmd.chars().all(char::is_whitespace) {
|
let forbid_prefix =
|
||||||
(false, cmd)
|
forbid_prefix || cmd.as_str().is_empty() || cmd.as_str().chars().all(char::is_whitespace);
|
||||||
} else {
|
cmd.or_forbid_prefix(forbid_prefix)
|
||||||
(true, prefix.to_string() + "run " + &cmd)
|
.apply_prefix(prefix.to_string() + "run ")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -343,7 +349,9 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled,
|
||||||
vec!["execute as @a if block ~ ~-1 ~ minecraft:stone run say hi".to_string()]
|
vec![CompiledCommand::new(
|
||||||
|
"execute as @a if block ~ ~-1 ~ minecraft:stone run say hi"
|
||||||
|
)]
|
||||||
);
|
);
|
||||||
|
|
||||||
let direct = Execute::Run(Box::new("say direct".into())).compile(
|
let direct = Execute::Run(Box::new("say direct".into())).compile(
|
||||||
|
@ -352,6 +360,6 @@ mod tests {
|
||||||
&FunctionCompilerState::default(),
|
&FunctionCompilerState::default(),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(direct, vec!["say direct".to_string()]);
|
assert_eq!(direct, vec![CompiledCommand::new("say direct")]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ use super::Function;
|
||||||
use crate::{
|
use crate::{
|
||||||
prelude::Datapack,
|
prelude::Datapack,
|
||||||
util::{
|
util::{
|
||||||
compile::{CompileOptions, FunctionCompilerState, MutCompilerState},
|
compile::{CompileOptions, CompiledCommand, FunctionCompilerState, MutCompilerState},
|
||||||
MacroString,
|
MacroString,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -53,14 +53,18 @@ impl Command {
|
||||||
options: &CompileOptions,
|
options: &CompileOptions,
|
||||||
global_state: &MutCompilerState,
|
global_state: &MutCompilerState,
|
||||||
function_state: &FunctionCompilerState,
|
function_state: &FunctionCompilerState,
|
||||||
) -> Vec<String> {
|
) -> Vec<CompiledCommand> {
|
||||||
match self {
|
match self {
|
||||||
Self::Raw(command) => vec![command.clone()],
|
Self::Raw(command) => vec![CompiledCommand::new(command.clone())],
|
||||||
Self::UsesMacro(command) => vec![command.compile()],
|
Self::UsesMacro(command) => {
|
||||||
|
vec![CompiledCommand::new(command.compile()).with_contains_macros(true)]
|
||||||
|
}
|
||||||
Self::Debug(message) => compile_debug(message, options),
|
Self::Debug(message) => compile_debug(message, options),
|
||||||
Self::Execute(ex) => ex.compile(options, global_state, function_state),
|
Self::Execute(ex) => ex.compile(options, global_state, function_state),
|
||||||
Self::Group(commands) => compile_group(commands, options, global_state, function_state),
|
Self::Group(commands) => compile_group(commands, options, global_state, function_state),
|
||||||
Self::Comment(comment) => vec!["#".to_string() + comment],
|
Self::Comment(comment) => {
|
||||||
|
vec![CompiledCommand::new("#".to_string() + comment).with_forbid_prefix(true)]
|
||||||
|
}
|
||||||
Self::Concat(a, b) => {
|
Self::Concat(a, b) => {
|
||||||
let a = a.compile(options, global_state, function_state);
|
let a = a.compile(options, global_state, function_state);
|
||||||
let b = b.compile(options, global_state, function_state);
|
let b = b.compile(options, global_state, function_state);
|
||||||
|
@ -72,7 +76,9 @@ impl Command {
|
||||||
} else if b.is_empty() {
|
} else if b.is_empty() {
|
||||||
a.clone()
|
a.clone()
|
||||||
} else {
|
} else {
|
||||||
a.clone() + b
|
b.clone()
|
||||||
|
.apply_prefix(a.as_str())
|
||||||
|
.or_forbid_prefix(a.forbids_prefix())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -169,12 +175,12 @@ impl From<&mut Function> for Command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile_debug(message: &MacroString, option: &CompileOptions) -> Vec<String> {
|
fn compile_debug(message: &MacroString, option: &CompileOptions) -> Vec<CompiledCommand> {
|
||||||
if option.debug {
|
if option.debug {
|
||||||
vec![format!(
|
vec![CompiledCommand::new(format!(
|
||||||
r#"tellraw @a [{{"text":"[","color":"dark_blue"}},{{"text":"DEBUG","color":"dark_green","hoverEvent":{{"action":"show_text","value":[{{"text":"Debug message generated by Shulkerbox"}},{{"text":"\nSet debug to 'false' to disable"}}]}}}},{{"text":"] ","color":"dark_blue"}},{{"text":"{}","color":"black"}}]"#,
|
r#"tellraw @a [{{"text":"[","color":"dark_blue"}},{{"text":"DEBUG","color":"dark_green","hoverEvent":{{"action":"show_text","value":[{{"text":"Debug message generated by Shulkerbox"}},{{"text":"\nSet debug to 'false' to disable"}}]}}}},{{"text":"] ","color":"dark_blue"}},{{"text":"{}","color":"black"}}]"#,
|
||||||
message.compile()
|
message.compile()
|
||||||
)]
|
))]
|
||||||
} else {
|
} else {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
@ -186,51 +192,52 @@ fn compile_group(
|
||||||
options: &CompileOptions,
|
options: &CompileOptions,
|
||||||
global_state: &MutCompilerState,
|
global_state: &MutCompilerState,
|
||||||
function_state: &FunctionCompilerState,
|
function_state: &FunctionCompilerState,
|
||||||
) -> Vec<String> {
|
) -> Vec<CompiledCommand> {
|
||||||
let command_count = commands
|
let command_count = commands
|
||||||
.iter()
|
.iter()
|
||||||
.map(|cmd| cmd.get_count(options))
|
.map(|cmd| cmd.get_count(options))
|
||||||
.sum::<usize>();
|
.sum::<usize>();
|
||||||
// only create a function if there are more than one command
|
// only create a function if there are more than one command
|
||||||
if command_count > 1 {
|
match command_count {
|
||||||
let uid = function_state.request_uid();
|
0 => Vec::new(),
|
||||||
let pass_macros = group_contains_macro(commands);
|
1 => commands[0].compile(options, global_state, function_state),
|
||||||
|
_ => {
|
||||||
|
let uid = function_state.request_uid();
|
||||||
|
let pass_macros = group_contains_macro(commands);
|
||||||
|
|
||||||
// calculate a hashed path for the function in the `sb` subfolder
|
// calculate a hashed path for the function in the `sb` subfolder
|
||||||
let function_path = {
|
let function_path = {
|
||||||
let function_path = function_state.path();
|
let function_path = function_state.path();
|
||||||
let function_path = function_path.strip_prefix("sb/").unwrap_or(function_path);
|
let function_path = function_path.strip_prefix("sb/").unwrap_or(function_path);
|
||||||
|
|
||||||
let pre_hash_path = function_path.to_owned() + ":" + &uid.to_string();
|
let pre_hash_path = function_path.to_owned() + ":" + &uid.to_string();
|
||||||
let hash = md5::hash(pre_hash_path).to_hex_lowercase();
|
let hash = md5::hash(pre_hash_path).to_hex_lowercase();
|
||||||
|
|
||||||
"sb/".to_string() + function_path + "/" + &hash[..16]
|
"sb/".to_string() + function_path + "/" + &hash[..16]
|
||||||
};
|
};
|
||||||
|
|
||||||
let namespace = function_state.namespace();
|
let namespace = function_state.namespace();
|
||||||
|
|
||||||
// create a new function with the commands
|
// create a new function with the commands
|
||||||
let mut function = Function::new(namespace, &function_path);
|
let mut function = Function::new(namespace, &function_path);
|
||||||
function.get_commands_mut().extend(commands.iter().cloned());
|
function.get_commands_mut().extend(commands.iter().cloned());
|
||||||
function_state.add_function(&function_path, function);
|
function_state.add_function(&function_path, function);
|
||||||
|
|
||||||
let mut function_invocation = format!("function {namespace}:{function_path}");
|
let mut function_invocation = format!("function {namespace}:{function_path}");
|
||||||
|
|
||||||
if pass_macros {
|
if pass_macros {
|
||||||
let macros_block = group_get_macros(commands)
|
// WARNING: this seems to be the only way to pass macros to the function called.
|
||||||
.into_iter()
|
// Because everything is passed as a string, it looses one "level" of escaping per pass.
|
||||||
.map(|m| format!("{m}:$({m})"))
|
let macros_block = group_get_macros(commands)
|
||||||
.collect::<Vec<_>>()
|
.into_iter()
|
||||||
.join(",");
|
.map(|m| format!(r#"{m}:"$({m})""#))
|
||||||
function_invocation.push_str(&format!(" {{{macros_block}}}"));
|
.collect::<Vec<_>>()
|
||||||
|
.join(",");
|
||||||
|
function_invocation.push_str(&format!(" {{{macros_block}}}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
vec![CompiledCommand::new(function_invocation).with_contains_macros(pass_macros)]
|
||||||
}
|
}
|
||||||
|
|
||||||
vec![function_invocation]
|
|
||||||
} else {
|
|
||||||
commands
|
|
||||||
.iter()
|
|
||||||
.flat_map(|cmd| cmd.compile(options, global_state, function_state))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,12 +395,12 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
command_a.compile(options, global_state, function_state),
|
command_a.compile(options, global_state, function_state),
|
||||||
vec!["say Hello, world!".to_string()]
|
vec![CompiledCommand::new("say Hello, world!")]
|
||||||
);
|
);
|
||||||
assert_eq!(command_a.get_count(options), 1);
|
assert_eq!(command_a.get_count(options), 1);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
command_b.compile(options, global_state, function_state),
|
command_b.compile(options, global_state, function_state),
|
||||||
vec!["say foo bar".to_string()]
|
vec![CompiledCommand::new("say foo bar")]
|
||||||
);
|
);
|
||||||
assert_eq!(command_b.get_count(options), 1);
|
assert_eq!(command_b.get_count(options), 1);
|
||||||
}
|
}
|
||||||
|
@ -408,7 +415,7 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
comment.compile(options, global_state, function_state),
|
comment.compile(options, global_state, function_state),
|
||||||
vec!["#this is a comment".to_string()]
|
vec![CompiledCommand::new("#this is a comment").with_forbid_prefix(true)]
|
||||||
);
|
);
|
||||||
assert_eq!(comment.get_count(options), 0);
|
assert_eq!(comment.get_count(options), 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,18 +68,20 @@ impl Function {
|
||||||
if c.contains_macro() {
|
if c.contains_macro() {
|
||||||
cmds.into_iter()
|
cmds.into_iter()
|
||||||
.map(|c| {
|
.map(|c| {
|
||||||
if c.starts_with('#') {
|
if c.contains_macros() {
|
||||||
c
|
let content = format!("${c}");
|
||||||
|
c.with_command(content)
|
||||||
} else {
|
} else {
|
||||||
format!("${c}")
|
c
|
||||||
}
|
}
|
||||||
|
.to_string()
|
||||||
})
|
})
|
||||||
.collect()
|
.collect::<Vec<_>>()
|
||||||
} else {
|
} else {
|
||||||
cmds
|
cmds.into_iter().map(|c| c.to_string()).collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
VFile::Text(content)
|
VFile::Text(content)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! Compile options for the compiler.
|
//! Compile options for the compiler.
|
||||||
|
|
||||||
use std::sync::Mutex;
|
use std::{fmt::Display, ops::Deref, sync::Mutex};
|
||||||
|
|
||||||
use getset::Getters;
|
use getset::Getters;
|
||||||
|
|
||||||
|
@ -86,3 +86,127 @@ impl FunctionCompilerState {
|
||||||
uid
|
uid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Compiled command, ready to be written to a function.
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
|
||||||
|
pub struct CompiledCommand {
|
||||||
|
/// The command string.
|
||||||
|
command: String,
|
||||||
|
/// Whether the command is not allowed to be prefixed.
|
||||||
|
forbid_prefix: bool,
|
||||||
|
/// Whether the command contains a macro.
|
||||||
|
contains_macros: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompiledCommand {
|
||||||
|
/// Create a new compiled command.
|
||||||
|
#[must_use]
|
||||||
|
pub fn new<S>(command: S) -> Self
|
||||||
|
where
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
command: command.into(),
|
||||||
|
forbid_prefix: false,
|
||||||
|
contains_macros: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the command string.
|
||||||
|
#[must_use]
|
||||||
|
pub fn as_str(&self) -> &str {
|
||||||
|
&self.command
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the command string.
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_command(mut self, command: String) -> Self {
|
||||||
|
self.command = command;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set whether the command is forbidden to be prefixed.
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_forbid_prefix(mut self, forbid_prefix: bool) -> Self {
|
||||||
|
self.forbid_prefix = forbid_prefix;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set whether the command contains a macro.
|
||||||
|
#[must_use]
|
||||||
|
pub fn with_contains_macros(mut self, contains_macros: bool) -> Self {
|
||||||
|
self.contains_macros = contains_macros;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get whether the command is forbidden to be prefixed.
|
||||||
|
#[must_use]
|
||||||
|
pub fn forbids_prefix(&self) -> bool {
|
||||||
|
self.forbid_prefix
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get whether the command contains a macro.
|
||||||
|
#[must_use]
|
||||||
|
pub fn contains_macros(&self) -> bool {
|
||||||
|
self.contains_macros
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Apply a prefix to the command (if allowed).
|
||||||
|
#[must_use]
|
||||||
|
pub fn apply_prefix<S>(mut self, prefix: S) -> Self
|
||||||
|
where
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
|
if !self.forbid_prefix {
|
||||||
|
self.command = prefix.into() + &self.command;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Combine current forbid prefix status with the input.
|
||||||
|
#[must_use]
|
||||||
|
pub fn or_forbid_prefix(mut self, forbid_prefix: bool) -> Self {
|
||||||
|
self.forbid_prefix = self.forbid_prefix || forbid_prefix;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Combine current contains macro status with the input.
|
||||||
|
#[must_use]
|
||||||
|
pub fn or_contains_macros(mut self, contains_macros: bool) -> Self {
|
||||||
|
self.contains_macros = self.contains_macros || contains_macros;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for CompiledCommand {
|
||||||
|
type Target = String;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.command
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for CompiledCommand {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self.command)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CompiledCommand> for String {
|
||||||
|
fn from(compiled_command: CompiledCommand) -> Self {
|
||||||
|
compiled_command.command
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for CompiledCommand {
|
||||||
|
fn from(value: String) -> Self {
|
||||||
|
Self::new(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for CompiledCommand {
|
||||||
|
fn from(value: &str) -> Self {
|
||||||
|
Self::new(value.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue