OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
10 #include "../stdafx.h"
12 #include "../gfx_func.h"
13 #include "../error_func.h"
14 #include "../network/network.h"
15 #include "../network/network_internal.h"
16 #include "../console_func.h"
17 #include "../genworld.h"
18 #include "../fileio_type.h"
20 #include "../blitter/factory.hpp"
21 #include "../company_func.h"
22 #include "../core/random_func.hpp"
23 #include "../saveload/saveload.h"
24 #include "../thread.h"
25 #include "../window_func.h"
30 # include <sys/time.h>
31 # include <sys/types.h>
37 static void DedicatedSignalHandler(
int sig)
41 signal(sig, DedicatedSignalHandler);
50 # include "../os/windows/win32.h"
52 static HANDLE _hInputReady, _hWaitForInputHandling;
53 static HANDLE _hThread;
54 static std::string _win_console_thread_buffer;
57 static void WINAPI CheckForConsoleInput()
59 SetCurrentThreadName(
"ottd:win-console");
62 std::getline(std::cin, _win_console_thread_buffer);
65 SignalObjectAndWait(_hInputReady, _hWaitForInputHandling, INFINITE, FALSE);
69 static void CreateWindowsConsoleThread()
73 _hInputReady = CreateEvent(
nullptr,
false,
false,
nullptr);
74 _hWaitForInputHandling = CreateEvent(
nullptr,
false,
false,
nullptr);
75 if (_hInputReady ==
nullptr || _hWaitForInputHandling ==
nullptr) UserError(
"Cannot create console event!");
77 _hThread = CreateThread(
nullptr, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput,
nullptr, 0, &dwThreadId);
78 if (_hThread ==
nullptr) UserError(
"Cannot create console thread!");
80 Debug(driver, 2,
"Windows console thread started");
83 static void CloseWindowsConsoleThread()
85 CloseHandle(_hThread);
86 CloseHandle(_hInputReady);
87 CloseHandle(_hWaitForInputHandling);
88 Debug(driver, 2,
"Windows console thread shut down");
93 #include "../safeguards.h"
96 static void *_dedicated_video_mem;
99 bool _dedicated_forks;
115 _screen.dst_ptr = _dedicated_video_mem;
122 CreateWindowsConsoleThread();
123 SetConsoleTitle(L
"OpenTTD Dedicated Server");
128 _set_error_mode(_OUT_TO_STDERR);
129 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
130 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
133 Debug(driver, 1,
"Loading dedicated server");
140 CloseWindowsConsoleThread();
142 free(_dedicated_video_mem);
150 static bool InputWaiting()
159 FD_SET(STDIN, &readfds);
162 return select(STDIN + 1, &readfds,
nullptr,
nullptr, &tv) > 0;
167 static bool InputWaiting()
169 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
174 static void DedicatedHandleKeyInput()
176 if (!InputWaiting())
return;
178 if (_exit_game)
return;
180 std::string input_line;
182 if (!std::getline(std::cin, input_line))
return;
185 std::swap(input_line, _win_console_thread_buffer);
186 SetEvent(_hWaitForInputHandling);
190 auto p = input_line.find_last_not_of(
"\r\n");
191 if (p != std::string::npos) p++;
199 signal(SIGTERM, DedicatedSignalHandler);
200 signal(SIGINT, DedicatedSignalHandler);
201 signal(SIGQUIT, DedicatedSignalHandler);
214 this->is_game_threaded =
false;
218 while (!_exit_game) {
219 if (!_dedicated_forks) DedicatedHandleKeyInput();
void Tick()
Give the video-driver a tick.
@ SM_START_HEIGHTMAP
Load a heightmap and start a new game from it.
@ SM_LOAD_GAME
Load game, Play Scenario.
void DoExitSave()
Do a save when exiting the game (_settings_client.gui.autosave_on_exit)
bool autosave_on_exit
save an autosave when you quit the game, but do not ask "Do you really want to quit?...
SaveLoadOperation
Operation performed on the file.
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
ClientSettings _settings_client
The current settings for this game.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
void StartNewGameWithoutGUI(uint32_t seed)
Start a normal game without the GUI.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
void IConsoleCmdExec(const std::string &command_string, const uint recurse_count)
Execute a given command passed to us.
virtual void PostResize()
Post resize event.
Factory for the dedicated server video driver.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
std::vector< std::string > StringList
Type for a list of strings.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
bool _network_dedicated
are we a dedicated server?
GameMode
Mode which defines the state of the game.
static const uint32_t GENERATE_NEW_SEED
Create a new random seed.
bool SafeLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, std::shared_ptr< LoadFilter > lf=nullptr)
Load the specified savegame but on error do different things.
SwitchMode _switch_mode
The next mainloop command.
CompanyID _current_company
Company currently doing an action.
DetailedFileType
Kinds of files in each AbstractFileType.
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
void DrainCommandQueue()
Execute all queued commands.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Interface for filtering a savegame till it is loaded.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
@ COMPANY_SPECTATOR
The client is spectating.
void MainLoop() override
Perform the actual drawing.
void Stop() override
Stop this driver.
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Dimension _cur_resolution
The current resolution.
bool _is_network_server
Does this client wants to be a network-server?
GUISettings gui
settings related to the GUI