{-# LANGUAGE AllowAmbiguousTypes #-}
module Mischief.Live
(
attachLive,
live,
LiveExtensions (..),
Extension,
OnLiveLoad,
liveIfChanged,
ghc2024Extensions,
)
where
import Control.Concurrent
import Control.Concurrent.STM
import Control.Monad
import Data.Data (Proxy (Proxy), TypeRep, Typeable, typeRep)
import Data.Foldable (for_)
import Data.Map (Map)
import Data.Map qualified as Map
import Language.Haskell.Interpreter
import Mischief.ECS
import Mischief.ECS.App (App (..))
import Mischief.ECS.Resources (modifyRes')
import Mischief.ECS.World (SystemAny (systemAny), World (..), runSystem)
import Mischief.Live.Interpreter
import System.Directory
import System.FSNotify
import Unsafe.Coerce
newtype LiveExtensions = LiveExtensions [Extension] deriving newtype (NonEmpty LiveExtensions -> LiveExtensions
LiveExtensions -> LiveExtensions -> LiveExtensions
(LiveExtensions -> LiveExtensions -> LiveExtensions)
-> (NonEmpty LiveExtensions -> LiveExtensions)
-> (forall b. Integral b => b -> LiveExtensions -> LiveExtensions)
-> Semigroup LiveExtensions
forall b. Integral b => b -> LiveExtensions -> LiveExtensions
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
$c<> :: LiveExtensions -> LiveExtensions -> LiveExtensions
<> :: LiveExtensions -> LiveExtensions -> LiveExtensions
$csconcat :: NonEmpty LiveExtensions -> LiveExtensions
sconcat :: NonEmpty LiveExtensions -> LiveExtensions
$cstimes :: forall b. Integral b => b -> LiveExtensions -> LiveExtensions
stimes :: forall b. Integral b => b -> LiveExtensions -> LiveExtensions
Semigroup)
instance Default LiveExtensions where
def :: LiveExtensions
def =
LiveExtensions
ghc2024Extensions
LiveExtensions -> LiveExtensions -> LiveExtensions
forall a. Semigroup a => a -> a -> a
<> [Extension] -> LiveExtensions
LiveExtensions
[ Extension
DeriveAnyClass,
String -> Extension
UnknownExtension String
"DerivingStrategies",
Extension
DuplicateRecordFields,
String -> Extension
UnknownExtension String
"ImportQualifiedPost",
Extension
MultiWayIf,
String -> Extension
UnknownExtension String
"NoFieldSelectors",
String -> Extension
UnknownExtension String
"OverloadedRecordDot",
Extension
OverloadedStrings,
Extension
QuasiQuotes,
Extension
TypeFamilyDependencies
]
ghc2024Extensions :: LiveExtensions
ghc2024Extensions :: LiveExtensions
ghc2024Extensions =
[Extension] -> LiveExtensions
LiveExtensions
[ Extension
BangPatterns,
Extension
BinaryLiterals,
Extension
ConstrainedClassMethods,
Extension
ConstraintKinds,
Extension
DataKinds,
Extension
DeriveDataTypeable,
Extension
DeriveFoldable,
Extension
DeriveFunctor,
Extension
DeriveGeneric,
Extension
DeriveLift,
Extension
DeriveTraversable,
String -> Extension
UnknownExtension String
"DerivingStrategies",
Extension
DisambiguateRecordFields,
Extension
DoAndIfThenElse,
Extension
EmptyCase,
Extension
EmptyDataDecls,
String -> Extension
UnknownExtension String
"EmptyDataDeriving",
Extension
ExistentialQuantification,
Extension
ExplicitForAll,
Extension
ExplicitNamespaces,
String -> Extension
UnknownExtension String
"FieldSelectors",
Extension
FlexibleContexts,
Extension
FlexibleInstances,
Extension
ForeignFunctionInterface,
Extension
GADTs,
Extension
GADTSyntax,
String -> Extension
UnknownExtension String
"GeneralisedNewtypeDeriving",
String -> Extension
UnknownExtension String
"HexFloatLiterals",
Extension
ImplicitPrelude,
String -> Extension
UnknownExtension String
"ImportQualifiedPost",
Extension
InstanceSigs,
Extension
KindSignatures,
Extension
LambdaCase,
Extension
MonoLocalBinds,
Extension
MonomorphismRestriction,
Extension
MultiParamTypeClasses,
Extension
NamedFieldPuns,
Extension
NamedWildCards,
String -> Extension
UnknownExtension String
"NumericUnderscores",
Extension
PatternGuards,
Extension
PolyKinds,
Extension
PostfixOperators,
Extension
RankNTypes,
Extension
RelaxedPolyRec,
Extension
RoleAnnotations,
Extension
ScopedTypeVariables,
Extension
StandaloneDeriving,
String -> Extension
UnknownExtension String
"StandaloneKindSignatures",
String -> Extension
UnknownExtension String
"StarIsType",
Extension
TraditionalRecordSyntax,
Extension
TupleSections,
Extension
TypeApplications,
Extension
TypeOperators,
Extension
TypeSynonymInstances
]
attachLive :: FilePath -> [FilePath] -> LiveExtensions -> App -> IO ()
attachLive :: String -> [String] -> LiveExtensions -> App -> IO ()
attachLive String
path [String]
files (LiveExtensions [Extension]
ext) App
app = do
path <- String -> IO String
makeAbsolute String
path
chanReq <- newBroadcastTChanIO
chanReqRead <- atomically $ dupTChan chanReq
runSystem (insertRes $ InterpreterChan chanReq) app.world
runSystem (insertRes $ LiveRegistry Map.empty) app.world
void $ forkIO $ withCurrentDirectory path $ interpreterInit files ext chanReqRead
void $ forkIO $ withManager $ watcher app.world path
watcher :: World -> FilePath -> WatchManager -> IO ()
watcher :: World -> String -> WatchManager -> IO ()
watcher World {TVar [System ()]
deferredAsync :: TVar [System ()]
deferredAsync :: World -> TVar [System ()]
deferredAsync} String
dir WatchManager
mgr = do
IO (IO ()) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (IO ()) -> IO ()) -> IO (IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ WatchManager -> String -> ActionPredicate -> Action -> IO (IO ())
watchTree WatchManager
mgr String
dir (Bool -> ActionPredicate
forall a b. a -> b -> a
const Bool
True) (Action -> IO (IO ())) -> Action -> IO (IO ())
forall a b. (a -> b) -> a -> b
$ IO () -> Action
forall a b. a -> b -> a
const (IO () -> Action) -> IO () -> Action
forall a b. (a -> b) -> a -> b
$ STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
TVar [System ()] -> ([System ()] -> [System ()]) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar [System ()]
deferredAsync ([System ()] -> [System ()] -> [System ()]
forall a. [a] -> [a] -> [a]
++ [System ()
resetLiveCode])
IO () -> IO ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> IO ()
threadDelay Int
10000000000000000
resetLiveCode :: System ()
resetLiveCode :: System ()
resetLiveCode = do
Just (InterpreterChan chanReq) <- System (Maybe InterpreterChan)
forall c (m :: * -> *) w.
(MonadSystem w m, Component c) =>
m (Maybe c)
res
liftIO $ atomically $ writeTChan chanReq Reset
insertRes $ LiveRegistry Map.empty
trigger OnLiveLoad
data LiveKey = LiveKey
{ LiveKey -> String
moduleName :: String,
LiveKey -> String
valName :: String,
LiveKey -> TypeRep
valType :: TypeRep
}
deriving (LiveKey -> LiveKey -> Bool
(LiveKey -> LiveKey -> Bool)
-> (LiveKey -> LiveKey -> Bool) -> Eq LiveKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LiveKey -> LiveKey -> Bool
== :: LiveKey -> LiveKey -> Bool
$c/= :: LiveKey -> LiveKey -> Bool
/= :: LiveKey -> LiveKey -> Bool
Eq, Eq LiveKey
Eq LiveKey =>
(LiveKey -> LiveKey -> Ordering)
-> (LiveKey -> LiveKey -> Bool)
-> (LiveKey -> LiveKey -> Bool)
-> (LiveKey -> LiveKey -> Bool)
-> (LiveKey -> LiveKey -> Bool)
-> (LiveKey -> LiveKey -> LiveKey)
-> (LiveKey -> LiveKey -> LiveKey)
-> Ord LiveKey
LiveKey -> LiveKey -> Bool
LiveKey -> LiveKey -> Ordering
LiveKey -> LiveKey -> LiveKey
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: LiveKey -> LiveKey -> Ordering
compare :: LiveKey -> LiveKey -> Ordering
$c< :: LiveKey -> LiveKey -> Bool
< :: LiveKey -> LiveKey -> Bool
$c<= :: LiveKey -> LiveKey -> Bool
<= :: LiveKey -> LiveKey -> Bool
$c> :: LiveKey -> LiveKey -> Bool
> :: LiveKey -> LiveKey -> Bool
$c>= :: LiveKey -> LiveKey -> Bool
>= :: LiveKey -> LiveKey -> Bool
$cmax :: LiveKey -> LiveKey -> LiveKey
max :: LiveKey -> LiveKey -> LiveKey
$cmin :: LiveKey -> LiveKey -> LiveKey
min :: LiveKey -> LiveKey -> LiveKey
Ord)
data LiveValue where
LiveValue :: (Typeable a) => a -> LiveValue
newtype LiveRegistry = LiveRegistry (Map LiveKey LiveValue) deriving anyclass (Typeable LiveRegistry
[HookRel LiveRegistry]
[Hook LiveRegistry]
Set DefaultComponentType
IsExclusive (IsExclusiveRel LiveRegistry)
(Typeable LiveRegistry,
IsExclusive (IsExclusiveRel LiveRegistry)) =>
Set DefaultComponentType
-> [Hook LiveRegistry]
-> [Hook LiveRegistry]
-> [Hook LiveRegistry]
-> [HookRel LiveRegistry]
-> [HookRel LiveRegistry]
-> [HookRel LiveRegistry]
-> Component LiveRegistry
forall c.
(Typeable c, IsExclusive (IsExclusiveRel c)) =>
Set DefaultComponentType
-> [Hook c]
-> [Hook c]
-> [Hook c]
-> [HookRel c]
-> [HookRel c]
-> [HookRel c]
-> Component c
$crequired :: Set DefaultComponentType
required :: Set DefaultComponentType
$conAdd :: [Hook LiveRegistry]
onAdd :: [Hook LiveRegistry]
$conSet :: [Hook LiveRegistry]
onSet :: [Hook LiveRegistry]
$conRemove :: [Hook LiveRegistry]
onRemove :: [Hook LiveRegistry]
$conAddRel :: [HookRel LiveRegistry]
onAddRel :: [HookRel LiveRegistry]
$conSetRel :: [HookRel LiveRegistry]
onSetRel :: [HookRel LiveRegistry]
$conRemoveRel :: [HookRel LiveRegistry]
onRemoveRel :: [HookRel LiveRegistry]
Component)
live :: forall a m w. (Typeable a, MonadSystem w m) => ModuleName -> String -> m (Maybe a)
live :: forall a (m :: * -> *) w.
(Typeable a, MonadSystem w m) =>
String -> String -> m (Maybe a)
live String
m String
f = do
Just (InterpreterChan chanReq) <- m (Maybe InterpreterChan)
forall c (m :: * -> *) w.
(MonadSystem w m, Component c) =>
m (Maybe c)
res
Just (LiveRegistry reg) <- res
let key = LiveKey {moduleName :: String
moduleName = String
m, valName :: String
valName = String
f, valType :: TypeRep
valType = Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy a -> TypeRep) -> Proxy a -> TypeRep
forall a b. (a -> b) -> a -> b
$ forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a}
case Map.lookup key reg of
Just (LiveValue a
val) -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ a -> a
forall a b. a -> b
unsafeCoerce a
val
Maybe LiveValue
Nothing -> do
a <- IO (Maybe a) -> m (Maybe a)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> m (Maybe a)) -> IO (Maybe a) -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ do
t <- Result a -> IO (TMVar (Result a))
forall a. a -> IO (TMVar a)
newTMVarIO Result a
forall a. Result a
AwaitingResult
atomically $ writeTChan chanReq (Request m f t)
waitForResult t
for_ a $ \a
a -> do
let s :: System ()
s = (LiveRegistry -> LiveRegistry) -> System ()
forall r. (Component r, Bundle r) => (r -> r) -> System ()
modifyRes' ((LiveRegistry -> LiveRegistry) -> System ())
-> (LiveRegistry -> LiveRegistry) -> System ()
forall a b. (a -> b) -> a -> b
$ \(LiveRegistry Map LiveKey LiveValue
m) -> Map LiveKey LiveValue -> LiveRegistry
LiveRegistry (Map LiveKey LiveValue -> LiveRegistry)
-> Map LiveKey LiveValue -> LiveRegistry
forall a b. (a -> b) -> a -> b
$ LiveKey
-> LiveValue -> Map LiveKey LiveValue -> Map LiveKey LiveValue
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert LiveKey
key (a -> LiveValue
forall a. Typeable a => a -> LiveValue
LiveValue a
a) Map LiveKey LiveValue
m
System () -> ParSystem () -> m ()
forall a. System a -> ParSystem a -> m a
forall (m :: * -> *) a.
SystemAny m =>
System a -> ParSystem a -> m a
systemAny System ()
s (System () -> ParSystem ()
forall w (m :: * -> *) a. MonadSystem w m => System a -> m ()
defer System ()
s)
pure a
liveIfChanged :: forall a m w. (Typeable a, MonadSystem w m) => ModuleName -> String -> m (Maybe a)
liveIfChanged :: forall a (m :: * -> *) w.
(Typeable a, MonadSystem w m) =>
String -> String -> m (Maybe a)
liveIfChanged String
m String
f = do
Just (InterpreterChan chanReq) <- m (Maybe InterpreterChan)
forall c (m :: * -> *) w.
(MonadSystem w m, Component c) =>
m (Maybe c)
res
Just (LiveRegistry reg) <- res
let key = LiveKey {moduleName :: String
moduleName = String
m, valName :: String
valName = String
f, valType :: TypeRep
valType = Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy a -> TypeRep) -> Proxy a -> TypeRep
forall a b. (a -> b) -> a -> b
$ forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a}
case Map.lookup key reg of
Just LiveValue
_ -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
Maybe LiveValue
Nothing -> do
a <- IO (Maybe a) -> m (Maybe a)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> m (Maybe a)) -> IO (Maybe a) -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ do
t <- Result a -> IO (TMVar (Result a))
forall a. a -> IO (TMVar a)
newTMVarIO Result a
forall a. Result a
AwaitingResult
atomically $ writeTChan chanReq (Request m f t)
waitForResult t
for_ a $ \a
a -> do
let s :: System ()
s = (LiveRegistry -> LiveRegistry) -> System ()
forall r. (Component r, Bundle r) => (r -> r) -> System ()
modifyRes' ((LiveRegistry -> LiveRegistry) -> System ())
-> (LiveRegistry -> LiveRegistry) -> System ()
forall a b. (a -> b) -> a -> b
$ \(LiveRegistry Map LiveKey LiveValue
m) -> Map LiveKey LiveValue -> LiveRegistry
LiveRegistry (Map LiveKey LiveValue -> LiveRegistry)
-> Map LiveKey LiveValue -> LiveRegistry
forall a b. (a -> b) -> a -> b
$ LiveKey
-> LiveValue -> Map LiveKey LiveValue -> Map LiveKey LiveValue
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert LiveKey
key (a -> LiveValue
forall a. Typeable a => a -> LiveValue
LiveValue a
a) Map LiveKey LiveValue
m
System () -> ParSystem () -> m ()
forall a. System a -> ParSystem a -> m a
forall (m :: * -> *) a.
SystemAny m =>
System a -> ParSystem a -> m a
systemAny System ()
s (System () -> ParSystem ()
forall w (m :: * -> *) a. MonadSystem w m => System a -> m ()
defer System ()
s)
pure a
waitForResult :: TMVar (Result a) -> IO (Maybe a)
waitForResult :: forall a. TMVar (Result a) -> IO (Maybe a)
waitForResult TMVar (Result a)
t = do
x <- STM (Result a) -> IO (Result a)
forall a. STM a -> IO a
atomically (STM (Result a) -> IO (Result a))
-> STM (Result a) -> IO (Result a)
forall a b. (a -> b) -> a -> b
$ TMVar (Result a) -> STM (Result a)
forall a. TMVar a -> STM a
readTMVar TMVar (Result a)
t
case x of
Result a
a -> Maybe a -> IO (Maybe a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> IO (Maybe a)) -> Maybe a -> IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ a -> a
forall a b. a -> b
unsafeCoerce a
a
Result a
NoResult -> Maybe a -> IO (Maybe a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
Result a
AwaitingResult -> TMVar (Result a) -> IO (Maybe a)
forall a. TMVar (Result a) -> IO (Maybe a)
waitForResult TMVar (Result a)
t
data OnLiveLoad = OnLiveLoad deriving (Typeable OnLiveLoad
Typeable OnLiveLoad =>
(OnLiveLoad -> ErasedEvent) -> Event OnLiveLoad
OnLiveLoad -> ErasedEvent
forall e. Typeable e => (e -> ErasedEvent) -> Event e
$ceraseEvent :: OnLiveLoad -> ErasedEvent
eraseEvent :: OnLiveLoad -> ErasedEvent
Mischief.ECS.Event)