module Rattletrap.Decode.Frame
  ( decodeFramesBits
  )
where

import Rattletrap.Decode.Common
import Rattletrap.Decode.Float32le
import Rattletrap.Decode.Replication
import Rattletrap.Type.ClassAttributeMap
import Rattletrap.Type.CompressedWord
import Rattletrap.Type.Frame
import Rattletrap.Type.Word32le

import qualified Control.Monad.Trans.Class as Trans
import qualified Control.Monad.Trans.State as State
import qualified Data.Map as Map

decodeFramesBits
  :: (Int, Int, Int)
  -> Int
  -> Word
  -> ClassAttributeMap
  -> State.StateT
       (Map.Map CompressedWord Word32le)
       DecodeBits
       [Frame]
decodeFramesBits :: (Int, Int, Int)
-> Int
-> Word
-> ClassAttributeMap
-> StateT (Map CompressedWord Word32le) DecodeBits [Frame]
decodeFramesBits (Int, Int, Int)
version Int
count Word
limit ClassAttributeMap
classes = if Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
  then [Frame] -> StateT (Map CompressedWord Word32le) DecodeBits [Frame]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
  else
    (:)
    (Frame -> [Frame] -> [Frame])
-> StateT (Map CompressedWord Word32le) DecodeBits Frame
-> StateT
     (Map CompressedWord Word32le) DecodeBits ([Frame] -> [Frame])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Int, Int)
-> Word
-> ClassAttributeMap
-> StateT (Map CompressedWord Word32le) DecodeBits Frame
decodeFrameBits (Int, Int, Int)
version Word
limit ClassAttributeMap
classes
    StateT
  (Map CompressedWord Word32le) DecodeBits ([Frame] -> [Frame])
-> StateT (Map CompressedWord Word32le) DecodeBits [Frame]
-> StateT (Map CompressedWord Word32le) DecodeBits [Frame]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Int, Int, Int)
-> Int
-> Word
-> ClassAttributeMap
-> StateT (Map CompressedWord Word32le) DecodeBits [Frame]
decodeFramesBits (Int, Int, Int)
version (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Word
limit ClassAttributeMap
classes

decodeFrameBits
  :: (Int, Int, Int)
  -> Word
  -> ClassAttributeMap
  -> State.StateT (Map.Map CompressedWord Word32le) DecodeBits Frame
decodeFrameBits :: (Int, Int, Int)
-> Word
-> ClassAttributeMap
-> StateT (Map CompressedWord Word32le) DecodeBits Frame
decodeFrameBits (Int, Int, Int)
version Word
limit ClassAttributeMap
classes =
  Float32le -> Float32le -> [Replication] -> Frame
Frame
    (Float32le -> Float32le -> [Replication] -> Frame)
-> StateT (Map CompressedWord Word32le) DecodeBits Float32le
-> StateT
     (Map CompressedWord Word32le)
     DecodeBits
     (Float32le -> [Replication] -> Frame)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BitGet Float32le
-> StateT (Map CompressedWord Word32le) DecodeBits Float32le
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
Trans.lift BitGet Float32le
decodeFloat32leBits
    StateT
  (Map CompressedWord Word32le)
  DecodeBits
  (Float32le -> [Replication] -> Frame)
-> StateT (Map CompressedWord Word32le) DecodeBits Float32le
-> StateT
     (Map CompressedWord Word32le) DecodeBits ([Replication] -> Frame)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> BitGet Float32le
-> StateT (Map CompressedWord Word32le) DecodeBits Float32le
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
Trans.lift BitGet Float32le
decodeFloat32leBits
    StateT
  (Map CompressedWord Word32le) DecodeBits ([Replication] -> Frame)
-> StateT (Map CompressedWord Word32le) DecodeBits [Replication]
-> StateT (Map CompressedWord Word32le) DecodeBits Frame
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Int, Int, Int)
-> Word
-> ClassAttributeMap
-> StateT (Map CompressedWord Word32le) DecodeBits [Replication]
decodeReplicationsBits (Int, Int, Int)
version Word
limit ClassAttributeMap
classes