Skip to content

Commit 06c1155

Browse files
committed
Added basic font support
1 parent 9d0fce1 commit 06c1155

37 files changed

+680
-144
lines changed

DSPTranslationPlugin/DSPTranslationPlugin.csproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,35 @@
5151
<HintPath>D:\Games\SteamLibrary\steamapps\common\Dyson Sphere Program\DSPGAME_Data\Managed\UnityEngine.JSONSerializeModule.dll</HintPath>
5252
<Private>False</Private>
5353
</Reference>
54+
<Reference Include="UnityEngine.TextRenderingModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
55+
<HintPath>D:\Games\SteamLibrary\steamapps\common\Dyson Sphere Program\DSPGAME_Data\Managed\UnityEngine.TextRenderingModule.dll</HintPath>
56+
</Reference>
57+
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
58+
<HintPath>D:\Games\SteamLibrary\steamapps\common\Dyson Sphere Program\DSPGAME_Data\Managed\UnityEngine.UI.dll</HintPath>
59+
</Reference>
5460
</ItemGroup>
5561

5662
<ItemGroup>
5763
<ProjectReference Include="..\TranslationCommon\TranslationCommon.csproj" />
5864
</ItemGroup>
5965

66+
<ItemGroup>
67+
<Content Include="..\icon.png">
68+
<Link>icon.png</Link>
69+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
70+
</Content>
71+
<Content Include="..\LICENSE">
72+
<Link>LICENSE</Link>
73+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
74+
</Content>
75+
<Content Include="..\manifest.json">
76+
<Link>manifest.json</Link>
77+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
78+
</Content>
79+
<Content Include="..\README.md">
80+
<Link>README.md</Link>
81+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
82+
</Content>
83+
</ItemGroup>
84+
6085
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=resources/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using HarmonyLib;
2+
using TranslationCommon.Translation;
3+
4+
namespace DSPTranslationPlugin.GameHarmony
5+
{
6+
7+
[HarmonyPatch(typeof(GameOption), nameof(GameOption.Apply))]
8+
public static class GameOption_Apply_Harmony
9+
{
10+
/// <summary>
11+
/// Load current language after pressing "Apply" button
12+
/// </summary>
13+
[HarmonyPrefix]
14+
public static void Prefix(GameOption __instance)
15+
{
16+
TranslationManager.LoadCurrentLanguage();
17+
}
18+
}
19+
}

DSPTranslationPlugin/Harmony/GlobalObject_Initialize_Harmony.cs renamed to DSPTranslationPlugin/GameHarmony/GlobalObject_Initialize_Harmony.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System;
22
using HarmonyLib;
3-
using TranslationCommon;
3+
using TranslationCommon.Translation;
44
using UnityEngine;
55

