Skip to content

Commit 46bcc5b

Browse files
author
Benoit Hudson
committed
UNI-22052: fix path problems, I think.
I'm adding tests next because it's getting tricky to verify by eye.
1 parent 0fbed1b commit 46bcc5b

File tree

2 files changed

+68
-54
lines changed

2 files changed

+68
-54
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,16 @@ public static GameObject[] CreateInstantiatedModelPrefab (GameObject [] unityGam
121121
continue;
122122
}
123123

124-
// make filepath relative to project folder
125-
if (fbxFileName.StartsWith (Application.dataPath, System.StringComparison.CurrentCulture))
126-
{
127-
fbxFileName = "Assets" + fbxFileName.Substring (Application.dataPath.Length);
128-
}
124+
// make filepath relative to assets folder
125+
var relativePath = FbxExporters.EditorTools.ExportSettings.ConvertToAssetRelativePath(fbxFileName);
129126

130127
// refresh the assetdata base so that we can query for the model
131128
AssetDatabase.Refresh ();
132129

133-
// replace w Model asset
134-
Object unityMainAsset = AssetDatabase.LoadMainAssetAtPath (fbxFileName);
130+
// Replace w Model asset. LoadMainAssetAtPath wants a path
131+
// relative to the project, not relative to the assets
132+
// folder.
133+
Object unityMainAsset = AssetDatabase.LoadMainAssetAtPath(Path.Combine("Assets", relativePath));
135134

136135
if (unityMainAsset != null) {
137136
Object unityObj = PrefabUtility.InstantiatePrefab (unityMainAsset);

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override void OnInspectorGUI() {
6060

6161
// Unless the user canceled, make sure they chose something in the Assets folder.
6262
if (!string.IsNullOrEmpty (fullPath)) {
63-
var relativePath = GetRelativePath(Application.dataPath, fullPath);
63+
var relativePath = ExportSettings.ConvertToAssetRelativePath(fullPath);
6464
if (string.IsNullOrEmpty(relativePath)
6565
|| relativePath == ".."
6666
|| relativePath.StartsWith(".." + Path.DirectorySeparatorChar)) {
@@ -81,39 +81,6 @@ public override void OnInspectorGUI() {
8181
}
8282
}
8383

84-
private string GetRelativePath(string fromDir, string toDir) {
85-
// https://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
86-
// With fixes to handle that fromDir and toDir are both directories (not files).
87-
if (String.IsNullOrEmpty(fromDir)) throw new ArgumentNullException("fromDir");
88-
if (String.IsNullOrEmpty(toDir)) throw new ArgumentNullException("toDir");
89-
90-
// MakeRelativeUri assumes the path is a file unless it ends with a
91-
// path separator, so add one. Having multiple in a row is no problem.
92-
fromDir += Path.DirectorySeparatorChar;
93-
toDir += Path.DirectorySeparatorChar;
94-
95-
// Workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=5921
96-
fromDir += Path.DirectorySeparatorChar;
97-
98-
Uri fromUri = new Uri(fromDir);
99-
Uri toUri = new Uri(toDir);
100-
101-
if (fromUri.Scheme != toUri.Scheme) { return null; } // path can't be made relative.
102-
103-
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
104-
String relativePath = Uri.UnescapeDataString(relativeUri.ToString());
105-
106-
if (string.IsNullOrEmpty(relativePath)) {
107-
// The relative path is empty if it's the same directory.
108-
relativePath = "./";
109-
}
110-
111-
if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase)) {
112-
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
113-
}
114-
115-
return relativePath;
116-
}
11784
}
11885

11986
[FilePath("ProjectSettings/FbxExportSettings.asset",FilePathAttribute.Location.ProjectFolder)]
@@ -148,19 +115,7 @@ public static string GetRelativeSavePath() {
148115
if (string.IsNullOrEmpty(relativePath)) {
149116
relativePath = kDefaultSavePath;
150117
}
151-
152-
// Normalize to the platform path separator.
153-
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar,
154-
Path.DirectorySeparatorChar);
155-
156-
// Trim off trailing slashes. If all we had was slashes, we're at
157-
// the root of the Application.dataPath so return "."
158-
relativePath = relativePath.TrimEnd(Path.DirectorySeparatorChar);
159-
if (string.IsNullOrEmpty(relativePath)) {
160-
relativePath = ".";
161-
}
162-
163-
return relativePath;
118+
return NormalizeRelativePath(relativePath);
164119
}
165120

166121
/// <summary>
@@ -183,6 +138,66 @@ public static void SetRelativeSavePath(string newPath) {
183138
.TrimEnd(Path.DirectorySeparatorChar);
184139
}
185140

141+
/// <summary>
142+
/// Convert an absolute path into a relative path like what you would
143+
/// get from GetRelativeSavePath.
144+
///
145+
/// Returns an empty string if the path is invalid.
146+
/// The path uses platform path separators, and no trailing or leading
147+
/// slashes.
148+
/// </summary>
149+
public static string ConvertToAssetRelativePath(string fullPathInAssets)
150+
{
151+
return GetRelativePath(Application.dataPath, fullPathInAssets);
152+
}
153+
154+
private static string GetRelativePath(string fromDir, string toDir) {
155+
// https://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
156+
// With fixes to handle that fromDir and toDir are both directories (not files).
157+
if (String.IsNullOrEmpty(fromDir)) throw new ArgumentNullException("fromDir");
158+
if (String.IsNullOrEmpty(toDir)) throw new ArgumentNullException("toDir");
159+
160+
// MakeRelativeUri assumes the path is a file unless it ends with a
161+
// path separator, so add one. Having multiple in a row is no problem.
162+
fromDir += Path.DirectorySeparatorChar;
163+
toDir += Path.DirectorySeparatorChar;
164+
165+
// Workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=5921
166+
fromDir += Path.DirectorySeparatorChar;
167+
168+
Uri fromUri = new Uri(fromDir);
169+
Uri toUri = new Uri(toDir);
170+
171+
if (fromUri.Scheme != toUri.Scheme) { return null; } // path can't be made relative.
172+
173+
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
174+
String relativePath = Uri.UnescapeDataString(relativeUri.ToString());
175+
176+
if (string.IsNullOrEmpty(relativePath)) {
177+
// The relative path is empty if it's the same directory.
178+
relativePath = ".";
179+
}
180+
181+
return NormalizeRelativePath(relativePath);
182+
}
183+
184+
private static string NormalizeRelativePath(string relativePath)
185+
{
186+
// Normalize to the platform path separator.
187+
relativePath = relativePath.Replace(
188+
Path.AltDirectorySeparatorChar,
189+
Path.DirectorySeparatorChar);
190+
191+
// Trim off leading and trailing slashes. If all we had was
192+
// slashes, we're at the root of the Application.dataPath so return
193+
// "."
194+
relativePath = relativePath.Trim(Path.DirectorySeparatorChar);
195+
if (string.IsNullOrEmpty(relativePath)) {
196+
relativePath = ".";
197+
}
198+
return relativePath;
199+
}
200+
186201
[MenuItem("Edit/Project Settings/Fbx Export", priority = 300)]
187202
static void ShowManager()
188203
{

0 commit comments

Comments
 (0)