This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin cleaning up the item translator
- Loading branch information
Showing
11 changed files
with
81 additions
and
118 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
proxy/src/main/java/org/dragonet/proxy/util/FileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.dragonet.proxy.util; | ||
|
||
import org.dragonet.proxy.DragonProxy; | ||
|
||
import java.io.InputStream; | ||
|
||
public class FileUtils { | ||
|
||
public static InputStream getResource(String path) { | ||
return DragonProxy.class.getClassLoader().getResourceAsStream(path); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
proxy/src/main/java/org/dragonet/proxy/util/registry/MappingEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.dragonet.proxy.util.registry; | ||
|
||
public interface MappingEntry { | ||
} |
34 changes: 26 additions & 8 deletions
34
proxy/src/main/java/org/dragonet/proxy/util/registry/Registry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
package org.dragonet.proxy.util.registry; | ||
|
||
import it.unimi.dsi.fastutil.objects.Object2ObjectMap; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import org.dragonet.proxy.DragonProxy; | ||
import org.dragonet.proxy.util.FileUtils; | ||
import org.reflections.Reflections; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.util.LinkedHashMap; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Consumer; | ||
|
||
public abstract class Registry { | ||
private static Class<? extends Annotation> annotationClass; | ||
|
||
// TODO: make this not static | ||
public static void registerType(Class<? extends Annotation> annotation) { | ||
annotationClass = annotation; | ||
} | ||
|
||
// TODO: make this not static | ||
public static void registerPath(String path, BiConsumer<Annotation, Class> callback) { | ||
public static void registerPath(String path, Class<? extends Annotation> annotationClass, BiConsumer<Annotation, Class> consumer) { | ||
for(Class clazz : new Reflections(path).getTypesAnnotatedWith(annotationClass)) { | ||
Annotation annotation = clazz.getAnnotation(annotationClass); | ||
|
||
callback.accept(annotation, clazz); | ||
consumer.accept(annotation, clazz); | ||
} | ||
} | ||
|
||
public static void registerMapping(String path, Class<? extends MappingEntry> type, Consumer<LinkedHashMap<String, MappingEntry>> consumer) { | ||
InputStream stream = FileUtils.getResource(path); | ||
if(stream == null) { | ||
throw new RuntimeException("Failed to load mappings: " + path); | ||
} | ||
|
||
LinkedHashMap<String, MappingEntry> entries; | ||
try { | ||
entries = DragonProxy.JSON_MAPPER.readValue(stream, DragonProxy.JSON_MAPPER.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, type)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to parse mappings: " + path, e); | ||
} | ||
|
||
consumer.accept(entries); | ||
} | ||
} |