Skip to content

Commit 3098187

Browse files
committed
refactor: cs2 runtime, cs1 instructions, if1 decoding, if3 decoding
1 parent 9057c04 commit 3098187

22 files changed

+1504
-1533
lines changed

src/main/java/org/runejs/client/ClientScriptRunner.java

Lines changed: 587 additions & 659 deletions
Large diffs are not rendered by default.

src/main/java/org/runejs/client/Game.java

Lines changed: 115 additions & 115 deletions
Large diffs are not rendered by default.

src/main/java/org/runejs/client/MovedStatics.java

Lines changed: 52 additions & 53 deletions
Large diffs are not rendered by default.

src/main/java/org/runejs/client/cache/cs/ClientScript.java

Lines changed: 126 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414

1515
public class ClientScript extends CachedNode {
1616

17-
private static NodeCache clientScriptCache = new NodeCache(128);
17+
private static NodeCache scriptCache = new NodeCache(128);
1818

1919
public int[] intOperands;
20-
public int intStackCount;
21-
public int stringStackCount;
22-
public int localStringCount;
20+
public int intArgCount;
21+
public int stringArgCount;
22+
public int stringLocalCount;
2323
public String[] stringOperands;
24-
public int localIntCount;
24+
public int intLocalCount;
2525
public int[] opcodes;
2626

2727
public static void clearClientScriptCache() {
28-
clientScriptCache.clear();
28+
scriptCache.clear();
2929
}
3030

3131

@@ -41,165 +41,163 @@ public static void clientScriptDebugger() {
4141
}
4242
}
4343

