forked from KatDevsGames/z3randomizer
-
Notifications
You must be signed in to change notification settings - Fork 6
/
decryption.asm
100 lines (77 loc) · 2.63 KB
/
decryption.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
!CryptoBuffer = "$7F5100"
;!keyBase = "$7F50D0"
;--------------------------------------------------------------------------------
LoadStaticDecryptionKey:
PHB : PHA : PHX : PHY : PHP
REP #$30 ; set 16-bit accumulator & index registers
LDX.w #StaticDecryptionKey ; Source
LDY.w #!keyBase ; Target
LDA.w #$000F ; Length
MVN $307F
PLP : PLY : PLX : PLA : PLB
RTL
;--------------------------------------------------------------------------------
;--------------------------------------------------------------------------------
RetrieveValueFromEncryptedTable:
;Requires 16-bit A and X/Y
;$00-$02 are long address of start of table
;A is desired byte offset into table
;Returns result in A
PHX : PHY
PHA
LDY $04 : PHY : LDY $06 : PHY : LDY $08 : PHY
LDY $0A : PHY : LDY $0C : PHY : LDY $0E : PHY
AND.w #$FFF8 : TAY
LDA [$00], Y : STA.l !CryptoBuffer : INY #2
LDA [$00], Y : STA.l !CryptoBuffer+2 : INY #2
LDA [$00], Y : STA.l !CryptoBuffer+4 : INY #2
LDA [$00], Y : STA.l !CryptoBuffer+6
LDA.w #$0002 : STA $04 ;set block size
JSL.l XXTEA_Decode
PLA : STA $0E : PLA : STA $0C : PLA : STA $0A
PLA : STA $08 : PLA : STA $06 : PLA : STA $04
PLA : AND.w #$0007 : TAX
LDA.l !CryptoBuffer, X
PHA
LDA.w #$0000
STA.l !CryptoBuffer
STA.l !CryptoBuffer+2
STA.l !CryptoBuffer+4
STA.l !CryptoBuffer+6
PLA
PLY : PLX
RTL
;--------------------------------------------------------------------------------
!ChestData = "$01E96C"
!ChestData_Payload = "$1EABC" ; !ChestData+$0150
!ChestPlayer_Payload = "$30E290"
;--------------------------------------------------------------------------------
GetChestData:
LDA.l IsEncrypted : BNE .encrypted
INC $0E : LDX.w #$FFFD ; what we wrote over
JML.l Dungeon_OpenKeyedObject_nextChest
.encrypted
INC $0E : LDX.w #$FFFE
.nextChest
INX #2 : CPX.w #$0150 : BEQ .couldntFindChest
LDA !ChestData, X : AND.w #$7FFF : CMP $A0 : BNE .nextChest
DEC $0E : BNE .nextChest
LDA $00 : PHA : LDA $02 : PHA
LDA.w #!ChestData_Payload : STA $00
LDA.w #!ChestData_Payload>>16 : STA $02
TXA : LSR
JSL RetrieveValueFromEncryptedTable
STA $0C
LDA.w #!ChestPlayer_Payload : STA $00
LDA.w #!ChestPlayer_Payload>>16 : STA $02
TXA : LSR
JSL RetrieveValueFromEncryptedTable
STA !MULTIWORLD_ITEM_PLAYER_ID
PLA : STA $02 : PLA : STA $00
LDA !ChestData, X : ASL A : BCC .smallChest
JML.l Dungeon_OpenKeyedObject_bigChest ;(bank01.asm line #13783)
.smallChest
JML.l Dungeon_OpenKeyedObject_smallChest
.couldntFindChest
JML.l Dungeon_OpenKeyedObject_couldntFindChest
;--------------------------------------------------------------------------------