@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 4.15 kB
JavaScript
import{path as t}from"../../core/path.js";import{Tags as e}from"../../core/tags.js";import{EventHandler as s}from"../../core/event-handler.js";import{findAvailableLocale as i}from"../i18n/utils.js";import{ABSOLUTE_URL as r}from"./constants.js";import{AssetFile as l}from"./asset-file.js";import{getApplication as a}from"../globals.js";import{http as h}from"../../platform/net/http.js";let o=-1;const n={pvr:"extCompressedTexturePVRTC",dxt:"extCompressedTextureS3TC",etc2:"extCompressedTextureETC",etc1:"extCompressedTextureETC1",basis:"canvas"},d=["pvr","dxt","etc2","etc1","basis"];class c extends s{constructor(t,s,i,r,l){super(),this._id=o--,this._name=t||"",this.type=s,this.tags=new e(this),this._preload=!1,this._file=null,this._data=r||{},this.options=l||{},this._resources=[],this.urlObject=null,this._i18n={},this.loaded=!1,this.loading=!1,this.registry=null,i&&(this.file=i)}set id(t){this._id=t}get id(){return this._id}set name(t){if(this._name===t)return;const e=this._name;this._name=t,this.fire("name",this,this._name,e)}get name(){return this._name}set file(t){if(t&&t.variants&&-1!==["texture","textureatlas","bundle"].indexOf(this.type)){var e;const s=(null==(e=this.registry)||null==(e=e._loader)?void 0:e._app)||a(),i=null==s?void 0:s.graphicsDevice;if(i)for(let e=0,r=d.length;e<r;e++){const r=d[e];if(t.variants[r]&&i[n[r]]){t=t.variants[r];break}if(s.enableBundles){const t=s.bundles.listBundlesForAsset(this);if(t&&t.find((t=>{var e;return null==t||null==(e=t.file)?void 0:e.variants[r]})))break}}}const s=this._file,i=t?new l(t.url,t.filename,t.hash,t.size,t.opt,t.contents):null;(!!i!=!!s||i&&!i.equals(s))&&(this._file=i,this.fire("change",this,"file",i,s),this.reload())}get file(){return this._file}set data(t){const e=this._data;this._data=t,t!==e&&(this.fire("change",this,"data",t,e),this.loaded&&this.registry._loader.patch(this,this.registry))}get data(){return this._data}set resource(t){const e=this._resources[0];this._resources[0]=t,this.fire("change",this,"resource",t,e)}get resource(){return this._resources[0]}set resources(t){const e=this._resources;this._resources=t,this.fire("change",this,"resources",t,e)}get resources(){return this._resources}set preload(t){t=!!t,this._preload!==t&&(this._preload=t,this._preload&&!this.loaded&&!this.loading&&this.registry&&this.registry.load(this))}get preload(){return this._preload}set loadFaces(t){t=!!t,this.hasOwnProperty("_loadFaces")&&t===this._loadFaces||(this._loadFaces=t,this.loaded&&this.registry._loader.patch(this,this.registry))}get loadFaces(){return this._loadFaces}getFileUrl(){const t=this.file;if(!t||!t.url)return null;let e=t.url;if(this.registry&&this.registry.prefix&&!r.test(e)&&(e=this.registry.prefix+e),"script"!==this.type&&t.hash){const s=-1!==e.indexOf("?")?"&":"?";e+=`${s}t=${t.hash}`}return e}getAbsoluteUrl(e){if(e.startsWith("blob:")||e.startsWith("data:"))return e;const s=t.getDirectory(this.file.url);return t.join(s,e)}getLocalizedAssetId(t){return t=i(t,this._i18n),this._i18n[t]||null}addLocalizedAssetId(t,e){this._i18n[t]=e,this.fire("add:localized",t,e)}removeLocalizedAssetId(t){const e=this._i18n[t];e&&(delete this._i18n[t],this.fire("remove:localized",t,e))}ready(t,e){e=e||this,this.loaded?t.call(e,this):this.once("load",(s=>{t.call(e,s)}))}reload(){this.loaded&&(this.loaded=!1,this.registry.load(this))}unload(){if(!this.loaded&&0===this._resources.length)return;this.fire("unload",this),this.registry.fire(`unload:${this.id}`,this);const t=this._resources;this.urlObject&&(URL.revokeObjectURL(this.urlObject),this.urlObject=null),this.resources=[],this.loaded=!1,this.file&&this.registry._loader.clearCache(this.getFileUrl(),this.type);for(let e=0;e<t.length;++e){const s=t[e];s&&s.destroy&&s.destroy()}}static fetchArrayBuffer(t,e,s,i=0){var r;null!=s&&null!=(r=s.file)&&r.contents?setTimeout((()=>{e(null,s.file.contents)})):h.get(t,{cache:!0,responseType:"arraybuffer",retry:i>0,maxRetries:i},e)}}c.EVENT_LOAD="load",c.EVENT_UNLOAD="unload",c.EVENT_REMOVE="remove",c.EVENT_ERROR="error",c.EVENT_CHANGE="change",c.EVENT_ADDLOCALIZED="add:localized",c.EVENT_REMOVELOCALIZED="remove:localized";export{c as Asset};