Skip to content

Commit

Permalink
packaging: More asset deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Feb 4, 2025
1 parent 98f49ce commit d0a7bf8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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);

Expand All @@ -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<Transform, Type, IPackageFile, int> componentAction, Action<GameObject, IPackageFile> 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
Expand Down Expand Up @@ -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<List<SwitchMapping>>(globalStorage.GetFile(PackageApi.SwitchesFile, PackageApi.Packer.FileExtension).GetData()),
Coils = PackageApi.Packer.Unpack<List<CoilMapping>>(globalStorage.GetFile(PackageApi.CoilsFile, PackageApi.Packer.FileExtension).GetData()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ public partial class ScriptableObjectPackable
/// This links the asset to the materials that use it.
/// </summary>
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,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using UnityEngine;
using VisualPinball.Unity.Editor.Packaging;
Expand Down Expand Up @@ -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<ScriptableObjectPackable>(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;
}
Expand Down

0 comments on commit d0a7bf8

Please sign in to comment.