private void WriteCode(AcsInstruction[] code) { for (int j = 0; j < code.Length; j++) { AcsInstruction instruction = code[j]; AcsInstruction next = (j == code.Length - 1 ? null : code[j + 1]); string s = $" /* {instruction.Offset,8} */ > {instruction.Opcode.Name} "; for (int i = 0; i < instruction.Arguments.Length; i++) { if (i == 0) { if (MapVariables.ContainsKey(instruction.Arguments[0]) && AcsInstruction.OpcodesAreEqual(instruction.Opcode, OpcodeEnum.PushMapVar)) { s += MapVariables[instruction.Arguments[0]].Name; } else if (AcsInstruction.OpcodesAreEqual(instruction.Opcode, OpcodeEnum.Call) || AcsInstruction.OpcodesAreEqual(instruction.Opcode, OpcodeEnum.CallDiscard)) { if (instruction.Arguments[0] >= 0 && instruction.Arguments[0] < FunctionList.Count) { s += FunctionList[instruction.Arguments[0]].Name; } else { s += instruction.Arguments[0] + " // unknown function!"; } } else { s += instruction.Arguments[i]; } } else { s += instruction.Arguments[i]; } if (i < instruction.Arguments.Length - 1) { s += ", "; } } if (AcsInstruction.OpcodesAreEqual(instruction.Opcode, OpcodeEnum.PushByte) || AcsInstruction.OpcodesAreEqual(instruction.Opcode, OpcodeEnum.PushNumber)) { if (AcsInstruction.OpcodesAreEqual(next.Opcode, OpcodeEnum.TagString) || AcsInstruction.OpcodesAreEqual(next.Opcode, OpcodeEnum.PrintString)) { int si = instruction.Arguments[0]; s += $" // (String table index {si}): " + StringTable[si]; } } WriteLine(s); } }
private void ReadAcs95(ref BinaryReader reader) { int numPointers = reader.ReadInt32(); for (int i = 0; i < numPointers; i++) { int number = reader.ReadInt32(); int pointer = reader.ReadInt32(); int argc = reader.ReadInt32(); int id = number % 1000; int typeNum = number / 1000; ScriptType type = (ScriptType)typeNum; Console.WriteLine("Script " + id + " is of type " + type); long pos = InputStream.Position; AcsScript script = new AcsScript(number, type, argc, pointer); InputStream.Seek(pointer, SeekOrigin.Begin); int len = 0; while (reader.ReadInt32() != (int)OpcodeEnum.Terminate) { len++; } InputStream.Seek(pointer, SeekOrigin.Begin); script.Code = AcsInstruction.ReadCode(this, ref reader, len); InputStream.Seek(pos, SeekOrigin.Begin); Scripts[number] = script; } int stringCount = reader.ReadInt32(); for (int i = 0; i < stringCount; i++) { int pointer = reader.ReadInt32(); long pos = InputStream.Position; InputStream.Seek(pointer, SeekOrigin.Begin); StringTable.Add(ReadString()); InputStream.Seek(pos, SeekOrigin.Begin); } }
private void ReadCode(ref BinaryReader reader) { foreach (var script in Scripts) { int num = script.Key; AcsScript s = script.Value; s.CodeSize = FindClosestPointer(s.Pointer) - s.Pointer; InputStream.Seek(s.Pointer, SeekOrigin.Begin); s.Code = AcsInstruction.ReadCode(this, ref reader, s.CodeSize); } foreach (var func in FunctionList) { func.CodeSize = FindClosestPointer(func.Pointer) - func.Pointer; InputStream.Seek(func.Pointer, SeekOrigin.Begin); func.Code = AcsInstruction.ReadCode(this, ref reader, func.CodeSize); } }