Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pf.bot.inject;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.HashMap;
- import java.util.Map;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.objectweb.asm.ClassAdapter;
- import org.objectweb.asm.ClassReader;
- import org.objectweb.asm.ClassVisitor;
- import org.objectweb.asm.ClassWriter;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- import pf.bot.inject.data.FieldData;
- import pf.bot.inject.scripts.FieldScript;
- import pf.bot.inject.scripts.InterfaceScript;
- public class ScriptModifier {
- public final Map<String, ClassWriter> writers = new HashMap<String, ClassWriter>();
- public final Map<String, ClassAdapter> adapters = new HashMap<String, ClassAdapter>();
- public byte[] process(String name, byte[] bytes) {
- ClassAdapter adapter = adapters.get(name);
- if (adapter != null) {
- ClassReader reader = new ClassReader(bytes);
- reader.accept(adapter, ClassReader.SKIP_FRAMES);
- return writers.get(name).toByteArray();
- }
- return bytes;
- }
- public byte[] process(String name, InputStream input) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- byte[] buffer = new byte[4096];
- int i;
- while ((i = input.read(buffer)) != -1) {
- out.write(buffer, 0, i);
- }
- return process(name, out.toByteArray());
- }
- public void load() {
- DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder doc = fact.newDocumentBuilder();
- Document document = doc.parse(new File("log.xml"));
- NodeList elements = document.getElementsByTagName("class");
- for (int i = 0; i < elements.getLength(); i++) {
- Element element = (Element) elements.item(i);
- String name = element.getAttribute("name");
- String newname = element.getAttribute("new");
- adapters.put(name, new InterfaceScript(make(name), newname));
- NodeList fields = element.getElementsByTagName("field");
- for (int k = 0; k < fields.getLength(); k++) {
- Element field = (Element) fields.item(k);
- FieldData fd = new FieldData();
- fd.origType = field.getAttribute("origType");
- fd.parent = field.getAttribute("parent");
- fd.stat = field.getAttribute("static");
- fd.name = field.getAttribute("new");
- fd.type = field.getAttribute("type");
- fd.orig = field.getAttribute("name");
- adapters.put(name, new FieldScript(make(name), fd));
- }
- }
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public ClassVisitor make(String name) {
- ClassAdapter adapter = adapters.get(name);
- if (adapter == null) {
- ClassWriter writer = new ClassWriter(0);
- writers.put(name, writer);
- return writer;
- }
- return adapter;
- }
- }
Add Comment
Please, Sign In to add comment