Skip to content

Commit e23cb5c

Browse files
committed
Revert "[Stable11.3] Cherry pick asset fixes (#10522)"
This reverts commit f24235b.
1 parent 837120d commit e23cb5c

File tree

10 files changed

+32
-140
lines changed

10 files changed

+32
-140
lines changed

pxtblocks/fields/field_animation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
8080
const frames = parseImageArrayString(text, this.params.taggedTemplate);
8181

8282
if (frames && frames.length) {
83-
const id = this.temporaryAssetId();
83+
const id = this.sourceBlock_.id;
8484

8585
const newAnimation: pxt.Animation = {
8686
internalID: -1,
@@ -97,7 +97,7 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
9797
if (asset) return asset;
9898
}
9999

100-
const id = this.temporaryAssetId();
100+
const id = this.sourceBlock_.id;
101101
const bitmap = new pxt.sprite.Bitmap(this.params.initWidth, this.params.initHeight).data()
102102

103103
const newAnimation: pxt.Animation = {

pxtblocks/fields/field_asset.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export abstract class FieldAssetEditor<U extends FieldAssetEditorOptions, V exte
264264
}
265265

266266
protected onFieldEditorHide(fv: pxt.react.FieldEditorView<pxt.Asset>) {
267-
let result = fv.getResult();
267+
const result = fv.getResult();
268268
const project = pxt.react.getTilemapProject();
269269

270270
if (this.asset.type === pxt.AssetType.Song) {
@@ -275,10 +275,15 @@ export abstract class FieldAssetEditor<U extends FieldAssetEditorOptions, V exte
275275
const old = this.getValue();
276276
if (pxt.assetEquals(this.asset, result)) return;
277277

278-
result = pxt.patchTemporaryAsset(this.asset, result, project);
279-
280278
const oldId = isTemporaryAsset(this.asset) ? null : this.asset.id;
281-
const newId = isTemporaryAsset(result) ? null : result.id;
279+
let newId = isTemporaryAsset(result) ? null : result.id;
280+
281+
if (!oldId && newId === this.sourceBlock_.id) {
282+
// The temporary assets we create just use the block id as the id; give it something
283+
// a little nicer
284+
result.id = project.generateNewID(result.type);
285+
newId = result.id;
286+
}
282287

283288
this.pendingEdit = true;
284289

@@ -547,10 +552,6 @@ export abstract class FieldAssetEditor<U extends FieldAssetEditorOptions, V exte
547552
protected isFullscreen() {
548553
return true;
549554
}
550-
551-
protected temporaryAssetId() {
552-
return this.sourceBlock_.id + "_" + this.name;
553-
}
554555
}
555556

556557
function isTemporaryAsset(asset: pxt.Asset) {

pxtblocks/fields/field_musiceditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class FieldMusicEditor extends FieldAssetEditor<FieldMusicEditorOptions,
6161

6262
const newAsset: pxt.Song = {
6363
internalID: -1,
64-
id: this.temporaryAssetId(),
64+
id: this.sourceBlock_.id,
6565
type: pxt.AssetType.Song,
6666
meta: {
6767
},

pxtblocks/fields/field_sprite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class FieldSpriteEditor extends FieldAssetEditor<FieldSpriteEditorOptions
7070

7171
const newAsset: pxt.ProjectImage = {
7272
internalID: -1,
73-
id: this.temporaryAssetId(),
73+
id: this.sourceBlock_.id,
7474
type: pxt.AssetType.Image,
7575
jresData: pxt.sprite.base64EncodeBitmap(data),
7676
meta: {

pxteditor/monaco-fields/field_musiceditor.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ export class MonacoSongEditor extends MonacoReactFieldEditor<pxt.Song> {
77
protected isPython: boolean;
88
protected isAsset: boolean;
99
protected text: string;
10-
protected editing: pxt.Asset;
1110

1211
protected textToValue(text: string): pxt.Song {
1312
this.isPython = text.indexOf("`") === -1
1413
this.text = text;
1514

1615
const match = pxt.parseAssetTSReference(text);
1716
if (match) {
18-
const { name: matchedName } = match;
17+
const { type, name: matchedName } = match;
1918
const name = matchedName.trim();
2019
const project = pxt.react.getTilemapProject();
2120
this.isAsset = true;
2221
const asset = project.lookupAssetByName(pxt.AssetType.Song, name);
2322
if (asset) {
24-
this.editing = asset;
2523
return asset;
2624
}
2725
else {
@@ -31,8 +29,6 @@ export class MonacoSongEditor extends MonacoReactFieldEditor<pxt.Song> {
3129
newAsset.meta.displayName = name;
3230
}
3331

34-
this.editing = newAsset;
35-
3632
return newAsset;
3733
}
3834
}
@@ -43,24 +39,18 @@ export class MonacoSongEditor extends MonacoReactFieldEditor<pxt.Song> {
4339
const contents = hexLiteralMatch[1].trim();
4440

4541
if (contents) {
46-
this.editing = createFakeAsset(pxt.assets.music.decodeSongFromHex(contents));
47-
}
48-
else {
49-
this.editing = createFakeAsset(pxt.assets.music.getEmptySong(2));
42+
return createFakeAsset(pxt.assets.music.decodeSongFromHex(contents));
5043
}
5144

52-
return this.editing;
45+
return createFakeAsset(pxt.assets.music.getEmptySong(2));
5346
}
5447

5548
return undefined; // never
5649
}
5750

5851
protected resultToText(result: pxt.Song): string {
59-
const project = pxt.react.getTilemapProject();
60-
project.pushUndo();
61-
62-
result = pxt.patchTemporaryAsset(this.editing, result, project) as pxt.Song;
6352
if (result.meta?.displayName) {
53+
const project = pxt.react.getTilemapProject();
6454
if (this.isAsset || project.lookupAsset(result.type, result.id)) {
6555
result = project.updateAsset(result)
6656
} else {

pxteditor/monaco-fields/field_sprite.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ export class MonacoSpriteEditor extends MonacoReactFieldEditor<pxt.ProjectImage>
77
protected isPython: boolean;
88
protected isAsset: boolean;
99
protected template: string;
10-
protected editing: pxt.Asset;
1110

1211
protected textToValue(text: string): pxt.ProjectImage {
1312
this.isPython = text.indexOf("`") === -1
1413
this.template = text.startsWith("bmp") ? "bmp" : "img"
1514

1615
const match = pxt.parseAssetTSReference(text);
1716
if (match) {
18-
const { name: matchedName } = match;
17+
const { type, name: matchedName } = match;
1918
const name = matchedName.trim();
2019
const project = pxt.react.getTilemapProject();
2120
this.isAsset = true;
2221
const asset = project.lookupAssetByName(pxt.AssetType.Image, name);
2322
if (asset) {
24-
this.editing = asset;
2523
return asset;
2624
}
2725
else {
@@ -31,23 +29,16 @@ export class MonacoSpriteEditor extends MonacoReactFieldEditor<pxt.ProjectImage>
3129
newAsset.meta.displayName = name;
3230
}
3331

34-
this.editing = newAsset;
35-
3632
return newAsset;
3733
}
3834
}
3935

40-
this.editing = createFakeAsset(pxt.sprite.imageLiteralToBitmap(text, this.template));
41-
42-
return this.editing;
36+
return createFakeAsset(pxt.sprite.imageLiteralToBitmap(text, this.template));
4337
}
4438

4539
protected resultToText(result: pxt.ProjectImage): string {
46-
const project = pxt.react.getTilemapProject();
47-
project.pushUndo();
48-
result = pxt.patchTemporaryAsset(this.editing, result, project) as pxt.ProjectImage;
49-
5040
if (result.meta?.displayName) {
41+
const project = pxt.react.getTilemapProject();
5142
if (this.isAsset || project.lookupAsset(result.type, result.id)) {
5243
result = project.updateAsset(result)
5344
} else {

pxteditor/monaco-fields/field_tilemap.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ const fieldEditorId = "tilemap-editor";
66
export class MonacoTilemapEditor extends MonacoReactFieldEditor<pxt.ProjectTilemap> {
77
protected isTilemapLiteral: boolean;
88
protected tilemapLiteral: string;
9-
protected editing: pxt.Asset;
109

1110
protected textToValue(text: string): pxt.ProjectTilemap {
1211
const tm = this.readTilemap(text);
1312

1413
const project = pxt.react.getTilemapProject();
1514
pxt.sprite.addMissingTilemapTilesAndReferences(project, tm);
1615

17-
this.editing = tm;
1816
return tm;
1917
}
2018

@@ -33,7 +31,7 @@ export class MonacoTilemapEditor extends MonacoReactFieldEditor<pxt.ProjectTilem
3331
// If the user is still typing, they might try to open the editor on an incomplete tilemap
3432
}
3533
return null;
36-
}
34+
}
3735
}
3836

3937
this.isTilemapLiteral = true;
@@ -73,7 +71,7 @@ export class MonacoTilemapEditor extends MonacoReactFieldEditor<pxt.ProjectTilem
7371
protected resultToText(asset: pxt.ProjectTilemap): string {
7472
const project = pxt.react.getTilemapProject();
7573
project.pushUndo();
76-
asset = pxt.patchTemporaryAsset(this.editing, asset, project) as pxt.ProjectTilemap;
74+
7775
pxt.sprite.updateTilemapReferencesFromResult(project, asset);
7876

7977
if (this.isTilemapLiteral) {

pxtlib/spriteutils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ namespace pxt.sprite {
145145
}
146146
}
147147

148-
dataLength() {
148+
protected dataLength() {
149149
return Math.ceil(this.width * this.height / 2);
150150
}
151151
}
@@ -179,7 +179,7 @@ namespace pxt.sprite {
179179
this.buf[index] = value;
180180
}
181181

182-
dataLength() {
182+
protected dataLength() {
183183
return this.width * this.height;
184184
}
185185
}

pxtlib/tilemap.ts

Lines changed: 6 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,6 @@ namespace pxt {
154154
const existing = this.lookupByID(id);
155155

156156
if (!assetEquals(existing, newValue)) {
157-
if (!validateAsset(newValue) && validateAsset(existing)) {
158-
pxt.warn("Refusing to overwrite asset with invalid version");
159-
pxt.tickEvent("assets.invalidAssetOverwrite", { assetType: newValue.type });
160-
return existing;
161-
}
162-
163157
this.removeByID(id);
164158
asset = this.add(newValue);
165159
this.notifyListener(newValue.internalID);
@@ -1715,10 +1709,6 @@ namespace pxt {
17151709

17161710
export function cloneAsset<U extends Asset>(asset: U): U {
17171711
asset.meta = Object.assign({}, asset.meta);
1718-
if (asset.meta.temporaryInfo) {
1719-
asset.meta.temporaryInfo = Object.assign({}, asset.meta.temporaryInfo);
1720-
}
1721-
17221712
switch (asset.type) {
17231713
case AssetType.Tile:
17241714
case AssetType.Image:
@@ -1958,69 +1948,6 @@ namespace pxt {
19581948
return getShortIDCore(asset.type, asset.id);
19591949
}
19601950

1961-
export function validateAsset(asset: pxt.Asset) {
1962-
if (!asset) return false;
1963-
1964-
switch (asset.type) {
1965-
case AssetType.Image:
1966-
case AssetType.Tile:
1967-
return validateImageAsset(asset as ProjectImage | Tile);
1968-
case AssetType.Tilemap:
1969-
return validateTilemap(asset as ProjectTilemap);
1970-
case AssetType.Animation:
1971-
return validateAnimation(asset as Animation)
1972-
case AssetType.Song:
1973-
return validateSong(asset as Song);
1974-
}
1975-
}
1976-
1977-
function validateImageAsset(asset: ProjectImage | Tile) {
1978-
if (!asset.bitmap) return false;
1979-
1980-
return validateBitmap(sprite.Bitmap.fromData(asset.bitmap));
1981-
}
1982-
1983-
function validateTilemap(tilemap: ProjectTilemap) {
1984-
if (
1985-
!tilemap.data ||
1986-
!tilemap.data.tilemap ||
1987-
!tilemap.data.tileset ||
1988-
!tilemap.data.tileset.tileWidth ||
1989-
!tilemap.data.tileset.tiles?.length ||
1990-
!tilemap.data.layers
1991-
) {
1992-
return false;
1993-
}
1994-
1995-
return validateBitmap(sprite.Bitmap.fromData(tilemap.data.layers)) &&
1996-
validateBitmap(tilemap.data.tilemap);
1997-
}
1998-
1999-
function validateAnimation(animation: Animation) {
2000-
if (!animation.frames?.length || animation.interval <= 0) {
2001-
return false;
2002-
}
2003-
2004-
return !animation.frames.some(frame => !validateBitmap(sprite.Bitmap.fromData(frame)));
2005-
}
2006-
2007-
function validateBitmap(bitmap: sprite.Bitmap) {
2008-
return bitmap.width > 0 &&
2009-
bitmap.height > 0 &&
2010-
!Number.isNaN(bitmap.x0) &&
2011-
!Number.isNaN(bitmap.y0) &&
2012-
bitmap.data().data.length === bitmap.dataLength();
2013-
}
2014-
2015-
function validateSong(song: Song) {
2016-
return song.song &&
2017-
song.song.ticksPerBeat > 0 &&
2018-
song.song.beatsPerMeasure > 0 &&
2019-
song.song.measures > 0 &&
2020-
song.song.beatsPerMinute > 0 &&
2021-
!!song.song.tracks;
2022-
}
2023-
20241951
function getShortIDCore(assetType: pxt.AssetType, id: string, allowNoPrefix = false) {
20251952
let prefix: string;
20261953
switch (assetType) {
@@ -2135,6 +2062,12 @@ namespace pxt {
21352062
}
21362063
}
21372064

2065+
2066+
function set16Bit(buf: Uint8ClampedArray, offset: number, value: number) {
2067+
buf[offset] = value & 0xff;
2068+
buf[offset + 1] = (value >> 8) & 0xff;
2069+
}
2070+
21382071
function read16Bit(buf: Uint8ClampedArray, offset: number) {
21392072
return buf[offset] | (buf[offset + 1] << 8)
21402073
}
@@ -2154,21 +2087,4 @@ namespace pxt {
21542087
case AssetType.Song: return snapshot.songs;
21552088
}
21562089
}
2157-
2158-
export function patchTemporaryAsset(oldValue: pxt.Asset, newValue: pxt.Asset, project: TilemapProject) {
2159-
if (!oldValue || assetEquals(oldValue, newValue)) return newValue;
2160-
2161-
newValue = cloneAsset(newValue);
2162-
const wasTemporary = !oldValue.meta.displayName;
2163-
const isTemporary = !newValue.meta.displayName;
2164-
2165-
// if we went from being temporary to no longer being temporary,
2166-
// make sure we replace the junk id with a new value
2167-
if (wasTemporary && !isTemporary) {
2168-
newValue.id = project.generateNewID(newValue.type);
2169-
newValue.internalID = project.getNewInternalId();
2170-
}
2171-
2172-
return newValue;
2173-
}
21742090
}

webapp/src/components/assetEditor/assetSidebar.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,13 @@ class AssetSidebarImpl extends React.Component<AssetSidebarProps, AssetSidebarSt
9595

9696
const project = pxt.react.getTilemapProject();
9797
project.pushUndo();
98-
result = pxt.patchTemporaryAsset(this.props.asset, result, project);
99-
100-
if (result.meta.displayName) {
101-
result = project.updateAsset(result);
102-
}
103-
10498
if (!this.props.asset.meta?.displayName && result.meta.temporaryInfo) {
10599
getBlocksEditor().updateTemporaryAsset(result);
106100
pkg.mainEditorPkg().lookupFile(`this/${pxt.MAIN_BLOCKS}`).setContentAsync(getBlocksEditor().getCurrentSource());
107101
}
108102

103+
if (result.meta.displayName) project.updateAsset(result);
104+
109105
this.props.dispatchChangeGalleryView(GalleryView.User);
110106
this.updateAssets().then(() => simulator.setDirty());
111107
}

0 commit comments

Comments
 (0)