Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
DropboxExternalContentProvider.cs
浏览该文件的文档.
1using System.Globalization;
2using System.Text;
3using System.Text.Json;
4using System.Text.Json.Nodes;
5using Engine;
7
8namespace Game {
10 public class LoginProcessData {
11 public bool IsTokenFlow;
12 public Action Success;
13 public Action<Exception> Failure;
15
17 provider.m_loginProcessData = null;
18 Success?.Invoke();
19 }
20
21 public void Fail(DropboxExternalContentProvider provider, Exception error) {
22 provider.m_loginProcessData = null;
23 Failure?.Invoke(error);
24 }
25 }
26
27 public const string m_appKey = "1unnzwkb8igx70k";
28 public const string m_appSecret = "3i5u3j3141php7u";
29 public const string m_redirectUri = "com.candyrufusgames.survivalcraft2://redirect";
30 public const string fName = "DropboxExternalContentProvider";
31
33
34 public string DisplayName => "Dropbox";
35
36 public string Description => LanguageControl.Get(fName, IsLoggedIn ? "1" : "2");
37
38 public bool SupportsListing => true;
39
40 public bool SupportsLinks => true;
41
42 public bool RequiresLogin => true;
43
44 public bool IsLocalProvider => false;
45
46 public bool IsLoggedIn => !string.IsNullOrEmpty(SettingsManager.DropboxAccessToken);
47
52
57
58 public void Login(CancellableProgress progress, Action success, Action<Exception> failure) {
59 try {
60 if (m_loginProcessData != null) {
61 throw new InvalidOperationException(LanguageControl.Get(fName, "3"));
62 }
64 throw new InvalidOperationException(LanguageControl.Get(fName, "4"));
65 }
66 Logout();
67 progress.Cancelled += delegate {
68 if (m_loginProcessData != null) {
69 LoginProcessData loginProcessData = m_loginProcessData;
70 m_loginProcessData = null;
71 loginProcessData.Fail(this, null);
72 }
73 };
74 m_loginProcessData = new LoginProcessData { Progress = progress, Success = success, Failure = failure };
76 }
77 catch (Exception obj) {
78 failure(obj);
79 }
80 }
81
82 public void Logout() {
84 }
85
86 public void List(string path, CancellableProgress progress, Action<ExternalContentEntry> success, Action<Exception> failure) {
87 try {
89 Dictionary<string, string> dictionary = new() {
90 { "Authorization", $"Bearer {SettingsManager.DropboxAccessToken}" }, { "Content-Type", "application/json" }
91 };
92 JsonObject jsonObject = new() {
93 { "path", NormalizePath(path) },
94 { "recursive", false },
95 { "include_media_info", false },
96 { "include_deleted", false },
97 { "include_has_explicit_shared_members", false }
98 };
99 MemoryStream data = new(Encoding.UTF8.GetBytes(jsonObject.ToJsonString()));
101 "https://api.dropboxapi.com/2/files/list_folder",
102 null,
103 dictionary,
104 data,
105 progress,
106 delegate(byte[] result) {
107 try {
108 success(JsonElementToEntry(JsonDocument.Parse(result, JsonDocumentReader.DefaultJsonOptions).RootElement));
109 }
110 catch (Exception obj2) {
111 failure(obj2);
112 }
113 },
114 delegate(Exception error) { failure(error); }
115 );
116 }
117 catch (Exception obj) {
118 failure(obj);
119 }
120 }
121
122 public void Download(string path, CancellableProgress progress, Action<Stream> success, Action<Exception> failure) {
123 try {
125 JsonObject jsonObject = new() { { "path", NormalizePath(path) } };
126 Dictionary<string, string> dictionary = new() {
127 { "Authorization", $"Bearer {SettingsManager.DropboxAccessToken}" }, { "Dropbox-API-Arg", jsonObject.ToJsonString() }
128 };
130 "https://content.dropboxapi.com/2/files/download",
131 null,
132 dictionary,
133 progress,
134 delegate(byte[] result) { success(new MemoryStream(result)); },
135 delegate(Exception error) { failure(error); }
136 );
137 }
138 catch (Exception obj) {
139 failure(obj);
140 }
141 }
142
143 public void Upload(string path, Stream stream, CancellableProgress progress, Action<string> success, Action<Exception> failure) {
144 try {
146 JsonObject jsonObject = new() { { "path", NormalizePath(path) }, { "mode", "add" }, { "autorename", true }, { "mute", false } };
147 Dictionary<string, string> dictionary = new() {
148 { "Authorization", $"Bearer {SettingsManager.DropboxAccessToken}" },
149 { "Content-Type", "application/octet-stream" },
150 { "Dropbox-API-Arg", jsonObject.ToJsonString() }
151 };
153 "https://content.dropboxapi.com/2/files/upload",
154 null,
155 dictionary,
156 stream,
157 progress,
158 delegate { success(null); },
159 delegate(Exception error) { failure(error); }
160 );
161 }
162 catch (Exception obj) {
163 failure(obj);
164 }
165 }
166
167 public void Link(string path, CancellableProgress progress, Action<string> success, Action<Exception> failure) {
168 try {
170 Dictionary<string, string> dictionary = new() {
171 { "Authorization", $"Bearer {SettingsManager.DropboxAccessToken}" }, { "Content-Type", "application/json" }
172 };
173 JsonObject jsonObject = new() { { "path", NormalizePath(path) }, { "short_url", false } };
174 MemoryStream data = new(Encoding.UTF8.GetBytes(jsonObject.ToJsonString()));
176 "https://api.dropboxapi.com/2/sharing/create_shared_link",
177 null,
178 dictionary,
179 data,
180 progress,
181 delegate(byte[] result) {
182 try {
183 success(JsonElementToLinkAddress(JsonDocument.Parse(result, JsonDocumentReader.DefaultJsonOptions).RootElement));
184 }
185 catch (Exception obj2) {
186 failure(obj2);
187 }
188 },
189 delegate(Exception error) { failure(error); }
190 );
191 }
192 catch (Exception obj) {
193 failure(obj);
194 }
195 }
196
197 public void LoginLaunchBrowser() {
198 try {
199 m_loginProcessData.IsTokenFlow = true;
200 Dictionary<string, string> dictionary = new() {
201 { "response_type", "token" },
202 { "client_id", m_appKey },
203 { "redirect_uri", m_redirectUri }
204 };
205 WebBrowserManager.LaunchBrowser($"https://www.dropbox.com/oauth2/authorize?{WebManager.UrlParametersToString(dictionary)}");
206 }
207 catch (Exception error) {
208 m_loginProcessData.Fail(this, error);
209 }
210 }
211
212 public void WindowActivated() {
213 if (m_loginProcessData != null
214 && !m_loginProcessData.IsTokenFlow) {
215 LoginProcessData loginProcessData = m_loginProcessData;
216 m_loginProcessData = null;
217 TextBoxDialog dialog = new(
219 "",
220 256,
221 delegate(string s) {
222 if (s != null) {
223 try {
225 "https://api.dropboxapi.com/oauth2/token",
226 new Dictionary<string, string> {
227 { "code", s.Trim() },
228 { "client_id", m_appKey },
229 { "client_secret", m_appSecret },
230 { "grant_type", "authorization_code" }
231 },
232 null,
233 new MemoryStream(),
234 loginProcessData.Progress,
235 delegate(byte[] result) {
237 .RootElement.GetProperty("access_token")
238 .GetString();
239 loginProcessData.Succeed(this);
240 },
241 delegate(Exception error) { loginProcessData.Fail(this, error); }
242 );
243 }
244 catch (Exception error2) {
245 loginProcessData.Fail(this, error2);
246 }
247 }
248 else {
249 loginProcessData.Fail(this, null);
250 }
251 }
252 );
253 DialogsManager.ShowDialog(null, dialog);
254 }
255 }
256
257 public void HandleUri(Uri uri) {
258 if (m_loginProcessData == null) {
259 m_loginProcessData = new LoginProcessData { IsTokenFlow = true };
260 }
261 LoginProcessData loginProcessData = m_loginProcessData;
262 m_loginProcessData = null;
263 if (loginProcessData.IsTokenFlow) {
264 try {
265 if (!(uri != null)
266 || string.IsNullOrEmpty(uri.Fragment)) {
267 goto 标签;
268 }
269 Dictionary<string, string> dictionary = WebManager.UrlParametersFromString(uri.Fragment.TrimStart('#'));
270 if (!dictionary.ContainsKey("access_token")) {
271 if (dictionary.TryGetValue("error", out string value)) {
272 throw new Exception(value);
273 }
274 goto 标签;
275 }
276 SettingsManager.DropboxAccessToken = dictionary["access_token"];
277 loginProcessData.Succeed(this);
278 goto end_IL_0038;
279 标签:
280 throw new Exception(LanguageControl.Get(fName, "6"));
281 end_IL_0038: ;
282 }
283 catch (Exception error) {
284 loginProcessData.Fail(this, error);
285 }
286 }
287 }
288
289 public void VerifyLoggedIn() {
290 if (!IsLoggedIn) {
291 throw new InvalidOperationException(LanguageControl.Get(fName, "7"));
292 }
293 }
294
295 public static ExternalContentEntry JsonElementToEntry(JsonElement jsonElement) {
296 ExternalContentEntry externalContentEntry = new();
297 if (jsonElement.TryGetProperty("entries", out JsonElement entries)) {
298 foreach (JsonProperty item in entries.EnumerateObject()) {
299 ExternalContentEntry externalContentEntry2 = new() { Path = item.Value.GetProperty("path_display").GetString() };
300 externalContentEntry2.Type = item.Value.GetProperty(".tag").GetString() == "folder"
303 if (externalContentEntry2.Type != ExternalContentType.Directory) {
304 externalContentEntry2.Time = item.Value.TryGetProperty("server_modified", out JsonElement server_modified)
305 ? DateTime.Parse(server_modified.GetString(), CultureInfo.InvariantCulture)
306 : new DateTime(2000, 1, 1);
307 externalContentEntry2.Size = item.Value.TryGetProperty("size", out JsonElement size) ? size.GetInt64() : 0;
308 }
309 externalContentEntry.ChildEntries.Add(externalContentEntry2);
310 }
311 }
312 return externalContentEntry;
313 }
314
315 public static string JsonElementToLinkAddress(JsonElement jsonElement) {
316 if (jsonElement.TryGetProperty("url", out JsonElement url)) {
317 return $"{url.GetString().Replace("www.dropbox.", "dl.dropbox.").Replace("?dl=0", "")}?dl=1";
318 }
319 throw new InvalidOperationException(LanguageControl.Get(fName, "8"));
320 }
321
322 public static string NormalizePath(string path) {
323 if (path == "/") {
324 return string.Empty;
325 }
326 if (path.Length > 0
327 && path[0] != '/') {
328 return $"/{path}";
329 }
330 return path;
331 }
332 }
333}
Android.Net.Uri Uri
static string GetExtension(string path)
static Action Activated
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
void Fail(DropboxExternalContentProvider provider, Exception error)
void Upload(string path, Stream stream, CancellableProgress progress, Action< string > success, Action< Exception > failure)
static string JsonElementToLinkAddress(JsonElement jsonElement)
void Link(string path, CancellableProgress progress, Action< string > success, Action< Exception > failure)
void Login(CancellableProgress progress, Action success, Action< Exception > failure)
static ExternalContentEntry JsonElementToEntry(JsonElement jsonElement)
void List(string path, CancellableProgress progress, Action< ExternalContentEntry > success, Action< Exception > failure)
void Download(string path, CancellableProgress progress, Action< Stream > success, Action< Exception > failure)
string Path
List< ExternalContentEntry > ChildEntries
ExternalContentType Type
static ExternalContentType ExtensionToType(string extension)
static readonly JsonDocumentOptions DefaultJsonOptions
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static Action< Uri > HandleUri
static void LaunchBrowser(string url)
static void Post(string address, Dictionary< string, string > parameters, Dictionary< string, string > headers, Stream data, CancellableProgress progress, Action< byte[]> success, Action< Exception > failure)
static bool IsInternetConnectionAvailable()
static void Get(string address, Dictionary< string, string > parameters, Dictionary< string, string > headers, CancellableProgress progress, Action< byte[]> success, Action< Exception > failure)
static Dictionary< string, string > UrlParametersFromString(string s)