mirror of
https://github.com/aelurum/AssetStudio.git
synced 2026-11-15 19:28:48 -05:00
Improve detection of Live2D models. Close #122
- Added an alternative way of model detection when a MonoScript asset is not loaded. - Fixed model detection regression after the https://github.com/aelurum/AssetStudio/commit/0066d673f45cfd07bbca4343e2fdf27660698c17
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using AssetStudio;
|
||||
using AssetStudio.Addons.CubismLive2D;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -331,7 +332,7 @@ namespace AssetStudioGUI
|
||||
|
||||
var types = new SortedSet<string>();
|
||||
types.UnionWith(exportableAssets.Select(x => x.TypeString));
|
||||
if (Studio.l2dAssetManager.ModelDict.Count > 0)
|
||||
if (Studio.l2dAssetsManager.ModelDict.Count > 0)
|
||||
{
|
||||
types.Add("MonoBehaviour (Live2D Model)");
|
||||
}
|
||||
@@ -939,6 +940,11 @@ namespace AssetStudioGUI
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (l2dAssetsManager.ModelDict.ContainsKey(m_MonoBehaviour))
|
||||
{
|
||||
PreviewMoc(assetItem, m_MonoBehaviour);
|
||||
break;
|
||||
}
|
||||
PreviewMonoBehaviour(m_MonoBehaviour);
|
||||
break;
|
||||
case ClassIDType.Font:
|
||||
@@ -1207,7 +1213,7 @@ namespace AssetStudioGUI
|
||||
using (var cubismMoc = new AssetStudio.Addons.CubismLive2D.CubismMoc(m_MonoBehaviour))
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
if (Studio.l2dAssetManager.ModelDict.TryGetValue(m_MonoBehaviour, out var model) && model != null)
|
||||
if (Studio.l2dAssetsManager.ModelDict.TryGetValue(m_MonoBehaviour, out var model) && model != null)
|
||||
{
|
||||
sb.AppendLine($"Model Name: {model.Name}");
|
||||
}
|
||||
@@ -1599,7 +1605,7 @@ namespace AssetStudioGUI
|
||||
Studio.assemblyLoader.Clear();
|
||||
Studio.exportableAssets.Clear();
|
||||
Studio.visibleAssets.Clear();
|
||||
Studio.l2dAssetManager.Clear();
|
||||
Studio.l2dAssetsManager.Clear();
|
||||
sceneTreeView.Nodes.Clear();
|
||||
assetListView.VirtualListSize = 0;
|
||||
assetListView.Items.Clear();
|
||||
@@ -1684,19 +1690,37 @@ namespace AssetStudioGUI
|
||||
switch (asset.Asset)
|
||||
{
|
||||
case MonoBehaviour m_MonoBehaviour:
|
||||
if (Studio.l2dAssetManager.ModelDict.Count > 0 && m_MonoBehaviour.m_Script.TryGet(out var m_Script))
|
||||
if (Studio.l2dAssetsManager.ModelDict.Count > 0)
|
||||
{
|
||||
if (m_Script.m_ClassName == "CubismMoc")
|
||||
if (m_MonoBehaviour.m_Script.TryGet(out var m_Script))
|
||||
{
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourMoc;
|
||||
switch (m_Script.m_ClassName)
|
||||
{
|
||||
case "CubismMoc":
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourMoc;
|
||||
break;
|
||||
case "CubismFadeMotionData":
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourFade;
|
||||
break;
|
||||
case "CubismFadeMotionList":
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourFadeLst;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (m_Script.m_ClassName == "CubismFadeMotionData")
|
||||
else if (CubismHelpers.TryGetCubismMonoBehaviourType(m_MonoBehaviour, out var type))
|
||||
{
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourFade;
|
||||
}
|
||||
else if (m_Script.m_ClassName == "CubismFadeMotionList")
|
||||
{
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourFadeLst;
|
||||
switch (type)
|
||||
{
|
||||
case CubismHelpers.CubismMonoBehaviourType.Moc:
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourMoc;
|
||||
break;
|
||||
case CubismHelpers.CubismMonoBehaviourType.FadeMotion:
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourFade;
|
||||
break;
|
||||
case CubismHelpers.CubismMonoBehaviourType.FadeMotionList:
|
||||
selectedTypes |= SelectedAssetType.MonoBehaviourFadeLst;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -1990,7 +2014,7 @@ namespace AssetStudioGUI
|
||||
}
|
||||
}
|
||||
visibleAssets = filterMoc
|
||||
? exportableAssets.FindAll(x => (x.Asset is MonoBehaviour monoBehaviour && Studio.l2dAssetManager.ModelDict.ContainsKey(monoBehaviour)) || show.Contains(x.Type))
|
||||
? exportableAssets.FindAll(x => (x.Asset is MonoBehaviour monoBehaviour && Studio.l2dAssetsManager.ModelDict.ContainsKey(monoBehaviour)) || show.Contains(x.Type))
|
||||
: exportableAssets.FindAll(x => show.Contains(x.Type));
|
||||
}
|
||||
else
|
||||
@@ -2325,7 +2349,7 @@ namespace AssetStudioGUI
|
||||
{
|
||||
if (exportableAssets.Count > 0)
|
||||
{
|
||||
if (Studio.l2dAssetManager.ModelDict.Count == 0)
|
||||
if (Studio.l2dAssetsManager.ModelDict.Count == 0)
|
||||
{
|
||||
Logger.Info("Live2D Cubism models were not found.");
|
||||
return;
|
||||
@@ -2365,7 +2389,7 @@ namespace AssetStudioGUI
|
||||
Logger.Info("No exportable assets loaded");
|
||||
return;
|
||||
}
|
||||
if (Studio.l2dAssetManager.ModelDict.Count == 0)
|
||||
if (Studio.l2dAssetsManager.ModelDict.Count == 0)
|
||||
{
|
||||
Logger.Info("Live2D Cubism models were not found.");
|
||||
return;
|
||||
@@ -2396,6 +2420,20 @@ namespace AssetStudioGUI
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case MonoBehaviour m_MonoBehaviour when CubismHelpers.TryGetCubismMonoBehaviourType(m_MonoBehaviour, out var type):
|
||||
switch (type)
|
||||
{
|
||||
case CubismHelpers.CubismMonoBehaviourType.Moc:
|
||||
selectedMocs.Add(m_MonoBehaviour);
|
||||
break;
|
||||
case CubismHelpers.CubismMonoBehaviourType.FadeMotion:
|
||||
selectedFadeMotions.Add(m_MonoBehaviour);
|
||||
break;
|
||||
case CubismHelpers.CubismMonoBehaviourType.FadeMotionList:
|
||||
selectedFadeLst = m_MonoBehaviour;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case AnimationClip m_AnimationClip:
|
||||
selectedClips.Add(m_AnimationClip);
|
||||
break;
|
||||
|
||||
+22
-9
@@ -6,12 +6,12 @@ using static CubismLive2DExtractor.CubismParsers;
|
||||
|
||||
namespace AssetStudioGUI
|
||||
{
|
||||
public class L2DAssetManager
|
||||
public class L2DAssetsManager
|
||||
{
|
||||
public readonly Dictionary<MonoBehaviour, CubismModel> ModelDict = new Dictionary<MonoBehaviour, CubismModel>();
|
||||
public readonly Dictionary<Object, string> AssetContainers = new Dictionary<Object, string>();
|
||||
|
||||
public L2DAssetManager () { }
|
||||
public L2DAssetsManager () { }
|
||||
|
||||
public void BindCubismAssetsByGO(GameObject m_GameObject, AssemblyLoader assemblyLoader)
|
||||
{
|
||||
@@ -32,12 +32,6 @@ namespace AssetStudioGUI
|
||||
|
||||
switch (m_ClassName)
|
||||
{
|
||||
case "CubismMoc":
|
||||
if (!ModelDict.ContainsKey(m_MonoBehaviour))
|
||||
{
|
||||
ModelDict.Add(m_MonoBehaviour, null);
|
||||
}
|
||||
break;
|
||||
case "CubismRenderer":
|
||||
modelGameObject.CubismModel.RenderTextureList.Add(m_MonoBehaviour);
|
||||
break;
|
||||
@@ -53,10 +47,29 @@ namespace AssetStudioGUI
|
||||
}
|
||||
}
|
||||
|
||||
public void BindCubismAssetsByMono(MonoBehaviour m_MonoBehaviour, CubismHelpers.CubismMonoBehaviourType type)
|
||||
{
|
||||
if (!m_MonoBehaviour.m_GameObject.TryGet(out var m_GameObject))
|
||||
return;
|
||||
|
||||
if (!TryGetModelGameObject(m_GameObject.m_Transform, out var modelGameObject))
|
||||
return;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case CubismHelpers.CubismMonoBehaviourType.RenderTexture:
|
||||
modelGameObject.CubismModel.RenderTextureList.Add(m_MonoBehaviour);
|
||||
break;
|
||||
case CubismHelpers.CubismMonoBehaviourType.PosePart:
|
||||
modelGameObject.CubismModel.PosePartList.Add(m_MonoBehaviour);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryGetCubismMoc(MonoBehaviour m_MonoBehaviour, AssemblyLoader assemblyLoader, out MonoBehaviour mocMono)
|
||||
{
|
||||
mocMono = null;
|
||||
var pptrDict = (OrderedDictionary)ParseMonoBehaviour(m_MonoBehaviour, CubismMonoBehaviourType.Model, assemblyLoader)?["_moc"];
|
||||
var pptrDict = (OrderedDictionary)ParseMonoBehaviour(m_MonoBehaviour, CubismHelpers.CubismMonoBehaviourType.ModelPPtr, assemblyLoader)?["_moc"];
|
||||
if (pptrDict == null)
|
||||
return false;
|
||||
|
||||
+44
-28
@@ -86,7 +86,7 @@ namespace AssetStudioGUI
|
||||
public static AssemblyLoader assemblyLoader = new AssemblyLoader();
|
||||
public static List<AssetItem> exportableAssets = new List<AssetItem>();
|
||||
public static List<AssetItem> visibleAssets = new List<AssetItem>();
|
||||
public static L2DAssetManager l2dAssetManager = new L2DAssetManager();
|
||||
public static L2DAssetsManager l2dAssetsManager = new L2DAssetsManager();
|
||||
internal static Action<string> StatusStripUpdate = x => { };
|
||||
internal static Fbx.Settings FbxSettings;
|
||||
|
||||
@@ -241,7 +241,7 @@ namespace AssetStudioGUI
|
||||
assetItem.Text = m_GameObject.m_Name;
|
||||
if (m_GameObject.CubismModel != null)
|
||||
{
|
||||
l2dAssetManager.BindCubismAssetsByGO(m_GameObject, assemblyLoader);
|
||||
l2dAssetsManager.BindCubismAssetsByGO(m_GameObject, assemblyLoader);
|
||||
}
|
||||
break;
|
||||
case Texture2D m_Texture2D:
|
||||
@@ -296,12 +296,27 @@ namespace AssetStudioGUI
|
||||
assetName = assetName == "" ? m_Script.m_ClassName : assetName;
|
||||
switch (m_Script.m_ClassName)
|
||||
{
|
||||
case "CubismMoc":
|
||||
case "CubismMoc" when !l2dAssetsManager.ModelDict.ContainsKey(m_MonoBehaviour):
|
||||
l2dAssetsManager.ModelDict[m_MonoBehaviour] = new CubismModel(m_MonoBehaviour);
|
||||
break;
|
||||
case "CubismRenderer":
|
||||
case "CubismDisplayInfoParameterName":
|
||||
case "CubismDisplayInfoPartName":
|
||||
case "CubismPosePart":
|
||||
l2dAssetManager.BindCubismAssetsByMono(m_MonoBehaviour, m_Script.m_ClassName);
|
||||
l2dAssetsManager.BindCubismAssetsByMono(m_MonoBehaviour, m_Script.m_ClassName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (CubismHelpers.TryGetCubismMonoBehaviourType(m_MonoBehaviour, out var type))
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case CubismHelpers.CubismMonoBehaviourType.Moc when !l2dAssetsManager.ModelDict.ContainsKey(m_MonoBehaviour):
|
||||
l2dAssetsManager.ModelDict[m_MonoBehaviour] = new CubismModel(m_MonoBehaviour);
|
||||
break;
|
||||
case CubismHelpers.CubismMonoBehaviourType.RenderTexture:
|
||||
case CubismHelpers.CubismMonoBehaviourType.PosePart:
|
||||
l2dAssetsManager.BindCubismAssetsByMono(m_MonoBehaviour, type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -371,7 +386,7 @@ namespace AssetStudioGUI
|
||||
case AnimationClip _:
|
||||
case Texture2D _:
|
||||
case MonoBehaviour _:
|
||||
l2dAssetManager.AssetContainers[obj] = container;
|
||||
l2dAssetsManager.AssetContainers[obj] = container;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1024,9 +1039,9 @@ namespace AssetStudioGUI
|
||||
{
|
||||
var tempMocPathDict = new Dictionary<MonoBehaviour, (string, string)>();
|
||||
var mocPathDict = new Dictionary<MonoBehaviour, string>();
|
||||
foreach (var mocMono in l2dAssetManager.ModelDict.Keys)
|
||||
foreach (var mocMono in l2dAssetsManager.ModelDict.Keys)
|
||||
{
|
||||
if (l2dAssetManager.AssetContainers.TryGetValue(mocMono, out var fullContainerPath))
|
||||
if (l2dAssetsManager.AssetContainers.TryGetValue(mocMono, out var fullContainerPath))
|
||||
{
|
||||
var pathSepIndex = fullContainerPath.LastIndexOf('/');
|
||||
var basePath = pathSepIndex > 0
|
||||
@@ -1065,12 +1080,15 @@ namespace AssetStudioGUI
|
||||
return mocPathDict;
|
||||
}
|
||||
|
||||
private static Dictionary<MonoBehaviour, List<Object>> GenerateMocAssetDict(Dictionary<MonoBehaviour, CubismModel> mocDict)
|
||||
private static Dictionary<MonoBehaviour, List<Object>> GenerateMocAssetDict(List<MonoBehaviour> filteredMocs)
|
||||
{
|
||||
var searchByFilename = Properties.Settings.Default.l2dAssetSearchByFilename;
|
||||
var l2dContainers = searchByFilename
|
||||
? new Dictionary<Object, string>()
|
||||
: l2dAssetManager.AssetContainers;
|
||||
: l2dAssetsManager.AssetContainers;
|
||||
var mocDict = filteredMocs != null
|
||||
? filteredMocs.ToDictionary(moc => moc, moc => l2dAssetsManager.ModelDict[moc])
|
||||
: l2dAssetsManager.ModelDict;
|
||||
|
||||
var mode = searchByFilename ? "file names" : "containers";
|
||||
Logger.Info($"Searching for Live2D assets using {mode}...");
|
||||
@@ -1131,7 +1149,7 @@ namespace AssetStudioGUI
|
||||
}
|
||||
if (searchByFilename)
|
||||
l2dContainers.Clear();
|
||||
|
||||
|
||||
return assetDict;
|
||||
}
|
||||
|
||||
@@ -1152,37 +1170,35 @@ namespace AssetStudioGUI
|
||||
l2dOptions.MotionMode = Live2DMotionMode.AnimationClipV2;
|
||||
else if (selFadeMotions != null || selFadeLst != null)
|
||||
l2dOptions.MotionMode = Live2DMotionMode.MonoBehaviour;
|
||||
var mocDict = selMocs != null
|
||||
? selMocs.ToDictionary(moc => moc, moc => l2dAssetManager.ModelDict[moc])
|
||||
: l2dAssetManager.ModelDict;
|
||||
|
||||
ThreadPool.QueueUserWorkItem(state =>
|
||||
{
|
||||
var assetDict = GenerateMocAssetDict(mocDict);
|
||||
if (mocDict.Keys.First().serializedType?.m_Type == null && !assemblyLoader.Loaded)
|
||||
var assetDict = GenerateMocAssetDict(selMocs);
|
||||
if (assetDict.Count > 0 && assetDict.Keys.First().serializedType?.m_Type == null && !assemblyLoader.Loaded)
|
||||
{
|
||||
Logger.Warning("Specifying the assembly folder may be needed for proper extraction");
|
||||
SelectAssemblyFolder();
|
||||
}
|
||||
var totalModelCount = assetDict.Count;
|
||||
var modelCounter = 0;
|
||||
Live2DExtractor.MocDict = mocDict;
|
||||
Live2DExtractor.Assembly = assemblyLoader;
|
||||
foreach (var assetGroupKvp in assetDict)
|
||||
{
|
||||
var srcContainer = l2dAssetManager.AssetContainers.TryGetValue(assetGroupKvp.Key, out var result)
|
||||
var mocMono = assetGroupKvp.Key;
|
||||
var srcContainer = l2dAssetsManager.AssetContainers.TryGetValue(mocMono, out var result)
|
||||
? result
|
||||
: assetGroupKvp.Key.assetsFile.fullName;
|
||||
: mocMono.assetsFile.fullName;
|
||||
l2dAssetsManager.ModelDict.TryGetValue(mocMono, out var cubismModel);
|
||||
|
||||
Logger.Info($"[{modelCounter + 1}/{totalModelCount}] Exporting Live2D from: \"{srcContainer}\"...");
|
||||
try
|
||||
{
|
||||
var cubismExtractor = new Live2DExtractor(assetGroupKvp, l2dOptions);
|
||||
var fileName = string.IsNullOrEmpty(cubismExtractor.MocMono.assetsFile.originalPath)
|
||||
? Path.GetFileNameWithoutExtension(cubismExtractor.MocMono.assetsFile.fileName)
|
||||
: Path.GetFileNameWithoutExtension(cubismExtractor.MocMono.assetsFile.originalPath);
|
||||
var modelName = !string.IsNullOrEmpty(cubismExtractor.Model?.Name)
|
||||
? cubismExtractor.Model.Name
|
||||
var cubismExtractor = new Live2DExtractor(assetGroupKvp.Value, cubismModel, l2dOptions);
|
||||
var fileName = string.IsNullOrEmpty(mocMono.assetsFile.originalPath)
|
||||
? Path.GetFileNameWithoutExtension(mocMono.assetsFile.fileName)
|
||||
: Path.GetFileNameWithoutExtension(mocMono.assetsFile.originalPath);
|
||||
var modelName = !string.IsNullOrEmpty(cubismModel?.Name)
|
||||
? cubismModel.Name
|
||||
: fileName;
|
||||
Logger.Info($"Model name: \"{modelName}\"");
|
||||
|
||||
@@ -1196,10 +1212,10 @@ namespace AssetStudioGUI
|
||||
modelPath = modelName;
|
||||
break;
|
||||
default: //ContainerPath
|
||||
var container = l2dOptions.SearchByFilename && cubismExtractor.Model != null
|
||||
? cubismExtractor.Model.Container
|
||||
var container = l2dOptions.SearchByFilename && cubismModel != null
|
||||
? cubismModel.Container
|
||||
: srcContainer;
|
||||
container = container == assetGroupKvp.Key.assetsFile.fullName
|
||||
container = container == mocMono.assetsFile.fullName
|
||||
? fileName
|
||||
: container;
|
||||
modelPath = Path.HasExtension(container)
|
||||
@@ -1208,7 +1224,7 @@ namespace AssetStudioGUI
|
||||
break;
|
||||
}
|
||||
var destPath = Exporter.SafePathCombine(baseDestPath, modelPath);
|
||||
cubismExtractor.ExtractCubismModel(destPath, GetParallelExportCount());
|
||||
cubismExtractor.ExtractCubismModel(mocMono, destPath, GetParallelExportCount());
|
||||
modelCounter++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user