yeKcim a écrit :
all rules i thought about : http://yeknan.free.fr/wormux/body_config.xml_check/checkyourbody.pdf
(first rule is apart, could probably made with existing soft)

i need a little message to know which part is incorrect for whitch test.

Here's a script in python for all of those tests. Unfortunately, it's mostly good when not reporting errors, and probably breaking otherwise. And I don't have access to line numbers, so you're on your own to use the context and find the exact position of the error.

It's also suboptimal (double or triple checks, redundant work, ...) but that's the least of its problem.

Also I suspect there are some parts in your document where you forgot to mention "and if not weapon, then".
#!/bin/env python
from xml.dom import minidom
import sys
import os.path

def getAttrList(list, name, attr):
    la = [elem.getAttribute(attr) for elem in list]
    if '' in la:
        print "###  An element of name '%s' doesn't have an attribute '%s'.\n" \
              "###  Please fix that first" % (name, attr)
        sys.exit(0)
    return la

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "Syntax: %s <xml document>" % sys.argv[0]
        sys.exit(0)
    
    root = minidom.parse(sys.argv[1]).documentElement
    base = os.path.dirname(sys.argv[1])

    # Movement analysis
    print "# Checking movements..."
    movements = root.getElementsByTagName("movement")
    for mov in movements:
        # Name
        if mov.hasAttribute("name"):
            print "  # Movement", mov.getAttribute("name")
        else:
            print "  # Movement without name"
         
        collisions = mov.getElementsByTagName("collision_rect")
        if len(collisions) != 1:
            print "    -> No collision rectanle defined!"
        for col in collisions:
            if not col.hasAttribute("left"):
                print "    -> No left for collision rect"
            if not col.hasAttribute("right"):
                print "    -> No right for collision rect"
            if not col.hasAttribute("top"):
                print "    -> No top for collision rect"
            if not col.hasAttribute("bottom"):
                print "    -> No bottom for collision rect"

        if not mov.hasAttribute("name"):
            print "    -> No name"
        if not mov.hasAttribute("speed"):
            print "    -> No speed"

        frames = mov.getElementsByTagName("frame")
        if not len(frames):
            print "    -> No frame defined"
        else:
            for i,f in enumerate(frames):
                print "    # Checking frame %u" % i
                members = f.getElementsByTagName("member")
                if not len(members):
                    print "      -> No member"
                    

    # Alias analysis
    print "# Checking aliases..."
    aliases = root.getElementsByTagName("alias")
    movements = root.getElementsByTagName("movement")

    alias_mov = getAttrList(aliases, "alias", "movement")
    mov_names = getAttrList(movements, "movement", "name")

    for ref in aliases:
        name = ref.getAttribute("movement")

        # Check that correspond_to is matched
        if ref.hasAttribute("correspond_to"):
            corresp = ref.getAttribute("correspond_to")
            if corresp not in mov_names and corresp not in alias_mov:
                print "  -> Found no such correspondance '%s' for alias '%s'" % 
(corresp, name)

        # Check there's only one alias with a movement definition
        if alias_mov.count(name) > 1:
            print "  -> Found 2 aliases with movement", name

    # Sprite analysis
    print "# Checking sprites"
    sprites=root.getElementsByTagName("sprite")

    # Sprite (type,name) not checked for duplicates now
    sprite_types=getAttrList(sprites, "sprite", "type")
    sprite_names=getAttrList(sprites, "sprite", "name")

    for sprite in sprites:
        if sprite.hasAttribute("name"):
            name=sprite.getAttribute("name")
        else:
            name=None
        if sprite.hasAttribute("type"):
            type_=sprite.getAttribute("type")
        else:
            type_=None
        print "  # Checking sprite %s" % name

        # Check attributes
        if not name:
            if not type_:
                print "  -> A sprite doesn't have a name or a type"
                type_="(undefined)"
            else:
                print "  -> A sprite of type '%s' doesn't have a name" % type_
            name="(undefined)"
        elif not type_:
            print "  -> A sprite of name '%s' doesn't have a type" % name
            type_="(undefined)"

        # Check images
        images=sprite.getElementsByTagName("image")
        if len(images) != 1:
            print "    -> Warning, %u images used" % len(images)
        for i,img in enumerate(images):
            if img.hasAttribute("file"):
                fn=img.getAttribute("file")
                if not os.path.isfile(os.path.join(base, fn)):
                    print "    -> File %s doesn't exist" % fn
            else:
                print "   -> No file defined!"

        # Check anchors
        anchors=sprite.getElementsByTagName("anchor")
        if len(anchors) != 1:
            print "    -> Warning, %u anchors defined" % len(anchors)

        # Check attached stuff
        for attached in sprite.getElementsByTagName("attached"):
            if not attached.hasAttribute("member_type"):
                print "    -> An attached element doesn't have a member_type"
            else:
                member_type = attached.getAttribute("member_type")
                if member_type!="weapon" and not member_type in sprite_types:
                    print "    -> No sprite to match member_type '%s'" % 
member_type

            if not attached.hasAttribute("dx"):
                print "    -> An attached element doesn't have a dx value"
            if not attached.hasAttribute("dy"):
                print "    -> An attached element doesn't have a dy value"

    # Check clothes
    print "# Checking clothes"
    clothes = root.getElementsByTagName("clothe")
    #print "Clothes: %s" % [c.getAttribute("name") for c in clothes]
    for clothe in clothes:
        c_members = clothe.getElementsByTagName("c_member")
        if not len(c_members):
            print "  -> No c_member for clothe '%s'" % 
clothe.getAttribut("name")
        else:
            for member in c_members:
                if member.hasAttribute("name"):
                    name = member.getAttribute("name")
                    if name != "weapon" and name not in sprite_names:
                        print "  -> c_member of name '%s' has no associated 
sprite" % name
                else:
                    print "  -> A c_member has no name!"

    # Consistency check
    print "# Consistency check"
    for mov in movements:
        if not mov.hasAttribute("name"):
            continue
        A = mov.getAttribute("name")
        print "  # Verifying movement '%s'" % A

        # Finding cloth of such type
        foundc = None
        normal = None
        for clothe in clothes:
            if not clothe.hasAttribute("name"):
                continue
            if clothe.getAttribute("name") == A:
                foundc = clothe
            if clothe.getAttribute("name") == "normal":
                normal = clothe
        if not foundc:
            if not normal:
                print "    -> No cloth at all!!"
                continue
            foundc = normal
        
        for member in mov.getElementsByTagName("member"):
            if not member.hasAttribute("type"):
                continue
            B = member.getAttribute("type")

            C = None
            for c_member in foundc.getElementsByTagName("c_member"):
                if not c_member.hasAttribute("name"):
                    continue
                C = c_member.getAttribute("name")
                # weapon sprites are skipped
                if C == "weapon":
                    continue
                print "     # Member of type '%s' and name '%s'" % (C, B)

                founds = []
                for sprite in sprites:
                    if not sprite.hasAttribute("type") or not 
sprite.hasAttribute("name"):
                        continue
                    if sprite.getAttribute("type") != B or 
sprite.getAttribute("name") != C:
                        continue
                    founds.append(sprite)

                    if not len(founds):
                        print "      -> No sprite matching it!"
                    if len(founds) > 1:
                        print "    -> Found %u sprites matching it!" % 
len(founds)
_______________________________________________
Wormux-dev mailing list
Wormux-dev@gna.org
https://mail.gna.org/listinfo/wormux-dev

Répondre à