|
| 1 | +// FTDiffGeneratorConfig.cs |
| 2 | +using UnityEngine; |
| 3 | +using UnityEditor; |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Diagnostics; |
| 7 | +using Debug = UnityEngine.Debug; |
| 8 | + |
| 9 | +[CreateAssetMenu(fileName = "NewFTDiffGenerator", menuName = "Pawlygon/FaceTracking Diff Generator")] |
| 10 | +public class FTDiffGeneratorConfig : ScriptableObject |
| 11 | +{ |
| 12 | + [Header("Prefab References")] |
| 13 | + public GameObject originalModelPrefab; |
| 14 | + public GameObject modifiedModelPrefab; |
| 15 | + |
| 16 | + [Header("Output Settings")] |
| 17 | + public DefaultAsset outputDirectory; |
| 18 | + |
| 19 | + [ContextMenu("Generate Diff Files")] |
| 20 | + public void GenerateDiffFiles() |
| 21 | + { |
| 22 | + string originalFbxPath = GetFBXPathFromPrefab(originalModelPrefab); |
| 23 | + string modifiedFbxPath = GetFBXPathFromPrefab(modifiedModelPrefab); |
| 24 | + |
| 25 | + if (string.IsNullOrEmpty(originalFbxPath) || string.IsNullOrEmpty(modifiedFbxPath)) |
| 26 | + { |
| 27 | + Debug.LogError("❌ Failed to resolve FBX paths from the given prefabs. Make sure your prefabs reference FBX models."); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + string baseName = Path.GetFileNameWithoutExtension(originalFbxPath).Replace(" ", "_"); |
| 32 | + |
| 33 | + string outputFolderPath = AssetDatabase.GetAssetPath(outputDirectory); |
| 34 | + string absoluteOutputPath = Path.GetFullPath(outputFolderPath); |
| 35 | + string diffOutputPath = Path.Combine(absoluteOutputPath, "patcher", "data", "DiffFiles"); |
| 36 | + |
| 37 | + Directory.CreateDirectory(diffOutputPath); |
| 38 | + |
| 39 | + string fbxDiffOutputPath = Path.Combine(diffOutputPath, baseName + ".hdiff"); |
| 40 | + string metaDiffOutputPath = Path.Combine(diffOutputPath, baseName + "Meta.hdiff"); |
| 41 | + |
| 42 | + string basePackagePath = Path.Combine("Packages", "net.pawlygon.unitytools", "hdiff", "hdiffz"); |
| 43 | + |
| 44 | + string hdiffExecutablePath = Application.platform switch |
| 45 | + { |
| 46 | + RuntimePlatform.WindowsEditor => Path.Combine(basePackagePath, "Windows", "hdiffz.exe"), |
| 47 | + RuntimePlatform.OSXEditor => Path.Combine(basePackagePath, "macOS", "hdiffz"), |
| 48 | + RuntimePlatform.LinuxEditor => Path.Combine(basePackagePath, "Linux", "hdiffz"), |
| 49 | + _ => throw new PlatformNotSupportedException("Unsupported platform for hdiffz") |
| 50 | + }; |
| 51 | + |
| 52 | + hdiffExecutablePath = Path.GetFullPath(hdiffExecutablePath); |
| 53 | + |
| 54 | +#if !UNITY_EDITOR_WIN |
| 55 | + if (!SetExecutablePermission(hdiffExecutablePath)) |
| 56 | + { |
| 57 | + Debug.LogError("❌ Failed to set executable permission for hdiffz."); |
| 58 | + return; |
| 59 | + } |
| 60 | +#endif |
| 61 | + |
| 62 | + if (!File.Exists(hdiffExecutablePath)) |
| 63 | + { |
| 64 | + Debug.LogWarning("⚠️ Skipping diff generation. Missing hdiffz.exe at: " + hdiffExecutablePath); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + string fbxArguments = $"\"{originalFbxPath}\" \"{modifiedFbxPath}\" \"{fbxDiffOutputPath}\""; |
| 69 | + string metaArguments = $"\"{originalFbxPath}.meta\" \"{modifiedFbxPath}.meta\" \"{metaDiffOutputPath}\""; |
| 70 | + |
| 71 | + Debug.Log("📦 Generating FBX diff..."); |
| 72 | + LaunchProcess(hdiffExecutablePath, fbxArguments); |
| 73 | + |
| 74 | + Debug.Log("📦 Generating Meta diff..."); |
| 75 | + LaunchProcess(hdiffExecutablePath, metaArguments); |
| 76 | + |
| 77 | + Debug.Log("✅ Diff files successfully created at: " + diffOutputPath); |
| 78 | + AssetDatabase.Refresh(); |
| 79 | + } |
| 80 | + |
| 81 | + private void LaunchProcess(string executablePath, string arguments) |
| 82 | + { |
| 83 | + try |
| 84 | + { |
| 85 | + var process = new Process |
| 86 | + { |
| 87 | + StartInfo = new ProcessStartInfo |
| 88 | + { |
| 89 | + FileName = executablePath, |
| 90 | + Arguments = arguments, |
| 91 | + WorkingDirectory = Application.dataPath, |
| 92 | + UseShellExecute = false, |
| 93 | + RedirectStandardOutput = true, |
| 94 | + RedirectStandardError = true, |
| 95 | + CreateNoWindow = true |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + process.Start(); |
| 100 | + string stdOutput = process.StandardOutput.ReadToEnd(); |
| 101 | + string stdError = process.StandardError.ReadToEnd(); |
| 102 | + process.WaitForExit(); |
| 103 | + |
| 104 | + if (!string.IsNullOrEmpty(stdOutput)) Debug.Log("🔧 hdiffz output: " + stdOutput); |
| 105 | + if (!string.IsNullOrEmpty(stdError)) Debug.LogWarning("⚠️ hdiffz error: " + stdError); |
| 106 | + } |
| 107 | + catch (Exception exception) |
| 108 | + { |
| 109 | + Debug.LogError($"❌ Failed to execute patch process: {exception.Message}"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private string GetFBXPathFromPrefab(GameObject prefab) |
| 114 | + { |
| 115 | + if (prefab == null) return null; |
| 116 | + |
| 117 | + string prefabPath = AssetDatabase.GetAssetPath(prefab); |
| 118 | + if (string.IsNullOrEmpty(prefabPath)) return null; |
| 119 | + |
| 120 | + GameObject rootModel = PrefabUtility.GetCorrespondingObjectFromOriginalSource(prefab); |
| 121 | + string modelPath = AssetDatabase.GetAssetPath(rootModel); |
| 122 | + |
| 123 | + if (Path.GetExtension(modelPath).ToLower() != ".fbx") |
| 124 | + { |
| 125 | + Debug.LogWarning("⚠️ The selected prefab is not linked to an FBX file: " + modelPath); |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + return Path.GetFullPath(modelPath); |
| 130 | + } |
| 131 | + |
| 132 | + private bool SetExecutablePermission(string path) |
| 133 | + { |
| 134 | + try |
| 135 | + { |
| 136 | + var chmod = new Process |
| 137 | + { |
| 138 | + StartInfo = new ProcessStartInfo |
| 139 | + { |
| 140 | + FileName = "/bin/chmod", |
| 141 | + Arguments = $"+x \"{path}\"", |
| 142 | + RedirectStandardOutput = true, |
| 143 | + RedirectStandardError = true, |
| 144 | + UseShellExecute = false, |
| 145 | + CreateNoWindow = true |
| 146 | + } |
| 147 | + }; |
| 148 | + chmod.Start(); |
| 149 | + chmod.WaitForExit(); |
| 150 | + return chmod.ExitCode == 0; |
| 151 | + } |
| 152 | + catch (Exception e) |
| 153 | + { |
| 154 | + Debug.LogError("Error while setting executable permission: " + e); |
| 155 | + return false; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments