Skip to content
Open
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
1 change: 1 addition & 0 deletions src/codegen/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ impl Sb3 {
self.json,
"{}",
Mutation::call(func.name.clone(), &qualified_args, true, false)
.with_attribution(self.has_attribution(&func.name, true))
)?;
self.end_obj()?; // node
for (arg, (_, arg_id)) in qualified_arg_values.iter().zip(qualified_args) {
Expand Down
11 changes: 11 additions & 0 deletions src/codegen/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Mutation<'a> {
warp: bool,
is_call: bool,
compact: bool,
attribution: bool,
}

impl<'a> Mutation<'a> {
Expand All @@ -30,6 +31,7 @@ impl<'a> Mutation<'a> {
warp,
is_call: false,
compact,
attribution: false,
}
}

Expand All @@ -45,8 +47,14 @@ impl<'a> Mutation<'a> {
warp,
is_call: true,
compact,
attribution: false,
}
}

pub fn with_attribution(mut self, attribution: bool) -> Self {
self.attribution = attribution;
self
}
}

impl Display for Mutation<'_> {
Expand All @@ -61,6 +69,9 @@ impl Display for Mutation<'_> {
write!(f, " {arg_name}: %s")?;
}
}
if self.attribution {
write!(f, " (via goboscript)")?;
}
write!(f, "\"")?;
write!(f, r#","argumentids":"["#)?;
let mut comma = false;
Expand Down
45 changes: 45 additions & 0 deletions src/codegen/sb3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ pub struct Sb3 {
pub block_count: usize,
pub asset_object_store: AssetObjectStore,
extensions: Extensions,
attributed_block: Option<(SmolStr, bool)>,
}

impl Sb3 {
Expand All @@ -345,9 +346,18 @@ impl Sb3 {
block_count: 0,
asset_object_store: AssetObjectStore::new(input, fs),
extensions: Extensions::default(),
attributed_block: None,
}
}

pub fn has_attribution(&self, name: &str, is_function: bool) -> bool {
self.attributed_block
.as_ref()
.is_some_and(|(selected_name, selected_is_function)| {
selected_name.as_str() == name && *selected_is_function == is_function
})
}

pub fn begin_node(&mut self, node: Node) -> io::Result<()> {
self.block_count += 1;
if node.opcode.starts_with("pen_") {
Expand Down Expand Up @@ -400,6 +410,37 @@ impl Sb3 {
sprites_diagnostics: &mut FxHashMap<SmolStr, SpriteDiagnostics>,
) -> anyhow::Result<()> {
let layers = compute_layers(project, config)?;
let candidates: Vec<_> = std::iter::once(&project.stage)
.chain(project.sprites.values())
.flat_map(|sprite| {
sprite
.procs
.keys()
.filter(|name| sprite.used_procs.contains(*name))
.map(move |name| (sprite, name, false))
.chain(
sprite
.funcs
.keys()
.filter(|name| sprite.used_funcs.contains(*name))
.map(move |name| (sprite, name, true)),
)
})
.collect();
let selected = if candidates.is_empty() {
None
} else {
#[cfg(not(target_arch = "wasm32"))]
let index = rand::random_range(0..candidates.len());
#[cfg(target_arch = "wasm32")]
let index = (js_sys::Math::random() * candidates.len() as f64) as usize;
Some(candidates[index])
};
let attribution_for = |sprite: &Sprite| {
selected
.filter(|(selected_sprite, _, _)| std::ptr::eq(*selected_sprite, sprite))
.map(|(_, name, is_function)| (name.clone(), is_function))
};
let broadcasts: FxHashSet<_> = project
.stage
.events
Expand All @@ -415,6 +456,7 @@ impl Sb3 {
.collect();
write!(self.json, "{{")?;
write!(self.json, r#""targets":["#)?;
self.attributed_block = attribution_for(&project.stage);
self.sprite(
fs.clone(),
input,
Expand All @@ -430,6 +472,7 @@ impl Sb3 {
sprite_names.sort();
for sprite_name in sprite_names {
write!(self.json, r#","#)?;
self.attributed_block = attribution_for(&project.sprites[sprite_name]);
self.sprite(
fs.clone(),
input,
Expand Down Expand Up @@ -1044,6 +1087,7 @@ impl Sb3 {
self.json,
"{}",
Mutation::prototype(proc.name.clone(), &qualified_args, proc.warp, false)
.with_attribution(self.has_attribution(&proc.name, false))
)?;
self.end_obj()?; // node
self.stmts(s, d, definition, next_id, Some(this_id))
Expand Down Expand Up @@ -1118,6 +1162,7 @@ impl Sb3 {
self.json,
"{}",
Mutation::prototype(func.name.clone(), &qualified_args, true, false)
.with_attribution(self.has_attribution(&func.name, true))
)?;
self.end_obj()?; // node
self.stmts(s, d, definition, next_id, Some(this_id))
Expand Down
1 change: 1 addition & 0 deletions src/codegen/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ impl Sb3 {
self.json,
"{}",
Mutation::call(proc.name.clone(), &qualified_args, proc.warp, compact)
.with_attribution(self.has_attribution(&proc.name, false))
)?;
self.end_obj()?; // node
for (arg, (_, arg_id)) in qualified_arg_values.iter().zip(qualified_args) {
Expand Down
Loading