on decrease-android-validity help sign in
60 lines 2.1 kB history raw
1{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Obelisk.ExecutableConfig.Lookup where
5import Control.Exception (bracket)
6import Control.Monad (forM)
7import Data.ByteString (ByteString)
8import qualified Data.ByteString as BS
9import Data.Map (Map)
10import qualified Data.Map as Map
11import Data.Text (Text)
12import qualified Data.Text.Encoding as T
13import Foreign.C.String (withCString)
14import Foreign.Ptr (nullPtr)
16import Obelisk.ExecutableConfig.Internal.AssetManager
18getFromMgr :: AAssetManager -> BS.ByteString -> IO (Maybe ByteString)
19getFromMgr mgr name = do
20 let open = do
21 a <- BS.useAsCString name $ \fn ->
22 assetManager_open mgr fn 3
23 return $ if unAAsset a == nullPtr
24 then Nothing
25 else Just a
26 close = mapM_ asset_close
27 bracket open close $ mapM $ \asset -> do
28 b <- asset_getBuffer asset
29 l <- asset_getLength asset
30 BS.packCStringLen (b, fromIntegral l)
32getConfigs :: IO (Map Text ByteString)
33getConfigs = fmap (Map.mapKeys T.decodeUtf8) $ bracket getAssets freeAssetManager $ \mgrObj -> do
34 mgr <- assetManagerFromJava mgrObj
35 let openDir = do
36 d <- withCString "config.files" $ \fn ->
37 assetManager_open mgr fn 3
38 return $ if unAAsset d == nullPtr
39 then Nothing
40 else Just d
41 closeDir = mapM_ asset_close
42 configPaths <- bracket openDir closeDir $ \case
43 Just asset -> do
44 b <- asset_getBuffer asset
45 l <- asset_getLength asset
46 lines0 <$> BS.packCStringLen (b, fromIntegral l)
47 Nothing -> error "could not open configuration manifest 'config.files'"
48 result <- fmap Map.fromList $ forM configPaths $ \fp ->
49 getFromMgr mgr ("config/" <> fp) >>= \case
50 Just v -> return (fp, v)
51 Nothing -> error $ "Config present in config.files but not in assets: " <> show fp
52 putStrLn $ "getConfigs: found " <> show result
53 pure result
55lines0 :: BS.ByteString -> [BS.ByteString]
56lines0 ps
57 | BS.null ps = []
58 | otherwise = case BS.elemIndex 0 ps of
59 Nothing -> [ps]
60 Just n -> BS.take n ps : lines0 (BS.drop (n+1) ps)