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

3
import java.io.*;
4
import java.lang.reflect.Type;
5
6
import java.nio.file.Files;
import java.nio.file.Path;
KingRainbow44's avatar
KingRainbow44 committed
7
import java.util.*;
Melledy's avatar
Melledy committed
8
9
10
11
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

12
import emu.grasscutter.data.binout.*;
13
import emu.grasscutter.game.world.SpawnDataEntry;
14
import emu.grasscutter.scripts.SceneIndexManager;
KingRainbow44's avatar
KingRainbow44 committed
15
import emu.grasscutter.utils.Utils;
16
import lombok.SneakyThrows;
Melledy's avatar
Melledy committed
17
18
import org.reflections.Reflections;

Yazawazi's avatar
Yazawazi committed
19
import com.google.gson.JsonElement;
20
import com.google.gson.annotations.SerializedName;
Melledy's avatar
Melledy committed
21
22
23
import com.google.gson.reflect.TypeToken;

import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
24
25
26
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
27
28
import emu.grasscutter.data.common.PointData;
import emu.grasscutter.data.common.ScenePointConfig;
29
import emu.grasscutter.game.world.SpawnDataEntry.*;
Melledy's avatar
Melledy committed
30
31
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

32
import static emu.grasscutter.config.Configuration.*;
33
import static emu.grasscutter.utils.Language.translate;
34

Melledy's avatar
Melledy committed
35
36
public class ResourceLoader {

37
	private static final List<String> loadedResources = new ArrayList<>();
38

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

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

		return classList;
	}
55

Melledy's avatar
Melledy committed
56
	public static void loadAll() {
57
58
        Grasscutter.getLogger().info(translate("messages.status.resources.loading"));

Melledy's avatar
Melledy committed
59
60
61
		// Load ability lists
		loadAbilityEmbryos();
		loadOpenConfig();
Melledy's avatar
Melledy committed
62
		loadAbilityModifiers();
Melledy's avatar
Melledy committed
63
64
65
		// Load resources
		loadResources();
		// Process into depots
66
		GameDepot.load();
Melledy's avatar
Melledy committed
67
		// Load spawn data and quests
Melledy's avatar
Melledy committed
68
		loadSpawnData();
Melledy's avatar
Melledy committed
69
		loadQuests();
70
71
		// Load scene points - must be done AFTER resources are loaded
		loadScenePoints();
72
73
		// Load default home layout
		loadHomeworldDefaultSaveData();
Akka's avatar
Akka committed
74
		loadNpcBornData();
75
76

        Grasscutter.getLogger().info(translate("messages.status.resources.finish"));
Melledy's avatar
Melledy committed
77
78
79
	}

	public static void loadResources() {
80
81
82
83
		loadResources(false);
	}

	public static void loadResources(boolean doReload) {
Melledy's avatar
Melledy committed
84
85
86
87
88
89
90
91
		for (Class<?> resourceDefinition : getResourceDefClasses()) {
			ResourceType type = resourceDefinition.getAnnotation(ResourceType.class);

			if (type == null) {
				continue;
			}

			@SuppressWarnings("rawtypes")
92
			Int2ObjectMap map = GameData.getMapByResourceDef(resourceDefinition);
Melledy's avatar
Melledy committed
93
94
95
96
97
98

			if (map == null) {
				continue;
			}

			try {
99
				loadFromResource(resourceDefinition, type, map, doReload);
Melledy's avatar
Melledy committed
100
			} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
101
				Grasscutter.getLogger().error("Error loading resource file: " + Arrays.toString(type.name()), e);
Melledy's avatar
Melledy committed
102
103
104
			}
		}
	}
105

Melledy's avatar
Melledy committed
106
	@SuppressWarnings("rawtypes")
107
108
109
110
111
112
	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);
			}
			loadedResources.add(c.getSimpleName());
113
            Grasscutter.getLogger().debug("Loaded " + map.size() + " " + c.getSimpleName() + "s.");
