Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6.  
  7. namespace PlanetbaseFramework
  8. {
  9. public class Modloader
  10. {
  11. public static List<ModBase> ModList = new List<ModBase>();
  12. public static void loadMods()
  13. {
  14. Debug.Log("Loading mod \"Planetbase Framework\"");
  15. ModBase frameworkMod = new FrameworkMod();
  16. ModList.Add(frameworkMod);
  17. frameworkMod.Init();
  18. Debug.Log("Loaded mod \"Planetbase Framework\"");
  19.  
  20. if (Directory.Exists(ModBase.BasePath))
  21. {
  22. string[] files = Directory.GetFiles(ModBase.BasePath, "*.dll");
  23. foreach (string file in files)
  24. {
  25. Type[] types;
  26. try
  27. {
  28. types = Assembly.LoadFile(file).GetTypes();
  29. }
  30. catch (ReflectionTypeLoadException e)
  31. {
  32. Debug.LogException(e);
  33. foreach (Exception loaderException in e.LoaderExceptions)
  34. {
  35. Debug.LogException(loaderException);
  36. }
  37.  
  38. continue;
  39. }
  40. catch (Exception e)
  41. {
  42. Debug.LogException(e);
  43.  
  44. continue;
  45. }
  46.  
  47. foreach(Type type in types)
  48. {
  49. if (typeof(ModBase).IsAssignableFrom(type) && !type.IsAbstract && type.IsPublic)
  50. {
  51. Debug.Log("Loading mod \"" + type.Name.ToString() + "\" from file \"" + file + "\"");
  52. ModBase mod = null;
  53. try
  54. {
  55. mod = Activator.CreateInstance(type) as ModBase;
  56. }
  57. catch (Exception e)
  58. {
  59. Debug.Log("Error loading mod from file: " + file + " of type: " + type.Name.ToString() + ". Exception thrown:");
  60. Debug.Log(e.ToString() + ": " + e.Message);
  61. }
  62.  
  63. if (mod != null)
  64. {
  65. try
  66. {
  67. mod.Init();
  68. ModList.Add(mod);
  69. Debug.Log("Loaded mod \"" + type.Name.ToString() + "\"");
  70. }
  71. catch (Exception e)
  72. {
  73. Debug.Log("Error initializing mod from file: " + file + " of type: " + type.Name.ToString() + ". Exception thrown:");
  74. Debug.Log(e.ToString() + ": " + e.Message);
  75. Debug.Log("Stacktrace: ");
  76. Debug.Log(e.StackTrace);
  77. }
  78. }
  79. else
  80. {
  81. Debug.Log("Failed to load mod \"" + type.Name.ToString() + "\" from file \"" + file + "\"");
  82. }
  83. }
  84. }
  85. }
  86. }
  87. else
  88. {
  89. Directory.CreateDirectory(ModBase.BasePath);
  90. }
  91. }
  92. public static void updateMods()
  93. {
  94. foreach(ModBase mod in ModList)
  95. {
  96. try
  97. {
  98. mod.Update();
  99. }
  100. catch (Exception e)
  101. {
  102. Debug.Log("Error updating mod " + mod.ModName + ". Exception thrown:");
  103. Debug.Log(e.ToString() + ": " + e.Message);
  104. Debug.Log("Stacktrace: ");
  105. Debug.Log(e.StackTrace);
  106. }
  107. }
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement