Skip to content

Commit 7691861

Browse files
committed
Implement Diff Generator
1 parent f144c96 commit 7691861

15 files changed

Lines changed: 285 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}

Packages/net.pawlygon.unitytools/Editor/FTDiffGenerator.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/net.pawlygon.unitytools/hdiff.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/net.pawlygon.unitytools/hdiff/hdiffz.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
MIT License
2+
3+
HDiffPatch
4+
Copyright (c) 2012-2022 housisong
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
24+
----------------------------------------------------------------------------------
25+
26+
libdivsufsort
27+
Copyright (c) 2003-2008 Yuta Mori All Rights Reserved.
28+
29+
Permission is hereby granted, free of charge, to any person
30+
obtaining a copy of this software and associated documentation
31+
files (the "Software"), to deal in the Software without
32+
restriction, including without limitation the rights to use,
33+
copy, modify, merge, publish, distribute, sublicense, and/or sell
34+
copies of the Software, and to permit persons to whom the
35+
Software is furnished to do so, subject to the following
36+
conditions:
37+
38+
The above copyright notice and this permission notice shall be
39+
included in all copies or substantial portions of the Software.
40+
41+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
43+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
44+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
45+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
46+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
48+
OTHER DEALINGS IN THE SOFTWARE.

Packages/net.pawlygon.unitytools/hdiff/hdiffz/License.txt.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/net.pawlygon.unitytools/hdiff/hdiffz/Linux.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.76 MB
Binary file not shown.

Packages/net.pawlygon.unitytools/hdiff/hdiffz/Linux/hdiffz.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/net.pawlygon.unitytools/hdiff/hdiffz/Mac.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)