module Rattletrap.Decode.PropertyValue
  ( decodePropertyValue
  )
where

import Rattletrap.Decode.Common
import Rattletrap.Decode.Dictionary
import Rattletrap.Decode.Float32le
import Rattletrap.Decode.Int32le
import Rattletrap.Decode.List
import Rattletrap.Decode.Str
import Rattletrap.Decode.Word64le
import Rattletrap.Decode.Word8le
import Rattletrap.Type.PropertyValue
import Rattletrap.Type.Str

decodePropertyValue :: Decode a -> Str -> Decode (PropertyValue a)
decodePropertyValue :: Decode a -> Str -> Decode (PropertyValue a)
decodePropertyValue Decode a
getProperty Str
kind = case Str -> String
fromStr Str
kind of
  String
"ArrayProperty" ->
    List (Dictionary a) -> PropertyValue a
forall a. List (Dictionary a) -> PropertyValue a
PropertyValueArray (List (Dictionary a) -> PropertyValue a)
-> Get (List (Dictionary a)) -> Decode (PropertyValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decode (Dictionary a) -> Get (List (Dictionary a))
forall a. Decode a -> Decode (List a)
decodeList (Decode a -> Decode (Dictionary a)
forall a. Decode a -> Decode (Dictionary a)
decodeDictionary Decode a
getProperty)
  String
"BoolProperty" -> Word8le -> PropertyValue a
forall a. Word8le -> PropertyValue a
PropertyValueBool (Word8le -> PropertyValue a)
-> Get Word8le -> Decode (PropertyValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8le
decodeWord8le
  String
"ByteProperty" -> do
    Str
k <- Decode Str
decodeStr
    Str -> Maybe Str -> PropertyValue a
forall a. Str -> Maybe Str -> PropertyValue a
PropertyValueByte Str
k
      (Maybe Str -> PropertyValue a)
-> Get (Maybe Str) -> Decode (PropertyValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> Decode Str -> Get (Maybe Str)
forall (m :: * -> *) (f :: * -> *) a.
(Applicative m, Alternative f) =>
Bool -> m a -> m (f a)
decodeWhen (Str -> String
fromStr Str
k String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= String
"OnlinePlatform_Steam") Decode Str
decodeStr
  String
"FloatProperty" -> Float32le -> PropertyValue a
forall a. Float32le -> PropertyValue a
PropertyValueFloat (Float32le -> PropertyValue a)
-> Get Float32le -> Decode (PropertyValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Float32le
decodeFloat32le
  String
"IntProperty" -> Int32le -> PropertyValue a
forall a. Int32le -> PropertyValue a
PropertyValueInt (Int32le -> PropertyValue a)
-> Get Int32le -> Decode (PropertyValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Int32le
decodeInt32le
  String
"NameProperty" -> Str -> PropertyValue a
forall a. Str -> PropertyValue a
PropertyValueName (Str -> PropertyValue a) -> Decode Str -> Decode (PropertyValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decode Str
decodeStr
  String
"QWordProperty" -> Word64le -> PropertyValue a
forall a. Word64le -> PropertyValue a
PropertyValueQWord (Word64le -> PropertyValue a)
-> Get Word64le -> Decode (PropertyValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word64le
decodeWord64le
  String
"StrProperty" -> Str -> PropertyValue a
forall a. Str -> PropertyValue a
PropertyValueStr (Str -> PropertyValue a) -> Decode Str -> Decode (PropertyValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decode Str
decodeStr
  String
_ -> String -> Decode (PropertyValue a)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"[RT07] don't know how to read property value " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Str -> String
forall a. Show a => a -> String
show Str
kind)