| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
Mischief.Live
Synopsis
- attachLive :: FilePath -> [FilePath] -> LiveExtensions -> App -> IO ()
- live :: forall a m w. (Typeable a, MonadSystem w m) => ModuleName -> String -> m (Maybe a)
- newtype LiveExtensions = LiveExtensions [Extension]
- data Extension
- data OnLiveLoad
- liveIfChanged :: forall a m w. (Typeable a, MonadSystem w m) => ModuleName -> String -> m (Maybe a)
- ghc2024Extensions :: LiveExtensions
Mischief Live
This library is a lightweight, yet powerful, addition to the core ECS library. It enables a form of live coding by running a background interpreter over the code from a chosen target, letting you modify the functions from it during the runtime of your main compiled library.
Project Structure
I will be describing the general recommended structure of a mischief app that uses this library. It is also recommended to
look at or clone one of the -live branches in the template repository for a more concrete example
or starting point.
In order to properly use this library, you will need at least one library besides your executable (the main "library" that you're running). You can define
it in your .cabal file. In the template, this library is generally named live, with it's starting point in a Live.hs module.
In this live library you can write any code that you wish to be able to freely change at runtime. Systems, shaders, and so on.
Your executable cannot depend on the live library, and the live library cannot depend on your executable. Because of this restriction, it is recommended to create a third library, containing the common logic and definitions that you wish both the executable and the live library to see. You'll generally define types, such as components and materials in this common library.
How to Run
In order to run your executable, you should use the following command:
cabal run --write-ghc-environment-files=always --enable-shared --enable-executable-dynamic
The first flag will write an environment file which the interpreter can read to see all dependencies. The other two
ensure your local libraries are compiled dynamically so that the interpreter can access them. These are very important! GHC
is likely to be stuck in the linking phase if you depend on mischief-live and don't compile your code dynamically!
On windows you may also need to add the -fthreaded flag, as this library uses fsnotify for reacting to file changes.
How to Use
This library's API is really simple! It only has two functions you need to be aware of.
First, you need to use attachLive to attach the interpreter to your app before running it:
app <-newAppattachLive"./live/" ["Live.hs"]defapp
The first parameter is the directory where your live library is located. The second is a list of
files to load when initializing the interpreter, relative to the previous path. There is also
a list of extensions you need to pass, but more on that later, def is usually enough.
Second, there is the main primitive introduces by this library, live:
x <-live@Int"Live" "myInt"
The live function takes a module name and a top-level function from that module, and produces its latest value. In this case,
the Live module is expected to look something like this:
module Live where
myInt :: Int
myInt = 3
The @Int type hint is optional, live usually infers the value.
This function is smart. It caches the functions you've already requested, so it only interprets them the first time you ask for them, at the start of the app or after you made a change to the live library.
There is also the function which will return a value only if this is the first time you're asking for it since the last change in the live library.liveIfChanged
The event is triggered each time a change in the live library is detected and the interpreter is reset.OnLiveLoad
Extensions
A list of language extensions that are applied when interpreting the live library need to be provided. The Default instance
adds all the extensions enabled by default by ghc2024, as well as the set of extensions recommended by Mischief. You can see
the exact list in the source of this file!
In order to customize the list of extensions, you can use the ghc2024Extensions and the def as a starting point, adding more extensions on top. For instance:
myExtensions ::LiveExtensionsmyExtensions =ghc2024Extensions<> LiveExtensions [UndecidableInstances]
You should set these to match the extensions listed under your live library in the .cabal file.
Preparing for Release
When releasing your app, you'll want to comment out the mischief-live dependencies in your .cabal file, and compile your
app normally, without the dynamic flags.
The simplest approach to ensure your app still works as expected with minimal refactoring is to have your executable depend on the
live library and import it, replacing all the live calls with actual calls to the relevant functions.
Options are being considered for streamlining this process, such as a macro that can smartly switch between live and the
direct calls. Your feedback and ideas are appreciated!
attachLive :: FilePath -> [FilePath] -> LiveExtensions -> App -> IO () Source #
live :: forall a m w. (Typeable a, MonadSystem w m) => ModuleName -> String -> m (Maybe a) Source #
newtype LiveExtensions Source #
Constructors
| LiveExtensions [Extension] |
Instances
| Default LiveExtensions Source # | |
Defined in Mischief.Live Methods def :: LiveExtensions # | |
| Semigroup LiveExtensions Source # | |
Defined in Mischief.Live Methods (<>) :: LiveExtensions -> LiveExtensions -> LiveExtensions # sconcat :: NonEmpty LiveExtensions -> LiveExtensions # stimes :: Integral b => b -> LiveExtensions -> LiveExtensions # | |
This represents language extensions beyond Haskell 98
that are supported by GHC (it was taken from
Cabal's Language.Haskell.Extension)
data OnLiveLoad Source #
Instances
| Event OnLiveLoad Source # | |
Defined in Mischief.Live Methods eraseEvent :: OnLiveLoad -> ErasedEvent # | |
liveIfChanged :: forall a m w. (Typeable a, MonadSystem w m) => ModuleName -> String -> m (Maybe a) Source #