Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AndroidAssetProvider.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 #if SILICONSTUDIO_PLATFORM_ANDROID
4 using System.IO;
5 
6 namespace SiliconStudio.Core.IO
7 {
8  /// <summary>
9  /// A FileProvider that will load file from Android AssetManager and cache them on the SDCARD for reading.
10  /// </summary>
11  public class AndroidAssetProvider : VirtualFileProviderBase
12  {
13  private readonly string assetRoot;
14  private FileSystemProvider fileSystemProvider;
15 
16  public AndroidAssetProvider(string rootPath, string assetRoot, string cachePath) : base(rootPath)
17  {
18  fileSystemProvider = new FileSystemProvider(rootPath + "/cache", cachePath);
19  this.assetRoot = assetRoot;
20  }
21 
22  public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamType = StreamFlags.None)
23  {
24  // For now, block multithreading (not sure it can work or not)
25  lock (fileSystemProvider)
26  {
27  if (!fileSystemProvider.FileExists(url))
28  {
29  // Ensure top directory exists
30  fileSystemProvider.CreateDirectory(VirtualFileSystem.GetParentFolder(url));
31 
32  using (var asset = PlatformAndroid.Context.Assets.Open(assetRoot + url))
33  using (var output = fileSystemProvider.OpenStream(url, VirtualFileMode.CreateNew, VirtualFileAccess.Write, share, streamType))
34  asset.CopyTo(output);
35  }
36  }
37 
38  return fileSystemProvider.OpenStream(url, mode, access);
39  }
40 
41  public override bool FileExists(string url)
42  {
43  try
44  {
45  using (var asset = PlatformAndroid.Context.Assets.Open(assetRoot + url))
46  {
47  return true;
48  }
49  }
50  catch (IOException)
51  {
52  return false;
53  }
54  }
55 
56  public override string GetAbsolutePath(string path)
57  {
58  return fileSystemProvider.GetAbsolutePath(path);
59  }
60  }
61 }
62 #endif
VirtualFileShare
File share capabilities, equivalent of System.IO.FileShare.
VirtualFileAccess
File access equivalent of System.IO.FileAccess.
VirtualFileMode
File mode equivalent of System.IO.FileMode.
StreamFlags
Describes the different type of streams.
Definition: StreamFlags.cs:11