ref: 将 MeloongCore 的 WPF 部分独立到另一个子工程

This commit is contained in:
龙腾猫跃 committed 2026-04-28 21:40:00 +08:00
1 parent 1036bc9642
commit 1910e96567
13 files changed
+103 -29

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
namespace MeloongCore;
/// <summary>
/// 线程安全的 <see cref="List{T}"/> 实现。
/// 在 <see cref="SynchronizedCollection{T}"/> 的基础上,当调用枚举器时返回一个临时副本,以避免列表在枚举过程中被修改导致异常。
/// </summary>
public class ConcurrentList<T> : SynchronizedCollection<T>, IEnumerable, IEnumerable<T> {
// 构造函数
public ConcurrentList() : base() { }
public ConcurrentList(IEnumerable<T> data) : base(new object(), data) { }
public static implicit operator ConcurrentList<T>(List<T> data) => new(data);
public static implicit operator List<T>(ConcurrentList<T> data) => new(data);
// 对枚举器进行覆写,返回一个临时副本
public new IEnumerator<T> GetEnumerator() {
lock (SyncRoot) {
return Items.ToList().GetEnumerator();
}
}
IEnumerator IEnumerable.GetEnumerator() {
lock (SyncRoot) {
return Items.ToList().GetEnumerator();
}
}
}
+56
View File
@@ -0,0 +1,56 @@
namespace MeloongCore;
/// <summary>
/// 使用 Windows MsDelta API 生成、应用增量补丁文件。
/// </summary>
public static class MsDelta {
/// <summary>
/// 生成增量补丁文件:比较 <paramref name="oldFilePath"/> 和 <paramref name="newFilePath"/>,将补丁写入 <paramref name="deltaFilePath"/>。
/// </summary>
/// <exception cref="FileNotFoundException" />
/// <exception cref="Win32Exception" />
public static void Create(string oldFilePath, string newFilePath, string deltaFilePath) {
if (!File.Exists(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath);
if (!File.Exists(newFilePath)) throw new FileNotFoundException($"新文件不存在:{newFilePath}", newFilePath);
DirectoryUtils.Create(deltaFilePath, isFilePath: true);
// fileTypeSet:https://learn.microsoft.com/en-us/previous-versions/bb417345(v=msdn.10)?redirectedfrom=MSDN#file-type-sets (15L: DELTA_FILE_TYPE_SET_EXECUTABLES)
// 131072L: IgnoreFileSizeLimit, 32u: Crc32
if (!CreateDelta(15L, 131072L, 0L,
FileUtils.ShortenPath(oldFilePath), FileUtils.ShortenPath(newFilePath), null, null, new DeltaInput(), IntPtr.Zero, 32u, FileUtils.ShortenPath(deltaFilePath)))
throw new Win32Exception();
}
/// <summary>
/// 应用增量补丁文件:将 <paramref name="deltaFilePath"/> 补丁应用到 <paramref name="oldFilePath"/>,将结果写入 <paramref name="newFilePath"/>。
/// </summary>
/// <exception cref="FileNotFoundException" />
/// <exception cref="Win32Exception" />
public static void Apply(string oldFilePath, string deltaFilePath, string newFilePath) {
if (!File.Exists(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath);
if (!File.Exists(deltaFilePath)) throw new FileNotFoundException($"补丁文件不存在:{deltaFilePath}", deltaFilePath);
DirectoryUtils.Create(newFilePath, isFilePath: true);
// 1L: AllowLegacy
if (!ApplyDelta(1L, FileUtils.ShortenPath(oldFilePath), FileUtils.ShortenPath(deltaFilePath), FileUtils.ShortenPath(newFilePath)))
throw new Win32Exception();
}
[StructLayout(LayoutKind.Sequential)]
private struct DeltaInput {
public IntPtr Start;
public IntPtr Size;
[MarshalAs(UnmanagedType.Bool)] public bool Editable;
}
[DllImport("msdelta.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CreateDelta(
long fileTypeSet, long setFlags, long resetFlags,
string sourceName, string targetName, string? sourceOptionsName,
string? targetOptionsName, DeltaInput globalOptions,
IntPtr targetFileTime, uint hashAlgId, string deltaName);
[DllImport("msdelta.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ApplyDelta(long applyFlags, string sourceName, string deltaName, string targetName);
}
+94
View File
@@ -0,0 +1,94 @@
namespace MeloongCore;
public readonly struct OneOf<T0, T1> {
private readonly int _index;
private readonly T0? _value0;
private readonly T1? _value1;
public static implicit operator OneOf<T0, T1>(T0 value) => new(0, value, default);
public static implicit operator OneOf<T0, T1>(T1 value) => new(1, default, value);
private OneOf(int index, T0? value0, T1? value1) {
_index = index;
_value0 = value0;
_value1 = value1;
}
/// <summary>
/// 对不同类型执行不同的操作,并返回同一个值。
/// </summary>
public TResult Switch<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1) => _index switch {
0 => f0(_value0!), _ => f1(_value1!)
};
/// <summary>
/// 对不同类型执行不同的操作。
/// </summary>
public void Switch(Action<T0> f0, Action<T1> f1) {
if (_index == 0) f0(_value0!);
else if (_index == 1) f1(_value1!);
}
/// <summary>
/// 判断当前的类型是否为 T。
/// </summary>
public bool Is<T>() => _index switch {
0 when typeof(T) == typeof(T0) => true,
1 when typeof(T) == typeof(T1) => true,
_ => false
};
/// <summary>
/// 假定当前的类型为 T,并返回该值。
/// 若当前值的类型不为 T,则抛出 InvalidOperationException。
/// </summary>
public T As<T>() => _index switch {
0 when typeof(T) == typeof(T0) => (T)(object)_value0!,
1 when typeof(T) == typeof(T1) => (T)(object)_value1!,
_ => throw new InvalidOperationException()
};
}
public readonly struct OneOf<T0, T1, T2> {
private readonly int _index;
private readonly T0? _value0;
private readonly T1? _value1;
private readonly T2? _value2;
public static implicit operator OneOf<T0, T1, T2>(T0 value) => new(0, value, default, default);
public static implicit operator OneOf<T0, T1, T2>(T1 value) => new(1, default, value, default);
public static implicit operator OneOf<T0, T1, T2>(T2 value) => new(2, default, default, value);
private OneOf(int index, T0? value0, T1? value1, T2? value2) {
_index = index;
_value0 = value0;
_value1 = value1;
_value2 = value2;
}
/// <summary>
/// 对不同类型执行不同的操作,并返回同一个值。
/// </summary>
public TResult Switch<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2) => _index switch {
0 => f0(_value0!), 1 => f1(_value1!), _ => f2(_value2!)
};
/// <summary>
/// 对不同类型执行不同的操作。
/// </summary>
public void Switch(Action<T0> f0, Action<T1> f1, Action<T2> f2) {
if (_index == 0) f0(_value0!);
else if (_index == 1) f1(_value1!);
else if (_index == 2) f2(_value2!);
}
/// <summary>
/// 判断当前的类型是否为 T。
/// </summary>
public bool Is<T>() => _index switch {
0 when typeof(T) == typeof(T0) => true,
1 when typeof(T) == typeof(T1) => true,
2 when typeof(T) == typeof(T2) => true,
_ => false
};
/// <summary>
/// 假定当前的类型为 T,并返回该值。
/// 若当前值的类型不为 T,则抛出 InvalidOperationException。
/// </summary>
public T As<T>() => _index switch {
0 when typeof(T) == typeof(T0) => (T) (object) _value0!,
1 when typeof(T) == typeof(T1) => (T) (object) _value1!,
2 when typeof(T) == typeof(T2) => (T) (object) _value2!,
_ => throw new InvalidOperationException()
};
}