Repository navigation
Expand file tree
/
Copy pathCreateUIFiles.cs
More file actions
371 lines (326 loc) · 21.2 KB
/
Copy pathCreateUIFiles.cs
File metadata and controls
371 lines (326 loc) · 21.2 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// This entire file is only for creating UI files when in DEBUG.
#if DEBUG
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace BuildingUse
{
/// <summary>
/// Create the files for UI:
/// One file for building status types.
/// One file for the value bindings for used and capacity data for each infomode.
/// One file for UI translation keys for C#. Includes settings.
/// One file for UI translation keys for UI. Excludes settings.
/// By creating the files from the C# enums and lists, C# and UI are ensured to be the same as each other.
/// </summary>
public static class CreateUIFiles
{
// Shortcut for UI constants dictionary.
// Dictionary key is the constant name.
// Dictionary value is the constant value.
private class UIConstants : Dictionary<string, string> { }
// Shortcut for translation keys list.
// Entry is used for constant name and constant value suffix.
private class TranslationKeys : List<string> { }
/// <summary>
/// Create the UI files.
/// </summary>
public static void Create()
{
CreateFileUIConstants();
CreateFileUIBindings();
CreateFileUITranslationKeys(true);
CreateFileUITranslationKeys(false);
}
/// <summary>
/// Create the file for UI constants.
/// </summary>
private static void CreateFileUIConstants()
{
// Start with the do not modify instructions.
StringBuilder sb = new StringBuilder();
sb.Append(DoNotModify());
// Include building status types.
sb.AppendLine("// Define building status types.");
sb.AppendLine("export enum " + nameof(BUBuildingStatusType));
sb.AppendLine("{");
foreach (BUBuildingStatusType buildingStatusType in Enum.GetValues(typeof(BUBuildingStatusType)))
{
sb.AppendLine($" {buildingStatusType.ToString().PadRight(32)} = {(int)buildingStatusType},");
}
sb.AppendLine("}");
// Write the file to the UI/src folder.
string uiConstantsPath = Path.Combine(GetSourceCodePath(), "UI", "src", "uiConstants.tsx");
File.WriteAllText(uiConstantsPath, sb.ToString());
}
/// <summary>
/// Create the file for UI bindings.
/// </summary>
private static void CreateFileUIBindings()
{
// Start with the do not modify instructions.
StringBuilder sb = new StringBuilder();
sb.Append(DoNotModify());
// Include imports.
sb.AppendLine("import { bindValue } from \"cs2/api\";");
sb.AppendLine("import mod from \"../mod.json\";");
// Do each building status type.
sb.AppendLine();
sb.AppendLine("// Define bindings for C# to UI for used and capacity data for each building status type.");
sb.AppendLine("export const uiBindings: any =");
sb.AppendLine("{");
foreach (BUInfoviewData infoviewData in BUInfoviewDatas.instance.Values)
{
foreach (BUBuildingStatusTypeData buildingStatusTypeData in infoviewData.buildingStatusTypeDatas.Values)
{
// Skip special cases.
if (!buildingStatusTypeData.IsSpecialCase)
{
// Include used, capacity, and count bindings.
BUBuildingStatusType buildingStatusType = buildingStatusTypeData.BuildingStatusType;
string used = buildingStatusType + "Used";
string capacity = buildingStatusType + "Capacity";
string count = buildingStatusType + "Count";
sb.AppendLine($" {used .PadRight(40)} : bindValue<number>(mod.id, {("\"" + used + "\",").PadRight(48)} 0),");
sb.AppendLine($" {capacity.PadRight(40)} : bindValue<number>(mod.id, {("\"" + capacity + "\",").PadRight(48)} 0),");
sb.AppendLine($" {count .PadRight(40)} : bindValue<number>(mod.id, {("\"" + count + "\",").PadRight(48)} 0),");
}
}
}
sb.AppendLine("}");
// Include binding names.
sb.AppendLine();
sb.AppendLine("// Define binding names for C# to UI.");
sb.AppendLine("export class uiBindingNames");
sb.AppendLine("{");
sb.AppendLine($" public static {BuildingUseUISystem.BindingNameCountVehiclesInUse .PadRight(40)} : string = \"{BuildingUseUISystem.BindingNameCountVehiclesInUse }\";");
sb.AppendLine($" public static {BuildingUseUISystem.BindingNameCountVehiclesInMaintenance .PadRight(40)} : string = \"{BuildingUseUISystem.BindingNameCountVehiclesInMaintenance }\";");
sb.AppendLine($" public static {BuildingUseUISystem.BindingNameEfficiencyMaxColor200Percent.PadRight(40)} : string = \"{BuildingUseUISystem.BindingNameEfficiencyMaxColor200Percent}\";");
sb.AppendLine($" public static {BuildingUseUISystem.BindingNameProductionMaxColor200Percent.PadRight(40)} : string = \"{BuildingUseUISystem.BindingNameProductionMaxColor200Percent}\";");
sb.AppendLine($" public static {BuildingUseUISystem.BindingNameSelectedDistrict .PadRight(40)} : string = \"{BuildingUseUISystem.BindingNameSelectedDistrict }\";");
sb.AppendLine($" public static {BuildingUseUISystem.BindingNameDistrictInfos .PadRight(40)} : string = \"{BuildingUseUISystem.BindingNameDistrictInfos }\";");
sb.AppendLine("}");
// Include event names.
sb.AppendLine();
sb.AppendLine("// Define event names for UI to C#.");
sb.AppendLine("export class uiEventNames");
sb.AppendLine("{");
sb.AppendLine($" public static {BuildingUseUISystem.EventNameCountVehiclesInUseClicked .PadRight(40)} : string = \"{BuildingUseUISystem.EventNameCountVehiclesInUseClicked }\";");
sb.AppendLine($" public static {BuildingUseUISystem.EventNameCountVehiclesInMaintenanceClicked.PadRight(40)} : string = \"{BuildingUseUISystem.EventNameCountVehiclesInMaintenanceClicked}\";");
sb.AppendLine($" public static {BuildingUseUISystem.EventNameEfficiencyMaxColorClicked .PadRight(40)} : string = \"{BuildingUseUISystem.EventNameEfficiencyMaxColorClicked }\";");
sb.AppendLine($" public static {BuildingUseUISystem.EventNameProductionMaxColorClicked .PadRight(40)} : string = \"{BuildingUseUISystem.EventNameProductionMaxColorClicked }\";");
sb.AppendLine($" public static {BuildingUseUISystem.EventNameSelectedDistrictChanged .PadRight(40)} : string = \"{BuildingUseUISystem.EventNameSelectedDistrictChanged }\";");
sb.AppendLine("}");
// Write the file to the UI/src folder.
string uiBindingsPath = Path.Combine(GetSourceCodePath(), "UI", "src", "uiBindings.tsx");
File.WriteAllText(uiBindingsPath, sb.ToString());
}
/// <summary>
/// Create the file for UI transtion keys.
/// One file for C# (i.e. CS) and one file for UI.
/// </summary>
private static void CreateFileUITranslationKeys(bool csFile)
{
// Start with the do not modify instructions.
StringBuilder sb = new StringBuilder();
sb.Append(DoNotModify());
// For C# file, include namespace.
if (csFile)
{
sb.AppendLine($"namespace {ModAssemblyInfo.Name}");
sb.AppendLine("{");
}
// Start class.
const string className = "UITranslationKey";
if (csFile)
{
sb.AppendLine(" // Define UI translation keys.");
sb.AppendLine(" public class " + className);
sb.AppendLine(" {");
}
else
{
sb.AppendLine("// Define UI translation keys.");
sb.AppendLine("export class " + className);
sb.AppendLine("{");
}
// Include title and description.
TranslationKeys titleDescription = new TranslationKeys()
{
"Title",
"Description",
};
sb.Append(GetTranslationsContent(csFile, "Mod title and description.", titleDescription));
// Include infoview titles.
UIConstants infoviewTitles = new UIConstants();
foreach (BUInfoviewData infoviewData in BUInfoviewDatas.instance.Values)
{
infoviewTitles.Add("InfoviewTitle" + infoviewData.Infoview.ToString(), $"Infoviews.INFOVIEW[{infoviewData.InfoviewName}]");
}
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "Infoview titles.", infoviewTitles));
// Include infomode titles and tooltips.
UIConstants infomodeTitles = new UIConstants();
UIConstants infomodeTooltips = new UIConstants();
foreach (BUInfoviewData infoviewData in BUInfoviewDatas.instance.Values)
{
foreach (BUBuildingStatusTypeData buildingStatusTypeData in infoviewData.buildingStatusTypeDatas.Values)
{
// Skip special cases.
if (!buildingStatusTypeData.IsSpecialCase)
{
BUBuildingStatusType buildingStatusType = buildingStatusTypeData.BuildingStatusType;
string buildingStatusTypeName = ModAssemblyInfo.Name + buildingStatusType.ToString();
infomodeTitles .Add("InfomodeTitle" + buildingStatusType.ToString(), $"Infoviews.INFOMODE[{ buildingStatusTypeName}]");
infomodeTooltips.Add("InfomodeTooltip" + buildingStatusType.ToString(), $"Infoviews.INFOMODE_TOOLTIP[{buildingStatusTypeName}]");
}
}
}
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "Infomode titles.", infomodeTitles));
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "Infomode tooltips.", infomodeTooltips));
// Include district selector text.
TranslationKeys districtSelectorText = new TranslationKeys() { "EntireCity", "DistrictSelectorTooltip", };
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "District selector text.", districtSelectorText));
// Include select/deselect text.
TranslationKeys selectDeselectText = new TranslationKeys() { "SelectAll", "DeselectAll", "SelectDeselectTooltip", };
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "Select/deselect text.", selectDeselectText));
// Include vehicles in use/in maintenance text.
TranslationKeys vehiclesInUseInMaintenanceText = new TranslationKeys() { "InUse", "InMaintenance", "InUseInMaintenanceTooltip", };
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "Vehicles in use/in maintenance text.", vehiclesInUseInMaintenanceText));
// Include max color text.
TranslationKeys maxColorText = new TranslationKeys() { "MaxColor100Percent", "MaxColor200Percent", "MaxColorTooltip", };
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "Max color text.", maxColorText));
// Include unit of measure text.
TranslationKeys unitOfMeasureText = new TranslationKeys() { "UnitOfMeasurePrefixKilo", "UnitOfMeasurePrefixMega", "GigaWattHour", };
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "Unit of measure text.", unitOfMeasureText));
// Include general text.
TranslationKeys generalText = new TranslationKeys() { "NoBuildings", };
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "General text.", generalText));
// For C# file only, include settings translations.
if (csFile)
{
// Construct settings.
UIConstants _translationKeySettings = new UIConstants()
{
{ "SettingTitle", Mod.ModSettings.GetSettingsLocaleID() },
{ "SettingGroupGeneral", Mod.ModSettings.GetOptionGroupLocaleID(ModSettings.GroupGeneral) },
{ "SettingZonedBuildingColorLabel", Mod.ModSettings.GetOptionLabelLocaleID(nameof(ModSettings.ZonedBuildingColor )) },
{ "SettingZonedBuildingColorDesc", Mod.ModSettings.GetOptionDescLocaleID (nameof(ModSettings.ZonedBuildingColor )) },
{ "SettingZonedBuildingColorChoiceThreeColors", Mod.ModSettings.GetEnumValueLocaleID (ModSettings.ZonedBuildingColorChoice.ThreeColors ) },
{ "SettingZonedBuildingColorChoiceZoneColor", Mod.ModSettings.GetEnumValueLocaleID (ModSettings.ZonedBuildingColorChoice.ZoneColor ) },
{ "SettingServiceBuildingColorLabel", Mod.ModSettings.GetOptionLabelLocaleID(nameof(ModSettings.ServiceBuildingColor )) },
{ "SettingServiceBuildingColorDesc", Mod.ModSettings.GetOptionDescLocaleID (nameof(ModSettings.ServiceBuildingColor )) },
{ "SettingServiceBuildingColorChoiceThreeColors", Mod.ModSettings.GetEnumValueLocaleID (ModSettings.ServiceBuildingColorChoice.ThreeColors ) },
{ "SettingServiceBuildingColorChoiceRed", Mod.ModSettings.GetEnumValueLocaleID (ModSettings.ServiceBuildingColorChoice.Red ) },
{ "SettingServiceBuildingColorChoiceVarious", Mod.ModSettings.GetEnumValueLocaleID (ModSettings.ServiceBuildingColorChoice.Various ) },
{ "SettingProductionInfoviewColorLabel", Mod.ModSettings.GetOptionLabelLocaleID(nameof(ModSettings.ProductionInfoviewColor )) },
{ "SettingProductionInfoviewColorDesc", Mod.ModSettings.GetOptionDescLocaleID (nameof(ModSettings.ProductionInfoviewColor )) },
{ "SettingProductionInfoviewColorChoiceThreeColors", Mod.ModSettings.GetEnumValueLocaleID (ModSettings.ProductionInfoviewColorChoice.ThreeColors ) },
{ "SettingProductionInfoviewColorChoiceVariousGame", Mod.ModSettings.GetEnumValueLocaleID (ModSettings.ProductionInfoviewColorChoice.VariousGame ) },
{ "SettingProductionInfoviewColorChoiceVariousResource",Mod.ModSettings.GetEnumValueLocaleID (ModSettings.ProductionInfoviewColorChoice.VariousResource) },
{ "SettingReverseColorsLabel", Mod.ModSettings.GetOptionLabelLocaleID(nameof(ModSettings.ReverseColors )) },
{ "SettingReverseColorsDesc", Mod.ModSettings.GetOptionDescLocaleID (nameof(ModSettings.ReverseColors )) },
{ "SettingGroupAbout", Mod.ModSettings.GetOptionGroupLocaleID(ModSettings.GroupAbout) },
{ "SettingModVersionLabel", Mod.ModSettings.GetOptionLabelLocaleID(nameof(ModSettings.ModVersion )) },
{ "SettingModVersionDesc", Mod.ModSettings.GetOptionDescLocaleID (nameof(ModSettings.ModVersion )) },
};
// Append settings to the file.
sb.AppendLine();
sb.Append(GetTranslationsContent(csFile, "Settings.", _translationKeySettings));
}
// End class.
sb.AppendLine(csFile ? " }": "}");
// For C# file, end namespace.
if (csFile)
{
sb.AppendLine("}");
}
// Write the file.
string uiBindingsPath;
if (csFile)
{
// Write the file to the Localization folder.
uiBindingsPath = Path.Combine(GetSourceCodePath(), "Localization", "UITranslationKey.cs");
}
else
{
// Write the file to the UI/src folder.
uiBindingsPath = Path.Combine(GetSourceCodePath(), "UI", "src", "uiTranslationKey.tsx");
}
File.WriteAllText(uiBindingsPath, sb.ToString());
}
/// <summary>
/// Get instructions for do not modify.
/// </summary>
/// <returns></returns>
private static string DoNotModify()
{
StringBuilder sb = new StringBuilder();
// Include do not modify instructions.
sb.AppendLine($"// DO NOT MODIFY THIS FILE.");
sb.AppendLine($"// This entire file was automatically generated by class {nameof(CreateUIFiles)}.");
sb.AppendLine($"// Make any needed changes in class {nameof(CreateUIFiles)}.");
sb.AppendLine();
return sb.ToString();
}
/// <summary>
/// Get the constants content.
/// </summary>
private static string GetTranslationsContent(bool csFile, string comment, UIConstants constants)
{
string indentation = (csFile ? " " : " ");
StringBuilder sb = new StringBuilder();
sb.AppendLine($"{indentation}// {comment}");
foreach (var key in constants.Keys)
{
if (csFile)
{
sb.AppendLine($"{indentation}public const string {key.PadRight(50)} = \"{constants[key]}\";");
}
else
{
sb.AppendLine($"{indentation}public static {key.PadRight(50)}: string = \"{constants[key]}\";");
}
}
return sb.ToString();
}
/// <summary>
/// Get the translations content.
/// </summary>
private static string GetTranslationsContent(bool csFile, string comment, TranslationKeys translationKeys)
{
string indentation = (csFile ? " " : " ");
StringBuilder sb = new StringBuilder();
sb.AppendLine($"{indentation}// {comment}");
foreach (var translationKey in translationKeys)
{
if (csFile)
{
sb.AppendLine($"{indentation}public const string {translationKey.PadRight(50)} = \"{ModAssemblyInfo.Name}.{translationKey}\";");
}
else
{
sb.AppendLine($"{indentation}public static {translationKey.PadRight(50)}: string = \"{ModAssemblyInfo.Name}.{translationKey}\";");
}
}
return sb.ToString();
}
/// <summary>
/// Get the full path of this C# source code file.
/// </summary>
private static string GetSourceCodePath([System.Runtime.CompilerServices.CallerFilePath] string sourceFile = "")
{
return Path.GetDirectoryName(sourceFile);
}
}
}
#endif