-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgssadv3.go
154 lines (122 loc) · 3.19 KB
/
rgssadv3.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package rpgo
import (
"encoding/binary"
"fmt"
"io"
)
//RPGMakerDecrypter.Decrypter/RGSSADv3.cs
type RGSSADv3 RGSSAD
// NewRGSSADv3
//
// Creates a new RGSSADv3 structure and configures it for use.
//
// Returns a pointer to the created structure, nil on success, nil and error
// otherwise.
func NewRGSSADv3(filepath string) (created *RGSSADv3, err error) {
realNew, err := NewRGSSAD(filepath)
if err != nil {
return nil, err
}
created = (*RGSSADv3)(realNew)
version, err := ((*RGSSAD)(created)).GetVersion()
if version != RGASSDv3 || err != nil {
return nil, fmt.Errorf("rpgo/rgssadv3: version not v3 or this:\n%s", err.Error())
}
created.readRGSSAD()
return created, nil
}
// Reads the encrypted RGSSADv3 archive and generates a ArchivedFile slice for
// the RGSSAD structure.
//
// This function is meant for internal use in NewRGSSADv3.
func (rpg *RGSSADv3) readRGSSAD() {
_, err := rpg.ByteReader.Seek(8, io.SeekStart)
if err != nil {
return
}
t := make([]byte, 4)
_, err = rpg.ByteReader.Read(t)
if err != nil {
return
}
num := int(binary.LittleEndian.Uint32(t))
key := uint(num)
key *= 9
key += 3
for {
newArchivedFile := new(RPGMakerArchivedFile)
// OFFSET
_, err = rpg.ByteReader.Read(t)
if err != nil {
return
}
num = int(binary.LittleEndian.Uint32(t))
newArchivedFile.Offset = int64(rpg.decryptInteger(num, key))
// SIZE
_, err = rpg.ByteReader.Read(t)
if err != nil {
return
}
num = int(binary.LittleEndian.Uint32(t))
newArchivedFile.Size = rpg.decryptInteger(num, key)
// KEY
_, err = rpg.ByteReader.Read(t)
if err != nil {
return
}
num = int(binary.LittleEndian.Uint32(t))
newArchivedFile.Key = uint(rpg.decryptInteger(num, key))
if newArchivedFile.Offset == 0 {
break
}
// NAME
_, err = rpg.ByteReader.Read(t)
if err != nil {
return
}
num := int(binary.LittleEndian.Uint32(t))
nameLen := rpg.decryptInteger(num, key)
u := make([]byte, nameLen)
_, err = rpg.ByteReader.Read(u)
if err != nil {
return
}
newArchivedFile.Name = rpg.decryptFilename(u, key)
rpg.ArchivedFiles = append(rpg.ArchivedFiles, *newArchivedFile)
}
}
// Decrypts an integer from the RGSSADv3 archive.
//
// This function is meant for internal use by readRGSSAD.
//
// Returns the decrypted integer.
func (*RGSSADv3) decryptInteger(value int, key uint) int {
result := int64(value) ^ int64(key)
return int(result)
}
// Decrypts a filename from the RGSSADv3 archive.
//
// This function is meant for internal use by readRGSSAD.
//
// Returns the decrypted filename as a string.
func (*RGSSADv3) decryptFilename(encryptedName []byte, key uint) (decryptedName string) {
keyBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(keyBytes, uint32(key))
i := 0
j := 0
for i < len(encryptedName) {
j %= 4
decryptedName += string(rune(encryptedName[i] ^ keyBytes[j]))
i++
j++
}
return decryptedName
}
// ExtractAllFiles
//
// See ExtractAllFiles in rgssad.go.
// A wrapper for ExtractAllFiles to remove the need for end-user casting to
// *RGSSAD.
func (rpg *RGSSADv3) ExtractAllFiles(outputDirectoryPath string, overrideExisting bool) error {
return (*RGSSAD)(rpg).ExtractAllFiles(outputDirectoryPath, overrideExisting)
}