diff --git a/packages/engine/src/assets/functions/resourceLoaderFunctions.ts b/packages/engine/src/assets/functions/resourceLoaderFunctions.ts index 9a8fff16bf..97da72b1f1 100644 --- a/packages/engine/src/assets/functions/resourceLoaderFunctions.ts +++ b/packages/engine/src/assets/functions/resourceLoaderFunctions.ts @@ -90,12 +90,12 @@ export const loadResource = ( onLoads: {} } }) - if (uuid) resources[url].onLoads.merge({ [uuid]: onLoad }) + if (uuid) resources[url].onLoads.merge({ [uuid]: { entity, onLoad } }) } else { //No need for callbacks if the asset has already been loaded callbacks = ResourceManager.resourceCallbacks[ResourceType.Unknown] resources[url].references.merge([entity]) - if (uuid) resources[url].onLoads.merge({ [uuid]: onLoad }) + if (uuid) resources[url].onLoads.merge({ [uuid]: { entity, onLoad } }) const resource = getState(ResourceState).resources[url] const asset = resource.asset as Cloneable | undefined @@ -160,13 +160,13 @@ export const loadResource = ( /** * - * Updates a model's resource without the url changing + * Updates a resource without the url changing * Removes the model from the resource state and reloads * * @param url the url of the asset to update * @returns */ -export const updateModelResource = (url: string) => { +const updateResource = (url: string) => { const resourceState = getMutableState(ResourceState) const resources = resourceState.nested('resources') const resource = resources[url] @@ -176,36 +176,27 @@ export const updateModelResource = (url: string) => { } const onLoads = resource.onLoads.get(NO_PROXY) if (!onLoads) { - console.warn('resourceLoaderFunctions:updateResource No callbacks found to update for url: ' + url) - return - } - - const onLoadArr = Object.values(onLoads) - const entities = resource.references.get(NO_PROXY) as Entity[] - if (onLoadArr.length !== entities.length) { - console.warn( - 'resourceLoaderFunctions:updateResource There should be one loaded callback for every reference, url: ' + url - ) + ResourceState.debugLog('resourceLoaderFunctions:updateResource No callbacks found to update for url: ' + url) return } ResourceState.debugLog('resourceLoaderFunctions:updateResource Updating asset for url: ' + url) const resourceType = resource.type.value ResourceManager.__unsafeRemoveResource(url) - for (let i = 0; i < onLoadArr.length; i++) { - const onLoad = onLoadArr[i] + for (const [uuid, loadObj] of Object.entries(onLoads)) { loadResource( url, resourceType, - entities[i], - (response: ResourceAssetType) => { - onLoad(response) - }, + loadObj.entity, + loadObj.onLoad, () => {}, (error) => { console.error('resourceLoaderFunctions:updateResource error updating resource for url: ' + url, error) }, - new AbortController().signal + new AbortController().signal, + uuid ) } } + +export const ResourceLoaderManager = { updateResource } diff --git a/packages/engine/src/assets/functions/resourceLoaderHooks.ts b/packages/engine/src/assets/functions/resourceLoaderHooks.ts index 7b4cdc5a48..b9ab7afd8a 100644 --- a/packages/engine/src/assets/functions/resourceLoaderHooks.ts +++ b/packages/engine/src/assets/functions/resourceLoaderHooks.ts @@ -46,7 +46,7 @@ function useLoader( const value = useHookstate(null) const error = useHookstate(null) const progress = useHookstate | null>(null) - const uuid = useHookstate(uuidv4()) + const uuid = useHookstate(uuidv4) const unload = () => { if (url) ResourceManager.unload(url, entity, uuid.value) diff --git a/packages/spatial/src/resources/ResourceState.ts b/packages/spatial/src/resources/ResourceState.ts index 38c522b06a..1806e4f1dd 100644 --- a/packages/spatial/src/resources/ResourceState.ts +++ b/packages/spatial/src/resources/ResourceState.ts @@ -108,7 +108,7 @@ type Resource = { references: Entity[] asset?: ResourceAssetType assetRefs?: Record - onLoads?: Record void> + onLoads?: Record void }> metadata: Metadata } diff --git a/packages/ui/src/components/editor/properties/model/index.tsx b/packages/ui/src/components/editor/properties/model/index.tsx index a33f34561c..dd4131f9ee 100644 --- a/packages/ui/src/components/editor/properties/model/index.tsx +++ b/packages/ui/src/components/editor/properties/model/index.tsx @@ -39,7 +39,7 @@ import ErrorPopUp from '@etherealengine/editor/src/components/popup/ErrorPopUp' import { EditorComponentType, commitProperty } from '@etherealengine/editor/src/components/properties/Util' import { exportRelativeGLTF } from '@etherealengine/editor/src/functions/exportGLTF' import { EditorState } from '@etherealengine/editor/src/services/EditorServices' -import { updateModelResource } from '@etherealengine/engine/src/assets/functions/resourceLoaderFunctions' +import { ResourceLoaderManager } from '@etherealengine/engine/src/assets/functions/resourceLoaderFunctions' import { recursiveHipsLookup } from '@etherealengine/engine/src/avatar/AvatarBoneMatching' import { getEntityErrors } from '@etherealengine/engine/src/scene/components/ErrorComponent' import { ModelComponent } from '@etherealengine/engine/src/scene/components/ModelComponent' @@ -107,6 +107,7 @@ export const ModelNodeEditor: EditorComponentType = (props) => { exportRelativeGLTF(entity, srcProject.value, fileName).then(() => { const nuPath = pathJoin(config.client.fileServer, 'projects', srcProject.value, fileName) commitProperty(ModelComponent, 'src')(nuPath) + ResourceLoaderManager.updateResource(nuPath) exporting.set(false) }) } @@ -137,7 +138,7 @@ export const ModelNodeEditor: EditorComponentType = (props) => { value={modelComponent.src.value} onRelease={(src) => { if (src !== modelComponent.src.value) commitProperty(ModelComponent, 'src')(src) - else updateModelResource(src) + else ResourceLoaderManager.updateResource(src) }} /> {errors?.LOADING_ERROR ||