gameServer/ConfigFile.hs
author Simon McVittie <smcv@debian.org>
Mon, 12 Sep 2022 10:40:53 -0400
branch1.0.0
changeset 15859 7b1d6dfa3173
parent 11046 47a8c19ecb60
permissions -rw-r--r--
Remove FindSDL2 find-module, use sdl2-config.cmake instead This requires SDL >= 2.0.4. Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was not itself built using CMake: it installs a sdl2-config.cmake file to ${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and library, analogous to a pkg-config .pc file. As a result, we no longer need to copy/paste a "find-module package" to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2). Find-module packages are now discouraged by the CMake developers, in favour of having upstream projects behave as config-file packages. This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR and SDL2_LIBRARY, but the standard behaviour for config-file packages is to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword to make sure we search in config-file package mode, and will not find a FindSDL2.cmake in some other directory that implements the old interface. In addition to deleting redundant code, this avoids some assumptions in FindSDL2 about the layout of a SDL installation. The current libsdl2-dev package in Debian breaks those assumptions; this is considered a bug and will hopefully be fixed soon, but it illustrates how fragile these assumptions can be. We can be more robust against different installation layouts by relying on SDL's own CMake integration. When linking to a copy of CMake in a non-standard location, users can now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point to it; previously, these users would have used the SDL2DIR environment variable. This continues to be unnecessary if using matching system-wide installations of CMake and SDL2, for example both from Debian.
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
            ]