51 lines
1.9 KiB
C#
51 lines
1.9 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
|
||
namespace XWorld.PublishTool
|
||
{
|
||
public sealed class FilesManifest
|
||
{
|
||
public sealed class Entry { public string Md5; public int Size; }
|
||
public Dictionary<string, Entry> Entries { get; } = new Dictionary<string, Entry>();
|
||
|
||
public void Add(string name, string md5, int size) => Entries[name] = new Entry { Md5 = md5, Size = size };
|
||
|
||
// 行格式沿用 dllfiles:{'name','md5',size},\n(按 name 排序,保证确定性)
|
||
public string Render()
|
||
{
|
||
var sb = new StringBuilder();
|
||
foreach (var kv in Entries.OrderBy(e => e.Key, System.StringComparer.Ordinal))
|
||
sb.Append("{'").Append(kv.Key).Append("','").Append(kv.Value.Md5).Append("',").Append(kv.Value.Size).Append("},\n");
|
||
return sb.ToString();
|
||
}
|
||
|
||
public static FilesManifest Parse(string text)
|
||
{
|
||
var m = new FilesManifest();
|
||
if (string.IsNullOrEmpty(text)) return m;
|
||
foreach (var raw in text.Split('\n'))
|
||
{
|
||
string line = raw.Trim();
|
||
int lb = line.IndexOf('{'); int rb = line.LastIndexOf('}');
|
||
if (lb < 0 || rb <= lb) continue;
|
||
string inner = line.Substring(lb + 1, rb - lb - 1).Replace("'", "");
|
||
string[] p = inner.Split(',');
|
||
if (p.Length < 3) continue;
|
||
int.TryParse(p[2].Trim(), out int size);
|
||
m.Add(p[0].Trim(), p[1].Trim(), size);
|
||
}
|
||
return m;
|
||
}
|
||
|
||
// 稳定摘要:排序后的 name+md5 串(用于签名)
|
||
public string Digest()
|
||
{
|
||
var sb = new StringBuilder();
|
||
foreach (var kv in Entries.OrderBy(e => e.Key, System.StringComparer.Ordinal))
|
||
sb.Append(kv.Key).Append('=').Append(kv.Value.Md5).Append(';');
|
||
return sb.ToString();
|
||
}
|
||
}
|
||
}
|