Commit graph

147 commits

Author SHA1 Message Date
Robert Müller a0df1ebfc0 Fix updated not being set correctly when deleting shell class 2023-01-02 15:29:43 +01:00
Robert Müller f3eeddf8bd Fix some registry keys not being closed if values are unchanged 2023-01-02 15:29:42 +01:00
bors[bot] b252f2e344
Merge #6199
6199: Register protocol and file extensions on client launch on Windows r=def- a=Robyt3

When launching the client on Windows, associate the protocol `ddnet` and the file extensions `.map` and `.demo` with the client executable.

See #6072.

## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
2023-01-02 12:58:15 +00:00
def c6339055b7 Address comment from https://github.com/ddnet/ddnet/pull/6206/
@Chairn
2022-12-31 00:16:30 +01:00
Chairn 2847d0f6d0 Added some test cases that were failing under new version 2022-12-30 01:11:57 +01:00
Chairn ed58668931 Remove possible overflow in str_comp_filenames (fixes #6204) 2022-12-30 00:29:19 +01:00
Robert Müller 3b73107100 Add shell_unregister to delete protocol/file extension handlers
This function removes the registry keys that are created with `shell_register_protocol` and `shell_register_extension`.

According to the Microsoft documentation, only the keys for the program IDs should be deleted.
The keys that associate the file extensions with the program IDs should be kept, as Windows will automatically ignore the value if the program ID does not exist.

See: https://learn.microsoft.com/en-us/windows/win32/shell/fa-file-types#deleting-registry-information-during-uninstallation
2022-12-29 18:03:08 +01:00
Robert Müller 8a3a8974d0 Add shell_update to notify system to update shell
The `shell_update` function notifies the system when the shell needs to be updated due to a changed protocol or file association.

An output parameter is added to the `shell_register_protocol` and `shell_register_extension` functions, to determine whether the shell needs to be updated after calling the functions.
We only check whether the application path and program association were changed, instead of checking whether any key or value was changed, as this reduces the amount of necessary checks and we assume that the other values are not externally changed.

Because updating the shell is a potentially expensive operation, this should only be done when necessary and only once after registering all protocols and extensions.
2022-12-29 18:02:48 +01:00
Robert Müller 4e130a29e4 Add shell_register_extension to register file extension on Windows
This function register a file extension on Windows by creating the necessary registry keys and values.

The file extension is only registered for the current user in the registry key `HKEY_CURRENT_USER\SOFTWARE\Classes`, so admin privileges are not required.

Windows does not allow changing the default file extension handler programatically. When the user opens a file for which a new file extension handler exists, a dialog is shown that allows the user to change the default handler.
2022-12-29 18:01:42 +01:00
Robert Müller 7294ce5cf6 Add shell_register_protocol to register protocol handler on Windows
This function registers a protocol handler on Windows by creating the necessary registry keys and values.

The handler is only registered for the current user in the registry key `HKEY_CURRENT_USER\SOFTWARE\Classes`, so admin privileges are not required.
2022-12-29 18:01:40 +01:00
heinrich5991 042f892f40 Allow translations to reorder string substitutions
This is supported on Windows
(https://docs.microsoft.com/en-us/cpp/c-runtime-library/printf-p-positional-parameters)
and on POSIX, so basically everywhere.

Add some tests to verify that the target system does indeed support
these positional parameters.

(cherry picked from commit ddd2b93190)
2022-12-25 21:45:26 +01:00
bors[bot] cf6e89c319
Merge #6035
6035: Fix various issues reported by cppcheck static analyser r=def- a=Robyt3

After generating `compile_commands.json` with cmake, I ran [cppcheck](https://cppcheck.sourceforge.io/) like this:

```
cppcheck --project=compile_commands.json -DWIN64 --suppressions-list=cppcheck.supp --enable=all 2>cppcheck.log
```

With these suppressions in `cppcheck.supp`:

```
cstyleCast
useStlAlgorithm
unusedFunction
variableScope
noExplicitConstructor
useInitializationList
noConstructor
uninitMemberVar
uninitMemberVarPrivate
uninitDerivedMemberVar
uninitStructMember
uninitvar
shadowFunction
memleakOnRealloc
internalAstError
virtualCallInConstructor
unknownMacro
noOperatorEq
noCopyConstructor
```

Many of these occur too often or are false positives. 

Here is a list of all remaining non-suppressed issues reported by cppcheck: [cppcheck.log](https://github.com/ddnet/ddnet/files/9997663/cppcheck.log)

And here is a list of all remaining issues including the suppressed ones: [cppcheck_all.log](https://github.com/ddnet/ddnet/files/9997662/cppcheck_all.log)

I couldn't get cppcheck's command line argument to ignore the external folders to work correctly, so I manually removed those entries from the files.

## Checklist

- [ ] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
2022-12-05 20:00:07 +00:00
Zwelf a7bc593725 Make semaphore wait handle EINTR
from `man sem_wait`:

    EINTR  The call was interrupted by a signal handler; see signal(7).
2022-12-05 16:48:10 +01:00
Robert Müller 98706d79d4 Mark parameters as const when possible
According to cppchecker's `constParameter` error:

```
src\engine\gfx\image_manipulation.cpp:7:58: style: Parameter 'pSrc' can be declared as pointer to const [constParameter]
static void Dilate(int w, int h, int BPP, unsigned char *pSrc, unsigned char *pDest, unsigned char AlphaThreshold = TW_DILATE_ALPHA_THRESHOLD)
                                                         ^
src\engine\gfx\image_manipulation.cpp:58:67: style: Parameter 'pSrc' can be declared as pointer to const [constParameter]
static void CopyColorValues(int w, int h, int BPP, unsigned char *pSrc, unsigned char *pDest)
                                                                  ^

src\engine\shared\network_conn.cpp:241:42: style: Parameter 'Addr' can be declared as reference to const [constParameter]
void CNetConnection::DirectInit(NETADDR &Addr, SECURITY_TOKEN SecurityToken, SECURITY_TOKEN Token, bool Sixup)
                                         ^

src\base\system.cpp:4060:71: style: Parameter 'random' can be declared as pointer to const [constParameter]
void generate_password(char *buffer, unsigned length, unsigned short *random, unsigned random_length)
                                                                      ^

src\engine\client\backend\vulkan\backend_vulkan.cpp:263:38: style: Parameter 'AllocatedMemory' can be declared as reference to const [constParameter]
  void Free(SMemoryHeapQueueElement &AllocatedMemory)
                                     ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:1708:47: style: Parameter 'ImgExtent' can be declared as reference to const [constParameter]
 static size_t ImageMipLevelCount(VkExtent3D &ImgExtent)
                                              ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:2801:29: style: Parameter 'Image' can be declared as reference to const [constParameter]
 void ImageBarrier(VkImage &Image, size_t MipMapBase, size_t MipMapCount, size_t LayerBase, size_t LayerCount, VkFormat Format, VkImageLayout OldLayout, VkImageLayout NewLayout)
                            ^
src\engine\client\backend\vulkan\backend_vulkan.cpp:6495:46: style: Parameter 'ExecBuffer' can be declared as reference to const [constParameter]
 void Cmd_Clear(SRenderCommandExecuteBuffer &ExecBuffer, const CCommandBuffer::SCommand_Clear *pCommand)
                                             ^

src\game\client\components\skins.cpp:83:72: style: Parameter 'pImg' can be declared as pointer to const [constParameter]
static void CheckMetrics(CSkin::SSkinMetricVariable &Metrics, uint8_t *pImg, int ImgWidth, int ImgX, int ImgY, int CheckWidth, int CheckHeight)
                                                                       ^

src\game\client\prediction\entities\character.h:106:37: style: Parameter 'pNewInput' can be declared as pointer to const [constParameter]
 void SetInput(CNetObj_PlayerInput *pNewInput)
                                    ^

src\game\client\prediction\gameworld.cpp:245:106: style: Parameter 'pNotThis' can be declared as pointer to const [constParameter]
CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, CCharacter *pNotThis, int CollideWith, class CCharacter *pThisOnly)
                                                                                                         ^
src\game\client\prediction\gameworld.cpp:245:151: style: Parameter 'pThisOnly' can be declared as pointer to const [constParameter]
CCharacter *CGameWorld::IntersectCharacter(vec2 Pos0, vec2 Pos1, float Radius, vec2 &NewPos, CCharacter *pNotThis, int CollideWith, class CCharacter *pThisOnly)
                                                                                                                                                      ^
src\game\client\prediction\gameworld.cpp:283:116: style: Parameter 'pNotThis' can be declared as pointer to const [constParameter]
std::list<class CCharacter *> CGameWorld::IntersectedCharacters(vec2 Pos0, vec2 Pos1, float Radius, class CEntity *pNotThis)
                                                                                                                   ^

src\game\client\ui.cpp:522:180: style: Parameter 'pReadCursor' can be declared as pointer to const [constParameter]
void CUI::DoLabel(CUIElement::SUIElementRect &RectEl, const CUIRect *pRect, const char *pText, float Size, int Align, const SLabelProperties &LabelProps, int StrLen, CTextCursor *pReadCursor)
                                                                                                                                                                                   ^

src\game\client\ui_scrollregion.cpp:23:86: style: Parameter 'pParams' can be declared as pointer to const [constParameter]
void CScrollRegion::Begin(CUIRect *pClipRect, vec2 *pOutOffset, CScrollRegionParams *pParams)
                                                                                     ^

src\game\server\scoreworker.h:239:29: style: Parameter 'aTimeCp' can be declared as const array [constParameter]
 void Set(float Time, float aTimeCp[NUM_CHECKPOINTS])
                            ^

src\game\server\score.cpp:135:80: style: Parameter 'aTimeCp' can be declared as const array [constParameter]
void CScore::SaveScore(int ClientID, float Time, const char *pTimestamp, float aTimeCp[NUM_CHECKPOINTS], bool NotEligible)
                                                                               ^

src\game\server\teeinfo.cpp:40:57: style: Parameter 'pUseCustomColors' can be declared as pointer to const [constParameter]
CTeeInfo::CTeeInfo(const char *apSkinPartNames[6], int *pUseCustomColors, int *pSkinPartColors)
                                                        ^
src\game\server\teeinfo.cpp:40:80: style: Parameter 'pSkinPartColors' can be declared as pointer to const [constParameter]
CTeeInfo::CTeeInfo(const char *apSkinPartNames[6], int *pUseCustomColors, int *pSkinPartColors)
                                                                               ^
```
2022-11-29 23:32:31 +01:00
Robert Müller e134e4e488 Remove redundant conditions (always either true or false)
According to cppcheck's `knownConditionTrueFalse` error:

```
src\base\system.cpp:1776:11: style: The comparison 'bytes == 0' is always true. [knownConditionTrueFalse]
 if(bytes == 0 && sock->ipv4sock >= 0)
          ^
src\base\system.cpp:1742:14: note: 'bytes' is assigned value '0' here.
 int bytes = 0;
             ^
src\base\system.cpp:1776:11: note: The comparison 'bytes == 0' is always true.
 if(bytes == 0 && sock->ipv4sock >= 0)
          ^

src\game\editor\io.cpp:887:33: style: Condition 'pSoundsItem->m_Version>=1' is always true [knownConditionTrueFalse]
      if(pSoundsItem->m_Version >= 1)
                                ^
src\game\editor\io.cpp:874:33: note: Assuming that condition 'pSoundsItem->m_Version<1' is not redundant
      if(pSoundsItem->m_Version < 1 || pSoundsItem->m_Version > CMapItemLayerSounds::CURRENT_VERSION)
                                ^
src\game\editor\io.cpp:887:33: note: Condition 'pSoundsItem->m_Version>=1' is always true
      if(pSoundsItem->m_Version >= 1)
                                ^
src\game\editor\io.cpp:914:33: style: Condition 'pSoundsItem->m_Version>=1' is always true [knownConditionTrueFalse]
      if(pSoundsItem->m_Version >= 1)
                                ^
src\game\editor\io.cpp:901:33: note: Assuming that condition 'pSoundsItem->m_Version<1' is not redundant
      if(pSoundsItem->m_Version < 1 || pSoundsItem->m_Version > CMapItemLayerSounds::CURRENT_VERSION)
                                ^
src\game\editor\io.cpp:914:33: note: Condition 'pSoundsItem->m_Version>=1' is always true
      if(pSoundsItem->m_Version >= 1)
                                ^

src\engine\client\backend\opengl\backend_opengl.cpp:207:68: style: Condition '*pStr=='\0'' is always false [knownConditionTrueFalse]
   else if(LastWasNumber && (*pStr == '.' || *pStr == ' ' || *pStr == '\0'))
                                                                   ^

src\game\client\components\maplayers.cpp:851:110: style: Condition 'DoTextureCoords' is always true [knownConditionTrueFalse]
        mem_copy_special(pUploadData + sizeof(vec2), pTmpTileTexCoords, sizeof(vec3), vtmpTiles.size() * 4, (DoTextureCoords ? (sizeof(vec2)) : 0));
                                                                                                             ^
src\game\client\components\maplayers.cpp:849:11: note: Assuming that condition 'DoTextureCoords' is not redundant
       if(DoTextureCoords)
          ^

src\game\client\components\maplayers.cpp:851:110: note: Condition 'DoTextureCoords' is always true
        mem_copy_special(pUploadData + sizeof(vec2), pTmpTileTexCoords, sizeof(vec3), vtmpTiles.size() * 4, (DoTextureCoords ? (sizeof(vec2)) : 0));
                                                                                                             ^

src\game\client\prediction\gameworld.cpp:567:5: style: Condition 'm_pParent' is always true [knownConditionTrueFalse]
 if(m_pParent && m_pParent->m_pChild && m_pParent->m_pChild != this)
    ^

src\game\client\components\menu_background.cpp:271:7: style: Condition 'NeedImageLoading' is always true [knownConditionTrueFalse]
   if(NeedImageLoading)
      ^
src\game\client\components\menu_background.cpp:268:23: note: Assignment 'NeedImageLoading=true', assigned value is 1
   NeedImageLoading = true;
                      ^
src\game\client\components\menu_background.cpp:271:7: note: Condition 'NeedImageLoading' is always true
   if(NeedImageLoading)
      ^

src\game\editor\editor.cpp:4991:6: style: Condition 'pEnvelope' is always true [knownConditionTrueFalse]
  if(pEnvelope)
     ^
```
2022-11-29 23:32:26 +01:00
Robert Müller 6501ae9b9d Add checks for remaining WideCharToMultiByte usages in system
Converting wide-char (UTF-16) to multi-byte (UTF-8) takes 1-2 wide-chars from the input and transforms them to 1-4 bytes, so having `char` and `WCHAR` buffers with equal static lengths means that this function can fail due to insufficient buffer sizes when the user has folder or files with very long names (many unicode codepoints).
Therefore checks are added that allow only the error `ERROR_INSUFFICIENT_BUFFER` when `WideCharToMultiByte` fails, which is expected on very long paths, as these would also lead to further errors later in the code. The respective functions will now fail with a return and ignore files that have too long names.
This could only completely be fixed using dynamically sized buffers for all paths, which seems like too much work and overhead.
Other errors are not expected and hence caught by the assertions, as those would indicate programming errors like wrong arguments being passed.

Reference: https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte
2022-10-29 21:17:48 +02:00
Robert Müller 6c30fb6f5a Remove duplicate buffer from fs_listdir functions
The second buffer was previously used when calling `fs_is_dir` explicity, but it has become from obsolete from #4657 by using the flag `FILE_ATTRIBUTE_DIRECTORY` instead.
2022-10-29 21:17:48 +02:00
Robert Müller 8bde109acb Use dynamic size for formatted Windows system messages
Extract `windows_format_system_message` and allocate appropriate buffer for the formatted system error messages, so messages of any length can be displayed.

The flag `FORMAT_MESSAGE_ALLOCATE_BUFFER` is used so `FormatMessageW` allocates the buffer which must later be freed with `LocalFree`.

The flag `FORMAT_MESSAGE_MAX_WIDTH_MASK` is also added so the formatted message will not contain any line breaks at the end, which would make log messages less readable.

Reference: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagew
2022-10-29 21:17:47 +02:00
Robert Müller a1c1a8f972 Add assertions for all MultiByteToWideChar usages in system
Converting multi-byte (UTF-8) to wide-char (UTF-16) take 1-4 bytes from the input and transforms them to 1-2 wide-chars, so having `char` and `WCHAR` buffers with equal lengths should mean that this function can't fail due to insufficient buffer sizes, unless a path that's already too long for Windows is being used internally.
Other errors are also not expected, as those would indicate programming errors like wrong arguments being passed.

The maximum length for paths in the Win32 API is 260 characters. This limitation can be lifted in Windows 10, but it must be done explicitly (opt-in) both in the Windows Registry by the user and in the application's manifest by us.

References:

- https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar
- https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry
2022-10-29 21:17:47 +02:00
Robert Müller 5c11937543 Use fegetenv/fesetenv instead of _control87 on Windows
As `_control87` doesn't seem to be available for some MinGW compilers.

Closes #5969.
2022-10-23 10:59:53 +02:00
Robert Müller c4cf26ea81 Replace atol usages with str_toint, as type is int anyway
As `atol` does not provide any way to detect errors and can cause undefined behavior on errors, i.e. when the result cannot be represented as a `long`.

The `atol` function was only used in `priv_net_extract` for parsing the port, which is an `int` and not a `long`, so the existing `str_toint` is used instead.

References:

- https://wiki.sei.cmu.edu/confluence/display/c/ERR07-C.+Prefer+functions+that+support+error+checking+over+equivalent+functions+that+don%27t
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/atol.html
2022-10-20 20:54:01 +02:00
Robert Müller 1469e88a79 Replace atof usages with strtod (str_tofloat wrapper)
As `atof` does not provide any way to detect errors and can cause undefined behavior on errors, i.e. when the result cannot be represented as a `float`.

References:

- https://wiki.sei.cmu.edu/confluence/display/c/ERR07-C.+Prefer+functions+that+support+error+checking+over+equivalent+functions+that+don%27t
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/atof.html
2022-10-20 20:30:12 +02:00
Robert Müller 6b043bf3b2 Replace atoi usages with strtol (str_toint wrapper)
As `atoi` does not provide any way to detect errors and can cause undefined behavior on errors, i.e. when the result cannot be represented as an `int`.

References:

- https://wiki.sei.cmu.edu/confluence/display/c/ERR07-C.+Prefer+functions+that+support+error+checking+over+equivalent+functions+that+don%27t
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/atoi.html
2022-10-20 20:30:11 +02:00
Robert Müller 25e6eed0e8 Fix compilation with VS2022 with exception handling enabled
By moving the calling convention `APIENTRY`, which expands to `__stdcall`, inside the parenthesis.

```
Severity	Code	Description	Project	File	Line	Suppression State
Error	C2143	syntax error: missing ')' before '('	engine-shared	src\base\system.cpp	4242
Warning	C4229	anachronism used: modifiers on data are ignored	engine-shared	src\base\system.cpp	4242
Error	C2059	syntax error: ')'	engine-shared	src\base\system.cpp	4242
Error	C2059	syntax error: ')'	engine-shared	src\base\system.cpp	4242
Error	C3536	'exception_log_file_path_func': cannot be used before it is initialized	engine-shared	src\base\system.cpp	4246
Error	C2446	'==': no conversion from 'nullptr' to 'int'	engine-shared	src\base\system.cpp	4246
Error	C2064	term does not evaluate to a function taking 1 arguments	engine-shared	src\base\system.cpp	4249
```
2022-10-16 18:16:58 +02:00
bors[bot] bd3b4f2d50
Merge #5942
5942: Support unicode with ExcHndl, use upstream module offsets, handle errors r=def- a=Robyt3

Update Dr. Mingw (ExcHndl) to 0.9.8.

Use the new `ExcHndlSetLogFileNameW` function to set the exception log file name using wide characters, to support paths containing unicode.

It's not necessary to call `ExcHndlInit` explicitly after loading `exchndl.dll`, as the `DllMain` will already initialize the exception handler when the DLL is loaded. Module offsets are supported by upstream ExcHndl now, so we don't need to provide our own version that supplies the module offset to `ExcHndlInit` anymore. Upstream ExcHndl will also resolve the source code lines for addresses automatically, when the executable is build with debug information.

Handle the cases that the exception handling module cannot be loaded and that the `ExcHndlSetLogFileNameW` function cannot be found in the module.

Update `scripts/parse_drmingw.sh`:

- Parse both old and new module offsets.
- Use tabs instead of spaces consistently.
- Reset the ANSI color after printing colored messages.

Closes #5877.

Needs https://github.com/ddnet/ddnet-libs/pull/34.

Example crash logs:

- [crash_debug_old.RTP.txt](https://github.com/ddnet/ddnet/files/9768008/crash_debug_old.RTP.txt)
- [crash_debug_new.RTP.txt](https://github.com/ddnet/ddnet/files/9768011/crash_debug_new.RTP.txt)
- [crash_release_old.RTP.txt](https://github.com/ddnet/ddnet/files/9768010/crash_release_old.RTP.txt)
- [crash_release_new.RTP.txt](https://github.com/ddnet/ddnet/files/9768009/crash_release_new.RTP.txt)

## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
2022-10-12 21:44:43 +00:00
Robert Müller fb317779fc Support unicode with ExcHndl, use upstream module offsets, handle errors
Update Dr. Mingw (ExcHndl) to 0.9.8.

Use the new `ExcHndlSetLogFileNameW` function to set the exception log file name using wide characters, to support paths containing unicode.

It's not necessary to call `ExcHndlInit` explicitly after loading `exchndl.dll`, as the `DllMain` will already initialize the exception handler when the DLL is loaded.
Module offsets are supported by upstream ExcHndl now, so we don't need to provide our own version that supplies the module offset to `ExcHndlInit` anymore.
Upstream ExcHndl will also resolve the source code lines for addresses automatically, when the executable is build with debug information.

Handle the cases that the exception handling module cannot be loaded and that the `ExcHndlSetLogFileNameW` function cannot be found in the module.

Update `scripts/parse_drmingw.sh`:

- Parse both old and new module offsets.
- Use tabs instead of spaces consistently.
- Reset the ANSI color after printing colored messages.
2022-10-12 21:11:35 +02:00
Robert Müller c68841a733 Ensure Windows DDE conversations are finished in open_link
Use `ShellExecuteExW` instead of `ShellExecuteW` to pass the additional flag `SEE_MASK_NOASYNC`. This should ensure that DDE (Dynamic data exchange) conversations are finished before the function returns, as it would otherwise be necessary to manually pump messages on the calling thread. DDE conversations may be initiated when shell extensions are loaded via `ShellExecute`. Failing to answer the DDE messages will block the calling UI thread and may cause a dead-lock.

The flag `SEE_MASK_FLAG_NO_UI` is used to prevent native error message popups when the link/file cannot be opened.

Additionally, the `ShellExecute` verb is changed from `"open"` to `NULL`, which will cause the default shell handler to be used instead, as the `"open"` verb may not be available for all targets. Though for most targets it will be the default verb anyway.

References:

- https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew
- https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shellexecuteinfow
- https://devblogs.microsoft.com/oldnewthing/20070430-00/?p=27063
- https://learn.microsoft.com/en-us/windows/win32/dataxchg/about-dynamic-data-exchange

Yet another attempt at solving #5744.
2022-10-12 17:18:57 +02:00
Robert Müller 4bb549b68e Initialize the Windows COM library on all threads
Use threading model `COINIT_APARTMENTTHREADED` on threads that own a window (the main client thread) and `COINIT_MULTITHREADED` on all other threads.

Add assertions to ensure that the COM library is initialized successfully and only once per thread.

References:

- https://learn.microsoft.com/en-us/windows/win32/learnwin32/initializing-the-com-library
- https://learn.microsoft.com/en-us/windows/win32/com/single-threaded-apartments
- https://learn.microsoft.com/en-us/windows/win32/com/multithreaded-apartments

Yet another attempt at solving #5744.
2022-10-12 17:18:57 +02:00
Robert Müller 1697231916 Handle non-default Windows directory in os_version_str
- Retrieve version information of `kernel32.dll` without providing a fixed path to the Windows directory, to handle the case that Windows is not installed in the default location `C:\Windows`. This works because `GetFileVersionInfoSizeW` and `GetFileVersionInfoW` use the search sequence defined by `LoadLibrary`, so they will find the DLL without the path explicitly being specified. The version is retrieved from `kernel32.dll` instead of `user32.dll`, as the former is more commonly used for this purpose.
- Use the unicode (wide character) functions consistently instead of the ANSI functions.
- Use the correct format specifier `%hu` for `unsigned short` (i.e. `WORD` in the Windows API).

References:

- https://learn.microsoft.com/en-us/windows/win32/api/winver/nf-winver-getfileversioninfosizew
- https://learn.microsoft.com/en-us/windows/win32/api/winver/nf-winver-getfileversioninfow
- https://stackoverflow.com/a/44672263/1708371
2022-10-03 13:17:26 +02:00
Robert Müller c924f6bb25 Use str_isspace in str_skip_(to_)whitespace(s), add tests
Update the `str_skip_to_whitespace(_const)` functions according to their documentation, which already stated that `\r` was also considered as whitespace.
2022-09-30 14:54:33 +02:00
Robert Müller 9472db419b Fix str_hex and add tests
- Properly null-terminate the destination buffer when the data size is zero.
- Fix early loop exit when destination buffer is too small for all data.
2022-09-30 14:54:33 +02:00
Robert Müller 02a7913121 Sort filenames case insensitive with str_comp_filenames, add tests
This changes `str_comp_filenames` so it sorts filenames case insensitive while also comparing digits characters as numbers.

This makes filename sorting consistent with the behavior in Windows Explorer.
2022-09-30 14:48:52 +02:00
Robert Müller ce145cfa4e Improve str_trim_words and add tests
- Change argument and return value to `const char *`, as the string is not modified by this function.
- Use our own `str_isspace` instead of standard library `isspace`.
- Always trim leading whitespace to correctly handle inputs with leading whitespace.
2022-09-30 12:29:32 +02:00
Robert Müller e98728f8f0 Extend str_isspace to consider \r as whitespace, add documentation
This allows the function to be reused in more places where `\r` is also considered as whitespace.
2022-09-30 12:13:23 +02:00
Robert Müller 61f763ff2c Use GetCurrentDirectoryW instead of _wgetcwd
For more consistent usage of the Windows API. `_wgetcwd` seems to be a wrapper around `GetCurrentDirectoryW` anyway, at least in the [Wine API](https://source.winehq.org/ident?_i=_wgetcwd).

Pass the correct size of the wide char buffer instead of passing the size of the output parameter buffer.

Remove a nullptr check. The argument should never be null and we don't check for null arguments anywhere else.
2022-09-24 17:09:31 +02:00
Robert Müller 2ba67c2306 Use SetCurrentDirectoryW instead of _wchdir
For more consistent usage of the Windows API. `_wchdir` seems to be a wrapper around `SetCurrentDirectoryW` anyway, at least in the [Wine API](https://source.winehq.org/ident?_i=_wchdir).
2022-09-24 17:09:31 +02:00
Robert Müller 8fb79242bf Use CreateDirectoryW + GetLastError instead of _wmkdir + errno
For more consistent usage of the Windows API. `_wmkdir` seems to be a wrapper around `CreateDirectoryW` anyway, at least in the [Wine API](https://source.winehq.org/ident?_i=_wmkdir).
2022-09-24 17:09:31 +02:00
heinrich5991 47d06bd0ca Fix receiving IPv6 packets after IPv4 ones on Linux
Previously, the socket addresses were truncated as the `msg_namelen`
field is both input **and** output: After receiving an IPv4 packet, the
socket address field would be too short for an IPv6 address.
2022-09-20 01:06:56 +02:00
heinrich5991 911835f940 Add str_has_cc to check for control characters in strings 2022-09-11 23:39:16 +02:00
Robert Müller 9a76ebdcb6 Save and restore FPU control word when calling ShellExecute
ShellExecute may load additional DLLs in the current context, which might change the FPU control word.

Another potential fix for #5744.

Sources:
- https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6457572
- https://web.archive.org/web/20070730055602/http://tolstoy.newcastle.edu.au/R/devel/06/02/4262.html
- http://www.delphigroups.info/2/d3/420953-2.html (only available in Google Cache for me)
- f4d80957e8/src/windows/native/sun/windows/awt_Desktop.cpp (L46-L49)
2022-09-05 22:18:39 +02:00
def 91bc21c489 No need for lengthy semaphore names on macOS, pointer address is enough
Also check on init failure and at least log, same as on UNIX
2022-08-30 10:09:35 +02:00
Robert Müller 713b6584f0 Add str_countchr to count occurrences of a char in a string 2022-08-23 10:08:46 +02:00
bors[bot] f355a3467f
Merge #5760
5760: Fix variable shadow warnings with MinGW r=def- a=Robyt3

```
ddnet/src/base/system.cpp: In function 'int byteval(const char*, unsigned char*)':
ddnet/src/base/system.cpp:3044:32: warning: declaration of 'byte' shadows a global declaration [-Wshadow]
 3044 | static int byteval(const char *byte, unsigned char *dst)
      |                    ~~~~~~~~~~~~^~~~
In file included from msys64/mingw64/include/objbase.h:8,
                 from ddnet/src/base/system.cpp:68:
msys64/mingw64/include/rpcndr.h:63:25: note: shadowed declaration is here
   63 |   typedef unsigned char byte;
      |                         ^~~~
ddnet/src/base/system.cpp: In function 'unsigned char str_byte_next(const char**)':
ddnet/src/base/system.cpp:3557:23: warning: declaration of 'byte' shadows a global declaration [-Wshadow]
 3557 |         unsigned char byte = **ptr;
      |                       ^~~~
msys64/mingw64/include/rpcndr.h:63:25: note: shadowed declaration is here
   63 |   typedef unsigned char byte;
      |                         ^~~~
ddnet/src/base/system.cpp: In function 'int str_utf8_decode(const char**)':
ddnet/src/base/system.cpp:3577:31: warning: declaration of 'byte' shadows a global declaration [-Wshadow]
 3577 |                 unsigned char byte = str_byte_next(ptr);
      |                               ^~~~
msys64/mingw64/include/rpcndr.h:63:25: note: shadowed declaration is here
   63 |   typedef unsigned char byte;
      |                         ^~~~
```

<!-- What is the motivation for the changes of this pull request -->

## Checklist

- [ ] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
2022-08-21 09:08:57 +00:00
Robert Müller 64042d29df Fix variable shadow warnings with MinGW
```
ddnet/src/base/system.cpp: In function 'int byteval(const char*, unsigned char*)':
ddnet/src/base/system.cpp:3044:32: warning: declaration of 'byte' shadows a global declaration [-Wshadow]
 3044 | static int byteval(const char *byte, unsigned char *dst)
      |                    ~~~~~~~~~~~~^~~~
In file included from msys64/mingw64/include/objbase.h:8,
                 from ddnet/src/base/system.cpp:68:
msys64/mingw64/include/rpcndr.h:63:25: note: shadowed declaration is here
   63 |   typedef unsigned char byte;
      |                         ^~~~
ddnet/src/base/system.cpp: In function 'unsigned char str_byte_next(const char**)':
ddnet/src/base/system.cpp:3557:23: warning: declaration of 'byte' shadows a global declaration [-Wshadow]
 3557 |         unsigned char byte = **ptr;
      |                       ^~~~
msys64/mingw64/include/rpcndr.h:63:25: note: shadowed declaration is here
   63 |   typedef unsigned char byte;
      |                         ^~~~
ddnet/src/base/system.cpp: In function 'int str_utf8_decode(const char**)':
ddnet/src/base/system.cpp:3577:31: warning: declaration of 'byte' shadows a global declaration [-Wshadow]
 3577 |                 unsigned char byte = str_byte_next(ptr);
      |                               ^~~~
msys64/mingw64/include/rpcndr.h:63:25: note: shadowed declaration is here
   63 |   typedef unsigned char byte;
      |                         ^~~~
```
2022-08-21 10:44:08 +02:00
Robert Müller 37cadc5c09 Add CWindowsComLifecycle RAII wrapper for Windows COM library 2022-08-21 10:34:37 +02:00
bors[bot] 4fc6a5b924
Merge #5754 #5757
5754: Auto refresh skins when changing related settings r=def- a=Jupeyy

motivation: downloaded skins aren't resettet, e.g. if they failed before. config change -> resets everything

## Checklist

- [x] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


5757: Make file link absolute, add `fs_is_relative_path` r=def- a=Robyt3

This fixes links not opening for relative paths, as links like `file://temp/skins` cannot be resolved by the shell.

Closes #5746.

## Checklist

- [X] Tested the change ingame (only on Windows)
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Jupeyy <jupjopjap@gmail.com>
Co-authored-by: Robert Müller <robytemueller@gmail.com>
2022-08-20 20:34:45 +00:00
Robert Müller 21fe945ca7 Make file link absolute, add fs_is_relative_path
This fixes links not opening for relative paths, as links like `file://temp/skins` cannot be resolved by the shell.
2022-08-20 20:56:46 +02:00
Robert Müller ec55212c52 Fix symlink handling of fs_listdir
On Windows, the check was incorrectly using logical or instead of bitwise or. Checking for the flag `FILE_ATTRIBUTE_REPARSE_POINT` is not necessary, as the `FILE_ATTRIBUTE_DIRECTORY` will correctly be set when the target of a symbolic link is a directory. This otherwise causes symbolic links to files to be incorrectly handled as directories.

On Linux, the minor performance optimization of using `entry->d_type` is reverted and `fs_is_dir` is used instead. This internally uses `stat`, which correctly returns the attributes for the symbolic link targets.
2022-08-20 15:57:43 +02:00
Patiga b580b83d31 List symlinks in list_dir
This functionality was lost in 082e26d5b
In my case I have a lot of symlinks in the maps folder (Downloads,
ddnet-maps, map_archive)
2022-08-18 12:51:22 +02:00
def c94de9b764 Further clang-tidy 14 issues (fixes #5612) 2022-07-27 09:07:47 +02:00