Skip to content

Commit 3ea4a55

Browse files
committed
回滚Packer。
把packer.Core的输出目录调整和packer一致
1 parent 3703d47 commit 3ea4a55

12 files changed

Lines changed: 77 additions & 53 deletions

File tree

src/Packer.Core/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
4343
.Where(ns => !config.Base.ExclusionNamespaces.Contains(ns.NamespaceName))
4444
.SelectMany(ns => ns.PackerPolicies
4545
.SelectMany(p => p.CreateProviders(ns, config.Floating.Merge(ns.LocalConfig))))
46-
.ToList();
46+
.ToLookup(p => p.Destination);
4747

4848
// -- 按 Destination 合并同名文件 ----------------------------------------------
4949
// 同类型 KVPFile(JsonFile/LangFile)走 Merge 方法合并词条
5050
// 其他类型冲突时保留第一个,打出警告
51-
var merged = allProviders.ToLookup(p => p.Destination).Select(group =>
51+
var merged = allProviders.Select(group =>
5252
{
5353
if (!group.Skip(1).Any()) return group.First();
5454
if (group.All(p => p is KVPFile))
@@ -76,7 +76,7 @@
7676
};
7777

7878
// -- 写入 ZIP -----------------------------------------------------------------
79-
var packName = $"./test/packer.core/{config.Base.Version}.zip";
79+
var packName = $"./Minecraft-Mod-Language-Modpack-{config.Base.Version}.zip";
8080
await using var stream = File.Create(packName);
8181
using (var archive = new ZipArchive(stream, ZipArchiveMode.Update, leaveOpen: true))
8282
{
@@ -93,5 +93,5 @@
9393
stream.Seek(0, SeekOrigin.Begin);
9494
var md5 = Convert.ToHexString(MD5.Create().ComputeHash(stream));
9595
Log.Information("打包文件的 MD5 值:{0}", md5);
96-
File.WriteAllText($"./test/packer.core/{config.Base.Version}.md5", md5);
96+
File.WriteAllText($"./{config.Base.Version}.md5", md5);
9797
Log.Information("对版本 {0} 的打包结束", version);

src/Packer/Extensions/ContentExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static string LogToDebug(this string message)
107107
/// <returns></returns>
108108
public static string ComputeMD5(this Stream stream)
109109
{
110-
stream.Seek(0, SeekOrigin.Begin); // 确保文件流的位置被重置
110+
stream.Position = 0; // 确保文件流的位置被重置
111111
return Convert.ToHexString(MD5.Create().ComputeHash(stream));
112112
}
113113
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace Packer.Extensions;
7+
8+
internal static class FileInfoExtension
9+
{
10+
public static string ReadAllText(this FileInfo file)
11+
{
12+
using var stream = file.OpenText();
13+
var text = stream.ReadToEnd();
14+
return text;
15+
}
16+
}

src/Packer/Helpers/ConfigHelpers.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public static class ConfigHelpers
2525
{
2626
var configFile = directory.GetFiles("local-config.json").FirstOrDefault();
2727

28-
if (configFile is null) return null;
28+
if (configFile is null)
29+
return null;
2930

3031
configFile.FullName.LogToDebug("读取文件:{0}");
31-
32-
using var reader = configFile.OpenText();
32+
3333
return JsonSerializer.Deserialize<FloatingConfig>(
34-
reader.ReadToEnd().LogToDebug(),
34+
configFile.ReadAllText().LogToDebug(),
3535
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
3636
}
3737

@@ -48,10 +48,11 @@ public static async Task<Config> RetrieveConfig(string configTemplate, string ve
4848

4949
Log.Information("配置位置:{0}", configPath);
5050

51-
var content = await File.ReadAllBytesAsync(configPath);
52-
return JsonSerializer.Deserialize<Config>(
51+
await using var content = File.OpenRead(configPath);
52+
53+
return (await JsonSerializer.DeserializeAsync<Config>(
5354
content,
54-
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })!;
55+
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }))!;
5556
}
5657

5758
/// <summary>
@@ -72,9 +73,8 @@ public static List<PackerPolicy> RetrievePolicy(DirectoryInfo directory)
7273

7374
file.FullName.LogToDebug("读取文件:{0}");
7475

75-
using var reader = file.OpenText();
7676
var result = JsonSerializer.Deserialize<List<PackerPolicy>>(
77-
reader.ReadToEnd().LogToDebug(),
77+
file.ReadAllText().LogToDebug(),
7878
new JsonSerializerOptions
7979
{
8080
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,

src/Packer/Models/Config.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public class FloatingConfig
121121
InclusionDomains = InclusionDomains.Concat(other.InclusionDomains).Distinct(),
122122
InclusionPaths = InclusionPaths.Concat(other.InclusionPaths).Distinct(),
123123
CharacterReplacement = CharacterReplacement.Concat(other.CharacterReplacement).DistinctBy(_ => _.Key)
124-
.ToDictionary(_ => _.Key, _ => _.Value),
124+
.ToDictionary(),
125125
DestinationReplacement = DestinationReplacement.Concat(other.DestinationReplacement).DistinctBy(_ => _.Key)
126-
.ToDictionary(_ => _.Key, _ => _.Value)
126+
.ToDictionary()
127127
};
128128

129129

src/Packer/Models/IResourceFileProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public IResourceFileProvider ReplaceContent(string searchPattern, string replace
4747
/// </summary>
4848
/// <param name="archive"></param>
4949
/// <exception cref="InvalidOperationException">资源包中已有同名文件</exception>
50-
public Task WriteToArchive(ZipArchive archive);
50+
public Task WriteToArchiveAsync(ZipArchive archive);
5151

5252
/// <summary>
5353
/// 目标在资源包中的相对位置,从根目录算起

src/Packer/Models/Providers/CompositionHelper.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using Packer.Extensions;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Text.Json;
@@ -16,10 +17,9 @@ public static partial class LangMappingHelper
1617
/// </summary>
1718
/// <param name="file">组合文件</param>
1819
public static LangMappingProvider CreateFromComposition(FileInfo file)
19-
{
20-
using var reader = file.OpenText();
20+
{
2121
var data = JsonSerializer.Deserialize<CompositionData>(
22-
reader.ReadToEnd(),
22+
file.ReadAllText(),
2323
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
2424
return new LangMappingProvider(
2525
new LangDictionaryWrapper(CompositionHelper.CreateRawDictionary(data)),
@@ -34,10 +34,9 @@ public static partial class JsonMappingHelper
3434
/// </summary>
3535
/// <param name="file">组合文件</param>
3636
public static JsonMappingProvider CreateFromComposition(FileInfo file)
37-
{
38-
using var reader = file.OpenText();
37+
{
3938
var data = JsonSerializer.Deserialize<CompositionData>(
40-
reader.ReadToEnd(),
39+
file.ReadAllText(),
4140
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
4241
return new JsonMappingProvider(
4342
JsonDictionaryWrapper.Create(CompositionHelper.CreateRawDictionary(data)),
@@ -67,7 +66,7 @@ internal static IEnumerable<KeyValuePair<IEnumerable<TOuter>, IEnumerable<TInner
6766
seed: new[] { KeyValuePair.Create(Enumerable.Empty<TOuter>(), Enumerable.Empty<TInner>()) }
6867
as IEnumerable<KeyValuePair<IEnumerable<TOuter>, IEnumerable<TInner>>>, // 这都需要手写...
6968
(accumulate, next) => from incomingPair in next
70-
join existingGroup in accumulate on true equals true
69+
from existingGroup in accumulate
7170
select KeyValuePair.Create(
7271
existingGroup.Key.Append(incomingPair.Key),
7372
existingGroup.Value.Append(incomingPair.Value)));

src/Packer/Models/Providers/McMetaProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override IResourceFileProvider ReplaceContent(string searchPattern, strin
3737
public override IResourceFileProvider ReplaceDestination(string searchPattern, string replacement)
3838
=> this;
3939
/// <inheritdoc/>
40-
public override async Task WriteToArchive(ZipArchive archive)
40+
public override async Task WriteToArchiveAsync(ZipArchive archive)
4141
{
4242
var destination = Destination.NormalizePath();
4343
Log.Debug("[McMetaProvider]写入路径 {0}", destination);

src/Packer/Models/Providers/RawFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public IResourceFileProvider ReplaceDestination(string searchPattern, string rep
4444
RegexOptions.Singleline));
4545

4646
/// <inheritdoc/>
47-
public async Task WriteToArchive(ZipArchive archive)
47+
public async Task WriteToArchiveAsync(ZipArchive archive)
4848
{
4949
var destination = Destination.NormalizePath();
5050
Log.Debug("[RawFile]写入路径 {0}", destination);

src/Packer/Models/Providers/TermMappingProvider.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ public ITermDictionary<JsonNode> ReplaceContent(string searchPattern, string rep
8484
var result = new Dictionary<string, JsonNode>();
8585
foreach (var (key, value) in this)
8686
{
87-
if (value is JsonValue jsonValue
88-
&& jsonValue.TryGetValue<string>(out var stringValue))
87+
if (value.GetValueKind() == JsonValueKind.String)
8988
{
90-
var replaced = Regex.Replace(stringValue,
89+
var replaced = Regex.Replace(value.GetValue<string>(),
9190
searchPattern,
9291
replacement,
9392
RegexOptions.Singleline);
94-
result.Add(key, JsonValue.Create(replaced)!); // 我猜不会null罢
93+
result.Add(key, JsonValue.Create(replaced)); // 我猜不会null罢
9594
continue;
9695
}
9796
result.Add(key, value);
@@ -101,12 +100,8 @@ public ITermDictionary<JsonNode> ReplaceContent(string searchPattern, string rep
101100

102101
public static ITermDictionary<JsonNode> Create(IDictionary<string, string> nominalMapping)
103102
{
104-
var query = from pair in nominalMapping
105-
let key = pair.Key
106-
let value = pair.Value
107-
let node = JsonValue.Create(value)!
108-
select (key, node);
109-
var transformed = query.ToDictionary(_ => _.key, _ => _.node as JsonNode);
103+
var transformed = nominalMapping
104+
.ToDictionary(p => p.Key, p => JsonValue.Create(p.Value) as JsonNode);
110105
return new JsonDictionaryWrapper(transformed);
111106
}
112107
}
@@ -145,7 +140,8 @@ public TermMappingProvider(ITermDictionary<TValue> map, string destination)
145140
/// <inheritdoc/>
146141
public IResourceFileProvider ApplyTo(IResourceFileProvider? baseProvider, ApplyOptions options)
147142
{
148-
if (baseProvider is null) return this;
143+
if (baseProvider is null)
144+
return this;
149145

150146
if (baseProvider is not TermMappingProvider<TValue> baseMapping)
151147
throw new ArgumentException($"Argument not an instance of {typeof(TermMappingProvider<TValue>)}.",
@@ -190,7 +186,7 @@ public IResourceFileProvider ReplaceDestination(string searchPattern, string rep
190186
RegexOptions.Singleline));
191187

192188
/// <inheritdoc/>
193-
public async Task WriteToArchive(ZipArchive archive)
189+
public async Task WriteToArchiveAsync(ZipArchive archive)
194190
{
195191
var destination = Destination.NormalizePath();
196192
Log.Debug("[TermMappingProvider`1]写入路径 {0}", destination);
@@ -287,7 +283,8 @@ internal static Dictionary<string, string> DeserializeFromLang(string content)
287283
var splitPosition = line.IndexOf('=');
288284

289285
// https://github.com/CFPAOrg/Minecraft-Mod-Language-Package/pull/3272/files#r1461545452
290-
if (splitPosition == -1) continue;
286+
if (splitPosition == -1)
287+
continue;
291288

292289
var key = line[..splitPosition];
293290
var value = splitPosition + 1 < line.Length

0 commit comments

Comments
 (0)