Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Switched back to cereal after porting Data.Binary.IEEE754 to cereal

Also renamed/rearranged some things
  • Loading branch information...
commit 798804ac6488a3c3fad0649856ee51c619170f17 1 parent 52d0228
Adam C. Foltzer authored August 02, 2011
3  .gitignore
... ...
@@ -1,3 +1,4 @@
1 1
 *~
2 2
 *#*
3  
-dist/
  3
+dist/
  4
+cabal-dev/
4  NBT.cabal → nbt.cabal
@@ -12,10 +12,10 @@ Cabal-version:       >=1.2
12 12
 
13 13
 Library
14 14
   Exposed-modules:     Data.NBT
  15
+  hs-source-dirs:      src
15 16
   Build-depends:       base >= 4, 
16  
-                       binary >= 0.5.0.2,
17 17
                        bytestring >= 0.9.1.10,
18  
-                       data-binary-ieee754 >= 0.4.2.1,
  18
+                       cereal >= 0.3.3.1,
19 19
                        utf8-string >= 0.3.6,
20 20
                        zlib >= 0.5.3.1,
21 21
                        QuickCheck
47  Data/NBT.hs → src/Data/NBT.hs
@@ -8,22 +8,24 @@ Maintainer  :  acfoltzer@gmail.com
8 8
 Stability   :  experimental
9 9
 Portability :  portable
10 10
 
11  
-Defines a 'Data.Binary.Binary' instance for Minecraft's NBT
12  
-binary data format. See the NBT specification for details:
  11
+Defines a Haskell representation of Minecraft's NBT binary data
  12
+format, along with instances of 'Data.Serialize.Serialize'. See the
  13
+NBT specification for details:
13 14
 <https://raw.github.com/acfoltzer/nbt/master/NBT-spec.txt>.
14 15
 
15 16
 -}
16 17
 
17 18
 module Data.NBT where
18 19
 
19  
-import Data.Binary ( Binary (..), decode, encode, getWord8, putWord8 )
20  
-import Data.Binary.Get ( Get, getLazyByteString, lookAhead, skip )
21  
-import Data.Binary.Put ( Put, putLazyByteString )
22  
-import Data.Binary.IEEE754
  20
+import Data.Serialize ( Serialize (..), decode, decodeLazy, encode, encodeLazy, getWord8, putWord8 )
  21
+import Data.Serialize.Get ( Get, getByteString, lookAhead, skip, remaining )
  22
+import Data.Serialize.Put ( Put, putByteString )
  23
+import Data.Serialize.IEEE754
23 24
 
24 25
 import qualified Codec.Compression.GZip as GZip
25  
-import qualified Data.ByteString.Lazy as B
26  
-import qualified Data.ByteString.Lazy.UTF8 as UTF8
  26
+import qualified Data.ByteString as B
  27
+import qualified Data.ByteString.Lazy as L
  28
+import qualified Data.ByteString.UTF8 as UTF8 ( fromString, toString )
27 29
 import Data.Int ( Int8, Int16, Int32, Int64 )
28 30
 import Data.Word ( Word8 )
29 31
 
@@ -47,15 +49,15 @@ data TagType = EndType
47 49
              | CompoundType
48 50
                deriving (Show, Eq, Enum)
49 51
 
50  
-instance Binary TagType where
51  
-    get = fmap (toEnum . fromEnum) getWord8
52  
-    put = putWord8 . toEnum . fromEnum
  52
+instance Serialize TagType where
  53
+    get = fmap (toEnum . fromIntegral) getWord8
  54
+    put = putWord8 . fromIntegral . fromEnum
53 55
 
54 56
 instance Arbitrary TagType where
55 57
     arbitrary = toEnum <$> choose (0, 10)
56 58
 
57 59
 prop_TagType :: TagType -> Bool
58  
-prop_TagType ty = decode (encode ty) == ty
  60
+prop_TagType ty = decode (encode ty) == Right ty
59 61
 
60 62
 -- | Primitive representation of NBT data. This type contains both named
61 63
 -- and unnamed variants; a 'Nothing' name signifies an unnamed tag, so
@@ -73,7 +75,7 @@ data NBT = EndTag
73 75
          | CompoundTag  (Maybe String) [NBT]
74 76
            deriving (Show, Eq)
75 77
 
76  
-instance Binary NBT where
  78
+instance Serialize NBT where
77 79
   get = get >>= \ty ->
78 80
     case ty of
79 81
       EndType       -> return EndTag
@@ -105,11 +107,11 @@ instance Binary NBT where
105 107
       getDouble n = DoubleTag n <$> getFloat64be
106 108
       getByteArray n = do
107 109
         len <- get :: Get Int32
108  
-        ByteArrayTag n len <$> getLazyByteString (toEnum $ fromEnum len)
  110
+        ByteArrayTag n len <$> getByteString (toEnum $ fromEnum len)
109 111
       getString n = do
110 112
         len <- get :: Get Int16
111 113
         StringTag n len <$> UTF8.toString 
112  
-                        <$> getLazyByteString (toEnum $ fromEnum len)
  114
+                        <$> getByteString (toEnum $ fromEnum len)
113 115
       getList n = do
114 116
         ty  <- get :: Get TagType
115 117
         len <- get :: Get Int32
@@ -181,10 +183,10 @@ instance Binary NBT where
181 183
       putLong             = put
182 184
       putFloat            = putFloat32be
183 185
       putDouble           = putFloat64be
184  
-      putByteArray len bs = put len >> putLazyByteString bs
  186
+      putByteArray len bs = put len >> putByteString bs
185 187
       putString str       = let bs = UTF8.fromString str 
186  
-                                len = (toEnum . fromEnum) (B.length bs)
187  
-                            in put (len :: Int16) >> putLazyByteString bs
  188
+                                len = fromIntegral (B.length bs)
  189
+                            in put (len :: Int16) >> putByteString bs
188 190
       putList ty len ts   = put ty >> put len >> forM_ ts put
189 191
       putCompound ts      = forM_ ts put >> put EndTag
190 192
 
@@ -222,10 +224,11 @@ instance Arbitrary NBT where
222 224
     mkArb ty (Just name)
223 225
 
224 226
 prop_NBTroundTrip :: NBT -> Bool
225  
-prop_NBTroundTrip nbt = decode (encode nbt) == nbt
  227
+prop_NBTroundTrip nbt = decode (encode nbt) == Right nbt
226 228
 
227 229
 test = do
228  
-  file <- fmap GZip.decompress $ B.readFile "Test/testWorld/level.dat"
229  
-  let dec = (decode file :: NBT)
  230
+  fileL <- GZip.decompress <$> L.readFile "test/testWorld/level.dat"
  231
+  let file = B.pack (L.unpack fileL)
  232
+      Right dec = (decode file :: Either String NBT)
230 233
       enc = encode dec
231  
-  return $ (enc == file, decode enc == dec)
  234
+  return $ (enc == file, decode enc == Right dec)
0  Test/testWorld/level.dat → test/testWorld/level.dat
File renamed without changes
0  Test/testWorld/region/r.-1.-1.mcr → test/testWorld/region/r.-1.-1.mcr
File renamed without changes
0  Test/testWorld/region/r.-1.0.mcr → test/testWorld/region/r.-1.0.mcr
File renamed without changes
0  Test/testWorld/region/r.0.-1.mcr → test/testWorld/region/r.0.-1.mcr
File renamed without changes
0  Test/testWorld/region/r.0.0.mcr → test/testWorld/region/r.0.0.mcr
File renamed without changes

0 notes on commit 798804a

Please sign in to comment.
Something went wrong with that request. Please try again.