浏览代码

extract tga tools

sebavan 6 年之前
父节点
当前提交
5686cd4a18
共有 3 个文件被更改,包括 53 次插入52 次删除
  1. 1 1
      src/Materials/Textures/Procedurals/customProceduralTexture.ts
  2. 0 48
      src/Misc/tools.ts
  3. 52 3
      src/Offline/database.ts

+ 1 - 1
src/Materials/Textures/Procedurals/customProceduralTexture.ts

@@ -54,7 +54,7 @@ import { ProceduralTexture } from "./proceduralTexture";
 
             xhr.open("GET", configFileUrl, true);
             xhr.addEventListener("load", () => {
-                if (xhr.status === 200 || Tools.ValidateXHRData(xhr, 1)) {
+                if (xhr.status === 200 || (xhr.responseText && xhr.responseText.length > 0)) {
                     try {
                         this._config = JSON.parse(xhr.response);
 

+ 0 - 48
src/Misc/tools.ts

@@ -6,7 +6,6 @@ import { IOfflineProvider } from "../Offline/IOfflineProvider";
 import { Camera } from "../Cameras/camera";
 import { Observable } from "./observable";
 import { FilesInput } from "./filesInput";
-import { TGATools } from "./tga";
 import { Constants } from "../Engines/constants";
 import { DomManagement } from "./domManagement";
 import { Logger } from "./logger";
@@ -1374,53 +1373,6 @@ declare type Engine = import("../Engines/engine").Engine;
         }
 
         /**
-         * Validates if xhr data is correct
-         * @param xhr defines the request to validate
-         * @param dataType defines the expected data type
-         * @returns true if data is correct
-         */
-        public static ValidateXHRData(xhr: XMLHttpRequest, dataType = 7): boolean {
-            // 1 for text (.babylon, manifest and shaders), 2 for TGA, 4 for DDS, 7 for all
-
-            try {
-                if (dataType & 1) {
-                    if (xhr.responseText && xhr.responseText.length > 0) {
-                        return true;
-                    } else if (dataType === 1) {
-                        return false;
-                    }
-                }
-
-                if (dataType & 2) {
-                    // Check header width and height since there is no "TGA" magic number
-                    var tgaHeader = TGATools.GetTGAHeader(xhr.response);
-
-                    if (tgaHeader.width && tgaHeader.height && tgaHeader.width > 0 && tgaHeader.height > 0) {
-                        return true;
-                    } else if (dataType === 2) {
-                        return false;
-                    }
-                }
-
-                if (dataType & 4) {
-                    // Check for the "DDS" magic number
-                    var ddsHeader = new Uint8Array(xhr.response, 0, 3);
-
-                    if (ddsHeader[0] === 68 && ddsHeader[1] === 68 && ddsHeader[2] === 83) {
-                        return true;
-                    } else {
-                        return false;
-                    }
-                }
-
-            } catch (e) {
-                // Global protection
-            }
-
-            return false;
-        }
-
-        /**
          * Implementation from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
          * Be aware Math.random() could cause collisions, but:
          * "All but 6 of the 128 bits of the ID are randomly generated, which means that for any two ids, there's a 1 in 2^^122 (or 5.3x10^^36) chance they'll collide"

+ 52 - 3
src/Offline/database.ts

@@ -1,8 +1,10 @@
+import { Nullable } from "../types";
 import { Tools } from "../Misc/tools";
 import { Logger } from "../Misc/logger";
-import { Nullable } from "../types";
+import { TGATools } from '../Misc/tga';
 import { Engine } from "../Engines/engine";
 import { IOfflineProvider } from "./IOfflineProvider";
+
     // Sets the default offline provider to Babylon.js
     Engine.OfflineProviderFactory = (urlToScene: string, callbackManifestChecked: (checked: boolean) => any, disableManifestCheck = false) => { return new Database(urlToScene, callbackManifestChecked, disableManifestCheck); };
 
@@ -117,7 +119,7 @@ import { IOfflineProvider } from "./IOfflineProvider";
             xhr.open("GET", manifestURL, true);
 
             xhr.addEventListener("load", () => {
-                if (xhr.status === 200 || Tools.ValidateXHRData(xhr, 1)) {
+                if (xhr.status === 200 || Database._ValidateXHRData(xhr, 1)) {
                     try {
                         var manifestFile = JSON.parse(xhr.response);
                         this._enableSceneOffline = manifestFile.enableSceneOffline;
@@ -601,7 +603,7 @@ import { IOfflineProvider } from "./IOfflineProvider";
                 }
 
                 xhr.addEventListener("load", () => {
-                    if (xhr.status === 200 || (xhr.status < 400 && Tools.ValidateXHRData(xhr, !useArrayBuffer ? 1 : 6))) {
+                    if (xhr.status === 200 || (xhr.status < 400 && Database._ValidateXHRData(xhr, !useArrayBuffer ? 1 : 6))) {
                         // Blob as response (XHR2)
                         fileData = !useArrayBuffer ? xhr.responseText : xhr.response;
 
@@ -676,4 +678,51 @@ import { IOfflineProvider } from "./IOfflineProvider";
                 callback();
             }
         }
+
+        /**
+         * Validates if xhr data is correct
+         * @param xhr defines the request to validate
+         * @param dataType defines the expected data type
+         * @returns true if data is correct
+         */
+        private static _ValidateXHRData(xhr: XMLHttpRequest, dataType = 7): boolean {
+            // 1 for text (.babylon, manifest and shaders), 2 for TGA, 4 for DDS, 7 for all
+
+            try {
+                if (dataType & 1) {
+                    if (xhr.responseText && xhr.responseText.length > 0) {
+                        return true;
+                    } else if (dataType === 1) {
+                        return false;
+                    }
+                }
+
+                if (dataType & 2) {
+                    // Check header width and height since there is no "TGA" magic number
+                    var tgaHeader = TGATools.GetTGAHeader(xhr.response);
+
+                    if (tgaHeader.width && tgaHeader.height && tgaHeader.width > 0 && tgaHeader.height > 0) {
+                        return true;
+                    } else if (dataType === 2) {
+                        return false;
+                    }
+                }
+
+                if (dataType & 4) {
+                    // Check for the "DDS" magic number
+                    var ddsHeader = new Uint8Array(xhr.response, 0, 3);
+
+                    if (ddsHeader[0] === 68 && ddsHeader[1] === 68 && ddsHeader[2] === 83) {
+                        return true;
+                    } else {
+                        return false;
+                    }
+                }
+
+            } catch (e) {
+                // Global protection
+            }
+
+            return false;
+        }
     }