All files / src/exporter/asset CocosCreator.ts

86.27% Statements 44/51
54.17% Branches 13/24
100% Functions 11/11
86.67% Lines 39/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 1381x                   1x         1x 2x           3x             3x   3x 3x 3x 3x 3x 3x 3x         3x               3x       3x     3x               3x 1x     2x 2x 2x                           1x               3x 1x 1x 1x 1x 1x 1x     1x                   3x   1x 1x             1x       3x   3x   3x           1x  
import * as path from 'path';
 
import { SchemaJson, Node } from '@drecom/scene-graph-schema';
import AssetExporter from '../../interface/AssetExporter';
import AssetExporterPlugin from '../../interface/AssetExporterPlugin';
import AssetExportMapEntity from '../../interface/AssetExportMapEntity';
 
/**
 * CocosCreator scene exporter
 */
export default class CocosCreator implements AssetExporter {
 
  /**
   * Returns runtime identifier string.
   */
  public getIdentifier(): string {
    return 'cocoscreator';
  }
 
  /**
   * Create asset export map.
   */
  public createExportMap(
    sceneGraphMap: Map<string, SchemaJson>,
    assetRoot: string,
    destDir: string,
    urlNameSpace: string,
    plugins?: Map<string, AssetExporterPlugin>
  ): Map<string, AssetExportMapEntity> {
    const exportMap = new Map<string, AssetExportMapEntity>();
 
    sceneGraphMap.forEach((graph) => {
      const scene = graph.scene;
      for (let i = 0; i < scene.length; i++) {
        const node = scene[i];
        Eif (node.sprite) {
          Eif (node.sprite.url) {
            exportMap.set(
              node.sprite.url,
              this.createExportMapEntity(node.sprite.url, assetRoot, destDir, urlNameSpace)
            );
          }
          Iif (node.sprite.atlasUrl) {
            exportMap.set(
              node.sprite.atlasUrl,
              this.createExportMapEntity(node.sprite.atlasUrl, assetRoot, destDir, urlNameSpace)
            );
          }
        }
 
        this.pluginPostProcess(node, exportMap, assetRoot, destDir, urlNameSpace, plugins);
      }
    });
 
    return exportMap;
  }
 
  public pluginPostProcess(
    node: Node,
    exportMap: Map<string, AssetExportMapEntity>,
    assetRoot: string,
    destDir: string,
    urlNameSpace: string,
    plugins?: Map<string, AssetExporterPlugin>
  ): void {
    if (!plugins) {
      return;
    }
 
    plugins.forEach((plugin) => {
      const paths = plugin.getExportMapExtendPaths(node);
      Eif (paths.length === 0) return;
 
      for (let j = 0; j < paths.length; j++) {
        exportMap.set(
          paths[j],
          this.createExportMapEntity(paths[j], assetRoot, destDir, urlNameSpace)
        );
      }
    });
  }
 
  /**
   * Replace paths in scene graph from absolute local path to relative path/url.
   */
  public replacePaths(
    sceneGraphMap: Map<string, SchemaJson>,
    exportMap: Map<string, AssetExportMapEntity>,
    plugins?: Map<string, AssetExporterPlugin>
  ): void {
    /**
     * replace local path with url
     */
    sceneGraphMap.forEach((sceneGraph) => {
      sceneGraph.scene.forEach((node) => {
        Eif (node.sprite) {
          Eif (node.sprite.url) {
            const entity = exportMap.get(node.sprite.url);
            Eif (entity) {
              node.sprite.url = entity.url;
            }
          }
          Iif (node.sprite.atlasUrl) {
            const entity = exportMap.get(node.sprite.atlasUrl);
            if (entity) {
              node.sprite.atlasUrl = entity.url;
            }
          }
        }
      });
    });
 
    if (!plugins) return;
 
    plugins.forEach((plugin) => {
      plugin.replaceExtendedPaths(sceneGraphMap, exportMap);
    });
  }
 
  /**
   * Create asset export map entity.
   */
  private createExportMapEntity(
    basePath: string,
    assetRoot: string,
    destDir: string,
    IurlNameSpace: string = ''
  ): AssetExportMapEntity {
    const relativePath = basePath.replace(RegExp(`\^${assetRoot}`), '');
 
    return {
      localSrcPath: basePath,
      localDestPath: path.join(destDir, relativePath),
      url: path.join(urlNameSpace, relativePath)
    };
  }
}