ResourceLoader.java 11 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
21
import emu.grasscutter.data.custom.AbilityEmbryoEntry;
import emu.grasscutter.data.custom.OpenConfigEntry;
Yazawazi's avatar
Yazawazi committed
22
import emu.grasscutter.data.custom.ScenePointEntry;
Melledy's avatar
Melledy committed
23
24
import emu.grasscutter.game.world.SpawnDataEntry;
import emu.grasscutter.game.world.SpawnDataEntry.SpawnGroupEntry;
Melledy's avatar
Melledy committed
25
26
27
28
29
30
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

public class ResourceLoader {

	public static List<Class<?>> getResourceDefClasses() {
		Reflections reflections = new Reflections(ResourceLoader.class.getPackage().getName());
31
		Set<?> classes = reflections.getSubTypesOf(GameResource.class);
Melledy's avatar
Melledy committed
32
33
34
35
36
37
38
39
40

		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
41
		classList.sort((a, b) -> b.getAnnotation(ResourceType.class).loadPriority().value() - a.getAnnotation(ResourceType.class).loadPriority().value());
Melledy's avatar
Melledy committed
42
43
44
45
46
47
48
49
50
51
52

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

			if (map == null) {
				continue;
			}

			try {
				loadFromResource(resourceDefinition, type, map);
			} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
109
				Grasscutter.getLogger().error("Error loading resource file: " + Arrays.toString(type.name()), e);
Melledy's avatar
Melledy committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
			}
		}
	}
	
	@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 {
124
125
126
127
128
129
130
131
132
		FileReader fileReader = new FileReader(Grasscutter.getConfig().RESOURCE_FOLDER + "ExcelBinOutput/" + fileName);
		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
133
134
135
		}
	}

Yazawazi's avatar
Yazawazi committed
136
137
	private static void loadScenePoints() {
		Pattern pattern = Pattern.compile("(?<=scene)(.*?)(?=_point.json)");
alt3ri's avatar
alt3ri committed
138
		File folder = new File(Grasscutter.getConfig().RESOURCE_FOLDER + "BinOutput/Scene/Point");
Yazawazi's avatar
Yazawazi committed
139
140
141
142
143
144

		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
145
		List<ScenePointEntry> scenePointList = new ArrayList<>();
146
		for (File file : Objects.requireNonNull(folder.listFiles())) {
Yazawazi's avatar
Yazawazi committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
			ScenePointConfig config = null;
			Integer sceneId = null;
			
			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
170
				pointData.setId(Integer.parseInt(entry.getKey()));
Yazawazi's avatar
Yazawazi committed
171
172
173

				ScenePointEntry sl = new ScenePointEntry(sceneId + "_" + entry.getKey(), pointData);
				scenePointList.add(sl);
174
175
176
				GameData.getScenePointIdList().add(pointData.getId());
				
				pointData.updateDailyDungeon();
Yazawazi's avatar
Yazawazi committed
177
178
179
			}

			for (ScenePointEntry entry : scenePointList) {
180
				GameData.getScenePointEntries().put(entry.getName(), entry);
Yazawazi's avatar
Yazawazi committed
181
182
183
184
			}
		}
	}

Melledy's avatar
Melledy committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	private static void loadAbilityEmbryos() {
		// Read from cached file if exists
		File embryoCache = new File(Grasscutter.getConfig().DATA_FOLDER + "AbilityEmbryos.json");
		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)");
200

Melledy's avatar
Melledy committed
201
			embryoList = new LinkedList<>();
KingRainbow44's avatar
KingRainbow44 committed
202
203
204
205
206
207
			File folder = new File(Utils.toFilePath(Grasscutter.getConfig().RESOURCE_FOLDER + "BinOutput/Avatar/"));
			File[] files = folder.listFiles();
			if(files == null) {
				Grasscutter.getLogger().error("Error loading ability embryos: no files found in " + folder.getAbsolutePath());
				return;
			}
208

KingRainbow44's avatar
KingRainbow44 committed
209
210
211
			for (File file : files) {
				AvatarConfig config;
				String avatarName;
212

Melledy's avatar
Melledy committed
213
214
215
216
217
218
				Matcher matcher = pattern.matcher(file.getName());
				if (matcher.find()) {
					avatarName = matcher.group(0);
				} else {
					continue;
				}
219

Melledy's avatar
Melledy committed
220
221
222
223
224
225
				try (FileReader fileReader = new FileReader(file)) {
					config = Grasscutter.getGsonFactory().fromJson(fileReader, AvatarConfig.class);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
226

Melledy's avatar
Melledy committed
227
228
229
				if (config.abilities == null) {
					continue;
				}
230

Melledy's avatar
Melledy committed
231
232
233
234
235
236
237
238
239
240
241
242
				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) {
243
			GameData.getAbilityEmbryoInfo().put(entry.getName(), entry);
Melledy's avatar
Melledy committed
244
245
246
		}
	}
	
Melledy's avatar
Melledy committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	private static void loadSpawnData() {
		// Read from cached file if exists
		File spawnDataEntries = new File(Grasscutter.getConfig().DATA_FOLDER + "Spawns.json");
		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) {
			entry.getSpawns().stream().forEach(s -> {
				s.setGroup(entry);
			});
270
			GameDepot.getSpawnListById(entry.getSceneId()).insert(entry, entry.getPos().getX(), entry.getPos().getZ());
Melledy's avatar
Melledy committed
271
272
273
		}
	}
	
Melledy's avatar
Melledy committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
	private static void loadOpenConfig() {
		// Read from cached file if exists
		File openConfigCache = new File(Grasscutter.getConfig().DATA_FOLDER + "OpenConfig.json");
		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
288
			String[] folderNames = {"BinOutput/Talent/EquipTalents/", "BinOutput/Talent/AvatarTalents/"};
Melledy's avatar
Melledy committed
289
290
			
			for (String name : folderNames) {
KingRainbow44's avatar
KingRainbow44 committed
291
292
293
294
295
				File folder = new File(Utils.toFilePath(Grasscutter.getConfig().RESOURCE_FOLDER + name));
				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
296
				
KingRainbow44's avatar
KingRainbow44 committed
297
				for (File file : files) {
Melledy's avatar
Melledy committed
298
299
300
301
					if (!file.getName().endsWith(".json")) {
						continue;
					}
					
KingRainbow44's avatar
KingRainbow44 committed
302
					Map<String, OpenConfigData[]> config;
Melledy's avatar
Melledy committed
303
304
305
306
307
308
309
310
311
					
					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()) {
312
						OpenConfigEntry entry = new OpenConfigEntry(e.getKey(), e.getValue());
Melledy's avatar
Melledy committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
						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) {
327
			GameData.getOpenConfigEntries().put(entry.getName(), entry);
Melledy's avatar
Melledy committed
328
329
		}
	}
330

Melledy's avatar
Melledy committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
	// 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;
	}
	
348
	public static class OpenConfigData {
Melledy's avatar
Melledy committed
349
350
351
		public String $type;
		public String abilityName;
		public int talentIndex;
352
353
		public int skillID;
		public int pointDelta;
Melledy's avatar
Melledy committed
354
355
	}
}