Elin Decompiled Documentation EA 23.331 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);
66 MOD.tones.Clear();
67 LangSetting langSetting = MOD.langs.TryGetValue(Lang.langCode);
68 if (langSetting == null)
69 {
70 Debug.LogError("Lang: " + Lang.langCode + " is not set");
71 langSetting = MOD.langs.FirstItem();
72 }
73 FileInfo[] files = new DirectoryInfo(langSetting.dir + "Data").GetFiles();
74 foreach (FileInfo fileInfo in files)
75 {
76 switch (fileInfo.Name)
77 {
78 case "Alias.xlsx":
79 MOD.listAlias.Add(new ExcelData(fileInfo.FullName));
80 break;
81 case "Name.xlsx":
82 MOD.listName.Add(new ExcelData(fileInfo.FullName));
83 break;
84 case "chara_talk.xlsx":
85 MOD.listTalk.Add(new ExcelData(fileInfo.FullName));
86 break;
87 case "god_talk.xlsx":
88 MOD.listGodTalk.Add(new ExcelData(fileInfo.FullName));
89 break;
90 case "chara_tone.xlsx":
91 MOD.tones.Add(new ExcelData(fileInfo.FullName));
92 break;
93 }
94 }
95 }
96
97 public virtual void ParseExtra(DirectoryInfo dir, BaseModPackage package)
98 {
99 }
100
101 public static void SubscribeEvent(string eventId, Action<object> handler)
102 {
103 if (handler != null)
104 {
105 if (!_eventHandlers.TryGetValue(eventId, out var value))
106 {
107 value = new HashSet<Action<object>>();
108 _eventHandlers[eventId] = value;
109 }
110 value.Add(handler);
111 }
112 }
113
114 public static void SubscribeEvent(string eventId, Action handler)
115 {
116 if (handler != null)
117 {
118 Action<object> action = delegate
119 {
120 handler();
121 };
122 (string, Delegate) key = (eventId, handler);
123 _typedEventHandlers[key] = action;
124 SubscribeEvent(eventId, action);
125 }
126 }
127
128 public static void SubscribeEvent<T>(string eventId, Action<T> handler)
129 {
130 if (handler == null)
131 {
132 return;
133 }
134 Action<object> action = delegate(object data)
135 {
136 if (!(data is T obj))
137 {
138 if (data != null)
139 {
140 throw new InvalidCastException($"{handler.Method.DeclaringType?.Assembly.GetName()} " + "expects " + typeof(T).Name + ", got " + data.GetType().Name);
141 }
142 handler(default(T));
143 }
144 else
145 {
146 handler(obj);
147 }
148 };
149 (string, Delegate) key = (eventId, handler);
150 _typedEventHandlers[key] = action;
151 SubscribeEvent(eventId, action);
152 }
153
154 public static void UnsubscribeEvent(string eventId, Action<object> handler)
155 {
156 if (_eventHandlers.TryGetValue(eventId, out var value))
157 {
158 value.Remove(handler);
159 if (value.Count == 0)
160 {
161 _eventHandlers.Remove(eventId);
162 }
163 }
164 }
165
166 public static void UnsubscribeEvent(string eventId, Action handler)
167 {
168 if (handler != null)
169 {
170 (string, Delegate) key = (eventId, handler);
171 if (_typedEventHandlers.TryGetValue(key, out var value))
172 {
173 UnsubscribeEvent(eventId, value);
174 _typedEventHandlers.Remove(key);
175 }
176 }
177 }
178
179 public static void UnsubscribeEvent<T>(string eventId, Action<T> handler)
180 {
181 if (handler != null)
182 {
183 (string, Delegate) key = (eventId, handler);
184 if (_typedEventHandlers.TryGetValue(key, out var value))
185 {
186 UnsubscribeEvent(eventId, value);
187 _typedEventHandlers.Remove(key);
188 }
189 }
190 }
191
192 public static void PublishEvent(string eventId, object data = null)
193 {
194 if (!_eventHandlers.TryGetValue(eventId, out var value))
195 {
196 return;
197 }
198 foreach (Action<object> item in value.ToList())
199 {
200 try
201 {
202 item(data);
203 }
204 catch (Exception arg)
205 {
206 Debug.LogError($"#event '{eventId}' throws error in one of the handlers\n{arg}");
207 }
208 }
209 }
210
211 public static void PublishEvent<T>(string eventId, T data = default(T))
212 {
213 PublishEvent(eventId, (object)data);
214 }
215
216 public static bool HasEventSubscriber(string eventId)
217 {
218 int? num = _eventHandlers.GetValueOrDefault(eventId)?.Count;
219 if (num.HasValue)
220 {
221 return num.GetValueOrDefault() > 0;
222 }
223 return false;
224 }
225}
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:14
static GodTalkDataList listGodTalk
Definition: MOD.cs:16
static ToneDataList tones
Definition: MOD.cs:18
static ExcelDataList listAlias
Definition: MOD.cs:10
static Dictionary< string, LangSetting > langs
Definition: MOD.cs:8
static ExcelDataList listName
Definition: MOD.cs:12