Browse Source

Merge pull request #7325 from sebavan/master

FileTools optionnal -> 7kb
sebavan 5 years ago
parent
commit
a5ef1d9fe7

+ 22 - 2
src/Engines/Processors/shaderProcessor.ts

@@ -8,7 +8,12 @@ import { ShaderDefineAndOperator } from './Expressions/Operators/shaderDefineAnd
 import { ShaderDefineExpression } from './Expressions/shaderDefineExpression';
 import { ShaderDefineArithmeticOperator } from './Expressions/Operators/shaderDefineArithmeticOperator';
 import { ProcessingOptions } from './shaderProcessingOptions';
-import { FileTools } from '../../Misc/fileTools';
+import { _DevTools } from '../../Misc/devTools';
+
+declare type WebRequest = import("../../Misc/webRequest").WebRequest;
+declare type LoadFileError = import("../../Misc/fileTools").LoadFileError;
+declare type IOfflineProvider = import("../../Offline/IOfflineProvider").IOfflineProvider;
+declare type IFileRequest  = import("../../Misc/fileRequest").IFileRequest;
 
 /** @hidden */
 export class ShaderProcessor {
@@ -331,7 +336,7 @@ export class ShaderProcessor {
             } else {
                 var includeShaderUrl = options.shadersRepository + "ShadersInclude/" + includeFile + ".fx";
 
-                FileTools.LoadFile(includeShaderUrl, (fileContent) => {
+                ShaderProcessor._FileToolsLoadFile(includeShaderUrl, (fileContent) => {
                     options.includesShadersStore[includeFile] = fileContent as string;
                     this._ProcessIncludes(<string>returnValue, options, callback);
                 });
@@ -343,4 +348,19 @@ export class ShaderProcessor {
 
         callback(returnValue);
     }
+
+    /**
+     * Loads a file from a url
+     * @param url url to load
+     * @param onSuccess callback called when the file successfully loads
+     * @param onProgress callback called while file is loading (if the server supports this mode)
+     * @param offlineProvider defines the offline provider for caching
+     * @param useArrayBuffer defines a boolean indicating that date must be returned as ArrayBuffer
+     * @param onError callback called when the file fails to load
+     * @returns a file request object
+     * @hidden
+     */
+    public static _FileToolsLoadFile(url: string, onSuccess: (data: string | ArrayBuffer, responseURL?: string) => void, onProgress?: (ev: ProgressEvent) => void, offlineProvider?: IOfflineProvider, useArrayBuffer?: boolean, onError?: (request?: WebRequest, exception?: LoadFileError) => void): IFileRequest {
+        throw  _DevTools.WarnImport("FileTools");
+    }
 }

+ 34 - 4
src/Engines/thinEngine.ts

@@ -23,13 +23,14 @@ import { IPipelineContext } from './IPipelineContext';
 import { WebGLPipelineContext } from './WebGL/webGLPipelineContext';
 import { VertexBuffer } from '../Meshes/buffer';
 import { InstancingAttributeInfo } from './instancingAttributeInfo';
-import { FileTools } from '../Misc/fileTools';
 import { BaseTexture } from '../Materials/Textures/baseTexture';
 import { IOfflineProvider } from '../Offline/IOfflineProvider';
 import { IEffectFallbacks } from '../Materials/iEffectFallbacks';
 import { IWebRequest } from '../Misc/interfaces/iWebRequest';
 import { CanvasGenerator } from '../Misc/canvasGenerator';
 
+declare type WebRequest = import("../Misc/webRequest").WebRequest;
+declare type LoadFileError = import("../Misc/fileTools").LoadFileError;
 declare type Observer<T> = import("../Misc/observable").Observer<T>;
 declare type VideoTexture = import("../Materials/Textures/videoTexture").VideoTexture;
 declare type RenderTargetTexture = import("../Materials/Textures/renderTargetTexture").RenderTargetTexture;
@@ -2952,11 +2953,11 @@ export class ThinEngine {
                 if (buffer && ((<HTMLImageElement>buffer).decoding || (<ImageBitmap>buffer).close)) {
                     onload(<HTMLImageElement>buffer);
                 } else {
-                    FileTools.LoadImage(url, onload, onInternalError, scene ? scene.offlineProvider : null, mimeType);
+                    ThinEngine._FileToolsLoadImage(url, onload, onInternalError, scene ? scene.offlineProvider : null, mimeType);
                 }
             }
             else if (typeof buffer === "string" || buffer instanceof ArrayBuffer || ArrayBuffer.isView(buffer) || buffer instanceof Blob) {
-                FileTools.LoadImage(buffer, onload, onInternalError, scene ? scene.offlineProvider : null, mimeType);
+                ThinEngine._FileToolsLoadImage(buffer, onload, onInternalError, scene ? scene.offlineProvider : null, mimeType);
             }
             else if (buffer) {
                 onload(<HTMLImageElement>buffer);
@@ -2967,6 +2968,20 @@ export class ThinEngine {
     }
 
     /**
+     * Loads an image as an HTMLImageElement.
+     * @param input url string, ArrayBuffer, or Blob to load
+     * @param onLoad callback called when the image successfully loads
+     * @param onError callback called when the image fails to load
+     * @param offlineProvider offline provider for caching
+     * @param mimeType optional mime type
+     * @returns the HTMLImageElement of the loaded image
+     * @hidden
+     */
+    public static _FileToolsLoadImage(input: string | ArrayBuffer | ArrayBufferView | Blob, onLoad: (img: HTMLImageElement | ImageBitmap) => void, onError: (message?: string, exception?: any) => void, offlineProvider: Nullable<IOfflineProvider>, mimeType?: string): Nullable<HTMLImageElement> {
+        throw _DevTools.WarnImport("FileTools");
+    }
+
+    /**
      * @hidden
      */
     public _rescaleTexture(source: InternalTexture, destination: InternalTexture, scene: Nullable<any>, internalFormat: number, onComplete: () => void): void {
@@ -4167,7 +4182,7 @@ export class ThinEngine {
     /** @hidden */
     public _loadFile(url: string, onSuccess: (data: string | ArrayBuffer, responseURL?: string) => void, onProgress?: (data: any) => void,
         offlineProvider?: IOfflineProvider, useArrayBuffer?: boolean, onError?: (request?: IWebRequest, exception?: any) => void): IFileRequest {
-        let request = FileTools.LoadFile(url, onSuccess, onProgress, offlineProvider, useArrayBuffer, onError);
+        let request = ThinEngine._FileToolsLoadFile(url, onSuccess, onProgress, offlineProvider, useArrayBuffer, onError);
         this._activeRequests.push(request);
         request.onCompleteObservable.add((request) => {
             this._activeRequests.splice(this._activeRequests.indexOf(request), 1);
@@ -4176,6 +4191,21 @@ export class ThinEngine {
     }
 
     /**
+     * Loads a file from a url
+     * @param url url to load
+     * @param onSuccess callback called when the file successfully loads
+     * @param onProgress callback called while file is loading (if the server supports this mode)
+     * @param offlineProvider defines the offline provider for caching
+     * @param useArrayBuffer defines a boolean indicating that date must be returned as ArrayBuffer
+     * @param onError callback called when the file fails to load
+     * @returns a file request object
+     * @hidden
+     */
+    public static _FileToolsLoadFile(url: string, onSuccess: (data: string | ArrayBuffer, responseURL?: string) => void, onProgress?: (ev: ProgressEvent) => void, offlineProvider?: IOfflineProvider, useArrayBuffer?: boolean, onError?: (request?: WebRequest, exception?: LoadFileError) => void): IFileRequest {
+        throw  _DevTools.WarnImport("FileTools");
+    }
+
+    /**
      * Reads pixels from the current frame buffer. Please note that this function can be slow
      * @param x defines the x coordinate of the rectangle where pixels must be read
      * @param y defines the y coordinate of the rectangle where pixels must be read

+ 2 - 0
src/Materials/Textures/baseTexture.ts

@@ -11,6 +11,8 @@ import { IAnimatable } from '../../Animations/animatable.interface';
 import { GUID } from '../../Misc/guid';
 import { ISize, Size } from '../../Maths/math.size';
 
+import "../../Misc/fileTools";
+
 declare type Animation = import("../../Animations/animation").Animation;
 
 /**

+ 23 - 17
src/Misc/fileTools.ts

@@ -8,6 +8,8 @@ import { FilesInputStore } from './filesInputStore';
 import { RetryStrategy } from './retryStrategy';
 import { BaseError } from './baseError';
 import { StringTools } from './stringTools';
+import { ThinEngine } from '../Engines/thinEngine';
+import { ShaderProcessor } from '../Engines/Processors/shaderProcessor';
 
 /** @ignore */
 export class LoadFileError extends BaseError {
@@ -110,12 +112,12 @@ export class FileTools {
             return;
         }
 
-        if (this.CorsBehavior) {
-            if (typeof (this.CorsBehavior) === 'string' || this.CorsBehavior instanceof String) {
-                element.crossOrigin = <string>this.CorsBehavior;
+        if (FileTools.CorsBehavior) {
+            if (typeof (FileTools.CorsBehavior) === 'string' || this.CorsBehavior instanceof String) {
+                element.crossOrigin = <string>FileTools.CorsBehavior;
             }
             else {
-                var result = this.CorsBehavior(url);
+                var result = FileTools.CorsBehavior(url);
                 if (result) {
                     element.crossOrigin = result;
                 }
@@ -149,12 +151,12 @@ export class FileTools {
             usingObjectURL = true;
         }
         else {
-            url = this._CleanUrl(input);
-            url = this.PreprocessUrl(input);
+            url = FileTools._CleanUrl(input);
+            url = FileTools.PreprocessUrl(input);
         }
 
         if (typeof Image === "undefined") {
-            this.LoadFile(url, (data) => {
+            FileTools.LoadFile(url, (data) => {
                 createImageBitmap(new Blob([data])).then((imgBmp) => {
                     onLoad(imgBmp);
                     if (usingObjectURL) {
@@ -175,7 +177,7 @@ export class FileTools {
         }
 
         var img = new Image();
-        this.SetCorsBehavior(url, img);
+        FileTools.SetCorsBehavior(url, img);
 
         const loadHandler = () => {
             img.removeEventListener("load", loadHandler);
@@ -307,11 +309,11 @@ export class FileTools {
             }
             const file = FilesInputStore.FilesToLoad[fileName];
             if (file) {
-                return this.ReadFile(file, onSuccess, onProgress, useArrayBuffer, onError ? (error) => onError(undefined, new LoadFileError(error.message, error.file)) : undefined);
+                return FileTools.ReadFile(file, onSuccess, onProgress, useArrayBuffer, onError ? (error) => onError(undefined, new LoadFileError(error.message, error.file)) : undefined);
             }
         }
 
-        return this.RequestFile(url, (data, request) => {
+        return FileTools.RequestFile(url, (data, request) => {
             onSuccess(data, request ? request.responseURL : undefined);
         }, onProgress, offlineProvider, useArrayBuffer, onError ? (error) => {
             onError(error.request, new LoadFileError(error.message, error.request));
@@ -329,10 +331,10 @@ export class FileTools {
      * @returns a file request object
      */
     public static RequestFile(url: string, onSuccess: (data: string | ArrayBuffer, request?: WebRequest) => void, onProgress?: (event: ProgressEvent) => void, offlineProvider?: IOfflineProvider, useArrayBuffer?: boolean, onError?: (error: RequestFileError) => void, onOpened?: (request: WebRequest) => void): IFileRequest {
-        url = this._CleanUrl(url);
-        url = this.PreprocessUrl(url);
+        url = FileTools._CleanUrl(url);
+        url = FileTools.PreprocessUrl(url);
 
-        const loadUrl = this.BaseUrl + url;
+        const loadUrl = FileTools.BaseUrl + url;
 
         let aborted = false;
         const fileRequest: IFileRequest = {
@@ -390,12 +392,12 @@ export class FileTools {
                         // Some browsers have issues where onreadystatechange can be called multiple times with the same value.
                         request.removeEventListener("readystatechange", onReadyStateChange);
 
-                        if ((request.status >= 200 && request.status < 300) || (request.status === 0 && (!DomManagement.IsWindowObjectExist() || this.IsFileURL()))) {
+                        if ((request.status >= 200 && request.status < 300) || (request.status === 0 && (!DomManagement.IsWindowObjectExist() || FileTools.IsFileURL()))) {
                             onSuccess(useArrayBuffer ? request.response : request.responseText, request);
                             return;
                         }
 
-                        let retryStrategy = this.DefaultRetryStrategy;
+                        let retryStrategy = FileTools.DefaultRetryStrategy;
                         if (retryStrategy) {
                             let waitTime = retryStrategy(loadUrl, request, retryIndex);
                             if (waitTime !== -1) {
@@ -438,7 +440,7 @@ export class FileTools {
                 // TODO: database needs to support aborting and should return a IFileRequest
 
                 if (offlineProvider) {
-                    offlineProvider.loadFile(this.BaseUrl + url, (data) => {
+                    offlineProvider.loadFile(FileTools.BaseUrl + url, (data) => {
                         if (!aborted) {
                             onSuccess(data);
                         }
@@ -468,4 +470,8 @@ export class FileTools {
     public static IsFileURL(): boolean {
         return location.protocol === "file:";
     }
-}
+}
+
+ThinEngine._FileToolsLoadImage = FileTools.LoadImage.bind(FileTools);
+ThinEngine._FileToolsLoadFile = FileTools.LoadFile.bind(FileTools);
+ShaderProcessor._FileToolsLoadFile = FileTools.LoadFile.bind(FileTools);