-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
186 lines (171 loc) · 7.4 KB
/
Plugin.cs
File metadata and controls
186 lines (171 loc) · 7.4 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
179
180
181
182
183
184
185
186
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
namespace OpenLocalization
{
[BepInPlugin(pluginGUID, pluginName, pluginVersion)]
[BepInProcess("Pharaoh.exe")]
public class OpenLocalization : BaseUnityPlugin
{
public const string pluginGUID = "com.github.AgainPsychoX.PANE.OpenLocalization";
public const string pluginName = "OpenLocalization";
public const string pluginVersion = "0.2.0.0";
private static OpenLocalization _instance;
public static OpenLocalization GetInstance()
{
return _instance;
}
public static ManualLogSource GetLogger()
{
return _instance.Logger;
}
void Awake()
{
_instance = this;
PrepareConfigEntries();
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), pluginGUID);
ReloadLocalizations();
Logger.LogInfo($"Plugin {this.Info.Metadata.Name} is loaded!");
}
public MyConfig ConfigEntries;
public static MyConfig GetConfig()
{
return _instance.ConfigEntries;
}
public class MyConfig
{
internal ConfigEntry<string> LocalizationsDirectoryRaw;
public ConfigEntry<bool> SkipAssets;
public string LocalizationsDirectory
{
get {
return Path.GetFullPath(LocalizationsDirectoryRaw.Value);
}
set {
LocalizationsDirectoryRaw.Value = PathUtils.GetPathPossiblyRelativeToGameRoot(value);
}
}
}
protected void PrepareConfigEntries()
{
ConfigEntries = new MyConfig();
ConfigEntries.LocalizationsDirectoryRaw = Config.Bind("General", "LocalizationsDirectory", PathUtils.GetPathPossiblyRelativeToGameRoot(DefaultLocalizationsDirectory),
new ConfigDescription("Directory path used for storing and loading the localizations files."));
ConfigEntries.SkipAssets = Config.Bind("General", "SkipAssets", true,
new ConfigDescription("Skips loading the built-in localizations from assets. If no localization files are found, the assets will be converted to the files then used."));
ConfigEntries.SkipAssets.SettingChanged += SkipAssetsSettingChanged;
Config.Bind("General", "_Reload", false,
new ConfigDescription("Button to reload the localizations", null,
new ConfigurationManagerAttributes
{
HideSettingName = true,
HideDefaultButton = true,
ReadOnly = true,
CustomDrawer = (ConfigEntryBase entry) => {
if (GUILayout.Button("Reload the localizations", GUILayout.ExpandWidth(true)))
{
ReloadLocalizations();
entry.BoxedValue = false;
}
},
}
)
);
Config.Bind("General", "_Update", false,
new ConfigDescription("Button to update the localizations from online spreadsheets.", null,
new ConfigurationManagerAttributes
{
HideSettingName = true,
HideDefaultButton = true,
ReadOnly = true,
CustomDrawer = (ConfigEntryBase entry) => {
if (GUILayout.Button("Update the localizations from online", GUILayout.ExpandWidth(true)))
{
UpdateLocalizations();
entry.BoxedValue = false;
}
},
}
)
);
Config.Bind("Information", "_Info", "",
new ConfigDescription("", null,
new ConfigurationManagerAttributes
{
HideSettingName = true,
HideDefaultButton = true,
ReadOnly = true,
CustomDrawer = (ConfigEntryBase entry) =>
{
GUILayout.Label(
String.Format("Built-in languages: \n{0}",
String.Join("\n", BuiltInLocalizationSource.Data.GetLanguages().ConvertAll(x => "+ " + x))),
GUILayout.ExpandWidth(true)
);
GUILayout.Label(
String.Format("All languages: \n{0}",
String.Join("\n", I2.Loc.LocalizationManager.GetAllLanguages().ConvertAll(x => "+ " + x))),
GUILayout.ExpandWidth(true)
);
GUILayout.BeginVertical();
GUILayout.Label("Mod written by AgainPsychoX#4444.\nSpecial thanks to Danie!#9942 and Takia_Gecko#1037.", GUILayout.ExpandWidth(true));
if (GUILayout.Button("Join our Discord!", GUILayout.ExpandWidth(true)))
{
System.Diagnostics.Process.Start("https://discord.gg/n5phrj222e");
}
GUILayout.EndVertical();
},
}
)
);
}
private void SkipAssetsSettingChanged(object sender, EventArgs e)
{
if (ConfigEntries.SkipAssets.Value)
{
BuiltInLocalizationSource.Load();
}
else
{
BuiltInLocalizationSource.SourceData = LocalizationSource.GetBuiltInLanguageSourceFromAssets();
}
}
static public string DefaultLocalizationsDirectory
{
get {
return Path.Combine(BepInEx.Paths.GameRootPath, "Localizations");
}
}
public List<LocalizationSource> LocalizationSources { get; protected set; }
public LocalizationSource BuiltInLocalizationSource { get; protected set; }
protected void ReloadLocalizations()
{
Logger.LogInfo("Reloading...");
LocalizationSources?.Clear();
LocalizationSources = LocalizationSource.LoadAll(GetConfig().LocalizationsDirectory).OrderBy(s => s.Order).ToList();
BuiltInLocalizationSource = LocalizationSources.Find(s => s.Name == LocalizationSource.builtInSourceName);
if (ConfigEntries.SkipAssets.Value)
{
BuiltInLocalizationSource.SourceData = LocalizationSource.GetBuiltInLanguageSourceFromAssets();
}
LocalizationManagerPatches.ReloadSources();
Logger.LogInfo("Reloaded!");
}
protected void UpdateLocalizations()
{
Logger.LogInfo("Updating...");
foreach (var source in LocalizationSources)
{
source.Update();
}
Logger.LogInfo("Updated!");
}
}
}