Skip to content

Instantly share code, notes, and snippets.

@yKimisaki
Created July 19, 2019 14:58
Show Gist options
  • Save yKimisaki/f02d3dd75f305ce22b66e2b4b8625f4c to your computer and use it in GitHub Desktop.
Save yKimisaki/f02d3dd75f305ce22b66e2b4b8625f4c to your computer and use it in GitHub Desktop.
/*
MIT License
Copyright (c) 2019 Yoshitaka Kimisaki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
void Main(string[] args)
{
// Localization
using (var client = new AirtableClient())
{
var flags = (BindingFlags.Public| BindingFlags.Instance);
var methods = typeof(AirtableBase).GetMethods(flags);
MethodInfo loadTableAsyncMethodInfo = null;
foreach (var method in methods)
{
if (method.IsGenericMethod && method.Name == "LoadTableAsync" && method.GetParameters().Length == 1)
{
loadTableAsyncMethodInfo = method;
break;
}
}
if (loadTableAsyncMethodInfo == null)
{
return;
}
using (var file = File.Open(Path.Combine(OutputClientRoot, "Localization.Generated.cs"), FileMode.OpenOrCreate))
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("using MyProject.Tables;");
stringBuilder.AppendLine("namespace MyProject.Client.LocalizationSystem");
stringBuilder.AppendLine("{");
stringBuilder.AppendLine($"\tpublic static class Localization");
stringBuilder.AppendLine("\t{");
var validSheets = new List<string>();
var assembly = Assembly.GetEntryAssembly();
foreach (var type in assembly.GetTypes())
{
var attribute = type.GetCustomAttribute<LocalizationSheetNameAttribute>();
if (attribute == null)
{
continue;
}
var loadTableAsync = loadTableAsyncMethodInfo.MakeGenericMethod(type);
dynamic rowsTask = loadTableAsync.Invoke(client.GetLocalizationBase(), new[] { attribute.SheetName });
dynamic rows = await rowsTask;
stringBuilder.AppendLine($"\t\tpublic static class {attribute.SheetName}");
stringBuilder.AppendLine("\t\t{");
stringBuilder.AppendLine($"\t\t\tprivate static {attribute.SheetName}LocalizationSheetTable table;");
var validRows = new List<(string, string)>();
foreach (var row in rows)
{
var rowType = row.GetType();
var idPropertyInfo = rowType.GetProperty("LocalizationID", flags);
var id = idPropertyInfo.GetValue(row) as string;
if (int.TryParse(id, out var _))
{
stringBuilder.AppendLine($"\t\t\tpublic static string _{id} => table.FindByLocalizationID(\"{id}\").GetLocalizedString();");
validRows.Add((id, $"_{id}"));
}
else
{
stringBuilder.AppendLine($"\t\t\tpublic static string {id} => table.FindByLocalizationID(\"{id}\").GetLocalizedString();");
validRows.Add((id, id));
}
}
stringBuilder.AppendLine("\t\t\tpublic static void Register(MemoryDatabase db)");
stringBuilder.AppendLine("\t\t\t{");
stringBuilder.AppendLine($"\t\t\t\ttable = db.{attribute.SheetName}LocalizationSheetTable;");
stringBuilder.AppendLine("\t\t\t}");
stringBuilder.AppendLine("\t\t\tpublic static string Search(string localizationID)");
stringBuilder.AppendLine("\t\t\t{");
stringBuilder.AppendLine("\t\t\t\tswitch (localizationID)");
stringBuilder.AppendLine("\t\t\t\t{");
foreach (var row in validRows)
{
stringBuilder.AppendLine($"\t\t\t\t\tcase \"{row.Item1}\":");
stringBuilder.AppendLine($"\t\t\t\t\t\treturn {row.Item2};");
}
stringBuilder.AppendLine("\t\t\t\t}");
stringBuilder.AppendLine("\t\t\t\treturn localizationID;");
stringBuilder.AppendLine("\t\t\t}");
stringBuilder.AppendLine("\t\t}");
validSheets.Add(attribute.SheetName);
}
stringBuilder.AppendLine("\t\tpublic static void Register(MemoryDatabase db)");
stringBuilder.AppendLine("\t\t{");
foreach (var validSheetName in validSheets)
{
stringBuilder.AppendLine($"\t\t\t{validSheetName}.Register(db);");
}
stringBuilder.AppendLine("\t\t}");
stringBuilder.AppendLine("\t\tpublic static string Search(LocalizationTable table, string localizationID)");
stringBuilder.AppendLine("\t\t{");
stringBuilder.AppendLine("\t\t\tswitch(table)");
stringBuilder.AppendLine("\t\t\t{");
foreach (var validSheetName in validSheets)
{
stringBuilder.AppendLine($"\t\t\t\tcase LocalizationTable.{validSheetName}:");
stringBuilder.AppendLine($"\t\t\t\t\treturn {validSheetName}.Search(localizationID);");
}
stringBuilder.AppendLine("\t\t\t}");
stringBuilder.AppendLine("\t\t\treturn localizationID;");
stringBuilder.AppendLine("\t\t}");
stringBuilder.AppendLine("\t}");
stringBuilder.AppendLine("\tpublic enum LocalizationTable");
stringBuilder.AppendLine("\t{");
foreach (var validSheetName in validSheets)
{
stringBuilder.AppendLine($"\t\t{validSheetName},");
}
stringBuilder.AppendLine("\t}");
stringBuilder.AppendLine("}");
file.SetLength(0);
file.Write(Encoding.UTF8.GetBytes(stringBuilder.ToString()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment