mumain/ClientLibrary/ConnectionManager.ConnectServerFunctions.cs
Mosch0512 1a4d25c01a Run and connect the client natively on Linux (issue #462 Phase 4)
The engine now boots into the login scene, renders via SDL/OpenGL, and
connects to a server on Linux. Brings up the remaining runtime pieces and
fixes the bugs that surfaced once the game actually ran.

Networking (.NET client library on Linux):
- Build MUnique.Client.Library as a native linux-x64 Native AOT shared
  object and dlopen the .so instead of the Windows .dll.
- Fix the native-to-managed string marshalling: the client passes wchar_t*,
  which is 4-byte UTF-32 on Linux but the library decoded it as UTF-16,
  garbling the server address into a bogus IP so connect() hung forever on
  the main thread (the apparent "black screen"). Decode by the platform's
  real wchar_t width. Updates the XSLT template so regeneration stays correct.

Runtime fixes uncovered by running:
- Config was never read on Linux: the exe-dir split looked for a backslash
  only, so config.ini was never found and every setting fell back to its
  default. Accept either path separator.
- UI piled into the top-left: CInput::Create bailed on the null SDL window
  handle before setting the screen size, so centered windows computed
  negative positions. Set the size regardless of the handle off Windows.
- LP64 width bugs reading binary assets (size fields are 32-bit on disk, but
  long/size_t are 8 bytes on Linux x64): pet.bmd speed array, BMD encrypted
  size, camera walk-script waypoint count.
- Double fclose() of the same handle (filter file, object files): tolerated
  on the old CRT, a double-free on glibc.
- mu_swprintf bounds the buffer by its real array size to stop a fortify
  abort, and the GL frame can be dumped via MU_CAPTURE_FRAME for headless
  verification.

Portable subsystems:
- GDI text backend backed by stb_truetype (CreateFont/CreateDIBSection/
  TextOut), so UI text renders without Win32 GDI.
- Case-correcting path resolver so Windows-style asset paths resolve on a
  case-sensitive filesystem, wired through every file-open shim.
- GameShop compiles on Linux: the UI and list managers build, only the
  WinINet/urlmon downloader is excluded, with the download entry points
  failing gracefully.
- Portable shims extended: StrSafe, Sleep/GetLastError, file attribute and
  directory helpers, system-info for the crash log, and the Linux entry point
  now calls the shared WinMain bootstrap.
2026-06-13 18:22:03 +02:00

337 lines
No EOL
12 KiB
C#

// <copyright file="ConnectionManager.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>
//------------------------------------------------------------------------------
// <auto-generated>
// This source code was auto-generated by an XSL transformation.
// Do not change this file. Instead, change the XML data which contains
// the packet definitions and re-run the transformation (publish/rebuild the
// client library project).
// </auto-generated>
//------------------------------------------------------------------------------
// ReSharper disable RedundantVerbatimPrefix
// ReSharper disable AssignmentIsFullyDiscarded
// ReSharper disable UnusedMember.Global
// ReSharper disable UseObjectOrCollectionInitializer
#nullable enable
namespace MUnique.Client.Library;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using MUnique.OpenMU.Network;
using MUnique.OpenMU.Network.Packets;
using MUnique.OpenMU.Network.Packets.ConnectServer;
/// <summary>
/// Extension methods to start writing messages of this namespace on a <see cref="IConnection"/>.
/// </summary>
public unsafe partial class ConnectionManager
{
/// <summary>
/// Sends a <see cref="ConnectionInfoRequest075" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <param name="serverId">The server id.</param>
/// <remarks>
/// Is sent by the client when: This packet is sent by the client after the user clicked on an entry of the server list.
/// Causes reaction on server side: The server will send a ConnectionInfo back to the client.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendConnectionInfoRequest075")]
public static void SendConnectionInfoRequest075(int handle, byte @serverId)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = ConnectionInfoRequest075Ref.Length;
var packet = new ConnectionInfoRequest075Ref(pipeWriter.GetSpan(length)[..length]);
packet.ServerId = @serverId;
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// Sends a <see cref="ConnectionInfoRequest" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <param name="serverId">The server id.</param>
/// <remarks>
/// Is sent by the client when: This packet is sent by the client after the user clicked on an entry of the server list.
/// Causes reaction on server side: The server will send a ConnectionInfo back to the client.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendConnectionInfoRequest")]
public static void SendConnectionInfoRequest(int handle, ushort @serverId)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = ConnectionInfoRequestRef.Length;
var packet = new ConnectionInfoRequestRef(pipeWriter.GetSpan(length)[..length]);
packet.ServerId = @serverId;
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// Sends a <see cref="ConnectionInfo" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <param name="ipAddress">The ip address.</param>
/// <param name="port">The port.</param>
/// <remarks>
/// Is sent by the server when: This packet is sent by the server after the client requested the connection information of a server. This happens after the user clicked on a server.
/// Causes reaction on client side: The client will try to connect to the server with the specified information.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendConnectionInfo")]
public static void SendConnectionInfo(int handle, IntPtr @ipAddress, ushort @port)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = ConnectionInfoRef.Length;
var packet = new ConnectionInfoRef(pipeWriter.GetSpan(length)[..length]);
packet.IpAddress = NativeInterop.PtrToWideString(@ipAddress);
packet.Port = @port;
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// Sends a <see cref="ServerListRequest" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <remarks>
/// Is sent by the client when: This packet is sent by the client after it connected and received the 'Hello' message.
/// Causes reaction on server side: The server will send a ServerListResponse back to the client.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendServerListRequest")]
public static void SendServerListRequest(int handle)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = ServerListRequestRef.Length;
var packet = new ServerListRequestRef(pipeWriter.GetSpan(length)[..length]);
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// Sends a <see cref="ServerListRequestOld" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <remarks>
/// Is sent by the client when: This packet is sent by the client (below season 1) after it connected and received the 'Hello' message.
/// Causes reaction on server side: The server will send a ServerListResponseOld back to the client.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendServerListRequestOld")]
public static void SendServerListRequestOld(int handle)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = ServerListRequestOldRef.Length;
var packet = new ServerListRequestOldRef(pipeWriter.GetSpan(length)[..length]);
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// Sends a <see cref="Hello" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <remarks>
/// Is sent by the server when: This packet is sent by the server after the client connected to the server.
/// Causes reaction on client side: A game client will request the server list. The launcher would request the patch state.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendHello")]
public static void SendHello(int handle)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = HelloRef.Length;
var packet = new HelloRef(pipeWriter.GetSpan(length)[..length]);
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// Sends a <see cref="PatchCheckRequest" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <param name="majorVersion">The major version.</param>
/// <param name="minorVersion">The minor version.</param>
/// <param name="patchVersion">The patch version.</param>
/// <remarks>
/// Is sent by the client when: This packet is sent by the client (launcher) to check if the patch version is high enough to be able to connect to the server.
/// Causes reaction on server side: The connect server will check the version and sends a 'PatchVersionOkay' or a 'ClientNeedsPatch' message.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendPatchCheckRequest")]
public static void SendPatchCheckRequest(int handle, byte @majorVersion, byte @minorVersion, byte @patchVersion)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = PatchCheckRequestRef.Length;
var packet = new PatchCheckRequestRef(pipeWriter.GetSpan(length)[..length]);
packet.MajorVersion = @majorVersion;
packet.MinorVersion = @minorVersion;
packet.PatchVersion = @patchVersion;
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// Sends a <see cref="PatchVersionOkay" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <remarks>
/// Is sent by the server when: This packet is sent by the server after the client (launcher) requested the to check the patch version and it was high enough.
/// Causes reaction on client side: The launcher will activate its start button.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendPatchVersionOkay")]
public static void SendPatchVersionOkay(int handle)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = PatchVersionOkayRef.Length;
var packet = new PatchVersionOkayRef(pipeWriter.GetSpan(length)[..length]);
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
/// <summary>
/// Sends a <see cref="ClientNeedsPatch" /> to this connection.
/// </summary>
/// <param name="handle">The handle of the connection.</param>
/// <param name="patchVersion">The patch version.</param>
/// <param name="patchAddress">The patch address, usually to a ftp server. The address is usually "encrypted" with the 3-byte XOR key (FC CF AB).</param>
/// <remarks>
/// Is sent by the server when: This packet is sent by the server after the client (launcher) requested to check the patch version and it requires an update.
/// Causes reaction on client side: The launcher will download the required patches and then activate the start button.
/// </remarks>
[UnmanagedCallersOnly(EntryPoint = "SendClientNeedsPatch")]
public static void SendClientNeedsPatch(int handle, byte @patchVersion, IntPtr @patchAddress)
{
if (!Connections.TryGetValue(handle, out var connection))
{
return;
}
try
{
connection.CreateAndSend(pipeWriter =>
{
var length = ClientNeedsPatchRef.Length;
var packet = new ClientNeedsPatchRef(pipeWriter.GetSpan(length)[..length]);
packet.PatchVersion = @patchVersion;
packet.PatchAddress = NativeInterop.PtrToWideString(@patchAddress);
return length;
});
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}}