-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
100 lines (84 loc) · 3.2 KB
/
Config.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
using Rocket.API;
using SDG.Unturned;
using Steamworks;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using static SMultiLangTranslations.Utils;
using UP = Rocket.Unturned.Player.UnturnedPlayer;
namespace SMultiLangTranslations
{
public class LangMapper
{
public List<LangMap> Mappings = new List<LangMap>();
/// <param name="code">Language-code</param>
/// <returns>All mappings with key that equals <paramref name="code" /></returns>
public IEnumerable<LangMap> this[string code] => Mappings.Where(x => x.Key.Compare(code));
/// <summary>
/// Adds new mapping
/// </summary>
public void Map(string key, string value) => Mappings.Add(new LangMap(key, value));
/// <summary>
/// Tries to get alternative lang-code for specified <paramref name="code" />
/// </summary>
/// <returns>Key of first mapping where Value equals <paramref name="code" /> or <see cref="Config.DefaultLangCode"/> from configuration else <see cref="Config.EnglishCode" /></returns>
public string Alternative(string code) => (
Mappings.FirstOrDefault(x => x.Value.Compare(code)) ??
new LangMap(conf?.DefaultLangCode ?? Config.EnglishCode, null)
).Key;
}
public class LangMap
{
[XmlAttribute]
public string Key, Value;
public LangMap() { }
public LangMap(string key, string value)
{
Key = key;
Value = value;
}
}
public class PlayerPreferences
{
[XmlAttribute("SteamID")]
public ulong steamID;
[XmlIgnore]
public CSteamID SteamID { get => new CSteamID(steamID); private set => steamID = value.m_SteamID; }
[XmlAttribute]
public string Language;
public PlayerPreferences() { }
public PlayerPreferences(CSteamID steamID, string language)
{
SteamID = steamID;
Language = language;
}
}
public class Config : IRocketPluginConfiguration
{
public string DefaultLangCode = EnglishCode;
public LangMapper Mappings = new LangMapper();
public List<PlayerPreferences> Preferences = new List<PlayerPreferences>();
public const string EnglishCode = "en";
public string GetLanguage(CSteamID SteamID)
{
string GetLang(CSteamID steamID) => (Preferences.FirstOrDefault(x => x.SteamID == SteamID) ?? new PlayerPreferences()).Language;
var p = PlayerTool.getPlayer(SteamID);
var lang = GetLang(SteamID);
lang = lang ?? p?.channel.owner.language ?? EnglishCode;
return lang;
}
public void SetLanguage(CSteamID SteamID, string language)
{
language = language.ToLowerInvariant();
var prefId = Preferences.FindIndex(x => x.SteamID == SteamID);
if (prefId < 0)
Preferences.Add(new PlayerPreferences(SteamID, language));
else Preferences[prefId].Language = language;
inst.Configuration.Save();
}
public void LoadDefaults()
{
Mappings.Map("ru", "ua");
}
}
}