Compare commits

...

1 Commits

Author SHA1 Message Date
Davy Hélard
b06ff9fd4e WIP: Update resources at hot-reload 2023-12-07 13:13:45 +01:00
10 changed files with 64 additions and 7 deletions

View File

@@ -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) {
@@ -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, {

View File

@@ -50,5 +50,9 @@ namespace gdjs {
this._nameToContent.clear();
this._fileToContent.clear();
}
getAllValues(): IterableIterator<C> {
return this._fileToContent.values();
}
}
}

View File

@@ -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);
@@ -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);
}
/**
@@ -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(

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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,

View File

@@ -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> = {};
@@ -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

View File

@@ -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).
*

View File

@@ -76,6 +76,10 @@ namespace gdjs {
return resourceKinds;
}
clearCache(): void {
this._loadedFontsData.clear();
}
/**
* Get the instance of the default `Pixi.BitmapFont`, always available.
*/

View File

@@ -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.