Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void unscramble(int scriptID, byte[] data, byte[] scrambledData) throws IOException {
- if (data.length != scrambledData.length) {
- return;
- }
- ByteBuffer buffer = new ByteBuffer(data);
- ByteBuffer scrambled = new ByteBuffer(scrambledData);
- boolean hasSwitches = true; //old OSRS doesnt have switches
- boolean newFormat = true;
- buffer.setPosition(data.length - 2);
- scrambled.setPosition(scrambledData.length - 2);
- int switchBlocksSize = buffer.readUnsignedShort();
- if (scrambled.readUnsignedShort() != switchBlocksSize) {
- return;
- }
- int codeBlockEnd = data.length - switchBlocksSize - 16 - 2;
- buffer.setPosition(codeBlockEnd);
- scrambled.setPosition(codeBlockEnd);
- int codeSize = buffer.readInt();
- if (scrambled.readInt() != codeSize) {
- return;
- }
- int intLocalsCount = buffer.readUnsignedShort();
- if (scrambled.readUnsignedShort() != intLocalsCount) {
- return;
- }
- int stringLocalsCount = buffer.readUnsignedShort();
- if (scrambled.readUnsignedShort() != stringLocalsCount) {
- return;
- }
- int longLocalsCount = buffer.readUnsignedShort();
- if (scrambled.readUnsignedShort() != longLocalsCount) {
- return;
- }
- int intArgsCount = buffer.readUnsignedShort();
- if (scrambled.readUnsignedShort() != intArgsCount) {
- return;
- }
- int stringArgsCount = buffer.readUnsignedShort();
- if (scrambled.readUnsignedShort() != stringArgsCount) {
- return;
- }
- int longArgsCount = buffer.readUnsignedShort();
- if (scrambled.readUnsignedShort() != longArgsCount) {
- return;
- }
- int switchesCount = buffer.readUByte();
- if (scrambled.readUByte() != switchesCount) {
- return;
- }
- Map[] switches = new HashMap[switchesCount];
- for (int i = 0; i < switchesCount; i++) {
- int numCases = buffer.readUnsignedShort();
- if (scrambled.readUnsignedShort() != numCases) {
- return;
- }
- switches[i] = new HashMap<Integer, Integer>(numCases);
- while (numCases-- > 0) {
- switches[i].put(buffer.readInt(), buffer.readInt());
- }
- }
- buffer.setPosition(0);
- scrambled.setPosition(0);
- String scriptName = buffer.readNullString();
- scrambled.readNullString();
- Map<Integer, Integer> s = new HashMap<>();
- Map<Integer, Integer> r = new HashMap<>();
- while (buffer.getPosition() < codeBlockEnd) {
- int opcode = buffer.readUnsignedShort();
- int scrambledOp = scrambled.readUnsignedShort();
- if (opcode == Opcodes.PUSH_STRING) {
- if (!scrambled.readString().equals(buffer.readString())) {
- // throw new RuntimeException("Script different");
- }
- } else if (opcode == Opcodes.PUSH_LONG) {
- long i = buffer.readLong();
- long j = scrambled.readLong();
- if (i != j) {
- return;
- }
- } /*else if (opcode == Opcodes.SWITCH) { // switch
- buffer.readInt();
- scrambled.readInt();
- }*/ else if (opcode == Opcodes.RETURN || opcode == Opcodes.POP_INT || opcode == Opcodes.POP_STRING || opcode >= 150) {
- int i = buffer.readUByte();
- int j = scrambled.readUByte();
- if (i != j) {
- return;
- }
- }/* else if (opcode == Opcodes.GOTO || opcode == Opcodes.INT_NE || opcode == Opcodes.INT_EQ || opcode == Opcodes.INT_LT || opcode == Opcodes.INT_GT || opcode == Opcodes.INT_LE || opcode == Opcodes.INT_GE || opcode == Opcodes.LONG_NE || opcode == Opcodes.LONG_EQ || opcode == Opcodes.LONG_LT || opcode == Opcodes.LONG_GT || opcode == Opcodes.LONG_LE || opcode == Opcodes.LONG_GE || opcode == Opcodes.EQ1 || opcode == Opcodes.EQ0) {
- buffer.readInt();
- scrambled.readInt();
- }*/ else {
- int i = buffer.readInt();
- int j = scrambled.readInt();
- if (i != j) {
- return;
- }
- }
- s.put(opcode, scrambledOp);
- r.put(scrambledOp, opcode);
- // if (unsure.contains(opcode)) {
- //
- // }
- // if (unscrambled.containsKey(opcode)) {
- // if (unscrambled.get(opcode) != scrambledOp) {
- // System.err.println("Duplicate scrambling found!!!");
- // }
- // } else if (unscrambled.containsValue(scrambledOp)) {
- // System.err.println("Duplicate value " + opcode + " & .. -> " + scrambledOp);
- // } else {
- // unscrambled.put(opcode, scrambledOp);
- // }
- }
- scramble.putAll(s);
- unscramble.putAll(r);
- // System.out.println("Script ok");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement