Skip to content

Commit d6a1db2

Browse files
committed
Merge pull request godotengine#85302 from RandomShaper/copy_constr_avb
Perform safe copies in `AnimatedValuesBackup::get_cache_copy()`
2 parents 8d43168 + fbb931d commit d6a1db2

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

scene/animation/animation_mixer.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,38 +2166,33 @@ AnimationMixer::TrackCache *AnimatedValuesBackup::get_cache_copy(AnimationMixer:
21662166
switch (p_cache->type) {
21672167
case Animation::TYPE_VALUE: {
21682168
AnimationMixer::TrackCacheValue *src = static_cast<AnimationMixer::TrackCacheValue *>(p_cache);
2169-
AnimationMixer::TrackCacheValue *tc = memnew(AnimationMixer::TrackCacheValue);
2170-
memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheValue));
2169+
AnimationMixer::TrackCacheValue *tc = memnew(AnimationMixer::TrackCacheValue(*src));
21712170
return tc;
21722171
}
21732172

21742173
case Animation::TYPE_POSITION_3D:
21752174
case Animation::TYPE_ROTATION_3D:
21762175
case Animation::TYPE_SCALE_3D: {
21772176
AnimationMixer::TrackCacheTransform *src = static_cast<AnimationMixer::TrackCacheTransform *>(p_cache);
2178-
AnimationMixer::TrackCacheTransform *tc = memnew(AnimationMixer::TrackCacheTransform);
2179-
memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheTransform));
2177+
AnimationMixer::TrackCacheTransform *tc = memnew(AnimationMixer::TrackCacheTransform(*src));
21802178
return tc;
21812179
}
21822180

21832181
case Animation::TYPE_BLEND_SHAPE: {
21842182
AnimationMixer::TrackCacheBlendShape *src = static_cast<AnimationMixer::TrackCacheBlendShape *>(p_cache);
2185-
AnimationMixer::TrackCacheBlendShape *tc = memnew(AnimationMixer::TrackCacheBlendShape);
2186-
memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheBlendShape));
2183+
AnimationMixer::TrackCacheBlendShape *tc = memnew(AnimationMixer::TrackCacheBlendShape(*src));
21872184
return tc;
21882185
}
21892186

21902187
case Animation::TYPE_BEZIER: {
21912188
AnimationMixer::TrackCacheBezier *src = static_cast<AnimationMixer::TrackCacheBezier *>(p_cache);
2192-
AnimationMixer::TrackCacheBezier *tc = memnew(AnimationMixer::TrackCacheBezier);
2193-
memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheBezier));
2189+
AnimationMixer::TrackCacheBezier *tc = memnew(AnimationMixer::TrackCacheBezier(*src));
21942190
return tc;
21952191
}
21962192

21972193
case Animation::TYPE_AUDIO: {
21982194
AnimationMixer::TrackCacheAudio *src = static_cast<AnimationMixer::TrackCacheAudio *>(p_cache);
2199-
AnimationMixer::TrackCacheAudio *tc = memnew(AnimationMixer::TrackCacheAudio);
2200-
memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheAudio));
2195+
AnimationMixer::TrackCacheAudio *tc = memnew(AnimationMixer::TrackCacheAudio(*src));
22012196
return tc;
22022197
}
22032198

scene/animation/animation_mixer.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ class AnimationMixer : public Node {
142142
ObjectID object_id;
143143
real_t total_weight = 0.0;
144144

145+
TrackCache() = default;
146+
TrackCache(const TrackCache &p_other) :
147+
root_motion(p_other.root_motion),
148+
setup_pass(p_other.setup_pass),
149+
type(p_other.type),
150+
object(p_other.object),
151+
object_id(p_other.object_id),
152+
total_weight(p_other.total_weight) {}
153+
145154
virtual ~TrackCache() {}
146155
};
147156

@@ -161,6 +170,24 @@ class AnimationMixer : public Node {
161170
Quaternion rot;
162171
Vector3 scale;
163172

173+
TrackCacheTransform(const TrackCacheTransform &p_other) :
174+
TrackCache(p_other),
175+
#ifndef _3D_DISABLED
176+
node_3d(p_other.node_3d),
177+
skeleton(p_other.skeleton),
178+
#endif
179+
bone_idx(p_other.bone_idx),
180+
loc_used(p_other.loc_used),
181+
rot_used(p_other.rot_used),
182+
scale_used(p_other.scale_used),
183+
init_loc(p_other.init_loc),
184+
init_rot(p_other.init_rot),
185+
init_scale(p_other.init_scale),
186+
loc(p_other.loc),
187+
rot(p_other.rot),
188+
scale(p_other.scale) {
189+
}
190+
164191
TrackCacheTransform() {
165192
type = Animation::TYPE_POSITION_3D;
166193
}
@@ -178,6 +205,14 @@ class AnimationMixer : public Node {
178205
float init_value = 0;
179206
float value = 0;
180207
int shape_index = -1;
208+
209+
TrackCacheBlendShape(const TrackCacheBlendShape &p_other) :
210+
TrackCache(p_other),
211+
mesh_3d(p_other.mesh_3d),
212+
init_value(p_other.init_value),
213+
value(p_other.value),
214+
shape_index(p_other.shape_index) {}
215+
181216
TrackCacheBlendShape() { type = Animation::TYPE_BLEND_SHAPE; }
182217
~TrackCacheBlendShape() {}
183218
};
@@ -189,6 +224,16 @@ class AnimationMixer : public Node {
189224
bool is_continuous = false;
190225
bool is_using_angle = false;
191226
Variant element_size;
227+
228+
TrackCacheValue(const TrackCacheValue &p_other) :
229+
TrackCache(p_other),
230+
init_value(p_other.init_value),
231+
value(p_other.value),
232+
subpath(p_other.subpath),
233+
is_continuous(p_other.is_continuous),
234+
is_using_angle(p_other.is_using_angle),
235+
element_size(p_other.element_size) {}
236+
192237
TrackCacheValue() { type = Animation::TYPE_VALUE; }
193238
~TrackCacheValue() {
194239
// Clear ref to avoid leaking.
@@ -206,6 +251,13 @@ class AnimationMixer : public Node {
206251
real_t init_value = 0.0;
207252
real_t value = 0.0;
208253
Vector<StringName> subpath;
254+
255+
TrackCacheBezier(const TrackCacheBezier &p_other) :
256+
TrackCache(p_other),
257+
init_value(p_other.init_value),
258+
value(p_other.value),
259+
subpath(p_other.subpath) {}
260+
209261
TrackCacheBezier() {
210262
type = Animation::TYPE_BEZIER;
211263
}
@@ -235,6 +287,12 @@ class AnimationMixer : public Node {
235287
Ref<AudioStreamPlaybackPolyphonic> audio_stream_playback;
236288
HashMap<ObjectID, PlayingAudioTrackInfo> playing_streams; // Key is Animation resource ObjectID.
237289

290+
TrackCacheAudio(const TrackCacheAudio &p_other) :
291+
TrackCache(p_other),
292+
audio_stream(p_other.audio_stream),
293+
audio_stream_playback(p_other.audio_stream_playback),
294+
playing_streams(p_other.playing_streams) {}
295+
238296
TrackCacheAudio() {
239297
type = Animation::TYPE_AUDIO;
240298
}

0 commit comments

Comments
 (0)