Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. private static void unscramble(int scriptID, byte[] data, byte[] scrambledData) throws IOException {
  2. if (data.length != scrambledData.length) {
  3. return;
  4. }
  5. ByteBuffer buffer = new ByteBuffer(data);
  6. ByteBuffer scrambled = new ByteBuffer(scrambledData);
  7. boolean hasSwitches = true; //old OSRS doesnt have switches
  8. boolean newFormat = true;
  9.  
  10. buffer.setPosition(data.length - 2);
  11. scrambled.setPosition(scrambledData.length - 2);
  12. int switchBlocksSize = buffer.readUnsignedShort();
  13. if (scrambled.readUnsignedShort() != switchBlocksSize) {
  14. return;
  15. }
  16.  
  17. int codeBlockEnd = data.length - switchBlocksSize - 16 - 2;
  18. buffer.setPosition(codeBlockEnd);
  19. scrambled.setPosition(codeBlockEnd);
  20.  
  21. int codeSize = buffer.readInt();
  22. if (scrambled.readInt() != codeSize) {
  23. return;
  24. }
  25. int intLocalsCount = buffer.readUnsignedShort();
  26. if (scrambled.readUnsignedShort() != intLocalsCount) {
  27. return;
  28. }
  29. int stringLocalsCount = buffer.readUnsignedShort();
  30. if (scrambled.readUnsignedShort() != stringLocalsCount) {
  31. return;
  32. }
  33. int longLocalsCount = buffer.readUnsignedShort();
  34. if (scrambled.readUnsignedShort() != longLocalsCount) {
  35. return;
  36. }
  37. int intArgsCount = buffer.readUnsignedShort();
  38. if (scrambled.readUnsignedShort() != intArgsCount) {
  39. return;
  40. }
  41. int stringArgsCount = buffer.readUnsignedShort();
  42. if (scrambled.readUnsignedShort() != stringArgsCount) {
  43. return;
  44. }
  45. int longArgsCount = buffer.readUnsignedShort();
  46. if (scrambled.readUnsignedShort() != longArgsCount) {
  47. return;
  48. }
  49. int switchesCount = buffer.readUByte();
  50. if (scrambled.readUByte() != switchesCount) {
  51. return;
  52. }
  53. Map[] switches = new HashMap[switchesCount];
  54. for (int i = 0; i < switchesCount; i++) {
  55. int numCases = buffer.readUnsignedShort();
  56. if (scrambled.readUnsignedShort() != numCases) {
  57. return;
  58. }
  59. switches[i] = new HashMap<Integer, Integer>(numCases);
  60. while (numCases-- > 0) {
  61. switches[i].put(buffer.readInt(), buffer.readInt());
  62. }
  63. }
  64. buffer.setPosition(0);
  65. scrambled.setPosition(0);
  66. String scriptName = buffer.readNullString();
  67. scrambled.readNullString();
  68. Map<Integer, Integer> s = new HashMap<>();
  69. Map<Integer, Integer> r = new HashMap<>();
  70. while (buffer.getPosition() < codeBlockEnd) {
  71. int opcode = buffer.readUnsignedShort();
  72. int scrambledOp = scrambled.readUnsignedShort();
  73. if (opcode == Opcodes.PUSH_STRING) {
  74. if (!scrambled.readString().equals(buffer.readString())) {
  75. // throw new RuntimeException("Script different");
  76. }
  77. } else if (opcode == Opcodes.PUSH_LONG) {
  78. long i = buffer.readLong();
  79. long j = scrambled.readLong();
  80. if (i != j) {
  81. return;
  82. }
  83. } /*else if (opcode == Opcodes.SWITCH) { // switch
  84. buffer.readInt();
  85. scrambled.readInt();
  86. }*/ else if (opcode == Opcodes.RETURN || opcode == Opcodes.POP_INT || opcode == Opcodes.POP_STRING || opcode >= 150) {
  87. int i = buffer.readUByte();
  88. int j = scrambled.readUByte();
  89. if (i != j) {
  90. return;
  91. }
  92. }/* 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) {
  93. buffer.readInt();
  94. scrambled.readInt();
  95. }*/ else {
  96. int i = buffer.readInt();
  97. int j = scrambled.readInt();
  98. if (i != j) {
  99. return;
  100. }
  101. }
  102. s.put(opcode, scrambledOp);
  103. r.put(scrambledOp, opcode);
  104. // if (unsure.contains(opcode)) {
  105. //
  106. // }
  107. // if (unscrambled.containsKey(opcode)) {
  108. // if (unscrambled.get(opcode) != scrambledOp) {
  109. // System.err.println("Duplicate scrambling found!!!");
  110. // }
  111. // } else if (unscrambled.containsValue(scrambledOp)) {
  112. // System.err.println("Duplicate value " + opcode + " & .. -> " + scrambledOp);
  113. // } else {
  114. // unscrambled.put(opcode, scrambledOp);
  115. // }
  116. }
  117. scramble.putAll(s);
  118. unscramble.putAll(r);
  119. // System.out.println("Script ok");
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement