Skip to content

Commit 3391f7e

Browse files
author
Benoit Hudson
committed
UT-511: fix a bug and some tests
The auto-updater is now robust to FbxPrefab not being found, or having duplicates, for whatever reason. In particular on 2018.1 we find it in a fixed location because the asset database can't find anything in the packages. Added a ChangeLog as is required; also now we use the ChangeLog to determine the version.
1 parent 88ee45f commit 3391f7e

File tree

5 files changed

+78
-31
lines changed

5 files changed

+78
-31
lines changed

Assets/com.unity.formats.fbx.tests/FbxPrefabAutoUpdaterTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ public void BasicTest ()
5454
var imported = new HashSet<string> (new string [] { "Assets/path/to/foo.fbx", m_fbxPath });
5555
Assert.IsTrue (FbxPrefabAutoUpdater.MayHaveFbxPrefabToFbxAsset (m_prefabPath, fbxPrefabPath,
5656
imported));
57+
58+
// Create an empty (and duplicate) FbxPrefab.cs to confuse the auto-updater. Make sure it's properly confused.
59+
// This test only works in 2018.2 or later; 2018.1 hard-codes the path.
60+
#if UNITY_2018_2_OR_LATER
61+
System.IO.File.WriteAllText(this.filePath + '/' + FbxPrefabAutoUpdater.FBX_PREFAB_FILE, "class CeciNestPasUnFbxPrefab : UnityEngine.MonoBehaviour {}\n");
62+
AssetDatabase.Refresh(); // force refresh
63+
// TODO: assert that we're getting a log warning, but this still works
64+
Assert.IsTrue (FbxPrefabAutoUpdater.MayHaveFbxPrefabToFbxAsset (m_prefabPath, fbxPrefabPath, imported));
65+
#endif
5766
}
5867

5968
[Test]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changes in Fbx Exporter
2+
3+
## [0.0.2-preview] - 2018-05-02
4+
5+
- Initial version for Package Manager

Packages/com.unity.formats.fbx/CHANGELOG.md.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/com.unity.formats.fbx/Editor/Scripts/FbxExporter.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public class ModelExporter : System.IDisposable
7777
@"";
7878

7979
/// <summary>
80-
/// Path to the README file in Unity's virtual file system.
80+
/// Path to the CHANGELOG file in Unity's virtual file system. Used to get the version number.
8181
/// </summary>
82-
const string ReadmeRelativePath = "Assets/com.unity.formats.fbx/README.txt";
82+
const string ChangeLogPath = "Packages/com.unity.formats.fbx/CHANGELOG.md";
8383

