Crossfire Server, Trunk  1.75.0
init.cpp File Reference
#include "global.h"
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "loader.h"
#include "version.h"
#include "server.h"
#include "sproto.h"
#include "assets.h"
#include "modules.h"
+ Include dependency graph for init.cpp:

Go to the source code of this file.

Data Structures

struct  Command_Line_Options
 One command line option definition. More...
 
struct  module_information
 

Typedefs

typedef void(* cmdlinefunc_args0) (void)
 Typedefs used when calling option handlers. More...
 
typedef void(* cmdlinefunc_args1) (const char *arg1)
 
typedef void(* cmdlinefunc_args2) (const char *arg1, const char *arg2)
 

Functions

void add_server_collect_hooks ()
 
static void call_version (void)
 Command line option: show version. More...
 
void close_modules ()
 Clean up all modules which are not disabled. More...
 
static void do_module (const char *name, bool enabled)
 Change the 'enabled' flag of a module. More...
 
static void free_materials (void)
 Frees all memory allocated to materials. More...
 
void free_server (void)
 Frees all memory allocated around here: More...
 
static materialtype_tget_empty_mat (void)
 Creates an empty materialtype_t structure. More...
 
static void help (void)
 Display the command line options and exits. More...
 
void init (int argc, char **argv)
 This is the main server initialization function. More...
 
static void init_beforeplay (void)
 Called before the server starts listening to connections, processes various dump-related options. More...
 
void init_modules ()
 Init all modules which are not disabled. More...
 
void init_signals (void)
 Setup our signal handlers. More...
 
static void init_startup (void)
 Checks if starting the server is allowed. More...
 
static void list_modules ()
 List all modules, then exit. More...
 
static void load_materials (BufferReader *reader, const char *filename)
 Loads the materials. More...
 
static void load_settings (void)
 This loads the settings file. More...
 
static void parse_args (int argc, char *argv[], int pass)
 Parse command line arguments. More...
 
static void rec_sighup (int i)
 SIGHUP handler. More...
 
static void server_dump_animations (void)
 Dump all animations, then exit. More...
 
static void server_dump_bonuses ()
 Dump all bonuses (from the stat_bonus file) then exit. More...
 
static void server_dump_faces (void)
 Dump all faces, then exit. More...
 
static void server_pack_assets (const char *assets, const char *filename)
 
static void set_confdir (const char *path)
 Command line option: set configuration path. More...
 
static void set_csport (const char *val)
 Change the server's port. More...
 
static void set_datadir (const char *path)
 Command line option: set data path. More...
 
static void set_debug (void)
 Command line option: debug flag. More...
 
static void set_disable_module (const char *name)
 Disable a module. More...
 
static void set_disable_plugin (const char *name)
 Disable a plugin. More...
 
static void set_dumpmon1 (void)
 Command line option: dump monsters. More...
 
static void set_dumpmon2 (void)
 Command line option: dump abilities. More...
 
static void set_dumpmon3 (void)
 Command line option: dump artifacts. More...
 
static void set_dumpmon4 (void)
 Command line option: dump spells. More...
 
static void set_dumpmon5 (void)
 Command line option: ? More...
 
static void set_dumpmon6 (void)
 Command line option: dump races. More...
 
static void set_dumpmon7 (void)
 Command line option: dump alchemy. More...
 
static void set_dumpmon8 (void)
 Command line option: dump gods. More...
 
static void set_dumpmon9 (void)
 Command line option: dump alchemy costs. More...
 
static void set_dumpmont (const char *name)
 Command line option: dump monster treasures. More...
 
static void set_enable_module (const char *name)
 Enable a module. More...
 
static void set_ignore_assets_errors ()
 Command line option: ignore assets errors. More...
 
static void set_localdir (const char *path)
 Command line option: set local path. More...
 
static void set_logfile (char *val)
 Command line option: set logfile name. More...
 
static void set_mapdir (const char *path)
 Command line option: set map path. More...
 
static void set_mondebug (void)
 Command line option: monster debug flag. More...
 
static void set_playerdir (const char *path)
 Command line option: set player path. More...
 
static void set_regions (const char *path)
 Command line option: set regions file name. More...
 
static void set_templatedir (const char *path)
 Command line option: set template path. More...
 
static void set_tmpdir (const char *path)
 Command line option: set temporary file path. More...
 
static void set_uniquedir (const char *path)
 Command line option: set unique path. More...
 
