Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
sysdep.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * various system-specific function implementations
25 */
26
27#ifndef INCLUDED_SYSDEP
28#define INCLUDED_SYSDEP
29
30#ifdef _MSC_VER
31# pragma warning(disable:4091) // hides previous local declaration
32#endif
33
34#include "lib/debug.h" // ErrorReactionInternal
35#include "lib/os_path.h"
36#include "lib/status.h"
37#include "lib/sysdep/os.h"
38
39#include <cstdio>
40#include <string>
41
42
43//
44// output
45//
46
47/**
48 * display a message.
49 *
50 * @param caption title message
51 * @param msg message contents
52 *
53 * implemented as a MessageBox on Win32 and printf on Unix.
54 * called from debug_DisplayMessage.
55 **/
56extern void sys_display_msg(const wchar_t* caption, const wchar_t* msg);
57
58/**
59 * show the error dialog.
60 *
61 * @param text to display (practically unlimited length)
62 * @param flags: see DebugDisplayErrorFlags.
63 * @return ErrorReactionInternal (except ERI_EXIT, which is acted on immediately)
64 *
65 * called from debug_DisplayError unless overridden by means of
66 * ah_display_error.
67 **/
68extern ErrorReactionInternal sys_display_error(const wchar_t* text, size_t flags);
69
70
71//
72// misc
73//
74
75/**
76 * @return whether a debugger is attached to the process
77 * (if so, it is safe to use debug_break; otherwise, that would
78 * raise an exception)
79 **/
81
82/**
83 * @return a wide string conversion of the platform's encoding of main's argv.
84 *
85 * (NB: wseh.cpp defines a wmain that converts argv to UTF-8 and calls main())
86 **/
87std::wstring sys_WideFromArgv(const char* argv_i);
88
89/**
90 * describe the current OS error state.
91 *
92 * @param err: if not 0, use that as the error code to translate; otherwise,
93 * uses GetLastError or similar.
94 * @param buf output buffer
95 * @param max_chars
96 *
97 * rationale: it is expected to be rare that OS return/error codes are
98 * actually seen by user code, but we leave the possibility open.
99 **/
100extern Status sys_StatusDescription(int err, wchar_t* buf, size_t max_chars);
101
102/**
103 * determine filename of the module to whom an address belongs.
104 *
105 * @param addr
106 * @param pathname Full path to module (unchanged unless INFO::OK is returned).
107 * @return Status
108 *
109 * note: this is useful for handling exceptions in other modules.
110 **/
111Status sys_get_module_filename(void* addr, OsPath& pathname);
112
113/**
114 * @return full pathname of the current executable.
115 *
116 * this is useful for determining installation directory, e.g. for VFS.
117 **/
119
120/**
121 * Get the current user's login name.
122 *
123 * @return login name, or empty string on error
124 */
125extern std::wstring sys_get_user_name();
126
127/**
128 * Have the user choose a directory via OS dialog.
129 *
130 * @param path Path's input value determines the starting directory for
131 * faster browsing. if INFO::OK is returned, it receives
132 * chosen directory path.
133 **/
134extern Status sys_pick_directory(OsPath& path);
135
136/**
137 * Open the user's default web browser to the given URL.
138 **/
139extern Status sys_open_url(const std::string& url);
140
141/**
142 * return the largest sector size [bytes] of any storage medium
143 * (HD, optical, etc.) in the system.
144 *
145 * this may be a bit slow to determine (iterates over all drives),
146 * but caches the result so subsequent calls are free.
147 * (caveat: device changes won't be noticed during this program run)
148 *
149 * sector size is relevant because Windows aio requires all IO
150 * buffers, offsets and lengths to be a multiple of it. this requirement
151 * is also carried over into the vfs / file.cpp interfaces for efficiency
152 * (avoids the need for copying to/from align buffers).
153 *
154 * waio uses the sector size to (in some cases) align IOs if
155 * they aren't already, but it's also needed by user code when
156 * aligning their buffers to meet the requirements.
157 *
158 * the largest size is used so that we can read from any drive. while this
159 * is a bit wasteful (more padding) and requires iterating over all drives,
160 * it is the only safe way: this may be called before we know which
161 * drives will be needed, and hardlinks may confuse things.
162 **/
163extern size_t sys_max_sector_size();
164
165/**
166 * generate high-quality random bytes.
167 *
168 * this should only be used with small numbers of bytes, to avoid
169 * hogging the system's entropy.
170 **/
171Status sys_generate_random_bytes(std::uint8_t* buf, size_t count);
172
173/**
174 * get the proxy address for accessing the given HTTP URL.
175 *
176 * this may be very slow (tens of seconds).
177 *
178 * @return INFO::OK on success; INFO::SKIPPED if no proxy found.
179 **/
180Status sys_get_proxy_config(const std::wstring& url, std::wstring& proxy);
181
182/**
183 * open a file like with fopen (but taking an OsPath argument).
184 */
185FILE* sys_OpenFile(const OsPath& pathname, const char* mode);
186
187/**
188 * directory separation character
189 **/
190#if OS_WIN
191# define SYS_DIR_SEP '\\'
192#else
193# define SYS_DIR_SEP '/'
194#endif
195
196#endif // #ifndef INCLUDED_SYSDEP
Definition: path.h:84
ErrorReactionInternal
all choices offered by the error dialog.
Definition: debug.h:141
std::int64_t Status
Error handling system.
Definition: status.h:174
bool sys_IsDebuggerPresent()
Definition: unix.cpp:52
Status sys_get_proxy_config(const std::wstring &url, std::wstring &proxy)
get the proxy address for accessing the given HTTP URL.
Definition: unix.cpp:348
Status sys_generate_random_bytes(std::uint8_t *buf, size_t count)
generate high-quality random bytes.
Definition: unix.cpp:325
OsPath sys_ExecutablePathname()
Get the path to the executable.
Definition: bsd.cpp:41
std::wstring sys_WideFromArgv(const char *argv_i)
Definition: unix.cpp:57
Status sys_pick_directory(OsPath &path)
Have the user choose a directory via OS dialog.
Definition: wsysdep.cpp:459
Status sys_open_url(const std::string &url)
Open the user's default web browser to the given URL.
Definition: unix.cpp:353
void sys_display_msg(const wchar_t *caption, const wchar_t *msg)
display a message.
Definition: unix.cpp:66
Status sys_StatusDescription(int err, wchar_t *buf, size_t max_chars)
describe the current OS error state.
Definition: unix.cpp:282
ErrorReactionInternal sys_display_error(const wchar_t *text, size_t flags)
show the error dialog.
Definition: unix.cpp:213
Status sys_get_module_filename(void *addr, OsPath &pathname)
determine filename of the module to whom an address belongs.
Definition: wsysdep.cpp:415
FILE * sys_OpenFile(const OsPath &pathname, const char *mode)
open a file like with fopen (but taking an OsPath argument).
Definition: unix.cpp:383
size_t sys_max_sector_size()
return the largest sector size [bytes] of any storage medium (HD, optical, etc.) in the system.
Definition: unix.cpp:293
std::wstring sys_get_user_name()
Get the current user's login name.
Definition: unix.cpp:302