@@ -265,55 +265,56 @@ export async function uploadCollaborationAsset(runtime, asset) {
265265 }
266266}
267267
268- export async function loadRemoteCostume ( costumeData , runtime , retries = 3 ) {
269-
270-
268+ export async function loadRemoteCostume ( costumeData , runtime , retries = 3 , existingCostume = null ) {
271269 const costume = { ...costumeData } ;
272270 if ( ! costume . md5 ) costume . md5 = costume . md5ext ;
273271 if ( ! costume . dataFormat && costume . md5ext ) {
274272 costume . dataFormat = costume . md5ext . split ( '.' ) . pop ( ) ;
275273 }
276-
277- const assetType = costume . dataFormat === 'svg'
278- ? runtime . storage . AssetType . ImageVector
274+ const assetType = costume . dataFormat === 'svg'
275+ ? runtime . storage . AssetType . ImageVector
279276 : runtime . storage . AssetType . ImageBitmap ;
280-
281277 try {
282-
283278 const asset = await runtime . storage . load ( assetType , costume . assetId , costume . dataFormat ) ;
284-
285279 if ( ! asset ) {
286280 if ( retries > 0 ) {
287-
288281 await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
289- return loadRemoteCostume ( costumeData , runtime , retries - 1 ) ;
282+ return loadRemoteCostume ( costumeData , runtime , retries - 1 , existingCostume ) ;
290283 }
291-
292- return costume ;
284+ return costume ;
293285 }
294-
295286 costume . asset = asset ;
296-
297-
298287 return new Promise ( resolve => {
299288 if ( costume . dataFormat === 'svg' ) {
300289 const svgString = asset . decodeText ( ) ;
301- costume . skinId = runtime . renderer . createSVGSkin ( svgString , [ costume . rotationCenterX , costume . rotationCenterY ] ) ;
290+ if ( existingCostume && existingCostume . skinId !== undefined && existingCostume . dataFormat === 'svg' ) {
291+ runtime . renderer . updateSVGSkin ( existingCostume . skinId , svgString , [ costume . rotationCenterX , costume . rotationCenterY ] ) ;
292+ costume . skinId = existingCostume . skinId ;
293+ } else {
294+ costume . skinId = runtime . renderer . createSVGSkin ( svgString , [ costume . rotationCenterX , costume . rotationCenterY ] ) ;
295+ if ( existingCostume && existingCostume . skinId !== undefined ) {
296+ runtime . renderer . destroySkin ( existingCostume . skinId ) ;
297+ }
298+ }
302299 costume . size = runtime . renderer . getSkinSize ( costume . skinId ) ;
303-
304300 resolve ( costume ) ;
305301 } else {
306302 const image = new Image ( ) ;
307303 image . onload = function ( ) {
308- const skinId = runtime . renderer . createBitmapSkin (
309- image ,
310- costume . bitmapResolution || 1 ,
311- [ costume . rotationCenterX , costume . rotationCenterY ]
312- ) ;
313- costume . skinId = skinId ;
304+ const resolution = costume . bitmapResolution || 1 ;
305+ const center = [ costume . rotationCenterX , costume . rotationCenterY ] ;
306+ if ( existingCostume && existingCostume . skinId !== undefined && existingCostume . dataFormat !== 'svg' ) {
307+ runtime . renderer . updateBitmapSkin ( existingCostume . skinId , image , resolution ,
308+ [ center [ 0 ] / resolution , center [ 1 ] / resolution ] ) ;
309+ costume . skinId = existingCostume . skinId ;
310+ } else {
311+ costume . skinId = runtime . renderer . createBitmapSkin ( image , resolution , center ) ;
312+ if ( existingCostume && existingCostume . skinId !== undefined ) {
313+ runtime . renderer . destroySkin ( existingCostume . skinId ) ;
314+ }
315+ }
314316 const renderSize = runtime . renderer . getSkinSize ( costume . skinId ) ;
315- costume . size = [ renderSize [ 0 ] * 2 , renderSize [ 1 ] * 2 ] ;
316-
317+ costume . size = [ renderSize [ 0 ] * 2 , renderSize [ 1 ] * 2 ] ;
317318 resolve ( costume ) ;
318319 } ;
319320 image . onerror = function ( err ) {
@@ -323,7 +324,6 @@ export async function loadRemoteCostume(costumeData, runtime, retries = 3) {
323324 }
324325 } ) ;
325326 } catch ( e ) {
326-
327327 return costume ;
328328 }
329329}
@@ -577,12 +577,8 @@ export function performInitialSync() {
577577 const remoteName = yTargetMap . get ( '__targetName' ) ;
578578 if ( remoteName ) {
579579 const localTarget = vm . runtime . targets . find ( t => t . getName ( ) === remoteName ) ;
580- if ( localTarget && localTarget . isStage && localTarget . id !== remoteId ) {
581- localTarget . id = remoteId ;
582- localTarget . originalTargetId = remoteId ;
583- Object . values ( localTarget . variables ) . forEach ( v => {
584- v . targetId = remoteId ;
585- } ) ;
580+ if ( localTarget && localTarget . isStage && localTarget . id !== remoteId ) {
581+ vm . runtime . updateTargetId ( localTarget , remoteId ) ;
586582 }
587583 }
588584 } ) ;
@@ -604,8 +600,7 @@ export function performInitialSync() {
604600 target = vm . runtime . getTargetForStage ( ) ;
605601 if ( target ) {
606602 localTargetsMap . delete ( target . id ) ;
607- target . id = id ;
608- target . originalTargetId = id ;
603+ vm . runtime . updateTargetId ( target , id ) ;
609604 }
610605 }
611606
0 commit comments