source: binary-improvements/7dtd-server-fixes/src/Mods.cs@ 228

Last change on this file since 228 was 224, checked in by alloc, 10 years ago

A11 preps

File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Reflection;
5using UnityEngine;
6
7namespace AllocsFixes
8{
9 public static class Mods {
10 public class ModData {
11 public Assembly assembly;
12 public string name;
13 public string version;
14 public ModAPI apiInstance;
15 }
16
17 private static readonly string MOD_PATH = Application.dataPath + "/../Mods";
18 private static Dictionary<string, ModData> loadedMods = new Dictionary<string, ModData> ();
19
20 public static void LoadMods () {
21 if (Directory.Exists (MOD_PATH)) {
22 Log.Out ("Loading mods");
23
24 foreach (string modDir in Directory.GetDirectories (MOD_PATH)) {
25 foreach (string modDll in Directory.GetFiles (modDir, "*.dll")) {
26 try {
27 Assembly assemblyInstance = Assembly.LoadFrom (modDll);
28
29 ModData md = new ModData ();
30 md.assembly = assemblyInstance;
31
32 md.apiInstance = CreateApiInstance (md);
33 if (md.apiInstance == null) {
34 Log.Warning ("DLL does not declare a class of type ModAPI, ignoring");
35 continue;
36 }
37 if (string.IsNullOrEmpty (md.apiInstance.ModName ())) {
38 Log.Warning ("DLL's ModAPI does not override ModName(), ignoring");
39 continue;
40 }
41 if (loadedMods.ContainsKey (md.apiInstance.ModName ())) {
42 Log.Warning ("DLL's ModAPI.ModName() returns name that's already loaded, ignoring");
43 continue;
44 }
45 md.name = md.apiInstance.ModName ();
46 md.version = md.apiInstance.ModVersion ();
47
48 loadedMods.Add (md.name, md);
49
50 Log.Out ("Loaded mod: " + md.name + " (" + md.version + ")");
51 } catch (Exception e) {
52 Log.Error ("Failed loading DLL");
53 Log.Exception (e);
54 }
55 }
56 }
57
58 Log.Out ("Loading mods done");
59 } else {
60 Log.Out ("No mods folder found");
61 }
62 }
63
64 public static bool ModLoaded (string _modName) {
65 return loadedMods.ContainsKey (_modName);
66 }
67
68 public static Assembly GetModAssembly (string _modName) {
69 if (ModLoaded (_modName)) {
70 return loadedMods [_modName].assembly;
71 }
72 return null;
73 }
74
75 public static List<ModData> LoadedMods () {
76 List<ModData> res = new List<ModData> ();
77 foreach (ModData md in loadedMods.Values) {
78 res.Add (md);
79 }
80 return res;
81 }
82
83 private static ModAPI CreateApiInstance (ModData _md) {
84 Type modApiType = typeof (ModAPI);
85 foreach (Type t in _md.assembly.GetTypes ()) {
86 if (modApiType.IsAssignableFrom (t)) {
87 Log.Out ("Found ModAPI");
88
89 return Activator.CreateInstance(t) as ModAPI;
90 }
91 }
92 return null;
93 }
94
95 public static void CallGameAwake () {
96 foreach (ModData md in loadedMods.Values) {
97 if (md.apiInstance != null) {
98 md.apiInstance.GameAwake ();
99 }
100 }
101 }
102
103 public static void CallGameShutdown () {
104 foreach (ModData md in loadedMods.Values) {
105 if (md.apiInstance != null) {
106 md.apiInstance.GameShutdown ();
107 }
108 }
109 }
110
111 public static void CallSavePlayerData (int _clientId, PlayerDataFile _playerDataFile) {
112 foreach (ModData md in loadedMods.Values) {
113 if (md.apiInstance != null) {
114 md.apiInstance.SavePlayerData (_clientId, _playerDataFile);
115 }
116 }
117 }
118
119 public static void CallRequestToSpawnPlayer (int _clientId, string _name, int _chunkViewDim, PlayerProfile _playerProfile) {
120 foreach (ModData md in loadedMods.Values) {
121 if (md.apiInstance != null) {
122 md.apiInstance.RequestToSpawnPlayer (_clientId, _name, _chunkViewDim, _playerProfile);
123 }
124 }
125 }
126
127 public static void CallPlayerDisconnected (ClientInfo _cInfo, bool _bShutdown) {
128 foreach (ModData md in loadedMods.Values) {
129 if (md.apiInstance != null) {
130 md.apiInstance.PlayerDisconnected (_cInfo, _bShutdown);
131 }
132 }
133 }
134
135 public static void CallCalcMapColors (Chunk _chunk) {
136 foreach (ModData md in loadedMods.Values) {
137 if (md.apiInstance != null) {
138 md.apiInstance.CalcMapColors (_chunk);
139 }
140 }
141 }
142
143
144 }
145}
Note: See TracBrowser for help on using the repository browser.