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

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

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

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

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

34
35
import static emu.grasscutter.Configuration.*;

Melledy's avatar
Melledy committed
36
37
38
39
public class ResourceLoader {

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

		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
50
		classList.sort((a, b) -> b.getAnnotation(ResourceType.class).loadPriority().value() - a.getAnnotation(ResourceType.class).loadPriority().value());
Melledy's avatar
Melledy committed
51
52
53
54
55
56
57
58

		return classList;
	}
	
	public static void loadAll() {
		// Load ability lists
		loadAbilityEmbryos();
		loadOpenConfig();
Melledy's avatar
Melledy committed
59
		loadAbilityModifiers();
Melledy's avatar
Melledy committed
60
61
62
		// Load resources
		loadResources();
		// Process into depots
63
		GameDepot.load();
Melledy's avatar
Melledy committed
64
		// Load spawn data and quests
Melledy's avatar
Melledy committed
65
		loadSpawnData();
Melledy's avatar
Melledy committed
66
		loadQuests();
67
68
		// Load scene points - must be done AFTER resources are loaded
		loadScenePoints();
Melledy's avatar
Melledy committed
69
70
		// Custom - TODO move this somewhere else
		try {
71
			GameData.getAvatarSkillDepotDataMap().get(504).setAbilities(
Melledy's avatar
Melledy committed
72
73
74
75
76
77
78
79
80
81
82
83
				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"
					}
			));
84
			GameData.getAvatarSkillDepotDataMap().get(704).setAbilities(
Melledy's avatar
Melledy committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
				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() {
		for (Class<?> resourceDefinition : getResourceDefClasses()) {
			ResourceType type = resourceDefinition.getAnnotation(ResourceType.class);

			if (type == null) {
				continue;
			}

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

			if (map == null) {
				continue;
			}

			try {
				loadFromResource(resourceDefinition, type, map);
			} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
120
				Grasscutter.getLogger().error("Error loading resource file: " + Arrays.toString(type.name()), e);
Melledy's avatar
Melledy committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
			}
		}
	}
	
	@SuppressWarnings("rawtypes")
	protected static void loadFromResource(Class<?> c, ResourceType type, Int2ObjectMap map) throws Exception {
		for (String name : type.name()) {
			loadFromResource(c, name, map);
		}
		Grasscutter.getLogger().info("Loaded " + map.size() + " " + c.getSimpleName() + "s.");
	}
	
	@SuppressWarnings({"rawtypes", "unchecked"})
	protected static void loadFromResource(Class<?> c, String fileName, Int2ObjectMap map) throws Exception {
135
		FileReader fileReader = new FileReader(RESOURCE("ExcelBinOutput/" + fileName));
136
137
138
139
140
141
142
143
		Gson gson = Grasscutter.getGsonFactory();
		List list = gson.fromJson(fileReader, List.class);

		for (Object o : list) {
			Map<String, Object> tempMap = Utils.switchPropertiesUpperLowerCase((Map<String, Object>) o, c);
			GameResource res = gson.fromJson(gson.toJson(tempMap), TypeToken.get(c).getType());
			res.onLoad();
			map.put(res.getId(), res);
Melledy's avatar
Melledy committed
144
145
146
		}
	}

Yazawazi's avatar
Yazawazi committed
147
148
	private static void loadScenePoints() {
		Pattern pattern = Pattern.compile("(?<=scene)(.*?)(?=_point.json)");
149
		File folder = new File(RESOURCE("BinOutput/Scene/Point"));
Yazawazi's avatar
Yazawazi committed
150
151
152
153
154
155

		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
156
		List<ScenePointEntry> scenePointList = new ArrayList<>();
157
		for (File file : Objects.requireNonNull(folder.listFiles())) {
158
			ScenePointConfig config; Integer sceneId;
Yazawazi's avatar
Yazawazi committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
			
			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
180
				pointData.setId(Integer.parseInt(entry.getKey()));
Yazawazi's avatar
Yazawazi committed
181
182
183

				ScenePointEntry sl = new ScenePointEntry(sceneId + "_" + entry.getKey(), pointData);
				scenePointList.add(sl);
184
185
186
				GameData.getScenePointIdList().add(pointData.getId());
				
				pointData.updateDailyDungeon();
Yazawazi's avatar
Yazawazi committed
187
188
189
			}

			for (ScenePointEntry entry : scenePointList) {
190
				GameData.getScenePointEntries().put(entry.getName(), entry);
Yazawazi's avatar
Yazawazi committed
191
192
193
194
			}
		}
	}

Melledy's avatar
Melledy committed
195
196
	private static void loadAbilityEmbryos() {
		// Read from cached file if exists
197
		File embryoCache = new File(DATA("AbilityEmbryos.json"));
Melledy's avatar
Melledy committed
198
199
200
201
202
203
204
205
206
207
208
209
		List<AbilityEmbryoEntry> embryoList = null;
		
		if (embryoCache.exists()) {
			// Load from cache
			try (FileReader fileReader = new FileReader(embryoCache)) {
				embryoList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, AbilityEmbryoEntry.class).getType());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			// Load from BinOutput
			Pattern pattern = Pattern.compile("(?<=ConfigAvatar_)(.*?)(?=.json)");
210

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

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

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

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

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

Melledy's avatar
Melledy committed
241
242
243
244
245
246
247
248
249
250
251
252
				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) {
253
			GameData.getAbilityEmbryoInfo().put(entry.getName(), entry);
Melledy's avatar
Melledy committed
254
255
256
		}
	}
	
