-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextChange.cs
More file actions
155 lines (134 loc) · 5.67 KB
/
TextChange.cs
File metadata and controls
155 lines (134 loc) · 5.67 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Collections.Generic;
using System.Text;
using HarmonyLib;
namespace SkillTrackerColoring
{
/// <summary>
/// WidgetTracker.Refreshメソッドへのパッチ
/// スキル名を主属性に応じた色でハイライトする
/// </summary>
[HarmonyPatch(typeof(WidgetTracker))]
[HarmonyPatch("Refresh")]
internal class SkillTrackerColorPatch
{
/// <summary>
/// スキル名からHex色文字列へのキャッシュ
/// </summary>
private static Dictionary<string, string> _elementNameToColorHex;
/// <summary>
/// キャッシュ構築済みフラグ
/// </summary>
private static bool _cacheBuilt;
/// <summary>
/// 設定から属性エイリアスとHex色文字列の対応を取得する
/// </summary>
private static Dictionary<string, string> GetAttributeColorHex()
{
return new Dictionary<string, string>
{
{ "STR", Plugin.GetColorHex(Plugin.ColorConfig.StrColor) },
{ "END", Plugin.GetColorHex(Plugin.ColorConfig.EndColor) },
{ "DEX", Plugin.GetColorHex(Plugin.ColorConfig.DexColor) },
{ "PER", Plugin.GetColorHex(Plugin.ColorConfig.PerColor) },
{ "LER", Plugin.GetColorHex(Plugin.ColorConfig.LerColor) },
{ "WIL", Plugin.GetColorHex(Plugin.ColorConfig.WilColor) },
{ "MAG", Plugin.GetColorHex(Plugin.ColorConfig.MagColor) },
{ "CHA", Plugin.GetColorHex(Plugin.ColorConfig.ChaColor) },
};
}
/// <summary>
/// WidgetTracker.Refresh実行後に呼び出されるパッチ
/// テキストの各行をスキルの主属性に応じた色で装飾する
/// </summary>
/// <param name="__instance">パッチ対象のWidgetTrackerインスタンス</param>
static void Postfix(WidgetTracker __instance)
{
try
{
BuildElementCache();
var text = __instance.text;
if (text == null || string.IsNullOrEmpty(text.text)) return;
var lines = text.text.Split('\n');
var sb = new StringBuilder();
for (int i = 0; i < lines.Length; i++)
{
if (i > 0) sb.Append('\n');
var line = lines[i];
var colorHex = GetColorHexForLine(line);
if (colorHex != null)
{
sb.Append("<color=#").Append(colorHex).Append('>').Append(line).Append("</color>");
}
else
{
sb.Append(line);
}
}
text.text = sb.ToString();
}
catch (System.Exception ex)
{
Plugin.Logger.LogError("Error in SkillTrackerColorPatch.Postfix: " + ex);
}
}
/// <summary>
/// ゲームのSourceElementデータからスキル名と色の対応キャッシュを構築する
/// aliasParentフィールドを使用して各スキルの親属性を判定する
/// </summary>
private static void BuildElementCache()
{
if (_cacheBuilt) return;
try
{
if (EClass.sources?.elements?.rows == null)
{
Plugin.Logger.LogWarning("EClass.sources.elements.rows is null");
return;
}
var attributeColorHex = GetAttributeColorHex();
_elementNameToColorHex = new Dictionary<string, string>();
foreach (var row in EClass.sources.elements.rows)
{
if (row == null) continue;
var elementName = row.GetName();
if (string.IsNullOrEmpty(elementName)) continue;
var alias = row.alias;
var aliasParent = row.aliasParent;
// 主属性自体の場合
if (!string.IsNullOrEmpty(alias) && attributeColorHex.TryGetValue(alias, out var colorHex))
{
_elementNameToColorHex[elementName] = colorHex;
continue;
}
// スキルの場合、親属性を確認
if (!string.IsNullOrEmpty(aliasParent) && attributeColorHex.TryGetValue(aliasParent, out colorHex))
{
_elementNameToColorHex[elementName] = colorHex;
}
}
_cacheBuilt = true;
}
catch (System.Exception ex)
{
Plugin.Logger.LogError("Error building element cache: " + ex);
}
}
/// <summary>
/// 指定された行に含まれるスキル名から対応するHex色文字列を取得する
/// </summary>
/// <param name="line">検索対象の行</param>
/// <returns>対応するHex色文字列、見つからない場合はnull</returns>
private static string GetColorHexForLine(string line)
{
if (_elementNameToColorHex == null) return null;
foreach (var kvp in _elementNameToColorHex)
{
if (line.Contains(kvp.Key))
{
return kvp.Value;
}
}
return null;
}
}
}