Add System.Directory library, allow for testing file existence

This commit is contained in:
2021-03-26 20:02:27 +01:00
parent 5620e45e0a
commit 485952fe53
2 changed files with 35 additions and 28 deletions
+1
View File
@@ -45,6 +45,7 @@ dependencies:
- foldl - foldl
- linear - linear
- aeson - aeson
- directory
library: library:
source-dirs: src source-dirs: src
+33 -27
View File
@@ -10,6 +10,8 @@ import qualified GHC.Int
import qualified SDL import qualified SDL
import SDL.Internal.Numbered as SDL.Internal.Numbered import SDL.Internal.Numbered as SDL.Internal.Numbered
import System.Directory
data Configuration = Configuration data Configuration = Configuration
{ windowxsize :: Int, { windowxsize :: Int,
windowysize :: Int windowysize :: Int
@@ -133,33 +135,37 @@ instance FromJSON KeyConfig where
loadKeyConfig :: IO KeyConfigSDL loadKeyConfig :: IO KeyConfigSDL
loadKeyConfig = do loadKeyConfig = do
mayConfig <- decodeFileStrict "keys.json" fExists <- doesFileExist "keys.json"
print mayConfig if fExists
case mayConfig of then do
Just config -> mayConfig <- decodeFileStrict "keys.json"
return print mayConfig
KeyConfigSDL case mayConfig of
{ moveUpKey = getSdlScancode $ moveUpBinding config, Just config ->
moveDownKey = getSdlScancode $ moveDownBinding config, return
moveLeftKey = getSdlScancode $ moveLeftBinding config, KeyConfigSDL
moveRightKey = getSdlScancode $ moveRightBinding config, { moveUpKey = getSdlScancode $ moveUpBinding config,
pauseKey = getSdlScancode $ pauseBinding config, moveDownKey = getSdlScancode $ moveDownBinding config,
escapeKey = getSdlScancode $ escapeBinding config, moveLeftKey = getSdlScancode $ moveLeftBinding config,
dropItemKey = getSdlScancode $ dropItemBinding config, moveRightKey = getSdlScancode $ moveRightBinding config,
toggleMapKey = getSdlScancode $ toggleMapBinding config, pauseKey = getSdlScancode $ pauseBinding config,
reloadKey = getSdlScancode $ reloadBinding config, escapeKey = getSdlScancode $ escapeBinding config,
testEventKey = getSdlScancode $ testEventBinding config, dropItemKey = getSdlScancode $ dropItemBinding config,
spaceActionKey = getSdlScancode $ spaceActionBinding config, toggleMapKey = getSdlScancode $ toggleMapBinding config,
rotateCameraPlusKey = getSdlScancode $ rotateCameraPlusBinding config, reloadKey = getSdlScancode $ reloadBinding config,
rotateCameraMinusKey = getSdlScancode $ rotateCameraMinusBinding config, testEventKey = getSdlScancode $ testEventBinding config,
zoomInKey = getSdlScancode $ zoomInBinding config, spaceActionKey = getSdlScancode $ spaceActionBinding config,
zoomOutKey = getSdlScancode $ zoomOutBinding config, rotateCameraPlusKey = getSdlScancode $ rotateCameraPlusBinding config,
newKey = getSdlScancode $ newBinding config rotateCameraMinusKey = getSdlScancode $ rotateCameraMinusBinding config,
} zoomInKey = getSdlScancode $ zoomInBinding config,
Nothing -> do zoomOutKey = getSdlScancode $ zoomOutBinding config,
putStrLn "invalid keys.json, loading default config" newKey = getSdlScancode $ newBinding config
-- This is duplicated but not sure how to reduce }
return defaultKeyConfigSDL Nothing -> do
putStrLn "invalid keys.json, loading default config"
-- This is duplicated but not sure how to reduce
return defaultKeyConfigSDL
else return defaultKeyConfigSDL
getSdlScancode :: Int -> SDL.Scancode getSdlScancode :: Int -> SDL.Scancode
getSdlScancode x = SDL.Internal.Numbered.fromNumber (fromIntegral x) :: SDL.Scancode getSdlScancode x = SDL.Internal.Numbered.fromNumber (fromIntegral x) :: SDL.Scancode