bianjiang 1 tahun lalu
induk
melakukan
be72f2f909
1 mengubah file dengan 90 tambahan dan 102 penghapusan
  1. 90 102
      src/comm/controllers/appScreenMsgCtrl.ts

+ 90 - 102
src/comm/controllers/appScreenMsgCtrl.ts

@@ -51,48 +51,27 @@ export class AppScreenMsgController extends Controller {
       this.exportLocalWebPack(assetDtl);
     }
   }
-  // loadHttp(url: string) {
-  //   const req = new XMLHttpRequest();
-  //   return new Promise((resolve, reject) => {
-  //     req.onload = function () {
-  //       if (req.status == 200) {
-  //         resolve(req.response);
-  //       }
-  //     };
-  //     // req.onreadystatechange = function() {
-  //     //   if (req.readyState == 4 && req.status == 200) {
-  //     //     resolve(req.response);
-  //     //   }
-  //     // }
-  //     req.timeout = 1000 * 60 * 3;
-  //     req.responseType = "blob";
-  //     req.open("GET", url, true);
-  //     req.send(null);
-  //   });
-  // }
 
   loadfile(dir: string, url: string) {
     if (!url) return;
     const { deviceCtrl } = useCtx();
     let names = url.split("/");
     let name = names[names.length - 1];
-
     const screenDir = deviceCtrl.appDataDir + "/screen";
     const filepath = screenDir + dir + name;
-    deviceCtrl.DownloadFile(url, filepath);
     return {
       file: dir + name,
-      // promise: new Promise((resolve, reject) => {
-      //   // scope.assetCtroller.db.ctx.http.request(url, {method:"GET",baseURL:"", noAuth:true, responseType:"blob", originBody:true})
-      //   this.loadHttp(url).then((blob: any) => {
-      //     resolve({ file: dir.substring(2) + name, blob: blob });
-      //   });
-      // }),
+      promise: new Promise((resolve) => {
+        deviceCtrl.DownloadFile(url, filepath).then((ok) => {
+          setTimeout(() => {
+            resolve({ name: name, ok: ok });
+          }, 0);
+        });
+      }),
     };
   }
