def _vtag_data(element: minidom.Node, tag: str) -> List[str]: for name in tag.split("/"): new_element = element.getElementsByTagName(name) if not new_element: return [] element = new_element[0] elements = element.getElementsByTagName("element") return [x.firstChild.data for x in elements if x.firstChild]
def _extract_track_data(track: Node): track_mbid = pylast._extract(track, "mbid") track_title = pylast._extract(track, "name") timestamp = dt.datetime.fromtimestamp( int(track.getElementsByTagName("date")[0].getAttribute("uts"))) artist_name = pylast._extract(track, "artist") artist_mbid = track.getElementsByTagName("artist")[0].getAttribute("mbid") album_title = pylast._extract(track, "album") album_mbid = track.getElementsByTagName("album")[0].getAttribute("mbid") # TODO: could call track/album/artist.getInfo here, and get more info? # Handle missing titles if album_title is None: album_title = "(unknown album)" # If we don't have mbids, synthesize them if not artist_mbid: artist_mbid = "md5:" + hashlib.md5( artist_name.encode("utf8")).hexdigest() if not album_mbid: h = hashlib.md5() h.update(artist_mbid.encode("utf8")) h.update(album_title.encode("utf8")) album_mbid = "md5:" + h.hexdigest() if not track_mbid: h = hashlib.md5() h.update(album_mbid.encode("utf8")) h.update(track_title.encode("utf8")) track_mbid = "md5:" + h.hexdigest() return { "artist": { "id": artist_mbid, "name": artist_name }, "album": { "id": album_mbid, "title": album_title, "artist_id": artist_mbid }, "track": { "id": track_mbid, "album_id": album_mbid, "title": track_title }, "play": { "track_id": track_mbid, "timestamp": timestamp }, }
def handle_tile_layer(layer: minidom.Node, builder: flatbuffers.Builder) -> int: width = int(layer.getAttribute('width')) height = int(layer.getAttribute('height')) tilewidth = float(layer.parentNode.getAttribute('tilewidth')) tileheight = float(layer.parentNode.getAttribute('tileheight')) dataNode = layer.getElementsByTagName('data')[0] if dataNode.firstChild is None: return dataString = dataNode.firstChild.nodeValue rows = dataString.splitlines() data = [] for row in rows: for tile in row.split(','): if tile == '': continue data.append(int(tile)) FlatBuffGenerated.Tilelayer.TilelayerStartTiledataVector( builder, len(data)) for tile in reversed(data): builder.PrependInt16(tile) dataOffset = builder.EndVector(len(data)) FlatBuffGenerated.Tilelayer.TilelayerStart(builder) FlatBuffGenerated.Tilelayer.TilelayerAddWidth(builder, width) FlatBuffGenerated.Tilelayer.TilelayerAddHeight(builder, height) tilesize = FlatBuffGenerated.Vec2.CreateVec2(builder, tilewidth, tileheight) FlatBuffGenerated.Tilelayer.TilelayerAddTilesize(builder, tilesize) FlatBuffGenerated.Tilelayer.TilelayerAddTiledata(builder, dataOffset) return FlatBuffGenerated.Tilelayer.TilelayerEnd(builder)
def handle_tileset(ts: minidom.Node, objs: GameObjects, builder: flatbuffers.Builder): firstgid = int(ts.getAttribute('firstgid')) for t in ts.getElementsByTagName('tile'): gid = int(t.getAttribute('id')) + firstgid name = os.path.basename( t.getElementsByTagName('image')[0].getAttribute('source')) fbname = builder.CreateString(name) FlatBuffGenerated.Tileinfo.TileinfoStart(builder) FlatBuffGenerated.Tileinfo.TileinfoAddName(builder, fbname) FlatBuffGenerated.Tileinfo.TileinfoAddGid(builder, gid) objs.tileinfo.append(FlatBuffGenerated.Tileinfo.TileinfoEnd(builder))
def handle_decoration_layer(g: minidom.Node, objs: GameObjects, builder: flatbuffers.Builder): for o in g.getElementsByTagName('object'): x, y, rot = get_pos(o) width, height = get_dim(o) gid = int(o.getAttribute('gid')) FlatBuffGenerated.Decoration.DecorationStart(builder) pos = FlatBuffGenerated.Vec2.CreateVec2(builder, x * GLOBAL_SCALE, y * GLOBAL_SCALE) FlatBuffGenerated.Decoration.DecorationAddPos(builder, pos) FlatBuffGenerated.Decoration.DecorationAddRotation(builder, rot) size = FlatBuffGenerated.Vec2.CreateVec2(builder, width, height) FlatBuffGenerated.Decoration.DecorationAddSize(builder, size) FlatBuffGenerated.Decoration.DecorationAddGid(builder, gid) objs.decoration.append( FlatBuffGenerated.Decoration.DecorationEnd(builder))
def handle_hidingspots_layer(g: minidom.Node, objs: GameObjects, builder: flatbuffers.Builder): for o in g.getElementsByTagName('object'): x, y, rotation = get_pos(o) poly = 0 width = 0 height = 0 isCircle = False if not o.getElementsByTagName('polygon'): width, height = get_dim(o) x += (math.cos(math.radians(rotation)) * width / 2.0 - math.sin(math.radians(rotation)) * height / 2.0) y += (math.cos(math.radians(rotation)) * height / 2.0 + math.sin(math.radians(rotation)) * width / 2.0) if o.getElementsByTagName('ellipse'): assertCircleness(o) isCircle = True else: polyverts = getPolygonVertices(o) FlatBuffGenerated.HidingSpot.HidingSpotStartPolyvertsVector( builder, len(polyverts)) for v in polyverts: vert_x, vert_y = v.split(",") vert_x_f = float(vert_x) * GLOBAL_SCALE vert_y_f = float(vert_y) * GLOBAL_SCALE FlatBuffGenerated.Vec2.CreateVec2(builder, vert_x_f, vert_y_f) poly = builder.EndVector(len(polyverts)) name = builder.CreateString(o.getAttribute('name')) FlatBuffGenerated.HidingSpot.HidingSpotStart(builder) pos = FlatBuffGenerated.Vec2.CreateVec2(builder, x * GLOBAL_SCALE, y * GLOBAL_SCALE) FlatBuffGenerated.HidingSpot.HidingSpotAddPos(builder, pos) FlatBuffGenerated.HidingSpot.HidingSpotAddRotation(builder, rotation) size = FlatBuffGenerated.Vec2.CreateVec2(builder, width * GLOBAL_SCALE, height * GLOBAL_SCALE) FlatBuffGenerated.HidingSpot.HidingSpotAddSize(builder, size) FlatBuffGenerated.HidingSpot.HidingSpotAddIsCircle(builder, isCircle) FlatBuffGenerated.HidingSpot.HidingSpotAddPolyverts(builder, poly) FlatBuffGenerated.HidingSpot.HidingSpotAddName(builder, name) objs.hidingspots.append( FlatBuffGenerated.HidingSpot.HidingSpotEnd(builder))
def handle_objects_layer(g: minidom.Node, objs: GameObjects, builder: flatbuffers.Builder): for o in g.getElementsByTagName('object'): x, y, rot = get_pos(o) width, height = get_dim(o) gid = int(o.getAttribute('gid')) hspotname = "" for prop in o.getElementsByTagName("property"): if prop.getAttribute("name") == "hspotName": hspotname = prop.getAttribute("value") hspotNameFb = builder.CreateString(hspotname) FlatBuffGenerated.Object.ObjectStart(builder) pos = FlatBuffGenerated.Vec2.CreateVec2(builder, x * GLOBAL_SCALE, y * GLOBAL_SCALE) FlatBuffGenerated.Object.ObjectAddPos(builder, pos) FlatBuffGenerated.Object.ObjectAddRotation(builder, rot) FlatBuffGenerated.Object.ObjectAddGid(builder, gid) size = FlatBuffGenerated.Vec2.CreateVec2(builder, width, height) FlatBuffGenerated.Object.ObjectAddSize(builder, size) FlatBuffGenerated.Object.ObjectAddHspotname(builder, hspotNameFb) objs.objects.append(FlatBuffGenerated.Object.ObjectEnd(builder))
def get_text_from_tag_name(parent: Node, tag_name: str) -> str: return get_text(parent.getElementsByTagName(tag_name)[0].childNodes)
def _tag_value(element: minidom.Node, tag: str) -> Optional[int]: item = element.getElementsByTagName(tag) if item: value = item[0].attributes["value"].value return int(value[0]) return None
def handle_meta_layer(g: minidom.Node, objs: GameObjects, builder: flatbuffers.Builder): for o in g.getElementsByTagName('object'): x, y, rotation = get_pos(o) width, height = get_dim(o) typ = o.getAttribute('type') if typ == 'playerwall': x += (math.cos(math.radians(rotation)) * width / 2.0 - math.sin(math.radians(rotation)) * height / 2.0) y += (math.cos(math.radians(rotation)) * height / 2.0 + math.sin(math.radians(rotation)) * width / 2.0) FlatBuffGenerated.PlayerWall.PlayerWallStart(builder) pos = FlatBuffGenerated.Vec2.CreateVec2(builder, x * GLOBAL_SCALE, y * GLOBAL_SCALE) FlatBuffGenerated.PlayerWall.PlayerWallAddPosition(builder, pos) size = FlatBuffGenerated.Vec2.CreateVec2( builder, width / 2.0 * GLOBAL_SCALE, height / 2.0 * GLOBAL_SCALE) FlatBuffGenerated.PlayerWall.PlayerWallAddSize(builder, size) FlatBuffGenerated.PlayerWall.PlayerWallAddRotation( builder, rotation) pwall = FlatBuffGenerated.PlayerWall.PlayerWallEnd(builder) objs.playerwalls.append(pwall) elif typ == 'waypoint': name = o.getAttribute('name') neighbors = [] isspawn = False isplayerspawn = False assertCircleness(o) x += (math.cos(math.radians(rotation)) * width / 2.0 - math.sin(math.radians(rotation)) * height / 2.0) y += (math.cos(math.radians(rotation)) * height / 2.0 + math.sin(math.radians(rotation)) * width / 2.0) radius = (width / 2.0) * GLOBAL_SCALE for prop in o.getElementsByTagName('property'): prop_name = prop.getAttribute('name') value = prop.getAttribute('value') if prop_name == 'wps': neighbors = value.split(',') elif prop_name == 'isspawn': isspawn = (value == 'true') elif prop_name == 'isplayerspawn': isplayerspawn = (value == 'true') else: print("WARNING: Unknown property {}".format(prop_name)) neighbors = list(filter(lambda x: x.strip() != "", neighbors)) if len(neighbors) < 2 and not isplayerspawn and not isspawn: print("WARNING: waypoint {} has < 2 neighbors".format(name)) name = builder.CreateString(name) neighOff = [builder.CreateString(x) for x in neighbors] FlatBuffGenerated.NavPoint.NavPointStartNeighborsVector( builder, len(neighOff)) for b in neighOff: builder.PrependUOffsetTRelative(b) neighs = builder.EndVector(len(neighOff)) FlatBuffGenerated.NavPoint.NavPointStart(builder) pos = FlatBuffGenerated.Vec2.CreateVec2(builder, x * GLOBAL_SCALE, y * GLOBAL_SCALE) FlatBuffGenerated.NavPoint.NavPointAddPosition(builder, pos) FlatBuffGenerated.NavPoint.NavPointAddRadius(builder, radius) FlatBuffGenerated.NavPoint.NavPointAddName(builder, name) FlatBuffGenerated.NavPoint.NavPointAddNeighbors(builder, neighs) FlatBuffGenerated.NavPoint.NavPointAddIsspawn(builder, isspawn) FlatBuffGenerated.NavPoint.NavPointAddIsplayerspawn( builder, isplayerspawn) objs.navpoints.append( FlatBuffGenerated.NavPoint.NavPointEnd(builder))