Melledy's avatar
Melledy committed
114
115
		}
	}
116

Melledy's avatar
Melledy committed
117
118
	@SuppressWarnings({"rawtypes", "unchecked"})
	protected static void loadFromResource(Class<?> c, String fileName, Int2ObjectMap map) throws Exception {
Melledy's avatar
Melledy committed
119
120
121
122
123
124
125
		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);
126
			}
Melledy's avatar
Melledy committed
127
128
129
		}
	}

Yazawazi's avatar
Yazawazi committed
130
131
	private static void loadScenePoints() {
		Pattern pattern = Pattern.compile("(?<=scene)(.*?)(?=_point.json)");
132
		File folder = new File(RESOURCE("BinOutput/Scene/Point"));
Yazawazi's avatar
Yazawazi committed
133
134
135
136
137
138

		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
139
		List<ScenePointEntry> scenePointList = new ArrayList<>();
140
		for (File file : Objects.requireNonNull(folder.listFiles())) {
141
			ScenePointConfig config; Integer sceneId;
142

Yazawazi's avatar
Yazawazi committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
			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
163
				pointData.setId(Integer.parseInt(entry.getKey()));
Yazawazi's avatar
Yazawazi committed
164
165
166

				ScenePointEntry sl = new ScenePointEntry(sceneId + "_" + entry.getKey(), pointData);
				scenePointList.add(sl);
167
				GameData.getScenePointIdList().add(pointData.getId());
168

169
				pointData.updateDailyDungeon();
Yazawazi's avatar
Yazawazi committed
170
171
172
			}

			for (ScenePointEntry entry : scenePointList) {
173
				GameData.getScenePointEntries().put(entry.getName(), entry);
Yazawazi's avatar
Yazawazi committed
174
175
176
177
			}
		}
	}

