ResourceLoader.java 13.7 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.data;

3
import java.io.*;
KingRainbow44's avatar
KingRainbow44 committed
4
import java.util.*;
Melledy's avatar
Melledy committed
5
6
7
8
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

9
import com.google.gson.Gson;
KingRainbow44's avatar
KingRainbow44 committed
10
import emu.grasscutter.utils.Utils;
Melledy's avatar
Melledy committed
11
12
import org.reflections.Reflections;

Yazawazi's avatar
Yazawazi committed
13
import com.google.gson.JsonElement;
Melledy's avatar
Melledy committed
14
15
16
import com.google.gson.reflect.TypeToken;

import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
17
18
19
20
21
22
23
24
25
import emu.grasscutter.data.binout.AbilityEmbryoEntry;
import emu.grasscutter.data.binout.AbilityModifier;
import emu.grasscutter.data.binout.AbilityModifierEntry;
import emu.grasscutter.data.binout.MainQuestData;
import emu.grasscutter.data.binout.OpenConfigEntry;
import emu.grasscutter.data.binout.ScenePointEntry;
import emu.grasscutter.data.binout.AbilityModifier.AbilityConfigData;
import emu.grasscutter.data.binout.AbilityModifier.AbilityModifierAction;
import emu.grasscutter.data.binout.AbilityModifier.AbilityModifierActionType;
Yazawazi's avatar
Yazawazi committed
26
27
import emu.grasscutter.data.common.PointData;
import emu.grasscutter.data.common.ScenePointConfig;
28
import emu.grasscutter.game.world.SpawnDataEntry.*;
Melledy's avatar
Melledy committed
29
30
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

31
32
import static emu.grasscutter.Configuration.*;

Melledy's avatar
Melledy committed
33
34
public class ResourceLoader {

35
36
	private static List<String> loadedResources = new ArrayList<String>();

Melledy's avatar
Melledy committed
37
38
	public static List<Class<?>> getResourceDefClasses() {
		Reflections reflections = new Reflections(ResourceLoader.class.getPackage().getName());
39
		Set<?> classes = reflections.getSubTypesOf(GameResource.class);
Melledy's avatar
Melledy committed
40
41
42
43
44
45
46
47
48

		List<Class<?>> classList = new ArrayList<>(classes.size());
		classes.forEach(o -> {
			Class<?> c = (Class<?>) o;
			if (c.getAnnotation(ResourceType.class) != null) {
				classList.add(c);
			}
		});

KingRainbow44's avatar
KingRainbow44 committed
49
		classList.sort((a, b) -> b.getAnnotation(ResourceType.class).loadPriority().value() - a.getAnnotation(ResourceType.class).loadPriority().value());
Melledy's avatar
Melledy committed
50
51
52
53
54
55
56
57

		return classList;
	}
	
	public static void loadAll() {
		// Load ability lists
		loadAbilityEmbryos();
		loadOpenConfig();
Melledy's avatar
Melledy committed
58
		loadAbilityModifiers();
Melledy's avatar
Melledy committed
59
60
61
		// Load resources
		loadResources();
		// Process into depots
62
		GameDepot.load();
Melledy's avatar
Melledy committed
63
		// Load spawn data and quests
Melledy's avatar
Melledy committed
64
		loadSpawnData();
Melledy's avatar
Melledy committed
65
		loadQuests();
66
67
		// Load scene points - must be done AFTER resources are loaded
		loadScenePoints();
Melledy's avatar
Melledy committed
68
69
		// Custom - TODO move this somewhere else
		try {
70
			GameData.getAvatarSkillDepotDataMap().get(504).setAbilities(
Melledy's avatar
Melledy committed
71
72
73
74
75
76
77
78
79
80
81
82
				new AbilityEmbryoEntry(
					"", 
					new String[] {
						"Avatar_PlayerBoy_ExtraAttack_Wind",
						"Avatar_Player_UziExplode_Mix",
						"Avatar_Player_UziExplode",
						"Avatar_Player_UziExplode_Strike_01",
						"Avatar_Player_UziExplode_Strike_02",
						"Avatar_Player_WindBreathe",
						"Avatar_Player_WindBreathe_CameraController"
					}
			));
83
			GameData.getAvatarSkillDepotDataMap().get(704).setAbilities(
Melledy's avatar
Melledy committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
				new AbilityEmbryoEntry(
					"", 
					new String[] {
						"Avatar_PlayerGirl_ExtraAttack_Wind",
						"Avatar_Player_UziExplode_Mix",
						"Avatar_Player_UziExplode",
						"Avatar_Player_UziExplode_Strike_01",
						"Avatar_Player_UziExplode_Strike_02",
						"Avatar_Player_WindBreathe",
						"Avatar_Player_WindBreathe_CameraController"
					}
			));
		} catch (Exception e) {
			Grasscutter.getLogger().error("Error loading abilities", e);
		}
	}

