Skip to content

Commit 237a074

Browse files
committed
core: Clarify behavior on duplicate character IDs
1 parent 181f092 commit 237a074

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

core/src/display_object/movie_clip.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3606,10 +3606,15 @@ impl<'gc, 'a> MovieClipShared<'gc> {
36063606
num_frames,
36073607
);
36083608

3609-
self.library_mut(context)
3610-
.register_character(id, Character::MovieClip(movie_clip));
3611-
3612-
self.preload_progress.cur_preload_symbol.set(Some(id));
3609+
if self.library_mut(context)
3610+
.register_character(id, Character::MovieClip(movie_clip))
3611+
{
3612+
self.preload_progress.cur_preload_symbol.set(Some(id));
3613+
} else {
3614+
// This character was already defined, so we can skip preloading it, as the
3615+
// character ID refers to the pre-existing character, and not this one.
3616+
return Ok(ControlFlow::Exit);
3617+
}
36133618

36143619
let should_exit = chunk_limit.did_ops_breach_limit(context, 4);
36153620
if should_exit {

core/src/library.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,22 @@ impl<'gc> MovieLibrary<'gc> {
142142
}
143143
}
144144

145-
pub fn register_character(&mut self, id: CharacterId, character: Character<'gc>) {
146-
// TODO(Herschel): What is the behavior if id already exists?
147-
if !self.contains_character(id) {
148-
if let Character::Font(font) = character {
149-
self.fonts.register(font);
145+
/// Registers a character; returns `true` if successful, or `false` if a character with
146+
/// the given ID already exists.
147+
pub fn register_character(&mut self, id: CharacterId, character: Character<'gc>) -> bool {
148+
use std::collections::hash_map::Entry;
149+
match self.characters.entry(id) {
150+
Entry::Vacant(e) => {
151+
if let Character::Font(font) = character {
152+
self.fonts.register(font);
153+
}
154+
e.insert(character);
155+
true
156+
}
157+
Entry::Occupied(_) => {
158+
tracing::error!("Character ID collision: Tried to register ID {} twice", id);
159+
false
150160
}
151-
152-
self.characters.insert(id, character);
153-
} else {
154-
tracing::error!("Character ID collision: Tried to register ID {} twice", id);
155161
}
156162
}
157163

0 commit comments

Comments
 (0)