static void signal_shutdown (int signum_unused)
 Signal handler that begins a normal server shutdown. More...
 
static void unset_debug (void)
 Command line option: unset debug flag. More...
 

Variables

static module_information modules []
 All built modules. More...
 
static struct Command_Line_Options options []
 Actual valid command line options. More...
 
ServerSettings serverSettings
 
static int should_exit = 0
 If set after command line argument parsing, then the server will exit. More...
 

Detailed Description

Server initialization, settings, and command-line handling.

Definition in file init.cpp.

Typedef Documentation

◆ cmdlinefunc_args0

typedef void(* cmdlinefunc_args0) (void)

Typedefs used when calling option handlers.

Definition at line 358 of file init.cpp.

◆ cmdlinefunc_args1

typedef void(* cmdlinefunc_args1) (const char *arg1)

Definition at line 359 of file init.cpp.

◆ cmdlinefunc_args2

typedef void(* cmdlinefunc_args2) (const char *arg1, const char *arg2)

Definition at line 360 of file init.cpp.

Function Documentation

◆ add_server_collect_hooks()

void add_server_collect_hooks ( )

Definition at line 1086 of file init.cpp.

References assets_add_collector_hook(), load_materials(), and load_races().

Referenced by init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ call_version()

static void call_version ( void  )
static

Command line option: show version.

Definition at line 113 of file init.cpp.

References FULL_VERSION.

◆ close_modules()

void close_modules ( )

Clean up all modules which are not disabled.

Definition at line 82 of file init.cpp.

References module_information::close, module_information::description, module_information::enabled, llevInfo, LOG(), modules, and module_information::name.

Referenced by cleanup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ do_module()

static void do_module ( const char *  name,
bool  enabled 
)
static

Change the 'enabled' flag of a module.

Parameters
namemodule's name, or 'All'.
enablednew value of the 'enabled' flag.
Note
will fatal() if the module name is invalid.

Definition at line 302 of file init.cpp.

References module_information::enabled, fatal(), llevError, LOG(), modules, name, module_information::name, and SEE_LAST_ERROR.

Referenced by set_disable_module(), and set_enable_module().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_materials()

static void free_materials ( void  )
static

Frees all memory allocated to materials.

Definition at line 585 of file init.cpp.

References materials.

Referenced by free_server().

+ Here is the caller graph for this function:

◆ free_server()

void free_server ( void  )

Frees all memory allocated around here:

  • materials
  • races

Definition at line 1138 of file init.cpp.

References ServerSettings::disabled_plugins, free_materials(), free_quest(), free_races(), and serverSettings.

Referenced by cleanup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_empty_mat()

static materialtype_t* get_empty_mat ( void  )
static

Creates an empty materialtype_t structure.

Returns
new blanked structure.
Note
will fatal() instead of returning NULL.

Definition at line 510 of file init.cpp.

References materialtype_t::description, fatal(), materialtype_t::mod, materialtype_t::name, NROFATTACKS, OUT_OF_MEMORY, and materialtype_t::save.

Referenced by load_materials().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ help()

static void help ( void  )
static

Display the command line options and exits.

Definition at line 1149 of file init.cpp.

◆ init()

void init ( int  argc,
char **  argv 
)

This is the main server initialization function.

Called only once, when starting the program.

Parameters
argcargument count.
argvarguments on the command line.

Definition at line 1098 of file init.cpp.

References accounts_load(), add_server_collect_hooks(), cftimer_init(), commands_init(), FULL_VERSION, hiscore_init(), init_beforeplay(), init_library(), init_modules(), init_ob_methods(), init_server(), init_signals(), init_skills(), init_startup(), llevInfo, load_settings(), LOG(), logfile, metaserver2_init(), parse_args(), read_map_log(), reset_sleep(), SRANDOM, and time.

Referenced by server_main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init_beforeplay()

static void init_beforeplay ( void  )
static

Called before the server starts listening to connections, processes various dump-related options.

Definition at line 1209 of file init.cpp.

References assets_finish_archetypes_for_play(), cleanup(), dump_abilities(), dump_alchemy(), dump_alchemy_costs(), dump_artifacts(), dump_gods(), dump_monster_treasure(), dump_races(), dump_spells(), Settings::dumparg, Settings::dumpvalues, finish_races(), init_archetype_pointers(), init_gods(), init_readable(), print_monsters(), and settings.

Referenced by init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init_modules()

void init_modules ( )

Init all modules which are not disabled.