8484
// NOTE: The ellipsis at the end of the Menu Item name prevents the context
8585
// from being passed to command, thus resulting in OnContextItem()
@@ -240,32 +240,34 @@ public static Material DefaultMaterial {
240240
/// </summary>
241241
public static string GetVersionFromReadme()
242242
{
243-
if (string.IsNullOrEmpty (ReadmeRelativePath)) {
244-
Debug.LogWarning ("Missing relative path to README");
245-
return null;
246-
}
247-
if (!File.Exists (ReadmeRelativePath)) {
248-
Debug.LogWarning (string.Format("Could not find README.txt at: {0}", ReadmeRelativePath));
243+
if (!File.Exists (ChangeLogPath)) {
244+
Debug.LogWarning (string.Format("Could not find version number, the ChangeLog file is missing from: {0}", ChangeLogPath));
249245
return null;
250246
}
251247

252-
try{
253-
var versionHeader = "VERSION:";
254-
var lines = File.ReadAllLines (ReadmeRelativePath);
248+
try {
249+
// The format is:
250+
// ## [a.b.c-whatever] - yyyy-mm-dd
251+
// we extract the first bit.
252+
var lines = File.ReadAllLines (ChangeLogPath);
255253
foreach (var line in lines) {
256-
if (line.StartsWith(versionHeader)) {
257-
var version = line.Replace (versionHeader, "");
254+
var match = System.Text.RegularExpressions.Regex.Match(line, @"^\s*##\s*\[(.*)\]");
255+
if (match.Success) {
256+
var version = match.Groups[1].Value;
258257
return version.Trim ();
259258
}
260259
}
260+
261+
// If we're here, we didn't find any match.
262+
Debug.LogWarning (string.Format("Could not find most recent version number in {0}", ChangeLogPath));
263+
return null;
261264
}
262265
catch(IOException e){
263266
Debug.LogException (e);
264-
Debug.LogWarning (string.Format("Error will reading file {0} ({1})", ReadmeRelativePath, e));
267+
Debug.LogWarning (string.Format("Error reading file {0} ({1})", ChangeLogPath, e));
265268
return null;
266269
}
267-
Debug.LogWarning (string.Format("Could not find version number in README.txt at: {0}", ReadmeRelativePath));
268-
return null;
270+
269271
}
270272

271273
/// <summary>

Packages/com.unity.formats.fbx/Editor/Scripts/FbxPrefabAutoUpdater.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ namespace FbxExporters
2424
/// </summary>
2525
public /*static*/ class FbxPrefabAutoUpdater : UnityEditor.AssetPostprocessor
2626
{
27-
#if UNITY_EDITOR
28-
public const string FBX_PREFAB_FILE = "/FbxPrefab.cs";
29-
#else
27+
#if COM_UNITY_FORMATS_FBX_AS_ASSET
3028
public const string FBX_PREFAB_FILE = "/UnityFbxPrefab.dll";
29+
#elif UNITY_2018_2_OR_LATER
30+
public const string FBX_PREFAB_FILE = "/FbxPrefab.cs";
31+
#else // Unity 2018.1 and fbx installed as a package
32+
public const string FBX_PREFAB_FILE = "Packages/com.unity.formats.fbx/Runtime/FbxPrefab.cs";
3133
#endif
3234

3335
const string MenuItemName = "GameObject/Update from FBX";
@@ -37,17 +39,36 @@ namespace FbxExporters
3739

3840
public static string FindFbxPrefabAssetPath()
3941
{
42+
#if COM_UNITY_FORMATS_FBX_AS_ASSET || UNITY_2018_2_OR_LATER
4043
// Find guids that are scripts that look like FbxPrefab.
4144
// That catches FbxPrefabTest too, so we have to make sure.
4245
var allGuids = AssetDatabase.FindAssets("FbxPrefab t:MonoScript");
46+
string foundPath = "";
4347
foreach (var guid in allGuids) {
4448
var path = AssetDatabase.GUIDToAssetPath(guid);
4549
if (path.EndsWith(FBX_PREFAB_FILE)) {
46-
return path;
50+
if (!string.IsNullOrEmpty(foundPath)) {
51+
// How did this happen? Anyway, just don't try.
52+
Debug.LogWarning(string.Format("{0} found in multiple places; did you forget to delete one of these?\n{1}\n{2}",
53+
FBX_PREFAB_FILE.Substring(1), foundPath, path));
54+
return "";
55+
}
56+
foundPath = path;
4757
}
4858
}
49-
Debug.LogError(string.Format("{0} not found; are you trying to uninstall {1}?", FBX_PREFAB_FILE.Substring(1), FbxExporters.Editor.ModelExporter.PACKAGE_UI_NAME));
59+
Debug.LogWarning(string.Format("{0} not found; are you trying to uninstall {1}?", FBX_PREFAB_FILE.Substring(1), FbxExporters.Editor.ModelExporter.PACKAGE_UI_NAME));
5060
return "";
61+
#else
62+
// In Unity 2018.1, FindAssets can't find FbxPrefab.cs in a package.
63+
// So we hardcode the path.
64+
var path = FBX_PREFAB_FILE;
65+
if (System.IO.File.Exists(System.IO.Path.GetFullPath(path))) {
66+
return path;
67+
} else {
68+
Debug.LogWarning(string.Format("{0} not found; are you trying to uninstall {1}?", FBX_PREFAB_FILE, FbxExporters.Editor.ModelExporter.PACKAGE_UI_NAME));
69+
return "";
70+
}
71+
#endif
5172
}
5273

5374
public static bool IsFbxAsset(string assetPath) {
@@ -68,19 +89,22 @@ public static bool IsPrefabAsset(string assetPath) {
6889
public static bool MayHaveFbxPrefabToFbxAsset(string prefabPath,
6990
string fbxPrefabScriptPath, HashSet<string> fbxImported) {
7091
var depPaths = AssetDatabase.GetDependencies(prefabPath, recursive: false);
71-
bool dependsOnFbxPrefab = false;
72-
bool dependsOnImportedFbx = false;
92+
93+
// If we can find the path to the FbxPrefab, check that the prefab depends on it.
94+
// If we can't find FbxPrefab.cs then just ignore that dependence requirement.
95+
if (!string.IsNullOrEmpty(fbxPrefabScriptPath)) {
96+
if (!depPaths.Contains(fbxPrefabScriptPath)) {
97+
return false;
98+
}
99+
}
100+
101+
// We found (or don't care about) the FbxPrefab, now check if we
102+
// depend on any of the imported fbx files.
73103
foreach (var dep in depPaths) {
74-
if (dep == fbxPrefabScriptPath) {
75-
if (dependsOnImportedFbx) { return true; }
76-
dependsOnFbxPrefab = true;
77-
} else if (fbxImported.Contains(dep)) {
78-
if (dependsOnFbxPrefab) { return true; }
79-
dependsOnImportedFbx = true;
104+
if (fbxImported.Contains(dep)) {
105+
return true;
80106
}
81107
}
82-
// Either none or only one of the conditions was true, which
83-
// means this prefab certainly doesn't match.
84108
return false;
85109
}
86110

0 commit comments

Comments
 (0)