@@ -280,21 +280,56 @@ private void BrowseBtn_Click(object sender, EventArgs e)
280
280
// http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
281
281
public static string GetRelativePath ( string fromPath , string toPath )
282
282
{
283
- Win32 . FileAttributes fromAttr = GetPathAttribute ( fromPath ) ;
284
- Win32 . FileAttributes toAttr = GetPathAttribute ( toPath ) ;
285
-
286
- var path = new StringBuilder ( 260 ) ; // MAX_PATH
287
- if ( Win32 . PathRelativePathTo (
288
- path ,
289
- fromPath ,
290
- fromAttr ,
291
- toPath ,
292
- toAttr ) == false )
283
+ if ( ! BizHawk . Common . PlatformLinkedLibSingleton . RunningOnUnix )
293
284
{
294
- throw new ArgumentException ( "Paths must have a common prefix" ) ;
285
+ Win32 . FileAttributes fromAttr = GetPathAttribute ( fromPath ) ;
286
+ Win32 . FileAttributes toAttr = GetPathAttribute ( toPath ) ;
287
+
288
+ var path = new StringBuilder ( 260 ) ; // MAX_PATH
289
+ if ( Win32 . PathRelativePathTo (
290
+ path ,
291
+ fromPath ,
292
+ fromAttr ,
293
+ toPath ,
294
+ toAttr ) == false )
295
+ {
296
+ throw new ArgumentException ( "Paths must have a common prefix" ) ;
297
+ }
298
+
299
+ return path . ToString ( ) ;
295
300
}
301
+ else
302
+ {
303
+ // mono doesnt know about shlwapi.dll that the code above tries to call
304
+ // it is available in WINE, but since this is the only call to that function,
305
+ // it's probably easier to get the relative path a different way
306
+ // this may actually work on windows as well - so maybe can replace the above when we move to .netcore
307
+ var dirSepChar = Path . DirectorySeparatorChar ;
308
+ string from = ! fromPath . EndsWith ( dirSepChar . ToString ( ) )
309
+ ? fromPath + dirSepChar
310
+ : fromPath ;
311
+ string to = ! toPath . EndsWith ( dirSepChar . ToString ( ) )
312
+ ? toPath + dirSepChar
313
+ : toPath ;
314
+
315
+ Uri fromUri = new Uri ( from ) ;
316
+ Uri toUri = new Uri ( to ) ;
317
+
318
+ if ( fromUri . Scheme != toUri . Scheme )
319
+ {
320
+ return toPath ;
321
+ }
322
+
323
+ Uri relativeUri = fromUri . MakeRelativeUri ( toUri ) ;
324
+ string relativePath = Uri . UnescapeDataString ( relativeUri . ToString ( ) ) ;
325
+
326
+ if ( string . Equals ( toUri . Scheme , Uri . UriSchemeFile , StringComparison . OrdinalIgnoreCase ) )
327
+ {
328
+ relativePath = relativePath . Replace ( Path . AltDirectorySeparatorChar , Path . DirectorySeparatorChar ) ;
329
+ }
296
330
297
- return path . ToString ( ) ;
331
+ return relativePath . TrimEnd ( dirSepChar ) ;
332
+ }
298
333
}
299
334
300
335
private static Win32 . FileAttributes GetPathAttribute ( string path )
0 commit comments