All files / src/exporter ExportManager.ts

60.87% Statements 28/46
50% Branches 5/10
83.33% Functions 5/6
59.09% Lines 26/44

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                    1x 1x             1x               1x         1x           1x                           1x 1x 1x 1x 1x   1x 1x                                         1x         2x 2x 1x     1x 1x                       1x             2x 2x 1x     1x 1x                     1x  
import { SchemaJson } from '@drecom/scene-graph-schema';
import SceneExporterPlugin from '../interface/SceneExporterPlugin';
import AssetExporterPlugin from '../interface/AssetExporterPlugin';
import SceneExporterConstructor from '../interface/SceneExporterConstructor';
import AssetExporterConstructor from '../interface/AssetExporterConstructor';
import AssetExportMapEntity from '../interface/AssetExportMapEntity';
 
/**
 * Bundles each export processes and manages running them.
 */
export default class ExportManager {
  private static exporters = new Map<string, {
    scene: SceneExporterConstructor,
    asset: AssetExporterConstructor
  }>();
  /**
   * Plugins placeholder
   */
  private plugins = {
    assets: new Map<string, AssetExporterPlugin>(),
    scenes: new Map<string, SceneExporterPlugin>()
  };
 
  /**
   * Register exporter class implements
   */
  public static registerExporterClass(
    runtimeId: string,
    scene: SceneExporterConstructor,
    asset: AssetExporterConstructor
  ): void {
    ExportManager.exporters.set(runtimeId.toLowerCase(), { scene, asset });
  }
 
  /**
   * Returnes registered keys of exporters
   */
  public static getRegisteredExporterRuntimes(): string[] {
    const runtimes = [];
    const it = ExportManager.exporters.keys();
    let item = it.next();
    while (!item.done) {
      runtimes.push(item.value);
      item = it.next();
    }
    return runtimes;
  }
 
  /**
   * Dynamically loads user defined plugin by absolute module path
   */
  public loadPlugins(plugins: string[] | AssetExporterPlugin[] | SceneExporterPlugin[]): void {
    for (let i = 0; i < plugins.length; i++) {
      const plugin: string | AssetExporterPlugin | SceneExporterPlugin = plugins[i];
      let instance;
      let pluginName;
 
      Eif (typeof plugin === 'string') {
        const Plugin = require(plugins[i] as string).default;
        instance = new Plugin() as AssetExporterPlugin | SceneExporterPlugin;
        pluginName = Plugin.name;
      } else {
        instance = plugin as AssetExporterPlugin | SceneExporterPlugin;
        pluginName = plugin.constructor.name;
      }
 
      if ((instance as AssetExporterPlugin).replaceExtendedPaths) {
        this.plugins.assets.set(pluginName, instance as AssetExporterPlugin);
      }
      // plugin implementations can be unified
      if ((instance as SceneExporterPlugin).extendSceneGraph) {
        this.plugins.scenes.set(pluginName, instance as SceneExporterPlugin);
      }
    }
  }
 
  /**
   * Exports scene graphs for given scene file paths
   */
  public exportScene(
    runtimeIdentifier: string,
    sceneFiles: string[],
    assetRoot: string
  ): Map<string, SchemaJson> {
    const exporters = ExportManager.exporters.get(runtimeIdentifier);
    if (!exporters) {
      throw new Error(`runtime '${runtimeIdentifier}' is not supported.`);
    }
 
    const exporter = new exporters.scene();
    const sceneGraphs = exporter.createSceneGraphSchemas(
      sceneFiles,
      assetRoot,
      this.plugins.scenes
    );
 
    return sceneGraphs;
  }
 
  /**
   * Create map for exporting assets
   */
  public exportAsset(
    sceneGraphs: Map<string, SchemaJson>,
    runtimeIdentifier: string,
    assetRoot: string,
    destDir: string,
    urlNameSpace: string
  ): Map<string, AssetExportMapEntity> {
    const exporters = ExportManager.exporters.get(runtimeIdentifier);
    if (!exporters) {
      throw new Error(`runtime '${runtimeIdentifier}' is not supported.`);
    }
 
    const exporter  = new exporters.asset();
    const exportMap = exporter.createExportMap(
      sceneGraphs,
      assetRoot,
      destDir,
      urlNameSpace,
      this.plugins.assets
    );
    exporter.replacePaths(sceneGraphs, exportMap, this.plugins.assets);
 
    return exportMap;
  }
}