From c73d81ccffa6f4974e62599e1e875b87ff4c6a9c Mon Sep 17 00:00:00 2001 From: Razmoth <32140579+Razmoth@users.noreply.github.com> Date: Sat, 20 Jan 2024 16:18:29 +0400 Subject: [PATCH] - [Core] rework duplicate file processing [GUI/CLI] --- AssetStudio.CLI/Exporter.cs | 47 +++++++++++--- AssetStudio.CLI/Settings.cs | 1 + AssetStudio.GUI/App.config | 3 + AssetStudio.GUI/Exporter.cs | 63 ++++++++++++++----- AssetStudio.GUI/MainForm.Designer.cs | 15 ++++- AssetStudio.GUI/MainForm.cs | 7 ++- .../Properties/Settings.Designer.cs | 12 ++++ AssetStudio.GUI/Properties/Settings.settings | 3 + 8 files changed, 121 insertions(+), 30 deletions(-) diff --git a/AssetStudio.CLI/Exporter.cs b/AssetStudio.CLI/Exporter.cs index deb2f8d..6ec097b 100644 --- a/AssetStudio.CLI/Exporter.cs +++ b/AssetStudio.CLI/Exporter.cs @@ -300,18 +300,45 @@ namespace AssetStudio.CLI private static bool TryExportFile(string dir, AssetItem item, string extension, out string fullPath) { var fileName = FixFileName(item.Text); - fullPath = Path.Combine(dir, fileName + extension); + fullPath = Path.Combine(dir, $"{fileName}{extension}"); if (!File.Exists(fullPath)) { Directory.CreateDirectory(dir); return true; } - fullPath = Path.Combine(dir, fileName + item.UniqueID + extension); - if (!File.Exists(fullPath)) + if (Properties.Settings.Default.allowDuplicates) + { + for (int i = 0; ; i++) + { + fullPath = Path.Combine(dir, $"{fileName} ({i}){extension}"); + if (!File.Exists(fullPath)) + { + return true; + } + } + } + return false; + } + + private static bool TryExportFolder(string dir, AssetItem item, out string fullPath) + { + var fileName = FixFileName(item.Text); + fullPath = Path.Combine(dir, fileName); + if (!Directory.Exists(fullPath)) { - Directory.CreateDirectory(dir); return true; } + if (Properties.Settings.Default.allowDuplicates) + { + for (int i = 0; ; i++) + { + fullPath = Path.Combine(dir, $"{fileName} ({i})"); + if (!Directory.Exists(fullPath)) + { + return true; + } + } + } return false; } @@ -329,11 +356,9 @@ namespace AssetStudio.CLI public static bool ExportAnimator(AssetItem item, string exportPath, List animationList = null) { - var exportFullPath = Path.Combine(exportPath, item.Text, item.Text + ".fbx"); - if (File.Exists(exportFullPath)) - { - exportFullPath = Path.Combine(exportPath, item.Text + item.UniqueID, item.Text + ".fbx"); - } + if (!TryExportFolder(exportPath, item, out var exportFullPath)) + return false; + var m_Animator = (Animator)item.Asset; var options = new ModelConverter.Options() { @@ -352,8 +377,10 @@ namespace AssetStudio.CLI public static bool ExportGameObject(AssetItem item, string exportPath, List animationList = null) { + if (!TryExportFolder(exportPath, item, out var exportFullPath)) + return false; + var m_GameObject = (GameObject)item.Asset; - exportPath = Path.Combine(exportPath, m_GameObject.m_Name) + Path.DirectorySeparatorChar; return ExportGameObject(m_GameObject, exportPath, animationList); } diff --git a/AssetStudio.CLI/Settings.cs b/AssetStudio.CLI/Settings.cs index 0a0e929..d1db43c 100644 --- a/AssetStudio.CLI/Settings.cs +++ b/AssetStudio.CLI/Settings.cs @@ -54,6 +54,7 @@ namespace AssetStudio.CLI.Properties { public bool restoreExtensionName => AppSettings.Get("restoreExtensionName", true); public bool enableFileLogging => AppSettings.Get("enableFileLogging", false); public bool minimalAssetMap => AppSettings.Get("minimalAssetMap", true); + public bool allowDuplicates => AppSettings.Get("allowDuplicates", false); public string texs => AppSettings.Get("texs", string.Empty); public string uvs => AppSettings.Get("uvs", string.Empty); diff --git a/AssetStudio.GUI/App.config b/AssetStudio.GUI/App.config index bc15cf3..e0883c6 100644 --- a/AssetStudio.GUI/App.config +++ b/AssetStudio.GUI/App.config @@ -109,6 +109,9 @@ False + + False + {} diff --git a/AssetStudio.GUI/Exporter.cs b/AssetStudio.GUI/Exporter.cs index 02eba55..cde49b0 100644 --- a/AssetStudio.GUI/Exporter.cs +++ b/AssetStudio.GUI/Exporter.cs @@ -300,18 +300,44 @@ namespace AssetStudio.GUI private static bool TryExportFile(string dir, AssetItem item, string extension, out string fullPath) { var fileName = FixFileName(item.Text); - fullPath = Path.Combine(dir, fileName + extension); + fullPath = Path.Combine(dir, $"{fileName}{extension}"); if (!File.Exists(fullPath)) { Directory.CreateDirectory(dir); return true; } - fullPath = Path.Combine(dir, fileName + item.UniqueID + extension); - if (!File.Exists(fullPath)) + if (Properties.Settings.Default.allowDuplicates) + { + for (int i = 0; ; i++) + { + fullPath = Path.Combine(dir, $"{fileName} ({i}){extension}"); + if (!File.Exists(fullPath)) + { + return true; + } + } + } + return false; + } + private static bool TryExportFolder(string dir, AssetItem item, out string fullPath) + { + var fileName = FixFileName(item.Text); + fullPath = Path.Combine(dir, fileName); + if (!Directory.Exists(fullPath)) { - Directory.CreateDirectory(dir); return true; } + if (Properties.Settings.Default.allowDuplicates) + { + for (int i = 0; ; i++) + { + fullPath = Path.Combine(dir, $"{fileName} ({i})"); + if (!Directory.Exists(fullPath)) + { + return true; + } + } + } return false; } public static bool ExportAnimationClip(AssetItem item, string exportPath) @@ -328,11 +354,10 @@ namespace AssetStudio.GUI public static bool ExportAnimator(AssetItem item, string exportPath, List animationList = null) { - var exportFullPath = Path.Combine(exportPath, item.Text, item.Text + ".fbx"); - if (File.Exists(exportFullPath)) - { - exportFullPath = Path.Combine(exportPath, item.Text + item.UniqueID, item.Text + ".fbx"); - } + if (!TryExportFolder(exportPath, item, out var exportFullPath)) + return false; + + exportFullPath = Path.Combine(exportFullPath, item.Text + ".fbx"); var m_Animator = (Animator)item.Asset; var options = new ModelConverter.Options() { @@ -351,17 +376,14 @@ namespace AssetStudio.GUI public static bool ExportGameObject(AssetItem item, string exportPath, List animationList = null) { - var exportFullPath = Path.Combine(exportPath, item.Text, item.Text + ".fbx"); - if (File.Exists(exportFullPath)) - { - exportFullPath = Path.Combine(exportPath, item.Text + item.UniqueID, item.Text + ".fbx"); - } + if (!TryExportFolder(exportPath, item, out var exportFullPath)) + return false; + var m_GameObject = (GameObject)item.Asset; - ExportGameObject(m_GameObject, exportFullPath, animationList); - return true; + return ExportGameObject(m_GameObject, exportFullPath, animationList); } - public static void ExportGameObject(GameObject gameObject, string exportPath, List animationList = null) + public static bool ExportGameObject(GameObject gameObject, string exportPath, List animationList = null) { var options = new ModelConverter.Options() { @@ -374,8 +396,15 @@ namespace AssetStudio.GUI var convert = animationList != null ? new ModelConverter(gameObject, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray()) : new ModelConverter(gameObject, options); + + if (convert.MeshList.Count == 0) + { + Logger.Info($"GameObject {gameObject.m_Name} has no mesh, skipping..."); + return false; + } exportPath = exportPath + FixFileName(gameObject.m_Name) + ".fbx"; ExportFbx(convert, exportPath); + return true; } public static void ExportGameObjectMerge(List gameObject, string exportPath, List animationList = null) diff --git a/AssetStudio.GUI/MainForm.Designer.cs b/AssetStudio.GUI/MainForm.Designer.cs index e78e1b8..2b21c21 100644 --- a/AssetStudio.GUI/MainForm.Designer.cs +++ b/AssetStudio.GUI/MainForm.Designer.cs @@ -173,6 +173,7 @@ namespace AssetStudio.GUI exportAnimatorwithselectedAnimationClipMenuItem = new System.Windows.Forms.ToolStripMenuItem(); goToSceneHierarchyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); showOriginalFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + allowDuplicates = new System.Windows.Forms.ToolStripMenuItem(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit(); splitContainer1.Panel1.SuspendLayout(); @@ -264,7 +265,7 @@ namespace AssetStudio.GUI // // optionsToolStripMenuItem // - optionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { displayAll, toolStripSeparator10, enablePreview, enableModelPreview, modelsOnly, toolStripSeparator11, displayInfo, enableResolveDependencies, skipContainer, toolStripSeparator12, toolStripMenuItem14, specifyUnityCNKey, toolStripSeparator13, toolStripMenuItem18, toolStripMenuItem19, showExpOpt }); + optionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { displayAll, toolStripSeparator10, enablePreview, enableModelPreview, modelsOnly, toolStripSeparator11, displayInfo, enableResolveDependencies, allowDuplicates, skipContainer, toolStripSeparator12, toolStripMenuItem14, specifyUnityCNKey, toolStripSeparator13, toolStripMenuItem18, toolStripMenuItem19, showExpOpt }); optionsToolStripMenuItem.Name = "optionsToolStripMenuItem"; optionsToolStripMenuItem.Size = new System.Drawing.Size(61, 20); optionsToolStripMenuItem.Text = "Options"; @@ -659,7 +660,7 @@ namespace AssetStudio.GUI allToolStripMenuItem.CheckOnClick = true; allToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked; allToolStripMenuItem.Name = "allToolStripMenuItem"; - allToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + allToolStripMenuItem.Size = new System.Drawing.Size(88, 22); allToolStripMenuItem.Text = "All"; allToolStripMenuItem.Click += typeToolStripMenuItem_Click; // @@ -1352,6 +1353,15 @@ namespace AssetStudio.GUI showOriginalFileToolStripMenuItem.Visible = false; showOriginalFileToolStripMenuItem.Click += showOriginalFileToolStripMenuItem_Click; // + // allowDuplicates + // + allowDuplicates.CheckOnClick = true; + allowDuplicates.Name = "allowDuplicates"; + allowDuplicates.Size = new System.Drawing.Size(225, 22); + allowDuplicates.Text = "Allow duplicates"; + allowDuplicates.ToolTipText = "Toggle the behaviour of exporting assets.\r\nEnable to allow assets with duplicate names to be exported."; + allowDuplicates.CheckedChanged += allowDuplicates_CheckedChanged; + // // MainForm // AllowDrop = true; @@ -1542,6 +1552,7 @@ namespace AssetStudio.GUI private System.Windows.Forms.ToolStripMenuItem sceneHierarchy; private System.Windows.Forms.ToolStripMenuItem assetMapTypeMenuItem; private System.Windows.Forms.ToolStripMenuItem loadCABMapToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem allowDuplicates; } } diff --git a/AssetStudio.GUI/MainForm.cs b/AssetStudio.GUI/MainForm.cs index e00afe0..d6bb836 100644 --- a/AssetStudio.GUI/MainForm.cs +++ b/AssetStudio.GUI/MainForm.cs @@ -105,6 +105,7 @@ namespace AssetStudio.GUI enableModelPreview.Checked = Properties.Settings.Default.enableModelPreview; modelsOnly.Checked = Properties.Settings.Default.modelsOnly; enableResolveDependencies.Checked = Properties.Settings.Default.enableResolveDependencies; + allowDuplicates.Checked = Properties.Settings.Default.allowDuplicates; skipContainer.Checked = Properties.Settings.Default.skipContainer; assetsManager.ResolveDependencies = enableResolveDependencies.Checked; SkipContainer = Properties.Settings.Default.skipContainer; @@ -2054,13 +2055,17 @@ namespace AssetStudio.GUI assetsManager.ResolveDependencies = enableResolveDependencies.Checked; } + private void allowDuplicates_CheckedChanged(object sender, EventArgs e) + { + Properties.Settings.Default.allowDuplicates = allowDuplicates.Checked; + Properties.Settings.Default.Save(); + } private void skipContainer_CheckedChanged(object sender, EventArgs e) { Properties.Settings.Default.skipContainer = skipContainer.Checked; Properties.Settings.Default.Save(); SkipContainer = skipContainer.Checked; - } private void assetMapTypeMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { diff --git a/AssetStudio.GUI/Properties/Settings.Designer.cs b/AssetStudio.GUI/Properties/Settings.Designer.cs index 6d3fb68..0bc1e7f 100644 --- a/AssetStudio.GUI/Properties/Settings.Designer.cs +++ b/AssetStudio.GUI/Properties/Settings.Designer.cs @@ -469,5 +469,17 @@ namespace AssetStudio.GUI.Properties { this["uvs"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool allowDuplicates { + get { + return ((bool)(this["allowDuplicates"])); + } + set { + this["allowDuplicates"] = value; + } + } } } diff --git a/AssetStudio.GUI/Properties/Settings.settings b/AssetStudio.GUI/Properties/Settings.settings index 1ef39c3..fdec7c4 100644 --- a/AssetStudio.GUI/Properties/Settings.settings +++ b/AssetStudio.GUI/Properties/Settings.settings @@ -113,5 +113,8 @@ {"UV0":{"Item1":true,"Item2":0},"UV1":{"Item1":true,"Item2":1},"UV2":{"Item1":false,"Item2":0},"UV3":{"Item1":false,"Item2":0},"UV4":{"Item1":false,"Item2":0},"UV5":{"Item1":false,"Item2":0},"UV6":{"Item1":false,"Item2":0},"UV7":{"Item1":false,"Item2":0}} + + False + \ No newline at end of file