Skip to content

Commit 5ea1462

Browse files
Merge pull request #2923 from thmq/HotFixBackpackScene
[HotFix] Fixed Crash when Unpacking Scene
2 parents 19d243f + 5346eae commit 5ea1462

5 files changed

Lines changed: 40 additions & 51 deletions

File tree

catroid/src/main/java/org/catrobat/catroid/content/Scene.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
import org.catrobat.catroid.content.bricks.Brick;
3030
import org.catrobat.catroid.content.bricks.BroadcastMessageBrick;
31-
import org.catrobat.catroid.content.bricks.UserListBrick;
32-
import org.catrobat.catroid.content.bricks.UserVariableBrick;
3331
import org.catrobat.catroid.formulaeditor.datacontainer.DataContainer;
3432
import org.catrobat.catroid.io.XStreamFieldKeyOrder;
3533
import org.catrobat.catroid.physics.PhysicsWorld;
@@ -203,20 +201,4 @@ public Set<String> getBroadcastMessagesInUse() {
203201
}
204202
return messagesInUse;
205203
}
206-
207-
public synchronized void correctUserVariableAndListReferences() {
208-
for (Sprite sprite : spriteList) {
209-
for (Brick brick : sprite.getAllBricks()) {
210-
if (brick instanceof UserVariableBrick) {
211-
((UserVariableBrick) brick).setUserVariable(dataContainer.getUserVariable(sprite,
212-
((UserVariableBrick) brick).getUserVariable().getName()));
213-
}
214-
215-
if (brick instanceof UserListBrick) {
216-
((UserListBrick) brick).setUserList(dataContainer.getUserList(sprite,
217-
((UserListBrick) brick).getUserList().getName()));
218-
}
219-
}
220-
}
221-
}
222204
}

catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/controller/LookController.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.catrobat.catroid.io.StorageOperations;
3030
import org.catrobat.catroid.ui.controller.BackpackListManager;
3131
import org.catrobat.catroid.ui.recyclerview.util.UniqueNameProvider;
32+
import org.catrobat.catroid.utils.Utils;
3233

3334
import java.io.File;
3435
import java.io.IOException;
@@ -53,7 +54,7 @@ public LookData copy(LookData lookToCopy, Scene dstScene, Sprite dstSprite) thro
5354

