class WindowGraphicsModule(EbModule.EbModule):
    _name = "Window Graphics"
    _ASMPTR_1 = 0x47c47
    _ASMPTR_2 = 0x47caa
    _ASMPTRS_NAMES = [0x1F70F, 0x1F72A, 0x1F745, 0x1F760, 0x1F77B]
    _PREVIEW_SUBPALS = [0, 0, 0, 0, 1, 1, 1, 4, 4, 4, 4, 6, 6, 6, 6, 6, 7, 7, 7,
            7, 7, 7, 7, 4, 4, 4, 4, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 0,
            1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 6, 6, 6,
            6, 3, 3, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 3, 3, 6, 3, 3, 3,
            3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 1, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0,
            0, 0, 0, 1, 0, 0 ]
    def __init__(self):
        self._gfx1 = EbTileGraphics(416, 8, 2)
        self._gfx2 = EbTileGraphics(7, 8, 2)
        self._flavPals = [EbPalettes(8,4) for i in range(7)]
        self._flavNames = [(i,TextTableEntry(None, 25)) for i in self._ASMPTRS_NAMES]
    def freeRanges(self):
        return [(0x200000, 0x20079f)] # Graphics
    def readFromRom(self, rom):
        with EbCompressedData() as tgb1:
            tgb1.readFromRom(rom, EbModule.toRegAddr(
                EbModule.readAsmPointer(rom, self._ASMPTR_1)))
            self._gfx1.readFromBlock(tgb1)
        updateProgress(20)
        with EbCompressedData() as tgb2:
            tgb2.readFromRom(rom, EbModule.toRegAddr(
                EbModule.readAsmPointer(rom, self._ASMPTR_2)))
            self._gfx2.readFromBlock(tgb2)
        updateProgress(20)
        # Read palettes
        loc = 0x201fc8
        for pal in self._flavPals:
            pal.readFromBlock(rom, loc=loc)
            loc += 64
        updateProgress(5)
        # Read names
        for ptr, field in self._flavNames:
            field.readFromRom(rom, EbModule.toRegAddr(
                EbModule.readAsmPointer(rom, ptr)))
        updateProgress(5)
    def writeToRom(self, rom):
        with EbCompressedData(self._gfx1.sizeBlock()) as gb:
            self._gfx1.writeToBlock(gb)
            EbModule.writeAsmPointer(rom, self._ASMPTR_1,
                    EbModule.toSnesAddr(gb.writeToFree(rom)))
        updateProgress(20)
        with EbCompressedData(self._gfx2.sizeBlock()) as gb:
            self._gfx2.writeToBlock(gb)
            EbModule.writeAsmPointer(rom, self._ASMPTR_2,
                    EbModule.toSnesAddr(gb.writeToFree(rom)))
        updateProgress(20)
        # Write pals
        loc = 0x201fc8
        for pal in self._flavPals:
            pal.writeToBlock(rom, loc=loc)
            loc += 64
        updateProgress(5)
        # Write names
        for ptr, field in self._flavNames:
            loc = EbModule.toSnesAddr(field.writeToFree(rom))
            EbModule.writeAsmPointer(rom, ptr, loc)
        updateProgress(5)
    def writeToProject(self, resourceOpener):
        arr1 = EbArrangement(16, 26)
        for i in range(416):
            arr1[i%16,i/16] = (False, False, False, self._PREVIEW_SUBPALS[i], i)
        i = 0
        for pal in self._flavPals:
            with resourceOpener("WindowGraphics/Windows1_" + str(i),
                    "png") as imgFile:
                img1 = arr1.toImage(self._gfx1, pal)
                img1.save(imgFile, "png")
            with resourceOpener("WindowGraphics/Windows2_" + str(i),
                    "png") as imgFile:
                img2 = self._gfx2.dumpToImage(pal.getSubpal(7), width=7)
                img2.save(imgFile, "png")
            i += 1
        updateProgress(40)
        # Write names
        with resourceOpener("WindowGraphics/flavor_names", "txt") as f:
            for ptr, field in self._flavNames:
                print >>f, field.dump()
        updateProgress(10)
    def readFromProject(self, resourceOpener):
        # Read graphics. Just use the first of each image.
        with resourceOpener("WindowGraphics/Windows1_0", "png") as imgFile:
            img = Image.open(imgFile)
            if img.mode != 'P':
                raise RuntimeError("WindowGraphics/Windows1_0 is not an indexed PNG.")
            self._gfx1.loadFromImage(img)
        updateProgress(20)
        with resourceOpener("WindowGraphics/Windows2_0", "png") as imgFile:
            img = Image.open(imgFile)
            if img.mode != 'P':
                raise RuntimeError("WindowGraphics/Windows2_0 is not an indexed PNG.")
            self._gfx2.loadFromImage(img)
        updateProgress(20)
        # Read pals from Windows1 of each flavor.
        # Read subpal 7 from Windows2 of each flavor.
        i = 0
        for pal in self._flavPals:
            # Read all the palette data from Windows1
            with resourceOpener("WindowGraphics/Windows1_" + str(i),
                    "png") as imgFile:
                img = Image.open(imgFile)
                if img.mode != 'P':
                    raise RuntimeError("WindowGraphics/Windows1_" + str(i) + " is not an indexed PNG.")
                palData = img.getpalette()
                m=0
                for j in range(8):
                    for k in range(4):
                        pal[j,k] = (palData[m], palData[m+1], palData[m+2])
                        m += 3
            # Overwrite subpalette 7 from the palette of Windows2
            with resourceOpener("WindowGraphics/Windows2_" + str(i),
                    "png") as imgFile:
                img = Image.open(imgFile)
                if img.mode != 'P':
                    raise RuntimeError("WindowGraphics/Windows2_" + str(i) + " is not an indexed PNG.")
                palData = img.getpalette()
                m=0
                for k in range(4):
                    pal[7,k] = (palData[m], palData[m+1], palData[m+2])
                    m += 3
            i += 1
        updateProgress(5)
        # Read names
        with resourceOpener("WindowGraphics/flavor_names", "txt") as f:
            for ptr, field in self._flavNames:
                field.load(f.readline()[:-1])
        updateProgress(5)
