-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssets.hs
More file actions
31 lines (26 loc) · 999 Bytes
/
Assets.hs
File metadata and controls
31 lines (26 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Assets
where
import Control.Monad
import Data.Generics
import qualified Data.Map as M
import Language.ECMAScript3
import Language.ECMAScript3.PrettyPrint
type JS = JavaScript SourcePos
assetGetterName = "getAsset"
getLoadedAssets :: JS -> [String]
getLoadedAssets = everything (++) $ [] `mkQ` assetName
where
assetName :: Expression SourcePos -> [String]
assetName (CallExpr _ func args)
| VarRef _ (Id _ funcName) <- func, funcName == assetGetterName,
[StringLit _ assetName] <- args = [assetName]
assetName _ = []
rewriteLoadedAssets :: M.Map String Int -> JS -> JS
rewriteLoadedAssets assetMap = everywhere $ mkT rewriteAssetName
where
rewriteAssetName :: Expression SourcePos -> Expression SourcePos
rewriteAssetName (CallExpr x1 func args)
| VarRef _ (Id _ funcName) <- func, funcName == assetGetterName,
[StringLit x2 assetName] <- args
= CallExpr x1 func [IntLit x2 (assetMap M.! assetName)]
rewriteAssetName x = x