Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Update resources at hot-reload #6029

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions GDJS/Runtime/Model3DManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ namespace gdjs {
return resourceKinds;
}

clearCache(): void {
this._loadedThreeModels.clear();
this._downloadedArrayBuffers.clear();
}

async processResource(resourceName: string): Promise<void> {
const resource = this._resourceLoader.getResource(resourceName);
if (!resource) {
Expand Down Expand Up @@ -114,6 +119,9 @@ namespace gdjs {
if (!loader) {
return;
}
if (this._loadedThreeModels.get(resource)) {
return;
}
const url = this._resourceLoader.getFullUrl(resource.file);
try {
const response = await fetch(url, {
Expand Down
4 changes: 4 additions & 0 deletions GDJS/Runtime/ResourceCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ namespace gdjs {
this._nameToContent.clear();
this._fileToContent.clear();
}

getAllValues(): IterableIterator<C> {
return this._fileToContent.values();
}
}
}
15 changes: 9 additions & 6 deletions GDJS/Runtime/ResourceLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,6 @@ namespace gdjs {
this._resources = new Map<string, ResourceData>();
this._globalResources = globalResources;

// These 3 attributes are filled by `setResources`.
this._sceneResources = new Map<string, Array<string>>();
this._sceneNamesToLoad = new Set<string>();
this._sceneNamesToMakeReady = new Set<string>();
this.setResources(resourceDataArray, globalResources, layoutDataArray);

this._imageManager = new gdjs.ImageManager(this);
this._soundManager = new gdjs.SoundManager(this);
this._fontManager = new gdjs.FontManager(this);
Expand All @@ -176,6 +170,12 @@ namespace gdjs {
this._resourceManagersMap.set(resourceKind, resourceManager);
}
}

// These 3 attributes are filled by `setResources`.
this._sceneResources = new Map<string, Array<string>>();
this._sceneNamesToLoad = new Set<string>();
this._sceneNamesToMakeReady = new Set<string>();
this.setResources(resourceDataArray, globalResources, layoutDataArray);
}

/**
Expand Down Expand Up @@ -212,6 +212,9 @@ namespace gdjs {
for (const resourceData of resourceDataArray) {
this._resources.set(resourceData.name, resourceData);
}
for (const resourceManager of this._resourceManagersMap.values()) {
resourceManager.clearCache();
}
}

async loadAllResources(
Expand Down
5 changes: 5 additions & 0 deletions GDJS/Runtime/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ namespace gdjs {
* Return the kind of resources handled by this manager.
*/
getResourceKinds(): Array<ResourceKind>;

/**
* Clear any resource in cache.
*/
clearCache():void;
}
}
2 changes: 2 additions & 0 deletions GDJS/Runtime/debugger-client/hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,8 @@ namespace gdjs {
): void {
let somethingChanged = false;

// TODO Refresh resources here.

// Check if default properties changed
if (oldInstance.x !== newInstance.x) {
runtimeObject.setX(newInstance.x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace gdjs {
return resourceKinds;
}

clearCache(): void {
// Hot-reload of font is not handled as fonts are unlikely to be edited.
}

/**
* Return the font family associated to the specified font resource name.
* The font resource must have been loaded before. If that's not the case,
Expand Down
6 changes: 5 additions & 1 deletion GDJS/Runtime/howler-sound-manager/howler-sound-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ namespace gdjs {
* It is basically a container to associate channels to sounds and keep a list
* of all sounds being played.
*/
export class HowlerSoundManager {
export class HowlerSoundManager implements gdjs.ResourceManager {
_loadedMusics = new gdjs.ResourceCache<Howl>();
_loadedSounds = new gdjs.ResourceCache<Howl>();
_availableResources: Record<string, ResourceData> = {};
Expand Down Expand Up @@ -437,6 +437,10 @@ namespace gdjs {
return resourceKinds;
}

clearCache(): void {
// TODO Handle hot-reload of sounds.
}

/**
* Ensure rate is in a range valid for Howler.js
* @return The clamped rate
Expand Down
5 changes: 5 additions & 0 deletions GDJS/Runtime/jsonmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ namespace gdjs {
return resourceKinds;
}

clearCache(): void {
this._loadedJsons.clear();
this._callbacks.clear();
}

/**
* Request all the json resources to be preloaded (unless they are marked as not preloaded).
*
Expand Down
4 changes: 4 additions & 0 deletions GDJS/Runtime/pixi-renderers/pixi-bitmapfont-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ namespace gdjs {
return resourceKinds;
}

clearCache(): void {
this._loadedFontsData.clear();
}

/**
* Get the instance of the default `Pixi.BitmapFont`, always available.
*/
Expand Down
18 changes: 18 additions & 0 deletions GDJS/Runtime/pixi-renderers/pixi-image-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ namespace gdjs {
return resourceKinds;
}

clearCache(): void {
for (const texture of this._loadedTextures.getAllValues()) {
texture.destroy(true);
}
for (const name in this._loadedThreeTextures.items) {
const texture = this._loadedThreeTextures.items[name];
texture.dispose();
}
for (const name in this._loadedThreeMaterials.items) {
const texture = this._loadedThreeMaterials.items[name];
texture.dispose();
}
this._loadedTextures.clear();
this._loadedThreeTextures.clear();
this._loadedThreeMaterials.clear();
PIXI.Assets.cache.reset();
}

/**
* Return the PIXI texture associated to the specified resource name.
* Returns a placeholder texture if not found.
Expand Down