Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
PlayScreen.cs
浏览该文件的文档.
1using System.Globalization;
2using System.Text;
3using System.Xml.Linq;
4using Engine;
6
7namespace Game {
8 public class PlayScreen : Screen {
13
14 public static int MaxWorlds = 300;
15 public double m_modTipsTime;
16 public long m_totalWorldsSize;
17 public CultureInfo m_cultureInfo;
18 public static string fName = "PlayScreen";
19
20 public virtual void OnWorldsListWidgetItemClicked(object item) {
21 if (item != null
22 && m_worldsListWidget.SelectedItem == item) {
23 Play(item);
24 }
25 }
26
27 public Widget WorldInfoWidget(object item) {
28 WorldInfo worldInfo = (WorldInfo)item;
29 XElement node2 = ContentManager.Get<XElement>("Widgets/SavedWorldItem");
30 ContainerWidget containerWidget = (ContainerWidget)LoadWidget(this, node2, null);
31 LabelWidget labelWidget = containerWidget.Children.Find<LabelWidget>("WorldItem.Name");
32 LabelWidget labelWidget2 = containerWidget.Children.Find<LabelWidget>("WorldItem.Details");
33 containerWidget.Tag = worldInfo;
34 labelWidget.Text = worldInfo.WorldSettings.Name;
35 labelWidget2.Text = string.Format(
36 "{0} | {1} | {2} | {3} | {4}",
37 DataSizeFormatter.Format(worldInfo.Size),
38 worldInfo.LastSaveTime.ToLocalTime().ToString(m_cultureInfo),
39 worldInfo.PlayerInfos.Count > 1
40 ? string.Format(LanguageControl.GetContentWidgets(fName, 9), worldInfo.PlayerInfos.Count)
41 : string.Format(LanguageControl.GetContentWidgets(fName, 10), 1),
42 LanguageControl.Get("GameMode", worldInfo.WorldSettings.GameMode.ToString()),
43 LanguageControl.Get("EnvironmentBehaviorMode", worldInfo.WorldSettings.EnvironmentBehaviorMode.ToString())
44 );
46 labelWidget2.Text = $"{labelWidget2.Text} | {(string.IsNullOrEmpty(worldInfo.SerializationVersion)
47 ? LanguageControl.GetContentWidgets("Usual", "Unknown")
48 : $"({worldInfo.SerializationVersion})")}";
49 }
51 "LoadWorldInfoWidget",
52 loader => {
53 loader.LoadWorldInfoWidget(worldInfo, node2, ref containerWidget);
54 return false;
55 }
56 );
57 return containerWidget;
58 }
59
60 public PlayScreen() {
61 XElement node = ContentManager.Get<XElement>("Screens/PlayScreen");
62 LoadContents(this, node);
67 ListPanelWidget worldsListWidget = m_worldsListWidget;
68 worldsListWidget.ItemWidgetFactory = (Func<object, Widget>)Delegate.Combine(worldsListWidget.ItemWidgetFactory, WorldInfoWidget);
69 m_worldsListWidget.ScrollPosition = 0f;
70 m_worldsListWidget.ScrollSpeed = 0f;
71 m_worldsListWidget.ItemClicked += OnWorldsListWidgetItemClicked;
72 m_modTipsTime = -10000000f;
73#if BROWSER
74 m_cultureInfo = CultureInfo.CurrentCulture;
75#else
77 ? CultureInfo.CurrentCulture
78 : new CultureInfo(Program.SystemLanguage);
79#endif
80 }
81
82 public override void Enter(object[] parameters) {
83 BusyDialog dialog = new(LanguageControl.GetContentWidgets(fName, 5), null);
84 DialogsManager.ShowDialog(null, dialog);
85 Task.Run(
86 delegate {
89 List<WorldInfo> worldInfos = new(WorldsManager.WorldInfos);
90 worldInfos.Sort((w1, w2) => DateTime.Compare(w2.LastSaveTime, w1.LastSaveTime));
92 delegate {
94 foreach (WorldInfo item in worldInfos) {
96 }
97 m_totalWorldsSize = worldInfos.Sum(wi => wi.Size);
98 if (selectedItem != null) {
99 m_worldsListWidget.SelectedItem = worldInfos.FirstOrDefault(wi => wi.DirectoryName == selectedItem.DirectoryName);
100 }
102 }
103 );
104 }
105 );
107
108 public override void Update() {
109 Vector2 size = new(310, 60);
110 if (SettingsManager.UIScale > 1f) {
111 size = new Vector2(250, 60);
112 }
113 m_playButton.Size = size;
114 m_newWorldButton.Size = size;
116 && WorldsManager.WorldInfos.IndexOf((WorldInfo)m_worldsListWidget.SelectedItem) < 0) {
117 m_worldsListWidget.SelectedItem = null;
118 }
119 if (m_worldsListWidget.Items.Count > 0) {
120 Children.Find<LabelWidget>("TopBar.Label").Text = string.Format(
121 LanguageControl.Get(fName, m_worldsListWidget.Items.Count > 1 ? "10" : "9"),
122 m_worldsListWidget.Items.Count,
123 DataSizeFormatter.Format(m_totalWorldsSize, 2)
124 );
125 }
126 else {
127 Children.Find<LabelWidget>("TopBar.Label").Text = LanguageControl.Get(fName, "11");
128 }
129 m_playButton.IsEnabled = m_worldsListWidget.SelectedItem != null;
130 m_propertiesButton.IsEnabled = m_worldsListWidget.SelectedItem != null;
131 if (m_playButton.IsClicked
132 && m_worldsListWidget.SelectedItem != null) {
133 Play(m_worldsListWidget.SelectedItem);
134 }
135 if (m_newWorldButton.IsClicked) {
136 if (WorldsManager.WorldInfos.Count >= MaxWorlds) {
137 DialogsManager.ShowDialog(
138 null,
139 new MessageDialog(
140 LanguageControl.GetContentWidgets(fName, 7),
141 string.Format(LanguageControl.GetContentWidgets(fName, 8), MaxWorlds),
142 LanguageControl.GetContentWidgets("Usual", "ok"),
143 null,
144 null
145 )
146 );
147 }
148 else {
149 ScreensManager.SwitchScreen("NewWorld");
150 m_worldsListWidget.SelectedItem = null;
151 }
152 }
153 if (m_propertiesButton.IsClicked
154 && m_worldsListWidget.SelectedItem != null) {
155 WorldInfo worldInfo = (WorldInfo)m_worldsListWidget.SelectedItem;
156 ScreensManager.SwitchScreen("ModifyWorld", worldInfo.DirectoryName, worldInfo.WorldSettings);
157 }
158 if (Input.Back
159 || Input.Cancel
160 || Children.Find<ButtonWidget>("TopBar.Back").IsClicked) {
161 ScreensManager.SwitchScreen("MainMenu");
162 m_worldsListWidget.SelectedItem = null;
163 }
164 }
165
167 /// </summary>
168 /// <param name="item">实际类型为WorldInfo</param>
169 public void Play(object item) {
170 bool flag = false;
171 WorldInfo worldInfo = item as WorldInfo;
172 string languageType = !ModsManager.Configs.TryGetValue("Language", out string config) ? "zh-CN" : config;
173 if (languageType == "zh-CN"
174 && Time.RealTime - m_modTipsTime > 3600f) {
176 flag |= ShowTips(item);
177 }
178 List<ValuesDictionary> modsNotLoaded = [];
179 List<ValuesDictionary> modsVersionNotCapable = [];
180 if (worldInfo != null) {
181 XElement projectNode = WorldsManager.GetProjectNode(worldInfo);
182 if (projectNode != null) {
183 XElement subsystemUsedModsNode = WorldsManager.GetSubsystemNode(projectNode, "UsedMods", false);
184 if (subsystemUsedModsNode != null) {
185 ValuesDictionary subsystemValuesDictionary = new();
186 subsystemValuesDictionary.ApplyOverrides(subsystemUsedModsNode);
187 int modsCount = subsystemValuesDictionary.GetValue("ModsCount", 0);
188 ValuesDictionary valuesDictionary = subsystemValuesDictionary.GetValue<ValuesDictionary>("Mods", null);
189 if (valuesDictionary != null) {
190 for (int i = 0; i < modsCount; i++) {
191 ValuesDictionary modDictionary = valuesDictionary.GetValue<ValuesDictionary>(i.ToString(), null);
192 if (modDictionary == null) {
193 continue;
194 }
195 bool entityGotten = ModsManager.GetModEntity(
196 modDictionary.GetValue("PackageName", string.Empty),
197 out ModEntity modEntity
198 );
199 if (!entityGotten) {
200 modsNotLoaded.Add(modDictionary);
201 continue;
202 }
203 bool versionComparePass = modEntity?.Loader?.CompareModVersion(
204 modEntity.modInfo.Version,
205 modDictionary.GetValue("Version", "?")
206 )
207 ?? true;
208 modDictionary.SetValue("CurrentVersion", modEntity.modInfo.Version);
209 if (!versionComparePass) {
210 modsVersionNotCapable.Add(modDictionary);
211 }
212 }
213 }
214 }
215 }
216 }
217 if (!flag) {
218 if (modsNotLoaded.Count > 0
219 || modsVersionNotCapable.Count > 0) {
220 StringBuilder text = new();
221 if (modsNotLoaded.Count > 0) {
222 text.AppendLine(LanguageControl.Get(fName, 3));
223 }
224 foreach (ValuesDictionary modDictionary in modsNotLoaded) {
225 text.AppendLine(
226 string.Format(
228 modDictionary.GetValue("Name", "?"),
229 modDictionary.GetValue("Version", "?")
230 )
231 );
232 }
233 if (modsVersionNotCapable.Count > 0) {
234 text.AppendLine(LanguageControl.Get(fName, 5));
235 }
236 foreach (ValuesDictionary modDictionary in modsVersionNotCapable) {
237 text.AppendLine(
238 string.Format(
240 modDictionary.GetValue("Name", "?"),
241 modDictionary.GetValue("Version", "?"),
242 modDictionary.GetValue("CurrentVersion", "?")
243 )
244 );
245 }
246 text.AppendLine(LanguageControl.Get(fName, 7));
248 this,
249 new MessageDialog(
251 text.ToString(),
254 delegate(MessageDialogButton button) {
255 if (button == MessageDialogButton.Button1) {
256 GameLoad(item);
257 }
258 }
259 )
260 );
261 }
262 else {
263 GameLoad(item);
264 }
265 }
267
268 public void GameLoad(object item) {
270 "BeforeGameLoading",
271 loader => {
272 item = loader.BeforeGameLoading(this, item);
273 return false;
274 }
275 );
276 if (item != null) {
277 ScreensManager.SwitchScreen("GameLoading", item, null);
278 }
279 m_worldsListWidget.SelectedItem = null;
281
282 public bool ShowTips(object item) {
283 string tips = string.Empty;
284 int num = 1;
285 try {
286 foreach (ModEntity modEntity in ModsManager.ModList) {
287 foreach (MotdManager.FilterMod value in MotdManager.FilterModAll) {
288 if (value.FilterAPIVersion == ModsManager.APIVersionString
289 && value.PackageName == modEntity.modInfo.PackageName
290 && CompareVersion(value.Version, modEntity.modInfo.Version)) {
291 tips += $"{num}.{modEntity.modInfo.Name}(v{modEntity.modInfo.Version}) {value.Explanation}\n";
292 num++;
293 }
294 }
295 }
296 }
297 catch {
298 return false;
299 }
300 if (!string.IsNullOrEmpty(tips)) {
301 DialogsManager.ShowDialog(
302 null,
303 new MessageDialog(
304 LanguageControl.Get(fName, "1"),
305 tips,
306 LanguageControl.Get(fName, "2"),
307 LanguageControl.Back,
308 delegate(MessageDialogButton button) {
309 if (button == MessageDialogButton.Button1) {
310 GameLoad(item);
311 }
312 }
313 )
314 );
315 return true;
316 }
317 return false;
319
320 public bool CompareVersion(string v1, string v2) {
321 if (v1 == "all") {
322 return true;
323 }
324 if (v1.Contains("~")) {
325 string[] versions = v1.Split(['~'], StringSplitOptions.RemoveEmptyEntries);
326 try {
327 double minv = double.Parse(versions[0]);
328 double maxv = double.Parse(versions[1]);
329 double v = double.Parse(v2);
330 return v >= minv && v <= maxv;
331 }
332 catch {
333 return false;
334 }
335 }
336 if (v1.Contains(";")) {
337 string[] versions = v1.Split([';'], StringSplitOptions.RemoveEmptyEntries);
338 foreach (string v in versions) {
339 if (v == v2) {
340 return true;
341 }
342 }
343 return false;
344 }
345 return v1 == v2;
346 }
347 }
348}
static void Dispatch(Action action, bool waitUntilCompleted=false)
static double RealTime
定义 Time.cs:38
readonly WidgetsList Children
static object Get(Type type, string name)
static string Format(long bytes, int significantDigits=3)
static void HideDialog(Dialog dialog)
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
static string GetContentWidgets(string name, string prop)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
string PackageName
static List< FilterMod > FilterModAll
void Play(object item)
bool ShowTips(object item)
override void Update()
CultureInfo m_cultureInfo
bool CompareVersion(string v1, string v2)
ButtonWidget m_propertiesButton
Widget WorldInfoWidget(object item)
ButtonWidget m_playButton
void GameLoad(object item)
ListPanelWidget m_worldsListWidget
ButtonWidget m_newWorldButton
static string fName
override void Enter(object[] parameters)
virtual void OnWorldsListWidgetItemClicked(object item)
static string SystemLanguage
static Widget LoadWidget(object eventsTarget, XElement node, ContainerWidget parentWidget)
WidgetInput Input
virtual void LoadContents(object eventsTarget, XElement node)
Widget Find(string name, Type type, bool throwIfNotFound=true)
string SerializationVersion
WorldSettings WorldSettings
DateTime LastSaveTime
List< PlayerInfo > PlayerInfos
EnvironmentBehaviorMode EnvironmentBehaviorMode
static void UpdateWorldsList()
static ReadOnlyList< WorldInfo > WorldInfos
static XElement GetProjectNode(WorldInfo worldInfo)
static XElement GetSubsystemNode(XElement projectNode, string subsystemName)
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook
static bool GetModEntity(string packagename, out ModEntity modEntity)
static List< ModEntity > ModList
所有已启用的模组
static string APIVersionString
static Dictionary< string, string > Configs
void ApplyOverrides(ValuesDictionary overridesValuesDictionary)