Melledy's avatar
Melledy committed
257
258
	private static void loadAbilityModifiers() {
		// Load from BinOutput
259
		File folder = new File(Utils.toFilePath(RESOURCE("BinOutput/Ability/Temp/AvatarAbilities/")));
Melledy's avatar
Melledy committed
260
261
262
263
264
265
266
		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) {
267
			List<AbilityConfigData> abilityConfigList;
Melledy's avatar
Melledy committed
268
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
			
			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
320
321
	private static void loadSpawnData() {
		// Read from cached file if exists
322
		File spawnDataEntries = new File(DATA("Spawns.json"));
Melledy's avatar
Melledy committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
		List<SpawnGroupEntry> spawnEntryList = null;
		
		if (spawnDataEntries.exists()) {
			// Load from cache
			try (FileReader fileReader = new FileReader(spawnDataEntries)) {
				spawnEntryList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, SpawnGroupEntry.class).getType());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (spawnEntryList == null || spawnEntryList.isEmpty()) {
			Grasscutter.getLogger().error("No spawn data loaded!");
			return;
		}

		for (SpawnGroupEntry entry : spawnEntryList) {
340
			entry.getSpawns().forEach(s -> s.setGroup(entry));
341
			GameDepot.getSpawnListById(entry.getSceneId()).insert(entry, entry.getPos().getX(), entry.getPos().getZ());
Melledy's avatar
Melledy committed
342
343
344
		}
	}
	
Melledy's avatar
Melledy committed
345
346
	private static void loadOpenConfig() {
		// Read from cached file if exists
347
		File openConfigCache = new File(DATA("OpenConfig.json"));
Melledy's avatar
Melledy committed
348
349
350
351
352
353
354
355
356
357
358
		List<OpenConfigEntry> list = null;
		
		if (openConfigCache.exists()) {
			try (FileReader fileReader = new FileReader(openConfigCache)) {
				list = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, OpenConfigEntry.class).getType());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			Map<String, OpenConfigEntry> map = new TreeMap<>();
			java.lang.reflect.Type type = new TypeToken<Map<String, OpenConfigData[]>>() {}.getType();
ayy lmao's avatar
ayy lmao committed
359
			String[] folderNames = {"BinOutput/Talent/EquipTalents/", "BinOutput/Talent/AvatarTalents/"};
Melledy's avatar
Melledy committed
360
361
			
			for (String name : folderNames) {
362
				File folder = new File(Utils.toFilePath(RESOURCE(name)));
KingRainbow44's avatar
KingRainbow44 committed
363
364
365
366
				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
367
				
KingRainbow44's avatar
KingRainbow44 committed
368
				for (File file : files) {
Melledy's avatar
Melledy committed
369
370
371
372
					if (!file.getName().endsWith(".json")) {
						continue;
					}
					
KingRainbow44's avatar
KingRainbow44 committed
373
					Map<String, OpenConfigData[]> config;
Melledy's avatar
Melledy committed
374
375
376
377
378
379
380
381
382
					
					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()) {
383
						OpenConfigEntry entry = new OpenConfigEntry(e.getKey(), e.getValue());
Melledy's avatar
Melledy committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
						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) {
398
			GameData.getOpenConfigEntries().put(entry.getName(), entry);
Melledy's avatar
Melledy committed
399
400
		}
	}
Melledy's avatar
Melledy committed
401
402
	
	private static void loadQuests() {
Melledy's avatar
Melledy committed
403
		File folder = new File(RESOURCE("BinOutput/Quest/"));
Melledy's avatar
Melledy committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
		
		if (!folder.exists()) {
			return;
		}
		
		for (File file : folder.listFiles()) {
			QuestConfigData mainQuest = null;
			
			try (FileReader fileReader = new FileReader(file)) {
				mainQuest = Grasscutter.getGsonFactory().fromJson(fileReader, QuestConfigData.class);
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
			
			if (mainQuest.getSubQuests() != null) {
				for (SubQuestConfigData subQuest : mainQuest.getSubQuests()) {
					QuestConfig quest = new QuestConfig(mainQuest, subQuest);
					GameData.getQuestConfigs().put(quest.getId(), quest);
				}
			}
		}
		
		Grasscutter.getLogger().info("Loaded " + GameData.getQuestConfigs().size() + " Quest Configs");
	}
429

Melledy's avatar
Melledy committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
	// 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;
	}
	
447
	public static class OpenConfigData {
Melledy's avatar
Melledy committed
448
449
450
		public String $type;
		public String abilityName;
		public int talentIndex;
451
452
		public int skillID;
		public int pointDelta;
Melledy's avatar
Melledy committed
453
454
	}
}