packages feed

mischief-assets-0.1.0.0: src/Mischief/Assets/Asset.hs

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# OPTIONS_GHC -Wno-redundant-constraints #-}

module Mischief.Assets.Asset
  ( Asset (..),
    OnLoad (..),
    setAssetSource,
    load,
    load_,
    loadRec,
    loadRec_,
    reload,
    retrieve,
    asset,
    assetRec,
    asset',
    Mischief.Assets.Asset.store,
    Mischief.Assets.Asset.save,
    saveEntity,
    Loaded,
    Loading,
    AssetType,
    decodeRef,
    Rec,
    addRec,
  )
where

import Control.Monad (void)
import Data.Set qualified as Set
import Data.Text (Text)
import Mischief.Assets.Internal.Asset
import Mischief.Assets.Internal.Path
import Mischief.ECS

load :: forall a. ((Loadable a ~ True), Asset a, Bundle a) => Text -> System Entity
load = loadByRelPath @a

load_ :: forall a. ((Loadable a ~ True), Asset a, Bundle a) => Text -> System ()
load_ = void . load @a

loadRec :: forall a. ((Loadable a ~ True), Asset a, Bundle a) => Text -> System Entity
loadRec t = do
  e <- loadByRelPath @a t
  insert LoadRec e
  pure e

loadRec_ :: forall a. ((Loadable a ~ True), Asset a, Bundle a) => Text -> System ()
loadRec_ t = do
  e <- loadByRelPath @a t
  insert LoadRec e

reload :: forall a. ((Loadable a ~ True), Asset a, Bundle a) => Text -> System Entity
reload = reloadByRelPath @a

assetRec :: forall a. AssetRef a -> System (Maybe a)
assetRec a = getAsset a True Set.empty

asset :: forall a. AssetRef a -> System (Maybe a)
asset a = getAsset a False Set.empty

asset' :: forall a. AssetRef a -> System (Maybe a)
asset' = getAsset'

retrieve :: forall a. AssetRef a -> System (Maybe Entity)
retrieve = getRefEntity @a

retrieve' :: forall a. AssetRef a -> System (Maybe Entity)
retrieve' = getRefEntity' @a

store :: (Asset a, Bundle a) => Text -> a -> System Entity
store = Mischief.Assets.Internal.Asset.store

save :: forall a. (Asset a, Saveable a ~ True, Bundle a) => Text -> System ()
save path = do
  e <- retrieve' @a (Ref path)
  case e of
    Nothing -> warn "Attempted to save an asset which isn't stored"
    Just e -> Mischief.Assets.Internal.Asset.save @a e

saveEntity :: forall a. (Asset a, Saveable a ~ True) => Entity -> System ()
saveEntity = Mischief.Assets.Internal.Asset.save @a