-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMod.cs
More file actions
178 lines (146 loc) · 7.08 KB
/
Mod.cs
File metadata and controls
178 lines (146 loc) · 7.08 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using Colossal;
using Colossal.Localization;
using Colossal.Logging;
using Game;
using Game.Modding;
using Game.SceneFlow;
using Game.Settings;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace EuropeanPortugueseLocale
{
public class Mod : IMod
{
public static ILog log = LogManager.GetLogger($"{nameof(EuropeanPortugueseLocale)}.{nameof(Mod)}").SetShowsErrorsInUI(false);
public void OnLoad(UpdateSystem updateSystem)
{
log.Info("=== European Portuguese Locale Mod Loading ===");
if (GameManager.instance.modManager.TryGetExecutableAsset(this, out var asset))
{
TryAddVanillaSource(Path.GetDirectoryName(asset.path));
}
GameManager.instance.localizationManager.LoadAvailableLocales();
typeof(InterfaceSettings).GetMethod("RegisterInOptionsUI", BindingFlags.Instance | BindingFlags.NonPublic,
null, new Type[] { typeof(string), typeof(bool) }, null)?
.Invoke(GameManager.instance.settings.userInterface, new object[] { "Interface", false });
log.Info("Mod loaded successfully");
}
private void TryAddVanillaSource(string modDir)
{
try
{
var vanillaJson = Path.Combine(modDir, "Localization", "pt-PT", "Vanilla", "pt-PT.json");
if (!File.Exists(vanillaJson))
{
log.Warn($"Vanilla JSON nao encontrado em: {vanillaJson}");
return;
}
var allEntries = JsonConvert.DeserializeObject<Dictionary<string, string>>(
File.ReadAllText(vanillaJson));
if (allEntries == null)
{
log.Warn("Falha ao desserializar Vanilla/pt-PT.json");
return;
}
var indexCounts = new Dictionary<string, int>();
foreach (var kv in allEntries)
{
var colonIdx = kv.Key.LastIndexOf(':');
if (colonIdx > 0 && int.TryParse(kv.Key.Substring(colonIdx + 1), out int idx))
{
var baseKey = kv.Key.Substring(0, colonIdx);
if (!indexCounts.ContainsKey(baseKey) || indexCounts[baseKey] <= idx)
indexCounts[baseKey] = idx + 1;
}
}
GameManager.instance.localizationManager.AddSource("pt-PT",
new VanillaLocaleSource(allEntries, indexCounts));
log.Info($"Adicionadas {allEntries.Count} entradas, {indexCounts.Count} categorias indexadas");
// UserInterface.ctor caches the hint list before our mod loads — fix it via reflection.
TryRefreshLoadingHints();
}
catch (Exception e)
{
log.Warn($"Nao foi possivel adicionar fonte vanilla: {e.Message}");
}
}
private void TryRefreshLoadingHints()
{
try
{
// Get UserInterface from GameManager
var uiProp = typeof(GameManager).GetProperty("userInterface",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var ui = uiProp?.GetValue(GameManager.instance);
if (ui == null) return;
// m_HintMessages lives inside OverlayBindings (one level below UserInterface)
object hintOwner = null;
FieldInfo hintField = null;
foreach (var topField in ui.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
var topVal = topField.GetValue(ui);
if (topVal == null) continue;
foreach (var sub in topVal.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
if (sub.Name == "m_HintMessages")
{
hintOwner = topVal;
hintField = sub;
break;
}
}
if (hintField != null) break;
}
if (hintField == null) { log.Warn("m_HintMessages nao encontrado"); return; }
// Get hint IDs from the active localization dictionary
var locMgr = GameManager.instance.localizationManager;
var dictProp = locMgr.GetType().GetProperty("activeDictionary",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var dict = dictProp?.GetValue(locMgr);
if (dict == null) return;
var getIdsMethod = dict.GetType().GetMethod("GetIndexedLocaleIDs",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (getIdsMethod == null) return;
var hintIdsList = getIdsMethod.Invoke(dict, new object[] { "Loading.HINTMESSAGE" })
as IList<string>;
if (hintIdsList == null) return;
var hintIdsArray = new string[hintIdsList.Count];
hintIdsList.CopyTo(hintIdsArray, 0);
// m_HintMessages is a ValueBinding<string[]> — call Update(string[])
var bindingObj = hintField.GetValue(hintOwner);
var updateMethod = bindingObj?.GetType().GetMethod("Update",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null, new Type[] { typeof(string[]) }, null);
updateMethod?.Invoke(bindingObj, new object[] { hintIdsArray });
log.Info($"Loading hints actualizados: {hintIdsArray.Length} entradas");
}
catch (Exception e)
{
log.Warn($"TryRefreshLoadingHints falhou: {e.Message}");
}
}
public void OnDispose() { }
private class VanillaLocaleSource : IDictionarySource
{
private readonly Dictionary<string, string> _entries;
private readonly Dictionary<string, int> _indexCounts;
public VanillaLocaleSource(Dictionary<string, string> entries, Dictionary<string, int> indexCounts)
{
_entries = entries;
_indexCounts = indexCounts;
}
public IEnumerable<KeyValuePair<string, string>> ReadEntries(
IList<IDictionaryEntryError> errors,
Dictionary<string, int> indexCounts)
{
foreach (var kv in _indexCounts)
indexCounts[kv.Key] = kv.Value;
return _entries;
}
public void Unload() { }
}
}
}