5455
LookData findOrCopy(LookData lookToCopy, Scene dstScene, Sprite dstSprite) throws IOException {
5556
for (LookData look : dstSprite.getLookList()) {
56-
if (look.getFile().equals(lookToCopy.getFile())) {
57+
if (compareByChecksum(look.getFile(), lookToCopy.getFile())) {
5758
return look;
5859
}
5960
}
@@ -77,7 +78,7 @@ public LookData pack(LookData lookToPack) throws IOException {
7778

7879
LookData packForSprite(LookData lookToPack, Sprite dstSprite) throws IOException {
7980
for (LookData look : dstSprite.getLookList()) {
80-
if (look.getFile().equals(lookToPack.getFile())) {
81+
if (compareByChecksum(look.getFile(), lookToPack.getFile())) {
8182
return look;
8283
}
8384
}
@@ -96,7 +97,7 @@ public LookData unpack(LookData lookToUnpack, Scene dstScene, Sprite dstSprite)
9697

9798
LookData unpackForSprite(LookData lookToUnpack, Scene dstScene, Sprite dstSprite) throws IOException {
9899
for (LookData look : dstSprite.getLookList()) {
99-
if (look.getFile().equals(lookToUnpack.getFile())) {
100+
if (compareByChecksum(look.getFile(), lookToUnpack.getFile())) {
100101
return look;
101102
}
102103
}
@@ -117,4 +118,11 @@ private List<String> getScope(List<LookData> items) {
117118
private File getImageDir(Scene scene) {
118119
return new File(scene.getDirectory(), IMAGE_DIRECTORY_NAME);
119120
}
121+
122+
private boolean compareByChecksum(File file1, File file2) {
123+
String checksum1 = Utils.md5Checksum(file1);
124+
String checksum2 = Utils.md5Checksum(file2);
125+
126+
return checksum1.equals(checksum2);
127+
}
120128
}

catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/controller/SceneController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ public Scene copy(Scene sceneToCopy, Project dstProject) throws IOException {
101101
scene.getSpriteList().add(spriteController.copy(sprite, sceneToCopy, scene));
102102
}
103103

104-
scene.correctUserVariableAndListReferences();
105104
return scene;
106105
}
107106

catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/controller/ScriptController.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,33 @@ public Script copy(Script scriptToCopy, Scene dstScene, Sprite dstSprite) throws
5959
Script script = scriptToCopy.clone();
6060

6161
for (Brick brick : script.getBrickList()) {
62-
if (brick instanceof SetLookBrick) {
62+
if (brick instanceof SetLookBrick && ((SetLookBrick) brick).getLook() != null) {
6363
((SetLookBrick) brick).setLook(lookController
6464
.findOrCopy(((SetLookBrick) brick).getLook(), dstScene, dstSprite));
6565
}
6666

67-
if (brick instanceof WhenBackgroundChangesBrick) {
67+
if (brick instanceof WhenBackgroundChangesBrick && ((WhenBackgroundChangesBrick) brick).getLook() != null) {
6868
((WhenBackgroundChangesBrick) brick).setLook(lookController
6969
.findOrCopy(((WhenBackgroundChangesBrick) brick).getLook(), dstScene, dstSprite));
7070
}
7171

72-
if (brick instanceof PlaySoundBrick) {
72+
if (brick instanceof PlaySoundBrick && ((PlaySoundBrick) brick).getSound() != null) {
7373
((PlaySoundBrick) brick).setSound(soundController
7474
.findOrCopy(((PlaySoundBrick) brick).getSound(), dstScene, dstSprite));
7575
}
7676

77-
if (brick instanceof PlaySoundAndWaitBrick) {
77+
if (brick instanceof PlaySoundAndWaitBrick && ((PlaySoundAndWaitBrick) brick).getSound() != null) {
7878
((PlaySoundAndWaitBrick) brick).setSound(soundController
7979
.findOrCopy(((PlaySoundAndWaitBrick) brick).getSound(), dstScene, dstSprite));
8080
}
8181

82-
if (brick instanceof UserVariableBrick) {
82+
if (brick instanceof UserVariableBrick && ((UserVariableBrick) brick).getUserVariable() != null) {
8383
UserVariable previousUserVar = ((UserVariableBrick) brick).getUserVariable();
8484
((UserVariableBrick) brick).setUserVariable(dstScene.getDataContainer()
8585
.getUserVariable(dstSprite, previousUserVar.getName()));
8686
}
8787

88-
if (brick instanceof UserListBrick) {
88+
if (brick instanceof UserListBrick && ((UserListBrick) brick).getUserList() != null) {
8989
UserList previousUserList = ((UserListBrick) brick).getUserList();
9090
((UserListBrick) brick).setUserList(dstScene.getDataContainer()
9191
.getUserList(dstSprite, previousUserList.getName()));
@@ -113,22 +113,22 @@ void packForSprite(Script scriptToPack, Sprite dstSprite) throws IOException, Cl
113113
Script script = scriptToPack.clone();
114114

115115
for (Brick brick : script.getBrickList()) {
116-
if (brick instanceof SetLookBrick) {
116+
if (brick instanceof SetLookBrick && ((SetLookBrick) brick).getLook() != null) {
117117
((SetLookBrick) brick).setLook(lookController
118118
.packForSprite(((SetLookBrick) brick).getLook(), dstSprite));
119119
}
120120

121-
if (brick instanceof WhenBackgroundChangesBrick) {
121+
if (brick instanceof WhenBackgroundChangesBrick && ((WhenBackgroundChangesBrick) brick).getLook() != null) {
122122
((WhenBackgroundChangesBrick) brick).setLook(lookController
123123
.packForSprite(((WhenBackgroundChangesBrick) brick).getLook(), dstSprite));
124124
}
125125

126-
if (brick instanceof PlaySoundBrick) {
126+
if (brick instanceof PlaySoundBrick && ((PlaySoundBrick) brick).getSound() != null) {
127127
((PlaySoundBrick) brick).setSound(soundController
128128
.packForSprite(((PlaySoundBrick) brick).getSound(), dstSprite));
129129
}
130130

131-
if (brick instanceof PlaySoundAndWaitBrick) {
131+
if (brick instanceof PlaySoundAndWaitBrick && ((PlaySoundAndWaitBrick) brick).getSound() != null) {
132132
((PlaySoundAndWaitBrick) brick).setSound(soundController
133133
.packForSprite(((PlaySoundAndWaitBrick) brick).getSound(), dstSprite));
134134
}
@@ -162,36 +162,28 @@ void unpackForSprite(Script scriptToUnpack, Scene dstScene, Sprite dstSprite) th
162162
return;
163163
}
164164

165-
if (brick instanceof SetLookBrick) {
165+
if (brick instanceof SetLookBrick && ((SetLookBrick) brick).getLook() != null) {
166166
((SetLookBrick) brick)
167167
.setLook(lookController
168-
.unpackForSprite(((SetLookBrick) brick).getLook(),
169-
dstScene,
170-
dstSprite));
168+
.unpackForSprite(((SetLookBrick) brick).getLook(), dstScene, dstSprite));
171169
}
172170

173-
if (brick instanceof WhenBackgroundChangesBrick) {
171+
if (brick instanceof WhenBackgroundChangesBrick && ((WhenBackgroundChangesBrick) brick).getLook() != null) {
174172
((WhenBackgroundChangesBrick) brick)
175173
.setLook(lookController
176-
.unpackForSprite(((WhenBackgroundChangesBrick) brick).getLook(),
177-
dstScene,
178-
dstSprite));
174+
.unpackForSprite(((WhenBackgroundChangesBrick) brick).getLook(), dstScene, dstSprite));
179175
}
180176

181-
if (brick instanceof PlaySoundBrick) {
177+
if (brick instanceof PlaySoundBrick && ((PlaySoundBrick) brick).getSound() != null) {
182178
((PlaySoundBrick) brick)
183179
.setSound(soundController
184-
.unpackForSprite(((PlaySoundBrick) brick).getSound(),
185-
dstScene,
186-
dstSprite));
180+
.unpackForSprite(((PlaySoundBrick) brick).getSound(), dstScene, dstSprite));
187181
}
188182

189-
if (brick instanceof PlaySoundAndWaitBrick) {
183+
if (brick instanceof PlaySoundAndWaitBrick && ((PlaySoundAndWaitBrick) brick).getSound() != null) {
190184
((PlaySoundAndWaitBrick) brick)
191185
.setSound(soundController
192-
.unpackForSprite(((PlaySoundAndWaitBrick) brick).getSound(),
193-
dstScene,
194-
dstSprite));
186+
.unpackForSprite(((PlaySoundAndWaitBrick) brick).getSound(), dstScene, dstSprite));
195187
}
196188
}
197189

catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/controller/SoundController.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.catrobat.catroid.io.StorageOperations;
3030
import org.catrobat.catroid.ui.controller.BackpackListManager;
3131
import org.catrobat.catroid.ui.recyclerview.util.UniqueNameProvider;
32+
import org.catrobat.catroid.utils.Utils;
3233

3334
import java.io.File;
3435
import java.io.IOException;
@@ -53,7 +54,7 @@ public SoundInfo copy(SoundInfo soundToCopy, Scene dstScene, Sprite dstSprite) t
5354

5455
SoundInfo findOrCopy(SoundInfo soundToCopy, Scene dstScene, Sprite dstSprite) throws IOException {
5556
for (SoundInfo sound : dstSprite.getSoundList()) {
56-
if (sound.getFile().equals(soundToCopy.getFile())) {
57+
if (compareByChecksum(sound.getFile(), soundToCopy.getFile())) {
5758
return sound;
5859
}
5960
}
@@ -77,7 +78,7 @@ public SoundInfo pack(SoundInfo soundToPack) throws IOException {
7778

7879
SoundInfo packForSprite(SoundInfo soundToPack, Sprite dstSprite) throws IOException {
7980
for (SoundInfo sound : dstSprite.getSoundList()) {
80-
if (sound.getFile().equals(soundToPack.getFile())) {
81+
if (compareByChecksum(sound.getFile(), soundToPack.getFile())) {
8182
return sound;
8283
}
8384
}
@@ -97,7 +98,7 @@ public SoundInfo unpack(SoundInfo soundToUnpack, Scene dstScene, Sprite dstSprit
9798

9899
SoundInfo unpackForSprite(SoundInfo soundToUnpack, Scene dstScene, Sprite dstSprite) throws IOException {
99100
for (SoundInfo sound : dstSprite.getSoundList()) {
100-
if (sound.getFile().equals(soundToUnpack.getFile())) {
101+
if (compareByChecksum(sound.getFile(), soundToUnpack.getFile())) {
101102
return sound;
102103
}
103104
}
@@ -118,4 +119,11 @@ private List<String> getScope(List<SoundInfo> items) {
118119
private File getSoundDir(Scene scene) {
119120
return new File(scene.getDirectory(), SOUND_DIRECTORY_NAME);
120121
}
122+
123+
private boolean compareByChecksum(File file1, File file2) {
124+
String checksum1 = Utils.md5Checksum(file1);
125+
String checksum2 = Utils.md5Checksum(file2);
126+
127+
return checksum1.equals(checksum2);
128+
}
121129
}

0 commit comments

Comments
 (0)