Definition at line 66 of file init.cpp.

References module_information::description, module_information::enabled, module_information::init, llevInfo, LOG(), modules, module_information::name, serverSettings, and settings.

Referenced by init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init_signals()

void init_signals ( void  )

Setup our signal handlers.

Definition at line 1325 of file init.cpp.

References rec_sighup(), and signal_shutdown().

Referenced by command_unloadplugin(), and init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init_startup()

static void init_startup ( void  )
static

Checks if starting the server is allowed.

Todo:
describe forbid_play() and such restrictions.

Definition at line 1273 of file init.cpp.

References buf, Settings::confdir, forbid_play(), llevError, LOG(), MAX_BUF, settings, and SHUTDOWN_FILE.

Referenced by init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ list_modules()

static void list_modules ( )
static

List all modules, then exit.

Definition at line 96 of file init.cpp.

References description, llevInfo, LOG(), modules, name, module_information::name, and should_exit.

+ Here is the call graph for this function:

◆ load_materials()

static void load_materials ( BufferReader reader,
const char *  filename 
)
static

Loads the materials.

Todo:
describe materials and such.

Definition at line 530 of file init.cpp.

References add_refcount(), add_string(), buf, bufferreader_next_line(), materialtype_t::description, FREE_AND_COPY_IF, get_empty_mat(), llevDebug, LOG(), materialtype_t::material, materials, materialtype_t::mod, materialtype_t::name, NROFATTACKS, and materialtype_t::save.

Referenced by add_server_collect_hooks().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ load_settings()

static void load_settings ( void  )
static

This loads the settings file.

There could be debate whether this should be here or in the common directory - but since only the server needs this information, having it here probably makes more sense.

Definition at line 597 of file init.cpp.

References Settings::account_block_create, Settings::account_trusted_host, Settings::allow_broken_converters, Settings::allow_denied_spells_writing, Settings::always_show_hp, Settings::armor_max_enchant, Settings::armor_speed_improvement, Settings::armor_speed_linear, Settings::armor_weight_linear, Settings::armor_weight_reduction, Settings::balanced_stat_loss, buf, Settings::casting_time, Settings::confdir, Settings::create_home_portals, Settings::crypt_mode, Settings::death_penalty_level, Settings::death_penalty_ratio, FALSE, Settings::fastclock, Settings::ignore_plugin_compatibility, Settings::item_power_factor, llevError, LOG(), Settings::log_timestamp, Settings::log_timestamp_format, MAX_BUF, MAX_NAME, Settings::max_stat, Settings::meta_comment, Settings::meta_host, Settings::meta_on, Settings::meta_port, Settings::meta_server, Settings::min_name, motd, Settings::motd, Settings::no_player_stealing, Settings::not_permadeth, NUM_STATS, Settings::permanent_exp_ratio, Settings::personalized_blessings, Settings::pk_luck_penalty, Settings::pk_max_experience, Settings::pk_max_experience_percent, Settings::real_wiz, Settings::recycle_tmp_maps, Settings::resurrection, Settings::roll_stat_points, safe_strncpy, Settings::search_items, set_csport(), Settings::set_friendly_fire, Settings::set_title, settings, Settings::simple_exp, Settings::special_break_map, Settings::spell_encumbrance, Settings::spell_failure_effects, Settings::spellpoint_level_depend, Settings::starting_stat_max, Settings::starting_stat_min, Settings::starting_stat_points, Settings::stat_file, Settings::stat_loss_on_death, strcasecmp(), strdup_local, TRUE, Settings::who_format, Settings::who_wiz_format, Settings::worldmapstartx, Settings::worldmapstarty, Settings::worldmaptilesizex, Settings::worldmaptilesizey, Settings::worldmaptilesx, and Settings::worldmaptilesy.

Referenced by init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_args()

static void parse_args ( int  argc,
char *  argv[],
int  pass 
)
static

Parse command line arguments.

Note since this may be called before the library has been set up, we don't use any of crossfires built in logging functions.

Parameters
argclength of argv.
argvarguments.
passinitialization pass arguments to use.
Todo:
describe pass.

Definition at line 458 of file init.cpp.

References cleanup(), Command_Line_Options::cmd_option, Command_Line_Options::func, Command_Line_Options::num_args, options, Command_Line_Options::pass, and should_exit.

Referenced by init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rec_sighup()

static void rec_sighup ( int  i)
static

SIGHUP handler.

