25
25
org_pos = 0
26
26
copy_pos = 0
27
27
28
- def string_to_array (s ):
29
- return [c for c in s ]
30
-
31
28
def array_find (index , arr1 , arr2 ):
32
29
m = len (arr1 )
33
30
n = len (arr2 )
@@ -38,7 +35,7 @@ def array_find(index, arr1, arr2):
38
35
#copy = arr1[i:i+n+3]
39
36
#print(copy)
40
37
if arr1 [i + n ]== 0 :
41
- print ("found org" )
38
+ # print("found org")
42
39
global org_pos
43
40
org_pos = i
44
41
return i
@@ -55,92 +52,59 @@ def array_find(index, arr1, arr2):
55
52
break
56
53
global cfunc
57
54
cfunc = arr1 [istart :istop ]
58
- print ("found: " + str (cfunc ))
55
+ # print("found: "+str(cfunc))
59
56
global copy_pos
60
57
copy_pos = istart
61
58
return istart
62
59
return - 1
63
60
64
- def isSubset (arr1 , arr2 ):
65
- m = len (arr1 )
66
- n = len (arr2 )
67
- i = 0
68
- j = 0
69
- for i in range (n ):
70
- for j in range (m ):
71
- if (arr2 [i ] == arr1 [j ]):
72
- break
73
-
74
- if (j == m ):
75
- return - 1
76
- return j
61
+ # always 2 functions to patch
62
+ def find_and_patch (source , sub ):
63
+ global org_pos
64
+ org_pos = 0
65
+ global copy_pos
66
+ copy_pos = 0
67
+ offset = array_find (0 , source , sub )
68
+ offset = array_find (offset + len (sub ), source , sub )
69
+ #print(org_pos,copy_pos)
70
+ if org_pos == 0 or copy_pos == 0 :
71
+ print (sub .decode ("utf-8" )+ " already patched or not needed" )
72
+ else :
73
+ print ("patching: " + sub .decode ("utf-8" ))
74
+ patch = sub
75
+ # patch with leading x
76
+ patch [0 ] = 120
77
+ patch .append (0 )
78
+ #print(patch)
79
+ source [copy_pos :copy_pos + len (patch )] = patch
80
+ source [org_pos :org_pos + len (patch )] = patch
81
+
77
82
78
83
def patch_builtins (source , target , env ):
79
84
print ("patching buildtins: " + file )
80
85
path = str (target [0 ])
81
86
directory = "/" .join (list (path .split ('/' )[0 :- 1 ]))
82
87
dpath = directory + "/src/Plugins/"
83
88
fpath = dpath + file
84
- #print(fpath)
85
-
89
+
86
90
with open (fpath , mode = 'rb' ) as f :
87
91
source = f .read ()
88
92
f .close ()
89
-
90
93
source = bytearray (source )
91
94
92
- print (len (source ))
93
-
94
- sub = bytearray ("__addsf3" , 'utf-8' )
95
+ # list patches here
96
+ patches = ["__addsf3" , "__subsf3" , "__mulsf3" , "__divsf3" , "__nesf2" ]
95
97
96
- # always 2 functions to patch
97
- global org_pos
98
- orgpos = 0
99
- global copy_pos
100
- copy_pos = 0
101
- offset = array_find (0 , source , sub )
102
- offset = array_find (offset + len (sub ), source , sub )
103
-
104
- if org_pos == 0 or copy_pos == 0 :
105
- print ("already patched" )
106
-
107
- print (org_pos ,copy_pos )
98
+ for x in patches :
99
+ sub = bytearray (x , 'utf-8' )
100
+ find_and_patch (source , sub )
108
101
109
- global cfunc
110
- # replace org with copy and invalidate org
111
- if org_pos > copy_pos :
112
- # move part 1
113
- len1 = copy_pos
114
- len2 = org_pos - copy_pos - len (cfunc )
115
- len3 = len (source ) - org_pos - len (sub )
116
- print (len1 ,len (cfunc ),len2 ,len (sub ),len3 )
102
+ #fpath = dpath + "test.o"
117
103
118
- print (len1 + len (cfunc )+ len2 + len (sub )+ len3 )
119
-
120
- sum = len1 + len (cfunc )+ len2 + len (sub )+ len3
121
- print (len (source ))
122
-
123
- part1 = bytearray (len1 )
124
- part2 = bytearray (len2 )
125
- part3 = bytearray (len3 )
126
-
127
- part1 [0 :len1 ] = source [0 :len1 ]
128
- part2 [0 :len2 ] = source [copy_pos + len (cfunc ):org_pos ]
129
- part3 [0 :len3 ] = source [org_pos + len (sub ):len (source )]
130
-
131
- source [copy_pos :len (sub )] = sub
132
- source [copy_pos + len (sub ):len2 ] = part2
133
- source [copy_pos + len (sub )+ len2 :] = cfunc
134
- source [copy_pos + len (sub )+ len2 + len (cfunc ):] = part3
135
-
136
-
137
- #fpath = dpath + "test.o"
138
-
139
- with open (fpath , mode = 'wb' ) as f :
140
- f .write (source )
141
- f .close ()
104
+ with open (fpath , mode = 'wb' ) as f :
105
+ f .write (source )
106
+ f .close ()
142
107
143
- print (len (part1 ),len (part2 ),len (part3 ))
144
108
145
109
env .AddPostAction ("$BUILD_DIR/${PROGNAME}.bin" , [patch_builtins ])
146
110
0 commit comments