1 import Text
.ParserCombinators
.Parsec
2 import System
.Environment
3 import Control
.Concurrent
7 import qualified Data
.Set
as S
8 import qualified Data
.Map
as M
11 import qualified Data
.ByteString
.Lazy
as B
13 import Control
.Concurrent
.MVar
17 waiter
<- newEmptyMVar
18 mapM (\f -> forkIO
$ compress f
>> putMVar waiter
()) files
19 replicateM
(length files
) (takeMVar waiter
)
22 parsed
<- parseFromFile parser file
26 outfile
<- openFile (replaceExtension file
".dmp") WriteMode
27 let flatTiles
= concat tiles
28 munge
= M
.fromList
. flip zip [(1::Word32
)..] . S
.toList
. S
.fromList
29 bytes
= log256
(maximum (map M
.size
[kinds
,values
]))
30 kinds
= munge
$ map fst flatTiles
31 values
= munge
$ map snd flatTiles
32 code num
= case bytes
of
33 1 -> encode
(fromIntegral num
:: Word8
)
34 2 -> encode
(fromIntegral num
:: Word16
)
36 mapM_ (hPutStrLn outfile
. show) [kinds
,values
]
37 forM_ tiles
$ \tile
-> do
38 forM_ tile
$ \(kind
,value) -> do
39 B
.hPut outfile
$ code
$ kinds
! kind
40 B
.hPut outfile
$ code
$ kinds
! kind
41 B
.hPut outfile
$ code
(0::Word32
) -- A single 0 word terminates a tile. Note that the kinds and values are 1-indexed.
46 type Tile
= [(String,String)]
48 parser
:: Parser
[Tile
]
49 parser
= many
$ (try hidden
<|
> element
) `manyTill`
(string "---" >> newline
)
51 kind
<- anyChar `manyTill`
string ": "
52 value <- anyChar `manyTill` newline
54 hidden
= string "Hidden" >> newline
>> return ("Hidden","")
57 log256 n
= 1 + log256
(n `
div`
256)