Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DriveFileProvider.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Text;
7 
8 namespace SiliconStudio.Core.IO
9 {
10  /// <summary>
11  /// Exposes the whole file system through folder similar to Cygwin.
12  /// As an example, "/c/Program Files/Test/Data.dat" would work.
13  /// </summary>
15  {
16  public static string DefaultRootPath = "/drive";
17 
18  public DriveFileProvider(string rootPath) : base(rootPath, null)
19  {
20  }
21 
22  /// <summary>
23  /// Resolves the VFS URL from a given file path.
24  /// </summary>
25  /// <param name="filePath">The file path.</param>
26  /// <returns></returns>
27  /// <exception cref="System.InvalidOperationException"></exception>
28  public string GetLocalPath(string filePath)
29  {
30 #if !NETFX_CORE
31  filePath = Path.GetFullPath(filePath);
32 #endif
33 
34  var resolveProviderResult = VirtualFileSystem.ResolveProvider(RootPath, true);
35  var provider = resolveProviderResult.Provider as DriveFileProvider;
36 
37  if (provider == null)
38  throw new InvalidOperationException();
39 
40  return provider.ConvertFullPathToUrl(filePath);
41  }
42 
43  /// <inheritdoc/>
44  protected override string ConvertUrlToFullPath(string url)
45  {
46  // Linux style: keep as is
47  if (VolumeSeparatorChar == '/')
48  return url;
49 
50  // TODO: Support more complex URL such as UNC or devices
51  // Windows style: reprocess URL like cygwin
52  var result = new StringBuilder(url.Length + 1);
53  int separatorIndex = 0;
54 
55  foreach (var c in url.ToCharArray())
56  {
58  {
59  if (separatorIndex == 1)
60  {
61  // Add volume separator on second /
62  result.Append(VolumeSeparatorChar);
63  }
64 
65  // Ignore first separator (before volume)
66  if (separatorIndex >= 1)
67  {
68  result.Append(DirectorySeparatorChar);
69  }
70 
71  separatorIndex++;
72  }
73  else
74  {
75  result.Append(c);
76  }
77  }
78 
79  return result.ToString();
80  }
81 
82  /// <inheritdoc/>
83  protected override string ConvertFullPathToUrl(string path)
84  {
85  // Linux style: keep as is
86  if (VolumeSeparatorChar == '/')
87  return path;
88 
89  // TODO: Support more complex URL such as UNC or devices
90  // Windows style: reprocess URL like cygwin
91  var result = new StringBuilder(path.Length + 1);
92 
93  result.Append(VirtualFileSystem.DirectorySeparatorChar);
94 
95  foreach (var c in path.ToCharArray())
96  {
97  if (c == VolumeSeparatorChar)
98  {
99  // TODO: More advanced validation, i.e. is there no directory separator before volume separator, etc...
100  }
101  else if (c == DirectorySeparatorChar)
102  {
103  result.Append(VirtualFileSystem.DirectorySeparatorChar);
104  }
105  else
106  {
107  result.Append(c);
108  }
109  }
110 
111  return result.ToString();
112  }
113  }
114 }
override string ConvertUrlToFullPath(string url)
Virtual abstraction over a file system. It handles access to files, http, packages, path rewrite, etc...
override string ConvertFullPathToUrl(string path)
A file system implementation for IVirtualFileProvider.
Exposes the whole file system through folder similar to Cygwin. As an example, "/c/Program Files/Test...
string GetLocalPath(string filePath)
Resolves the VFS URL from a given file path.