更新:主な問題を解決したので、私の解決策が良いスタイルであるかどうかの良いレビューに報奨金を授与します.
最近、マップを記述した 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 中間操作も) を実行できないことがわかりましたが、ここで立ち往生しています。どのように進めればよいか本当にわかりません。