6-
namespace DSPSimpleBuilding
6+
namespace DSPTranslationPlugin.GameHarmony
77
{
88
[HarmonyPatch(typeof(GlobalObject), "Initialize")]
99
public static class GlobalObject_Initialize_Harmony
1010
{
11+
/// <summary>
12+
/// Try settings previously saved language
13+
/// </summary>
1114
[HarmonyPrefix]
1215
public static void Prefix()
1316
{

DSPTranslationPlugin/Harmony/GlobalObject_SaveLastLanguage_Harmony.cs renamed to DSPTranslationPlugin/GameHarmony/GlobalObject_SaveLastLanguage_Harmony.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using HarmonyLib;
2-
using TranslationCommon;
2+
using TranslationCommon.Translation;
33
using UnityEngine;
44

5-
namespace DSPSimpleBuilding
5+
namespace DSPTranslationPlugin.GameHarmony
66
{
77
[HarmonyPatch(typeof(GlobalObject), nameof(GlobalObject.SaveLastLanguage))]
88
public static class GlobalObject_SaveLastLanguage_Harmony
99
{
10+
/// <summary>
11+
/// Save last used language
12+
/// </summary>
1013
[HarmonyPrefix]
1114
public static void Prefix()
1215
{

DSPTranslationPlugin/Harmony/Localizer_Refresh_Harmony.cs renamed to DSPTranslationPlugin/GameHarmony/Localizer_Refresh_Harmony.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using HarmonyLib;
22
using UnityEngine;
33

4-
namespace DSPSimpleBuilding
4+
namespace DSPTranslationPlugin.GameHarmony
55
{
66
/// <summary>
77
/// Localizer postfix for expanding translation box to match new credits text

DSPTranslationPlugin/Harmony/StringTranslate_Translate_Harmony.cs renamed to DSPTranslationPlugin/GameHarmony/StringTranslate_Translate_Harmony.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
22
using HarmonyLib;
3-
using TranslationCommon;
3+
using TranslationCommon.Translation;
44

5-
namespace DSPSimpleBuilding
5+
namespace DSPTranslationPlugin.GameHarmony
66
{
77
[HarmonyPatch(typeof(StringTranslate), nameof(StringTranslate.Translate), typeof(string))]
88
public static class StringTranslate_Translate_Harmony
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System.Collections.Generic;
2+
using System.Reflection;
3+
using DSPTranslationPlugin.UnityHarmony;
4+
using HarmonyLib;
5+
using TranslationCommon;
6+
using TranslationCommon.Translation;
7+
using UnityEngine;
8+
using UnityEngine.UI;
9+
10+
namespace DSPTranslationPlugin.GameHarmony
11+
{
12+
[HarmonyPatch(typeof(UIOptionWindow), "TempOptionToUI")]
13+
public static class UIOptionWindow_TempOptionToUI_Harmony
14+
{
15+
private static int? _originalUICount;
16+
private static UIComboBox _languageComboBox;
17+
private static UIComboBox _tipLevelComboBox;
18+
19+
private static string[] InGameFonts = new[]
20+
{
21+
"Default",
22+
};
23+
24+
[HarmonyPrefix]
25+
public static void Prefix(UIOptionWindow __instance)
26+
{
27+
if (_languageComboBox == null)
28+
{
29+
// ReSharper disable PossibleNullReferenceException
30+
_languageComboBox = (UIComboBox) __instance.GetType()
31+
.GetField("languageComp", BindingFlags.Instance | BindingFlags.NonPublic)
32+
.GetValue(__instance);
33+
_tipLevelComboBox = (UIComboBox) __instance.GetType()
34+
.GetField("tipLevelComp", BindingFlags.Instance | BindingFlags.NonPublic)
35+
.GetValue(__instance);
36+
// ReSharper restore PossibleNullReferenceException
37+
}
38+
39+
AddComboBoxLanguage(__instance);
40+
}
41+
42+
[HarmonyPostfix]
43+
public static void Postfix(UIOptionWindow __instance)
44+
{
45+
ApplyComboBoxLanguage(__instance);
46+
CreateFontCompoBox();
47+
}
48+
49+
/// <summary>
50+
/// Create option combobox with selection of font
51+
/// </summary>
52+
private static void CreateFontCompoBox()
53+
{
54+
var parent = _languageComboBox.transform.parent.parent;
55+
if (_originalUICount == null)
56+
{
57+
_originalUICount = parent.childCount;
58+
}
59+
60+
var genericComboBox = _tipLevelComboBox.transform.parent;
61+
62+
// Needs to initialize UI
63+
if (_originalUICount == parent.childCount)
64+
{
65+
var prevPosition = genericComboBox.GetComponent<RectTransform>();
66+
67+
foreach (var fontName in InGameFonts)
68+
{
69+
// Add new combobox
70+
var root = Object.Instantiate(genericComboBox,
71+
genericComboBox.parent);
72+
73+
root.gameObject.SetActive(true);
74+
Object.Destroy(root.GetComponent<Localizer>());
75+
root.GetComponent<Text>().text = $"Font - {fontName}";
76+
77+
var newComboBox = root.GetComponentInChildren<UIComboBox>();
78+
newComboBox.Items.Clear();
79+
newComboBox.Items.Add(fontName);
80+
foreach (var installedFont in TextFontManager.InstalledFonts)
81+
{
82+
newComboBox.Items.Add(installedFont);
83+
}
84+
85+
newComboBox.text = fontName;
86+
87+
var settingsIndex = TextFontManager.GetSettingIndex(fontName);
88+
ConsoleLogger.LogWarning("TextFontManager.GetSettingIndex " + settingsIndex);
89+
newComboBox.itemIndex = settingsIndex;
90+
91+
newComboBox.onItemIndexChange.AddListener(() =>
92+
{
93+
var selectedFontName = newComboBox.Items[newComboBox.itemIndex];
94+
if (selectedFontName == fontName)
95+
{
96+
TextFontManager.RestoreDefaultFont(fontName);
97+
}
98+
else
99+
{
100+
var font = Font.CreateDynamicFontFromOSFont(selectedFontName, 12);
101+
TextFontManager.ApplyCustomFont(fontName, font);
102+
}
103+
});
104+
105+
// Set Option position
106+
var rectTransform = root.GetComponent<RectTransform>();
107+
rectTransform.anchoredPosition =
108+
prevPosition.anchoredPosition - new Vector2(0, prevPosition.sizeDelta.y + 5);
109+
prevPosition = rectTransform;
110+
}
111+
}
112+
}
113+
114+
private static void AddComboBoxLanguage(UIOptionWindow __instance)
115+
{
116+
if (!_languageComboBox.Items.Contains("(Original) Française"))
117+
{
118+
_languageComboBox.Items.Add("(Original) Française");
119+
}
120+
121+
foreach (var langauge in TranslationManager.Langauges)
122+
{
123+
if (!_languageComboBox.Items.Contains(langauge.Settings.LanguageDisplayName))
124+
{
125+
_languageComboBox.Items.Add(langauge.Settings.LanguageDisplayName);
126+
}
127+
}
128+
}
129+
130+
private static void ApplyComboBoxLanguage(UIOptionWindow __instance)
131+
{
132+
if (TranslationManager.CurrentLanguage != null)
133+
{
134+
for (int i = 0; i < TranslationManager.Langauges.Count; i++)
135+
{
136+
if (TranslationManager.CurrentLanguage.Settings.LanguageDisplayName ==
137+
TranslationManager.Langauges[i].Settings.LanguageDisplayName)
138+
_languageComboBox.itemIndex = 3 + i;
139+
}
140+
}
141+
}
142+
}
143+
}

DSPTranslationPlugin/Harmony/UIOptionWindow_UIToTempOption_Harmony.cs renamed to DSPTranslationPlugin/GameHarmony/UIOptionWindow_UIToTempOption_Harmony.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Reflection;
22
using HarmonyLib;
3-
using TranslationCommon;
3+
using TranslationCommon.Translation;
44

5-
namespace DSPSimpleBuilding
5+
namespace DSPTranslationPlugin.GameHarmony
66
{
77
[HarmonyPatch(typeof(UIOptionWindow), "UIToTempOption")]
88
public static class UIOptionWindow_UIToTempOption_Harmony

DSPTranslationPlugin/Harmony/VFPreload_PreloadThread_Harmony.cs renamed to DSPTranslationPlugin/GameHarmony/VFPreload_PreloadThread_Harmony.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
1+
using System.IO;
52
using System.Reflection;
6-
using BepInEx;
73
using HarmonyLib;
8-
using Mono.Cecil;
9-
using Mono.Cecil.Cil;
10-
using SimpleJSON;
114
using TranslationCommon;
12-
using UnityEngine;
13-
using OpCodes = System.Reflection.Emit.OpCodes;
5+
using TranslationCommon.SimpleJSON;
6+
using TranslationCommon.Translation;
147

15-
namespace DSPSimpleBuilding
8+
namespace DSPTranslationPlugin.GameHarmony
169
{
17-
[BepInPlugin("com.muchaszewski.dsp_translationPlugin", "DSP Community Translation", "0.1.0.0")]
18-
public class TranslationPlugin : BaseUnityPlugin
19-
{
20-
private void Awake()
21-
{
22-
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
23-
}
24-
}
25-
2610
[HarmonyPatch(typeof(VFPreload), "PreloadThread")]
2711
public class VFPreload_PreloadThread
2812
{

0 commit comments

Comments
 (0)