-
-
Save y785/9a4dc0d091a3abad42e29a7b6e1f5bcc to your computer and use it in GitHub Desktop.
pseudo code, not real
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
public class Loader { | |
public void load() { | |
try { | |
var path = Paths.get("scripts/java/"); | |
var urls = path.toUri().toURL(); | |
log.debug("Urls: {}", urls); | |
clsLoader = new URLClassLoader(new URL[]{urls}); | |
Map<String, Class<MoeScript>> npcScripts = new HashMap<>(); | |
With.continueException(getAllClasses("moe.maple.scripts.npc"), name -> { | |
processPkg(npcScripts, name); | |
}); | |
// ... and so on, for every other package. | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private Collection<String> getAllClasses(String fullpkg, Path path) { | |
var ret = new LinkedList<String>(); | |
var pkg = fullpkg+"."; | |
var dir = Paths.get(path.toString(), pkg.replace(".", "/")).toFile().listFiles(); | |
if (dir == null) | |
return ret; | |
With.continueException(dir, f -> { | |
if (f.isDirectory()) { | |
ret.addAll(getAllClasses(fullpkg+"."+f.getName(), path)); | |
} else { | |
var name = pkg + (f.getName().replace(".class", "")); | |
ret.add(name); | |
} | |
}); | |
return ret; | |
} | |
private Collection<String> getAllClasses(String fullpkg) { | |
return getAllClasses(fullpkg, path); | |
} | |
private void processPkg(Map<String, Class<MoeScript>> map, String name) throws ClassNotFoundException { | |
var cls = clsLoader.loadClass(name); | |
var annotation = cls.getAnnotation(Script.class); | |
if (annotation == null) return; | |
if (annotation.name().isEmpty()) throw new IllegalArgumentException("Ehh, you forgot to set name, you idiot: "+name); | |
map.put(annotation.name(), (Class<MoeScript>)cls); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment