2

更新:主な問題を解決したので、私の解決策が良いスタイルであるかどうかの良いレビューに報奨金を授与します.

最近、マップを記述した XML ファイルであるTMXファイルを解析しようとしています。この形式の興味深い点の 1 つは、外部のタイルセットを指定できることです。

htiled作業の大部分は既に処理されているため、外部のタイルセットを処理できるようにライブラリを拡張しようとしてきましたが、これまでのところ成功していません。

基本的に、ここで達成しようとしているタスクは、次の 2 つのドキュメントが与えられた場合map.tmxです。

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="12" height="12" tilewidth="64" tileheight="64">
 <tileset firstgid="1" source="ground.tsx"/>
 ...
</map>

ground.tsx:

<?xml version="1.0" encoding="UTF-8"?>
<tileset name="ground" tilewidth="64" tileheight="64" spacing="32">
 <image source="ground.png" width="64" height="64"/>
 <tile id="0">
  <properties>
   <property name="sol" value=""/>
  </properties>
 </tile>
</tileset>

構造を返します:

Map {..., tilesets = [Tileset { name = "ground", ...}]}

(内部のみ) タイルセットを解析する作業方法は次のとおりです。

tilesets ∷ IOSArrow XmlTree [Tileset]
tilesets = listA tileset

tileset ∷ IOSArrow XmlTree Tileset
tileset = isElem >>> hasName "tileset" >>> proc ts → do
  tsName        ← getAttrValue "name"                        ⤙ ts
  tsInitialGid  ← getAttrR "firstgid"                        ⤙ ts
  tsTileWidth   ← getAttrR "tilewidth"                       ⤙ ts
  tsTileHeight  ← getAttrR "tileheight"                      ⤙ ts
  tsMargin      ← arr (fromMaybe 0) . getAttrMaybe "margin"  ⤙ ts
  tsSpacing     ← arr (fromMaybe 0) . getAttrMaybe "spacing" ⤙ ts
  tsImages      ← images                                     ⤙ ts
  tsTileProperties ← listA tileProperties                    ⤙ ts
  returnA ⤙ Tileset {..}
  where tileProperties ∷ IOSArrow XmlTree (Word32, Properties)
        tileProperties = getChildren >>> isElem >>> hasName "tile"
                     >>> getAttrR "id" &&& properties
        images = listA (getChildren >>> image)

現在の要素の属性にtilesets応じて、現在の要素または外部ドキュメントを使用するようにメソッドを適応させようとしましたが、役に立ちませんでした:source

tilesets ∷ IOSArrow XmlTree [Tileset]
tilesets = listA $ proc ts → do
  source ← isElem >>> hasName "tileset" >>> getAttrValue "source" ⤙ ts
  case source of
    "" → tileset ⤙ ts
    f → tileset ⤙ readDocument [withValidate no, withWarnings yes] f

(これは私の多くの試みの1つです)。

通常、矢印コマンドを使用していないか、値が矢印の中にあるべきではないのに矢印の内側にあるとGHCが通知するポイントに到達します。タイプ セーフな方法で透過的に IO (およびおそらく XHT 中間操作も) を実行できないことがわかりましたが、ここで立ち往生しています。どのように進めればよいか本当にわかりません。

4

1 に答える 1

3

私はそれで動作するようにreadFromDocumentなりましたifA

tilesets ∷ FilePath → IOSArrow XmlTree [Tileset]
tilesets mapPath =
  listA $ getChildren >>> isElem >>> hasName "tileset"
  >>> getAttrR "firstgid" &&& ifA (hasAttr "source") (externalTileset mapPath) id
  >>> tileset

externalTileset ∷ FilePath → IOSArrow XmlTree XmlTree
externalTileset mapPath =
  arr (const (dropFileName mapPath)) &&& getAttrValue "source"
  >>> arr (uncurry (</>))
  >>> readFromDocument [ withValidate no, withWarnings yes ]
  >>> getChildren >>> isElem >>> hasName "tileset"

tileset ∷ IOSArrow (Word32, XmlTree) Tileset
tileset = proc (tsInitialGid, ts) → do
  tsName           ← getAttrValue "name"                        ⤙ ts
  tsTileWidth      ← getAttrR "tilewidth"                       ⤙ ts
  tsTileHeight     ← getAttrR "tileheight"                      ⤙ ts
  tsMargin         ← arr (fromMaybe 0) . getAttrMaybe "margin"  ⤙ ts
  tsSpacing        ← arr (fromMaybe 0) . getAttrMaybe "spacing" ⤙ ts
  tsImages         ← images                                     ⤙ ts
  tsTileProperties ← listA tileProperties                       ⤙ ts
  returnA ⤙ Tileset {..}
  where tileProperties ∷ IOSArrow XmlTree (Word32, Properties)
        tileProperties = getChildren >>> isElem >>> hasName "tile"
                         >>> getAttrR "id" &&& properties
        images = listA (getChildren >>> image)
于 2014-05-31T07:22:40.177 に答える