never executed always true always false
1 module Reanimate.Morph.Cache
2 ( cachePointCorrespondence -- :: Int -> PointCorrespondence -> PointCorrespondence
3 ) where
4
5 import Control.Exception
6 import qualified Data.ByteString as B
7 import Data.Hashable
8 import Data.Serialize
9 import Reanimate.Cache (encodeInt)
10 import Reanimate.Misc (renameOrCopyFile)
11 import Reanimate.Morph.Common
12 import System.Directory
13 import System.FilePath
14 import System.IO
15 import System.IO.Temp
16 import System.IO.Unsafe
17
18 -- type PointCorrespondence = Polygon → Polygon → (Polygon, Polygon)
19 cachePointCorrespondence :: Int -> PointCorrespondence -> PointCorrespondence
20 cachePointCorrespondence ident fn src dst = unsafePerformIO $ do
21 root <- getXdgDirectory XdgCache "reanimate"
22 createDirectoryIfMissing True root
23 let path = root </> template
24 hit <- doesFileExist path
25 if hit
26 then do
27 inp <- B.readFile path
28 case decode inp of
29 Left{} -> do
30 removeFile path
31 gen path
32 Right out -> return out
33 else gen path
34 where
35 gen path = do
36 correspondence <- evaluate (fn src dst)
37 withSystemTempFile template $ \tmp h -> do
38 hClose h
39 B.writeFile tmp (encode correspondence)
40 renameOrCopyFile tmp path
41 return correspondence
42 template = encodeInt key <.> "morph"
43 key = hashWithSalt ident (src,dst)