Melledy's avatar
Melledy committed
178
179
	private static void loadAbilityEmbryos() {
		List<AbilityEmbryoEntry> embryoList = null;
180
181

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

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

Melledy's avatar
Melledy committed
190
			embryoList = new LinkedList<>();
191
			File folder = new File(Utils.toFilePath(RESOURCE("BinOutput/Avatar/")));
KingRainbow44's avatar
KingRainbow44 committed
192
193
194
195
196
			File[] files = folder.listFiles();
			if(files == null) {
				Grasscutter.getLogger().error("Error loading ability embryos: no files found in " + folder.getAbsolutePath());
				return;
			}
197

KingRainbow44's avatar
KingRainbow44 committed
198
199
200
			for (File file : files) {
				AvatarConfig config;
				String avatarName;
201

Melledy's avatar
Melledy committed
202
203
204
205
206
207
				Matcher matcher = pattern.matcher(file.getName());
				if (matcher.find()) {
					avatarName = matcher.group(0);
				} else {
					continue;
				}
208

Melledy's avatar
Melledy committed
209
210
211
212
213
214
				try (FileReader fileReader = new FileReader(file)) {
					config = Grasscutter.getGsonFactory().fromJson(fileReader, AvatarConfig.class);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
215

Melledy's avatar
Melledy committed
216
217
218
				if (config.abilities == null) {
					continue;
				}
219

Melledy's avatar
Melledy committed
220
221
222
223
				int s = config.abilities.size();
				AbilityEmbryoEntry al = new AbilityEmbryoEntry(avatarName, config.abilities.stream().map(Object::toString).toArray(size -> new String[s]));
				embryoList.add(al);
			}
224

225
			File playerElementsFile = new File(Utils.toFilePath(RESOURCE("BinOutput/AbilityGroup/AbilityGroup_Other_PlayerElementAbility.json")));
226

227
228
229
230
231
232
233
			if (playerElementsFile.exists()) {
				try (FileReader fileReader = new FileReader(playerElementsFile)) {
					GameDepot.setPlayerAbilities(Grasscutter.getGsonFactory().fromJson(fileReader, new TypeToken<Map<String, AvatarConfig>>(){}.getType()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
Melledy's avatar
Melledy committed
234
		}
235

Melledy's avatar
Melledy committed
236
237
238
239
240
241
		if (embryoList == null || embryoList.isEmpty()) {
			Grasscutter.getLogger().error("No embryos loaded!");
			return;
		}

		for (AbilityEmbryoEntry entry : embryoList) {
242
			GameData.getAbilityEmbryoInfo().put(entry.getName(), entry);
Melledy's avatar
Melledy committed
243
244
		}
	}
245

Melledy's avatar
Melledy committed
246
247
	private static void loadAbilityModifiers() {
		// Load from BinOutput
248
		File folder = new File(Utils.toFilePath(RESOURCE("BinOutput/Ability/Temp/AvatarAbilities/")));
Melledy's avatar
Melledy committed
249
250
251
252
253
254
255
		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) {
256
			List<AbilityConfigData> abilityConfigList;
257

Melledy's avatar
Melledy committed
258
259
260
261
262
263
			try (FileReader fileReader = new FileReader(file)) {
				abilityConfigList = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, AbilityConfigData.class).getType());
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
264

Melledy's avatar
Melledy committed
265
266
267
268
			for (AbilityConfigData data : abilityConfigList) {
				if (data.Default.modifiers == null || data.Default.modifiers.size() == 0) {
					continue;
				}
269

Melledy's avatar
Melledy committed
270
				AbilityModifierEntry modifierEntry = new AbilityModifierEntry(data.Default.abilityName);
271

Melledy's avatar
Melledy committed
272
273
				for (Entry<String, AbilityModifier> entry : data.Default.modifiers.entrySet()) {
					AbilityModifier modifier = entry.getValue();
274

Melledy's avatar
Melledy committed
275
276
277
278
279
280
281
282
283
					// Stare.
					if (modifier.onAdded != null) {
						for (AbilityModifierAction action : modifier.onAdded) {
							if (action.$type.contains("HealHP")) {
								action.type = AbilityModifierActionType.HealHP;
								modifierEntry.getOnAdded().add(action);
							}
						}
					}
284

Melledy's avatar
Melledy committed
285
286
287
288
289
290
291
292
					if (modifier.onThinkInterval != null) {
						for (AbilityModifierAction action : modifier.onThinkInterval) {
							if (action.$type.contains("HealHP")) {
								action.type = AbilityModifierActionType.HealHP;
								modifierEntry.getOnThinkInterval().add(action);
							}
						}
					}
293

Melledy's avatar
Melledy committed
294
295
296
297
298
299
300
301
302
					if (modifier.onRemoved != null) {
						for (AbilityModifierAction action : modifier.onRemoved) {
							if (action.$type.contains("HealHP")) {
								action.type = AbilityModifierActionType.HealHP;
								modifierEntry.getOnRemoved().add(action);
							}
						}
					}
				}
303

Melledy's avatar
Melledy committed
304
305
306
307
				GameData.getAbilityModifiers().put(modifierEntry.getName(), modifierEntry);
			}
		}
	}
308

Melledy's avatar
Melledy committed
309
	private static void loadSpawnData() {
310
		String[] spawnDataNames = {"Spawns.json", "GadgetSpawns.json"};
311
312
313
314
		ArrayList<SpawnGroupEntry> spawnEntryMap = new ArrayList<>();

        for (String name : spawnDataNames) {
            // Load spawn entries from file
315
            try (InputStreamReader reader = DataLoader.loadReader(name)) {
316
                Type type = TypeToken.getParameterized(Collection.class, SpawnGroupEntry.class).getType();
317
                List<SpawnGroupEntry> list = Grasscutter.getGsonFactory().fromJson(reader, type);
318
319
320
321
322
323

                // Add spawns to group if it already exists in our spawn group map
                spawnEntryMap.addAll(list);
            } catch (Exception ignored) {}
        }

324
		if (spawnEntryMap.isEmpty()) {
Melledy's avatar
Melledy committed
325
326
327
328
			Grasscutter.getLogger().error("No spawn data loaded!");
			return;
		}

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
        HashMap<GridBlockId, ArrayList<SpawnDataEntry>> areaSort = new HashMap<>();
        //key = sceneId,x,z , value = ArrayList<SpawnDataEntry>
        for (SpawnGroupEntry entry : spawnEntryMap) {
            entry.getSpawns().forEach(
                s -> {
                    s.setGroup(entry);
                    GridBlockId point = s.getBlockId();
                    if(!areaSort.containsKey(point)) {
                        areaSort.put(point, new ArrayList<>());
                    }
                    areaSort.get(point).add(s);
                }
            );
        }
        GameDepot.addSpawnListById(areaSort);
Melledy's avatar
Melledy committed
344
	}
345

Melledy's avatar
Melledy committed
346
347
348
	private static void loadOpenConfig() {
		// Read from cached file if exists
		List<OpenConfigEntry> list = null;
349
350
351
352
353
354

		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
355
356
			Map<String, OpenConfigEntry> map = new TreeMap<>();
			java.lang.reflect.Type type = new TypeToken<Map<String, OpenConfigData[]>>() {}.getType();
ayy lmao's avatar
ayy lmao committed
357
			String[] folderNames = {"BinOutput/Talent/EquipTalents/", "BinOutput/Talent/AvatarTalents/"};
358

Melledy's avatar
Melledy committed
359
			for (String name : folderNames) {
360
				File folder = new File(Utils.toFilePath(RESOURCE(name)));
KingRainbow44's avatar
KingRainbow44 committed
361
362
363
364
				File[] files = folder.listFiles();
				if(files == null) {
					Grasscutter.getLogger().error("Error loading open config: no files found in " + folder.getAbsolutePath()); return;
				}
365

KingRainbow44's avatar
KingRainbow44 committed
366
				for (File file : files) {
Melledy's avatar
Melledy committed
367
368
369
					if (!file.getName().endsWith(".json")) {
						continue;
					}
370

KingRainbow44's avatar
KingRainbow44 committed
371
					Map<String, OpenConfigData[]> config;
372

Melledy's avatar
Melledy committed
373
374
375
376
377
378
					try (FileReader fileReader = new FileReader(file)) {
						config = Grasscutter.getGsonFactory().fromJson(fileReader, type);
					} catch (Exception e) {
						e.printStackTrace();
						continue;
					}
379

Melledy's avatar
Melledy committed
380
					for (Entry<String, OpenConfigData[]> e : config.entrySet()) {
381
						OpenConfigEntry entry = new OpenConfigEntry(e.getKey(), e.getValue());
Melledy's avatar
Melledy committed
382
383
384
385
						map.put(entry.getName(), entry);
					}
				}
			}
386

Melledy's avatar
Melledy committed
387
388
			list = new ArrayList<>(map.values());
		}
389

Melledy's avatar
Melledy committed
390
391
392
393
		if (list == null || list.isEmpty()) {
			Grasscutter.getLogger().error("No openconfig entries loaded!");
			return;
		}
394

Melledy's avatar
Melledy committed
395
		for (OpenConfigEntry entry : list) {
396
			GameData.getOpenConfigEntries().put(entry.getName(), entry);
Melledy's avatar
Melledy committed
397
398
		}
	}
399

Melledy's avatar
Melledy committed
400
	private static void loadQuests() {
Melledy's avatar
Melledy committed
401
		File folder = new File(RESOURCE("BinOutput/Quest/"));
402

Melledy's avatar
Melledy committed
403
404
405
		if (!folder.exists()) {
			return;
		}
406

Melledy's avatar
Melledy committed
407
		for (File file : folder.listFiles()) {
Melledy's avatar
Melledy committed
408
			MainQuestData mainQuest = null;
409

Melledy's avatar
Melledy committed
410
			try (FileReader fileReader = new FileReader(file)) {
Melledy's avatar
Melledy committed
411
				mainQuest = Grasscutter.getGsonFactory().fromJson(fileReader, MainQuestData.class);
Melledy's avatar
Melledy committed
412
413
414
415
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
416

Melledy's avatar
Melledy committed
417
			GameData.getMainQuestDataMap().put(mainQuest.getId(), mainQuest);
Melledy's avatar
Melledy committed
418
		}
419
420

		Grasscutter.getLogger().debug("Loaded " + GameData.getMainQuestDataMap().size() + " MainQuestDatas.");
Melledy's avatar
Melledy committed
421
	}
422

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
	@SneakyThrows
	private static void loadHomeworldDefaultSaveData(){
		var folder = Files.list(Path.of(RESOURCE("BinOutput/HomeworldDefaultSave"))).toList();
		var pattern = Pattern.compile("scene(.*)_home_config.json");

		for(var file : folder){
			var matcher = pattern.matcher(file.getFileName().toString());
			if(!matcher.find()){
				continue;
			}
			var sceneId = matcher.group(1);

			var data = Grasscutter.getGsonFactory().fromJson(Files.readString(file), HomeworldDefaultSaveData.class);

			GameData.getHomeworldDefaultSaveData().put(Integer.parseInt(sceneId), data);
		}

440
		Grasscutter.getLogger().debug("Loaded " + GameData.getHomeworldDefaultSaveData().size() + " HomeworldDefaultSaveDatas.");
441
442
	}

Akka's avatar
Akka committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
	@SneakyThrows
	private static void loadNpcBornData(){
		var folder = Files.list(Path.of(RESOURCE("BinOutput/Scene/SceneNpcBorn"))).toList();

		for(var file : folder){
			if(file.toFile().isDirectory()){
				continue;
			}

			var data = Grasscutter.getGsonFactory().fromJson(Files.readString(file), SceneNpcBornData.class);
			if(data.getBornPosList() == null || data.getBornPosList().size() == 0){
				continue;
			}

457
			data.setIndex(SceneIndexManager.buildIndex(3, data.getBornPosList(), item -> item.getPos().toPoint()));
Akka's avatar
Akka committed
458
459
460
			GameData.getSceneNpcBornData().put(data.getSceneId(), data);
		}

461
		Grasscutter.getLogger().debug("Loaded " + GameData.getSceneNpcBornData().size() + " SceneNpcBornDatas.");
Akka's avatar
Akka committed
462
	}
463

Melledy's avatar
Melledy committed
464
	// BinOutput configs
465

466
467
	public static class AvatarConfig {
		@SerializedName(value="abilities", alternate={"targetAbilities"})
Melledy's avatar
Melledy committed
468
		public ArrayList<AvatarConfigAbility> abilities;
469
	}
470

471
472
473
474
	public static class AvatarConfigAbility {
		public String abilityName;
		public String toString() {
			return abilityName;
Melledy's avatar
Melledy committed
475
476
		}
	}
477

Melledy's avatar
Melledy committed
478
479
480
	private static class OpenConfig {
		public OpenConfigData[] data;
	}
481

482
	public static class OpenConfigData {
Melledy's avatar
Melledy committed
483
484
		public String $type;
		public String abilityName;
485

486
		@SerializedName(value="talentIndex", alternate={"OJOFFKLNAHN"})
Melledy's avatar
Melledy committed
487
		public int talentIndex;
488

489
		@SerializedName(value="skillID", alternate={"overtime"})
490
		public int skillID;
491

492
		@SerializedName(value="pointDelta", alternate={"IGEBKIHPOIF"})
493
		public int pointDelta;
Melledy's avatar
Melledy committed
494
495
	}
}