/// <summary>
 /// Creates a new converter for a new table
 /// </summary>
 /// <param name="sourceContainer">Source table container</param>
 /// <param name="fileName">File name of the file being imported</param>
 /// <param name="options">Optional convert options</param>
 public VpxSceneConverter(FileTableContainer sourceContainer, string fileName = "", ConvertOptions options = null)
 {
     _sourceContainer = sourceContainer;
     _sourceTable     = sourceContainer.Table;
     _patcher         = PatcherManager.GetPatcher();
     _patcher?.Set(sourceContainer, fileName, this, this);
     _options = options ?? new ConvertOptions();
 }
        /// <summary>
        /// Creates a converter based on an existing table in the scene.
        /// </summary>
        /// <param name="tableComponent">Existing component</param>
        public VpxSceneConverter(TableComponent tableComponent)
        {
            _options = new ConvertOptions();
            _tableGo = tableComponent.gameObject;
            var playfieldComponent = _tableGo.GetComponentInChildren <PlayfieldComponent>();

            if (!playfieldComponent)
            {
                throw new InvalidOperationException("Cannot find playfield hierarchy.");
            }
            _playfieldGo    = playfieldComponent.gameObject;
            _tableComponent = tableComponent;
            _sourceTable    = new Table(_tableComponent.TableContainer, new TableData());

            // get materials in scene
            var guids = AssetDatabase.FindAssets("t:Material");

            foreach (var guid in guids)
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var material  = AssetDatabase.LoadAssetAtPath <Material>(assetPath);
                if (material != null)
                {
                    _materials[material.name] = material;
                }
            }

            // get group parents in scene
            for (var i = 0; i < _playfieldGo.transform.childCount; i++)
            {
                var child   = _playfieldGo.transform.GetChild(i);
                var childGo = child.gameObject;
                _groupParents[childGo.name] = childGo;
            }

            CreateFileHierarchy();
        }
        public static GameObject ImportIntoScene(FileTableContainer tableContainer, string filename = "", GameObject parent = null, bool applyPatch = true, string tableName = null, Stopwatch sw = null, ConvertOptions options = null)
        {
            sw ??= Stopwatch.StartNew();
            if (tableName == null && !string.IsNullOrEmpty(filename))
            {
                tableName = Path.GetFileNameWithoutExtension(filename);
            }

            // load table
            var loadedIn        = sw.ElapsedMilliseconds;
            var converter       = new VpxSceneConverter(tableContainer, filename, options);
            var tableGameObject = converter.Convert(applyPatch, tableName);
            var convertedIn     = sw.ElapsedMilliseconds;

            // if an object was selected in the editor, make it its parent
            if (parent != null)
            {
                GameObjectUtility.SetParentAndAlign(tableGameObject, parent);
            }

            // register undo system
            Undo.RegisterCreatedObjectUndo(tableGameObject, "Import VPX table file");

            // select imported object
            Selection.activeObject = tableGameObject;

            Logger.Info($"Imported {filename} in {convertedIn}ms (loaded after {loadedIn}ms).");

            return(tableGameObject);
        }
        public static GameObject ImportIntoScene(string path, GameObject parent = null, bool applyPatch = true, string tableName = null, ConvertOptions options = null)
        {
            var sw = Stopwatch.StartNew();

            return(ImportIntoScene(TableLoader.LoadTable(path), Path.GetFileName(path), parent, applyPatch, tableName, sw, options));
        }