	public static void loadResources() {
102
103
104
105
		loadResources(false);
	}

	public static void loadResources(boolean doReload) {
Melledy's avatar
Melledy committed
106
107
108
109
110
111
112
113
		for (Class<?> resourceDefinition : getResourceDefClasses()) {
			ResourceType type = resourceDefinition.getAnnotation(ResourceType.class);

			if (type == null) {
				continue;
			}

			@SuppressWarnings("rawtypes")
114
			Int2ObjectMap map = GameData.getMapByResourceDef(resourceDefinition);
Melledy's avatar
Melledy committed
115
116
117
118
119
120

			if (map == null) {
				continue;
			}

			try {
121
				loadFromResource(resourceDefinition, type, map, doReload);
Melledy's avatar
Melledy committed
122
			} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
123
				Grasscutter.getLogger().error("Error loading resource file: " + Arrays.toString(type.name()), e);
Melledy's avatar
Melledy committed
124
125
126
127
128
			}
		}
	}
	
	@SuppressWarnings("rawtypes")
129
130
131
132
133
134
135
	protected static void loadFromResource(Class<?> c, ResourceType type, Int2ObjectMap map, boolean doReload) throws Exception {
		if(!loadedResources.contains(c.getSimpleName()) || doReload) {
			for (String name : type.name()) {
				loadFromResource(c, name, map);
			}
			Grasscutter.getLogger().info("Loaded " + map.size() + " " + c.getSimpleName() + "s.");
			loadedResources.add(c.getSimpleName());
Melledy's avatar
Melledy committed
136
137
		}
	}
138

Melledy's avatar
Melledy committed
139
140
	@SuppressWarnings({"rawtypes", "unchecked"})
	protected static void loadFromResource(Class<?> c, String fileName, Int2ObjectMap map) throws Exception {
Melledy's avatar
Melledy committed
141
142
143
144
145
146
147
		try (FileReader fileReader = new FileReader(RESOURCE("ExcelBinOutput/" + fileName))) {
			List list = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, c).getType());

			for (Object o : list) {
				GameResource res = (GameResource) o;
				res.onLoad();
				map.put(res.getId(), res);
148
			}
Melledy's avatar
Melledy committed
149
150
151
		}
	}

