Elin Decompiled Documentation EA 23.286 Nightly Patch 1
Loading...
Searching...
No Matches
ModUtil.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.IO;
5using System.Linq;
6using System.Reflection;
7using System.Threading;
8using NPOI.SS.UserModel;
9using NPOI.XSSF.UserModel;
10using UnityEngine;
11using UnityEngine.Networking;
12
13public class ModUtil : EClass
14{
16
17 public static Dictionary<string, string> fallbackTypes = new Dictionary<string, string>();
18
19 public static IReadOnlyDictionary<string, SourceData> SourceMapping => (from f in typeof(SourceManager).GetFields()
20 where typeof(SourceData).IsAssignableFrom(f.FieldType)
21 select f).ToDictionary((FieldInfo f) => f.FieldType.Name, (FieldInfo f) => f.GetValue(EClass.sources) as SourceData);
22
23 public static void OnModsActivated()
24 {
25 SoundManager.current.soundLoaders.Add(LoadSoundData);
26 BaseModManager.PublishEvent("elin.mods.activated");
27 }
28
29 public static void LoadTypeFallback()
30 {
31 string text = "type_resolver.txt";
32 string[] array = new string[0];
33 if (File.Exists(CorePath.RootData + text))
34 {
35 array = IO.LoadTextArray(CorePath.RootData + text);
36 }
37 else
38 {
39 array = new string[2] { "TrueArena,ArenaWaveEvent,ZoneEvent", "Elin-GeneRecombinator,Elin_GeneRecombinator.IncubationSacrifice,Chara" };
40 IO.SaveTextArray(CorePath.RootData + text, array);
41 }
42 string[] array2 = array;
43 for (int i = 0; i < array2.Length; i++)
44 {
45 string[] array3 = array2[i].Split(',');
46 if (array3.Length >= 2)
47 {
48 RegisterSerializedTypeFallback(array3[0], array3[1], array3[2]);
49 }
50 }
51 }
52
53 public static void RegisterSerializedTypeFallback(string nameAssembly, string nameType, string nameFallbackType)
54 {
55 fallbackTypes[nameType] = nameFallbackType;
56 }
57
58 public static void ImportExcel(string pathToExcelFile, string sheetName, SourceData source)
59 {
60 UnityEngine.Debug.Log("ImportExcel source:" + source?.ToString() + " Path:" + pathToExcelFile);
61 using FileStream @is = File.Open(pathToExcelFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
62 XSSFWorkbook xSSFWorkbook = new XSSFWorkbook((Stream)@is);
63 for (int i = 0; i < xSSFWorkbook.NumberOfSheets; i++)
64 {
65 ISheet sheetAt = xSSFWorkbook.GetSheetAt(i);
66 if (sheetAt.SheetName != sheetName)
67 {
68 continue;
69 }
70 UnityEngine.Debug.Log("Importing Sheet:" + sheetName);
71 try
72 {
73 ExcelParser.path = pathToExcelFile;
74 if (!source.ImportData(sheetAt, new FileInfo(pathToExcelFile).Name, overwrite: true))
75 {
76 UnityEngine.Debug.LogError(ERROR.msg);
77 break;
78 }
79 UnityEngine.Debug.Log("Imported " + sheetAt.SheetName);
80 source.Reset();
81 }
82 catch (Exception ex)
83 {
84 UnityEngine.Debug.LogError("[Error] Skipping import " + sheetAt.SheetName + " :" + ex.Message + "/" + ex.Source + "/" + ex.StackTrace);
85 break;
86 }
87 }
88 }
89
91 {
92 return ModManagerCore.Instance.MappedPackages.Values.FirstOrDefault((EMod p) => p.sourceRows.Contains(row)) as ModPackage;
93 }
94
95 public static ModPackage GetModPackage(string modId)
96 {
97 return ModManagerCore.Instance.MappedPackages.GetValueOrDefault(modId) as ModPackage;
98 }
99
100 public static SerializableSoundData GetSoundMeta(string soundPath)
101 {
102 string path = Path.ChangeExtension(soundPath, ".json");
103 SerializableSoundData serializableSoundData;
104 if (File.Exists(path))
105 {
106 try
107 {
108 serializableSoundData = IO.LoadFile<SerializableSoundData>(path);
109 if (serializableSoundData.dataVersion == SerializableSoundData.SoundDataMetaVersion.V1)
110 {
111 return serializableSoundData;
112 }
113 }
114 catch
115 {
116 }
117 }
118 serializableSoundData = new SerializableSoundData();
119 if (soundPath.NormalizePath().Contains("/Sound/BGM/"))
120 {
121 serializableSoundData.type = SoundData.Type.BGM;
122 serializableSoundData.bgmDataOptional = new SerializableBGMData();
123 }
124 IO.SaveFile(path, serializableSoundData);
125 return serializableSoundData;
126 }
127
128 public static AudioType GetAudioType(string extension)
129 {
130 return extension.ToLowerInvariant().Trim() switch
131 {
132 ".acc" => AudioType.ACC,
133 ".mp3" => AudioType.MPEG,
134 ".ogg" => AudioType.OGGVORBIS,
135 ".wav" => AudioType.WAV,
136 _ => AudioType.UNKNOWN,
137 };
138 }
139
140 public static SoundData LoadSoundData(string soundId)
141 {
142 if (!MOD.sounds.TryGetValue(soundId, out var value) || !value.Exists)
143 {
144 return null;
145 }
146 return LoadSoundData(value);
147 }
148
149 public static SoundData LoadSoundData(FileInfo soundFile)
150 {
151 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(soundFile.FullName);
152 string fullName = soundFile.FullName;
153 AudioType audioType = GetAudioType(soundFile.Extension);
154 bool stream = fullName.NormalizePath().Contains("/BGM/") && audioType == AudioType.OGGVORBIS;
155 using UnityWebRequest unityWebRequest = AudioClipStream.GetAudioClip("file://" + fullName, audioType, compressed: false, stream);
156 unityWebRequest.SendWebRequest();
157 Stopwatch stopwatch = Stopwatch.StartNew();
158 while (!unityWebRequest.isDone && stopwatch.ElapsedMilliseconds < 5000)
159 {
160 Thread.Sleep(1);
161 }
162 if (unityWebRequest.result != UnityWebRequest.Result.Success)
163 {
164 UnityEngine.Debug.LogError("#sound '" + fileNameWithoutExtension + "' failed to load: " + unityWebRequest.error.IsEmpty("timeout"));
165 return null;
166 }
167 AudioClip content = DownloadHandlerAudioClip.GetContent(unityWebRequest);
168 int? num = content?.samples;
169 if (!num.HasValue || num.GetValueOrDefault() <= 0)
170 {
171 UnityEngine.Debug.LogError($"#sound '{fileNameWithoutExtension}' sample is null: {audioType}");
172 return null;
173 }
174 content.name = fileNameWithoutExtension;
175 SoundData soundData = GetSoundMeta(fullName).ToSoundData();
176 if (soundData is BGMData bGMData)
177 {
178 bGMData._name = Path.GetFileNameWithoutExtension(fullName);
179 if (bGMData.song == null)
180 {
181 bGMData.song = new BGMData.SongData();
182 bGMData.song.parts.Add(new BGMData.Part());
183 }
184 }
185 soundData.clip = content;
186 soundData.name = fileNameWithoutExtension;
187 UnityEngine.Debug.Log($"#sound '{fileNameWithoutExtension}' loaded: {audioType}/{content.length}s");
188 SoundManager.current.dictData[fileNameWithoutExtension] = soundData;
189 return soundData;
190 }
191}
$
Definition: ModManager.cs:85
static void PublishEvent(string eventId, object data=null)
static string RootData
Definition: CorePath.cs:208
Definition: EClass.cs:6
static SourceManager sources
Definition: EClass.cs:43
Definition: ERROR.cs:2
static string msg
Definition: ERROR.cs:3
Definition: MOD.cs:7
static Dictionary< string, FileInfo > sounds
Definition: MOD.cs:24
static SoundData LoadSoundData(string soundId)
Definition: ModUtil.cs:140
static IReadOnlyDictionary< string, SourceData > SourceMapping
Definition: ModUtil.cs:19
static ModPackage GetModPackage(string modId)
Definition: ModUtil.cs:95
static SourceImporter sourceImporter
Definition: ModUtil.cs:15
static AudioType GetAudioType(string extension)
Definition: ModUtil.cs:128
static SerializableSoundData GetSoundMeta(string soundPath)
Definition: ModUtil.cs:100
static void RegisterSerializedTypeFallback(string nameAssembly, string nameType, string nameFallbackType)
Definition: ModUtil.cs:53
static Dictionary< string, string > fallbackTypes
Definition: ModUtil.cs:17
static ModPackage FindSourceRowPackage(SourceData.BaseRow row)
Definition: ModUtil.cs:90
static SoundData LoadSoundData(FileInfo soundFile)
Definition: ModUtil.cs:149
static void OnModsActivated()
Definition: ModUtil.cs:23
static void LoadTypeFallback()
Definition: ModUtil.cs:29
static void ImportExcel(string pathToExcelFile, string sheetName, SourceData source)
Definition: ModUtil.cs:58
override bool ImportData(ISheet sheet, string bookname, bool overwrite=false)
Definition: SourceData.cs:136
override void Reset()
Definition: SourceData.cs:115