Skip to content

Commit

Permalink
Refactor resource loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Melledy committed Mar 16, 2024
1 parent 0d5ad9d commit bb43425
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
19 changes: 1 addition & 18 deletions src/main/java/emu/lunarcore/data/GameData.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,7 @@ public class GameData {

// Configs (Bin)
@Getter private static Object2ObjectMap<String, FloorInfo> floorInfos = new Object2ObjectOpenHashMap<>();

public static Int2ObjectMap<?> getMapForExcel(Class<?> resourceDefinition) {
Int2ObjectMap<?> map = null;

try {
Field field = GameData.class.getDeclaredField(Utils.lowerCaseFirstChar(resourceDefinition.getSimpleName()) + "Map");
field.setAccessible(true);

map = (Int2ObjectMap<?>) field.get(null);

field.setAccessible(false);
} catch (Exception e) {

}

return map;
}


public static List<Integer> getAllRelicIds() {
return relicExcelMap.values().stream().map(RelicExcel::getId).toList();
}
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/emu/lunarcore/data/ResourceLoader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emu.lunarcore.data;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -21,6 +22,7 @@
import emu.lunarcore.data.ResourceDeserializers.LunarCoreHashDeserializer;
import emu.lunarcore.data.config.FloorInfo.FloorGroupSimpleInfo;
import emu.lunarcore.data.custom.ActivityScheduleData;
import emu.lunarcore.util.Utils;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

public class ResourceLoader {
Expand Down Expand Up @@ -63,6 +65,23 @@ private static void checkDataFolder() {
dir.mkdir();
}
}

private static Int2ObjectMap<?> getMapForExcel(Class<?> dataClass, Class<?> resourceDefinition) {
Int2ObjectMap<?> map = null;

try {
Field field = dataClass.getDeclaredField(Utils.lowerCaseFirstChar(resourceDefinition.getSimpleName()) + "Map");
field.setAccessible(true);

map = (Int2ObjectMap<?>) field.get(null);

field.setAccessible(false);
} catch (Exception e) {

}

return map;
}

private static List<Class<?>> getResourceDefClasses() {
Reflections reflections = new Reflections(ResourceLoader.class.getPackage().getName());
Expand Down Expand Up @@ -90,7 +109,7 @@ private static void loadResources() {
}

@SuppressWarnings("rawtypes")
Int2ObjectMap map = GameData.getMapForExcel(resourceDefinition);
Int2ObjectMap map = ResourceLoader.getMapForExcel(type.gameDataClass(), resourceDefinition);

try {
loadFromResource(resourceDefinition, type, map);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/emu/lunarcore/data/ResourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/** Names of the file that this Resource loads from */
String[] name();

Class<?> gameDataClass() default GameData.class;

/** Load priority - dictates which order to load this resource, with "highest" being loaded first */
LoadPriority loadPriority() default LoadPriority.NORMAL;
Expand Down

0 comments on commit bb43425

Please sign in to comment.