Skip to content

Commit 012627a

Browse files
committed
up 组件下载
1 parent 2914621 commit 012627a

File tree

86 files changed

+2124
-834
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+2124
-834
lines changed

src/ColorDesktop.Api/IPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface IPlugin
3232
/// 获取图标
3333
/// </summary>
3434
/// <returns></returns>
35-
Bitmap? GetIcon();
35+
Stream? GetIcon();
3636
/// <summary>
3737
/// 打开实例设置
3838
/// </summary>

src/ColorDesktop.Debug/ColorDesktop.Debug.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<ItemGroup>
1111
<ProjectReference Include="..\ColorDesktop.Launcher\ColorDesktop.Launcher.csproj" />
1212
<ProjectReference Include="..\Plugins\ColorDesktop.AnalogClockPlugin\ColorDesktop.AnalogClockPlugin.csproj" />
13-
<ProjectReference Include="..\Plugins\ColorDesktop.BgmPlugin\ColorDesktop.BmPlugin.csproj" />
13+
<ProjectReference Include="..\Plugins\ColorDesktop.BmPlugin\ColorDesktop.BmPlugin.csproj" />
1414
<ProjectReference Include="..\Plugins\ColorDesktop.CalendarPlugin\ColorDesktop.CalendarPlugin.csproj" />
1515
<ProjectReference Include="..\Plugins\ColorDesktop.ClockPlugin\ColorDesktop.ClockPlugin.csproj" />
1616
<ProjectReference Include="..\Plugins\ColorDesktop.OneWordPlugin\ColorDesktop.OneWordPlugin.csproj" />

src/ColorDesktop.Launcher/App.axaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public override void Initialize()
100100

101101
PluginManager.Init();
102102
InstanceManager.Init();
103+
TempManager.Init();
103104

104105
AvaloniaXamlLoader.Load(this);
105106

src/ColorDesktop.Launcher/ColorDesktop.Launcher.csproj

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,13 @@
3838
</ItemGroup>
3939

4040
<ItemGroup>
41-
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
42-
<PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaVersion)" />
43-
<PackageReference Include="Avalonia.Controls.ColorPicker" Version="$(AvaloniaVersion)" />
44-
<PackageReference Include="Avalonia.Controls.DataGrid" Version="$(AvaloniaVersion)" />
45-
<PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
46-
<PackageReference Include="Avalonia.Markup.Xaml.Loader" Version="$(AvaloniaVersion)" />
47-
<PackageReference Include="Avalonia.Skia" Version="$(AvaloniaVersion)" />
48-
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
49-
<PackageReference Include="Avalonia.Svg.Skia" Version="11.1.0.1" />
50-
<PackageReference Include="DialogHost.Avalonia" Version="0.8.1" />
51-
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.1.0" />
52-
<PackageReference Include="Avalonia.Controls.TreeDataGrid" Version="11.0.10" />
53-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
54-
<PackageReference Include="SkiaSharp" Version="2.88.8" />
5541
<PackageReference Include="DotNetty.Buffers" Version="0.7.6" />
5642
<PackageReference Include="DotNetty.Common" Version="0.7.6" />
5743
<PackageReference Include="DotNetty.Handlers" Version="0.7.6" />
58-
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
59-
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="$(AvaloniaVersion)" />
6044
</ItemGroup>
6145

6246
<ItemGroup>
6347
<ProjectReference Include="..\ColorDesktop.Api\ColorDesktop.Api.csproj" />
6448
</ItemGroup>
65-
66-
<ItemGroup>
67-
<Compile Update="UI\Controls\Dialog\InstanceSettingControl.axaml.cs">
68-
<DependentUpon>InstanceSettingControl.axaml</DependentUpon>
69-
</Compile>
70-
</ItemGroup>
7149

7250
</Project>

src/ColorDesktop.Launcher/Helper/ConfigHelper.cs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using ColorDesktop.Api;
33
using ColorDesktop.Launcher.Hook;
44
using ColorDesktop.Launcher.Objs;
5+
using ColorDesktop.Launcher.UI.Models.Items;
6+
using ColorDesktop.Launcher.Utils;
57

