Skip to content

Commit

Permalink
Fix problems of ProvideExtractorsService:
Browse files Browse the repository at this point in the history
- Remove redundant `await Task.Yield();`
- Valid file path to msg.yml is now set:
  ```
  - name: msg/jp/sys.bar
    method: binarc
    source:
    - name: sys
      method: kh2msg
      source:
      - name: msg/jp/sys_sys.yml
        language: jp
      type: list
  ```
- Japanese evt messages are now set to `je` like `language: je`:
  ```
  - name: msg/jp/al.bar
    method: binarc
    source:
    - name: sys
      method: kh2msg
      source:
      - name: msg/jp/al_al.yml
        language: je
      type: list
  ```
  • Loading branch information
kenjiuno committed Dec 28, 2024
1 parent e7d028d commit 8ed8a4a
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions OpenKh.Patcher/BarEntryExtractor/ProvideExtractorsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace OpenKh.Patcher.BarEntryExtractor
{
public class ProvideExtractorsService
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

private readonly ISerializer _yamlSer = new SerializerBuilder()
.Build();

Expand All @@ -23,7 +25,7 @@ public class ProvideExtractorsService
private Extractor CreateMessageExtractor(
IEnumerable<string> tags,
Regex sourceFilePattern,
Func<Match, string> languageSelector,
string language,
IMessageDecode decoder
)
{
Expand All @@ -34,17 +36,24 @@ IMessageDecode decoder
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
var msgEntries = Msg.Read(barEntry.Stream.FromBegin());
var messages = new List<Dictionary<string, object>>();
foreach (var msgEntry in msgEntries)
{
var readableMessage = MsgSerializer.SerializeText(decoder.Decode(msgEntry.Data));
// The format of the dictionary needs to be matched with the patcher.
// Check the method: OpenKh.Patcher.PatcherProcessor.PatchKh2Msg
messages.Add(
new Dictionary<string, object>
{
{ "id", msgEntry.Id },
{ language, readableMessage }
}
);
}
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
msgEntries
.Select(
msgEntry => new IdText(
msgEntry.Id,
MsgSerializer.SerializeText(decoder.Decode(msgEntry.Data))
)
)
messages
)
);
},
Expand All @@ -57,17 +66,15 @@ IMessageDecode decoder
Source = CreateSourceFromArgs(
new AssetFile
{
Name = "sys",
Language = languageSelector(sourceFilePattern.Match(arg.OriginalRelativePath)),
Name = arg.SourceName,
Language = language,
}
),
}
)
);
}

private record IdText(int Id, string Text);

public IEnumerable<Extractor> GetExtractors()
{
var extractors = new List<Extractor>();
Expand All @@ -79,7 +86,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.SystemData.Trsr.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -110,7 +116,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.SystemData.Item.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -141,7 +146,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.Battle.Fmlv.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -172,7 +176,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.Battle.Lvup.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -203,7 +206,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.Battle.Bons.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -234,7 +236,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.Battle.Atkp.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -265,7 +266,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.Battle.Plrp.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -296,7 +296,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.SystemData.Cmd.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -327,7 +326,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.Battle.Enmp.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -358,7 +356,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.SystemData.Sklt.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -389,7 +386,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.Battle.Przt.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -420,7 +416,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
OpenKh.Kh2.Battle.Magc.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -451,7 +446,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".model",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return barEntry.Stream.ReadAllBytes();
}
));
Expand All @@ -463,42 +457,50 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".tim",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return barEntry.Stream.ReadAllBytes();
}
));

foreach (var lang in "us,fr,gr,it,sp".Split(','))
{
extractors.Add(CreateMessageExtractor(
tags: new[] { "message", "sys.bar", "InternationalSystem" },
sourceFilePattern: new Regex($@"msg/{lang}/sys\.bar"),
language: lang,
decoder: Encoders.InternationalSystem
));

extractors.Add(CreateMessageExtractor(
tags: new[] { "message", "xx.bar", "InternationalSystem" },
sourceFilePattern: new Regex(@"msg/(?<language>us|fr|gr|it|sp)/(sys|[a-z]{2})\.bar"),
languageSelector: match => match.Groups["language"].Value,
decoder: Encoders.InternationalSystem
));
extractors.Add(CreateMessageExtractor(
tags: new[] { "message", "xx.bar", "InternationalSystem" },
sourceFilePattern: new Regex($@"msg/{lang}/[a-z]{{2}}\.bar"),
language: lang,
decoder: Encoders.InternationalSystem
));
}

extractors.Add(CreateMessageExtractor(
tags: new[] { "message", "sys.bar", "TurkishSystem" },
sourceFilePattern: new Regex(@"msg/tr/sys\.bar"),
languageSelector: match => "tr",
language: "tr",
decoder: Encoders.TurkishSystem
));
extractors.Add(CreateMessageExtractor(
tags: new[] { "message", "xx.bar", "TurkishSystem" },
sourceFilePattern: new Regex(@"msg/tr/[a-z]{2}\.bar"),
languageSelector: match => "tr",
language: "tr",
decoder: Encoders.TurkishSystem
));

extractors.Add(CreateMessageExtractor(
tags: new[] { "message", "sys.bar", "JapaneseSystem" },
sourceFilePattern: new Regex(@"msg/jp/sys\.bar"),
languageSelector: match => "jp",
language: "jp",
decoder: Encoders.JapaneseSystem
));
extractors.Add(CreateMessageExtractor(
tags: new[] { "message", "xx.bar", "JapaneseEvent" },
sourceFilePattern: new Regex(@"msg/jp/[a-z]{2}.bar"),
languageSelector: match => "je",
language: "je",
decoder: Encoders.JapaneseEvent
));

Expand All @@ -510,7 +512,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".bdscript",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
var decoder = new BdxDecoder(
read: new MemoryStream(barEntry.Stream.ReadAllBytes(), false),
codeRevealer: true,
Expand Down Expand Up @@ -544,7 +545,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".script",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
Kh2.Ard.AreaDataScript.Decompile(
Kh2.Ard.AreaDataScript.Read(barEntry.Stream.FromBegin())
Expand Down Expand Up @@ -575,7 +575,6 @@ public IEnumerable<Extractor> GetExtractors()
FileExtension: ".yml",
ExtractAsync: async (barEntry) =>
{
await Task.Yield();
return Encoding.UTF8.GetBytes(
_yamlSer.Serialize(
Kh2.Ard.SpawnPoint.Read(
Expand All @@ -602,5 +601,7 @@ public IEnumerable<Extractor> GetExtractors()

return extractors.AsReadOnly();
}

#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
}
}

0 comments on commit 8ed8a4a

Please sign in to comment.