Skip to content

Commit 4cc5972

Browse files
committed
补全 Append / DestinationReplacement / 文档修正
- TextFile 加 PolicyItem 和 Merge(TextFile) 方法处理 Append - KVPFile 的 PolicyItem 上提至 TextFile,子类继承 - Program.cs 合并逻辑统一为 TextFile 分支,内部分发 KVPFile - DestinationReplacement 在 ZIP 写入时用预编译 Regex 替换路径 - FloatingConfig 文档注释修正:内容替换表 → 目标地址替换表 - CompositionEntry 提取到独立文件
1 parent 82d8718 commit 4cc5972

9 files changed

Lines changed: 67 additions & 59 deletions

File tree

src/Packer.Core/Model/Abstract/INamespaceProvider.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,3 @@ public interface INamespaceProvider
3434
/// </summary>
3535
IEnumerable<INamespaceResource> GetNamespaces(string modName, string version);
3636
}
37-
38-
/// <summary>
39-
/// 命名空间资源。是资源包产出物的基本单元。
40-
/// </summary>
41-
public interface INamespaceResource
42-
{
43-
/// <summary>所属模组标识(如 "jei", "minecraft")</summary>
44-
string ModName { get; }
45-
46-
/// <summary>命名空间名称(如 "minecraft", "cfpa")</summary>
47-
string NamespaceName { get; }
48-
49-
/// <summary>目标 Minecraft 版本(如 "1.21")</summary>
50-
string ModVersion { get; }
51-
52-
/// <summary>文件系统上的完整路径</summary>
53-
string LocalPath { get; }
54-
55-
/// <summary>命名空间下的局域浮动配置(local-config.json),无则为 <see cref="FloatingConfig.Shared"/></summary>
56-
FloatingConfig LocalConfig { get; }
57-
58-
/// <summary>打包策略集合,无则为 <see cref="PackerPolicy.Shared"/></summary>
59-
PackerPolicy PackerPolicies { get; }
60-
61-
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Packer.Core.Model.Abstract;
2+
3+
/// <summary>
4+
/// 命名空间资源。是资源包产出物的基本单元。
5+
/// </summary>
6+
public interface INamespaceResource
7+
{
8+
/// <summary>所属模组标识(如 "jei", "minecraft")</summary>
9+
string ModName { get; }
10+
11+
/// <summary>命名空间名称(如 "minecraft", "cfpa")</summary>
12+
string NamespaceName { get; }
13+
14+
/// <summary>目标 Minecraft 版本(如 "1.21")</summary>
15+
string ModVersion { get; }
16+
17+
/// <summary>文件系统上的完整路径</summary>
18+
string LocalPath { get; }
19+
20+
/// <summary>命名空间下的局域浮动配置(local-config.json),无则为 <see cref="FloatingConfig.Shared"/></summary>
21+
FloatingConfig LocalConfig { get; }
22+
23+
/// <summary>打包策略集合,无则为 <see cref="PackerPolicy.Shared"/></summary>
24+
PackerPolicy PackerPolicies { get; }
25+
26+
}

src/Packer.Core/Model/Configuration/Config.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public record Config(BaseConfig Base, FloatingConfig Floating)
1010
/// <summary>
1111
/// 从命名空间下的局域配置加载内容。
1212
/// </summary>
13-
public Config Modify(FloatingConfig floatingConfig)
13+
public Config Modify(FloatingConfig? floatingConfig)
1414
{
1515
return this with { Floating = Floating.Merge(floatingConfig) };
1616
}

src/Packer.Core/Model/Configuration/FloatingConfig.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/// <param name="InclusionPaths">强制包含的路径</param>
99
/// <param name="ExclusionPaths">强制排除的路径</param>
1010
/// <param name="CharacterReplacement">文本字符替换表</param>
11-
/// <param name="DestinationReplacement">内容替换表</param>
11+
/// <param name="DestinationReplacement">目标地址替换表</param>
1212
public record FloatingConfig(
1313
IEnumerable<string> InclusionDomains,
1414
IEnumerable<string> ExclusionDomains,
@@ -65,16 +65,17 @@ public static FloatingConfig Load(INamespaceResource ns)
6565
/// <summary>
6666
/// 从另一对象合并配置
6767
/// </summary>
68-
public FloatingConfig Merge(FloatingConfig other)
68+
public FloatingConfig Merge(FloatingConfig? other)
6969
{
70-
if (this == Shared)
71-
{
72-
return other;
73-
}
70+
7471
if ((other ??= Shared) == Shared)
7572
{
7673
return this;
7774
}
75+
if (this == Shared)
76+
{
77+
return other;
78+
}
7879

7980
return new FloatingConfig(
8081
InclusionDomains: InclusionDomains.Union(other.InclusionDomains).ToFrozenSet(),

src/Packer.Core/Model/PackerPolicys/PackerPolicyItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected IResourceFileProvider CreateProvider(string filePath, string relativeP
7373

7474
case { parentDir: "lang" }:
7575
case { ext: ".txt" or ".json" or ".md" }:
76-
return new TextFile(File.ReadAllText(filePath), relativePath) { Namespace = ns };
76+
return new TextFile(File.ReadAllText(filePath), relativePath) { Namespace = ns, PolicyItem = this };
7777

7878
default:
7979
return new RawFile(filePath, relativePath) { Namespace = ns };

src/Packer.Core/Model/ResourceFile/KVPFile.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ public abstract class KVPFile : TextFile
99
/// <summary>键值对条目</summary>
1010
public Dictionary<string, string> Entries { get; protected set; } = [];
1111

12-
/// <summary>创建此 provider 的策略项,合并时读取 <c>ModifyOnly</c></summary>
13-
public PackerPolicyItem? PolicyItem { get; set; }
1412
/// <param name="relativePath">在资源包中的相对路径</param>
1513
protected KVPFile(string relativePath) : base(relativePath)
1614
{

src/Packer.Core/Model/ResourceFile/TextFile.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class TextFile : ResourceFileProvider
77
/// <summary>生效的浮动配置,<see cref="GetContentStream"/> 写入前根据此配置做字符替换。</summary>
88
public FloatingConfig? EffectiveConfig { get; set; }
99

10+
/// <summary>创建此 provider 的策略项,合并时读取 <c>Append</c></summary>
11+
public PackerPolicyItem? PolicyItem { get; set; }
12+
1013
public TextFile(string content, string relativePath)
1114
: base(relativePath)
1215
{
@@ -16,6 +19,16 @@ public TextFile(string content, string relativePath)
1619
protected TextFile(string relativePath)
1720
: base(relativePath) { }
1821

22+
/// <summary>与另一个 TextFile 合并。</summary>
23+
public virtual TextFile Merge(TextFile other)
24+
{
25+
if (this is KVPFile selfKvp && other is KVPFile otherKvp)
26+
return selfKvp.Merge(otherKvp);
27+
return other.PolicyItem?.Append == true
28+
? new TextFile(string.Concat(Content, Environment.NewLine, other.Content), RelativePath)
29+
: this;
30+
}
31+
1932
public override Stream GetContentStream()
2033
{
2134
var text = Content;

src/Packer.Core/Program.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// 入口:解析参数 → 加载配置 → 获取命名空间(全量/增量)
44
// → 按策略展开 Provider → 合并 → 替换 → 写 ZIP → MD5
55
// ============================================================
6-
6+
77
// -- 参数解析 ------------------------------------------------------------------
88
var versionOpt = new Option<string>("--version", "--v");
99
var incrementOpt = new Option<bool>("--increment", "--i");
@@ -49,13 +49,15 @@
4949
// 其他类型冲突时保留第一个,打出警告
5050
var merged = allProviders.Select(group =>
5151
{
52-
if (!group.Skip(1).Any()) return group.First();
53-
if (group.All(p => p is KVPFile))
52+
if (!group.Skip(1).Any())
53+
return group.First();
54+
if (group.All(p => p is TextFile))
5455
{
55-
var r = group.Cast<KVPFile>().Aggregate((a, n) => a.Merge(n));
56-
if (r is ResourceFileProvider rfp)
57-
rfp.Namespace = group.Cast<KVPFile>().First().Namespace;
58-
return r;
56+
TextFile result = group.All(p => p is KVPFile)
57+
? group.Cast<KVPFile>().Aggregate((a, n) => a.Merge(n))
58+
: group.Cast<TextFile>().Aggregate((a, n) => a.Merge(n));
59+
result.Namespace = ((ResourceFileProvider)group.First()).Namespace;
60+
return result;
5961
}
6062
Log.Warning("Destination 冲突但无法合并: {Dest}, 保留第一个", group.Key);
6163
return group.First();
@@ -76,12 +78,21 @@
7678
// -- 写入 ZIP -----------------------------------------------------------------
7779
var packName = $"./Minecraft-Mod-Language-Modpack-{config.Base.Version}.zip";
7880
await using var stream = File.Create(packName);
81+
var destReplacements = config.Floating.DestinationReplacement
82+
.ToDictionary(kv => new Regex(kv.Key), kv => kv.Value);
83+
7984
using (var archive = new ZipArchive(stream, ZipArchiveMode.Update, leaveOpen: true))
8085
{
8186
foreach (var p in merged.Concat(initialFiles))
8287
{
88+
var dest = p.Destination;
89+
foreach (var (pattern, replacement) in destReplacements)
90+
{
91+
dest = pattern.Replace(dest, replacement);
92+
}
93+
8394
using var content = p.GetContentStream();
84-
var entry = archive.CreateEntry(p.Destination);
95+
var entry = archive.CreateEntry(dest);
8596
using var es = entry.Open();
8697
await content.CopyToAsync(es);
8798
}

src/Packer/Extensions/FileInfoExtension.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)