Elin Decompiled Documentation EA 23.317 Nightly
Loading...
Searching...
No Matches
BaseModManager.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using UnityEngine;
6
7public class BaseModManager
8{
9 [Serializable]
10 public class BaseResource
11 {
12 public string resourcePath;
13
14 public List<string> files;
15
16 public void Parse<T>(ModItemList<T> list) where T : UnityEngine.Object
17 {
18 foreach (string file in files)
19 {
20 list.Add(null, resourcePath + "/" + file);
21 }
22 }
23 }
24
25 public static BaseModManager Instance;
26
27 public static string rootMod;
28
29 public static string rootDefaultPacakge;
30
31 public static bool isInitialized;
32
33 public static List<string> listChainLoad = new List<string>();
34
35 private static readonly Dictionary<string, HashSet<Action<object>>> _eventHandlers = new Dictionary<string, HashSet<Action<object>>>();
36
37 private static readonly Dictionary<(string, Delegate), Action<object>> _typedEventHandlers = new Dictionary<(string, Delegate), Action<object>>();
38
39 public DirectoryInfo dirWorkshop;
40
41 public int priorityIndex;
42
43 [NonSerialized]
44 public List<BaseModPackage> packages = new List<BaseModPackage>();
45
46 public virtual void Init(string path, string defaultPackage = "_Elona")
47 {
48 Debug.Log("Initializing ModManager:" + defaultPackage + "/" + path);
49 Instance = this;
50 rootMod = (rootDefaultPacakge = path);
51 _eventHandlers.Clear();
52 _typedEventHandlers.Clear();
53 if (!defaultPackage.IsEmpty())
54 {
55 rootDefaultPacakge = rootMod + defaultPackage + "/";
56 }
57 }
58
59 public void InitLang()
60 {
61 Debug.Log("Initializing Langs: " + Lang.langCode);
64 MOD.tones.Clear();
65 LangSetting langSetting = MOD.langs.TryGetValue(Lang.langCode);
66 if (langSetting == null)
67 {
68 Debug.LogError("Lang: " + Lang.langCode + " is not set");
69 langSetting = MOD.langs.FirstItem();
70 }
71 FileInfo[] files = new DirectoryInfo(langSetting.dir + "Data").GetFiles();
72 foreach (FileInfo fileInfo in files)
73 {
74 switch (fileInfo.Name)
75 {
76 case "Alias.xlsx":
77 Lang.alias = new ExcelData(fileInfo.FullName);
78 break;
79 case "Name.xlsx":
80 Lang.names = new ExcelData(fileInfo.FullName);
81 break;
82 case "chara_talk.xlsx":
83 MOD.listTalk.Add(new ExcelData(fileInfo.FullName));
84 break;
85 case "god_talk.xlsx":
86 MOD.listGodTalk.Add(new ExcelData(fileInfo.FullName));
87 break;
88 case "chara_tone.xlsx":
89 MOD.tones.Add(new ExcelData(fileInfo.FullName));
90 break;
91 }
92 }
93 }
94
95 public virtual void ParseExtra(DirectoryInfo dir, BaseModPackage package)
96 {
97 }
98
99 public static void SubscribeEvent(string eventId, Action<object> handler)
100 {
101 if (handler != null)
102 {
103 if (!_eventHandlers.TryGetValue(eventId, out var value))
104 {
105 value = new HashSet<Action<object>>();
106 _eventHandlers[eventId] = value;
107 }
108 value.Add(handler);
109 }
110 }
111
112 public static void SubscribeEvent(string eventId, Action handler)
113 {
114 if (handler != null)
115 {
116 Action<object> action = delegate
117 {
118 handler();
119 };
120 (string, Delegate) key = (eventId, handler);
121 _typedEventHandlers[key] = action;
122 SubscribeEvent(eventId, action);
123 }
124 }
125
126 public static void SubscribeEvent<T>(string eventId, Action<T> handler)
127 {
128 if (handler == null)
129 {
130 return;
131 }
132 Action<object> action = delegate(object data)
133 {
134 if (!(data is T obj))
135 {
136 if (data != null)
137 {
138 throw new InvalidCastException($"{handler.Method.DeclaringType?.Assembly.GetName()} " + "expects " + typeof(T).Name + ", got " + data.GetType().Name);
139 }
140 handler(default(T));
141 }
142 else
143 {
144 handler(obj);
145 }
146 };
147 (string, Delegate) key = (eventId, handler);
148 _typedEventHandlers[key] = action;
149 SubscribeEvent(eventId, action);
150 }
151
152 public static void UnsubscribeEvent(string eventId, Action<object> handler)
153 {
154 if (_eventHandlers.TryGetValue(eventId, out var value))
155 {
156 value.Remove(handler);
157 if (value.Count == 0)
158 {
159 _eventHandlers.Remove(eventId);
160 }
161 }
162 }
163
164 public static void UnsubscribeEvent(string eventId, Action handler)
165 {
166 if (handler != null)
167 {
168 (string, Delegate) key = (eventId, handler);
169 if (_typedEventHandlers.TryGetValue(key, out var value))
170 {
171 UnsubscribeEvent(eventId, value);
172 _typedEventHandlers.Remove(key);
173 }
174 }
175 }
176
177 public static void UnsubscribeEvent<T>(string eventId, Action<T> handler)
178 {
179 if (handler != null)
180 {
181 (string, Delegate) key = (eventId, handler);
182 if (_typedEventHandlers.TryGetValue(key, out var value))
183 {
184 UnsubscribeEvent(eventId, value);
185 _typedEventHandlers.Remove(key);
186 }
187 }
188 }
189
190 public static void PublishEvent(string eventId, object data = null)
191 {
192 if (!_eventHandlers.TryGetValue(eventId, out var value))
193 {
194 return;
195 }
196 foreach (Action<object> item in value.ToList())
197 {
198 try
199 {
200 item(data);
201 }
202 catch (Exception arg)
203 {
204 Debug.LogError($"#event '{eventId}' throws error in one of the handlers\n{arg}");
205 }
206 }
207 }
208
209 public static void PublishEvent<T>(string eventId, T data = default(T))
210 {
211 PublishEvent(eventId, (object)data);
212 }
213
214 public static bool HasEventSubscriber(string eventId)
215 {
216 int? num = _eventHandlers.GetValueOrDefault(eventId)?.Count;
217 if (num.HasValue)
218 {
219 return num.GetValueOrDefault() > 0;
220 }
221 return false;
222 }
223}
void Parse< T >(ModItemList< T > list)
List< BaseModPackage > packages
static readonly Dictionary< string, HashSet< Action< object > > > _eventHandlers
virtual void Init(string path, string defaultPackage="_Elona")
static void UnsubscribeEvent(string eventId, Action< object > handler)
static readonly Dictionary<(string, Delegate), Action< object > > _typedEventHandlers
static void UnsubscribeEvent< T >(string eventId, Action< T > handler)
static bool isInitialized
static string rootDefaultPacakge
virtual void ParseExtra(DirectoryInfo dir, BaseModPackage package)
static BaseModManager Instance
static string rootMod
static void PublishEvent(string eventId, object data=null)
static void SubscribeEvent(string eventId, Action handler)
static List< string > listChainLoad
static void UnsubscribeEvent(string eventId, Action handler)
static void SubscribeEvent(string eventId, Action< object > handler)
static bool HasEventSubscriber(string eventId)
DirectoryInfo dirWorkshop
static void SubscribeEvent< T >(string eventId, Action< T > handler)
static void PublishEvent< T >(string eventId, T data=default(T))
void Add(ExcelData data)
string dir
Definition: LangSetting.cs:28
Definition: Lang.cs:7
static string langCode
Definition: Lang.cs:29
Definition: MOD.cs:7
static TalkDataList listTalk
Definition: MOD.cs:10
static GodTalkDataList listGodTalk
Definition: MOD.cs:12
static ToneDataList tones
Definition: MOD.cs:14
static Dictionary< string, LangSetting > langs
Definition: MOD.cs:8