読者です 読者をやめる 読者になる 読者になる

chikuchikugonzalezの雑記帳

趣味とか日記とかメモとか(∩゚д゚)

SAEのエクスポートCSV→sprmake2定義ファイルを行うPythonスクリプト書いた

たぶんこれで楽は出来るようになるはず。特にテストはしてない&自分環境のみ想定。

#!python
# -*- coding: utf-8 -*-

import sys
import os
import re
import csv

"""
指定ディレクトリの中を指定パターンで検索する
"""
def listFiles(dir, pattern):
    matcher = re.compile(pattern)
    fileList = []
    for root, dirs, files in os.walk(dir):
        for file in files:
            if matcher.match(file):
                fileList.append(os.path.join(root, file)[len(dir) + 1:].replace('\\', '/'))
    return fileList

"""
スプライトレコード
"""
class Record:

    def __init__(self, csvRow):
        self.file = csvRow[0]
        self.group = int(csvRow[1])
        self.number = int(csvRow[2])
        self.x = int(csvRow[3])
        self.y = int(csvRow[4])
        self.shared = (int(csvRow[5][:1]) is 1)

    def __str__(self):
        line = '%d, %d, %s, %d, %d' % (self.group, self.number, self.file, self.x, self.y)
#        if not self.shared:
#            line = line + "\t? usepal = -1"
        return line

class SpriteList:

    def __init__(self, input):
        reader = csv.reader(input)
        self.records = []
        for row in reader:
            self.records.append(Record(row))

    def __iter__(self):
        for record in self.records:
            yield record

"""
SFF version2定義クラス (for sprmake2)
"""
class SFFv2:
    
    def __init__(self, input, name):
        self.sprites = SpriteList(input)
        self.name = name
        self.compress5 = 'lz5'
        self.compress8 = 'rle8'
        self.compress24 = 'none'
        self.decompressOnLoad = True
        self.detectDuplicates = True
        self.autoCrop = True
        self.detectPaletteDuplicates = True
        self.discardDuplicatePalettes = True
        self.reversePalette = False
        self.reversePngPalette = False
        self.palettes = {}

    def addPalette(self, group, no, fileName):
        if group not in self.palettes:
            self.palettes[group] = {}
        self.palettes[group][no] = fileName

    def setDefaultPalette(self, group, no):
        self.defaultPalette = (group, no)

    def output(self, out):
        self.outputBase(out)
        self.outputDefaultOptions(out)
        self.outputPalettes(out)
        self.outputSprites(out)

    def outputBase(self, out):
        out.write("[Output]\n")
        out.write("filename = %s\n" % self.name)
        out.write("\n")

    def outputDefaultOptions(self, out):
        out.write("[Option]\n")
        out.write(";input.dir =\n")
        out.write("sprite.compress.5 = %s\n" % self.compress5)
        out.write("sprite.compress.8 = %s\n" % self.compress8)
        out.write("sprite.compress.24 = %s\n" % self.compress24)
        out.write("sprite.decompressonload = %d\n" % (1 if self.decompressOnLoad else 0))
        out.write("sprite.detectduplicates = %d\n" % (1 if self.detectDuplicates else 0))
        out.write("sprite.autocrop = %d\n" % (1 if self.autoCrop else 0))
        out.write("pal.detectduplicates = %d\n" % (1 if self.detectPaletteDuplicates else 0))
        out.write("pal.discardduplicates = %d\n" % (1 if self.discardDuplicatePalettes else 0))
        out.write("pal.reverseact = %d\n" % (1 if self.reversePalette else 0))
        out.write("pal.reversepng = %d\n" % (1 if self.reversePngPalette else 0))
        out.write("\n")

    def outputPalettes(self, out):
        out.write("[Pal]\n")
        for group in self.palettes:
            palettes = self.palettes[group]
            for no in palettes:
                out.write("%d, %d, %s, 0, 255\n" % (group, no, palettes[no]))
        out.write("\n")
#        out.write("[Option]\n")
#        out.write("sprite.usepal = %d, %d\n" % (self.defaultPalette[0], self.defaultPalette[1]))
#        out.write("\n")

    def outputSprites(self, out):
        shared = False
        out.write("[Option]\n")             # 独自パレット
        out.write("sprite.usepal = -1\n")
        out.write("[Sprite]\n")
        for sprite in self.sprites:
            #sys.stderr.write("%s, %d\n" %(sprite.group == 9000, sprite.group))
            if not shared:
                if ((sprite.group == 9000 or sprite.group == 0) and sprite.number is 0):
                    #sys.stderr.write("%d, %d\n" % (sprite.group, sprite.number))
                    shared = True
                    out.write("[Option]\n")
                    out.write("sprite.usepal = %d, %d\n" % (self.defaultPalette[0], self.defaultPalette[1]))
                    #out.write("sprite.removecolors = 0, 255\n")
                    out.write("[Sprite]\n")
            else:
                if not sprite.shared:
                    shared = False
                    out.write("[Option]\n")
                    out.write("sprite.usepal = -1\n")
                    out.write("[Sprite]\n")
            out.write("%s\n" % sprite)
        out.write("\n")

def usage():
    sys.stderr.write("USAGE: %s <sff name> [<input csv> [<output file>]]\n" % (sys.argv[0]))
    sys.stderr.write("\t<sff name>\tName of SFF File Name\n")
    sys.stderr.write("\t<input csv>\tSff Air Editor style SFF Export CSV\n")
    sys.stderr.write("\t<output file>\tOutput File Name\n")
    sys.stderr.write("\n")
    sys.stderr.write("<input csv> and <output file> can be omitted. If omitted these elements, uses <STDIN> for input and <STDOUT> for output.\n")

if __name__ == '__main__':
    input = sys.stdin
    output = sys.stdout
    name = None
    if len(sys.argv) > 1:
        name = sys.argv[1]
    else:
        usage()
        exit()

    if not name is None:
        if len(sys.argv) > 2:
            input = file(sys.argv[2], "r")
        if len(sys.argv) > 3:
            output = file(sys.argv[3], "w")

        sffv2 = SFFv2(input, name)
        palettes = listFiles(os.getcwd(), ".*act$")
        for i in range(0, len(palettes)):
            sffv2.addPalette(1, i + 1, palettes[i])
        sffv2.setDefaultPalette(1, 1)
        sffv2.output(output)
    else:
        sys.stderr.write("Error: No given sff name\n")

#    for file in listFiles(os.getcwd(), '.*\\.act$'):
#        print file