68
namespace ColorDesktop.Launcher.Helper;
79

@@ -14,11 +16,27 @@ public static void LoadConfig()
1416
Config = ConfigUtils.Config(new ConfigObj()
1517
{
1618
EnablePlugin = [],
17-
EnableInstance = []
19+
EnableInstance = [],
20+
PluginSource =
21+
[
22+
new()
23+
{
24+
Url = HttpUtils.Url,
25+
Enable = true
26+
}
27+
]
1828
}, Program.RunDir + "config.json");
1929

2030
Config.EnablePlugin ??= [];
2131
Config.EnableInstance ??= [];
32+
Config.PluginSource ??=
33+
[
34+
new()
35+
{
36+
Url = HttpUtils.Url,
37+
Enable = true
38+
}
39+
];
2240
}
2341

2442
public static void SaveConfig()
@@ -96,4 +114,75 @@ public static void SetWindowTran(WindowTransparencyType value)
96114
Config.Tran = value;
97115
SaveConfig();
98116
}
117+
118+
public static void SetSourceEnable(string url, bool value)
119+
{
120+
foreach (var item in Config.PluginSource)
121+
{
122+
if (item.Url == url)
123+
{
124+
item.Enable = value;
125+
SaveConfig();
126+
return;
127+
}
128+
}
129+
}
130+
131+
public static bool HaveSource(string url)
132+
{
133+
foreach (var item in Config.PluginSource)
134+
{
135+
if (item.Url == url)
136+
{
137+
return true;
138+
}
139+
}
140+
141+
return false;
142+
}
143+
144+
public static void SetSourceUrl(string url, string newValue)
145+
{
146+
foreach (var item in Config.PluginSource)
147+
{
148+
if (item.Url == url)
149+
{
150+
item.Url = newValue;
151+
SaveConfig();
152+
return;
153+
}
154+
}
155+
}
156+
157+
public static void RemoveSource(string? url)
158+
{
159+
foreach (var item in Config.PluginSource)
160+
{
161+
if (item.Url == url)
162+
{
163+
Config.PluginSource.Remove(item);
164+
SaveConfig();
165+
return;
166+
}
167+
}
168+
}
169+
170+
public static void AddSource(PluginSourceItemModel model)
171+
{
172+
foreach (var item in Config.PluginSource)
173+
{
174+
if (item.Url == model.Url)
175+
{
176+
return;
177+
}
178+
}
179+
180+
Config.PluginSource.Add(new()
181+
{
182+
Enable = model.Enable,
183+
Url = model.Url!
184+
});
185+
186+
SaveConfig();
187+
}
99188
}

src/ColorDesktop.Launcher/Manager/PluginManager.cs

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Threading.Tasks;
56
using ColorDesktop.Api;
67
using ColorDesktop.Launcher.Helper;
78
using ColorDesktop.Launcher.Objs;
@@ -84,7 +85,7 @@ public static void Init()
8485
if (!PluginAssemblys.TryGetValue(item1.ID, out var ass1))
8586
{
8687
list.Add(item.Key);
87-
SetPluginState(item.Key, PluginState.LoadError);
88+
SetPluginState(item.Key, PluginState.DepNotFound);
8889
Logs.Error(string.Format("组件 {0} 加载失败,没有找到依赖 {1}", item, item1));
8990
break;
9091
}
@@ -98,7 +99,7 @@ public static void Init()
9899
catch (Exception e)
99100
{
100101
list.Add(item.Key);
101-
SetPluginState(item.Key, PluginState.LoadError);
102+
SetPluginState(item.Key, PluginState.DepNotFound);
102103
Logs.Error(string.Format("组件 {0} 加载依赖失败", item), e);
103104
}
104105
}
@@ -151,6 +152,7 @@ public static void StartPlugin()
151152
try
152153
{
153154
item.Value.Plugin.Init(item.Value.Local, InstanceManager.WorkDir, LanguageType.zh_cn);
155+
SetPluginState(item.Key, PluginState.Disable);
154156
}
155157
catch (Exception e)
156158
{
@@ -310,54 +312,49 @@ public static bool IsEnable(string id)
310312
return false;
311313
}
312314

313-
/// <summary>
314-
/// 组件是否启用错误
315-
/// </summary>
316-
/// <param name="id"></param>
317-
/// <returns></returns>
318-
public static bool IsEnableFail(string id)
315+
public static PluginState GetPluginState(string id)
319316
{
320317
if (PluginStates.TryGetValue(id, out var state))
321318
{
322-
return state == PluginState.EnableError;
319+
return state;
323320
}
324321

325-
return false;
322+
return PluginState.LoadError;
326323
}
327324

328-
/// <summary>
329-
/// 组件是否加载错误
330-
/// </summary>
331-
/// <param name="id"></param>
332-
/// <returns></returns>
333-
public static bool IsFail(string id)
325+
public static bool HavePluginSetting(string id)
334326
{
335-
if (PluginStates.TryGetValue(id, out var state))
327+
if (PluginAssemblys.TryGetValue(id, out var plugin))
336328
{
337-
return state is PluginState.LoadError or PluginState.DepNotFound;
329+
return plugin.Plugin.HavePluginSetting;
338330
}
339331

340332
return false;
341333
}
342334

343-
public static bool HavePluginSetting(string id)
335+
public static bool IsCoreLib(string id)
344336
{
345337
if (PluginAssemblys.TryGetValue(id, out var plugin))
346338
{
347-
return plugin.Plugin.HavePluginSetting;
339+
return plugin.Plugin.IsCoreLib;
348340
}
349341

350342
return false;
351343
}
352344

353-
public static bool IsCoreLib(string id)
345+
public static bool HavePlugin(string id)
354346
{
355-
if (PluginAssemblys.TryGetValue(id, out var plugin))
347+
return Plugins.ContainsKey(id);
348+
}
349+
350+
public static string? GetPluginVersion(string id)
351+
{
352+
if (Plugins.TryGetValue(id, out var obj))
356353
{
357-
return plugin.Plugin.IsCoreLib;
354+
return obj.Version;
358355
}
359356

360-
return false;
357+
return null;
361358
}
362359

363360
private static void DisablePlugin(string id, IPlugin plugin)
@@ -385,4 +382,51 @@ private static void SetPluginState(string id, PluginState state)
385382
PluginStates[id] = state;
386383
}
387384
}
385+
386+
public static async Task<bool> Download(PluginDownloadObj.ItemObj obj, string baseurl)
387+
{
388+
var dir = RunDir + "/" + obj.ID;
389+
int a = 1;
390+
while (Directory.Exists(dir))
391+
{
392+
dir = RunDir + "/" + obj.ID + $" ({a ++})";
393+
}
394+
395+
Directory.CreateDirectory(dir);
396+
397+
foreach (var item in obj.Files)
398+
{
399+
var res = await TempManager.Download(baseurl + obj.Url + "/" + item.Name, dir + "/" + item.Name, item.Sha1);
400+
if (!res)
401+
{
402+
Directory.Delete(dir, true);
403+
return false;
404+
}
405+
}
406+
407+
var list2 = PathHelper.GetAllFile(dir);
408+
var config = list2.FirstOrDefault(item =>
409+
item.Name.Equals(ConfigName, StringComparison.CurrentCultureIgnoreCase));
410+
if (config == null)
411+
{
412+
return false;
413+
}
414+
var obj1 = JsonConvert.DeserializeObject<PluginDataObj>(File.ReadAllText(config.FullName));
415+
if (obj1 == null)
416+
{
417+
return false;
418+
}
419+
420+
Plugins.Add(obj.ID, obj1);
421+
422+
if (obj1.ApiVersion != Program.ApiVersion)
423+
{
424+
Logs.Error(string.Format("组件 {0} 的API版本不一致", obj1.ID));
425+
SetPluginState(obj1.ID, PluginState.LoadError);
426+
return false;
427+
}
428+
429+
SetPluginState(obj1.ID, PluginState.Unload);
430+
return true;
431+
}
388432
}

0 commit comments

Comments
 (0)