Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Reload resources on model export #10970

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
33 changes: 12 additions & 21 deletions packages/engine/src/assets/functions/resourceLoaderFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ export const loadResource = <T extends ResourceAssetType>(
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<T> | undefined
Expand Down Expand Up @@ -160,13 +160,13 @@ export const loadResource = <T extends ResourceAssetType>(

/**
*
* 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]
Expand All @@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function useLoader<T extends ResourceAssetType>(
const value = useHookstate<T | null>(null)
const error = useHookstate<ErrorEvent | Error | null>(null)
const progress = useHookstate<ProgressEvent<EventTarget> | null>(null)
const uuid = useHookstate<string>(uuidv4())
const uuid = useHookstate<string>(uuidv4)

const unload = () => {
if (url) ResourceManager.unload(url, entity, uuid.value)
Expand Down
2 changes: 1 addition & 1 deletion packages/spatial/src/resources/ResourceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type Resource = {
references: Entity[]
asset?: ResourceAssetType
assetRefs?: Record<ResourceType, string[]>
onLoads?: Record<string, (response: ResourceAssetType) => void>
onLoads?: Record<string, { entity: Entity; onLoad: (response: ResourceAssetType) => void }>
metadata: Metadata
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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 ||
Expand Down
Loading