From baebcded450f16b78d9e4725c43e7b6e4acad01c Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 16 Jun 2021 15:23:10 +0300 Subject: [PATCH 1/4] Started refactoring. Restructured project. Fix naming. Refactor locdatamap parser. --- CR2W2JSON.Core/LocDataMapParser.cs | 80 ----------------- .../{ => Parser}/AudioEventArrayParser.cs | 88 ++++++------------- CR2W2JSON.Core/{ => Parser}/IParser.cs | 2 +- CR2W2JSON.Core/Parser/LocDataMapParser.cs | 42 +++++++++ CR2W2JSON.Core/{ => Parser}/OnScreenParser.cs | 23 +++-- .../StringIdVariantLengthsReportParser.cs} | 6 +- .../{ => Parser}/SubtitlesMapParser.cs | 2 +- .../{ => Parser}/SubtitlesParser.cs | 2 +- .../VoLanguageDataMapParser.cs} | 6 +- .../{VOMapParser.cs => Parser/VoMapParser.cs} | 6 +- CR2W2JSON.Core/Program.cs | 7 +- 11 files changed, 101 insertions(+), 163 deletions(-) delete mode 100644 CR2W2JSON.Core/LocDataMapParser.cs rename CR2W2JSON.Core/{ => Parser}/AudioEventArrayParser.cs (59%) rename CR2W2JSON.Core/{ => Parser}/IParser.cs (68%) create mode 100644 CR2W2JSON.Core/Parser/LocDataMapParser.cs rename CR2W2JSON.Core/{ => Parser}/OnScreenParser.cs (73%) rename CR2W2JSON.Core/{StringIDVariantLengthsReportParser.cs => Parser/StringIdVariantLengthsReportParser.cs} (92%) rename CR2W2JSON.Core/{ => Parser}/SubtitlesMapParser.cs (98%) rename CR2W2JSON.Core/{ => Parser}/SubtitlesParser.cs (98%) rename CR2W2JSON.Core/{VOLanguageDataMapParser.cs => Parser/VoLanguageDataMapParser.cs} (95%) rename CR2W2JSON.Core/{VOMapParser.cs => Parser/VoMapParser.cs} (94%) diff --git a/CR2W2JSON.Core/LocDataMapParser.cs b/CR2W2JSON.Core/LocDataMapParser.cs deleted file mode 100644 index 30996d0..0000000 --- a/CR2W2JSON.Core/LocDataMapParser.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System.Collections.Generic; -using System.Text.Json.Serialization; -using WolvenKit.Common.Model.Cr2w; - -namespace CR2W2JSON.Core -{ - public class LocDataMapParser : IParser - { - class Entry - { - [JsonInclude] - [JsonPropertyName("langCode")] - public string LangCode; - - [JsonInclude] - [JsonPropertyName("onscreensPath")] - public string OnscreensPath; - - [JsonInclude] - [JsonPropertyName("subtitlePath")] - public string SubtitlePath; - } - - class EntriesArray - { - [JsonInclude] - [JsonPropertyName("entries")] - public List Entries; - } - - private readonly ICR2WExport _chunk; - - public LocDataMapParser(ICR2WExport chunk) - { - _chunk = chunk; - } - - public object GetData() - { - var output = new EntriesArray(); - - foreach (var v in _chunk.data.ChildrEditableVariables) - { - //"entries": - output.Entries = GetMetaData(v); - } - - return output; - } - - private List GetMetaData(IEditableVariable evar) - { - var metaList = new List(); - - foreach (var sVariable in evar.ChildrEditableVariables) - { - var obj = new Entry(); - foreach (var editableVariable in sVariable.ChildrEditableVariables) - { - var rv = editableVariable.REDValue; - switch (editableVariable.REDName) - { - case "langCode": - obj.LangCode = rv; - break; - case "onscreensPath": - obj.OnscreensPath = rv.Replace("[Soft]", ""); - break; - case "subtitlePath": - obj.SubtitlePath = rv.Replace("[Soft]", ""); - break; - } - } - metaList.Add(obj); - } - - return metaList; - } - } -} diff --git a/CR2W2JSON.Core/AudioEventArrayParser.cs b/CR2W2JSON.Core/Parser/AudioEventArrayParser.cs similarity index 59% rename from CR2W2JSON.Core/AudioEventArrayParser.cs rename to CR2W2JSON.Core/Parser/AudioEventArrayParser.cs index c2a2ee1..8c24450 100644 --- a/CR2W2JSON.Core/AudioEventArrayParser.cs +++ b/CR2W2JSON.Core/Parser/AudioEventArrayParser.cs @@ -3,61 +3,31 @@ using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; -namespace CR2W2JSON.Core +namespace CR2W2JSON.Core.Parser { class AudioEventArray { - [JsonInclude] - [JsonPropertyName("isSortedByRedHash")] - public bool IsSortedByRedHash = true; - - [JsonInclude] - [JsonPropertyName("events")] - public List Events; - - [JsonInclude] - [JsonPropertyName("switchGroup")] - public List SwitchGroup; - - [JsonInclude] - [JsonPropertyName("switch")] - public List Switch; + [JsonInclude] [JsonPropertyName("isSortedByRedHash")] public bool IsSortedByRedHash = true; + [JsonInclude] [JsonPropertyName("events")] public List Events; + [JsonInclude] [JsonPropertyName("switchGroup")] public List SwitchGroup; - [JsonInclude] - [JsonPropertyName("stateGroup")] - public List StateGroup; - - [JsonInclude] - [JsonPropertyName("state")] - public List State; - - [JsonInclude] - [JsonPropertyName("gameParameter")] - public List GameParameter; - - [JsonInclude] - [JsonPropertyName("bus")] - public List Bus; + [JsonInclude] [JsonPropertyName("switch")] public List Switch; + [JsonInclude] [JsonPropertyName("stateGroup")] public List StateGroup; + [JsonInclude] [JsonPropertyName("state")] public List State; + [JsonInclude] [JsonPropertyName("gameParameter")] public List GameParameter; + [JsonInclude] [JsonPropertyName("bus")] public List Bus; } class EventMetaData { - [JsonPropertyName("redId")] - public string RedId { get; set; } - [JsonPropertyName("wwiseId")] - public UInt32 WwiseId { get; set; } - [JsonPropertyName("maxAttenuation")] - public float MaxAttenuation { get; set; } - [JsonPropertyName("minDuration")] - public float MinDuration { get; set; } - [JsonPropertyName("maxDuration")] - public float MaxDuration { get; set; } - [JsonPropertyName("isLooping")] - public bool IsLooping { get; set; } - [JsonPropertyName("stopActionEvents")] - public List StopActionEvents { get; set; } - [JsonPropertyName("tags")] - public List Tags { get; set; } + [JsonInclude] [JsonPropertyName("redId")] public string RedId; + [JsonInclude] [JsonPropertyName("wwiseId")] public UInt32 WwiseId; + [JsonInclude] [JsonPropertyName("maxAttenuation")] public float MaxAttenuation; + [JsonInclude] [JsonPropertyName("minDuration")] public float MinDuration; + [JsonInclude] [JsonPropertyName("maxDuration")] public float MaxDuration; + [JsonInclude] [JsonPropertyName("isLooping")] public bool IsLooping; + [JsonInclude] [JsonPropertyName("stopActionEvents")] public List StopActionEvents; + [JsonInclude] [JsonPropertyName("tags")] public List Tags; } public class AudioEventArrayParser : IParser @@ -141,20 +111,10 @@ private List GetMetaData(IEditableVariable evar) obj.IsLooping = bool.Parse(rv); break; case "stopActionEvents": - var evList = new List(); - foreach (var ev in editableVariable.ChildrEditableVariables) - { - evList.Add(ev.REDValue); - } - obj.StopActionEvents = evList; + obj.StopActionEvents = GetListFromEdVars(editableVariable.ChildrEditableVariables); break; case "tags": - var tagList = new List(); - foreach (var ev in editableVariable.ChildrEditableVariables) - { - tagList.Add(ev.REDValue); - } - obj.Tags = tagList; + obj.Tags = GetListFromEdVars(editableVariable.ChildrEditableVariables); break; } } @@ -163,5 +123,15 @@ private List GetMetaData(IEditableVariable evar) return metaList; } + + private List GetListFromEdVars(List vars) + { + var varList = new List(); + foreach (var ev in vars) + { + varList.Add(ev.REDValue); + } + return varList; + } } } \ No newline at end of file diff --git a/CR2W2JSON.Core/IParser.cs b/CR2W2JSON.Core/Parser/IParser.cs similarity index 68% rename from CR2W2JSON.Core/IParser.cs rename to CR2W2JSON.Core/Parser/IParser.cs index 6b4bde0..c7b5883 100644 --- a/CR2W2JSON.Core/IParser.cs +++ b/CR2W2JSON.Core/Parser/IParser.cs @@ -1,4 +1,4 @@ -namespace CR2W2JSON.Core +namespace CR2W2JSON.Core.Parser { public interface IParser { diff --git a/CR2W2JSON.Core/Parser/LocDataMapParser.cs b/CR2W2JSON.Core/Parser/LocDataMapParser.cs new file mode 100644 index 0000000..a5d174b --- /dev/null +++ b/CR2W2JSON.Core/Parser/LocDataMapParser.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using WolvenKit.Common.Model.Cr2w; + +namespace CR2W2JSON.Core.Parser +{ + public class LocDataMapParser : IParser + { + private readonly ICR2WExport _chunk; + + public LocDataMapParser(ICR2WExport chunk) + { + _chunk = chunk; + } + + public object GetData() + { + return new Dictionary>> + { + {"entries", GetLocDataMapEntries(_chunk.data.ChildrEditableVariables[0])} + }; + } + + private List> GetLocDataMapEntries(IEditableVariable evar) + { + var metaList = new List>(); + + foreach (var sVariable in evar.ChildrEditableVariables) + { + var obj = new Dictionary(); + foreach (var editableVariable in sVariable.ChildrEditableVariables) + { + var rv = editableVariable.REDValue; + obj.Add(editableVariable.REDName, rv.Replace("[Soft]", "")); + } + + metaList.Add(obj); + } + + return metaList; + } + } +} diff --git a/CR2W2JSON.Core/OnScreenParser.cs b/CR2W2JSON.Core/Parser/OnScreenParser.cs similarity index 73% rename from CR2W2JSON.Core/OnScreenParser.cs rename to CR2W2JSON.Core/Parser/OnScreenParser.cs index fed96de..c0fb158 100644 --- a/CR2W2JSON.Core/OnScreenParser.cs +++ b/CR2W2JSON.Core/Parser/OnScreenParser.cs @@ -3,26 +3,31 @@ using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; -namespace CR2W2JSON.Core +namespace CR2W2JSON.Core.Parser { class OnScreenEntries { - [JsonInclude][JsonPropertyName("entries")] + [JsonInclude] + [JsonPropertyName("entries")] public List Entries = new(); } class OnScreenEntry { - [JsonInclude][JsonPropertyName("primaryKey")] + [JsonInclude] + [JsonPropertyName("primaryKey")] public UInt64 PrimaryKey; - [JsonInclude][JsonPropertyName("secondaryKey")] + [JsonInclude] + [JsonPropertyName("secondaryKey")] public string SecondaryKey; - [JsonInclude][JsonPropertyName("femaleVariant")] + [JsonInclude] + [JsonPropertyName("femaleVariant")] public string FemaleVariant; - [JsonInclude][JsonPropertyName("maleVariant")] + [JsonInclude] + [JsonPropertyName("maleVariant")] public string MaleVariant; } @@ -50,13 +55,13 @@ public object GetData() entry.PrimaryKey = UInt64.Parse(ev.REDValue); break; case "secondaryKey": - entry.SecondaryKey = ev.REDValue != null ? ev.REDValue : ""; + entry.SecondaryKey = ev.REDValue ?? ""; break; case "femaleVariant": - entry.FemaleVariant = ev.REDValue != null ? ev.REDValue : ""; + entry.FemaleVariant = ev.REDValue ?? ""; break; case "maleVariant": - entry.MaleVariant = ev.REDValue != null ? ev.REDValue : ""; + entry.MaleVariant = ev.REDValue ?? ""; break; } } diff --git a/CR2W2JSON.Core/StringIDVariantLengthsReportParser.cs b/CR2W2JSON.Core/Parser/StringIdVariantLengthsReportParser.cs similarity index 92% rename from CR2W2JSON.Core/StringIDVariantLengthsReportParser.cs rename to CR2W2JSON.Core/Parser/StringIdVariantLengthsReportParser.cs index 1da2c61..1ffe554 100644 --- a/CR2W2JSON.Core/StringIDVariantLengthsReportParser.cs +++ b/CR2W2JSON.Core/Parser/StringIdVariantLengthsReportParser.cs @@ -2,9 +2,9 @@ using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; -namespace CR2W2JSON.Core +namespace CR2W2JSON.Core.Parser { - public class StringIDVariantLengthsReportParser : IParser + public class StringIdVariantLengthsReportParser : IParser { class Entry { @@ -30,7 +30,7 @@ class EntriesArray private readonly ICR2WExport _chunk; - public StringIDVariantLengthsReportParser(ICR2WExport chunk) + public StringIdVariantLengthsReportParser(ICR2WExport chunk) { _chunk = chunk; } diff --git a/CR2W2JSON.Core/SubtitlesMapParser.cs b/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs similarity index 98% rename from CR2W2JSON.Core/SubtitlesMapParser.cs rename to CR2W2JSON.Core/Parser/SubtitlesMapParser.cs index c504363..7ab5a60 100644 --- a/CR2W2JSON.Core/SubtitlesMapParser.cs +++ b/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs @@ -2,7 +2,7 @@ using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; -namespace CR2W2JSON.Core +namespace CR2W2JSON.Core.Parser { public class SubtitlesMapParser : IParser { diff --git a/CR2W2JSON.Core/SubtitlesParser.cs b/CR2W2JSON.Core/Parser/SubtitlesParser.cs similarity index 98% rename from CR2W2JSON.Core/SubtitlesParser.cs rename to CR2W2JSON.Core/Parser/SubtitlesParser.cs index de3313d..fefe7ed 100644 --- a/CR2W2JSON.Core/SubtitlesParser.cs +++ b/CR2W2JSON.Core/Parser/SubtitlesParser.cs @@ -2,7 +2,7 @@ using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; -namespace CR2W2JSON.Core +namespace CR2W2JSON.Core.Parser { public class SubtitlesParser : IParser { diff --git a/CR2W2JSON.Core/VOLanguageDataMapParser.cs b/CR2W2JSON.Core/Parser/VoLanguageDataMapParser.cs similarity index 95% rename from CR2W2JSON.Core/VOLanguageDataMapParser.cs rename to CR2W2JSON.Core/Parser/VoLanguageDataMapParser.cs index 7893c66..af7f1be 100644 --- a/CR2W2JSON.Core/VOLanguageDataMapParser.cs +++ b/CR2W2JSON.Core/Parser/VoLanguageDataMapParser.cs @@ -2,9 +2,9 @@ using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; -namespace CR2W2JSON.Core +namespace CR2W2JSON.Core.Parser { - public class VOLanguageDataMapParser : IParser + public class VoLanguageDataMapParser : IParser { class Entry { @@ -36,7 +36,7 @@ class EntriesArray private readonly ICR2WExport _chunk; - public VOLanguageDataMapParser(ICR2WExport chunk) + public VoLanguageDataMapParser(ICR2WExport chunk) { _chunk = chunk; } diff --git a/CR2W2JSON.Core/VOMapParser.cs b/CR2W2JSON.Core/Parser/VoMapParser.cs similarity index 94% rename from CR2W2JSON.Core/VOMapParser.cs rename to CR2W2JSON.Core/Parser/VoMapParser.cs index 5596d0d..4d092e1 100644 --- a/CR2W2JSON.Core/VOMapParser.cs +++ b/CR2W2JSON.Core/Parser/VoMapParser.cs @@ -2,9 +2,9 @@ using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; -namespace CR2W2JSON.Core +namespace CR2W2JSON.Core.Parser { - public class VOMapParser : IParser + public class VoMapParser : IParser { class Entry { @@ -30,7 +30,7 @@ class EntriesArray private readonly ICR2WExport _chunk; - public VOMapParser(ICR2WExport chunk) + public VoMapParser(ICR2WExport chunk) { _chunk = chunk; } diff --git a/CR2W2JSON.Core/Program.cs b/CR2W2JSON.Core/Program.cs index d0c3856..ebfee7b 100644 --- a/CR2W2JSON.Core/Program.cs +++ b/CR2W2JSON.Core/Program.cs @@ -7,6 +7,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using Catel.IoC; +using CR2W2JSON.Core.Parser; using WolvenKit.Common.Model.Cr2w; using WolvenKit.Common.Services; using WolvenKit.RED4.CR2W; @@ -119,11 +120,11 @@ static int Main(string[] args) case "localizationPersistenceLocDataMap": return new LocDataMapParser(chunk); case "locVoLanguageDataMap": - return new VOLanguageDataMapParser(chunk); + return new VoLanguageDataMapParser(chunk); case "locVoiceoverMap": - return new VOMapParser(chunk); + return new VoMapParser(chunk); case "locVoiceoverLengthMap": - return new StringIDVariantLengthsReportParser(chunk); + return new StringIdVariantLengthsReportParser(chunk); case "localizationPersistenceSubtitleEntries": return new SubtitlesParser(chunk); case "localizationPersistenceSubtitleMap": From 55de5356ac9374ecfd4683fe50a8b2fb0c6205d4 Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 16 Jun 2021 15:29:00 +0300 Subject: [PATCH 2/4] Yet another refactoring of LocDataMapParser. --- CR2W2JSON.Core/Parser/LocDataMapParser.cs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/CR2W2JSON.Core/Parser/LocDataMapParser.cs b/CR2W2JSON.Core/Parser/LocDataMapParser.cs index a5d174b..35959b5 100644 --- a/CR2W2JSON.Core/Parser/LocDataMapParser.cs +++ b/CR2W2JSON.Core/Parser/LocDataMapParser.cs @@ -12,31 +12,26 @@ public LocDataMapParser(ICR2WExport chunk) _chunk = chunk; } - public object GetData() + public object GetData() => new Dictionary>> { - return new Dictionary>> - { - {"entries", GetLocDataMapEntries(_chunk.data.ChildrEditableVariables[0])} - }; - } + {"entries", GetLocDataMapEntries(_chunk.data.ChildrEditableVariables[0])} + }; private List> GetLocDataMapEntries(IEditableVariable evar) { - var metaList = new List>(); + var entries = new List>(); foreach (var sVariable in evar.ChildrEditableVariables) { - var obj = new Dictionary(); - foreach (var editableVariable in sVariable.ChildrEditableVariables) + var entry = new Dictionary(); + foreach (var ev in sVariable.ChildrEditableVariables) { - var rv = editableVariable.REDValue; - obj.Add(editableVariable.REDName, rv.Replace("[Soft]", "")); + entry.Add(ev.REDName, ev.REDValue.Replace("[Soft]", "")); } - - metaList.Add(obj); + entries.Add(entry); } - return metaList; + return entries; } } } From ce263a9126028dce328de4ef0cefd6d50870c8dc Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 16 Jun 2021 20:28:54 +0300 Subject: [PATCH 3/4] Total refactoring. --- CR2W2JSON.Core/Parser/AbstractParser.cs | 21 ++++ .../Parser/AudioEventArrayParser.cs | 104 +++--------------- CR2W2JSON.Core/Parser/IParser.cs | 7 -- CR2W2JSON.Core/Parser/LocDataMapParser.cs | 24 ++-- CR2W2JSON.Core/Parser/OnScreenParser.cs | 60 +++------- .../StringIdVariantLengthsReportParser.cs | 73 +++--------- CR2W2JSON.Core/Parser/SubtitlesMapParser.cs | 89 ++++++--------- CR2W2JSON.Core/Parser/SubtitlesParser.cs | 73 +++--------- .../Parser/VoLanguageDataMapParser.cs | 73 +++--------- CR2W2JSON.Core/Parser/VoMapParser.cs | 78 +++---------- CR2W2JSON.Core/Program.cs | 91 +++++---------- 11 files changed, 185 insertions(+), 508 deletions(-) create mode 100644 CR2W2JSON.Core/Parser/AbstractParser.cs delete mode 100644 CR2W2JSON.Core/Parser/IParser.cs diff --git a/CR2W2JSON.Core/Parser/AbstractParser.cs b/CR2W2JSON.Core/Parser/AbstractParser.cs new file mode 100644 index 0000000..b1f2b4c --- /dev/null +++ b/CR2W2JSON.Core/Parser/AbstractParser.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.Runtime.Serialization; +using WolvenKit.Common.Model.Cr2w; + +namespace CR2W2JSON.Core.Parser +{ + public abstract class AbstractParser + { + protected readonly ICR2WExport Chunk; + + protected AbstractParser(ICR2WExport chunk) + { + Chunk = chunk; + } + + public abstract ISerializable GetData(); + + protected Dictionary>> GetEntriesDictionary(List> entryList) => + new() {{"entries", entryList}}; + } +} \ No newline at end of file diff --git a/CR2W2JSON.Core/Parser/AudioEventArrayParser.cs b/CR2W2JSON.Core/Parser/AudioEventArrayParser.cs index 8c24450..d57304e 100644 --- a/CR2W2JSON.Core/Parser/AudioEventArrayParser.cs +++ b/CR2W2JSON.Core/Parser/AudioEventArrayParser.cs @@ -1,120 +1,50 @@ using System; using System.Collections.Generic; -using System.Text.Json.Serialization; +using System.Linq; +using System.Runtime.Serialization; using WolvenKit.Common.Model.Cr2w; namespace CR2W2JSON.Core.Parser { - class AudioEventArray + public class AudioEventArrayParser : AbstractParser { - [JsonInclude] [JsonPropertyName("isSortedByRedHash")] public bool IsSortedByRedHash = true; - [JsonInclude] [JsonPropertyName("events")] public List Events; - [JsonInclude] [JsonPropertyName("switchGroup")] public List SwitchGroup; - - [JsonInclude] [JsonPropertyName("switch")] public List Switch; - [JsonInclude] [JsonPropertyName("stateGroup")] public List StateGroup; - [JsonInclude] [JsonPropertyName("state")] public List State; - [JsonInclude] [JsonPropertyName("gameParameter")] public List GameParameter; - [JsonInclude] [JsonPropertyName("bus")] public List Bus; - } - - class EventMetaData - { - [JsonInclude] [JsonPropertyName("redId")] public string RedId; - [JsonInclude] [JsonPropertyName("wwiseId")] public UInt32 WwiseId; - [JsonInclude] [JsonPropertyName("maxAttenuation")] public float MaxAttenuation; - [JsonInclude] [JsonPropertyName("minDuration")] public float MinDuration; - [JsonInclude] [JsonPropertyName("maxDuration")] public float MaxDuration; - [JsonInclude] [JsonPropertyName("isLooping")] public bool IsLooping; - [JsonInclude] [JsonPropertyName("stopActionEvents")] public List StopActionEvents; - [JsonInclude] [JsonPropertyName("tags")] public List Tags; - } - - public class AudioEventArrayParser : IParser - { - private readonly ICR2WExport _chunk; - - public AudioEventArrayParser(ICR2WExport chunk) + public override ISerializable GetData() { - _chunk = chunk; - } - - public object GetData() - { - var output = new AudioEventArray(); - - foreach (var v in _chunk.data.ChildrEditableVariables) - { - switch (v.REDName) - { - case "events": - output.Events = GetMetaData(v); - break; - - case "switchGroup": - output.SwitchGroup = GetMetaData(v); - break; - - case "switch": - output.Switch = GetMetaData(v); - break; - - case "stateGroup": - output.StateGroup = GetMetaData(v); - break; - - case "state": - output.State = GetMetaData(v); - break; - - case "gameParameter": - output.GameParameter = GetMetaData(v); - break; - - case "bus": - output.Bus = GetMetaData(v); - break; - } - } - - return output; + return Chunk.data.ChildrEditableVariables.ToDictionary( + v => v.REDName, v => GetMetaData(v) + ); } - private List GetMetaData(IEditableVariable evar) + private List> GetMetaData(IEditableVariable evar) { - var metaList = new List(); + var metaList = new List>(); foreach (var sVariable in evar.ChildrEditableVariables) { - var obj = new EventMetaData(); + var obj = new Dictionary(); foreach (var editableVariable in sVariable.ChildrEditableVariables) { var rv = editableVariable.REDValue; + var redName = editableVariable.REDName; switch (editableVariable.REDName) { case "redId": - obj.RedId = rv; + obj.Add(redName, rv); break; case "wwiseId": - obj.WwiseId = UInt32.Parse(rv); + obj.Add(redName, UInt32.Parse(rv)); break; case "maxAttenuation": - obj.MaxAttenuation = float.Parse(rv); - break; case "minDuration": - obj.MinDuration = float.Parse(rv); - break; case "maxDuration": - obj.MaxDuration = float.Parse(rv); + obj.Add(redName, float.Parse(rv)); break; case "isLooping": - obj.IsLooping = bool.Parse(rv); + obj.Add(redName, bool.Parse(rv)); break; case "stopActionEvents": - obj.StopActionEvents = GetListFromEdVars(editableVariable.ChildrEditableVariables); - break; case "tags": - obj.Tags = GetListFromEdVars(editableVariable.ChildrEditableVariables); + obj.Add(redName, GetListFromEdVars(editableVariable.ChildrEditableVariables)); break; } } @@ -133,5 +63,7 @@ private List GetListFromEdVars(List vars) } return varList; } + + public AudioEventArrayParser(ICR2WExport chunk) : base(chunk) {} } } \ No newline at end of file diff --git a/CR2W2JSON.Core/Parser/IParser.cs b/CR2W2JSON.Core/Parser/IParser.cs deleted file mode 100644 index c7b5883..0000000 --- a/CR2W2JSON.Core/Parser/IParser.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace CR2W2JSON.Core.Parser -{ - public interface IParser - { - public object GetData(); - } -} \ No newline at end of file diff --git a/CR2W2JSON.Core/Parser/LocDataMapParser.cs b/CR2W2JSON.Core/Parser/LocDataMapParser.cs index 35959b5..5de9460 100644 --- a/CR2W2JSON.Core/Parser/LocDataMapParser.cs +++ b/CR2W2JSON.Core/Parser/LocDataMapParser.cs @@ -1,29 +1,21 @@ using System.Collections.Generic; +using System.Runtime.Serialization; using WolvenKit.Common.Model.Cr2w; namespace CR2W2JSON.Core.Parser { - public class LocDataMapParser : IParser + public class LocDataMapAbstractParser : AbstractParser { - private readonly ICR2WExport _chunk; + public override ISerializable GetData() => + GetEntriesDictionary(GetLocDataMapEntries(Chunk.data.ChildrEditableVariables[0])); - public LocDataMapParser(ICR2WExport chunk) + private List> GetLocDataMapEntries(IEditableVariable evar) { - _chunk = chunk; - } - - public object GetData() => new Dictionary>> - { - {"entries", GetLocDataMapEntries(_chunk.data.ChildrEditableVariables[0])} - }; - - private List> GetLocDataMapEntries(IEditableVariable evar) - { - var entries = new List>(); + var entries = new List>(); foreach (var sVariable in evar.ChildrEditableVariables) { - var entry = new Dictionary(); + var entry = new Dictionary(); foreach (var ev in sVariable.ChildrEditableVariables) { entry.Add(ev.REDName, ev.REDValue.Replace("[Soft]", "")); @@ -33,5 +25,7 @@ private List> GetLocDataMapEntries(IEditableVariable return entries; } + + public LocDataMapAbstractParser(ICR2WExport chunk) : base(chunk) {} } } diff --git a/CR2W2JSON.Core/Parser/OnScreenParser.cs b/CR2W2JSON.Core/Parser/OnScreenParser.cs index c0fb158..a954592 100644 --- a/CR2W2JSON.Core/Parser/OnScreenParser.cs +++ b/CR2W2JSON.Core/Parser/OnScreenParser.cs @@ -1,75 +1,41 @@ using System; using System.Collections.Generic; +using System.Runtime.Serialization; using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; namespace CR2W2JSON.Core.Parser { - class OnScreenEntries + public class OnScreenParser : AbstractParser { - [JsonInclude] - [JsonPropertyName("entries")] - public List Entries = new(); - } - - class OnScreenEntry - { - [JsonInclude] - [JsonPropertyName("primaryKey")] - public UInt64 PrimaryKey; - - [JsonInclude] - [JsonPropertyName("secondaryKey")] - public string SecondaryKey; - - [JsonInclude] - [JsonPropertyName("femaleVariant")] - public string FemaleVariant; - - [JsonInclude] - [JsonPropertyName("maleVariant")] - public string MaleVariant; - } - - public class OnScreenParser : IParser - { - private ICR2WExport _chunk; - - public OnScreenParser(ICR2WExport chunk) - { - _chunk = chunk; - } - - public object GetData() + public override ISerializable GetData() { - var entries = new OnScreenEntries(); + var entries = new List>(); - foreach (var childr in _chunk.data.ChildrEditableVariables[0].ChildrEditableVariables) + foreach (var childr in Chunk.data.ChildrEditableVariables[0].ChildrEditableVariables) { - var entry = new OnScreenEntry(); + var entry = new Dictionary(); foreach (var ev in childr.ChildrEditableVariables) { switch (ev.REDName) { case "primaryKey": - entry.PrimaryKey = UInt64.Parse(ev.REDValue); + entry.Add(ev.REDName, UInt64.Parse(ev.REDValue)); break; case "secondaryKey": - entry.SecondaryKey = ev.REDValue ?? ""; - break; case "femaleVariant": - entry.FemaleVariant = ev.REDValue ?? ""; - break; case "maleVariant": - entry.MaleVariant = ev.REDValue ?? ""; + entry.Add(ev.REDName, ev.REDValue ?? ""); break; } } - entries.Entries.Add(entry); + entries.Add(entry); } - - return entries; + + return GetEntriesDictionary(entries); } + + public OnScreenParser(ICR2WExport chunk) : base(chunk) {} } } \ No newline at end of file diff --git a/CR2W2JSON.Core/Parser/StringIdVariantLengthsReportParser.cs b/CR2W2JSON.Core/Parser/StringIdVariantLengthsReportParser.cs index 1ffe554..8c6f576 100644 --- a/CR2W2JSON.Core/Parser/StringIdVariantLengthsReportParser.cs +++ b/CR2W2JSON.Core/Parser/StringIdVariantLengthsReportParser.cs @@ -1,80 +1,37 @@ using System.Collections.Generic; -using System.Text.Json.Serialization; +using System.Runtime.Serialization; using WolvenKit.Common.Model.Cr2w; +using WolvenKit.RED4.CR2W; namespace CR2W2JSON.Core.Parser { - public class StringIdVariantLengthsReportParser : IParser + public class StringIdVariantLengthsReportParser : AbstractParser { - class Entry + public override ISerializable GetData() { - [JsonInclude] - [JsonPropertyName("femaleLength")] - public float FemaleLength; - - [JsonInclude] - [JsonPropertyName("maleLength")] - public float MaleLength; - - [JsonInclude] - [JsonPropertyName("stringId")] - public string StringId; - } - - class EntriesArray - { - [JsonInclude] - [JsonPropertyName("entries")] - public List Entries; - } - - private readonly ICR2WExport _chunk; - - public StringIdVariantLengthsReportParser(ICR2WExport chunk) - { - _chunk = chunk; + return GetEntriesDictionary(GetReportEntries(Chunk.data.ChildrEditableVariables[0])); } - public object GetData() + private List> GetReportEntries(IEditableVariable evar) { - var output = new EntriesArray(); - - foreach (var v in _chunk.data.ChildrEditableVariables) - { - //"entries": - output.Entries = GetMetaData(v); - } - - return output; - } - - private List GetMetaData(IEditableVariable evar) - { - var metaList = new List(); + var metaList = new List>(); foreach (var sVariable in evar.ChildrEditableVariables) { - var obj = new Entry(); + var entry = new Dictionary(); foreach (var editableVariable in sVariable.ChildrEditableVariables) { - var rv = editableVariable.REDValue; - switch (editableVariable.REDName) - { - case "femaleLength": - obj.FemaleLength = float.Parse(rv); - break; - case "maleLength": - obj.MaleLength = float.Parse(rv); - break; - case "stringId": - obj.StringId = $"{ulong.Parse(rv):X}"; - break; - } + var redValue = editableVariable.REDValue; + var redName = editableVariable.REDName; + entry.Add(redName, redName == "stringId" ? $"{ulong.Parse(redValue):X}" : float.Parse(redValue)); + } - metaList.Add(obj); + metaList.Add(entry); } return metaList; } + + public StringIdVariantLengthsReportParser(ICR2WExport chunk) : base(chunk) {} } } diff --git a/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs b/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs index 7ab5a60..75b3f41 100644 --- a/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs +++ b/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs @@ -1,80 +1,63 @@ using System.Collections.Generic; -using System.Text.Json.Serialization; +using System.IO; +using System.Runtime.Serialization; +using System.Text.Json; using WolvenKit.Common.Model.Cr2w; +using WolvenKit.RED4.CR2W; namespace CR2W2JSON.Core.Parser { - public class SubtitlesMapParser : IParser + public class SubtitlesMapParser : AbstractParser { - public class Entry + public override ISerializable GetData() { - [JsonInclude] - [JsonPropertyName("subtitleFile")] - public string SubtitleFile; - - [JsonInclude] - [JsonPropertyName("subtitleGroup")] - public string SubtitleGroup; - } - - class EntriesArray - { - [JsonInclude] - [JsonPropertyName("entries")] - public List Entries; - } - - private readonly ICR2WExport _chunk; - private List entries; - - public SubtitlesMapParser(ICR2WExport chunk) - { - _chunk = chunk; + return GetEntriesDictionary(GetSubtitleEntries(Chunk.data.ChildrEditableVariables[0])); } - public object GetData() + public void ParseSubtitleFiles(DirectoryInfo input, DirectoryInfo output) { - var output = new EntriesArray(); + var entries = GetSubtitleEntries(Chunk.data.ChildrEditableVariables[0]); - foreach (var v in _chunk.data.ChildrEditableVariables) + foreach (var entry in entries) { - //"entries": - entries = GetMetaData(v); - output.Entries = entries; - } + var subFileName = (string) entry["subtitleFile"]; + var relPath = subFileName[subFileName.IndexOf(entry["subtitleGroup"])..]; + var subFile = new FileInfo(Path.Combine(input.FullName, relPath)); + var subOutFile = new FileInfo(Path.Combine(output.FullName, relPath)); + var subCr2w = new Cp77FileService().TryReadCr2WFile(File.OpenRead(subFile.FullName)); + var vcc = subCr2w.Chunks[0].VirtualChildrenChunks[0]; + + var json = new Json + { + RootType = vcc.REDType, + Data = new SubtitlesParser(vcc).GetData() + }; - return output; - } + Directory.CreateDirectory(subOutFile.DirectoryName); - public List GetEntries() - { - return entries; + File.WriteAllText(subOutFile.FullName,JsonSerializer.Serialize(json)); + } } - - private List GetMetaData(IEditableVariable evar) + + private List> GetSubtitleEntries(IEditableVariable evar) { - var metaList = new List(); + var entryList = new List>(); foreach (var sVariable in evar.ChildrEditableVariables) { - var obj = new Entry(); + var entry = new Dictionary(); foreach (var editableVariable in sVariable.ChildrEditableVariables) { - var rv = editableVariable.REDValue; - switch (editableVariable.REDName) - { - case "subtitleFile": - obj.SubtitleFile = rv.Replace("[Soft]", ""); - break; - case "subtitleGroup": - obj.SubtitleGroup = rv; - break; - } + var redValue = editableVariable.REDValue; + var redName = editableVariable.REDName; + entry.Add(redName, redName == "subtitleGroup" ? redValue : redValue.Replace("[Soft]", "")); } - metaList.Add(obj); + entryList.Add(entry); } - return metaList; + return entryList; } + + public SubtitlesMapParser(ICR2WExport chunk) : base(chunk) {} } } diff --git a/CR2W2JSON.Core/Parser/SubtitlesParser.cs b/CR2W2JSON.Core/Parser/SubtitlesParser.cs index fefe7ed..92a518b 100644 --- a/CR2W2JSON.Core/Parser/SubtitlesParser.cs +++ b/CR2W2JSON.Core/Parser/SubtitlesParser.cs @@ -1,80 +1,35 @@ using System.Collections.Generic; -using System.Text.Json.Serialization; +using System.Runtime.Serialization; using WolvenKit.Common.Model.Cr2w; namespace CR2W2JSON.Core.Parser { - public class SubtitlesParser : IParser + public class SubtitlesParser : AbstractParser { - class Entry + public override ISerializable GetData() { - [JsonInclude] - [JsonPropertyName("femaleVariant")] - public string FemaleVariant; - - [JsonInclude] - [JsonPropertyName("maleVariant")] - public string MaleVariant; - - [JsonInclude] - [JsonPropertyName("stringId")] - public string StringId; - } - - class EntriesArray - { - [JsonInclude] - [JsonPropertyName("entries")] - public List Entries; - } - - private readonly ICR2WExport _chunk; - - public SubtitlesParser(ICR2WExport chunk) - { - _chunk = chunk; + return GetEntriesDictionary(GetMetaData(Chunk.data.ChildrEditableVariables[0])); } - public object GetData() + private List> GetMetaData(IEditableVariable evar) { - var output = new EntriesArray(); - - foreach (var v in _chunk.data.ChildrEditableVariables) - { - //"entries": - output.Entries = GetMetaData(v); - } - - return output; - } - - private List GetMetaData(IEditableVariable evar) - { - var metaList = new List(); + var entries = new List>(); foreach (var sVariable in evar.ChildrEditableVariables) { - var obj = new Entry(); + var obj = new Dictionary(); foreach (var editableVariable in sVariable.ChildrEditableVariables) { - var rv = editableVariable.REDValue; - switch (editableVariable.REDName) - { - case "femaleVariant": - obj.FemaleVariant = rv != null ? rv : ""; - break; - case "maleVariant": - obj.MaleVariant = rv != null ? rv : ""; - break; - case "stringId": - obj.StringId = $"{ulong.Parse(rv):X}"; - break; - } + var redName = editableVariable.REDName; + var redValue = editableVariable.REDValue; + obj.Add(redName, redName == "stringId" ? $"{ulong.Parse(redValue):X}" : redValue ?? ""); } - metaList.Add(obj); + entries.Add(obj); } - return metaList; + return entries; } + + public SubtitlesParser(ICR2WExport chunk) : base(chunk) {} } } diff --git a/CR2W2JSON.Core/Parser/VoLanguageDataMapParser.cs b/CR2W2JSON.Core/Parser/VoLanguageDataMapParser.cs index af7f1be..8a7ab99 100644 --- a/CR2W2JSON.Core/Parser/VoLanguageDataMapParser.cs +++ b/CR2W2JSON.Core/Parser/VoLanguageDataMapParser.cs @@ -1,85 +1,42 @@ using System.Collections.Generic; +using System.Runtime.Serialization; using System.Text.Json.Serialization; using WolvenKit.Common.Model.Cr2w; namespace CR2W2JSON.Core.Parser { - public class VoLanguageDataMapParser : IParser + public class VoLanguageDataMapParser : AbstractParser { - class Entry + public override ISerializable GetData() { - [JsonInclude] - [JsonPropertyName("languageCode")] - public string LanguageCode; - - //there is a bug from REDs in naming: "lenghtMapReport" instead of "lengthMapReport" - [JsonInclude] - [JsonPropertyName("lenghtMapReport")] - public string LengthMapReport; - - //there is a bug from REDs in naming: "voiceverMapReport" instead of "voiceoverMapReport" - [JsonInclude] - [JsonPropertyName("voiceverMapReport")] - public string VoiceoverMapReport; - - [JsonInclude] - [JsonPropertyName("voMapChunks")] - public List VoMapChunks; - } - - class EntriesArray - { - [JsonInclude] - [JsonPropertyName("entries")] - public List Entries; - } - - private readonly ICR2WExport _chunk; - - public VoLanguageDataMapParser(ICR2WExport chunk) - { - _chunk = chunk; + return GetEntriesDictionary(GetMetaData(Chunk.data.ChildrEditableVariables[0])); } - public object GetData() + private List> GetMetaData(IEditableVariable evar) { - var output = new EntriesArray(); - - foreach (var v in _chunk.data.ChildrEditableVariables) - { - //"entries": - output.Entries = GetMetaData(v); - } - - return output; - } - - private List GetMetaData(IEditableVariable evar) - { - var metaList = new List(); + var metaList = new List>(); foreach (var sVariable in evar.ChildrEditableVariables) { - var obj = new Entry(); - obj.VoMapChunks = new List(); + var obj = new Dictionary(); + var voMapChunks = new List(); foreach (var editableVariable in sVariable.ChildrEditableVariables) { - var rv = editableVariable.REDValue; - switch (editableVariable.REDName) + var redValue = editableVariable.REDValue; + var redName = editableVariable.REDName; + switch (redName) { case "languageCode": - obj.LanguageCode = rv; + obj.Add(redName, redValue); break; case "lenghtMapReport": - obj.LengthMapReport = rv.Replace("[Soft]", "").Replace("[Default]", " "); - break; case "voiceverMapReport": - obj.VoiceoverMapReport = rv.Replace("[Default]", " ").Replace("[Soft]", ""); + obj.Add(redName, redValue.Replace("[Default]", " ").Replace("[Soft]", "")); break; case "voMapChunks": foreach (var entrs in editableVariable.ChildrEditableVariables) { - obj.VoMapChunks.Add(entrs.REDValue.Replace("[Soft]", "")); + voMapChunks.Add(entrs.REDValue.Replace("[Soft]", "")); } break; } @@ -89,5 +46,7 @@ private List GetMetaData(IEditableVariable evar) return metaList; } + + public VoLanguageDataMapParser(ICR2WExport chunk) : base(chunk) {} } } diff --git a/CR2W2JSON.Core/Parser/VoMapParser.cs b/CR2W2JSON.Core/Parser/VoMapParser.cs index 4d092e1..7fa58c0 100644 --- a/CR2W2JSON.Core/Parser/VoMapParser.cs +++ b/CR2W2JSON.Core/Parser/VoMapParser.cs @@ -1,80 +1,34 @@ using System.Collections.Generic; -using System.Text.Json.Serialization; +using System.Runtime.Serialization; using WolvenKit.Common.Model.Cr2w; namespace CR2W2JSON.Core.Parser { - public class VoMapParser : IParser + public class VoMapParser : AbstractParser { - class Entry - { - [JsonInclude] - [JsonPropertyName("femaleResPath")] - public string FemaleResPath; - - [JsonInclude] - [JsonPropertyName("maleResPath")] - public string MaleResPath; - - [JsonInclude] - [JsonPropertyName("stringId")] - public string StringId; - } - - class EntriesArray - { - [JsonInclude] - [JsonPropertyName("entries")] - public List Entries; - } - - private readonly ICR2WExport _chunk; + public override ISerializable GetData() => + GetEntriesDictionary(GetVoMapEntries(Chunk.data.ChildrEditableVariables[0])); - public VoMapParser(ICR2WExport chunk) + private List> GetVoMapEntries(IEditableVariable evar) { - _chunk = chunk; - } - - public object GetData() - { - var output = new EntriesArray(); - - foreach (var v in _chunk.data.ChildrEditableVariables) - { - //"entries": - output.Entries = GetMetaData(v); - } - - return output; - } - - private List GetMetaData(IEditableVariable evar) - { - var metaList = new List(); + var entries = new List>(); foreach (var sVariable in evar.ChildrEditableVariables) { - var obj = new Entry(); - foreach (var editableVariable in sVariable.ChildrEditableVariables) + var entry = new Dictionary(); + foreach (var ev in sVariable.ChildrEditableVariables) { - var rv = editableVariable.REDValue; - switch (editableVariable.REDName) - { - case "femaleResPath": - obj.FemaleResPath = rv.Replace("[Soft]", ""); - break; - case "maleResPath": - obj.MaleResPath = rv.Replace("[Soft]", ""); - break; - case "stringId": - obj.StringId = $"{ulong.Parse(rv):X}"; - break; - } + var name = ev.REDName; + var rv = ev.REDValue; + entry.Add(name, name == "stringId" ? $"{ulong.Parse(rv):X}" : rv.Replace("[Soft]", "")); + } - metaList.Add(obj); + entries.Add(entry); } - return metaList; + return entries; } + + public VoMapParser(ICR2WExport chunk) : base(chunk) {} } } diff --git a/CR2W2JSON.Core/Program.cs b/CR2W2JSON.Core/Program.cs index ebfee7b..73152be 100644 --- a/CR2W2JSON.Core/Program.cs +++ b/CR2W2JSON.Core/Program.cs @@ -1,5 +1,4 @@ -#nullable enable -using System; +using System; using System.CommandLine; using System.CommandLine.Invocation; using System.Diagnostics.CodeAnalysis; @@ -14,21 +13,16 @@ namespace CR2W2JSON.Core { - class Json + internal class Json { - [JsonInclude] - [JsonPropertyName("RootType")] - public string RootType; - - [JsonInclude] - [JsonPropertyName("Data")] - public object Data; + [JsonInclude] [JsonPropertyName("RootType")] public string RootType; + [JsonInclude] [JsonPropertyName("Data")] public object Data; } - - class Program + + internal static class Program { [SuppressMessage("ReSharper.DPA", "DPA0002: Excessive memory allocations in SOH", MessageId = "type: System.Reflection.CustomAttributeNamedParameter[]")] - static int Main(string[] args) + private static int Main(string[] args) { var rootCommand = new RootCommand { @@ -63,41 +57,19 @@ static int Main(string[] args) Data = parser.GetData() }; - //process subtitles from subtitles map file if (parser.GetType() == typeof(SubtitlesMapParser)) { - //iterate through all SubtitlesMap entries - foreach (var entrs in ((SubtitlesMapParser)parser).GetEntries()) + if (input.DirectoryName == output.DirectoryName) { - //define input subtitle file location - //define output subtitle file location - string sbtlRelPath = entrs.SubtitleFile.Substring(entrs.SubtitleFile.IndexOf(entrs.SubtitleGroup)); - FileInfo sbtlInPath = new FileInfo(input.DirectoryName + "\\" + sbtlRelPath); - FileInfo sbtlOutPath = new FileInfo(output.DirectoryName + "\\" + sbtlRelPath); - try - { - CR2W = s.TryReadCr2WFile(File.OpenRead(sbtlInPath.FullName)); - var subtitleVCChunks = CR2W.Chunks[0].VirtualChildrenChunks[0]; - SubtitlesParser sp = new SubtitlesParser(subtitleVCChunks); - var subtitleJson = new Json - { - RootType = subtitleVCChunks.REDType, - Data = sp.GetData() - }; - - if (!Directory.Exists(sbtlOutPath.DirectoryName)) - { - Directory.CreateDirectory(sbtlOutPath.DirectoryName); - } - File.WriteAllText(sbtlOutPath.FullName, JsonSerializer.Serialize(subtitleJson)); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - } + Console.WriteLine("To convert subtitles the output directory must be " + + "different from the input directory."); + Environment.Exit(2); } + + ((SubtitlesMapParser)parser). + ParseSubtitleFiles(input.Directory, output.Directory); } - + File.WriteAllText(output.FullName, JsonSerializer.Serialize(json)); } catch (Exception e) @@ -109,29 +81,20 @@ static int Main(string[] args) return rootCommand.InvokeAsync(args).Result; } - private static IParser? GetParserByType(string type, ICR2WExport chunk) + private static AbstractParser GetParserByType(string type, ICR2WExport chunk) { - switch (type) + return type switch { - case "audioAudioEventArray": - return new AudioEventArrayParser(chunk); - case "localizationPersistenceOnScreenEntries": - return new OnScreenParser(chunk); - case "localizationPersistenceLocDataMap": - return new LocDataMapParser(chunk); - case "locVoLanguageDataMap": - return new VoLanguageDataMapParser(chunk); - case "locVoiceoverMap": - return new VoMapParser(chunk); - case "locVoiceoverLengthMap": - return new StringIdVariantLengthsReportParser(chunk); - case "localizationPersistenceSubtitleEntries": - return new SubtitlesParser(chunk); - case "localizationPersistenceSubtitleMap": - return new SubtitlesMapParser(chunk); - } - - return null; + "audioAudioEventArray" => new AudioEventArrayParser(chunk), + "localizationPersistenceOnScreenEntries" => new OnScreenParser(chunk), + "localizationPersistenceLocDataMap" => new LocDataMapAbstractParser(chunk), + "locVoLanguageDataMap" => new VoLanguageDataMapParser(chunk), + "locVoiceoverMap" => new VoMapParser(chunk), + "locVoiceoverLengthMap" => new StringIdVariantLengthsReportParser(chunk), + "localizationPersistenceSubtitleEntries" => new SubtitlesParser(chunk), + "localizationPersistenceSubtitleMap" => new SubtitlesMapParser(chunk), + _ => null + }; } } } From 8d883f44b0a55d66f8c25bb1e3eec6bc1cbacba1 Mon Sep 17 00:00:00 2001 From: chidorishar <13747383+chidorishar@users.noreply.github.com> Date: Wed, 16 Jun 2021 22:39:38 +0300 Subject: [PATCH 4/4] Additional polishing and performance issues solving --- CR2W2JSON.Core/Parser/AbstractParser.cs | 2 +- CR2W2JSON.Core/Parser/SubtitlesMapParser.cs | 38 +++++++++++++-------- CR2W2JSON.Core/Parser/SubtitlesParser.cs | 2 ++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/CR2W2JSON.Core/Parser/AbstractParser.cs b/CR2W2JSON.Core/Parser/AbstractParser.cs index b1f2b4c..c593915 100644 --- a/CR2W2JSON.Core/Parser/AbstractParser.cs +++ b/CR2W2JSON.Core/Parser/AbstractParser.cs @@ -6,7 +6,7 @@ namespace CR2W2JSON.Core.Parser { public abstract class AbstractParser { - protected readonly ICR2WExport Chunk; + protected ICR2WExport Chunk; protected AbstractParser(ICR2WExport chunk) { diff --git a/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs b/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs index 75b3f41..d174927 100644 --- a/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs +++ b/CR2W2JSON.Core/Parser/SubtitlesMapParser.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; using System.Text.Json; @@ -17,25 +18,32 @@ public override ISerializable GetData() public void ParseSubtitleFiles(DirectoryInfo input, DirectoryInfo output) { var entries = GetSubtitleEntries(Chunk.data.ChildrEditableVariables[0]); + var cr2wReader = new Cp77FileService(); + var subtitleJson = new Json(); + var subtitlesParser = new SubtitlesParser(null); foreach (var entry in entries) { - var subFileName = (string) entry["subtitleFile"]; - var relPath = subFileName[subFileName.IndexOf(entry["subtitleGroup"])..]; - var subFile = new FileInfo(Path.Combine(input.FullName, relPath)); - var subOutFile = new FileInfo(Path.Combine(output.FullName, relPath)); - var subCr2w = new Cp77FileService().TryReadCr2WFile(File.OpenRead(subFile.FullName)); - var vcc = subCr2w.Chunks[0].VirtualChildrenChunks[0]; - - var json = new Json + try { - RootType = vcc.REDType, - Data = new SubtitlesParser(vcc).GetData() - }; + var subFileName = (string)entry["subtitleFile"]; + var relPath = subFileName[subFileName.IndexOf(entry["subtitleGroup"])..]; + var subFile = new FileInfo(Path.Combine(input.FullName, relPath)); + var subOutFile = new FileInfo(Path.Combine(output.FullName, relPath)); + var subCr2w = cr2wReader.TryReadCr2WFile(File.OpenRead(subFile.FullName)); + var vcc = subCr2w.Chunks[0].VirtualChildrenChunks[0]; - Directory.CreateDirectory(subOutFile.DirectoryName); + subtitlesParser.SetChunk(vcc); + subtitleJson.Data = subtitlesParser.GetData(); + subtitleJson.RootType = vcc.REDType; - File.WriteAllText(subOutFile.FullName,JsonSerializer.Serialize(json)); + Directory.CreateDirectory(subOutFile.DirectoryName); + + File.WriteAllText(subOutFile.FullName, JsonSerializer.Serialize(subtitleJson)); + } catch (Exception e) + { + Console.WriteLine(e.Message); + } } } @@ -57,7 +65,7 @@ private List> GetSubtitleEntries(IEditableVariable e return entryList; } - + public SubtitlesMapParser(ICR2WExport chunk) : base(chunk) {} } } diff --git a/CR2W2JSON.Core/Parser/SubtitlesParser.cs b/CR2W2JSON.Core/Parser/SubtitlesParser.cs index 92a518b..d9d59cc 100644 --- a/CR2W2JSON.Core/Parser/SubtitlesParser.cs +++ b/CR2W2JSON.Core/Parser/SubtitlesParser.cs @@ -30,6 +30,8 @@ private List> GetMetaData(IEditableVariable evar) return entries; } + public void SetChunk(ICR2WExport chunk) { Chunk = chunk; } + public SubtitlesParser(ICR2WExport chunk) : base(chunk) {} } }