Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
AndroidSdCardExternalContentProvider.cs
浏览该文件的文档.
1#if ANDROID
2using Engine;
3using Game;
4public class AndroidSdCardExternalContentProvider : IExternalContentProvider {
5 string m_rootDirectory;
6 public static string fName = "AndroidSdCardExternalContentProvider";
7 public string DisplayName => LanguageControl.Get(fName, 1);
8
9 public string Description {
10 get {
11 InitializeFilesystemAccess();
12 return m_rootDirectory;
13 }
14 }
15
16 public bool SupportsListing => true;
17
18 public bool SupportsLinks => false;
19
20 public bool RequiresLogin => false;
21
22 public bool IsLoggedIn => true;
23
24 public bool IsLocalProvider => true;
25
26 public void Dispose() { }
27
28 public void Login(CancellableProgress progress, Action success, Action<Exception> failure) => failure(new NotSupportedException());
29
30 public void Logout() {
31 throw new NotSupportedException();
32 }
33
34 public void List(string path, CancellableProgress progress, Action<ExternalContentEntry> success, Action<Exception> failure) {
35 ThreadPool.QueueUserWorkItem(
36 delegate {
37 try {
38 InitializeFilesystemAccess();
39 string internalPath = ToInternalPath(path);
40 ExternalContentEntry entry = GetDirectoryEntry(internalPath, true);
41 Dispatcher.Dispatch(delegate { success(entry); });
42 }
43 catch (Exception ex) {
44 Dispatcher.Dispatch(delegate { failure(ex); });
45 }
46 }
47 );
48 }
49
50 public void Download(string path, CancellableProgress progress, Action<Stream> success, Action<Exception> failure) {
51 ThreadPool.QueueUserWorkItem(
52 delegate {
53 try {
54 InitializeFilesystemAccess();
55 string path2 = ToInternalPath(path);
56 FileStream stream = new(path2, FileMode.Open, FileAccess.Read, FileShare.Read);
57 Dispatcher.Dispatch(delegate { success(stream); });
58 }
59 catch (Exception ex) {
60 Dispatcher.Dispatch(delegate { failure(ex); });
61 }
62 }
63 );
64 }
65
66 public void Upload(string path, Stream stream, CancellableProgress progress, Action<string> success, Action<Exception> failure) {
67 ThreadPool.QueueUserWorkItem(
68 delegate {
69 try {
70 InitializeFilesystemAccess();
71 string uniquePath = GetUniquePath(ToInternalPath(path));
72 string po = uniquePath;
73 if (po.StartsWith("android:")) {
74 po = Storage.GetSystemPath(po);
75 }
76 string pp = Storage.GetDirectoryName(po);
77 Directory.CreateDirectory(pp);
78 using (FileStream destination = new(po, FileMode.Create, FileAccess.Write, FileShare.None)) {
79 stream.CopyTo(destination);
80 }
81 Dispatcher.Dispatch(delegate { success(uniquePath); });
82 }
83 catch (Exception ex) {
84 Dispatcher.Dispatch(delegate { failure(ex); });
85 }
86 }
87 );
88 }
89
90 public void Link(string path, CancellableProgress progress, Action<string> success, Action<Exception> failure) {
91 failure(new NotSupportedException());
92 }
93
94 public ExternalContentEntry GetDirectoryEntry(string internalPath, bool scanContents) {
95 ExternalContentEntry externalContentEntry = new() {
96 Type = ExternalContentType.Directory, Path = ToExternalPath(internalPath), Time = new DateTime(1970, 1, 1)
97 };
98 if (scanContents) {
99 string[] directories = Directory.GetDirectories(internalPath);
100 foreach (string internalPath2 in directories) {
101 externalContentEntry.ChildEntries.Add(GetDirectoryEntry(internalPath2, false));
102 }
103 directories = Directory.GetFiles(internalPath);
104 foreach (string text in directories) {
105 FileInfo fileInfo = new(text);
106 ExternalContentEntry externalContentEntry2 = new() {
107 Type = ExternalContentManager.ExtensionToType(Path.GetExtension(text)),
108 Path = ToExternalPath(text),
109 Size = fileInfo.Length,
110 Time = fileInfo.CreationTime
111 };
112 externalContentEntry.ChildEntries.Add(externalContentEntry2);
113 }
114 }
115 return externalContentEntry;
116 }
117
118 public static string GetUniquePath(string path) {
119 int num = 1;
120 string text = path;
121 string directoryName = Path.GetDirectoryName(path);
122 if (directoryName == null) {
123 return path;
124 }
125 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
126 string extension = Path.GetExtension(path);
127 while (Storage.FileExists(text)
128 && num < 1000) {
129 string path2 = fileNameWithoutExtension + num + extension;
130 text = Path.Combine(directoryName, path2);
131 num++;
132 }
133 return text;
134 }
135
136 public string ToExternalPath(string internalPath) => Path.GetFullPath(internalPath);
137
138 public string ToInternalPath(string externalPath) => Path.Combine(m_rootDirectory, externalPath);
139
140 public void InitializeFilesystemAccess() {
141 //Java.IO.File externalFilesDir = ((Context)Window.Activity).GetExternalFilesDir((string)null);
142 m_rootDirectory = $"{RunPath.AndroidFilePath}/files";
143 if (!Storage.DirectoryExists(m_rootDirectory)) {
144 Storage.CreateDirectory(m_rootDirectory);
145 }
146 }
147}
148#endif
List< ExternalContentEntry > ChildEntries