|
2 | 2 | exception BoxFittingError of string |
3 | 3 |
|
4 | 4 | type fillers = |
5 | | - { nop_code:int list ; nop_code_alt:int list; fillers:int list array } |
| 5 | + { nop_code:int list ; nop_code_alt:int list; fillers:int list array; rewriting:(int list * int list) list } |
6 | 6 | let default_fillers () = { |
7 | 7 | nop_code = |
8 | 8 | if !Settings.game = Ruby || !Settings.game = Sapphire then |
@@ -40,6 +40,8 @@ let default_fillers () = { |
40 | 40 | [0x00 ; 0x00 ; 0xFF ; 0x00](* 00FF0000 *) ; |
41 | 41 | [0x00 ; 0x00 ; 0x00 ; 0xFF](* FF000000 *) ; |
42 | 42 | |] |
| 43 | + ; |
| 44 | + rewriting = [] |
43 | 45 | } |
44 | 46 |
|
45 | 47 | let padding = [0x00 ; 0x00 ; 0x00 ; 0x00] |
@@ -200,11 +202,32 @@ let fit_codes_into_boxes ?(fill_last=true) ?(fillers) ?(start=0) ?(exit=None) co |
200 | 202 | let res = res@paddings in |
201 | 203 | add_codes_after ~final:true fillers res ecode |
202 | 204 | in |
203 | | - (* Split by box *) |
204 | 205 | let res, unformatted = List.map fst res, (res |> regroup_by 4 |> unpack) in |
205 | 206 | let unformatted = unformatted |> List.map (fun (cmd, b) -> (Name.command_for_codes cmd, b)) in |
| 207 | + (* Apply rewriting rules *) |
| 208 | + let replace pos lst = |
| 209 | + let matches (pre,post) = |
| 210 | + let k = List.length pre in |
| 211 | + k = List.length post && k mod m = 0 && |
| 212 | + let lst = List.drop pos lst |> List.take k in |
| 213 | + List.equal Int.equal lst pre |
| 214 | + in |
| 215 | + match List.find_opt matches fillers.rewriting with |
| 216 | + | None -> lst |
| 217 | + | Some (_,post) -> |
| 218 | + let k = List.length post in |
| 219 | + List.concat [List.take pos lst ; post ; List.drop (pos+k) lst] |
| 220 | + in |
| 221 | + let rec rewrite pos lst = |
| 222 | + if pos + m > List.length lst then lst |
| 223 | + else |
| 224 | + let lst = replace pos lst in |
| 225 | + rewrite (pos+m) lst |
| 226 | + in |
| 227 | + let res = rewrite 0 res in |
| 228 | + (* Split by box *) |
206 | 229 | let res = split_raw_into_boxes ~fill_last res in |
207 | | - (* If a box has a padding... *) |
| 230 | + (* If a box has a padding... replace with nop_code or nop_code_alt *) |
208 | 231 | let res = res |> List.mapi (fun i lst -> |
209 | 232 | let pos = modulo (-i*(name_size+1)) m in |
210 | 233 | let rec replace_if_padding first pos lst = |
|
0 commit comments