PluginAdapter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using CommonAIEditor.Plugins.Win32;
  3. using CommonAI.Zone;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Configuration;
  7. using CommonLang.Property;
  8. using CommonAIServer.Node;
  9. using CommonLang.Protocol;
  10. namespace CommonAIEditor
  11. {
  12. public static class EditorPlugin
  13. {
  14. private static IGameEditorPlugin mGameEditorPlugin;
  15. private static IGameEditorPlugin mDefaultGameEditorPlugin;
  16. public static string PluginDLL { get; private set; }
  17. public static string ServerDLL { get; private set; }
  18. public static void Init()
  19. {
  20. InitPlugins();
  21. InitDataPath();
  22. }
  23. private static void InitPlugins()
  24. {
  25. string data_plugin = ConfigurationManager.AppSettings["data_plugin"] + "";
  26. string edit_plugin = ConfigurationManager.AppSettings["edit_plugin"] + "";
  27. string node_plugin = ConfigurationManager.AppSettings["node_plugin"] + "";
  28. //-------------------------------------------------------------------------------------
  29. try
  30. {
  31. if (!string.IsNullOrEmpty(data_plugin))
  32. {
  33. EditorPlugin.PluginDLL = Application.StartupPath + "\\" + data_plugin;
  34. TemplateManager.IsEditor = true;
  35. var factory = ReflectionUtil.LoadInterfaceFromDLL<InstanceZoneFactory>(PluginDLL);
  36. if (factory == null) throw new Exception(data_plugin);
  37. TemplateManager.setFactory(factory);
  38. }
  39. }
  40. catch (Exception err)
  41. {
  42. MessageBox.Show("数据插件初始化失败 : " + data_plugin + "\n" + err.Message);
  43. }
  44. //-------------------------------------------------------------------------------------
  45. try
  46. {
  47. if (!string.IsNullOrEmpty(node_plugin))
  48. {
  49. EditorPlugin.ServerDLL = Application.StartupPath + "\\" + node_plugin;
  50. var factory = ReflectionUtil.LoadInterfaceFromDLL<ZoneNodeFactory>(ServerDLL);
  51. if (factory == null) throw new Exception(node_plugin);
  52. }
  53. }
  54. catch (Exception err)
  55. {
  56. MessageBox.Show("服务器插件初始化失败 : " + node_plugin + "\n" + err.Message);
  57. }
  58. //-------------------------------------------------------------------------------------
  59. try
  60. {
  61. if (!string.IsNullOrEmpty(edit_plugin))
  62. {
  63. mDefaultGameEditorPlugin = new Win32EditorGamePlugin();
  64. try
  65. {
  66. Type type = ReflectionUtil.LoadTypeFromDLL<IGameEditorPlugin>(edit_plugin);
  67. mGameEditorPlugin = (IGameEditorPlugin)ReflectionUtil.CreateInstance(type);
  68. if (mGameEditorPlugin == null) throw new Exception(type.FullName);
  69. }
  70. catch (Exception err)
  71. {
  72. MessageBox.Show(err.Message);
  73. mGameEditorPlugin = new Win32EditorGamePlugin();
  74. }
  75. mGameEditorPlugin.PluginDLL = (PluginDLL);
  76. mDefaultGameEditorPlugin.PluginDLL = (PluginDLL);
  77. }
  78. }
  79. catch (Exception err)
  80. {
  81. MessageBox.Show("编辑器插件初始化失败 : " + edit_plugin + "\n" + err.Message);
  82. }
  83. //-------------------------------------------------------------------------------------
  84. }
  85. private static void InitDataPath()
  86. {
  87. string data_root = ConfigurationManager.AppSettings["data_root"];
  88. Editor.SetDataRoot(Application.StartupPath);
  89. if (!string.IsNullOrEmpty(data_root) && Directory.Exists(Application.StartupPath + "\\" + data_root + "\\data"))
  90. {
  91. Editor.SetDataRoot(Path.GetFullPath(Application.StartupPath + "\\" + data_root));
  92. }
  93. else if (Directory.Exists(Application.StartupPath + "\\..\\data"))
  94. {
  95. Editor.SetDataRoot(Path.GetFullPath(Application.StartupPath + "\\.."));
  96. }
  97. MessageFactoryGenerator codec = TemplateManager.MessageCodec;
  98. File.WriteAllText(Editor.EditorRootDir + "/codec.txt", codec.ListAll());
  99. mGameEditorPlugin.DataRoot = (Editor.EditorRootDir);
  100. mDefaultGameEditorPlugin.DataRoot = (Editor.EditorRootDir);
  101. }
  102. public static IGameEditorPlugin CurrentPlugin
  103. {
  104. get { return mGameEditorPlugin; }
  105. }
  106. public static IGameEditorPlugin DefaultPlugin
  107. {
  108. get { return mDefaultGameEditorPlugin; }
  109. }
  110. }
  111. }