Minor tweaks

Translation util only for debug build (unused in release)
Simplify some plugin load expressions
This commit is contained in:
Kurt 2022-02-26 21:45:31 -08:00
parent 76afe91bbb
commit ae74ee7931
4 changed files with 127 additions and 121 deletions

View file

@ -1,17 +1,19 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using static PKHeX.WinForms.PluginLoadSetting;
namespace PKHeX.WinForms;
namespace PKHeX.WinForms
{
public static class PluginLoader
{
public static IEnumerable<T> LoadPlugins<T>(string pluginPath, PluginLoadSetting loadSetting) where T : class
{
var dllFileNames = !Directory.Exists(pluginPath)
? Enumerable.Empty<string>()
? Array.Empty<string>() // Don't immediately return, as we may be loading plugins merged with this .exe
: Directory.EnumerateFiles(pluginPath, "*.dll", SearchOption.AllDirectories);
var assemblies = GetAssemblies(dllFileNames, loadSetting);
var pluginTypes = GetPluginsOfType<T>(assemblies);
@ -26,8 +28,8 @@ namespace PKHeX.WinForms
try { activate = (T?)Activator.CreateInstance(t); }
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Unable to load plugin [{t.Name}]: {t.FullName}");
System.Diagnostics.Debug.WriteLine(ex.Message);
Debug.WriteLine($"Unable to load plugin [{t.Name}]: {t.FullName}");
Debug.WriteLine(ex.Message);
continue;
}
if (activate != null)
@ -37,27 +39,32 @@ namespace PKHeX.WinForms
private static IEnumerable<Assembly> GetAssemblies(IEnumerable<string> dllFileNames, PluginLoadSetting loadSetting)
{
var assemblies = dllFileNames.Select(GetPluginLoadMethod(loadSetting));
if (loadSetting is PluginLoadSetting.LoadFromMerged or PluginLoadSetting.LoadFileMerged or PluginLoadSetting.UnsafeMerged)
var loadMethod = GetPluginLoadMethod(loadSetting);
var assemblies = dllFileNames.Select(loadMethod);
if (loadSetting.IsMerged())
assemblies = assemblies.Concat(new[] { Assembly.GetExecutingAssembly() }); // load merged too
return assemblies;
}
private static Func<string, Assembly> GetPluginLoadMethod(PluginLoadSetting pls) => pls switch
{
PluginLoadSetting.LoadFrom or PluginLoadSetting.LoadFromMerged => Assembly.LoadFrom,
PluginLoadSetting.LoadFile or PluginLoadSetting.LoadFileMerged => Assembly.LoadFile,
PluginLoadSetting.UnsafeLoadFrom or PluginLoadSetting.UnsafeMerged => Assembly.UnsafeLoadFrom,
_ => throw new NotImplementedException($"PluginLoadSetting: {pls} method not defined."),
LoadFrom or LoadFromMerged => Assembly.LoadFrom,
LoadFile or LoadFileMerged => Assembly.LoadFile,
UnsafeLoadFrom or UnsafeMerged => Assembly.UnsafeLoadFrom,
_ => throw new IndexOutOfRangeException($"PluginLoadSetting: {pls} method not defined."),
};
public static bool IsMerged(this PluginLoadSetting loadSetting) => loadSetting is LoadFromMerged or LoadFileMerged or UnsafeMerged;
private static IEnumerable<Type> GetPluginsOfType<T>(IEnumerable<Assembly> assemblies)
{
var pluginType = typeof(T);
return assemblies.SelectMany(z => GetPluginTypes(z, pluginType));
var interfaceTypeName = typeof(T).FullName;
if (interfaceTypeName is null)
return Array.Empty<Type>();
return assemblies.SelectMany(z => GetPluginTypes(z, interfaceTypeName));
}
private static IEnumerable<Type> GetPluginTypes(Assembly z, Type pluginType)
private static IEnumerable<Type> GetPluginTypes(Assembly z, string interfaceTypeName)
{
try
{
@ -67,35 +74,31 @@ namespace PKHeX.WinForms
attachMethod?.Invoke(null, Array.Empty<object>());
var types = z.GetTypes();
return types.Where(type => IsTypePlugin(type, pluginType));
return types.Where(type => IsTypePlugin(type, interfaceTypeName));
}
// User plugins can be out of date, with mismatching API surfaces.
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}");
System.Diagnostics.Debug.WriteLine(ex.Message);
if (ex is ReflectionTypeLoadException rtle)
{
Debug.WriteLine($"Unable to load plugin [{interfaceTypeName}]: {z.FullName}");
Debug.WriteLine(ex.Message);
if (ex is not ReflectionTypeLoadException rtle)
return Array.Empty<Type>();
foreach (var le in rtle.LoaderExceptions)
{
if (le is not null)
System.Diagnostics.Debug.WriteLine(le.Message);
}
Debug.WriteLine(le.Message);
}
return Array.Empty<Type>();
}
}
private static bool IsTypePlugin(Type type, Type pluginType)
private static bool IsTypePlugin(Type type, string interfaceTypeName)
{
if (type.IsInterface || type.IsAbstract)
return false;
var name = pluginType.FullName;
if (name == null)
return false;
if (type.GetInterface(name) == null)
if (type.GetInterface(interfaceTypeName) == null)
return false;
return true;
}
}
}