Yazawazi's avatar
Yazawazi committed
152
153
	private static void loadScenePoints() {
		Pattern pattern = Pattern.compile("(?<=scene)(.*?)(?=_point.json)");
154
		File folder = new File(RESOURCE("BinOutput/Scene/Point"));
Yazawazi's avatar
Yazawazi committed
155
156
157
158
159
160

		if (!folder.isDirectory() || !folder.exists() || folder.listFiles() == null) {
			Grasscutter.getLogger().error("Scene point files cannot be found, you cannot use teleport waypoints!");
			return;
		}

Yazawazi's avatar
Yazawazi committed
161
		List<ScenePointEntry> scenePointList = new ArrayList<>();
162
		for (File file : Objects.requireNonNull(folder.listFiles())) {
163
			ScenePointConfig config; Integer sceneId;
Yazawazi's avatar
Yazawazi committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
			
			Matcher matcher = pattern.matcher(file.getName());
			if (matcher.find()) {
				sceneId = Integer.parseInt(matcher.group(1));
			} else {
				continue;
			}

			try (FileReader fileReader = new FileReader(file)) {
				config = Grasscutter.getGsonFactory().fromJson(fileReader, ScenePointConfig.class);
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}

			if (config.points == null) {
				continue;
			}

			for (Map.Entry<String, JsonElement> entry : config.points.entrySet()) {
				PointData pointData = Grasscutter.getGsonFactory().fromJson(entry.getValue(), PointData.class);
Melledy's avatar
Melledy committed
185
				pointData.setId(Integer.parseInt(entry.getKey()));
Yazawazi's avatar
Yazawazi committed
186
187
188

				ScenePointEntry sl = new ScenePointEntry(sceneId + "_" + entry.getKey(), pointData);
				scenePointList.add(sl);
189
190
191
				GameData.getScenePointIdList().add(pointData.getId());
				
				pointData.updateDailyDungeon();
Yazawazi's avatar
Yazawazi committed
192
193
194
			}

			for (ScenePointEntry entry : scenePointList) {
195
				GameData.getScenePointEntries().put(entry.getName(), entry);
Yazawazi's avatar
Yazawazi committed
196
197
198
199
			}
		}
	}

Melledy's avatar
Melledy committed
200
201
	private static void loadAbilityEmbryos() {
		List<AbilityEmbryoEntry> embryoList = null;
202
203
204
205
206
207
208

		// Read from cached file if exists
		try(InputStream embryoCache = DataLoader.load("AbilityEmbryos.json", false)) {
			embryoList = Grasscutter.getGsonFactory().fromJson(new InputStreamReader(embryoCache), TypeToken.getParameterized(Collection.class, AbilityEmbryoEntry.class).getType());
		} catch(Exception ignored) {}

		if(embryoList == null) {
Melledy's avatar
Melledy committed
209
210
			// Load from BinOutput
			Pattern pattern = Pattern.compile("(?<=ConfigAvatar_)(.*?)(?=.json)");
211

Melledy's avatar
Melledy committed
212
			embryoList = new LinkedList<>();
213
			File folder = new File(Utils.toFilePath(RESOURCE("BinOutput/Avatar/")));
KingRainbow44's avatar
KingRainbow44 committed
214
215
216
217
218
			File[] files = folder.listFiles();
			if(files == null) {
				Grasscutter.getLogger().error("Error loading ability embryos: no files found in " + folder.getAbsolutePath());
				return;
			}
219

KingRainbow44's avatar
KingRainbow44 committed
220
221
222
			for (File file : files) {
				AvatarConfig config;
				String avatarName;
223

Melledy's avatar
Melledy committed
224
225
226
227
228
229
				Matcher matcher = pattern.matcher(file.getName());
				if (matcher.find()) {
					avatarName = matcher.group(0);
				} else {
					continue;
				}
230

Melledy's avatar
Melledy committed
231
232
233
234
235
236
				try (FileReader fileReader = new FileReader(file)) {
					config = Grasscutter.getGsonFactory().fromJson(fileReader, AvatarConfig.class);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
237

Melledy's avatar
Melledy committed
238
239
240
				if (config.abilities == null) {
					continue;
				}
241

Melledy's avatar
Melledy committed
242
243
244
245
246
247
248
249
250
251
252
253
				int s = config.abilities.size();
				AbilityEmbryoEntry al = new AbilityEmbryoEntry(avatarName, config.abilities.stream().map(Object::toString).toArray(size -> new String[s]));
				embryoList.add(al);
			}
		}
		
		if (embryoList == null || embryoList.isEmpty()) {
			Grasscutter.getLogger().error("No embryos loaded!");
			return;
		}

		for (AbilityEmbryoEntry entry : embryoList) {
254
			GameData.getAbilityEmbryoInfo().put(entry.getName(), entry);
Melledy's avatar
Melledy committed
255
256
257
		}
	}
	
Melledy's avatar
Melledy committed
258
259
	private static void loadAbilityModifiers() {
		// Load from BinOutput
260
		File folder = new File(Utils.toFilePath(RESOURCE("BinOutput/Ability/Temp/AvatarAbilities/")));
Melledy's avatar
Melledy committed
261
262
263
264
265
266
267
		File[] files = folder.listFiles();
		if (files == null) {
			Grasscutter.getLogger().error("Error loading ability modifiers: no files found in " + folder.getAbsolutePath());
			return;
		}

		for (File file : files) {
268
			List<AbilityConfigData> abilityConfigList;
Melledy's avatar
Melledy committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
			
			try (FileReader fileReader = new FileReader(file)) {
				abilityConfigList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, AbilityConfigData.class).getType());
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
			
			for (AbilityConfigData data : abilityConfigList) {
				if (data.Default.modifiers == null || data.Default.modifiers.size() == 0) {
					continue;
				}
				
				AbilityModifierEntry modifierEntry = new AbilityModifierEntry(data.Default.abilityName);
				
				for (Entry<String, AbilityModifier> entry : data.Default.modifiers.entrySet()) {
					AbilityModifier modifier = entry.getValue();
					
					// Stare.
					if (modifier.onAdded != null) {
						for (AbilityModifierAction action : modifier.onAdded) {
							if (action.$type.contains("HealHP")) {
								action.type = AbilityModifierActionType.HealHP;
								modifierEntry.getOnAdded().add(action);
							}
						}
					}
					
					if (modifier.onThinkInterval != null) {
						for (AbilityModifierAction action : modifier.onThinkInterval) {
							if (action.$type.contains("HealHP")) {
								action.type = AbilityModifierActionType.HealHP;
								modifierEntry.getOnThinkInterval().add(action);
							}
						}
					}
					
					if (modifier.onRemoved != null) {
						for (AbilityModifierAction action : modifier.onRemoved) {
							if (action.$type.contains("HealHP")) {
								action.type = AbilityModifierActionType.HealHP;
								modifierEntry.getOnRemoved().add(action);
							}
						}
					}
				}
				
				GameData.getAbilityModifiers().put(modifierEntry.getName(), modifierEntry);
			}
		}
	}
	
