-
Notifications
You must be signed in to change notification settings - Fork 7
/
FluentFunctionProvider.cs
183 lines (158 loc) · 6.92 KB
/
FluentFunctionProvider.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
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
using StardewModdingAPI;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Shockah.ProjectFluent
{
internal interface IFluentFunctionProvider
{
IEnumerable<(IManifest mod, string name, FluentFunction function)> GetFluentFunctions();
}
internal class SerialFluentFunctionProvider : IFluentFunctionProvider
{
private IFluentFunctionProvider[] Providers { get; set; }
public SerialFluentFunctionProvider(params IFluentFunctionProvider[] providers)
{
// making a copy on purpose
this.Providers = providers.ToArray();
}
public IEnumerable<(IManifest mod, string name, FluentFunction function)> GetFluentFunctions()
{
foreach (var provider in Providers)
foreach (var function in provider.GetFluentFunctions())
yield return function;
}
}
internal class BuiltInFluentFunctionProvider : IFluentFunctionProvider
{
private IManifest ProjectFluentMod { get; set; }
private IModRegistry ModRegistry { get; set; }
private IFluentValueFactory FluentValueFactory { get; set; }
private IModTranslationsProvider ModTranslationsProvider { get; set; }
internal IFluentProvider FluentProvider { get; set; } = null!;
private IDictionary<(IGameLocale locale, string mod, string? name), IFluent<string>> FluentCache { get; set; } = new Dictionary<(IGameLocale locale, string mod, string? name), IFluent<string>>();
public BuiltInFluentFunctionProvider(IManifest projectFluentMod, IModRegistry modRegistry, IFluentValueFactory fluentValueFactory, IModTranslationsProvider modTranslationsProvider)
{
this.ProjectFluentMod = projectFluentMod;
this.ModRegistry = modRegistry;
this.FluentValueFactory = fluentValueFactory;
this.ModTranslationsProvider = modTranslationsProvider;
}
public IEnumerable<(IManifest mod, string name, FluentFunction function)> GetFluentFunctions()
{
yield return (ProjectFluentMod, "MOD_NAME", ModNameFunction);
yield return (ProjectFluentMod, "FLUENT", FluentFunction);
yield return (ProjectFluentMod, "I18N", I18nFunction);
yield return (ProjectFluentMod, "TOUPPER", ToUpperFunction);
yield return (ProjectFluentMod, "TOLOWER", ToLowerFunction);
yield return (ProjectFluentMod, "CAPITALIZE_WORDS", CapitalizeWordsFunction);
}
private IFluentFunctionValue ModNameFunction(
IGameLocale locale,
IManifest mod,
IReadOnlyList<IFluentFunctionValue> positionalArguments,
IReadOnlyDictionary<string, IFluentFunctionValue> namedArguments)
{
var modID = mod.UniqueID;
if (positionalArguments.Count >= 1)
modID = positionalArguments[0].AsString();
var otherMod = ModRegistry.Get(modID);
return FluentValueFactory.CreateStringValue(otherMod?.Manifest.Name ?? modID);
}
private IFluentFunctionValue FluentFunction(
IGameLocale locale,
IManifest mod,
IReadOnlyList<IFluentFunctionValue> positionalArguments,
IReadOnlyDictionary<string, IFluentFunctionValue> namedArguments)
{
if (positionalArguments.Count == 0)
throw new ArgumentException("Missing `Key` positional argument.");
string targetKey = positionalArguments[0].AsString();
string targetFluentMod = mod.UniqueID;
string? targetFluentFile = null;
var remainingNamedArguments = new Dictionary<string, IFluentFunctionValue>(namedArguments);
if (remainingNamedArguments.TryGetValue("mod", out var modArg))
{
targetFluentMod = modArg.AsString();
remainingNamedArguments.Remove("mod");
}
if (remainingNamedArguments.TryGetValue("file", out var fileArg))
{
targetFluentFile = fileArg.AsString();
remainingNamedArguments.Remove("file");
}
if (!FluentCache.TryGetValue((locale, targetFluentMod, targetFluentFile), out var fluent))
{
var otherMod = ModRegistry.Get(targetFluentMod);
if (otherMod is null)
throw new ArgumentException($"Mod `{targetFluentMod}` is not installed.");
fluent = FluentProvider.GetFluent(locale, otherMod.Manifest, targetFluentFile);
FluentCache[(locale, targetFluentMod, targetFluentFile)] = fluent;
}
var remainingStringNamedArguments = new Dictionary<string, string>();
foreach (var (key, arg) in remainingNamedArguments)
remainingStringNamedArguments[key] = arg.AsString();
return FluentValueFactory.CreateStringValue(fluent.Get(targetKey, remainingStringNamedArguments));
}
private IFluentFunctionValue I18nFunction(
IGameLocale locale,
IManifest mod,
IReadOnlyList<IFluentFunctionValue> positionalArguments,
IReadOnlyDictionary<string, IFluentFunctionValue> namedArguments)
{
if (positionalArguments.Count == 0)
throw new ArgumentException("Missing `Key` positional argument.");
string targetKey = positionalArguments[0].AsString();
string targetI18nMod = mod.UniqueID;
var remainingNamedArguments = new Dictionary<string, IFluentFunctionValue>(namedArguments);
if (remainingNamedArguments.TryGetValue("mod", out var modArg))
{
targetI18nMod = modArg.AsString();
remainingNamedArguments.Remove("mod");
}
var otherMod = ModRegistry.Get(targetI18nMod);
if (otherMod is null)
throw new ArgumentException($"Mod `{targetI18nMod}` is not installed.");
var translations = ModTranslationsProvider.GetModTranslations(otherMod.Manifest);
if (translations is null)
return FluentValueFactory.CreateStringValue(targetKey);
var remainingStringNamedArguments = new Dictionary<string, string>();
foreach (var (key, arg) in remainingNamedArguments)
remainingStringNamedArguments[key] = arg.AsString();
return FluentValueFactory.CreateStringValue(translations.Get(targetKey, remainingStringNamedArguments));
}
private IFluentFunctionValue ToUpperFunction(
IGameLocale locale,
IManifest mod,
IReadOnlyList<IFluentFunctionValue> positionalArguments,
IReadOnlyDictionary<string, IFluentFunctionValue> namedArguments)
{
if (positionalArguments.Count == 0)
throw new ArgumentException("Missing `Value` positional argument.");
string value = positionalArguments[0].AsString();
return FluentValueFactory.CreateStringValue(value.ToUpper(locale.CultureInfo));
}
private IFluentFunctionValue ToLowerFunction(
IGameLocale locale,
IManifest mod,
IReadOnlyList<IFluentFunctionValue> positionalArguments,
IReadOnlyDictionary<string, IFluentFunctionValue> namedArguments)
{
if (positionalArguments.Count == 0)
throw new ArgumentException("Missing `Value` positional argument.");
string value = positionalArguments[0].AsString();
return FluentValueFactory.CreateStringValue(value.ToLower(locale.CultureInfo));
}
private IFluentFunctionValue CapitalizeWordsFunction(
IGameLocale locale,
IManifest mod,
IReadOnlyList<IFluentFunctionValue> positionalArguments,
IReadOnlyDictionary<string, IFluentFunctionValue> namedArguments)
{
if (positionalArguments.Count == 0)
throw new ArgumentException("Missing `Value` positional argument.");
string value = positionalArguments[0].AsString();
return FluentValueFactory.CreateStringValue(locale.CultureInfo.TextInfo.ToTitleCase(value));
}
}
}