CWX-mods/Live/CWX_VoiceAdder/VoicePatch.cs

48 lines
1.4 KiB
C#
Raw Normal View History

2022-05-14 19:53:00 +01:00
using Aki.Reflection.Patching;
using Aki.Reflection.Utils;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using System.IO;
namespace VoiceAdd
{
public class VoicePatch : ModulePatch
{
private static Type _targetType;
public VoicePatch()
{
_targetType = PatchConstants.EftTypes.Single(IsTargetType);
}
private bool IsTargetType(Type type)
{
return type.GetMethod("TakePhrasePath") != null;
}
protected override MethodBase GetTargetMethod()
{
return _targetType.GetMethod("TakePhrasePath");
}
[PatchPrefix]
private static void PatchPrefix()
{
string json = new StreamReader("./user/mods/StalkerVoices/voices.json").ReadToEnd();
Dictionary<string, string> voices = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
Dictionary<string, string> value = Traverse.Create(_targetType).Field<Dictionary<string, string>>("dictionary_0").Value;
foreach (var voicename in voices.Keys)
{
voices.TryGetValue(voicename, out var voicebundle);
if (!value.ContainsKey(voicename))
value.Add(voicename, voicebundle);
}
}
}
}