44-
public static ClientScript decodeClientScript(int arg0, int arg1) {
44+
public static ClientScript get(int arg0, int arg1) {
4545
long scriptId = arg0 + (arg1 << 16);
46-
ClientScript clientScript = (ClientScript) clientScriptCache.get(scriptId);
47-
if(clientScript != null) {
48-
return clientScript;
46+
ClientScript script = (ClientScript) scriptCache.get(scriptId);
47+
if (script != null) {
48+
return script;
4949
}
5050

5151
Buffer buffer = new Buffer(CacheArchive.clientScriptCacheArchive.getFileByName(arg1 + Native.comma + arg0, Native.emptyString));
52-
clientScript = new ClientScript();
52+
script = new ClientScript();
5353
buffer.currentPosition = buffer.buffer.length - 12;
5454
int opcodeCount = buffer.getIntBE();
5555
int operandIndex = 0;
56-
clientScript.localIntCount = buffer.getUnsignedShortBE();
57-
clientScript.localStringCount = buffer.getUnsignedShortBE();
58-
clientScript.intStackCount = buffer.getUnsignedShortBE();
59-
clientScript.stringStackCount = buffer.getUnsignedShortBE();
60-
clientScript.stringOperands = new String[opcodeCount];
61-
clientScript.intOperands = new int[opcodeCount];
56+
script.intLocalCount = buffer.getUnsignedShortBE();
57+
script.stringLocalCount = buffer.getUnsignedShortBE();
58+
script.intArgCount = buffer.getUnsignedShortBE();
59+
script.stringArgCount = buffer.getUnsignedShortBE();
60+
script.stringOperands = new String[opcodeCount];
61+
script.intOperands = new int[opcodeCount];
6262
buffer.currentPosition = 0;
63-
clientScript.opcodes = new int[opcodeCount];
64-
while(buffer.currentPosition < -12 + buffer.buffer.length) {
63+
script.opcodes = new int[opcodeCount];
64+
while (buffer.currentPosition < -12 + buffer.buffer.length) {
6565
int opcode = buffer.getUnsignedShortBE();
66-
if(opcode == 3) {
67-
clientScript.stringOperands[operandIndex] = buffer.getString();
68-
} else if(opcode >= 100 || opcode == 21 || opcode == 38 || opcode == 39) {
69-
clientScript.intOperands[operandIndex] = buffer.getUnsignedByte();
66+
if (opcode == 3) {
67+
script.stringOperands[operandIndex] = buffer.getString();
68+
} else if (opcode >= 100 || opcode == 21 || opcode == 38 || opcode == 39) {
69+
script.intOperands[operandIndex] = buffer.getUnsignedByte();
7070
} else {
71-
clientScript.intOperands[operandIndex] = buffer.getIntBE();
71+
script.intOperands[operandIndex] = buffer.getIntBE();
7272
}
73-
clientScript.opcodes[operandIndex++] = opcode;
73+
script.opcodes[operandIndex++] = opcode;
7474
}
7575
System.out.println("Caching script " + scriptId);
76-
clientScriptCache.put(scriptId, clientScript);
77-
return clientScript;
76+
scriptCache.put(scriptId, script);
77+
return script;
7878
}
7979

80-
81-
public static int parseClientScripts(int scriptIndex, GameInterface gameInterface1) {
82-
if (gameInterface1.clientScripts == null || scriptIndex >= gameInterface1.clientScripts.length) {
80+
// execute if1 scripts (would be ideal to put this into another class ala Cs1ScriptRunner)
81+
public static int run(int id, GameInterface component) {
82+
if (component.scripts == null || id >= component.scripts.length) {
8383
return -2;
8484
}
85+
8586
try {
86-
int[] opcodes = gameInterface1.clientScripts[scriptIndex];
87-
int i = 0;
88-
int scriptDataIndex = 0;
87+
int[] script = component.scripts[id];
88+
int accumulator = 0;
89+
int pc = 0;
8990
int operator = 0;
91+
9092
while (true) {
91-
int operand = 0;
92-
int nextOperator = 0;
93-
int opcode = opcodes[scriptDataIndex++];
93+
int value = 0;
94+
int nextAccumulatorMode = 0;
95+
int opcode = script[pc++];
96+
9497
if (opcode == 0) {
95-
return i;
96-
}
97-
if (opcode == 15) {
98-
nextOperator = 1;
99-
}
100-
if (opcode == 16) {
101-
nextOperator = 2;
98+
return accumulator;
10299
}
100+
103101
if (opcode == 1) {
104-
operand = Player.playerLevels[opcodes[scriptDataIndex++]];
105-
}
106-
if (opcode == 2) {
107-
operand = Player.nextLevels[opcodes[scriptDataIndex++]];
108-
}
109-
if (opcode == 3) {
110-
operand = Player.playerExperience[opcodes[scriptDataIndex++]];
111-
}
112-
if (opcode == 17) {
113-
nextOperator = 3;
114-
}
115-
if (opcode == 4) {
116-
int i_19_ = opcodes[scriptDataIndex++] << 16;
117-
i_19_ += opcodes[scriptDataIndex++];
118-
GameInterface gameInterface = GameInterface.getInterface(i_19_);
119-
int i_20_ = opcodes[scriptDataIndex++];
120-
if (i_20_ != -1 && (!ItemDefinition.forId(i_20_, 10).members || MovedStatics.membersWorld)) {
121-
for (int i_21_ = 0; i_21_ < gameInterface.items.length; i_21_++) {
122-
if (1 + i_20_ == gameInterface.items[i_21_]) {
123-
operand += gameInterface.itemAmounts[i_21_];
102+
// stat_level
103+
value = Player.boostedLevels[script[pc++]];
104+
} else if (opcode == 2) {
105+
// stat_base_level
106+
value = Player.baseLevels[script[pc++]];
107+
} else if (opcode == 3) {
108+
// stat_xp
109+
value = Player.experience[script[pc++]];
110+
} else if (opcode == 4) {
111+
// inv_count
112+
int componentId = script[pc++] << 16;
113+
componentId += script[pc++];
114+
GameInterface otherComponent = GameInterface.getInterface(componentId);
115+
int objType = script[pc++];
116+
if (objType != -1 && (!ItemDefinition.forId(objType, 10).members || MovedStatics.membersWorld)) {
117+
for (int slot = 0; slot < otherComponent.invSlotObjId.length; slot++) {
118+
if (otherComponent.invSlotObjId[slot] == objType + 1) {
119+
value += otherComponent.invSlotObjCount[slot];
124120
}
125121
}
126122
}
127-
}
128-
if (opcode == 5) {
129-
int temp = opcodes[scriptDataIndex++];
130-
operand = VarPlayerDefinition.varPlayers[temp];
131-
}
132-
if (opcode == 6) {
133-
operand = Player.experienceForLevels[-1 + Player.nextLevels[opcodes[scriptDataIndex++]]];
134-
}
135-
if (opcode == 7) {
136-
int varPlayerIndex = opcodes[scriptDataIndex++];
137-
operand = 100 * VarPlayerDefinition.varPlayers[varPlayerIndex] / 46875;
138-
}
139-
if (opcode == 8) {
140-
operand = Player.localPlayer.combatLevel;
141-
}
142-
if (opcode == 9) {
143-
for (int i_22_ = 0; i_22_ < 25; i_22_++) {
144-
if (ClientScriptRunner.aBooleanArray548[i_22_]) {
145-
operand += Player.nextLevels[i_22_];
123+
} else if (opcode == 5) {
124+
// testvar
125+
int temp = script[pc++];
126+
value = VarPlayerDefinition.varPlayers[temp];
127+
} else if (opcode == 6) {
128+
// stat_xp_remaining
129+
value = Player.experienceForLevels[-1 + Player.baseLevels[script[pc++]]];
130+
} else if (opcode == 7) {
131+
int varPlayerIndex = script[pc++];
132+
value = 100 * VarPlayerDefinition.varPlayers[varPlayerIndex] / 46875;
133+
} else if (opcode == 8) {
134+
// comlevel
135+
value = Player.localPlayer.combatLevel;
136+
} else if (opcode == 9) {
137+
// stat_total
138+
for (int skill = 0; skill < 25; skill++) {
139+
if (ClientScriptRunner.ENABLED_SKILLS[skill]) {
140+
value += Player.baseLevels[skill];
146141
}
147142
}
148-
}
149-
if (opcode == 10) {
150-
int i_23_ = opcodes[scriptDataIndex++] << 16;
151-
i_23_ += opcodes[scriptDataIndex++];
152-
GameInterface gameInterface = GameInterface.getInterface(i_23_);
153-
int i_24_ = opcodes[scriptDataIndex++];
154-
if (i_24_ != -1 && (!ItemDefinition.forId(i_24_, 10).members || MovedStatics.membersWorld)) {
155-
for (int i_25_ = 0; gameInterface.items.length > i_25_; i_25_++) {
156-
if (i_24_ + 1 == gameInterface.items[i_25_]) {
157-
operand = 999999999;
143+
} else if (opcode == 10) {
144+
// inv_contains
145+
int componentId = script[pc++] << 16;
146+
componentId += script[pc++];
147+
GameInterface otherComponent = GameInterface.getInterface(componentId);
148+
int objType = script[pc++];
149+
if (objType != -1 && (!ItemDefinition.forId(objType, 10).members || MovedStatics.membersWorld)) {
150+
for (int slot = 0; otherComponent.invSlotObjId.length > slot; slot++) {
151+
if (objType + 1 == otherComponent.invSlotObjId[slot]) {
152+
value = 999999999;
158153
break;
159154
}
160155
}
161156
}
157+
} else if (opcode == 11) {
158+
// runenergy
159+
value = ClientScriptRunner.runEnergy;
160+
} else if (opcode == 12) {
161+
// runweight
162+
value = MovedStatics.runWeight;
163+
} else if (opcode == 13) {
164+
// testbit
165+
int varpValue = VarPlayerDefinition.varPlayers[script[pc++]];
166+
int bit = script[pc++];
167+
value = (1 << bit & varpValue) != 0 ? 1 : 0;
168+
} else if (opcode == 14) {
169+
// getvarbit
170+
int varbit = script[pc++];
171+
value = VarbitDefinition.getVarbitValue(varbit);
172+
} else if (opcode == 15) {
173+
nextAccumulatorMode = 1;
174+
} else if (opcode == 16) {
175+
nextAccumulatorMode = 2;
176+
} else if (opcode == 17) {
177+
nextAccumulatorMode = 3;
178+
} else if (opcode == 18) {
179+
value = (Player.localPlayer.worldX >> 7) + MovedStatics.baseX;
180+
} else if (opcode == 19) {
181+
value = (Player.localPlayer.worldY >> 7) + MovedStatics.baseY;
182+
} else if (opcode == 20) {
183+
// push_int
184+
value = script[pc++];
162185
}
163-
if (opcode == 11) {
164-
operand = ClientScriptRunner.runEnergy;
165-
}
166-
if (opcode == 12) {
167-
operand = MovedStatics.carryWeight;
168-
}
169-
if (opcode == 13) {
170-
int varPlayerValue = VarPlayerDefinition.varPlayers[opcodes[scriptDataIndex++]];
171-
int leastSignificantBit = opcodes[scriptDataIndex++];
172-
operand = (1 << leastSignificantBit & varPlayerValue) != 0 ? 1 : 0;
173-
}
174-
if (opcode == 14) {
175-
int varbitId = opcodes[scriptDataIndex++];
176-
operand = VarbitDefinition.getVarbitValue(varbitId);
177-
}
178-
if (opcode == 18) {
179-
operand = (Player.localPlayer.worldX >> 7) + MovedStatics.baseX;
180-
}
181-
if (opcode == 19) {
182-
operand = (Player.localPlayer.worldY >> 7) + MovedStatics.baseY;
183-
}
184-
if (opcode == 20) {
185-
operand = opcodes[scriptDataIndex++];
186-
}
187-
if (nextOperator == 0) {
186+
187+
if (nextAccumulatorMode == 0) {
188188
if (operator == 0) {
189-
i += operand;
190-
}
191-
if (operator == 1) {
192-
i -= operand;
193-
}
194-
if (operator == 2 && operand != 0) {
195-
i /= operand;
196-
}
197-
if (operator == 3) {
198-
i *= operand;
189+
accumulator += value;
190+
} else if (operator == 1) {
191+
accumulator -= value;
192+
} else if (operator == 2 && value != 0) {
193+
accumulator /= value;
194+
} else if (operator == 3) {
195+
accumulator *= value;
199196
}
197+
200198
operator = 0;
201199
} else {
202-
operator = nextOperator;
200+
operator = nextAccumulatorMode;
203201
}
204202
}
205203
} catch (Exception exception) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.runejs.client.cache.cs;
2+
3+
public class GoSubFrame {
4+
5+
public ClientScript script;
6+
public int[] intLocals;
7+
public String[] stringLocals;
8+
public int pc = -1;
9+
10+
}

src/main/java/org/runejs/client/cache/cs/InvokedScript.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)