SIGHUP handlers on daemons typically make them reread the config files and reinitialize itself. This behaviour is better left for an explicit shutdown and restart with Crossfire, as there is just too much persistent runtime state. However, another function of SIGHUP handlers is to reopen the log file for logrotate's benefit. We can do that here.

Parameters
iunused.

Definition at line 1313 of file init.cpp.

References logfile, and reopen_logfile.

Referenced by init_signals().

+ Here is the caller graph for this function:

◆ server_dump_animations()

static void server_dump_animations ( void  )
static

Dump all animations, then exit.

Definition at line 335 of file init.cpp.

References cleanup(), and dump_animations().

+ Here is the call graph for this function:

◆ server_dump_bonuses()

static void server_dump_bonuses ( )
static

Dump all bonuses (from the stat_bonus file) then exit.

Definition at line 351 of file init.cpp.

References cleanup(), and dump_stat_bonuses().

+ Here is the call graph for this function:

◆ server_dump_faces()

static void server_dump_faces ( void  )
static

Dump all faces, then exit.

Definition at line 343 of file init.cpp.

References cleanup(), and dump_faces().

+ Here is the call graph for this function:

◆ server_pack_assets()

static void server_pack_assets ( const char *  assets,
const char *  filename 
)
static

Definition at line 266 of file init.cpp.

References assets_pack(), and should_exit.

+ Here is the call graph for this function:

◆ set_confdir()

static void set_confdir ( const char *  path)
static

Command line option: set configuration path.

Parameters
pathnew path.

Definition at line 199 of file init.cpp.

References Settings::confdir, and settings.

◆ set_csport()

static void set_csport ( const char *  val)
static

Change the server's port.

Will exit() if invalid value.

Parameters
valport to use. Must be a valid one, between 1 and 65535 inclusive.

Definition at line 279 of file init.cpp.

References Settings::csport, llevError, LOG(), and settings.

Referenced by load_settings().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_datadir()

static void set_datadir ( const char *  path)
static

Command line option: set data path.

Parameters
pathnew path.

Definition at line 191 of file init.cpp.

References Settings::datadir, and settings.

◆ set_debug()

static void set_debug ( void  )
static

Command line option: debug flag.

Definition at line 119 of file init.cpp.

References Settings::debug, llevDebug, and settings.

◆ set_disable_module()

static void set_disable_module ( const char *  name)
static

Disable a module.

Parameters
namemodule's name, or 'All'.

Definition at line 320 of file init.cpp.

References do_module(), and name.

+ Here is the call graph for this function:

◆ set_disable_plugin()

static void set_disable_plugin ( const char *  name)
static

Disable a plugin.

Parameters
nameplugin's name, without extension.

Definition at line 292 of file init.cpp.

References ServerSettings::disabled_plugins, name, and serverSettings.

◆ set_dumpmon1()

static void set_dumpmon1 ( void  )
static

Command line option: dump monsters.

Definition at line 134 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmon2()

static void set_dumpmon2 ( void  )
static

Command line option: dump abilities.

Definition at line 139 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmon3()

static void set_dumpmon3 ( void  )
static

Command line option: dump artifacts.

Definition at line 144 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmon4()

static void set_dumpmon4 ( void  )
static

Command line option: dump spells.

Definition at line 149 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmon5()

static void set_dumpmon5 ( void  )
static

Command line option: ?

Definition at line 154 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmon6()

static void set_dumpmon6 ( void  )
static

Command line option: dump races.

Definition at line 159 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmon7()

static void set_dumpmon7 ( void  )
static

Command line option: dump alchemy.

Definition at line 164 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmon8()

static void set_dumpmon8 ( void  )
static

Command line option: dump gods.

Definition at line 169 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmon9()

static void set_dumpmon9 ( void  )
static

Command line option: dump alchemy costs.

Definition at line 174 of file init.cpp.

References Settings::dumpvalues, and settings.

◆ set_dumpmont()

static void set_dumpmont ( const char *  name)
static

Command line option: dump monster treasures.

Parameters
namemonster's name to dump treasure for.

Definition at line 182 of file init.cpp.

References Settings::dumparg, Settings::dumpvalues, name, and settings.

◆ set_enable_module()

static void set_enable_module ( const char *  name)
static

Enable a module.

Parameters
namemodule's name, or 'All'.

Definition at line 328 of file init.cpp.

References do_module(), and name.

+ Here is the call graph for this function:

◆ set_ignore_assets_errors()

