gameServer/ConfigFile.hs
author unc0rr
Wed, 09 Mar 2011 21:41:16 +0300
changeset 4995 d3ca68e4860e
parent 4992 408301a9d2d6
child 5009 12135f659bf1
permissions -rw-r--r--
Fix comments spelling
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4975
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
     1
{-# LANGUAGE RankNTypes #-}
4974
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
     2
module ConfigFile where
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
     3
4975
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
     4
import Data.Maybe
4974
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
     5
import Data.TConfig
4975
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
     6
import qualified Data.ByteString.Char8 as B
4974
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
     7
-------------------
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
     8
import CoreTypes
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
     9
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    10
cfgFileName :: String
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    11
cfgFileName = "hedgewars-server.ini"
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    12
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    13
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    14
readServerConfig :: ServerInfo -> IO ServerInfo
4975
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    15
readServerConfig serverInfo' = do
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    16
    cfg <- readConfig cfgFileName
4975
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    17
    let si = serverInfo'{
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    18
        dbHost = value "dbHost" cfg
4982
3572eaf14340 Add dbName parameter to .ini file, fix some warnings
unc0rr
parents: 4975
diff changeset
    19
        , dbName = value "dbName" cfg
4975
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    20
        , dbLogin = value "dbLogin" cfg
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    21
        , dbPassword = value "dbPassword" cfg
4988
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    22
        , serverMessage = value "sv_message" cfg
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    23
        , serverMessageForOldVersions = value "sv_messageOld" cfg
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    24
        , latestReleaseVersion = read . fromJust $ getValue "sv_latestProto" cfg
4975
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    25
        , serverConfig = Just cfg
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    26
    }
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    27
    return si
31da8979e5b1 Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents: 4974
diff changeset
    28
    where
4988
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    29
        value n c = B.pack . fromJust2 n $ getValue n c
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    30
        fromJust2 n Nothing = error $ "Missing config entry " ++ n
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    31
        fromJust2 _ (Just a) = a
4974
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
    32
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    33
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    34
writeServerConfig :: ServerInfo -> IO ()
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    35
writeServerConfig ServerInfo{serverConfig = Nothing} = return ()
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    36
writeServerConfig ServerInfo{
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    37
    dbHost = dh,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    38
    dbName = dn,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    39
    dbLogin = dl,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    40
    dbPassword = dp,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    41
    serverMessage = sm,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    42
    serverMessageForOldVersions = smo,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    43
    latestReleaseVersion = ver,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    44
    serverConfig = Just cfg}
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    45
        = do
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    46
    let newCfg = foldl (\c (n, v) -> repConfig n (B.unpack v) c) cfg entries
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    47
    writeConfig cfgFileName (repConfig "sv_latestProto" (show ver) newCfg)
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    48
    where
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    49
        entries = [
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    50
            ("dbHost", dh)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    51
            , ("dbName", dn)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    52
            , ("dbLogin", dl)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    53
            , ("dbPassword", dp)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    54
            , ("sv_message", sm)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    55
            , ("sv_messageOld", smo)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    56
            ]