Skip to content

Commit 82c8245

Browse files
author
Benoit Hudson
committed
Fix (hopefully) bugs in windows path names.
Use GetRandomFbxFilePath() or GetRandomPrefabAssetPath() to make sure to get the right path on all platforms.
1 parent 01c337e commit 82c8245

File tree

4 files changed

+70
-24
lines changed

4 files changed

+70
-24
lines changed

Assets/FbxExporters/Editor/UnitTests/ExportPerformanceTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void TestPerformance ()
6262
{
6363
Assert.IsNotNull (m_toExport);
6464

65-
var filename = GetRandomFileNamePath();
65+
var filename = GetRandomFbxFilePath();
6666

6767
UnityEngine.Debug.unityLogger.logEnabled = false;
6868

@@ -77,4 +77,4 @@ public void TestPerformance ()
7777
Assert.LessOrEqual(m_stopwatch.ElapsedMilliseconds, PerformanceThresholdMilliseconds);
7878
}
7979
}
80-
}
80+
}

Assets/FbxExporters/Editor/UnitTests/ExporterTestBase.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ protected string filePath {
3737

3838
private string MakeFileName(string baseName = null, string prefixName = null, string extName = null)
3939
{
40-
if (baseName==null)
41-
baseName = Path.GetRandomFileName();
40+
if (baseName==null) {
41+
// GetRandomFileName makes a random 8.3 filename
42+
// We don't want the extension.
43+
baseName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
44+
}
4245

4346
if (prefixName==null)
4447
prefixName = this.fileNamePrefix;
@@ -49,7 +52,31 @@ private string MakeFileName(string baseName = null, string prefixName = null, st
4952
return prefixName + baseName + extName;
5053
}
5154

52-
protected string GetRandomFileNamePath(string pathName = null, string prefixName = null, string extName = null)
55+
/// <summary>
56+
/// Create a random path for a file.
57+
///
58+
/// By default the pathName is null, which defaults to a particular
59+
/// folder in the Assets folder that will be deleted on termination of
60+
/// this test.
61+
///
62+
/// By default the prefix is a fixed string. You can set it differently if you care.
63+
///
64+
/// By default the extension is ".fbx". You can set it differently if you care.
65+
///
66+
/// By default we use platform path separators. If you want a random
67+
/// asset path e.g. for AssetDatabase.LoadMainAssetAtPath or for
68+
/// PrefabUtility.CreatePrefab, you need to use the '/' as separator
69+
/// (even on windows).
70+
///
71+
/// See also convenience functions like:
72+
/// GetRandomPrefabAssetPath()
73+
/// GetRandomFbxFilePath()
74+
/// </summary>
75+
protected string GetRandomFileNamePath(
76+
string pathName = null,
77+
string prefixName = null,
78+
string extName = null,
79+
bool unityPathSeparator = false)
5380
{
5481
string temp;
5582

@@ -60,12 +87,32 @@ protected string GetRandomFileNamePath(string pathName = null, string prefixName
6087
// repeat until you find a file that does not already exist
6188
do {
6289
temp = Path.Combine (pathName, MakeFileName(prefixName: prefixName, extName: extName));
63-
6490
} while(File.Exists (temp));
6591

92+
// Unity asset paths need a slash on all platforms.
93+
if (unityPathSeparator) {
94+
temp = temp.Replace('\\', '/');
95+
}
96+
6697
return temp;
6798
}
6899

100+
/// <summary>
101+
/// Return a random .fbx path that you can use in
102+
/// the File APIs.
103+
/// </summary>
104+
protected string GetRandomFbxFilePath() {
105+
return GetRandomFileNamePath(extName: ".fbx", unityPathSeparator: false);
106+
}
107+
108+
/// <summary>
109+
/// Return a random .prefab path that you can use in
110+
/// PrefabUtility.CreatePrefab.
111+
/// </summary>
112+
protected string GetRandomPrefabAssetPath() {
113+
return GetRandomFileNamePath(extName: ".prefab", unityPathSeparator: true);
114+
}
115+
69116
/// <summary>
70117
/// Creates a test hierarchy.
71118
///

Assets/FbxExporters/Editor/UnitTests/FbxPrefabAutoUpdaterTest.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
using UnityEngine;
99
using UnityEditor;
10-
using UnityEngine.TestTools;
1110
using NUnit.Framework;
1211
using System.IO;
1312
using System.Collections.Generic;
14-
using FbxSdk;
1513

1614
namespace FbxExporters.UnitTests
1715
{
@@ -37,7 +35,7 @@ public void Init ()
3735
// Delete the object right away (don't even wait for term).
3836
var fbxInstance = PrefabUtility.InstantiatePrefab(m_fbx) as GameObject;
3937
fbxInstance.AddComponent<FbxPrefab>().SetSourceModel(m_fbx);
40-
m_prefabPath = GetRandomFileNamePath(extName: ".prefab");
38+
m_prefabPath = GetRandomPrefabAssetPath();
4139
m_prefab = PrefabUtility.CreatePrefab(m_prefabPath, fbxInstance);
4240
AssetDatabase.Refresh ();
4341
Assert.AreEqual(m_prefabPath, AssetDatabase.GetAssetPath(m_prefab));
@@ -110,17 +108,17 @@ public void ExpensivePerformanceTest ()
110108
//
111109
// Then modify one fbx model. Shouldn't take longer than 1s.
112110
var hierarchy = CreateGameObject("the_root");
113-
var baseName = GetRandomFileNamePath(extName: "");
114-
FbxExporters.Editor.ModelExporter.ExportObject(baseName + ".fbx", hierarchy);
111+
var baseName = GetRandomFbxFilePath();
112+
FbxExporters.Editor.ModelExporter.ExportObject(baseName, hierarchy);
115113

116114
// Create N fbx models by copying files. Import them all at once.
117115
var names = new string[n];
118116
names[0] = baseName;
119117
stopwatch.Reset();
120118
stopwatch.Start();
121119
for(int i = 1; i < n; ++i) {
122-
names[i] = GetRandomFileNamePath(extName : "");
123-
System.IO.File.Copy(names[0] + ".fbx", names[i] + ".fbx");
120+
names[i] = GetRandomFbxFilePath();
121+
System.IO.File.Copy(names[0], names[i]);
124122
}
125123
Debug.Log("Created fbx files in " + stopwatch.ElapsedMilliseconds);
126124

@@ -136,7 +134,7 @@ public void ExpensivePerformanceTest ()
136134
stopwatch.Start();
137135
var fbxFiles = new GameObject[n / 2];
138136
for(int i = 0; i < n / 2; ++i) {
139-
fbxFiles[i] = AssetDatabase.LoadMainAssetAtPath(names[i] + ".fbx") as GameObject;
137+
fbxFiles[i] = AssetDatabase.LoadMainAssetAtPath(names[i]) as GameObject;
140138
Assert.IsTrue(fbxFiles[i]);
141139
}
142140
Debug.Log("Loaded fbx files in " + stopwatch.ElapsedMilliseconds);
@@ -148,34 +146,35 @@ public void ExpensivePerformanceTest ()
148146
Assert.IsTrue(instance);
149147
var fbxPrefab = instance.AddComponent<FbxPrefab>();
150148
fbxPrefab.SetSourceModel(fbxFiles[i]);
151-
UnityEditor.PrefabUtility.CreatePrefab(names[i] + ".prefab", fbxFiles[i]);
149+
PrefabUtility.CreatePrefab(GetRandomPrefabAssetPath(), fbxFiles[i]);
152150
}
153151
Debug.Log("Created prefabs in " + stopwatch.ElapsedMilliseconds);
154152

155153
// Export a new hierarchy and update one fbx file.
156154
// Make sure we're timing just the assetdatabase refresh by
157155
// creating a file and then copying it, and not the FbxExporter.
158156
var newHierarchy = CreateHierarchy();
159-
FbxExporters.Editor.ModelExporter.ExportObject(names[0] + "_new.fbx", newHierarchy);
157+
var newHierarchyName = GetRandomFbxFilePath();
158+
FbxExporters.Editor.ModelExporter.ExportObject(newHierarchyName, newHierarchy);
160159
try {
161160
UnityEngine.Debug.unityLogger.logEnabled = false;
162161
stopwatch.Reset ();
163162
stopwatch.Start ();
164-
File.Copy(names[0] + "_new.fbx", names[0] + ".fbx", overwrite: true);
163+
File.Copy(newHierarchyName, names[0], overwrite: true);
165164
AssetDatabase.Refresh(); // force the update right now.
166165
} finally {
167166
UnityEngine.Debug.unityLogger.logEnabled = true;
168167
}
169168
Debug.Log("Import (one change) in " + stopwatch.ElapsedMilliseconds);
170169
Assert.LessOrEqual(stopwatch.ElapsedMilliseconds, NoUpdateTimeLimit);
171170

172-
// Try what happens when nothing gets updated.
171+
// Try what happens when no prefab gets updated.
173172
try {
174173
UnityEngine.Debug.unityLogger.logEnabled = false;
175174
stopwatch.Reset ();
176175
stopwatch.Start ();
177-
string newHierarchyFbxFile = GetRandomFileNamePath(extName : ".fbx");
178-
File.Copy(names[0] + ".fbx", newHierarchyFbxFile);
176+
string newHierarchyFbxFile = GetRandomFbxFilePath();
177+
File.Copy(names[0], newHierarchyFbxFile);
179178
AssetDatabase.Refresh(); // force the update right now.
180179
} finally {
181180
UnityEngine.Debug.unityLogger.logEnabled = true;

Assets/FbxExporters/Editor/UnitTests/FbxPrefabTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void Init() {
3535
// Convert it to FBX. The asset file will be deleted automatically
3636
// on termination.
3737
var fbxAsset = FbxExporters.Editor.ModelExporter.ExportObject(
38-
GetRandomFileNamePath(), m_original);
38+
GetRandomFbxFilePath(), m_original);
3939
m_source = AssetDatabase.LoadMainAssetAtPath(fbxAsset) as GameObject;
4040
Assert.IsTrue(m_source);
4141

@@ -46,7 +46,7 @@ public void Init() {
4646
fbxPrefab.SetSourceModel(m_source);
4747
fbxPrefab.SetAutoUpdate(true);
4848
m_autoPrefab = PrefabUtility.CreatePrefab(
49-
GetRandomFileNamePath(extName: ".prefab"),
49+
GetRandomPrefabAssetPath(),
5050
prefabInstance);
5151
}
5252

@@ -57,7 +57,7 @@ public void Init() {
5757
fbxPrefab.SetSourceModel(m_source);
5858
fbxPrefab.SetAutoUpdate(false);
5959
m_manualPrefab = PrefabUtility.CreatePrefab(
60-
GetRandomFileNamePath(extName: ".prefab"),
60+
GetRandomPrefabAssetPath(),
6161
prefabInstance);
6262
}
6363
}
@@ -148,7 +148,7 @@ public void BasicTest() {
148148
// (but is a totally different file). This will cause an update
149149
// immediately.
150150
var fbxAsset = FbxExporters.Editor.ModelExporter.ExportObject(
151-
GetRandomFileNamePath(), m_original);
151+
GetRandomFbxFilePath(), m_original);
152152
var newSource = AssetDatabase.LoadMainAssetAtPath(fbxAsset) as GameObject;
153153
Assert.IsTrue(newSource);
154154
manualPrefabComponent.SetSourceModel(newSource);

0 commit comments

Comments
 (0)