diff --git a/src-generated/edu/nps/moves/dis7/entities/EntityTypeFactory.java b/src-generated/edu/nps/moves/dis7/entities/EntityTypeFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..dff3317bb98491709090a72b359c39bbeed74219 --- /dev/null +++ b/src-generated/edu/nps/moves/dis7/entities/EntityTypeFactory.java @@ -0,0 +1,43 @@ +package edu.nps.moves.dis7.entities; + +import edu.nps.moves.dis7.pdus.EntityType; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class EntityTypeFactory +{ + private static Map<Integer,String> uid2ClassNameMap = new HashMap<>(); + static { + try { + InputStream ins = EntityTypeFactory.class.getResourceAsStream("uid2EntityClass.properties"); + Properties prop = new Properties(); + prop.load(ins); + prop.entrySet().stream().forEach(e->uid2ClassNameMap.put(Integer.parseInt(e.getKey().toString()), e.getValue().toString())); + } + catch( IOException ex) { + throw new RuntimeException (ex); + } + } + + public static EntityType makeEntity(int uid) + { + String name = uid2ClassNameMap.get(uid); + if(name==null) + return null; + + try { + Class cls = Class.forName(name); + Constructor<EntityType> constr = (Constructor<EntityType>) cls.getConstructors()[0]; + return constr.newInstance(); + } + catch(ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException ex) { + System.err.println(name+"(uid "+uid+") could not be instantiated. Check for proper entity jar(s) on classpath."); + return null; + } + } +} \ No newline at end of file