XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

XFE字典储存新增与string之间的隐式转换 新增UAC权限管理器 新增配置文件管理器

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

代码差异

9 个文件 +266 -2
Modified XFE各类拓展.NetCore/FormatExtension/XFEDictionary.cs +10 -0
@@ -320,4 +320,14 @@ public class XFEDictionary : ICollection<XFEEntry>
320 320 /// XFE字典存储
321 321 /// </summary>
322 322 public XFEDictionary() { }
323 /// <summary>
324 /// 从字符串加载字典
325 /// </summary>
326 /// <param name="dictionaryString"></param>
327 public static implicit operator XFEDictionary(string dictionaryString) => new(dictionaryString);
328 /// <summary>
329 /// 将字典转为字符串
330 /// </summary>
331 /// <param name="xFEEntries"></param>
332 public static implicit operator string(XFEDictionary xFEEntries) => new(xFEEntries.ToString());
323 333 }
Modified XFE各类拓展.NetCore/FormatExtension/XFEMultiDictionary.cs +10 -0
@@ -289,4 +289,14 @@ public class XFEMultiDictionary : ICollection<XFEEntry>
289 289 /// XFE字典存储
290 290 /// </summary>
291 291 public XFEMultiDictionary() { }
292 /// <summary>
293 /// 从字符串加载字典
294 /// </summary>
295 /// <param name="dictionaryString"></param>
296 public static implicit operator XFEMultiDictionary(string dictionaryString) => new(dictionaryString);
297 /// <summary>
298 /// 将字典转为字符串
299 /// </summary>
300 /// <param name="xFEEntries"></param>
301 public static implicit operator string(XFEMultiDictionary xFEEntries) => new(xFEEntries.ToString());
292 302 }
Added XFE各类拓展.NetCore/PermissionExtension/AdministratorPermission.cs +72 -0
@@ -0,0 +1,72 @@
1 using System.Diagnostics;
2 using System.Security.Principal;
3 using XFE各类拓展.NetCore.FileExtension;
4
5 namespace XFE各类拓展.NetCore.PermissionExtension;
6
7 #if WINDOWS
8 #pragma warning disable CA1416 // 验证平台兼容性
9 /// <summary>
10 /// UAC权限(管理员权限)
11 /// </summary>
12 public static partial class AdministratorPermission
13 {
14 /// <summary>
15 /// 当前请求的状态
16 /// </summary>
17 public static CurrentPermissionState PermissionState { get; private set; }
18 /// <summary>
19 /// 当前是否以管理员身份运行
20 /// </summary>
21 /// <returns></returns>
22 public static bool IsAdministrator()
23 {
24 var identity = WindowsIdentity.GetCurrent();
25 var principal = new WindowsPrincipal(identity);
26 return principal.IsInRole(WindowsBuiltInRole.Administrator);
27 }
28 /// <summary>
29 ///
30 /// </summary>
31 public static void GetPermissionAndReboot()
32 {
33 try
34 {
35 "0".WriteIn("pm.xfe");
36 var startInfo = new ProcessStartInfo
37 {
38 UseShellExecute = true,
39 WorkingDirectory = Environment.CurrentDirectory,
40 FileName = Environment.ProcessPath,
41 Verb = "runas" // 使用UAC权限提升
42 };
43 Process.Start(startInfo);
44 Environment.Exit(0);
45 }
46 catch (Exception ex)
47 {
48 File.Delete("pm.xfe");
49 throw new XFEExtensionException("无法获取管理员权限", ex);
50 }
51 }
52 static AdministratorPermission()
53 {
54 var result = "pm.xfe".ReadOut();
55 var isAdmin = IsAdministrator();
56 if (isAdmin)
57 {
58 PermissionState = CurrentPermissionState.Administration;
59 }
60 else
61 {
62 if (result == "-1")
63 PermissionState = CurrentPermissionState.Normal;
64 else
65 PermissionState = CurrentPermissionState.PermissionDenied;
66 }
67 if (File.Exists("pm.xfe"))
68 File.Delete("pm.xfe");
69 }
70 }
71 #pragma warning restore CA1416 // 验证平台兼容性
72 #endif
Added XFE各类拓展.NetCore/PermissionExtension/CurrentPermissionState.cs +11 -0
@@ -0,0 +1,11 @@
1 namespace XFE各类拓展.NetCore.PermissionExtension;
2
3 public static partial class AdministratorPermission
4 {
5 public enum CurrentPermissionState
6 {
7 Administration,
8 PermissionDenied,
9 Normal
10 }
11 }
Added XFE各类拓展.NetCore/ProfileExtension/ProfileEntryInfo.cs +24 -0
@@ -0,0 +1,24 @@
1 using System.Reflection;
2
3 namespace XFE各类拓展.NetCore.ProfileExtension;
4
5 /// <summary>
6 /// 配置文件属性信息
7 /// </summary>
8 /// <param name="name">属性名称</param>
9 /// <param name="propertyInfo">属性信息</param>
10 public class ProfileEntryInfo(string name, PropertyInfo propertyInfo)
11 {
12 /// <summary>
13 /// 属性名称
14 /// </summary>
15 public string Name { get; set; } = name;
16 /// <summary>
17 /// 属性值
18 /// </summary>
19 public string? Value { get { return Property.GetValue(null)?.ToString(); } }
20 /// <summary>
21 /// 属性信息
22 /// </summary>
23 public PropertyInfo Property { get; init; } = propertyInfo;
24 }
Added XFE各类拓展.NetCore/ProfileExtension/ProfileInfo.cs +42 -0
@@ -0,0 +1,42 @@
1 using System.Reflection;
2
3 namespace XFE各类拓展.NetCore.ProfileExtension;
4
5 /// <summary>
6 /// 配置文件信息
7 /// </summary>
8 /// <param name="profileType">配置文件类型</param>
9 /// <param name="path">配置文件储存位置</param>
10 /// <param name="description">配置文件描述</param>
11 public class ProfileInfo(Type profileType, string path = "", string description = "")
12 {
13 /// <summary>
14 /// 配置文件类型
15 /// </summary>
16 public Type Profile { get; init; } = profileType;
17 /// <summary>
18 /// 配置文件储存位置
19 /// </summary>
20 public string Path { get; init; } = path == "" ? $"{profileType.Name}.xfe" : path;
21 /// <summary>
22 /// 配置文件描述
23 /// </summary>
24 public string? Description { get; set; } = description;
25 /// <summary>
26 /// 配置文件属性列表
27 /// </summary>
28 public List<ProfileEntryInfo> PropertiesInfo { get; init; } = GetPropertiesWithProfileAttribute(profileType);
29 internal static List<ProfileEntryInfo> GetPropertiesWithProfileAttribute(Type type)
30 {
31 var profileEntryList = new List<ProfileEntryInfo>();
32 foreach (var propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
33 if (propertyInfo.GetCustomAttribute<ProfilePropertyAttribute>() is not null)
34 profileEntryList.Add(new ProfileEntryInfo(propertyInfo.Name, propertyInfo));
35 return profileEntryList;
36 }
37 /// <summary>
38 /// 配置文件类型生成
39 /// </summary>
40 /// <param name="profileType"></param>
41 public static implicit operator ProfileInfo(Type profileType) => new(profileType);
42 }
Added XFE各类拓展.NetCore/ProfileExtension/ProfilePropertyAttribute.cs +7 -0
@@ -0,0 +1,7 @@
1 namespace XFE各类拓展.NetCore.ProfileExtension;
2
3 /// <summary>
4 /// 将属性视添加至储存列表
5 /// </summary>
6 [AttributeUsage(AttributeTargets.Property)]
7 public class ProfilePropertyAttribute : Attribute { }
Added XFE各类拓展.NetCore/ProfileExtension/XFEProfile.cs +84 -0
@@ -0,0 +1,84 @@
1 using XFE各类拓展.NetCore.FormatExtension;
2 using static System.Runtime.InteropServices.JavaScript.JSType;
3
4 namespace XFE各类拓展.NetCore.ProfileExtension;
5
6 /// <summary>
7 /// XFE配置文件,实现配置文件读写自动化
8 /// </summary>
9 public abstract class XFEProfile
10 {
11 /// <summary>
12 /// 配置文件清单
13 /// </summary>
14 public static List<ProfileInfo> Profiles { get; private set; } = [];
15 /// <summary>
16 /// 加载配置文件
17 /// </summary>
18 /// <param name="profileInfo"></param>
19 /// <returns></returns>
20 public static async Task LoadProfiles(params ProfileInfo[] profileInfo)
21 {
22 await Task.Run(() =>
23 {
24 Profiles.AddRange(profileInfo);
25 foreach (var profile in Profiles)
26 {
27 if (!File.Exists(profile.Path))
28 continue;
29 XFEDictionary propertyFileContent = File.ReadAllText(profile.Path);
30 for (int i = 0; i < profile.PropertiesInfo.Count; i++)
31 {
32 for (int j = 0; j < propertyFileContent.Count; j++)
33 {
34 var propertyInfo = profile.PropertiesInfo[i];
35 var property = propertyFileContent.ElementAt(j);
36 if (property.Header == propertyInfo.Name)
37 {
38 profile.PropertiesInfo[i].Property.SetValue(null, Convert.ChangeType(property.Content, propertyInfo.Property.PropertyType));
39 continue;
40 }
41 foreach (var propertySecFind in propertyFileContent)
42 {
43 if (propertySecFind.Header == propertyInfo.Name)
44 {
45 profile.PropertiesInfo[i].Property.SetValue(null, Convert.ChangeType(propertySecFind.Content, propertyInfo.Property.PropertyType));
46 break;
47 }
48 }
49 }
50 }
51 }
52 });
53 }
54 /// <summary>
55 /// 储存指定的配置文件
56 /// </summary>
57 /// <param name="profileInfo">配置文件</param>
58 /// <returns></returns>
59 public static async Task SaveProfile(ProfileInfo profileInfo)
60 {
61 var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile);
62 if (waitSaveProfile is null)
63 return;
64 var saveProfileDictionary = new XFEDictionary();
65 foreach (var property in waitSaveProfile.PropertiesInfo)
66 saveProfileDictionary.Add(property.Name, property.Value is null ? string.Empty : property.Value);
67 await File.WriteAllTextAsync(waitSaveProfile.Path, saveProfileDictionary);
68 }
69 /// <summary>
70 /// 储存配置文件
71 /// </summary>
72 /// <returns></returns>
73 public static async Task SaveProfiles()
74 {
75 foreach (var profile in Profiles)
76 await SaveProfile(profile);
77 }
78 /// <summary>
79 /// 可写在属性的set访问器后,用于自动储存
80 /// </summary>
81 /// <param name="profileInfo"></param>
82 /// <returns></returns>
83 protected static async Task AutoSave(ProfileInfo profileInfo) => await SaveProfile(profileInfo);
84 }
Modified XFE各类拓展.NetCore/WebExtension/XFEDownloader.cs +6 -2
@@ -8,6 +8,7 @@ namespace XFE各类拓展.NetCore.WebExtension;
8 8 /// <summary>
9 9 /// XFE下载器
10 10 /// </summary>
11 [Obsolete("出现严重漏洞,暂停使用,预计下个版本恢复", true)]
11 12 public class XFEDownloader : IDisposable
12 13 {
13 14 private bool disposedValue;
@@ -59,9 +60,11 @@ public class XFEDownloader : IDisposable
59 60 downloadTasks.Add(Task.Run(async () =>
60 61 {
61 62 using var fileStream = new FileStream(SavePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 8192, true);
62 long lastBufferDownloadSize = continueDownload ? await fileStream.GetValidPosition() : fileStream.Length / FileSegmentCount * currentSegment;
63 63 long endPosition = currentSegment == FileSegmentCount - 1 ? fileStream.Length - fileStream.Length / FileSegmentCount * currentSegment : fileStream.Length / FileSegmentCount * (currentSegment + 1);
64 long startBufferIndex = fileStream.Length / FileSegmentCount * currentSegment;
65 long lastBufferDownloadSize = continueDownload ? await fileStream.GetValidPosition(startBufferIndex, endPosition) : startBufferIndex;
64 66 fileStream.Seek(lastBufferDownloadSize, SeekOrigin.Begin);
67 long currentSegmentDownloadedBuffeSize = lastBufferDownloadSize - startBufferIndex;
65 68 if (continueDownload)
66 69 {
67 70 httpClient.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue(lastBufferDownloadSize, endPosition);
@@ -76,11 +79,12 @@ public class XFEDownloader : IDisposable
76 79 while (!IsPaused && !disposedValue) { }
77 80 await fileStream.WriteAsync(buffer.AsMemory(0, currentRead));
78 81 totalRead += currentRead;
82 currentSegmentDownloadedBuffeSize += currentRead;
79 83 var bufferCopy = new byte[8192];
80 84 bufferCopy.CopyTo(buffer, 0);
81 85 if (totalRead == totalFileSize)
82 86 Downloaded = true;
83 BufferDownloaded?.Invoke(this, new FileDownloadedEventArgs(bufferCopy, totalRead, totalFileSize, Downloaded));
87 BufferDownloaded?.Invoke(this, new FileDownloadedEventArgs(bufferCopy, totalRead, totalFileSize, fileStream.Length / FileSegmentCount, currentSegmentDownloadedBuffeSize, currentSegment, Downloaded));
84 88 }
85 89 }));
86 90 }