From 0d479cba878681ca6f1bc376a76905a546a6ede8 Mon Sep 17 00:00:00 2001 From: justin Date: Mon, 25 Jul 2022 23:24:09 +0100 Subject: [PATCH] Use JSON as save format --- src/Dodge/Data/ActionPlan.hs | 2 +- src/Dodge/Data/WorldEffect.hs | 4 ++-- src/Dodge/Save.hs | 29 +++++++++++++++++++---------- src/Geometry/Data.hs | 2 +- src/IntMapHelp.hs | 4 ++-- src/Picture/Data.hs | 1 - src/Quaternion.hs | 4 ++-- 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/Dodge/Data/ActionPlan.hs b/src/Dodge/Data/ActionPlan.hs index de00b4ff8..b583bb4be 100644 --- a/src/Dodge/Data/ActionPlan.hs +++ b/src/Dodge/Data/ActionPlan.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE DeriveGeneric #-} +--{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE StrictData #-} {-# LANGUAGE DeriveGeneric #-} diff --git a/src/Dodge/Data/WorldEffect.hs b/src/Dodge/Data/WorldEffect.hs index 58d0f0a32..e1b64afa3 100644 --- a/src/Dodge/Data/WorldEffect.hs +++ b/src/Dodge/Data/WorldEffect.hs @@ -1,5 +1,5 @@ -{-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE TemplateHaskell #-} +--{-# LANGUAGE DeriveGeneric #-} +--{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE StrictData #-} {-# LANGUAGE DeriveGeneric #-} module Dodge.Data.WorldEffect where diff --git a/src/Dodge/Save.hs b/src/Dodge/Save.hs index 44434b4e7..f8e18c7ad 100644 --- a/src/Dodge/Save.hs +++ b/src/Dodge/Save.hs @@ -7,8 +7,10 @@ module Dodge.Save , readSaveSlot ) where import Dodge.Data - -import Text.Read (readMaybe) +import Data.Aeson +import qualified Data.Aeson.Encode.Pretty as AEP +import qualified Data.ByteString.Lazy as BS +--import Text.Read (readMaybe) import qualified Data.Map.Strict as M --import Data.Maybe import Control.Lens @@ -18,24 +20,31 @@ import System.Directory writeSaveSlot :: SaveSlot -> Universe -> IO () writeSaveSlot ss u = do createDirectoryIfMissing True "saveSlot" - writeFile (saveSlotPath ss) (show $ u ^. uvWorld . cWorld) - -saveSlotPath :: SaveSlot -> String -saveSlotPath (SaveSlotNum i) = "saveSlot/" ++ show i -saveSlotPath QuicksaveSlot = "saveSlot/QuickSave" -saveSlotPath LevelStartSlot = "saveSlot/LevelStartSave" + BS.writeFile (saveSlotPath ss) + $ AEP.encodePretty' + (AEP.Config (AEP.Spaces 2) compare AEP.Generic False) + (u ^. uvWorld . cWorld) + --writeFile (saveSlotPath ss) (show $ u ^. uvWorld . cWorld) readSaveSlot :: SaveSlot -> Universe -> IO Universe readSaveSlot ss uv = do fExists <- doesFileExist $ saveSlotPath ss if fExists then do - cwstr <- readFile $ saveSlotPath ss - case readMaybe cwstr of + --cwstr <- readFile $ saveSlotPath ss + --case readMaybe cwstr of + cwstr <- decodeFileStrict $ saveSlotPath ss + case cwstr of Nothing -> putStrLn "loadSaveSlot failed to read saved file" >> return uv Just cw -> return $ uv & uvWorld . cWorld .~ cw else putStrLn "loadSaveSlot failed to find saved file" >> return uv +saveSlotPath :: SaveSlot -> String +saveSlotPath (SaveSlotNum i) = "saveSlot/" ++ show i +saveSlotPath QuicksaveSlot = "saveSlot/QuickSave" +saveSlotPath LevelStartSlot = "saveSlot/LevelStartSave" + + saveWorldInEmptySlot :: SaveSlot -> Universe -> Universe saveWorldInEmptySlot slot w = case M.lookup slot $ _savedWorlds w of Nothing -> w & savedWorlds %~ M.insert slot (clearKeys (_uvWorld w)) diff --git a/src/Geometry/Data.hs b/src/Geometry/Data.hs index 61652d3db..0c8a69a3b 100644 --- a/src/Geometry/Data.hs +++ b/src/Geometry/Data.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE DeriveGeneric #-} +--{-# LANGUAGE DeriveGeneric #-} {-# OPTIONS_GHC -fno-warn-orphans #-} {- | WARNING: orphan instances concerning Aeson classes and Linear.Vx datatypes have been introduced. diff --git a/src/IntMapHelp.hs b/src/IntMapHelp.hs index a455dae10..deec9e923 100644 --- a/src/IntMapHelp.hs +++ b/src/IntMapHelp.hs @@ -1,5 +1,5 @@ -{-# LANGUAGE StandaloneDeriving #-} -{-# LANGUAGE DeriveGeneric #-} +--{-# LANGUAGE StandaloneDeriving #-} +--{-# LANGUAGE DeriveGeneric #-} {-# OPTIONS_GHC -Wno-unused-imports #-} {-# OPTIONS_GHC -fno-warn-orphans #-} {- | diff --git a/src/Picture/Data.hs b/src/Picture/Data.hs index bca8ae5bc..93d2c8495 100644 --- a/src/Picture/Data.hs +++ b/src/Picture/Data.hs @@ -1,5 +1,4 @@ {-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE StrictData #-} {-# LANGUAGE TemplateHaskell #-} module Picture.Data diff --git a/src/Quaternion.hs b/src/Quaternion.hs index 86aef24a1..a85ccea91 100644 --- a/src/Quaternion.hs +++ b/src/Quaternion.hs @@ -1,5 +1,5 @@ -{-# LANGUAGE StandaloneDeriving #-} -{-# LANGUAGE DeriveGeneric #-} +--{-# LANGUAGE StandaloneDeriving #-} +--{-# LANGUAGE DeriveGeneric #-} {-# OPTIONS_GHC -fno-warn-orphans #-} {- | WARNING: orphan instances concerning Aeson classes and Linear.Quaternion datatypes have been introduced.