module Rattletrap.Header where
import Rattletrap.Primitive
import Rattletrap.Property
import Rattletrap.PropertyValue
import qualified Data.Binary as Binary
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
data Header = Header
{ headerEngineVersion :: Word32
, headerLicenseeVersion :: Word32
, headerPatchVersion :: Maybe Word32
, headerLabel :: Text
, headerProperties :: Dictionary Property
} deriving (Eq, Ord, Show)
getHeader :: Binary.Get Header
getHeader = do
engineVersion <- getWord32
licenseeVersion <- getWord32
patchVersion <- getPatchVersion engineVersion licenseeVersion
label <- getText
properties <- getDictionary getProperty
pure (Header engineVersion licenseeVersion patchVersion label properties)
getPatchVersion :: Word32 -> Word32 -> Binary.Get (Maybe Word32)
getPatchVersion major minor = if hasPatchVersion major minor
then do
patchVersion <- getWord32
pure (Just patchVersion)
else pure Nothing
hasPatchVersion :: Word32 -> Word32 -> Bool
hasPatchVersion major minor = major >= Word32 868 && minor >= Word32 18
putHeader :: Header -> Binary.Put
putHeader header = do
putWord32 (headerEngineVersion header)
putWord32 (headerLicenseeVersion header)
case headerPatchVersion header of
Nothing -> pure ()
Just patchVersion -> putWord32 patchVersion
putText (headerLabel header)
putDictionary putProperty (headerProperties header)
getVersion :: Header -> (Int, Int, Int)
getVersion header =
( fromIntegral (word32Value (headerEngineVersion header))
, fromIntegral (word32Value (headerLicenseeVersion header))
, Maybe.fromMaybe 0 (fmap (\ v -> fromIntegral (word32Value v)) (headerPatchVersion header))
)
getNumFrames :: Header -> Int
getNumFrames header =
let
key = textValue (stringToText "NumFrames")
properties = dictionaryValue (headerProperties header)
in case Map.lookup key properties of
Just (Property _ _ (IntProperty numFrames)) ->
fromIntegral (int32Value numFrames)
_ -> 0
getMaxChannels :: Header -> Word
getMaxChannels header =
let
key = textValue (stringToText "MaxChannels")
properties = dictionaryValue (headerProperties header)
in case Map.lookup key properties of
Just (Property _ _ (IntProperty numFrames)) ->
fromIntegral (int32Value numFrames)
_ -> 1023