-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLocalization.cs
121 lines (106 loc) · 4.1 KB
/
Localization.cs
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
using Stunlock.Core;
using Stunlock.Localization;
using System.Reflection;
using System.Text.Json;
namespace Eclipse;
internal class Localization
{
struct Code
{
public string Key { get; set; }
public string Value { get; set; }
public string Description { get; set; }
}
struct Node
{
public string Guid { get; set; }
public string Text { get; set; }
}
struct Words
{
public string Original { get; set; }
public string Translation { get; set; }
}
struct LocalizationFile
{
public Code[] Codes { get; set; }
public Node[] Nodes { get; set; }
public Words[] Words { get; set; }
}
internal class LocalKey
{
public string Key { get; set; }
}
//static readonly Dictionary<PrefabGUID, LocalizationKey> LocalizationKeyMap = [];
static readonly Dictionary<string, string> GuidStringsToLocalizedNames = []; // 1 and 2, need 2 to be keys in other dictionary back to the guid string 1 then back to the prefab int
static readonly Dictionary<int, string> PrefabHashesToGuidStrings = [];
static readonly Dictionary<string, string> LocalizedNamesToGuidStrings = [];
static readonly Dictionary<string, int> GuidStringsToPrefabHashes = [];
public Localization()
{
LoadLocalizations();
LoadPrefabNames();
}
static void LoadLocalizations()
{
var resourceName = "Eclipse.Localization.English.json";
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(resourceName);
using StreamReader localizationReader = new(stream);
string jsonContent = localizationReader.ReadToEnd();
var localizationFile = JsonSerializer.Deserialize<LocalizationFile>(jsonContent);
var nodesDict = localizationFile.Nodes
.ToDictionary(x => x.Guid, x => x.Text);
//.ForEach(kvp => GuidStringsToLocalizedNames[kvp.Key] = kvp.Value);
nodesDict.ForEach(kvp => GuidStringsToLocalizedNames[kvp.Key] = kvp.Value);
nodesDict.ForEach(kvp => LocalizedNamesToGuidStrings[kvp.Value] = kvp.Key);
}
static void LoadPrefabNames()
{
var resourceName = "Eclipse.Localization.Prefabs.json";
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(resourceName);
using StreamReader reader = new(stream);
string jsonContent = reader.ReadToEnd();
var prefabNames = JsonSerializer.Deserialize<Dictionary<int, string>>(jsonContent);
prefabNames.ForEach(kvp => PrefabHashesToGuidStrings[kvp.Key] = kvp.Value);
prefabNames.ForEach(kvp => GuidStringsToPrefabHashes[kvp.Value] = kvp.Key);
}
static string GetLocalizationFromKey(LocalizationKey key)
{
var guid = key.Key.ToGuid().ToString();
return GetLocalization(guid);
}
public static PrefabGUID GetPrefabGUIDFromLocalizedName(string name)
{
if (LocalizedNamesToGuidStrings.TryGetValue(name, out string guidString) && GuidStringsToPrefabHashes.TryGetValue(guidString, out int prefabHash))
{
return new(prefabHash);
}
return PrefabGUID.Empty;
}
public static string GetLocalizedPrefabName(PrefabGUID prefabGUID)
{
if (PrefabHashesToGuidStrings.TryGetValue(prefabGUID.GuidHash, out var itemLocalizationHash))
{
return GetLocalization(itemLocalizationHash);
}
return prefabGUID.GetPrefabName();
}
public static string GetLocalization(string Guid)
{
if (GuidStringsToLocalizedNames.TryGetValue(Guid, out var Text))
{
return Text;
}
return "Couldn't find localizationKey!";
}
public static LocalizationKey GetLocalizationKeyFromPrefabGUID(PrefabGUID prefabGUID)
{
if (PrefabHashesToGuidStrings.TryGetValue(prefabGUID.GuidHash, out var itemLocalizationHash))
{
return new LocalizationKey { Key = AssetGuid.FromString(itemLocalizationHash) };
}
return LocalizationKey.Empty; // Or handle appropriately
}
}