static void set_ignore_assets_errors ( )
static

Command line option: ignore assets errors.

Definition at line 262 of file init.cpp.

References Settings::ignore_assets_errors, and settings.

◆ set_localdir()

static void set_localdir ( const char *  path)
static

Command line option: set local path.

Parameters
pathnew path.

Definition at line 207 of file init.cpp.

References Settings::localdir, and settings.

◆ set_logfile()

static void set_logfile ( char *  val)
static

Command line option: set logfile name.

Parameters
valnew name.

Definition at line 108 of file init.cpp.

References Settings::logfilename, and settings.

◆ set_mapdir()

static void set_mapdir ( const char *  path)
static

Command line option: set map path.

Parameters
pathnew path.

Definition at line 215 of file init.cpp.

References Settings::mapdir, and settings.

◆ set_mondebug()

static void set_mondebug ( void  )
static

Command line option: monster debug flag.

Definition at line 129 of file init.cpp.

References Settings::debug, llevMonster, and settings.

◆ set_playerdir()

static void set_playerdir ( const char *  path)
static

Command line option: set player path.

Parameters
pathnew path.

Definition at line 247 of file init.cpp.

References Settings::playerdir, and settings.

◆ set_regions()

static void set_regions ( const char *  path)
static

Command line option: set regions file name.

Parameters
pathnew name.

Definition at line 223 of file init.cpp.

References Settings::regions, and settings.

◆ set_templatedir()

static void set_templatedir ( const char *  path)
static

Command line option: set template path.

Parameters
pathnew path.

Definition at line 239 of file init.cpp.

References settings, and Settings::templatedir.

◆ set_tmpdir()

static void set_tmpdir ( const char *  path)
static

Command line option: set temporary file path.

Parameters
pathnew path.

Definition at line 255 of file init.cpp.

References settings, and Settings::tmpdir.

◆ set_uniquedir()

static void set_uniquedir ( const char *  path)
static

Command line option: set unique path.

Parameters
pathnew path.

Definition at line 231 of file init.cpp.

References settings, and Settings::uniquedir.

◆ signal_shutdown()

static void signal_shutdown ( int  signum_unused)
static

Signal handler that begins a normal server shutdown.

Definition at line 1296 of file init.cpp.

References shutdown_flag.

Referenced by init_signals().

+ Here is the caller graph for this function:

◆ unset_debug()

static void unset_debug ( void  )
static

Command line option: unset debug flag.

Definition at line 124 of file init.cpp.

References Settings::debug, llevInfo, and settings.

Variable Documentation

◆ modules

module_information modules[]
static
Initial value:
= {
{ "citybell", "Ring bells every hour for defined temples", true, cfcitybell_init, cfcitybell_close },
{ "citylife", "Add NPCs in towns", true, citylife_init, citylife_close },
{ "rhg", "Add random maps to exits in towns", false, random_house_generator_init, random_house_generator_close },
{ NULL, NULL, false, NULL, NULL }
}

All built modules.

Definition at line 56 of file init.cpp.

Referenced by close_modules(), do_module(), init_modules(), and list_modules().

◆ options

struct Command_Line_Options options[]
static

Actual valid command line options.

The way this system works is pretty simple - parse_args takes the options passed to the program and a pass number. If an option matches both in name and in pass (and we have enough options), we call the associated function. This makes writing a multi pass system very easy, and it is very easy to add in new options.

Definition at line 386 of file init.cpp.

Referenced by find_player_options(), make_doors(), and parse_args().

◆ serverSettings

◆ should_exit

int should_exit = 0
static

If set after command line argument parsing, then the server will exit.

Definition at line 45 of file init.cpp.

Referenced by list_modules(), parse_args(), and server_pack_assets().

citylife_init
void citylife_init(Settings *settings, ServerSettings *serverSettings)
Definition: citylife.cpp:426
random_house_generator_init
void random_house_generator_init(Settings *settings, ServerSettings *serverSettings)
Module initialisation.
Definition: random_house_generator.cpp:211
random_house_generator_close
void random_house_generator_close()
Close the module.
Definition: random_house_generator.cpp:221
cfcitybell_init
void cfcitybell_init(Settings *settings, ServerSettings *serverSettings)
Citybells module initialisation.
Definition: cfcitybell.cpp:153
cfcitybell_close
void cfcitybell_close()
Definition: cfcitybell.cpp:165
citylife_close
void citylife_close()
Definition: citylife.cpp:437