Melledy's avatar
Melledy committed
321
322
	private static void loadSpawnData() {
		List<SpawnGroupEntry> spawnEntryList = null;
323
324
325
326
327

		// Read from cached file if exists
		try(InputStream spawnDataEntries = DataLoader.load("Spawns.json")) {
			spawnEntryList = Grasscutter.getGsonFactory().fromJson(new InputStreamReader(spawnDataEntries), TypeToken.getParameterized(Collection.class, SpawnGroupEntry.class).getType());
		} catch (Exception ignored) {}
Melledy's avatar
Melledy committed
328
329
330
331
332
333
334
		
		if (spawnEntryList == null || spawnEntryList.isEmpty()) {
			Grasscutter.getLogger().error("No spawn data loaded!");
			return;
		}

		for (SpawnGroupEntry entry : spawnEntryList) {
335
			entry.getSpawns().forEach(s -> s.setGroup(entry));
336
			GameDepot.getSpawnListById(entry.getSceneId()).insert(entry, entry.getPos().getX(), entry.getPos().getZ());
Melledy's avatar
Melledy committed
337
338
339
		}
	}
	
Melledy's avatar
Melledy committed
340
341
342
	private static void loadOpenConfig() {
		// Read from cached file if exists
		List<OpenConfigEntry> list = null;
343
344
345
346
347
348

		try(InputStream openConfigCache = DataLoader.load("OpenConfig.json", false)) {
			list = Grasscutter.getGsonFactory().fromJson(new InputStreamReader(openConfigCache), TypeToken.getParameterized(Collection.class, SpawnGroupEntry.class).getType());
		} catch (Exception ignored) {}

		if (list == null) {
Melledy's avatar
Melledy committed
349
350
			Map<String, OpenConfigEntry> map = new TreeMap<>();
			java.lang.reflect.Type type = new TypeToken<Map<String, OpenConfigData[]>>() {}.getType();
ayy lmao's avatar
ayy lmao committed
351
			String[] folderNames = {"BinOutput/Talent/EquipTalents/", "BinOutput/Talent/AvatarTalents/"};
Melledy's avatar
Melledy committed
352
353
			
			for (String name : folderNames) {
354
				File folder = new File(Utils.toFilePath(RESOURCE(name)));
KingRainbow44's avatar
KingRainbow44 committed
355
356
357
358
				File[] files = folder.listFiles();
				if(files == null) {
					Grasscutter.getLogger().error("Error loading open config: no files found in " + folder.getAbsolutePath()); return;
				}
Melledy's avatar
Melledy committed
359
				
KingRainbow44's avatar
KingRainbow44 committed
360
				for (File file : files) {
Melledy's avatar
Melledy committed
361
362
363
364
					if (!file.getName().endsWith(".json")) {
						continue;
					}
					
KingRainbow44's avatar
KingRainbow44 committed
365
					Map<String, OpenConfigData[]> config;
Melledy's avatar
Melledy committed
366
367
368
369
370
371
372
373
374
					
					try (FileReader fileReader = new FileReader(file)) {
						config = Grasscutter.getGsonFactory().fromJson(fileReader, type);
					} catch (Exception e) {
						e.printStackTrace();
						continue;
					}
					
					for (Entry<String, OpenConfigData[]> e : config.entrySet()) {
375
						OpenConfigEntry entry = new OpenConfigEntry(e.getKey(), e.getValue());
Melledy's avatar
Melledy committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
						map.put(entry.getName(), entry);
					}
				}
			}
			
			list = new ArrayList<>(map.values());
		}
		
		if (list == null || list.isEmpty()) {
			Grasscutter.getLogger().error("No openconfig entries loaded!");
			return;
		}
		
		for (OpenConfigEntry entry : list) {
390
			GameData.getOpenConfigEntries().put(entry.getName(), entry);
Melledy's avatar
Melledy committed
391
392
		}
	}
