XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

SpaceEngineersModDev/SpaceEngineersBlueprintEditorInWinUI

优化Wait等待机制

a787876
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

4 个文件 +87 -58
Added SpaceEngineersBlueprintEditor/Utilities/Helpers/FunctionHelper.cs +10 -0
@@ -0,0 +1,10 @@
1 namespace SpaceEngineersBlueprintEditor.Utilities.Helpers;
2
3 public static class Helper
4 {
5 public static async Task Wait(Func<bool> func, int delay = 100)
6 {
7 while (!func())
8 await Task.Delay(delay);
9 }
10 }
Modified SpaceEngineersBlueprintEditor/Utilities/Helpers/SpaceEngineersHelper.cs +2 -9
@@ -1,15 +1,8 @@
1 using Microsoft.UI.Dispatching;
2 using Microsoft.UI.Xaml.Media.Imaging;
3 using Microsoft.Win32;
1 using Microsoft.Win32;
4 2 using Sandbox.Definitions;
5 using SpaceEngineersBlueprintEditor.Model;
6 3 using SpaceEngineersBlueprintEditor.SpaceEngineersCore;
7 using System.Collections.Concurrent;
8 using System.Diagnostics;
9 4 using VRage.Collections;
10 5 using VRage.Game;
11 using VRage.Game.Definitions;
12 using Windows.Storage.Streams;
13 6
14 7 namespace SpaceEngineersBlueprintEditor.Utilities.Helpers;
15 8
@@ -24,7 +17,7 @@ public static class SpaceEngineersHelper
24 17 public static IEnumerable<MyScenarioDefinition> ScenarioDefinition => AllDefinitions.Where(definition => definition is MyScenarioDefinition).Cast<MyScenarioDefinition>();
25 18 public static async Task LoadDefinitionViewDataListAsync()
26 19 {
27 while (!Initializer.IsDefinitionsLoadComplete) { await Task.Delay(100); }
20 await Helper.Wait(() => Initializer.IsDefinitionsLoadComplete);
28 21 foreach (var definition in AllDefinitions)
29 22 if (definition is not null && definition.Icons is not null && definition.Icons.Length > 0)
30 23 {
Modified SpaceEngineersBlueprintEditor/ViewModels/BlueprintDetailPageViewModel.cs +74 -48
@@ -67,30 +67,42 @@ public partial class BlueprintDetailPageViewModel : ViewModelBase
67 67 IsProgressRingVisible = false;
68 68 return;
69 69 }
70 while (!SpaceEngineersHelper.IsLoadComplete) await Task.Delay(100);
70 await Helper.Wait(() => SpaceEngineersHelper.IsLoadComplete);
71 await LoadBlueprintAsync();
72 if (currentBlueprint is not null)
73 {
74 AuthorName = $"蓝图作者:{currentBlueprint.DisplayName}(Steam ID: {currentBlueprint.OwnerSteamId})";
75 BlueprintFileSize = $"蓝图大小:{currentBlueprintInfoViewData.FileSize}";
76 BlueprintPath = $"蓝图路径:{currentBlueprintInfoViewData.FilePath}";
77 IsBlueprintDestructible = $"是否可被破坏:{(currentBlueprint.CubeGrids.Any(grid => !grid.DestructibleBlocks) ? currentBlueprint.CubeGrids.Any(grid => grid.DestructibleBlocks) ? "部分可被破坏" : "整体不可破坏" : "可被破坏")}";
78 IsBlueprintEditable = $"是否可在游戏内编辑:{(currentBlueprint.CubeGrids.Any(grid => !grid.Editable) ? currentBlueprint.CubeGrids.Any(grid => grid.Editable) ? "部分可编辑" : "整体不可编辑" : "可编辑")}";
79 CalculateDLC();
80 CalculateCubeGrids();
81 CalculateComponent();
82 IsProgressRingVisible = false;
83 messageService?.ShowMessage("蓝图加载完成", "完成", InfoBarSeverity.Success);
84 }
85 else
86 {
87 IsProgressRingVisible = false;
88 messageService?.ShowMessage("未能加载蓝图", "错误", InfoBarSeverity.Error);
89 }
90 }
91
92 private async Task LoadBlueprintAsync()
93 {
71 94 await Task.Run(() =>
72 95 {
73 96 currentDefinitions = SpaceEngineerDefinitions.Load<MyObjectBuilder_Definitions>(e.FilePath);
74 97 if (currentDefinitions is not null && currentDefinitions.ShipBlueprints is not null && currentDefinitions.ShipBlueprints.Length > 0)
75 98 currentBlueprint = currentDefinitions.ShipBlueprints[0];
76 99 });
100 }
101
102 private void CalculateCubeGrids()
103 {
77 104 if (currentBlueprint is not null)
78 105 {
79 AuthorName = $"蓝图作者:{currentBlueprint.DisplayName}(Steam ID: {currentBlueprint.OwnerSteamId})";
80 BlueprintFileSize = $"蓝图大小:{currentBlueprintInfoViewData.FileSize}";
81 BlueprintPath = $"蓝图路径:{currentBlueprintInfoViewData.FilePath}";
82 IsBlueprintDestructible = $"是否可被破坏:{(currentBlueprint.CubeGrids.Any(grid => !grid.DestructibleBlocks) ? currentBlueprint.CubeGrids.Any(grid => grid.DestructibleBlocks) ? "部分可被破坏" : "整体不可破坏" : "可被破坏")}";
83 IsBlueprintEditable = $"是否可在游戏内编辑:{(currentBlueprint.CubeGrids.Any(grid => !grid.Editable) ? currentBlueprint.CubeGrids.Any(grid => grid.Editable) ? "部分可编辑" : "整体不可编辑" : "可编辑")}";
84 if (currentBlueprint.DLCs is not null)
85 {
86 foreach (var dlc in currentBlueprint.DLCs)
87 if (dlc is not null)
88 DLCList.Add(dlc);
89 }
90 else
91 {
92 DLCList.Add("无DLC");
93 }
94 106 if (currentBlueprint.CubeGrids is not null)
95 107 {
96 108 int totalCubeCount = 0;
@@ -100,34 +112,6 @@ public partial class BlueprintDetailPageViewModel : ViewModelBase
100 112 totalCubeCount += grid.CubeBlocks.Count;
101 113 CubeGridList.Add(new($"网格名称:{grid.DisplayName}", $"方块数量:{grid.CubeBlocks.Count}"));
102 114 }
103 var cubeHashDictionary = new Dictionary<int, int>();
104 var componentDictionary = new Dictionary<MyComponentDefinition, int>();
105 currentBlueprint.CubeGrids.ForEach(grid => grid.CubeBlocks.ForEach(cube =>
106 {
107 if (cubeHashDictionary.TryGetValue(cube.SubtypeId.m_hash, out int value))
108 cubeHashDictionary[cube.SubtypeId.m_hash] = ++value;
109 else
110 cubeHashDictionary.TryAdd(cube.SubtypeId.m_hash, 1);
111 }));
112 cubeHashDictionary.ForEach(cubeHashEntry =>
113 {
114 if (SpaceEngineersHelper.GetDefinition(cubeHashEntry.Key) is MyCubeBlockDefinition definition && definition is not null && definition.Components is not null && definition.Components.Length > 0)
115 {
116 definition.Components.ForEach(component =>
117 {
118 if (componentDictionary.ContainsKey(component.Definition))
119 componentDictionary[component.Definition] += component.Count * cubeHashEntry.Value;
120 else
121 componentDictionary.TryAdd(component.Definition, component.Count * cubeHashEntry.Value);
122 });
123 }
124 });
125 componentDictionary.ForEach(componentEntry => ComponentList.Add(new()
126 {
127 DefinitionBase = componentEntry.Key,
128 AdditionalInfo = $"x{componentEntry.Value}",
129 ImageSource = componentEntry.Key.Icons is not null && componentEntry.Key.Icons.Length > 0 ? new BitmapImage(new(@$"{AppPath.DefinitionImages}\{FileHelper.ChangeExtension(componentEntry.Key.Icons[0], "png")}")) : null
130 }));
131 115 TotalCubeNumber = $"方块总数:{totalCubeCount}";
132 116 TotalGridNumber = $"网格总数:{currentBlueprint.CubeGrids.Length}";
133 117 }
@@ -139,13 +123,55 @@ public partial class BlueprintDetailPageViewModel : ViewModelBase
139 123 {
140 124 CubeGridList.Add(new("未找到网格", "方块数量:无"));
141 125 }
142 IsProgressRingVisible = false;
143 messageService?.ShowMessage("蓝图加载完成", "完成", InfoBarSeverity.Success);
126 }
127 }
128
129 private void CalculateComponent()
130 {
131 if (currentBlueprint is not null)
132 {
133 var cubeHashDictionary = new Dictionary<int, int>();
134 var componentDictionary = new Dictionary<MyComponentDefinition, int>();
135 currentBlueprint.CubeGrids.ForEach(grid => grid.CubeBlocks.ForEach(cube =>
136 {
137 if (cubeHashDictionary.TryGetValue(cube.SubtypeId.m_hash, out int value))
138 cubeHashDictionary[cube.SubtypeId.m_hash] = ++value;
139 else
140 cubeHashDictionary.TryAdd(cube.SubtypeId.m_hash, 1);
141 }));
142 cubeHashDictionary.ForEach(cubeHashEntry =>
143 {
144 if (SpaceEngineersHelper.GetDefinition(cubeHashEntry.Key) is MyCubeBlockDefinition definition && definition is not null && definition.Components is not null && definition.Components.Length > 0)
145 {
146 definition.Components.ForEach(component =>
147 {
148 if (componentDictionary.ContainsKey(component.Definition))
149 componentDictionary[component.Definition] += component.Count * cubeHashEntry.Value;
150 else
151 componentDictionary.TryAdd(component.Definition, component.Count * cubeHashEntry.Value);
152 });
153 }
154 });
155 componentDictionary.ForEach(componentEntry => ComponentList.Add(new()
156 {
157 DefinitionBase = componentEntry.Key,
158 AdditionalInfo = $"x{componentEntry.Value}",
159 ImageSource = componentEntry.Key.Icons is not null && componentEntry.Key.Icons.Length > 0 ? new BitmapImage(new(@$"{AppPath.DefinitionImages}\{FileHelper.ChangeExtension(componentEntry.Key.Icons[0], "png")}")) : null
160 }));
161 }
162 }
163
164 private void CalculateDLC()
165 {
166 if (currentBlueprint is not null && currentBlueprint.DLCs is not null)
167 {
168 foreach (var dlc in currentBlueprint.DLCs)
169 if (dlc is not null)
170 DLCList.Add(dlc);
144 171 }
145 172 else
146 173 {
147 IsProgressRingVisible = false;
148 messageService?.ShowMessage("未能加载蓝图", "错误", InfoBarSeverity.Error);
174 DLCList.Add("无DLC");
149 175 }
150 176 }
151 177
Modified SpaceEngineersBlueprintEditor/ViewModels/GameDefinitionsViewPageViewModel.cs +1 -1
@@ -66,7 +66,7 @@ public partial class GameDefinitionsViewPageViewModel : ViewModelBase
66 66 private async void NavigationParameterService_ParameterChange(object? sender, string e)
67 67 {
68 68 currentParameter = e;
69 while (!Initializer.IsDefinitionsLoadComplete || !DefinitionPropertiesDisplayService.IsPageLoaded) { await Task.Delay(100); }
69 await Helper.Wait(() => Initializer.IsDefinitionsLoadComplete || DefinitionPropertiesDisplayService.IsPageLoaded);
70 70 LoadDefinitions(GetCurrentDefinitions(currentParameter));
71 71 IsUIVisible = true;
72 72 IsProgressRingVisible = false;