Esempio n. 2
0
class FontModule(EbModule.EbModule):
    _name = "Fonts"
    _CREDITS_PREVIEW_SUBPALS = [
            1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1,
            1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
    _ASMPTR_CREDITS_GFX = 0x4f1a7
    _ADDR_CREDITS_PAL = 0x21e914
    def __init__(self):
        self._fonts = [
                Font(0x210cda, 0x210c7a, 16, 16),
                Font(0x2013b9, 0x201359, 16, 16),
                Font(0x2122fa, 0x21229a, 16, 16),
                Font(0x21193a, 0x2118da, 8, 16),
                Font(0x211f9a, 0x211f3a, 8, 8)
                ]
        self._cfont = EbTileGraphics(192, 8, 2)
        self._cpal = EbPalettes(2, 4)
        self._pct = 50.0/(len(self._fonts)+1)
    def freeRanges(self):
        return [(0x21e528, 0x21e913)] # Credits font graphics
    def readCreditsFontFromRom(self, rom):
        self._cpal.readFromBlock(rom, loc=self._ADDR_CREDITS_PAL)
        with EbCompressedData() as cb:
            cb.readFromRom(rom,
                    EbModule.toRegAddr(
                        EbModule.readAsmPointer(
                            rom, self._ASMPTR_CREDITS_GFX)))
            self._cfont.readFromBlock(cb)
    def readFromRom(self, rom):
        for f in self._fonts:
            f.readFromRom(rom)
            updateProgress(self._pct)

        self.readCreditsFontFromRom(rom)
        updateProgress(self._pct)
    def writeToRom(self, rom):
        for f in self._fonts:
            f.writeToRom(rom)
            updateProgress(self._pct)

        self._cpal.writeToBlock(rom, loc=self._ADDR_CREDITS_PAL)
        with EbCompressedData(self._cfont.sizeBlock()) as cb:
            self._cfont.writeToBlock(cb)
            EbModule.writeAsmPointer(rom, self._ASMPTR_CREDITS_GFX,
                    EbModule.toSnesAddr(cb.writeToFree(rom)))
        updateProgress(self._pct)
    def writeCreditsFontToProject(self, resourceOpener):
        arr = EbArrangement(16, 12)
        for i in range(192):
            arr[i%16, i/16] = (False, False, False,
                    self._CREDITS_PREVIEW_SUBPALS[i], i)
        img = arr.toImage(self._cfont, self._cpal)
        with resourceOpener("Fonts/credits", "png") as imgFile:
            img.save(imgFile, "png")
            imgFile.close()
    def writeToProject(self, resourceOpener):
        out = dict()
        i=0
        for font in self._fonts:
            # Write the PNG
            img = font.toImage()
            with resourceOpener("Fonts/" + str(i), 'png') as imgFile:
                img.save(imgFile, 'png')

            # Write the widths
            out = font.dumpWidths()
            with resourceOpener("Fonts/" + str(i) + "_widths", "yml") as f:
                yaml.dump(out, f, default_flow_style=False,
                        Dumper=yaml.CSafeDumper)
            i += 1
            updateProgress(self._pct)

        self.writeCreditsFontToProject(resourceOpener)
        updateProgress(self._pct)
    def readFromProject(self, resourceOpener):
        i = 0
        for font in self._fonts:
            with resourceOpener("Fonts/" + str(i), "png") as imgFile:
                img = Image.open(imgFile)
                if img.mode != 'P':
                    raise RuntimeError("Fonts/" + str(i) + " is not an indexed PNG.")
                font.fromImage(img)
            with resourceOpener("Fonts/" + str(i) + "_widths", "yml") as f:
                input = yaml.load(f, Loader=yaml.CSafeLoader)
                font.loadWidths(input)
            i += 1
            updateProgress(self._pct)

        with resourceOpener("Fonts/credits", "png") as imgFile:
            img = Image.open(imgFile)
            if img.mode != 'P':
                    raise RuntimeError("Fonts/credits is not an indexed PNG.")
            self._cfont.loadFromImage(img)
            self._cpal.loadFromImage(img)
        updateProgress(self._pct)
    def upgradeProject(self, oldVersion, newVersion, rom, resourceOpenerR,
            resourceOpenerW):
        if oldVersion == newVersion:
            updateProgress(100)
            return
        elif oldVersion <= 2:
            self.readCreditsFontFromRom(rom)
            self.writeCreditsFontToProject(resourceOpenerW)
            self.upgradeProject(3, newVersion, rom, resourceOpenerR,
                                        resourceOpenerW)
        else:
            self.upgradeProject(oldVersion+1, newVersion, rom, resourceOpenerR,
                                        resourceOpenerW)