Advertisement
SforzandoCF

l

Aug 27th, 2024 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.69 KB | None | 0 0
  1. package lx01.platform.reflect;
  2.  
  3. public final class LX01Class {
  4.     private final DataTree dataTree;
  5.     private final String name;
  6.     private final String[] paths;
  7.    
  8.     LX01Class (byte[] data, String fullName) {
  9.         this(parseDataTree(data), fullName.substring(0, fullName.lastIndexOf(".")).split("."), fullName.substring(fullName.lastIndexOf(".") + 1));
  10.     }
  11.    
  12.     LX01Class (DataTree tree, String fullName) {
  13.         this(tree, fullName.substring(0, fullName.lastIndexOf(".")).split("."), fullName.substring(fullName.lastIndexOf(".") + 1));
  14.     }
  15.    
  16.     LX01Class (byte[] data, String[] path, String lastName) {
  17.         this(parseDataTree(data), path, lastName);
  18.     }
  19.    
  20.     LX01Class (DataTree tree, String[] path, String lastName) {
  21.         this.dataTree = tree;
  22.         this.name = lastName;
  23.         this.paths = path;
  24.         String[] allPaths = new String[this.paths.length + 1];
  25.         System.arraycopy(this.paths, 0, allPaths, 0, this.paths.length);
  26.         allPaths[this.paths.length] = this.name;
  27.         LX01Classes.register(allPaths);
  28.     }
  29.    
  30.     public static LX01Class get (String name) throws ClassNotFoundException {
  31.         return get(name.split("."));
  32.     }
  33.    
  34.     public static LX01Class get (String[] path) throws ClassNotFoundException {
  35.         LX01Class lc = Classes.get(path);
  36.         if (lc == null) throw new ClassNotFoundException("Path does not point to a qualified LX01 class");
  37.         return lc;
  38.     }
  39.    
  40.     private static DataTree parseDataTree (byte[] data) {
  41.         return DataTree.parse(data);
  42.     }
  43.    
  44.     public List<Function> getFunctions (String name) throws FunctionNotFoundException {
  45.         List<Function> funcs = List.<Function>of();
  46.         for (Function f : this.dataTree.getFunctions()) if (f.getName().contains(name)) funcs.add(f);
  47.         if (funcs.size() == 0) throw new FunctionNotFoundException("Could not get specified function");
  48.         return funcs;
  49.     }
  50.    
  51.     public Function getFunction (String name) throws FunctionNotFoundException {
  52.         for (Function f : this.dataTree.getFunctions()) if (f.getName().equals(name)) return f;
  53.         throw new FunctionNotFoundException("Could not get specified function");
  54.     }
  55.    
  56.     public List<Variable> getVariables (String name) throws VariableNotFoundException {
  57.         List<Variable> vars = List.<Variable>of();
  58.         for (Variable v : this.dataTree.getVariables()) if (v.getName().contains(name)) vars.add(v);
  59.         if (vars.size() == 0) throw new VariableNotFoundException("Could not get specified variable");
  60.         return vars;
  61.     }
  62.    
  63.     public Variable getVariable (String name) throws VariableNotFoundException {
  64.         for (Variable v : this.dataTree.getVariables()) if (v.getName().equals(name)) return v;
  65.         throw new VariableNotFoundException("Could not get specified variable");
  66.     }
  67.    
  68.     public List<LX01Class> getClasses (String name) throws ClassNotFoundException {
  69.         List<LX01Class> cls = List.<LX01Class>of();
  70.         for (LX01Class c : this.dataTree.getClasses()) if (c.getName().contains(name)) cls.add(c);
  71.         if (cls.size() == 0) throw new ClassNotFoundException("Could not get specified class");
  72.         return cls;
  73.     }
  74.    
  75.     public LX01Class getClass (String name) throws ClassNotFoundException {
  76.         for (LX01Class c : this.dataTree.getClasses()) if (c.getName().equals(name)) return c;
  77.         throw new ClassNotFoundException("Could not get specified class");
  78.     }
  79. }
  80.  
  81. class DataTree {
  82.     private boolean initialized = false;
  83.     private Map<Class<?>, ?> types = Map.<Class<?>, ?>of();
  84.     private List<String> properties = List.<String>of();
  85.    
  86.     private DataTree () {}
  87.    
  88.     public void add (Object obj) {
  89.         if (!validType(obj) || this.initialized) return;
  90.         if (obj instanceof Function f)
  91.             this.add(Function.class, f);
  92.         else if (obj instanceof Variable v)
  93.             this.add(Variable.class, v);
  94.         else if (obj instanceof DataTree c)
  95.             this.add(LX01Class.class, c);
  96.     }
  97.    
  98.     private <T> void add (Class<T> clazz, T t) {
  99.         this.types.put(clazz, t);
  100.     }
  101.    
  102.     private boolean validType (Object obj) {
  103.         return obj instanceof Function || obj instanceof Variable || obj instanceof LX01Class;
  104.     }
  105.    
  106.     static DataTree parse (byte[] data) {
  107.         byte[] actualData = new byte[data.length - 8];
  108.         System.arraycopy(data, 8, actualData, 0, actualData.length);
  109.         DataTree tree = new DataTree();
  110.         boolean loop = true;
  111.         while (loop) {
  112.             int type = actualData[0];
  113.             switch (type) {
  114.                 case 0:
  115.                     short length = ((short)actualData[1]) << 8 | actualData[2];
  116.                     byte[] classData = new byte[length];
  117.                     System.arraycopy(actualData, 3, classData, 0, length);
  118.                     tree.add(LX01Class.class, parseClass(classData));
  119.                     byte[] data2 = new byte[actualData.length - length - 3];
  120.                     System.arraycopy(actualData, 3, data2, 0, data2.length);
  121.                     System.arraycopy(data2, 0, actualData, 0, data2.length);
  122.                     break;
  123.                 case 1:
  124.                     short length = ((short)actualData[1]) << 8 | actualData[2];
  125.                     byte[] functionData = new byte[length];
  126.                     System.arraycopy(actualData, 3, functionData, 0, length);
  127.                     tree.add(Function.class, parseFunction(functionData));
  128.                     byte[] data2 = new byte[actualData.length - length - 3];
  129.                     System.arraycopy(actualData, 3, data2, 0, data2.length);
  130.                     System.arraycopy(data2, 0, actualData, 0, data2.length);
  131.                     break;
  132.                 case 2:
  133.                     short length = ((short)actualData[1]) << 8 | actualData[2];
  134.                     byte[] variableData = new byte[length];
  135.                     System.arraycopy(actualData, 3, variableData, 0, length);
  136.                     tree.add(Variable.class, parseVariable(variableData));
  137.                     byte[] data2 = new byte[actualData.length - length - 3];
  138.                     System.arraycopy(actualData, 3, data2, 0, data2.length);
  139.                     System.arraycopy(data2, 0, actualData, 0, data2.length);
  140.                     break;
  141.                 case 3:
  142.                     short length = ((short)actualData[1]) << 8 | actualData[2];
  143.                     byte[] stringData = new byte[length];
  144.                     System.arraycopy(actualData, 3, stringData, 0, length);
  145.                     tree.properties.add(new String(stringData));
  146.                     byte[] data2 = new byte[actualData.length - length - 3];
  147.                     System.arraycopy(actualData, 3, data2, 0, data2.length);
  148.                     System.arraycopy(data2, 0, actualData, 0, data2.length);
  149.                     break;
  150.                 default:
  151.                     throw new IllegalTypeException("Unknown byte: 0x" + Integer.toHexString(type).substring(6));
  152.             }
  153.         }
  154.     }
  155.    
  156.     private static Function parseFunction (byte[] data) {
  157.         return Function.parse(data);
  158.     }
  159.    
  160.     private static Variable parseVariable (byte[] data) {
  161.         return Variable.parse(data);
  162.     }
  163.    
  164.     private LX01Class parseClass (byte[] data) {
  165.         short length = ((short)data[1]) << 8 | data[2];
  166.         byte[] name = new byte[length];
  167.         System.arraycopy(data, 2, name, 0, length);
  168.         String nameString = new String(name);
  169.         byte[] split = new byte[data.length - 2 - length];
  170.         System.arraycopy(data, 2 + length, split, 0, split.length);
  171.         return new LX01Class(split, nameString);
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement