Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
FileSystemProvider.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.Globalization;
5 using System.IO;
6 
7 namespace SiliconStudio.Core.IO
8 {
9  /// <summary>
10  /// A file system implementation for IVirtualFileProvider.
11  /// </summary>
13  {
14 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
15  public static readonly char VolumeSeparatorChar = ':';
16  public static readonly char DirectorySeparatorChar = '\\';
17 #else
18  public static readonly char VolumeSeparatorChar = Path.VolumeSeparatorChar;
19  public static readonly char DirectorySeparatorChar = Path.DirectorySeparatorChar;
20 #endif
21  public static readonly char AltDirectorySeparatorChar = AltDirectorySeparatorChar == '/' ? '\\' : '/';
22 
23  /// <summary>
24  /// Base path of this provider (every path will be relative to this one).
25  /// </summary>
26  private string localBasePath;
27 
28  /// <summary>
29  /// Initializes a new instance of the <see cref="FileSystemProvider" /> class with the given base path.
30  /// </summary>
31  /// <param name="rootPath">The root path of this provider.</param>
32  /// <param name="localBasePath">The path to a local directory where this instance will load the files from.</param>
33  public FileSystemProvider(string rootPath, string localBasePath) : base(rootPath)
34  {
35  ChangeBasePath(localBasePath);
36  }
37 
38  public void ChangeBasePath(string basePath)
39  {
40  localBasePath = basePath;
41 
42  if (localBasePath != null)
43  localBasePath = localBasePath.Replace(AltDirectorySeparatorChar, DirectorySeparatorChar);
44 
45  // Ensure localBasePath ends with a \
46  if (localBasePath != null && !localBasePath.EndsWith(DirectorySeparatorChar.ToString()))
47  localBasePath = localBasePath + DirectorySeparatorChar;
48  }
49 
50  protected virtual string ConvertUrlToFullPath(string url)
51  {
52  if (localBasePath == null)
53  return url;
54  return localBasePath + url.Replace(VirtualFileSystem.DirectorySeparatorChar, DirectorySeparatorChar);
55  }
56 
57  protected virtual string ConvertFullPathToUrl(string path)
58  {
59  if (localBasePath == null)
60  return path;
61 
62  if (!path.StartsWith(localBasePath, StringComparison.OrdinalIgnoreCase))
63  throw new InvalidOperationException("Trying to convert back a path that is not in this file system provider.");
64 
65  return path.Substring(localBasePath.Length).Replace(DirectorySeparatorChar, VirtualFileSystem.DirectorySeparatorChar);
66  }
67 
68  public override bool DirectoryExists(string url)
69  {
70  var path = ConvertUrlToFullPath(url);
71  return NativeFile.DirectoryExists(path);
72  }
73 
74  /// <inheritdoc/>
75  public override void CreateDirectory(string url)
76  {
77  var path = ConvertUrlToFullPath(url);
78  try
79  {
80  NativeFile.DirectoryCreate(path);
81  }
82  catch (Exception ex)
83  {
84  throw new InvalidOperationException("Unable to create directory [{0}]".ToFormat(path), ex);
85  }
86  }
87 
88  /// <inheritdoc/>
89  public override bool FileExists(string url)
90  {
91  return NativeFile.FileExists(ConvertUrlToFullPath(url));
92  }
93 
94  public override long FileSize(string url)
95  {
96  return NativeFile.FileSize(ConvertUrlToFullPath(url));
97  }
98 
99  /// <inheritdoc/>
100  public override void FileDelete(string url)
101  {
102  NativeFile.FileDelete(ConvertUrlToFullPath(url));
103  }
104  }
105 }
Virtual abstraction over a file system. It handles access to files, http, packages, path rewrite, etc...
FileSystemProvider(string rootPath, string localBasePath)
Initializes a new instance of the FileSystemProvider class with the given base path.
Abstract base class for IVirtualFileProvider.
A file system implementation for IVirtualFileProvider.
override long FileSize(string url)
Returns the size of the specified file in bytes The file or directory for which to obtain sizeA long ...
override void CreateDirectory(string url)
Creates all directories so that url exists. The URL.
virtual string ConvertFullPathToUrl(string path)
virtual string ConvertUrlToFullPath(string url)
override bool FileExists(string url)
Determines whether the specified path points to an existing file. The path.
override bool DirectoryExists(string url)
Determines whether the specified path points to an existing directory.
override void FileDelete(string url)
Deletes the specified file. The URL.