mirror of
https://github.com/pret/pokemon-reverse-engineering-tools.git
synced 2026-03-21 17:24:42 -05:00
Fix enough py3 issues for pokeyellow to finish building
Mostly xrange (screw it, just use range), spurious tabs, and division.
This commit is contained in:
parent
c71e7dd977
commit
39c0d6d94b
|
|
@ -19,7 +19,7 @@ def split(list_, interval):
|
|||
"""
|
||||
Split a list by length.
|
||||
"""
|
||||
for i in xrange(0, len(list_), interval):
|
||||
for i in range(0, len(list_), interval):
|
||||
j = min(i + interval, len(list_))
|
||||
yield list_[i:j]
|
||||
|
||||
|
|
@ -249,7 +249,7 @@ def flatten(planar):
|
|||
bottom = bottom
|
||||
top = top
|
||||
strip = []
|
||||
for i in xrange(7,-1,-1):
|
||||
for i in range(7,-1,-1):
|
||||
color = (
|
||||
(bottom >> i & 1) +
|
||||
(top *2 >> i & 2)
|
||||
|
|
@ -268,10 +268,10 @@ def to_lines(image, width):
|
|||
height = len(image) / width
|
||||
|
||||
lines = []
|
||||
for cur_line in xrange(height):
|
||||
for cur_line in range(height):
|
||||
tile_row = cur_line / tile_height
|
||||
line = []
|
||||
for column in xrange(num_columns):
|
||||
for column in range(num_columns):
|
||||
anchor = (
|
||||
num_columns * tile_row * tile_width * tile_height +
|
||||
column * tile_width * tile_height +
|
||||
|
|
@ -292,7 +292,7 @@ def dmg2rgb(word):
|
|||
value >>= 5
|
||||
word = shift(word)
|
||||
# distribution is less even w/ << 3
|
||||
red, green, blue = [int(color * 8.25) for color in [next(word) for _ in xrange(3)]]
|
||||
red, green, blue = [int(color * 8.25) for color in [next(word) for _ in range(3)]]
|
||||
alpha = 255
|
||||
return (red, green, blue, alpha)
|
||||
|
||||
|
|
@ -448,7 +448,7 @@ def convert_2bpp_to_png(image, **kwargs):
|
|||
trailing = len(image) % pic_length
|
||||
|
||||
pic = []
|
||||
for i in xrange(0, len(image) - trailing, pic_length):
|
||||
for i in range(0, len(image) - trailing, pic_length):
|
||||
pic += transpose_tiles(image[i:i+pic_length], h)
|
||||
image = bytearray(pic) + image[len(image) - trailing:]
|
||||
|
||||
|
|
@ -522,7 +522,7 @@ def get_pic_animation(tmap, w, h):
|
|||
base = frames.pop(0)
|
||||
bitmasks = []
|
||||
|
||||
for i in xrange(len(frames)):
|
||||
for i in range(len(frames)):
|
||||
frame_text += '\tdw .frame{}\n'.format(i + 1)
|
||||
|
||||
for i, frame in enumerate(frames):
|
||||
|
|
@ -648,7 +648,7 @@ def png_to_2bpp(filein, **kwargs):
|
|||
palette = []
|
||||
for line in rgba:
|
||||
newline = []
|
||||
for px in xrange(0, len(line), len_px):
|
||||
for px in range(0, len(line), len_px):
|
||||
color = dict(zip('rgba', line[px:px+len_px]))
|
||||
if color not in palette:
|
||||
if len(palette) < 4:
|
||||
|
|
@ -704,11 +704,11 @@ def png_to_2bpp(filein, **kwargs):
|
|||
num_rows = max(height, tile_height) / tile_height
|
||||
image = []
|
||||
|
||||
for row in xrange(num_rows):
|
||||
for column in xrange(num_columns):
|
||||
for row in range(num_rows):
|
||||
for column in range(num_columns):
|
||||
|
||||
# Split it up into strips to convert to planar data
|
||||
for strip in xrange(min(tile_height, height)):
|
||||
for strip in range(min(tile_height, height)):
|
||||
anchor = (
|
||||
row * num_columns * tile_width * tile_height +
|
||||
column * tile_width +
|
||||
|
|
@ -737,10 +737,10 @@ def png_to_2bpp(filein, **kwargs):
|
|||
tile_width = width / 8
|
||||
trailing = len(tiles) % pic_length
|
||||
new_image = []
|
||||
for block in xrange(len(tiles) / pic_length):
|
||||
for block in range(len(tiles) / pic_length):
|
||||
offset = (h * tile_width) * ((block * w) / tile_width) + ((block * w) % tile_width)
|
||||
pic = []
|
||||
for row in xrange(h):
|
||||
for row in range(h):
|
||||
index = offset + (row * tile_width)
|
||||
pic += tiles[index:index + w]
|
||||
new_image += transpose(pic, w)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from bisect import bisect_left, bisect_right
|
||||
from itertools import izip
|
||||
try:
|
||||
from future_builtins import zip
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
class IntervalMap(object):
|
||||
"""
|
||||
|
|
@ -75,7 +79,7 @@ class IntervalMap(object):
|
|||
((low_bound, high_bound), value)
|
||||
these items are returned in order"""
|
||||
previous_bound = None
|
||||
for (b, v) in izip(self._bounds, self._items):
|
||||
for (b, v) in zip(self._bounds, self._items):
|
||||
if v is not None:
|
||||
yield (previous_bound, b), v
|
||||
previous_bound = b
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
"""
|
||||
Pokemon Crystal data de/compression.
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
"""
|
||||
A rundown of Pokemon Crystal's compression scheme:
|
||||
|
|
@ -9,7 +10,6 @@ A rundown of Pokemon Crystal's compression scheme:
|
|||
Control commands occupy bits 5-7.
|
||||
Bits 0-4 serve as the first parameter <n> for each command.
|
||||
"""
|
||||
from __future__ import print_function
|
||||
lz_commands = {
|
||||
'literal': 0, # n values for n bytes
|
||||
'iterate': 1, # one value for n bytes
|
||||
|
|
@ -45,8 +45,8 @@ lz_end = 0xff
|
|||
|
||||
|
||||
bit_flipped = [
|
||||
sum(((byte >> i) & 1) << (7 - i) for i in xrange(8))
|
||||
for byte in xrange(0x100)
|
||||
sum(((byte >> i) & 1) << (7 - i) for i in range(8))
|
||||
for byte in range(0x100)
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -190,7 +190,7 @@ class Compressed:
|
|||
)
|
||||
for method in self.lookback_methods:
|
||||
min_score = self.min_scores[method]
|
||||
for address in xrange(self.address+1, self.address+best_score):
|
||||
for address in range(self.address+1, self.address+best_score):
|
||||
length, index = self.find_lookback(method, address)
|
||||
if length > max(min_score, best_score):
|
||||
# BUG: lookbacks can reduce themselves. This appears to be a bug in the target also.
|
||||
|
|
@ -212,7 +212,7 @@ class Compressed:
|
|||
|
||||
def find_lookback(self, method, address=None):
|
||||
"""Temporarily stubbed, because the real function doesn't run in polynomial time."""
|
||||
return 0, None
|
||||
return 0, None
|
||||
|
||||
def broken_find_lookback(self, method, address=None):
|
||||
if address is None:
|
||||
|
|
@ -544,7 +544,7 @@ class Decompressed:
|
|||
Write alternating bytes.
|
||||
"""
|
||||
alts = [next(self), next(self)]
|
||||
self.output += [ alts[x & 1] for x in xrange(self.length) ]
|
||||
self.output += [ alts[x & 1] for x in range(self.length) ]
|
||||
|
||||
def blank(self):
|
||||
"""
|
||||
|
|
@ -576,6 +576,6 @@ class Decompressed:
|
|||
self.get_offset()
|
||||
self.direction = direction
|
||||
# Note: appends must be one at a time (this way, repeats can draw from themselves if required)
|
||||
for i in xrange(self.length):
|
||||
for i in range(self.length):
|
||||
byte = self.output[ self.offset + i * direction ]
|
||||
self.output.append( table[byte] if table else byte )
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
A library for use with compressed monster and trainer pics in pokered.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
|
@ -29,14 +30,14 @@ class Decompressor:
|
|||
Ported to python 2.7 from the python 3 code at https://github.com/magical/pokemon-sprites-rby.
|
||||
"""
|
||||
|
||||
table1 = [(2 << i) - 1 for i in xrange(16)]
|
||||
table1 = [(2 << i) - 1 for i in range(16)]
|
||||
table2 = [
|
||||
[0x0, 0x1, 0x3, 0x2, 0x7, 0x6, 0x4, 0x5, 0xf, 0xe, 0xc, 0xd, 0x8, 0x9, 0xb, 0xa],
|
||||
[0xf, 0xe, 0xc, 0xd, 0x8, 0x9, 0xb, 0xa, 0x0, 0x1, 0x3, 0x2, 0x7, 0x6, 0x4, 0x5], # prev ^ 0xf
|
||||
[0x0, 0x8, 0xc, 0x4, 0xe, 0x6, 0x2, 0xa, 0xf, 0x7, 0x3, 0xb, 0x1, 0x9, 0xd, 0x5],
|
||||
[0xf, 0x7, 0x3, 0xb, 0x1, 0x9, 0xd, 0x5, 0x0, 0x8, 0xc, 0x4, 0xe, 0x6, 0x2, 0xa], # prev ^ 0xf
|
||||
]
|
||||
table3 = [bitflip(i, 4) for i in xrange(16)]
|
||||
table3 = [bitflip(i, 4) for i in range(16)]
|
||||
|
||||
tilesize = 8
|
||||
|
||||
|
|
@ -118,7 +119,7 @@ class Decompressor:
|
|||
a = self._readint(i + 1)
|
||||
n += a
|
||||
|
||||
for i in xrange(n):
|
||||
for i in range(n):
|
||||
ram.append(0)
|
||||
|
||||
def _read_data_chunk(self, ram, size):
|
||||
|
|
@ -135,9 +136,9 @@ class Decompressor:
|
|||
if mirror is None:
|
||||
mirror = self.mirror
|
||||
|
||||
for x in xrange(self.sizex):
|
||||
for x in range(self.sizex):
|
||||
bit = 0
|
||||
for y in xrange(self.sizey):
|
||||
for y in range(self.sizey):
|
||||
i = y * self.sizex + x
|
||||
a = (ram[i] >> 4) & 0xf
|
||||
b = ram[i] & 0xf
|
||||
|
|
@ -158,7 +159,7 @@ class Decompressor:
|
|||
if mirror is None:
|
||||
mirror = self.mirror
|
||||
|
||||
for i in xrange(len(ram2)):
|
||||
for i in range(len(ram2)):
|
||||
if mirror:
|
||||
a = (ram2[i] >> 4) & 0xf
|
||||
b = ram2[i] & 0xf
|
||||
|
|
@ -170,10 +171,10 @@ class Decompressor:
|
|||
|
||||
def _deinterlace_bitgroups(self, bits):
|
||||
l = []
|
||||
for y in xrange(self.sizey):
|
||||
for x in xrange(self.sizex):
|
||||
for y in range(self.sizey):
|
||||
for x in range(self.sizex):
|
||||
i = 4 * y * self.sizex + x
|
||||
for j in xrange(4):
|
||||
for j in range(4):
|
||||
l.append(bits[i])
|
||||
i += self.sizex
|
||||
return l
|
||||
|
|
@ -193,12 +194,12 @@ def fbitstream(f):
|
|||
break
|
||||
byte = ord(char)
|
||||
|
||||
for i in xrange(7, -1, -1):
|
||||
for i in range(7, -1, -1):
|
||||
yield (byte >> i) & 1
|
||||
|
||||
def bitstream(b):
|
||||
for byte in b:
|
||||
for i in xrange(7, -1, -1):
|
||||
for i in range(7, -1, -1):
|
||||
yield (byte >> i) & 1
|
||||
|
||||
def readint(bs, count):
|
||||
|
|
@ -211,7 +212,7 @@ def readint(bs, count):
|
|||
|
||||
def bitgroups_to_bytes(bits):
|
||||
l = []
|
||||
for i in xrange(0, len(bits) - 3, 4):
|
||||
for i in range(0, len(bits) - 3, 4):
|
||||
n = ((bits[i + 0] << 6)
|
||||
| (bits[i + 1] << 4)
|
||||
| (bits[i + 2] << 2)
|
||||
|
|
@ -231,21 +232,21 @@ class Compressor:
|
|||
Adapted from stag019's C compressor.
|
||||
"""
|
||||
|
||||
table1 = [(2 << i) - 1 for i in xrange(16)]
|
||||
table1 = [(2 << i) - 1 for i in range(16)]
|
||||
table2 = [
|
||||
[0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8],
|
||||
[0x8, 0x9, 0xb, 0xa, 0xe, 0xf, 0xd, 0xc, 0x4, 0x5, 0x7, 0x6, 0x2, 0x3, 0x1, 0x0], # reverse
|
||||
]
|
||||
table3 = [bitflip(i, 4) for i in xrange(16)]
|
||||
table3 = [bitflip(i, 4) for i in range(16)]
|
||||
|
||||
def __init__(self, image, width=None, height=None):
|
||||
self.image = bytearray(image)
|
||||
self.size = len(self.image)
|
||||
|
||||
planar_tile = 8 * 8 / 4
|
||||
tile_size = self.size / planar_tile
|
||||
if height and not width: width = tile_size / height
|
||||
elif width and not height: height = tile_size / width
|
||||
planar_tile = 8 * 8 // 4
|
||||
tile_size = self.size // planar_tile
|
||||
if height and not width: width = tile_size // height
|
||||
elif width and not height: height = tile_size // width
|
||||
elif not width and not height: width = height = int(sqrt(tile_size))
|
||||
self.width, self.height = width, height
|
||||
|
||||
|
|
@ -257,7 +258,7 @@ class Compressor:
|
|||
rams = [[],[]]
|
||||
datas = []
|
||||
|
||||
for mode in xrange(3):
|
||||
for mode in range(3):
|
||||
|
||||
# Order is redundant for mode 0.
|
||||
|
||||
|
|
@ -270,10 +271,10 @@ class Compressor:
|
|||
|
||||
# Using order 0 instead of 1 breaks this feature.
|
||||
|
||||
for order in xrange(2):
|
||||
for order in range(2):
|
||||
if mode == 0 and order == 0:
|
||||
continue
|
||||
for i in xrange(2):
|
||||
for i in range(2):
|
||||
rams[i] = self.image[i::2]
|
||||
self._interpret_compress(rams, mode, order)
|
||||
datas += [(self.data[:], int(self.which_bit))]
|
||||
|
|
@ -320,10 +321,10 @@ class Compressor:
|
|||
nums = 0
|
||||
bitgroups = []
|
||||
|
||||
for x in xrange(self.width):
|
||||
for bit in xrange(0, 8, 2):
|
||||
for x in range(self.width):
|
||||
for bit in range(0, 8, 2):
|
||||
byte = x * self.height * 8
|
||||
for y in xrange(self.height * 8):
|
||||
for y in range(self.height * 8):
|
||||
bitgroup = (ram[byte] >> (6 - bit)) & 3
|
||||
if bitgroup == 0:
|
||||
if rle == 0:
|
||||
|
|
@ -378,16 +379,16 @@ class Compressor:
|
|||
v >>= 1
|
||||
bitcount += 1
|
||||
|
||||
for j in xrange(bitcount):
|
||||
for j in range(bitcount):
|
||||
self._writebit(1)
|
||||
self._writebit(0)
|
||||
for j in xrange(bitcount, -1, -1):
|
||||
for j in range(bitcount, -1, -1):
|
||||
self._writebit((number >> j) & 1)
|
||||
|
||||
def _encode(self, ram, mirror=None):
|
||||
a = b = 0
|
||||
for i in xrange(len(ram)):
|
||||
j = i / self.height
|
||||
for i in range(len(ram)):
|
||||
j = i // self.height
|
||||
j += i % self.height * self.width * 8
|
||||
if i % self.height == 0:
|
||||
b = 0
|
||||
|
|
@ -403,7 +404,7 @@ class Compressor:
|
|||
ram[j] = (code_1 << 4) | code_2
|
||||
|
||||
def _xor(self, ram1, ram2):
|
||||
for i in xrange(len(ram2)):
|
||||
for i in range(len(ram2)):
|
||||
ram2[i] ^= ram1[i]
|
||||
|
||||
def _writebit(self, bit):
|
||||
|
|
@ -416,7 +417,7 @@ class Compressor:
|
|||
def _writeint(self, num, size=None):
|
||||
bits = []
|
||||
if size:
|
||||
for i in xrange(size):
|
||||
for i in range(size):
|
||||
bits += [num & 1]
|
||||
num >>= 1
|
||||
else:
|
||||
|
|
@ -469,7 +470,7 @@ def compress_file(filename):
|
|||
pic = bytearray(pic)
|
||||
output_filename = os.path.splitext(filename)[0] + '.pic'
|
||||
with open(output_filename, 'wb') as out:
|
||||
out.write(pic)
|
||||
out.write(pic)
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
|||
|
|
@ -566,11 +566,11 @@ class Preprocessor(object):
|
|||
if show_original_lines:
|
||||
sys.stdout.write("; original_line: " + original_line)
|
||||
|
||||
# rgbasm can handle other macros too
|
||||
# rgbasm can handle other macros too
|
||||
if "is_rgbasm_macro" in dir(macro):
|
||||
if macro.is_rgbasm_macro:
|
||||
sys.stdout.write(original_line)
|
||||
return
|
||||
return
|
||||
|
||||
# certain macros don't need an initial byte written
|
||||
# do: all scripting macros
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user