-- This module parses server.properties file, -- the Minecraft configuration file module Network.Loshadka.Properties (Properties, readProps) where import Network (PortNumber) import Data.Maybe import Data.List.Split (splitOn) data Property = MOTD String | ServerPort PortNumber | MaxPlayers Int | WhiteList Bool deriving (Eq) type Properties = (String, PortNumber, Int, Bool) type Key = (String, Maybe Property) readProps :: String -> IO Properties readProps path = do props <- readFile (path ++ "/server.properties") return $ parse props parse :: String -> Properties parse props = (motd, port, maxPlayers, whiteList) where (MOTD motd : ServerPort port : MaxPlayers maxPlayers : WhiteList whiteList : []) = fetchLines props fetchLines :: String -> [Property] fetchLines props = fetchProperties $ map parseLine (lines props) parseLine :: String -> Key parseLine prop = do if (head prop) == '#' then ("", Nothing) else case splitOn "=" prop of ["motd", value] -> ("motd", Just $ MOTD value) ["server-port", value] -> ("server-port", Just $ ServerPort $ fromIntegral (read value :: Integer)) ["max-players", value] -> ("max-players", Just $ MaxPlayers $ read value) ["white-list", value] -> ("white-list", Just $ WhiteList $ read value) _ -> ("", Nothing) fetchProperties :: [Key] -> [Property] fetchProperties ps = [motd, serverPort, maxPlayers, whiteList] where ns = filter (/= ("", Nothing)) ps motd = fetchProperty "motd" ns serverPort = fetchProperty "server-port" ns maxPlayers = fetchProperty "max-players" ns whiteList = fetchProperty "white-list" ns fetchProperty :: String -> [Key] -> Property fetchProperty k ps = fromJust $ fromJust $ lookup k ps