View file

@ -124,7 +124,7 @@ namespace PKHeX.WinForms
var headers = dgData.Columns.Cast<DataGridViewColumn>();
await s.WriteLineAsync(string.Join(",", headers.Skip(1).Select(column => $"\"{column.HeaderText}\""))).ConfigureAwait(false);
foreach (var cells in from DataGridViewRow row in dgData.Rows select row.Cells.Cast<DataGridViewCell>())
foreach (var cells in dgData.Rows.Cast<DataGridViewRow>().Select(row => row.Cells.Cast<DataGridViewCell>()))
await s.WriteLineAsync(string.Join(",", cells.Skip(1).Select(cell => $"\"{cell.Value}\""))).ConfigureAwait(false);
}

View file

@ -46,17 +46,19 @@ namespace PKHeX.WinForms
private static void UpdateTranslations()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var types = assembly.GetTypes();
// add mode
WinFormsTranslator.SetRemovalMode(false);
WinFormsTranslator.LoadSettings<PKHeXSettings>(DefaultLanguage);
WinFormsTranslator.LoadAllForms(LoadBanlist); // populate with every possible control
WinFormsTranslator.LoadAllForms(types, LoadBanlist); // populate with every possible control
WinFormsTranslator.UpdateAll(DefaultLanguage, Languages); // propagate to others
WinFormsTranslator.DumpAll(Banlist); // dump current to file
// de-populate
WinFormsTranslator.SetRemovalMode(); // remove used keys, don't add any
WinFormsTranslator.LoadSettings<PKHeXSettings>(DefaultLanguage, false);
WinFormsTranslator.LoadAllForms(LoadBanlist);
WinFormsTranslator.LoadAllForms(types, LoadBanlist);
WinFormsTranslator.RemoveAll(DefaultLanguage, PurgeBanlist); // remove all lines from above generated files that still remain
// Move translated files from the debug exe loc to their project location

View file

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
@ -31,11 +30,20 @@ namespace PKHeX.WinForms
private static void TranslateForm(Control form, TranslationContext context)
{
form.SuspendLayout();
var formname = form.Name;
// Translate Title
form.Text = context.GetTranslatedText(formname, form.Text);
var formName = form.Name;
form.Text = context.GetTranslatedText(formName, form.Text);
// Translate Controls
var translatable = GetTranslatableControls(form);
foreach (var c in translatable)
TranslateControl(c, context, formName);
form.ResumeLayout();
}
private static void TranslateControl(object c, TranslationContext context, string formname)
{
if (c is Control r)
{
@ -52,8 +60,6 @@ namespace PKHeX.WinForms
t.Text = updated;
}
}
form.ResumeLayout();
}
private static IEnumerable<string> GetTranslationFile(string lang)
{
@ -132,12 +138,14 @@ namespace PKHeX.WinForms
foreach (var dropDownItem in item.DropDownItems.OfType<ToolStripMenuItem>())
{
yield return dropDownItem;
if (!dropDownItem.HasDropDownItems) continue;
if (!dropDownItem.HasDropDownItems)
continue;
foreach (ToolStripMenuItem subItem in GetToolsStripDropDownItems(dropDownItem))
yield return subItem;
}
}
#if DEBUG
public static void UpdateAll(string baseLanguage, IEnumerable<string> others)
{
var baseContext = GetContext(baseLanguage);
@ -161,16 +169,14 @@ namespace PKHeX.WinForms
}
}
public static void LoadAllForms(params string[] banlist)
public static void LoadAllForms(IEnumerable<System.Type> types, params string[] banlist)
{
var q = from t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
where t.BaseType == typeof(Form) && !banlist.Contains(t.Name)
select t;
foreach (var t in q)
types = types.Where(t => t.BaseType == typeof(Form) && !banlist.Contains(t.Name));
foreach (var t in types)
{
var constructors = t.GetConstructors();
if (constructors.Length == 0)
{ Debug.WriteLine($"No constructors: {t.Name}"); continue; }
{ System.Diagnostics.Debug.WriteLine($"No constructors: {t.Name}"); continue; }
var argCount = constructors[0].GetParameters().Length;
try
{
@ -179,7 +185,7 @@ namespace PKHeX.WinForms
// This is a debug utility method, will always be logging. Shouldn't ever fail.
catch
{
Debug.Write($"Failed to create a new form {t}");
System.Diagnostics.Debug.Write($"Failed to create a new form {t}");
}
}
}
@ -235,6 +241,7 @@ namespace PKHeX.WinForms
}
}
}
#endif
}
public sealed class TranslationContext
@ -278,11 +285,5 @@ namespace PKHeX.WinForms
GetTranslatedText(kvp.Key, kvp.Value);
AddNew = oldAdd;
}
public void RemoveKeys(TranslationContext other)
{
foreach (var kvp in other.Translation)
Translation.Remove(kvp.Key);
}
}
}