-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
90 lines (76 loc) · 3.16 KB
/
Main.hs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{-# LANGUAGE Safe #-}
{- |
Module : Main
Description : Main module for Yasgheld
Copyright : (c) Andrew Ray, 2017
License : MIT
Main module for Yasgheld
-}
module Main (main) where
import System.Environment (getArgs)
import System.IO
import Data.List (isSubsequenceOf)
import Data.Aviary.Birds
import SoundChange
import SoundGroup
import Util (replace, removeComment, dropBlanks)
{- |
IO Handler for the game
-}
main :: IO ()
main =
do { [infile, scfile, sgfile, outfile] <- getArgs -- get filenames
-- Read the sound group file
; soundgroupfile <- openFile (fileExt sgfile ".ygg") ReadMode
; sgfcontent <- hGetContents soundgroupfile
; let soundgroups =
if ".ygg1" `isSubsequenceOf` sgfile
then
map ((\(a,b) -> (a,b, Nothing)).read.removeComment) $
dropBlanks $ lines sgfcontent
else
zipWith parseSoundGroup [1..] $ dropBlanks $ lines sgfcontent
-- Read the sound change file
; soundchangefile <- openFile (fileExt scfile ".ygc") ReadMode
; scfcontent <- hGetContents soundchangefile
; let soundchanges =
map (read.removeComment) $ -- remove comments and read
dropBlanks $ -- remove blank lines
lines $ subGroups soundgroups scfcontent
-- Read the word list file
; wordlistinfile <- openFile (fileExt infile ".ygw") ReadMode
; wlifcontent <- hGetContents wordlistinfile
; let wordlistin = map removeComment $ dropBlanks $ lines wlifcontent
-- Write the output file
; let wordlistout = applyAllToAll soundchanges soundgroups wordlistin
; writeFile (fileExt outfile ".ygw") (unlines wordlistout)
; hClose soundchangefile
; hClose wordlistinfile
; hClose soundgroupfile
}
-- | Append the given extension to the given filename if no
-- other extension is already present
fileExt :: String -- ^ The filename to append the extension to
-> String -- ^ The extension to append
-> String -- ^ The filename with extension
fileExt fn ext = if '.' `elem` fn then fn else fn ++ ext
-- | applies all given <SoundChange>s to all given Strings.
applyAllToAll :: [SoundChange] -- ^ <SoundChange>s to apply
-> [SoundGroup] -- ^ <SoundGroup>s to use during application
-> [String] -- ^ Strings to apply SoundChanges to
-> [String] -- ^ String after application
applyAllToAll scs sgs = map $ foldr ((.).(`applySoundChange` sgs)) id scs
-- | Substitutes long names for short names in the given string
-- for all given SoundGroups, if they have long names
subGroups :: [SoundGroup] -- ^ <SoundGroup>s to use
-> String -- ^ String to replace in
-> String -- ^ String after replacement
subGroups = foldr ((.).replaceGroup) id
-- | Substitutes long name for short name in the given string
-- for the given SoundGroup, if it has a long name
replaceGroup :: SoundGroup -- ^ The <SoundGroup> to use
-> String -- ^ String to replace in
-> String -- ^ String after replacement
replaceGroup (_, _, Nothing) str = str
replaceGroup (short, _, Just long) str =
replace ("[" ++ long ++ "]") [short] str