gameServer/ConfigFile.hs
author nemo
Sun, 01 Nov 2015 15:08:39 -0500
changeset 11272 9621fdcad965
parent 11046 47a8c19ecb60
permissions -rw-r--r--
do the same thing for the chute/birdy/saucer block
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10460
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
     1
{-
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
     2
 * Hedgewars, a free turn based strategy game
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10460
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
10460
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
     4
 *
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
     8
 *
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    12
 * GNU General Public License for more details.
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    13
 *
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    14
 * You should have received a copy of the GNU General Public License
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    15
 * along with this program; if not, write to the Free Software
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    17
 \-}
8dcea9087d75 Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents: 8401
diff changeset
    18
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
    19
{-# LANGUAGE RankNTypes #-}
4974
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
    20
module ConfigFile where
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
    21
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
    22
import Data.Maybe
4974
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
    23
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
    24
import qualified Data.ByteString.Char8 as B
4974
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
    25
-------------------
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
    26
import CoreTypes
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
    27
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    28
cfgFileName :: String
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    29
cfgFileName = "hedgewars-server.ini"
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    30
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    31
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    32
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
    33
readServerConfig serverInfo' = do
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    34
    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
    35
    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
    36
        dbHost = value "dbHost" cfg
4982
3572eaf14340 Add dbName parameter to .ini file, fix some warnings
unc0rr
parents: 4975
diff changeset
    37
        , 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
    38
        , 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
    39
        , dbPassword = value "dbPassword" cfg
4988
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    40
        , serverMessage = value "sv_message" cfg
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    41
        , serverMessageForOldVersions = value "sv_messageOld" cfg
5009
12135f659bf1 Store bans in .ini file
unc0rr
parents: 4992
diff changeset
    42
        , bans = read . fromJust2 "bans" $ getValue "bans" cfg
4988
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    43
        , 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
    44
        , 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
    45
    }
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
    46
    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
    47
    where
4988
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    48
        value n c = B.pack . fromJust2 n $ getValue n c
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    49
        fromJust2 n Nothing = error $ "Missing config entry " ++ n
bd540ba66599 Store more parameters in ini file
unc0rr
parents: 4982
diff changeset
    50
        fromJust2 _ (Just a) = a
4974
078cd026a7b1 Add stubs for server config reading and writing routines
unc0rr
parents:
diff changeset
    51
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    52
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4990
diff changeset
    53
writeServerConfig :: ServerInfo -> IO ()
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    54
writeServerConfig ServerInfo{serverConfig = Nothing} = return ()
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    55
writeServerConfig ServerInfo{
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    56
    dbHost = dh,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    57
    dbName = dn,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    58
    dbLogin = dl,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    59
    dbPassword = dp,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    60
    serverMessage = sm,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    61
    serverMessageForOldVersions = smo,
5009
12135f659bf1 Store bans in .ini file
unc0rr
parents: 4992
diff changeset
    62
    bans = b,
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    63
    latestReleaseVersion = ver,
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    64
    serverConfig = Just cfg}
5009
12135f659bf1 Store bans in .ini file
unc0rr
parents: 4992
diff changeset
    65
        =
12135f659bf1 Store bans in .ini file
unc0rr
parents: 4992
diff changeset
    66
    writeConfig cfgFileName $ foldl1 (.) entries cfg
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    67
    where
5009
12135f659bf1 Store bans in .ini file
unc0rr
parents: 4992
diff changeset
    68
        entries =
12135f659bf1 Store bans in .ini file
unc0rr
parents: 4992
diff changeset
    69
            repConfig "sv_latestProto" (show ver)
12135f659bf1 Store bans in .ini file
unc0rr
parents: 4992
diff changeset
    70
            : repConfig "bans" (show b)
12135f659bf1 Store bans in .ini file
unc0rr
parents: 4992
diff changeset
    71
            : map (\(n, v) -> repConfig n (B.unpack v)) [
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    72
            ("dbHost", dh)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    73
            , ("dbName", dn)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    74
            , ("dbLogin", dl)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    75
            , ("dbPassword", dp)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    76
            , ("sv_message", sm)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    77
            , ("sv_messageOld", smo)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents: 4988
diff changeset
    78
            ]