diff --git a/VisualPinball.Unity/VisualPinball.Unity.Editor/Packaging/PackageReader.cs b/VisualPinball.Unity/VisualPinball.Unity.Editor/Packaging/PackageReader.cs index 8f0706482..00c32805e 100644 --- a/VisualPinball.Unity/VisualPinball.Unity.Editor/Packaging/PackageReader.cs +++ b/VisualPinball.Unity/VisualPinball.Unity.Editor/Packaging/PackageReader.cs @@ -25,6 +25,7 @@ using UnityEngine; using UnityEngine.SceneManagement; using VisualPinball.Unity.Editor.Packaging; +using VisualPinball.Unity.Packaging; using Logger = NLog.Logger; using Object = UnityEngine.Object; @@ -33,11 +34,12 @@ namespace VisualPinball.Unity.Editor public class PackageReader { private readonly string _vpePath; - private IPackageFolder _tableStorage; + private IPackageFolder _tableFolder; private string _assetPath; private string _tableName; private GameObject _table; private readonly PackNameLookup _typeLookup; + private PackagedFiles _packageFiles; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public PackageReader(string vpePath) @@ -57,6 +59,8 @@ public async Task ImportIntoScene(string tableName) Setup(storage); await ImportModels(); + ReadAssets(); + // create components and update game objects ReadPackables(PackageApi.ItemFolder, (item, type, stream, index) => { // add or update component @@ -91,13 +95,15 @@ public async Task ImportIntoScene(string tableName) private void Setup(IPackageStorage storage) { // open storages - _tableStorage = storage.GetFolder(PackageApi.TableFolder); + _tableFolder = storage.GetFolder(PackageApi.TableFolder); _assetPath = Path.Combine(Application.dataPath, "Resources", _tableName); var n = 0; while (Directory.Exists(_assetPath)) { _assetPath = Path.Combine(Application.dataPath, "Resources", $"{_tableName} ({++n})"); } Directory.CreateDirectory(_assetPath); + + _packageFiles = new PackagedFiles(_tableFolder); } private async Task ImportModels() @@ -107,7 +113,7 @@ private async Task ImportModels() AssetDatabase.StartAssetEditing(); // dump glb - var sceneStream = _tableStorage.GetFile(PackageApi.SceneFile); + var sceneStream = _tableFolder.GetFile(PackageApi.SceneFile); await using var glbFileStream = new FileStream(glbPath, FileMode.Create, FileAccess.Write); await sceneStream.AsStream().CopyToAsync(glbFileStream); @@ -132,9 +138,14 @@ private async Task ImportModels() EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene()); // mark the active scene as dirty so that the changes are saved. } + private void ReadAssets() + { + _packageFiles.UnpackAssets(); + } + private void ReadPackables(string storageRoot, Action componentAction, Action itemAction = null) { - var itemsStorage = _tableStorage.GetFolder(storageRoot); + var itemsStorage = _tableFolder.GetFolder(storageRoot); // -> storageRoot <- / 0.0.0 / CompType / 0 itemsStorage.VisitFolders(itemStorage => { // storageRoot / -> 0.0.0 <- / CompType / 0 @@ -164,7 +175,7 @@ private void ReadGlobals() if (!tableComponent) { throw new Exception("Cannot find table component on table object."); } - var globalStorage = _tableStorage.GetFolder(PackageApi.GlobalFolder); + var globalStorage = _tableFolder.GetFolder(PackageApi.GlobalFolder); tableComponent.MappingConfig = new MappingConfig { Switches = PackageApi.Packer.Unpack>(globalStorage.GetFile(PackageApi.SwitchesFile, PackageApi.Packer.FileExtension).GetData()), Coils = PackageApi.Packer.Unpack>(globalStorage.GetFile(PackageApi.CoilsFile, PackageApi.Packer.FileExtension).GetData()), diff --git a/VisualPinball.Unity/VisualPinball.Unity/Packaging/CommonPackables.cs b/VisualPinball.Unity/VisualPinball.Unity/Packaging/CommonPackables.cs index 0a4f616c0..c5222ab63 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Packaging/CommonPackables.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Packaging/CommonPackables.cs @@ -129,12 +129,14 @@ public partial class ScriptableObjectPackable /// This links the asset to the materials that use it. /// public int InstanceId; + public string Type; public ScriptableObject Object; public static byte[] Pack(ScriptableObject obj) { return PackageApi.Packer.Pack(new ScriptableObjectPackable { InstanceId = obj.GetInstanceID(), + Type = obj.GetType().AssemblyQualifiedName, Object = obj, }); } diff --git a/VisualPinball.Unity/VisualPinball.Unity/Packaging/PackagedFiles.cs b/VisualPinball.Unity/VisualPinball.Unity/Packaging/PackagedFiles.cs index c2699b66a..25eb2d882 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Packaging/PackagedFiles.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Packaging/PackagedFiles.cs @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +using System; using System.Collections.Generic; using UnityEngine; using VisualPinball.Unity.Editor.Packaging; @@ -57,13 +58,34 @@ public void PackAssets() } } + public void UnpackAssets() + { + if (!_tableFolder.TryGetFolder(PackageApi.AssetFolder, out var assetFolder)) { + return; + } + assetFolder.VisitFolders(assetTypeFolder => { + assetTypeFolder.VisitFiles(assetFile => { + + var asset = PackageApi.Packer.Unpack(assetFile.GetData()); + + // var type = Type.GetType(assemblyQualifiedName); + // if (type == null) { + // throw new InvalidOperationException($"Type not found: {assemblyQualifiedName}"); + // } + // object instance = Activator.CreateInstance(type); + // var physicsMaterialAsset = (VisualPinball.Unity.PhysicsMaterialAsset)instance; + + }); + }); + } + private string UniqueName(IPackageFolder folder, string name) { var baseName = name; var i = 1; while (true) { if (folder.TryGetFile(name, out _, PackageApi.Packer.FileExtension)) { - name = $"{baseName}_({++i})"; + name = $"{baseName} ({++i})"; } else { return name; }