240 lines
8.7 KiB
C#
240 lines
8.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.IO;
|
|
using UnityEngine.Events;
|
|
using System.Collections;
|
|
using TriLibCore.SFB;
|
|
using Studio.Util;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
using System.Linq;
|
|
using Studio.Repositories;
|
|
using Studio.AssetTool;
|
|
using Ookii.Dialogs;
|
|
using Studio.Command;
|
|
using Studio.Manage;
|
|
using Studio.AssetLibraryTree;
|
|
|
|
namespace Studio.AssetTool
|
|
{
|
|
public class FBXFileManager : Manager
|
|
{
|
|
public CustomAssetDataRepository dataRepo;
|
|
|
|
public string identifier;
|
|
public string password;
|
|
public UnityEvent<string> onRemoveFbxFile;
|
|
public SaveData saveData;
|
|
|
|
public SharedMaterial sharedMaterial;
|
|
private MessagePackFileManager<SaveData> messagePacker;
|
|
private bool isSaveTaskComplete = true;
|
|
public bool isLoadTaskComplete = true;
|
|
public Transform fbxAnchor;
|
|
public Action onLoadComplete;
|
|
|
|
public FBXFileManager()
|
|
{
|
|
dataRepo = new CustomAssetDataRepository();
|
|
saveData = new SaveData();
|
|
sharedMaterial = new SharedMaterial();
|
|
messagePacker = new MessagePackFileManager<SaveData>();
|
|
messagePacker.Initialize();
|
|
identifier = string.IsNullOrEmpty(identifier) ? "defaultAssetData" : identifier;
|
|
fbxAnchor = new GameObject("LocalAssetDataAnchor").transform;
|
|
}
|
|
public override void Init()
|
|
{
|
|
|
|
}
|
|
|
|
public void LoadLocalFBXDirectory(string folderPath)
|
|
{
|
|
List<string> filePaths = new List<string>();
|
|
var path = folderPath;
|
|
|
|
var files = Directory.GetFiles(path, "*.fbx", SearchOption.AllDirectories);
|
|
|
|
foreach (var file in files)
|
|
{
|
|
if (dataRepo.TryGetDataFromPath(path, out var p))
|
|
continue;
|
|
|
|
filePaths.Add(file);
|
|
}
|
|
|
|
CoroutineRunner.instance.StartCoroutine(LoadAssetFiles(filePaths));
|
|
}
|
|
|
|
IEnumerator LoadAssetFiles(List<string> filePaths)
|
|
{
|
|
foreach(var file in filePaths)
|
|
{
|
|
var assetName = Path.GetFileNameWithoutExtension(file);
|
|
var directoryPath = Path.GetDirectoryName(file);
|
|
var folderName = Path.GetFileName(directoryPath);
|
|
|
|
if (dataRepo.TryGetDataFromName(assetName, out var p))
|
|
continue;
|
|
|
|
GameObject newObject = new GameObject(assetName);
|
|
newObject.transform.parent = fbxAnchor;
|
|
CustomAssetData assetData = newObject.AddComponent<CustomAssetData>();
|
|
assetData.assetName = assetName;
|
|
assetData.folderName = folderName;
|
|
|
|
assetData.LoadLocalFBX(file);
|
|
yield return new WaitUntil(() => (assetData.isLoadComplete == true && assetData.progress == 1) || assetData.isLoadError == true);
|
|
|
|
Texture2D thumbnail = RuntimePreviewGenerator.GenerateModelPreview(assetData.loadedObject.transform, 320, 200);
|
|
thumbnail = TextureUtil.MakeReadableTexture(thumbnail);
|
|
assetData.thumbnail = thumbnail;
|
|
|
|
OnLoadComplete(assetData);
|
|
}
|
|
onLoadComplete?.Invoke();
|
|
}
|
|
|
|
public void SaveToLocalData(List<CustomAssetData> assetDatas, string path = "")
|
|
{
|
|
if (isSaveTaskComplete == false)
|
|
{
|
|
return;
|
|
}
|
|
//StartCoroutine(CoroutineSaveToLocalData(assetDatas, path));
|
|
}
|
|
public void LoadLocalData()
|
|
{
|
|
if (isLoadTaskComplete == false)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string path = Application.streamingAssetsPath + "/baseAssetData";
|
|
string filePath = GetLocalPath(path);
|
|
|
|
if (!CheckFilePathExists(filePath))
|
|
{
|
|
isLoadTaskComplete = true;
|
|
return;
|
|
}
|
|
|
|
// var command = new ActivateMoveGizmoCommand(rtgController);
|
|
//CommandInvoker.instance.Invoke(command);
|
|
|
|
CoroutineRunner.instance.StartCoroutine(CoroutineLoadFromLocalData(filePath));
|
|
}
|
|
|
|
public CustomAssetData GetCustomAssetData(string name)
|
|
{
|
|
return dataRepo.FindFromName(name);
|
|
//return listAssets.FirstOrDefault(x => x.name == name);
|
|
}
|
|
|
|
string GetLocalPath(string path)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
{
|
|
return Path.Combine(Application.persistentDataPath, identifier);
|
|
}
|
|
else
|
|
{
|
|
return path;
|
|
}
|
|
}
|
|
bool CheckFilePathExists(string filePath)
|
|
{
|
|
if (!System.IO.File.Exists(filePath))
|
|
{
|
|
Debug.Log("No File Found At : " + filePath);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
IEnumerator CoroutineLoadFromLocalData(string filePath)
|
|
{
|
|
isLoadTaskComplete = false;
|
|
|
|
Task<SaveData> task = Task<SaveData>.Run(() => messagePacker.LoadAsync(filePath));
|
|
yield return new WaitUntil(() => task.IsCompleted);
|
|
|
|
if (task.Result == null)
|
|
{
|
|
Debug.LogError("Error on loading local data.");
|
|
Debug.LogError("Error file location : " + filePath);
|
|
isLoadTaskComplete = true;
|
|
yield break;
|
|
}
|
|
|
|
saveData = task.Result;
|
|
CoroutineRunner.instance.StartCoroutine(LoadDefaultModelDatas());
|
|
isLoadTaskComplete = true;
|
|
}
|
|
|
|
|
|
IEnumerator LoadDefaultModelDatas()
|
|
{
|
|
foreach (var tex in saveData.textureDatas)
|
|
{
|
|
sharedMaterial.AddTextureData(tex);
|
|
}
|
|
|
|
for (int i = 0; i < saveData.modelDatas.Length; i++)
|
|
{
|
|
var modelData = saveData.modelDatas[i];
|
|
string assetName = modelData.attributes.FirstOrDefault(x => x[0].Equals("assetName"))?[1];
|
|
|
|
if (dataRepo.TryGetDataFromName(assetName, out var p))
|
|
continue;
|
|
LoadSaveData(saveData, i);
|
|
yield return null;
|
|
}
|
|
}
|
|
void LoadSaveData(SaveData saveData, int index)
|
|
{
|
|
var modelData = saveData.modelDatas[index];
|
|
string assetName = modelData.attributes.FirstOrDefault(x => x[0].Equals("assetName"))?[1];
|
|
string folderName = modelData.attributes.FirstOrDefault(x => x[0].Equals("folderName"))?[1];
|
|
string createDate = modelData.attributes.FirstOrDefault(x => x[0].Equals("createDate"))?[1];
|
|
string lastRevisionDate = modelData.attributes.FirstOrDefault(x => x[0].Equals("lastRevisionDate"))?[1];
|
|
string uploadDate = modelData.attributes.FirstOrDefault(x => x[0].Equals("uploadDate"))?[1];
|
|
string creator = modelData.attributes.FirstOrDefault(x => x[0].Equals("creator"))?[1];
|
|
string manager = modelData.attributes.FirstOrDefault(x => x[0].Equals("manager"))?[1];
|
|
|
|
Texture2D thumbnail = new Texture2D(1, 1);
|
|
thumbnail.LoadImage(saveData.thumbnailDatas[index].data);
|
|
|
|
GameObject newObject = new GameObject(assetName);
|
|
newObject.transform.parent = fbxAnchor;
|
|
CustomAssetData assetData = newObject.AddComponent<CustomAssetData>();
|
|
assetData.assetName = assetName;
|
|
assetData.folderName = folderName;
|
|
assetData.createDate = createDate;
|
|
assetData.LastRevisionDate = lastRevisionDate;
|
|
assetData.UploadDate = uploadDate;
|
|
assetData.Creator = creator;
|
|
assetData.Manager = manager;
|
|
assetData.loadedObject = modelData.LoadModelData(sharedMaterial);
|
|
assetData.thumbnail = thumbnail;
|
|
|
|
OnLoadComplete(assetData);
|
|
|
|
//assetData.onLoadProgress.RemoveAllListeners();
|
|
//assetData.onLoadProgress.AddListener((x) => item.onLoadProgress?.Invoke(x));
|
|
}
|
|
|
|
void OnLoadComplete(CustomAssetData assetData)
|
|
{
|
|
assetData.OnLoadComplete();
|
|
string folderName = assetData.folderName;
|
|
dataRepo.Add(assetData);
|
|
//listAssets.Add(assetData);
|
|
var connector = ManagerHub.instance.Get<CustomAssetConnector>();
|
|
AssetLibraryItem urlSectionItem = connector.assetScrollView.FindItem(folderName, AssetLibraryItemType.folder) ??
|
|
connector.assetScrollView.AddItem(folderName, null, AssetLibraryItemType.folder);
|
|
|
|
AssetLibraryItem item = connector.assetScrollView.AddItem(assetData.assetName, assetData.gameObject, AssetLibraryItemType.file, urlSectionItem);
|
|
}
|
|
}
|
|
} |