+
   async exportLocalWebPack(asset: any) {
-    // this.exportLocalEnv3ds();
-    // return;
     queenApi.showLoading("资源下载中");
     const { deviceCtrl } = useCtx();
 
@@ -115,83 +94,101 @@ export class AppScreenMsgController extends Controller {
     let osgbinUrl = geom.osgjs.url;
     source.geoms = [geom];
     let f = this.loadfile(wk, geom.osgjs.url);
-    // files.push(f?.promise);
+    files.push(f?.promise);
     geom.osgjs.url = f?.file;
 
     osgbinUrl = osgbinUrl.substring(0, osgbinUrl.length - 5) + "bin";
     f = this.loadfile(wk, osgbinUrl);
-    // files.push(f?.promise);
-
+    files.push(f?.promise);
     f = this.loadfile(wk, geom.shadow.url);
     if (f) {
+      files.push(f?.promise);
       geom.shadow.url = f.file;
-      // files.push(f.promise);
     }
     delete geom.file;
     delete geom.glb;
 
     //材质处理
-    source.mats.forEach((m: any) => {
-      //base map
+    for (let i = 0; i < source.mats.length; i++) {
+      const m = source.mats[i];
+      //base
       let f = scope.loadfile(wk, m.albedo.texture?.url);
       if (f) {
-        // files.push(f.promise);
-        m.albedo.texture.url = f.file;
+        files.push(f?.promise);
+        source.mats[i].albedo.texture.url = f.file;
       }
-
       //法线
       if (m.normal?.texture) {
         f = scope.loadfile(wk, m.normal.texture?.url);
         if (f) {
-          // files.push(f.promise);
-          m.normal.texture.url = f.file;
+          files.push(f?.promise);
+          source.mats[i].normal.texture.url = f.file;
         }
       }
-
       //粗糙度
       if (m.roughness.texture) {
         f = scope.loadfile(wk, m.roughness.texture.url);
         if (f) {
-          // files.push(f.promise);
-          m.roughness.texture.url = f.file;
+          files.push(f?.promise);
+          source.mats[i].roughness.texture.url = f.file;
         }
       }
-
       //金属度
       if (m.metalness.texture) {
         f = scope.loadfile(wk, m.metalness.texture.url);
         if (f) {
-          // files.push(f.promise);
-          m.metalness.texture.url = f.file;
+          files.push(f?.promise);
+          source.mats[i].metalness.texture.url = f.file;
         }
       }
-
       //透明度
       if (m.opacity.texture) {
         f = scope.loadfile(wk, m.opacity.texture.url);
         if (f) {
-          // files.push(f.promise);
-          m.opacity.texture.url = f.file;
+          files.push(f?.promise);
+          source.mats[i].opacity.texture.url = f.file;
         }
       }
-    });
+    }
+
     //封面图
     let thumbnail = pack.thumbnail;
     const thumb = this.loadfile(wk, thumbnail.url);
+    files.push(thumb?.promise);
     pack.thumbnail.url = thumb?.file;
     //环境球
     const wkenv3d = "/assets/env3d/";
+    const env3dIds: any = [];
+    source.scenes.forEach((item: any) => {
+      if (item.envId) {
+        const envItem = source.env3ds.find((e: any) => {
+          const id = e.userData.assetId || e.id;
+          return id == item.envId;
+        });
+        if (envItem) {
+          env3dIds.push(envItem.id);
+        }
+      }
+    });
     source.env3ds.forEach((item: any) => {
       const id = item.userData.assetId || item.id;
-
+      if (!env3dIds.includes(id)) {
+        return false;
+      }
       const wk = wkenv3d + id + "/bg/";
-      if (item.background?.image?.url) {
-        item.background.image.url = wk + getFileName(item.background.image.url);
+      let f = this.loadfile(wk, item.background?.image?.url);
+      if (f) {
+        item.background.image.url = f.file;
+        files.push(f.promise);
       }
       const twk = wkenv3d + id + "/t/";
       item.config.textures.forEach((t: any) => {
         t.images.forEach((im: any) => {
-          im.file = twk + getFileName(im.file);
+          const f = this.loadfile(twk, im.file);
+          if (f) {
+            im.file = f.file;
+            files.push(f.promise);
+          }
         });
       });
     });
@@ -201,58 +198,49 @@ export class AppScreenMsgController extends Controller {
       //@ts-ignore
       const url = c.background?.image?.url;
       if (url) {
-        //@ts-ignore
-        c.background.image.url = wk3 + getFileName(url);
+        const f = this.loadfile(wk3, url);
+        if (f) {
+          //@ts-ignore
+          c.background.image.url = f.file;
+          files.push(f.promise);
+        }
       }
     });
 
     const configFile = JSON.stringify(pack);
-
-    const res = await deviceCtrl.WriteFileText(
-      `${deviceCtrl.appDataDir}/screen/assets/packs/${pack._id}.json`,
-      configFile
-    );
-    const currentCategory = localStorage.getItem("category") || "default";
-    const assetsJson = await deviceCtrl.ReadFileText(
-      `${deviceCtrl.appDataDir}/screen/assets_${currentCategory}.json`
-    );
-    if (assetsJson.text) {
-      const assets = JSON.parse(assetsJson.text);
-      const assetItem = {
-        _id: pack._id,
-        name: pack.name,
-        thumbnail: pack.thumbnail.url,
-        type: "3D",
-        meshId: `/assets/packs/${pack._id}.json`,
-      };
-      assets.push(assetItem);
-      await deviceCtrl.WriteFileText(
-        `${deviceCtrl.appDataDir}/screen/assets_${currentCategory}.json`,
-        JSON.stringify(assets)
+    Promise.all(files).then(async (ressult) => {
+      queenApi.hideLoading();
+      const failRes = ressult.filter((e) => {
+        return e.ok == false;
+      });
+      if (failRes.length > 0) {
+        queenApi.messageError(`${failRes[0].name}等文件下载失败!`);
+        return;
+      }
+      const res = await deviceCtrl.WriteFileText(
+        `${deviceCtrl.appDataDir}/screen/assets/packs/${pack._id}.json`,
+        configFile
       );
-    }
-    queenApi.hideLoading();
-    this.screenModule.actions.getAssetList();
-    // Promise.all(files).then((blobs) => {
-    //   let zip = new JSZip();
-    //   blobs.forEach((blob: any, i) => {
-    //     zip.file(`${blob.file}`, blob.blob, { binary: true });
-    //   });
-    //   zip.file(
-    //     `${pack._id}.json`,
-    //     new Blob([configFile], {
-    //       type: "text/plain",
-    //     })
-    //   );
-    //   const appDataDir = deviceCtrl.appDataDir;
-    //   const zipDir = `${appDataDir}/screen/${pack._id}.zip`;
-    //   zip.generateAsync({ type: "blob" }).then((content: any) => {
-    //     // deviceCtrl.DownloadFile(content, zipDir);
-    //     console.log(zipDir);
-    //     FileSaver.saveAs(content, zipDir);
-    //     queenApi.hideLoading();
-    //     // const ok=deviceCtrl.Unzip(zipDir,)
-    //   });
-    // });
+      const currentCategory = localStorage.getItem("category") || "default";
+      const assetsJson = await deviceCtrl.ReadFileText(
+        `${deviceCtrl.appDataDir}/screen/assets_${currentCategory}.json`
+      );
+      if (assetsJson.text) {
+        const assets = JSON.parse(assetsJson.text);
+        const assetItem = {
+          _id: pack._id,
+          name: pack.name,
+          thumbnail: pack.thumbnail.url,
+          type: "3D",
+          meshId: `/assets/packs/${pack._id}.json`,
+        };
+        assets.push(assetItem);
+        await deviceCtrl.WriteFileText(
+          `${deviceCtrl.appDataDir}/screen/assets_${currentCategory}.json`,
+          JSON.stringify(assets)
+        );
+      }
+      this.screenModule.actions.getAssetList();
+    });
   }
 }