diff --git a/Assets/Gothic-Core/Scripts/Domain/Npc/Actions/AnimationActions/OutputSvm.cs b/Assets/Gothic-Core/Scripts/Domain/Npc/Actions/AnimationActions/OutputSvm.cs index 9b6af5602..a94a63a55 100644 --- a/Assets/Gothic-Core/Scripts/Domain/Npc/Actions/AnimationActions/OutputSvm.cs +++ b/Assets/Gothic-Core/Scripts/Domain/Npc/Actions/AnimationActions/OutputSvm.cs @@ -25,8 +25,8 @@ public override void Start() key = key.Trim(); _preparedSvmFileName = svm?.GetAudioName(key); - // ZenKit's SvmInstance only knows G1's C_SVM members. Vanilla scripts name every entry SVM__, - // like the OU.BIN subtitle. + // A key ZenKit's SvmInstance doesn't know (a mod's own C_SVM member). Vanilla scripts name every entry + // SVM__, like the OU.BIN subtitle. if (_preparedSvmFileName == null && svm != null && !string.IsNullOrEmpty(key)) _preparedSvmFileName = $"SVM_{NpcInstance.Voice}_{key.TrimStart('$')}"; diff --git a/Assets/Gothic-Core/Scripts/Extensions/ZenKitExtension.cs b/Assets/Gothic-Core/Scripts/Extensions/ZenKitExtension.cs index 475a5a6a1..e7de044f3 100644 --- a/Assets/Gothic-Core/Scripts/Extensions/ZenKitExtension.cs +++ b/Assets/Gothic-Core/Scripts/Extensions/ZenKitExtension.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Reflection; using Gothic.Core.Const; using Gothic.Core.Logging; using Gothic.Core.Models.Container; @@ -355,8 +356,10 @@ public static int GetSymbolInt(this DaedalusVm vm, string symbolName, ushort ind } /// - /// Leveraging switch statements with string => member mapping as it's faster than reflection. - /// https://www.jacksondunstan.com/articles/2972 + /// The C_SVM member a script's SVM key ($MONSTERKILLED, "$Aargh_1") names, i.e. the line's audio/subtitle name. + /// The switch holds the keys whose names differ from ZenKit's property (G1's truncated keys, ZenKit typos), + /// every other key is found by name among all members ZenKit knows (G1 and G2, cached once). Switch first, + /// as it's faster than reflection: https://www.jacksondunstan.com/articles/2972 /// [SuppressMessage("ReSharper", "StringLiteralTypo")] public static string GetAudioName(this SvmInstance svm, string svmEntry) @@ -399,6 +402,8 @@ public static string GetAudioName(this SvmInstance svm, string svmEntry) "$handsoff" => svm.HandsOff, "$getoutofhere" => svm.GetOutOfHere, "$youviolatedforbiddenterritor" => svm.YouViolatedForbiddenTerritory, + // ZenKit's typos. + "$doesntwork" => svm.DoesntMork, "$youwannafoolme" => svm.YouWannaFoolMe, "$whatsthissupposedtobe" => svm.WhatsThisSupposedToBe, "$whyareyouinhere" => svm.WhyAreYouInHere, @@ -427,7 +432,7 @@ public static string GetAudioName(this SvmInstance svm, string svmEntry) "$nolearnnopoints" => svm.NoLearnNoPoints, "$nolearnovermax" => svm.NoLearnOverMax, "$nolearnyoualreadyknow" => svm.NoLearnYouAlreadyKnow, - "$nolearnyourebetter" => svm.NoLearnYouAlreadyKnow, + "$nolearnyourebetter" => svm.NoLearnYoureBetter, "$heyyou" => svm.HeyYou, "$notnow" => svm.NotNow, "$whatdoyouwant" => svm.WhatDoYouWant, @@ -497,15 +502,43 @@ public static string GetAudioName(this SvmInstance svm, string svmEntry) "$sc_heyturnaround02" => svm.ScHeyTurnAround02, "$sc_heyturnaround03" => svm.ScHeyTurnAround03, "$sc_heyturnaround04" => svm.ScHeyTurnAround04, - _ => null + _ => GetSvmMemberByName(svm, svmEntry) }; + // A key ZenKit doesn't know (a mod's own C_SVM member): OutputSvm falls back to SVM__. if (fileName == null) + Logger.LogWarningOnce(svmEntry, $"[Svm] Key {svmEntry} is no C_SVM member ZenKit knows.", LogCat.Dialog); + + return fileName; + } + + // Every string member of ZenKit's SvmInstance by its name without '_', lower case (SMALLTALK28 -> smalltalk28). + private static Dictionary _svmMembers; + + [CanBeNull] + private static string GetSvmMemberByName(SvmInstance svm, string svmEntry) + { + if (_svmMembers == null) { - Logger.LogError($"key {svmEntry} not (yet) implemented.", LogCat.Misc); + _svmMembers = new Dictionary(); + foreach (var property in typeof(SvmInstance).GetProperties(BindingFlags.Public | BindingFlags.Instance)) + { + if (property.PropertyType == typeof(string) && property.GetIndexParameters().Length == 0) + _svmMembers[NormalizeSvmName(property.Name)] = property; + } } - return fileName; + if (!_svmMembers.TryGetValue(NormalizeSvmName(svmEntry), out var member)) + return null; + + // A member the game's C_SVM doesn't have (a G2 key in G1) is empty. + var value = member.GetValue(svm) as string; + return string.IsNullOrEmpty(value) ? null : value; + } + + private static string NormalizeSvmName(string name) + { + return name.TrimStart('$').Replace("_", string.Empty).ToLowerInvariant(); } ///