Skip to content

Commit 65deb2a

Browse files
AXiX-officialK0lb3
authored andcommitted
some fix for Python api usage according to comment
1 parent a3a5cd5 commit 65deb2a

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

UnityPy/UnityPyBoost.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class TypeTreeNode:
3939
_clean_name: str
4040

4141
def decrypt_block(
42-
index_bytes: bytes,
43-
substitute_bytes: bytes,
42+
index_bytes: Union[bytes, bytearray],
43+
substitute_bytes: Union[bytes, bytearray],
4444
data: Union[bytes, bytearray],
4545
index: int,
46-
) -> bytearray: ...
46+
) -> bytes: ...

UnityPy/helpers/ArchiveStorageManager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def __init__(self, reader: EndianBinaryReader) -> None:
108108
def decrypt_block(self, data: bytes, index: int):
109109

110110
if UnityPyBoost:
111-
return UnityPyBoost.decrypt_block(bytes(self.index), bytes(self.substitute), data, index)
111+
return UnityPyBoost.decrypt_block(self.index, self.substitute, data, index)
112112

113113
offset = 0
114114
size = len(data)

UnityPyBoost/ArchiveStorageDecryptor.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "ArchiveStorageDecryptor.hpp"
44
#include <Python.h>
55

6-
inline unsigned char decrypt_byte(unsigned char *bytes, uint64_t& offset, uint64_t& index, unsigned char *index_data, unsigned char *substitute_data)
6+
inline unsigned char decrypt_byte(unsigned char *bytes, uint64_t& offset, uint64_t& index, const unsigned char *index_data, const unsigned char *substitute_data)
77
{
88
unsigned char count_byte = substitute_data[((index >> 2) & 3) + 4]
99
+ substitute_data[index & 3]
@@ -15,7 +15,7 @@ inline unsigned char decrypt_byte(unsigned char *bytes, uint64_t& offset, uint64
1515
return count_byte;
1616
}
1717

18-
inline uint64_t decrypt(unsigned char *bytes, uint64_t index, uint64_t remaining, unsigned char *index_data, unsigned char *substitute_data)
18+
inline uint64_t decrypt(unsigned char *bytes, uint64_t index, uint64_t remaining, const unsigned char *index_data, const unsigned char *substitute_data)
1919
{
2020
uint64_t offset = 0;
2121

@@ -53,39 +53,37 @@ inline uint64_t decrypt(unsigned char *bytes, uint64_t index, uint64_t remaining
5353
}
5454

5555
PyObject *decrypt_block(PyObject *self, PyObject *args) {
56-
PyObject *py_index_bytes;
57-
PyObject *py_substitute_bytes;
58-
PyObject *py_data;
56+
Py_buffer index_data;
57+
Py_buffer substitute_data;
58+
Py_buffer data;
5959
uint64_t index;
6060

61-
if (!PyArg_ParseTuple(args, "OOOi", &py_index_bytes, &py_substitute_bytes, &py_data, &index)) {
61+
if (!PyArg_ParseTuple(args, "y*y*y*K", &index_data, &substitute_data, &data, &index)) {
62+
if (index_data.buf) PyBuffer_Release(&index_data);
63+
if (substitute_data.buf) PyBuffer_Release(&substitute_data);
64+
if (data.buf) PyBuffer_Release(&data);
6265
return NULL;
6366
}
6467

65-
PyObject *result = PyBytes_FromObject(py_data);
66-
67-
Py_buffer view;
68-
if (PyObject_GetBuffer(result, &view, PyBUF_SIMPLE) != 0) {
69-
return NULL;
70-
}
71-
72-
if (!PyBytes_Check(py_index_bytes) || !PyBytes_Check(py_substitute_bytes)) {
73-
PyBuffer_Release(&view);
74-
PyErr_SetString(PyExc_TypeError, "Attributes 'index' and 'substitute' must be bytes");
68+
PyObject *result = PyBytes_FromStringAndSize(NULL, data.len);
69+
if (result == NULL) {
70+
PyBuffer_Release(&index_data);
71+
PyBuffer_Release(&substitute_data);
72+
PyBuffer_Release(&data);
7573
return NULL;
7674
}
7775

78-
unsigned char *data = (unsigned char *)view.buf;
79-
uint64_t size = (uint64_t)view.len;
80-
unsigned char *index_data = (unsigned char *)PyBytes_AS_STRING(py_index_bytes);
81-
unsigned char *substitute_data = (unsigned char *)PyBytes_AS_STRING(py_substitute_bytes);
76+
unsigned char *result_raw = (unsigned char *)PyBytes_AS_STRING(result);
77+
memcpy(result_raw, data.buf, data.len);
8278

8379
uint64_t offset = 0;
84-
while (offset < size) {
85-
offset += decrypt(data + offset, index++, size - offset, index_data, substitute_data);
80+
while (offset < data.len) {
81+
offset += decrypt(result_raw + offset, index++, data.len - offset, (unsigned char *)index_data.buf, (unsigned char *)substitute_data.buf);
8682
}
8783

88-
PyBuffer_Release(&view);
84+
PyBuffer_Release(&index_data);
85+
PyBuffer_Release(&substitute_data);
86+
PyBuffer_Release(&data);
8987

9088
return result;
9189
}

0 commit comments

Comments
 (0)