Melledy's avatar
Melledy committed
393
394
	
	private static void loadQuests() {
Melledy's avatar
Melledy committed
395
		File folder = new File(RESOURCE("BinOutput/Quest/"));
Melledy's avatar
Melledy committed
396
397
398
399
400
401
		
		if (!folder.exists()) {
			return;
		}
		
		for (File file : folder.listFiles()) {
Melledy's avatar
Melledy committed
402
			MainQuestData mainQuest = null;
Melledy's avatar
Melledy committed
403
404
			
			try (FileReader fileReader = new FileReader(file)) {
Melledy's avatar
Melledy committed
405
				mainQuest = Grasscutter.getGsonFactory().fromJson(fileReader, MainQuestData.class);
Melledy's avatar
Melledy committed
406
407
408
409
410
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
			
Melledy's avatar
Melledy committed
411
			GameData.getMainQuestDataMap().put(mainQuest.getId(), mainQuest);
Melledy's avatar
Melledy committed
412
413
		}
		
Melledy's avatar
Melledy committed
414
		Grasscutter.getLogger().info("Loaded " + GameData.getMainQuestDataMap().size() + " MainQuestDatas.");
Melledy's avatar
Melledy committed
415
	}
416

Melledy's avatar
Melledy committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
	// BinOutput configs
	
	private static class AvatarConfig {
		public ArrayList<AvatarConfigAbility> abilities;
		
		private static class AvatarConfigAbility {
			public String abilityName;
			public String toString() {
				return abilityName;
			}
		}
	}
	
	private static class OpenConfig {
		public OpenConfigData[] data;
	}
	
434
	public static class OpenConfigData {
Melledy's avatar
Melledy committed
435
436
437
		public String $type;
		public String abilityName;
		public int talentIndex;
438
439
		public int skillID;
		public int pointDelta;
Melledy's avatar
Melledy committed
440
441
	}
}