-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
91 lines (76 loc) · 3.01 KB
/
Copy pathPlugin.cs
File metadata and controls
91 lines (76 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
namespace TunicTranslation;
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInProcess("Tunic.exe")]
public class Plugin : BasePlugin
{
internal static new ManualLogSource Log;
public override void Load()
{
Log = base.Log;
Log.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");
Harmony.CreateAndPatchAll(typeof(Plugin));
Log.LogInfo($"Patches executed");
}
[HarmonyPrefix, HarmonyPatch(typeof(NPCDialogue), nameof(NPCDialogue.DisplayDialogue))]
public static void NpcDialogueDisplayPreHook(LanguageLine script, bool pauseTime)
{
Log.LogInfo($"NPC dialogue: {script.text}");
}
[HarmonyPostfix, HarmonyPatch(typeof(Parser), nameof(Parser.findSymbol))]
// ReSharper disable once InconsistentNaming, BepInEx expects double underscore
public static void FindSymbolPostHook(string s, ref int __result)
{
// ignore special symbols (such as [sword]) and localizable strings (such as "npc_gen_moonlight")
// localizable strings probably don't appear here, but it doesn't hurt to have the check
if (s.StartsWith('[') || s.StartsWith('"'))
return;
// deny any multi-character symbols. Tunic will try to resolve sequences such as "oo" to a symbol, but we
// currently we only support mapping 1 character to 1 symbol. If a symbol is not mapped (ie. return value is
// -1), it will try individual characters which we force here.
if (s.Length != 1)
{
__result = -1;
return;
}
// map a character such as `A` or `$` to a symbol index and overwrite return value
var resolved = ResolveChar(ReplacePhonetic(s[0]));
if (resolved is { } r)
__result = r;
else
Log.LogWarning($"Unable to resolve char: {s[0]}");
}
private const int AlphabetOffset = 42;
/// <summary>
/// The standard character part of the symbol set. This set is preceded by the original symbols and succeeded by
/// various symbols.
/// </summary>
private const string Alphabet = " •,?!^()/\"\":0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.";
/// <summary> Replace special phonetic characters with the closest one in our alphabet </summary>
private static char ReplacePhonetic(char c)
{
return c switch
{
'$' => 's', // sh, as in "$oud" (should), "wi$iz" (wishes)
'%' => 't', // th, as in "%i^k" (think)
'#' => 'd', // th, as in "$uh" (the)
'^' => 'n', // ng, as in "surprIzi^" (surprising), "%l^" (thing)
// NOTE: would want this to be a dash, but no such symbol exists
'~' => '/', // small pause, used in uh~gehn (again), uh~gO (ago), uh~lahs (alas)
_ => c
};
}
/// <summary> Find a character in <see cref="Alphabet"/>, and return the symbol index if found. </summary>
private static int? ResolveChar(char c)
{
var index = Alphabet.IndexOf(c);
if (index == -1)
index = Alphabet.ToLowerInvariant().IndexOf(c);
if (index == -1)
return null;
return AlphabetOffset + index;
}
}