Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial import

  • Loading branch information...
commit d5ce702a4eb5d763ff3f5820842983d096b5d1e6 0 parents
@acfoltzer authored
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c) 2010, Adam C. Foltzer
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Adam C. Foltzer nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 NBT.cabal
@@ -0,0 +1,57 @@
+-- NBT.cabal auto-generated by cabal init. For additional options, see
+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.
+-- The name of the package.
+Name: NBT
+
+-- The package version. See the Haskell package versioning policy
+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for
+-- standards guiding when and how versions should be incremented.
+Version: 0.1
+
+-- A short (one-line) description of the package.
+Synopsis: NBT API for Haskell
+
+-- A longer description of the package.
+-- Description:
+
+-- The license under which the package is released.
+License: BSD3
+
+-- The file containing the license text.
+License-file: LICENSE
+
+-- The package author(s).
+Author: Adam C. Foltzer
+
+-- An email address to which users can send suggestions, bug reports,
+-- and patches.
+Maintainer: acfoltzer@gmail.com
+
+-- A copyright notice.
+-- Copyright:
+
+Category: Data
+
+Build-type: Simple
+
+-- Extra files to be distributed with the package, such as examples or
+-- a README.
+-- Extra-source-files:
+
+-- Constraint on the version of Cabal needed to build this package.
+Cabal-version: >=1.2
+
+
+Library
+ -- Modules exported by the library.
+ -- Exposed-modules:
+
+ -- Packages needed in order to build this package.
+ -- Build-depends:
+
+ -- Modules not exported by this package.
+ -- Other-modules:
+
+ -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.
+ -- Build-tools:
+
153 NBT.hs
@@ -0,0 +1,153 @@
+module NBT where
+
+import qualified Data.ByteString.Lazy as BS
+import qualified Codec.Compression.GZip as GZip
+import Data.Int (Int8, Int16, Int32, Int64)
+import Data.Word (Word8)
+import Data.Binary
+import Data.Binary.IEEE754
+import qualified Codec.Binary.UTF8.String as BStr
+
+import Control.Monad
+
+type TagType = Word8
+endType :: TagType
+byteType :: TagType
+shortType :: TagType
+intType :: TagType
+longType :: TagType
+floatType :: TagType
+doubleType :: TagType
+byteArrayType :: TagType
+stringType :: TagType
+listType :: TagType
+compoundType :: TagType
+endType = 0
+byteType = 1
+shortType = 2
+intType = 3
+longType = 4
+floatType = 5
+doubleType = 6
+byteArrayType = 7
+stringType = 8
+listType = 9
+compoundType = 10
+
+data Tag = EndTag
+ | ByteTag String Int8
+ | ShortTag String Int16
+ | IntTag String Int32
+ | LongTag String Int64
+ | FloatTag String Float
+ | DoubleTag String Double
+ | ByteArrayTag String Int32 [Word8]
+ | StringTag String Int16 String
+ | ListTag String TagType Int32 [Tag]
+ | CompoundTag String [Tag]
+ deriving (Show, Eq)
+
+instance Binary Tag where
+ get = do tagType <- getWord8
+ case tagType of
+ 0 -> getEnd
+ 1 ->
+ do name <- readName
+ getByte name
+ 2 ->
+ do name <- readName
+ getShort name
+ 3 ->
+ do name <- readName
+ getInt name
+ 4 ->
+ do name <- readName
+ getLong name
+ 5 ->
+ do name <- readName
+ getFloat name
+ 6 ->
+ do name <- readName
+ getDouble name
+ 7 ->
+ do name <- readName
+ getByteArray name
+ 8 ->
+ do name <- readName
+ getString name
+ 9 ->
+ do name <- readName
+ listType <- getWord8
+ length <- get :: Get Int32
+ payload <- getList listType ((fromIntegral . toInteger) length)
+ return $ ListTag name listType length payload
+ 10 ->
+ do name <- readName
+ payload <- getCompound
+ return $ CompoundTag name payload
+ _ -> error $ "saw type " ++ (show tagType)
+ where readName =
+ do length <- get :: Get Int16
+ words <- sequence $ take ((fromIntegral . toInteger) length)
+ $ repeat (get :: Get Word8)
+ return $ BStr.decode words
+ getEnd = return EndTag
+ getByte name = do
+ payload <- get
+ return $ ByteTag name payload
+ getShort name = do
+ payload <- get
+ return $ ShortTag name payload
+ getInt name = do
+ payload <- get
+ return $ IntTag name payload
+ getLong name = do
+ payload <- get
+ return $ LongTag name payload
+ getFloat name = do
+ payload <- getFloat32be
+ return $ FloatTag name payload
+ getDouble name = do
+ payload <- getFloat64be
+ return $ DoubleTag name payload
+ getByteArray name = do
+ length <- get :: Get Int32
+ payload <- sequence $ take ((fromIntegral . toInteger) length)
+ $ repeat (get :: Get Word8)
+ return $ ByteArrayTag name length payload
+ getString name = do
+ length <- get :: Get Int16
+ payload <- sequence $ take ((fromIntegral . toInteger) length)
+ $ repeat (get :: Get Word8)
+ return $ StringTag name length (BStr.decode payload)
+ getList listType 0 = return []
+ getList listType n =
+ do tag <- case listType of
+ 1 -> getByte ""
+ 2 -> getShort ""
+ 3 -> getInt ""
+ 4 -> getLong ""
+ 5 -> getFloat ""
+ 6 -> getDouble ""
+ 7 -> getByteArray ""
+ 8 -> getString ""
+ 9 -> do listType' <- getWord8
+ length <- get :: Get Int32
+ payload <- getList listType' ((fromIntegral . toInteger) length)
+ return $ ListTag "" listType' length payload
+ 10 -> do payload <- getCompound
+ return $ CompoundTag "" payload
+ tags <- getList listType (n-1)
+ return (tag:tags)
+ getCompound =
+ do tag <- get :: Get Tag
+ case tag of
+ EndTag -> return []
+ _ -> do tags <- getCompound
+ return (tag:tags)
+
+
+
+test = do
+ file <- BS.readFile "level.dat"
+ print $ ((decode $ GZip.decompress file) :: Tag)
0  README
No changes.
2  Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
BIN  level.dat
Binary file not shown
BIN  level.gz
Binary file not shown
BIN  level.uncompressed
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.