Skip to content

Commit

Permalink
Merge pull request #1131 from kenjiuno/fix-mm-msg-extraction
Browse files Browse the repository at this point in the history
Fix some YamlGenerator (ModsManager) inconvenience
  • Loading branch information
shananas authored Dec 28, 2024
2 parents bf15aab + 8ed8a4a commit 0f5ba57
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 47 deletions.
2 changes: 1 addition & 1 deletion OpenKh.Kh2/Messages/Internals/JapaneseEventDecode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class JapaneseEventDecode : IMessageDecode
[0x14] = new DataCmdModel(MessageCommand.Delay, 2),
[0x15] = new DataCmdModel(MessageCommand.CharDelay, 2),
[0x16] = new SingleDataCmdModel(MessageCommand.Unknown16),
[0x17] = null,
[0x17] = new DataCmdModel(MessageCommand.DelayAndFade, 2),
[0x18] = new DataCmdModel(MessageCommand.Unknown18, 2),
[0x19] = new TableCmdModel(MessageCommand.Table2, JapaneseEventTable._table2),
[0x1a] = new TableCmdModel(MessageCommand.Table3, JapaneseEventTable._table3),
Expand Down
2 changes: 1 addition & 1 deletion OpenKh.Kh2/Messages/Internals/JapaneseSystemDecode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class JapaneseSystemDecode : IMessageDecode
[0x14] = new DataCmdModel(MessageCommand.Delay, 2),
[0x15] = new DataCmdModel(MessageCommand.CharDelay, 2),
[0x16] = new SingleDataCmdModel(MessageCommand.Unknown16),
[0x17] = null,
[0x17] = new DataCmdModel(MessageCommand.DelayAndFade, 2),
[0x18] = new DataCmdModel(MessageCommand.Unknown18, 2),
[0x19] = new TableCmdModel(MessageCommand.Table2, JapaneseSystemTable._table2),
[0x1a] = new TableCmdModel(MessageCommand.Table3, JapaneseSystemTable._table3),
Expand Down
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
}
}
6 changes: 3 additions & 3 deletions OpenKh.Tools.ModsManager/ViewModels/ModViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ModViewModel(ModModel model, IChangeModEnableState changeModEnableState)
}
else
{
Author = _model.Metadata.OriginalAuthor;
Author = _model.Metadata?.OriginalAuthor;
Name = Source;
}

Expand Down Expand Up @@ -131,7 +131,7 @@ public bool Enabled
public string ReportBugUrl => $"https://github.com/{Source}/issues";
public string FilesToPatch => string.Join('\n', GetFilesToPatch());

public string Description => _model.Metadata.Description;
public string Description => _model.Metadata?.Description;

public string Homepage
{
Expand Down Expand Up @@ -163,7 +163,7 @@ public int UpdateCount

private IEnumerable<string> GetFilesToPatch()
{
foreach (var asset in _model.Metadata.Assets)
foreach (var asset in _model.Metadata?.Assets ?? Enumerable.Empty<Patcher.AssetFile>())
{
yield return asset.Name;
if (asset.Multi != null)
Expand Down
6 changes: 4 additions & 2 deletions OpenKh.Tools.ModsManager/Views/YamlGeneratorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ Func<Metadata, Task> action
? await File.ReadAllBytesAsync(modYmlFilePath)
: null;

var createNewModYml = rawInput == null;

var mod = (rawInput != null)
? Metadata.Read(new MemoryStream(rawInput, false))
: new Metadata();

var createNewModYml = mod == null;

mod ??= new Metadata(); // The empty yaml file brings null instance.

mod.Title ??= Path.GetFileName(Path.GetDirectoryName(modYmlFilePath));
mod.OriginalAuthor ??= "You";
mod.Description ??= $"Generated on {DateTime.UtcNow:R}";
Expand Down

0 comments on commit 0f5ba57

Please sign in to comment.