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 "../saveload/saveload.h"
24#include "../window_func.h"
30# include <sys/types.h>
36static void DedicatedSignalHandler(
int sig)
40 signal(sig, DedicatedSignalHandler);
49# include "../os/windows/win32.h"
51static HANDLE _hInputReady, _hWaitForInputHandling;
52static HANDLE _hThread;
53static std::string _win_console_thread_buffer;
56static void WINAPI CheckForConsoleInput()
61 std::getline(std::cin, _win_console_thread_buffer);
64 SignalObjectAndWait(_hInputReady, _hWaitForInputHandling, INFINITE, FALSE);
68static void CreateWindowsConsoleThread()
72 _hInputReady = CreateEvent(
nullptr,
false,
false,
nullptr);
73 _hWaitForInputHandling = CreateEvent(
nullptr,
false,
false,
nullptr);
74 if (_hInputReady ==
nullptr || _hWaitForInputHandling ==
nullptr) UserError(
"Cannot create console event!");
76 _hThread = CreateThread(
nullptr, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput,
nullptr, 0, &dwThreadId);
77 if (_hThread ==
nullptr) UserError(
"Cannot create console thread!");
79 Debug(driver, 2,
"Windows console thread started");
82static void CloseWindowsConsoleThread()
84 CloseHandle(_hThread);
85 CloseHandle(_hInputReady);
86 CloseHandle(_hWaitForInputHandling);
87 Debug(driver, 2,
"Windows console thread shut down");
92#include "../safeguards.h"
95static std::unique_ptr<uint8_t[]> _dedicated_video_mem;
110 if (bpp != 0) _dedicated_video_mem = std::make_unique<uint8_t[]>(
static_cast<size_t>(
_cur_resolution.width) *
_cur_resolution.height * (bpp / 8));
114 _screen.dst_ptr = _dedicated_video_mem.get();
121 CreateWindowsConsoleThread();
122 SetConsoleTitle(L
"OpenTTD Dedicated Server");
127 _set_error_mode(_OUT_TO_STDERR);
128 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
129 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
132 Debug(driver, 1,
"Loading dedicated server");
139 CloseWindowsConsoleThread();
141 _dedicated_video_mem =
nullptr;
149static bool InputWaiting()
158 FD_SET(STDIN, &readfds);
161 return select(STDIN + 1, &readfds,
nullptr,
nullptr, &tv) > 0;
166static bool InputWaiting()
168 return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0;
173static void DedicatedHandleKeyInput()
175 if (!InputWaiting())
return;
177 if (_exit_game)
return;
179 std::string input_line;
181 if (!std::getline(std::cin, input_line))
return;
184 std::swap(input_line, _win_console_thread_buffer);
185 SetEvent(_hWaitForInputHandling);
189 auto p = input_line.find_last_not_of(
"\r\n");
190 if (p != std::string::npos) p++;
198 signal(SIGTERM, DedicatedSignalHandler);
199 signal(SIGINT, DedicatedSignalHandler);
200 signal(SIGQUIT, DedicatedSignalHandler);
213 this->is_game_threaded =
false;
217 while (!_exit_game) {
218 if (!_dedicated_forks) DedicatedHandleKeyInput();
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
virtual void PostResize()
Post resize event.
Factory for the dedicated server video driver.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
void MainLoop() override
Perform the actual drawing.
void Stop() override
Stop this driver.
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
void Tick()
Give the video-driver a tick.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void DrainCommandQueue()
Execute all queued commands.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
CompanyID _current_company
Company currently doing an action.
static constexpr CompanyID COMPANY_SPECTATOR
The client is spectating.
void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
Execute a given command passed to us.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Base for the dedicated video driver.
Dimension _cur_resolution
The current resolution.
SaveLoadOperation
Operation performed on the file.
DetailedFileType
Kinds of files in each AbstractFileType.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
static const uint32_t GENERATE_NEW_SEED
Create a new random seed.
void StartNewGameWithoutGUI(uint32_t seed)
Start a normal game without the GUI.
SwitchMode _switch_mode
The next mainloop command.
bool _is_network_server
Does this client wants to be a network-server?
bool _network_dedicated
are we a dedicated server?
GameMode
Mode which defines the state of the game.
@ 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)
ClientSettings _settings_client
The current settings for this game.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
std::vector< std::string > StringList
Type for a list of strings.
GUISettings gui
settings related to the GUI
bool autosave_on_exit
save an autosave when you quit the game, but do not ask "Do you really want to quit?...
Interface for filtering a savegame till it is loaded.
void SetCurrentThreadName(const std::string &thread_name)
Name the thread this function is called on for the debugger.