Skip to content

Commit b3f9e83

Browse files
authored
🔀 Merge 🐛 Blueprint settings from previously opened models leak into new, and converted models (#392)
This either fixes, or greatly improves the leaking issue.
2 parents 90ff5bc + 8eaf4c9 commit b3f9e83

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/blueprintFormat.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export interface IBlueprintFormatJSON {
123123
/**
124124
* The project settings of the Blueprint
125125
*/
126-
blueprint_settings?: NonNullable<typeof Project>['animated_java']
126+
blueprint_settings?: Partial<NonNullable<typeof Project>['animated_java']>
127127
/**
128128
* The variants of the Blueprint
129129
*/
@@ -156,6 +156,7 @@ export interface IBlueprintFormatJSON {
156156
export function convertToBlueprint() {
157157
// Convert the current project to a Blueprint
158158
Project!.save_path = ''
159+
Project!.last_used_export_namespace = ''
159160

160161
for (const group of Group.all) {
161162
group.createUniqueName(Group.all.filter(g => g !== group))
@@ -171,7 +172,17 @@ export function convertToBlueprint() {
171172
}
172173

173174
export function getDefaultProjectSettings(): ModelProject['animated_java'] {
174-
return blueprintSettings.defaultValues
175+
return { ...blueprintSettings.defaultValues }
176+
}
177+
178+
function initializeBoundingBoxUpdate() {
179+
if (boundingBoxUpdateIntervalId == undefined) {
180+
boundingBoxUpdateIntervalId = setInterval(() => {
181+
updateBoundingBox()
182+
}, 500)
183+
events.UNLOAD.subscribe(() => clearInterval(boundingBoxUpdateIntervalId), true)
184+
events.UNINSTALL.subscribe(() => clearInterval(boundingBoxUpdateIntervalId), true)
185+
}
175186
}
176187

177188
export function updateBoundingBox() {
@@ -260,11 +271,14 @@ export const BLUEPRINT_CODEC = new Blockbench.Codec('animated_java_blueprint', {
260271
}
261272

262273
if (model.blueprint_settings) {
263-
Project.animated_java = { ...Project.animated_java, ...model.blueprint_settings }
274+
Project.animated_java = {
275+
...blueprintSettings.defaultValues,
276+
...model.blueprint_settings,
277+
}
264278
}
265279

266280
Project.last_used_export_namespace =
267-
model.meta.last_used_export_namespace || Project.animated_java.export_namespace
281+
model.meta.last_used_export_namespace ?? Project.animated_java.export_namespace
268282

269283
if (model.textures) {
270284
for (const texture of model.textures) {
@@ -410,7 +424,7 @@ export const BLUEPRINT_CODEC = new Blockbench.Codec('animated_java_blueprint', {
410424
save_location: Project.save_path,
411425
last_used_export_namespace: Project.last_used_export_namespace,
412426
},
413-
blueprint_settings: Project.animated_java,
427+
blueprint_settings: { ...Project.animated_java },
414428
resolution: {
415429
width: Project.texture_width || 16,
416430
height: Project.texture_height || 16,
@@ -562,25 +576,19 @@ export const BLUEPRINT_FORMAT = new Blockbench.ModelFormat({
562576
onSetup(project, newModel) {
563577
if (!Project) return
564578
console.log('Animated Java Blueprint format setup')
579+
565580
const defaults = getDefaultProjectSettings()
566-
Project.animated_java ??= defaults
567-
for (const [key, value] of Object.entries(defaults) as Array<
568-
[keyof ModelProject['animated_java'], any]
569-
>) {
570-
if (Project.animated_java[key] === undefined) {
571-
// @ts-ignore
572-
Project.animated_java[key] = value
573-
}
581+
if (newModel) {
582+
Project.animated_java = defaults
583+
Project.last_used_export_namespace = ''
584+
} else {
585+
Project.animated_java = { ...defaults, ...Project!.animated_java }
574586
}
575587

576588
const thisProject = Project
577589
Project.variants ??= []
578-
Project.last_used_export_namespace = Project.animated_java.export_namespace
579-
const updateBoundingBoxIntervalId = setInterval(() => {
580-
updateBoundingBox()
581-
}, 500)
582-
events.UNLOAD.subscribe(() => clearInterval(updateBoundingBoxIntervalId), true)
583-
events.UNINSTALL.subscribe(() => clearInterval(updateBoundingBoxIntervalId), true)
590+
591+
initializeBoundingBoxUpdate()
584592

585593
Project.loadingPromises ??= []
586594
Project.loadingPromises.push(
@@ -617,9 +625,13 @@ export const BLUEPRINT_FORMAT = new Blockbench.ModelFormat({
617625
)
618626
},
619627

620-
onActivation() {
621-
console.log('Animated Java Blueprint format activated')
622-
},
628+
// onActivation() {
629+
// console.group('Animated Java Blueprint format activated')
630+
// },
631+
632+
// onDeactivation() {
633+
// console.group('Animated Java Blueprint format deactivated')
634+
// },
623635

624636
codec: BLUEPRINT_CODEC,
625637

src/interface/dialog/blueprintSettings.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { PACKAGE } from '../../constants'
2-
import { SvelteDialog } from '../../util/svelteDialog'
3-
import { Valuable } from '../../util/stores'
1+
import { updateBoundingBox } from '../../blueprintFormat'
2+
import { defaultValues, ExportMode } from '../../blueprintSettings'
43
import BlueprintSettingsDialogSvelteComponent from '../../components/blueprintSettingsDialog.svelte'
4+
import { PACKAGE } from '../../constants'
5+
import { MinecraftVersion } from '../../systems/datapackCompiler/mcbFiles'
56
import { toSafeFuntionName } from '../../util/minecraftUtil'
6-
import { defaultValues, ExportMode } from '../../blueprintSettings'
7+
import { Valuable } from '../../util/stores'
8+
import { SvelteDialog } from '../../util/svelteDialog'
79
import { translate } from '../../util/translation'
8-
import { updateBoundingBox } from '../../blueprintFormat'
9-
import { MinecraftVersion } from '../../systems/datapackCompiler/mcbFiles'
1010

1111
function getSettings() {
1212
return {

0 commit comments

Comments
 (0)