Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
DiskExternalContentProvider.cs
浏览该文件的文档.
1using Engine;
2
3namespace Game {
5 public string DisplayName => LanguageControl.Get(fName, "DisplayName");
6
7 public bool SupportsLinks => false;
8
9 public bool SupportsListing => true;
10
11 public bool RequiresLogin => false;
12
13 public bool IsLoggedIn => true;
14
15 public static string fName = "DiskExternalContentProvider";
16
17 public static string LocalPath = AppDomain.CurrentDomain.BaseDirectory;
18
19 public bool IsLocalProvider => true;
20
21 public string Description => LanguageControl.Get(fName, "Description");
22
24 if (!Directory.Exists(LocalPath)) {
25 Directory.CreateDirectory(LocalPath);
26 }
27 }
28
29 public void Dispose() { }
30
31 public void Download(string path, CancellableProgress progress, Action<Stream> success, Action<Exception> failure) {
32 if (!File.Exists(path)) {
33 failure(new FileNotFoundException());
34 return;
35 }
36 FileStream fileStream = File.OpenRead(path);
37 ThreadPool.QueueUserWorkItem(
38 delegate {
39 try {
40 success(fileStream);
41 }
42 catch (Exception ex) {
43 failure(ex);
44 }
45 }
46 );
47 }
48
49 public void Link(string path, CancellableProgress progress, Action<string> success, Action<Exception> failure) {
50 failure(new NotSupportedException());
51 }
52
53 public void List(string path, CancellableProgress progress, Action<ExternalContentEntry> success, Action<Exception> failure) {
54 ThreadPool.QueueUserWorkItem(
55 delegate {
56 try {
57 string internalPath = path;
58 ExternalContentEntry entry = GetDirectoryEntry(internalPath, true);
59 success(entry);
60 }
61 catch (Exception ex) {
62 failure(ex);
63 }
64 }
65 );
66 }
67
68 public void Login(CancellableProgress progress, Action success, Action<Exception> failure) {
69 failure(new NotSupportedException());
70 }
71
72 public void Logout() {
73 throw new NotSupportedException();
74 }
75
76 public void Upload(string path, Stream stream, CancellableProgress progress, Action<string> success, Action<Exception> failure) {
77 ThreadPool.QueueUserWorkItem(
78 delegate {
79 try {
80 string destinationPath = Path.Combine(LocalPath, path);
81 using (FileStream destination = new(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None)) {
82 stream.CopyTo(destination);
83 }
84 Dispatcher.Dispatch(delegate { success(destinationPath); });
85 }
86 catch (Exception ex) {
87 Dispatcher.Dispatch(delegate { failure(ex); });
88 }
89 }
90 );
91 }
92
93 public ExternalContentEntry GetDirectoryEntry(string internalPath, bool scanContents) {
94 ExternalContentEntry externalContentEntry = new() {
95 Type = ExternalContentType.Directory, Path = internalPath, Time = new DateTime(1970, 1, 1)
96 };
97 if (scanContents) {
98 if (internalPath.Length == 2
99 && internalPath[1] == ':') {
100 internalPath += '/';
101 }
102 string[] directories = Directory.GetDirectories(internalPath);
103 foreach (string internalPath2 in directories) {
104 externalContentEntry.ChildEntries.Add(GetDirectoryEntry(internalPath2, false));
105 }
106 directories = Directory.GetFiles(internalPath);
107 foreach (string text in directories) {
108 FileInfo fileInfo = new(text);
109 ExternalContentEntry externalContentEntry2 = new() {
110 Type = ExternalContentManager.ExtensionToType(Path.GetExtension(text)),
111 Path = text,
112 Size = fileInfo.Length,
113 Time = fileInfo.CreationTime
114 };
115 externalContentEntry.ChildEntries.Add(externalContentEntry2);
116 }
117 }
118 return externalContentEntry;
119 }
120 }
121}
static void Dispatch(Action action, bool waitUntilCompleted=false)
void Login(CancellableProgress progress, Action success, Action< Exception > failure)
void Upload(string path, Stream stream, CancellableProgress progress, Action< string > success, Action< Exception > failure)
void Link(string path, CancellableProgress progress, Action< string > success, Action< Exception > failure)
ExternalContentEntry GetDirectoryEntry(string internalPath, bool scanContents)
void List(string path, CancellableProgress progress, Action< ExternalContentEntry > success, Action< Exception > failure)
void Download(string path, CancellableProgress progress, Action< Stream > success, Action< Exception > failure)
List< ExternalContentEntry > ChildEntries
static ExternalContentType ExtensionToType(string extension)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串