Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ExternalContentScreen.cs
浏览该文件的文档.
1using System.Globalization;
2using System.Xml.Linq;
3using Engine;
4
5namespace Game {
15
16 public string m_path;
17 public bool m_listDirty;
18 public Dictionary<string, bool> m_downloadedFiles = [];
20 public const string fName = "ExternalContentScreen";
21
23 XElement node = ContentManager.Get<XElement>("Screens/ExternalContentScreen");
24 LoadContents(this, node);
25 m_directoryLabel = Children.Find<LabelWidget>("TopBar.Label");
26 m_directoryList = Children.Find<ListPanelWidget>("DirectoryList");
27 m_providerNameLabel = Children.Find<LabelWidget>("ProviderName");
28 m_changeProviderButton = Children.Find<ButtonWidget>("ChangeProvider");
29 m_loginLogoutButton = Children.Find<ButtonWidget>("LoginLogout");
30 m_upDirectoryButton = Children.Find<ButtonWidget>("UpDirectory");
31 m_actionButton = Children.Find<ButtonWidget>("Action");
32 m_copyLinkButton = Children.Find<ButtonWidget>("CopyLink");
33 m_directoryList.ItemWidgetFactory = delegate(object item) {
34 ExternalContentEntry externalContentEntry2 = (ExternalContentEntry)item;
35 XElement node2 = ContentManager.Get<XElement>("Widgets/ExternalContentItem");
36 ContainerWidget containerWidget = (ContainerWidget)LoadWidget(this, node2, null);
37 string fileName = Storage.GetFileName(externalContentEntry2.Path);
38 string text = m_downloadedFiles.ContainsKey(externalContentEntry2.Path) ? LanguageControl.Get(fName, 11) : string.Empty;
39 string text2 = externalContentEntry2.Type != ExternalContentType.Directory
40 ? $"{ExternalContentManager.GetEntryTypeDescription(externalContentEntry2.Type)} | {DataSizeFormatter.Format(externalContentEntry2.Size)} | {externalContentEntry2.Time:dd-MMM-yyyy HH:mm}{text}"
42 containerWidget.Children.Find<RectangleWidget>("ExternalContentItem.Icon").Subtexture =
43 ExternalContentManager.GetEntryTypeIcon(externalContentEntry2.Type);
44 containerWidget.Children.Find<LabelWidget>("ExternalContentItem.Text").Text = fileName;
45 containerWidget.Children.Find<LabelWidget>("ExternalContentItem.Details").Text = text2;
46 return containerWidget;
47 };
48 m_directoryList.ItemClicked += delegate(object item) {
49 if (m_directoryList.SelectedItem == item) {
50 if (item is ExternalContentEntry externalContentEntry
51 && externalContentEntry.Type == ExternalContentType.Directory) {
52 SetPath(externalContentEntry.Path);
53 }
54 }
55 };
56 }
57
58 public override void Enter(object[] parameters) {
59 m_directoryList.ClearItems();
60 SetPath(null);
61 m_listDirty = true;
62 }
63
64 public override void Update() {
65 if (m_listDirty) {
66 m_listDirty = false;
67 UpdateList();
68 }
69 ExternalContentEntry externalContentEntry = null;
70 if (m_directoryList.SelectedIndex.HasValue) {
71 externalContentEntry = m_directoryList.Items[m_directoryList.SelectedIndex.Value] as ExternalContentEntry;
72 }
73 if (externalContentEntry != null) {
74 m_actionButton.IsVisible = true;
75 if (externalContentEntry.Type == ExternalContentType.Directory) {
76 m_actionButton.Text = LanguageControl.Get(fName, 1);
77 m_actionButton.IsEnabled = true;
78 m_copyLinkButton.IsEnabled = false;
79 }
80 else {
81 m_actionButton.Text = LanguageControl.Get(fName, 2);
83 ExternalContentManager.ExtensionToType(Storage.GetExtension(externalContentEntry.Path).ToLower())
84 )) {
85 m_actionButton.IsEnabled = true;
86 m_copyLinkButton.IsEnabled = true;
87 }
88 else {
89 m_actionButton.IsEnabled = false;
90 m_copyLinkButton.IsEnabled = false;
91 }
92 }
93 }
94 else {
95 m_actionButton.IsVisible = false;
96 m_copyLinkButton.IsVisible = false;
97 }
98 m_directoryLabel.Text = m_externalContentProvider.IsLoggedIn
99 ? string.Format(LanguageControl.Get(fName, 3), m_path)
101 m_providerNameLabel.Text = m_externalContentProvider.DisplayName;
102 #if WINDOWS
103 m_upDirectoryButton.IsEnabled = m_externalContentProvider.IsLoggedIn && !(m_path.Length == 2 && m_path[1] == ':');
104 #elif ANDROID
105 m_upDirectoryButton.IsEnabled = m_externalContentProvider.IsLoggedIn && m_path != "/storage/emulated/0";
106 #else
107 m_upDirectoryButton.IsEnabled = m_externalContentProvider.IsLoggedIn && m_path != "/";
108 #endif
109 m_loginLogoutButton.Text = m_externalContentProvider.IsLoggedIn ? LanguageControl.Get(fName, 5) : LanguageControl.Get(fName, 6);
110 m_loginLogoutButton.IsVisible = m_externalContentProvider.RequiresLogin;
111 m_copyLinkButton.IsVisible = m_externalContentProvider.SupportsLinks;
112 m_copyLinkButton.IsEnabled = externalContentEntry != null
114 if (m_changeProviderButton.IsClicked) {
116 null,
119 true,
120 delegate(IExternalContentProvider provider) {
121 m_externalContentProvider = provider;
122 m_listDirty = true;
123 SetPath(null);
124 }
125 )
126 );
127 }
128 if (m_upDirectoryButton.IsClicked) {
129 string directoryName = Storage.GetDirectoryName(m_path);
130 SetPath(directoryName);
131 }
132 if (m_actionButton.IsClicked
133 && externalContentEntry != null) {
134 if (externalContentEntry.Type == ExternalContentType.Directory) {
135 SetPath(externalContentEntry.Path);
136 }
137 else {
138 DownloadEntry(externalContentEntry);
139 }
140 }
141 if (m_copyLinkButton.IsClicked
142 && externalContentEntry != null
144 CancellableBusyDialog busyDialog = new(LanguageControl.Get(fName, 8), false);
145 DialogsManager.ShowDialog(null, busyDialog);
147 externalContentEntry.Path,
148 busyDialog.Progress,
149 delegate(string link) {
150 DialogsManager.HideDialog(busyDialog);
152 },
153 delegate(Exception error) {
154 DialogsManager.HideDialog(busyDialog);
155 DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Error, error.Message, LanguageControl.Ok, null, null));
156 }
157 );
158 }
159 if (m_loginLogoutButton.IsClicked) {
160 if (m_externalContentProvider.IsLoggedIn) {
162 SetPath(null);
163 m_listDirty = true;
164 }
165 else {
168 false,
169 delegate {
170 SetPath(null);
171 m_listDirty = true;
172 }
173 );
174 }
175 }
176 /*if (!string.IsNullOrEmpty(ExternalContentManager.openFilePath)) {
177 try {
178 ExternalContentEntry externalContentEntry1 = new() {
179 Type = ExternalContentManager.ExtensionToType(Storage.GetExtension(ExternalContentManager.openFilePath)),
180 Path = ExternalContentManager.openFilePath,
181 Size = new FileInfo(ExternalContentManager.openFilePath).Length,
182 Time = new FileInfo(ExternalContentManager.openFilePath).CreationTime
183 };
184 if (ExternalContentManager.IsEntryTypeDownloadSupported(externalContentEntry1.Type)) {
185 DownloadEntry(externalContentEntry1);
186 }
187 else {
188 string message = LanguageControl.Get(fName, 14) + ExternalContentManager.openFilePath;
189 DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Get(fName, 13), message, LanguageControl.Ok, null, null));
190 Log.Error(message);
191 }
192 }
193 catch (Exception e) {
194 DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Get(fName, 13), e.ToString(), LanguageControl.Ok, null, null));
195 Log.Error($"{LanguageControl.Get(fName, 13)} {ExternalContentManager.openFilePath}\n{e}");
196 }
197 ExternalContentManager.openFilePath = string.Empty;
198 }*/
199 if (Input.Back
200 || Input.Cancel
201 || Children.Find<ButtonWidget>("TopBar.Back").IsClicked) {
202 ScreensManager.SwitchScreen("Content");
203 }
204 }
205
206 public void SetPath(string path) {
207 if (string.IsNullOrEmpty(path)) {
208#if ANDROID
209 path = Storage.GetSystemPath($"{RunPath.AndroidFilePath}/files");
210#else
212#endif
213 }
214 path = path.Replace('\\', '/');
215 if (path.Length > 1 && path.EndsWith("/")) {
216 path = path.Substring(0, path.Length - 1);
217 }
218 if (path != m_path) {
219 m_path = path;
220 m_listDirty = true;
221 }
222 }
223
224 public virtual void UpdateList() {
225 m_directoryList.ClearItems();
226 if (m_externalContentProvider != null
227 && m_externalContentProvider.IsLoggedIn) {
228 CancellableBusyDialog busyDialog = new(LanguageControl.Get(fName, 9), false);
229 DialogsManager.ShowDialog(null, busyDialog);
231 m_path,
232 busyDialog.Progress,
233 delegate(ExternalContentEntry entry) {
234 DialogsManager.HideDialog(busyDialog);
235 List<ExternalContentEntry> list = new(entry.ChildEntries.Where(EntryFilter).Take(1000));
236 m_directoryList.ClearItems();
237 list.Sort((e1, e2) => e1.Type == ExternalContentType.Directory && e2.Type != ExternalContentType.Directory ? -1 :
238 e1.Type != ExternalContentType.Directory && e2.Type == ExternalContentType.Directory ? 1 :
239 string.Compare(e1.Path, e2.Path, CultureInfo.InvariantCulture, CompareOptions.None)
240 );
241 foreach (ExternalContentEntry item in list) {
242 m_directoryList.AddItem(item);
243 }
244 },
245 delegate(Exception error) {
246 DialogsManager.HideDialog(busyDialog);
247 DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Error, error.Message, LanguageControl.Ok, null, null));
248 }
249 );
250 }
251 }
252
254 CancellableBusyDialog busyDialog = new(LanguageControl.Get(fName, 10), false);
255 DialogsManager.ShowDialog(null, busyDialog);
257 entry.Path,
258 busyDialog.Progress,
259 delegate(Stream stream) {
260 busyDialog.LargeMessage = LanguageControl.Get(fName, 12);
262 stream,
263 entry.Type,
264 Storage.GetFileName(entry.Path),
265 delegate {
266 stream.Dispose();
267 DialogsManager.HideDialog(busyDialog);
268 if (entry.Type != ExternalContentType.Mod) {
269 DialogsManager.ShowDialog(
270 null,
271 new MessageDialog(
272 LanguageControl.Success,
273 string.Format(LanguageControl.Get("ContentScreen", "4"), Storage.GetFileName(entry.Path)),
274 LanguageControl.Ok,
275 null,
276 null
277 )
278 );
279 }
280 },
281 delegate(Exception error) {
282 stream.Dispose();
283 DialogsManager.HideDialog(busyDialog);
284 DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Error, error.Message, LanguageControl.Ok, null, null));
285 }
286 );
287 },
288 delegate(Exception error) {
289 DialogsManager.HideDialog(busyDialog);
290 DialogsManager.ShowDialog(null, new MessageDialog(LanguageControl.Error, error.Message, LanguageControl.Ok, null, null));
291 }
292 );
293 }
294
295 public static bool EntryFilter(ExternalContentEntry entry) => entry.Type != ExternalContentType.Unknown;
296 }
297}
static string GetSystemPath(string path)
static string GetExtension(string path)
static string GetDirectoryName(string path)
static string GetFileName(string path)
readonly WidgetsList Children
static object Get(Type type, string name)
static void HideDialog(Dialog dialog)
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
string Path
List< ExternalContentEntry > ChildEntries
ExternalContentType Type
static void ImportExternalContent(Stream stream, ExternalContentType type, string name, Action< string > success, Action< Exception > failure)
static bool IsEntryTypeDownloadSupported(ExternalContentType type)
static ExternalContentType ExtensionToType(string extension)
static void ShowLoginUiIfNeeded(IExternalContentProvider provider, bool showWarningDialog, Action handler)
static Subtexture GetEntryTypeIcon(ExternalContentType type)
static string GetEntryTypeDescription(ExternalContentType type)
static IExternalContentProvider DefaultProvider
static bool EntryFilter(ExternalContentEntry entry)
Dictionary< string, bool > m_downloadedFiles
void DownloadEntry(ExternalContentEntry entry)
IExternalContentProvider m_externalContentProvider
override void Enter(object[] parameters)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static void SwitchScreen(string name, params object[] parameters)
static Widget LoadWidget(object eventsTarget, XElement node, ContainerWidget parentWidget)
virtual void LoadContents(object eventsTarget, XElement node)