KnownTranslateEndpoints.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  8. using XUnity.AutoTranslator.Plugin.Core.Constants;
  9. using XUnity.AutoTranslator.Plugin.Core.Web;
  10. namespace XUnity.AutoTranslator.Plugin.Core.Endpoints
  11. {
  12. internal static class KnownTranslateEndpoints
  13. {
  14. public static List<ConfiguredEndpoint> CreateEndpoints( GameObject go, InitializationContext context )
  15. {
  16. var endpoints = new List<ConfiguredEndpoint>();
  17. // could dynamically load types from other assemblies...
  18. //var integratedTypes = AssemblyLoader.GetAllTypesOf<ITranslateEndpoint>( typeof( KnownEndpoints ).Assembly );
  19. var pluginFolder = Path.Combine( PluginEnvironment.Current.DataPath, Settings.PluginFolder );
  20. var dynamicTypes = AssemblyLoader.GetAllTypesOf<ITranslateEndpoint>( pluginFolder );
  21. foreach( var type in dynamicTypes )
  22. {
  23. AddEndpoint( go, context, endpoints, type );
  24. }
  25. return endpoints;
  26. }
  27. private static void AddEndpoint( GameObject go, InitializationContext context, List<ConfiguredEndpoint> endpoints, Type type )
  28. {
  29. ITranslateEndpoint endpoint;
  30. try
  31. {
  32. if( typeof( MonoBehaviour ).IsAssignableFrom( type ) )
  33. {
  34. // allow implementing plugins to hook into Unity lifecycle
  35. endpoint = (ITranslateEndpoint)go.AddComponent( type );
  36. UnityEngine.Object.DontDestroyOnLoad( (UnityEngine.Object)endpoint );
  37. }
  38. else
  39. {
  40. // or... just use any old object
  41. endpoint = (ITranslateEndpoint)Activator.CreateInstance( type );
  42. }
  43. }
  44. catch( Exception e )
  45. {
  46. XuaLogger.Current.Error( e, "Could not instantiate class: " + type.Name );
  47. return;
  48. }
  49. try
  50. {
  51. endpoint.Initialize( context );
  52. endpoints.Add( new ConfiguredEndpoint( endpoint, null ) );
  53. }
  54. catch( Exception e )
  55. {
  56. endpoints.Add( new ConfiguredEndpoint( endpoint, e ) );
  57. }
  58. }
  59. }
  60. }