fix: Adds CUO settings support and adds more robust 7.0.9 support (#945)
This commit is contained in:
parent
941452de4a
commit
5b7b99e0de
12 changed files with 958 additions and 773 deletions
268
Projects/Server/Client/ClientVersion.cs
Normal file
268
Projects/Server/Client/ClientVersion.cs
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: ClientVersion.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Server.Buffers;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public enum ClientType
|
||||
{
|
||||
Regular,
|
||||
UOTD,
|
||||
God,
|
||||
SA
|
||||
}
|
||||
|
||||
public class ClientVersion : IComparable<ClientVersion>, IComparer<ClientVersion>
|
||||
{
|
||||
public static readonly ClientVersion Version400a = new("4.0.0a");
|
||||
public static readonly ClientVersion Version407a = new("4.0.7a");
|
||||
public static readonly ClientVersion Version500a = new("5.0.0a");
|
||||
public static readonly ClientVersion Version502b = new("5.0.2b");
|
||||
public static readonly ClientVersion Version6000 = new("6.0.0.0");
|
||||
public static readonly ClientVersion Version6017 = new("6.0.1.7");
|
||||
public static readonly ClientVersion Version60142 = new("6.0.14.2");
|
||||
public static readonly ClientVersion Version7000 = new("7.0.0.0");
|
||||
public static readonly ClientVersion Version7090 = new("7.0.9.0");
|
||||
public static readonly ClientVersion Version70130 = new("7.0.13.0");
|
||||
public static readonly ClientVersion Version70160 = new("7.0.16.0");
|
||||
public static readonly ClientVersion Version70300 = new("7.0.30.0");
|
||||
public static readonly ClientVersion Version70331 = new("7.0.33.1");
|
||||
public static readonly ClientVersion Version704565 = new("7.0.45.65");
|
||||
public static readonly ClientVersion Version70500 = new("7.0.50.0");
|
||||
public static readonly ClientVersion Version70610 = new("7.0.61.0");
|
||||
|
||||
public ClientVersion(int maj, int min, int rev, int pat, ClientType type = ClientType.Regular)
|
||||
{
|
||||
Major = maj;
|
||||
Minor = min;
|
||||
Revision = rev;
|
||||
Patch = pat;
|
||||
Type = type;
|
||||
|
||||
SourceString = Utility.Intern(ToStringImpl());
|
||||
}
|
||||
|
||||
public ClientVersion(string fmt)
|
||||
{
|
||||
fmt = fmt.ToLower();
|
||||
SourceString = Utility.Intern(fmt);
|
||||
|
||||
try
|
||||
{
|
||||
var br1 = fmt.IndexOfOrdinal('.');
|
||||
var br2 = fmt.IndexOf('.', br1 + 1);
|
||||
|
||||
var br3 = br2 + 1;
|
||||
while (br3 < fmt.Length && char.IsDigit(fmt, br3))
|
||||
{
|
||||
br3++;
|
||||
}
|
||||
|
||||
Major = Utility.ToInt32(fmt.AsSpan()[..br1]);
|
||||
Minor = Utility.ToInt32(fmt.Substring(br1 + 1, br2 - br1 - 1));
|
||||
Revision = Utility.ToInt32(fmt.Substring(br2 + 1, br3 - br2 - 1));
|
||||
|
||||
if (br3 < fmt.Length)
|
||||
{
|
||||
if (Major <= 5 && Minor <= 0 && Revision <= 6) // Anything before 5.0.7
|
||||
{
|
||||
if (!char.IsWhiteSpace(fmt, br3))
|
||||
{
|
||||
Patch = fmt[br3] - 'a' + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Patch = Utility.ToInt32(fmt.Substring(br3 + 1, fmt.Length - br3 - 1));
|
||||
}
|
||||
}
|
||||
|
||||
if (fmt.InsensitiveContains("god") || fmt.InsensitiveContains("gq"))
|
||||
{
|
||||
Type = ClientType.God;
|
||||
}
|
||||
else if (fmt.InsensitiveContains("third dawn") ||
|
||||
fmt.InsensitiveContains("uo:td") ||
|
||||
fmt.InsensitiveContains("uotd") ||
|
||||
fmt.InsensitiveContains("uo3d") ||
|
||||
fmt.InsensitiveContains("uo:3d"))
|
||||
{
|
||||
Type = ClientType.UOTD;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type = ClientType.Regular;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Major = 0;
|
||||
Minor = 0;
|
||||
Revision = 0;
|
||||
Patch = 0;
|
||||
Type = ClientType.Regular;
|
||||
}
|
||||
}
|
||||
|
||||
public int Major { get; }
|
||||
|
||||
public int Minor { get; }
|
||||
|
||||
public int Revision { get; }
|
||||
|
||||
public int Patch { get; }
|
||||
|
||||
public ClientType Type { get; }
|
||||
|
||||
public string SourceString { get; }
|
||||
|
||||
public int CompareTo(ClientVersion o)
|
||||
{
|
||||
if (o == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (Major > o.Major)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (Major < o.Major)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (Minor > o.Minor)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (Minor < o.Minor)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (Revision > o.Revision)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (Revision < o.Revision)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (Patch > o.Patch)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (Patch < o.Patch)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IComparer<ClientVersion>.Compare(ClientVersion x, ClientVersion y) => Compare(x, y);
|
||||
|
||||
public static bool operator ==(ClientVersion l, ClientVersion r) => Compare(l, r) == 0;
|
||||
|
||||
public static bool operator !=(ClientVersion l, ClientVersion r) => Compare(l, r) != 0;
|
||||
|
||||
public static bool operator >=(ClientVersion l, ClientVersion r) => Compare(l, r) >= 0;
|
||||
|
||||
public static bool operator >(ClientVersion l, ClientVersion r) => Compare(l, r) > 0;
|
||||
|
||||
public static bool operator <=(ClientVersion l, ClientVersion r) => Compare(l, r) <= 0;
|
||||
|
||||
public static bool operator <(ClientVersion l, ClientVersion r) => Compare(l, r) < 0;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override int GetHashCode() => HashCode.Combine(Major, Minor, Revision, Patch, Type);
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
var v = obj as ClientVersion;
|
||||
|
||||
return Major == v?.Major
|
||||
&& Minor == v.Minor
|
||||
&& Revision == v.Revision
|
||||
&& Patch == v.Patch
|
||||
&& Type == v.Type;
|
||||
}
|
||||
|
||||
private string ToStringImpl()
|
||||
{
|
||||
using var builder = new ValueStringBuilder(stackalloc char[32]);
|
||||
|
||||
builder.Append(Major.ToString());
|
||||
builder.Append('.');
|
||||
builder.Append(Minor.ToString());
|
||||
builder.Append('.');
|
||||
builder.Append(Revision.ToString());
|
||||
|
||||
if (Major <= 5 && Minor <= 0 && Revision <= 6) // Anything before 5.0.7
|
||||
{
|
||||
if (Patch > 0)
|
||||
{
|
||||
builder.Append((char)('a' + (Patch - 1)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append('.');
|
||||
builder.Append(Patch.ToString());
|
||||
}
|
||||
|
||||
if (Type != ClientType.Regular)
|
||||
{
|
||||
builder.Append(' ');
|
||||
builder.Append(Type.ToString().ToLower());
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public override string ToString() => SourceString;
|
||||
|
||||
public static bool IsNull(object x) => ReferenceEquals(x, null);
|
||||
|
||||
public static int Compare(ClientVersion a, ClientVersion b)
|
||||
{
|
||||
if (IsNull(a) && IsNull(b))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (IsNull(a))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (IsNull(b))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return a.CompareTo(b);
|
||||
}
|
||||
}
|
||||
133
Projects/Server/Client/UOClient.cs
Normal file
133
Projects/Server/Client/UOClient.cs
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: UOClient.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.IO;
|
||||
using System.Text.Json.Serialization;
|
||||
using Server.Json;
|
||||
using Server.Logging;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public static class UOClient
|
||||
{
|
||||
private static readonly ILogger logger = LogFactory.GetLogger(typeof(UOClient));
|
||||
|
||||
private static bool _automaticallyDetected;
|
||||
|
||||
public static CUOSettings CuoSettings { get; private set; }
|
||||
public static ClientVersion ServerClientVersion { get; private set; }
|
||||
|
||||
public static void Load()
|
||||
{
|
||||
ServerClientVersion = ServerConfiguration.GetSetting("clientData.clientVersion", (ClientVersion)null);
|
||||
|
||||
if (ServerClientVersion == null)
|
||||
{
|
||||
ServerClientVersion = DetectCUOClient() ?? DetectClassicClient();
|
||||
_automaticallyDetected = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Configure()
|
||||
{
|
||||
if (ServerClientVersion == null)
|
||||
{
|
||||
logger.Warning("Could not detect client version.");
|
||||
}
|
||||
else if (CuoSettings.ClientVersion == ServerClientVersion)
|
||||
{
|
||||
logger.Information($"Automatically detected client version {ServerClientVersion} from CUO settings.");
|
||||
}
|
||||
else if (_automaticallyDetected)
|
||||
{
|
||||
logger.Information($"Automatically detected client version {ServerClientVersion}");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Information($"Manually configured to use client version {ServerClientVersion}");
|
||||
}
|
||||
}
|
||||
|
||||
private static ClientVersion DetectCUOClient()
|
||||
{
|
||||
var path = Core.FindDataFile("settings.json", false);
|
||||
if (File.Exists(path))
|
||||
{
|
||||
var settings = JsonConfig.Deserialize<CUOSettings>(path);
|
||||
var file = new FileInfo(path);
|
||||
|
||||
if (settings.UltimaOnlineDirectory != null)
|
||||
{
|
||||
settings.UltimaOnlineDirectory = PathUtility.GetFullPath(settings.UltimaOnlineDirectory, file.DirectoryName);
|
||||
if (Directory.Exists(settings.UltimaOnlineDirectory))
|
||||
{
|
||||
CuoSettings = settings;
|
||||
}
|
||||
}
|
||||
|
||||
return settings.ClientVersion;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static ClientVersion DetectClassicClient()
|
||||
{
|
||||
var path = Core.FindDataFile("client.exe", false);
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
using FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var buffer = GC.AllocateUninitializedArray<byte>((int)fs.Length, true);
|
||||
fs.Read(buffer);
|
||||
// VS_VERSION_INFO (unicode)
|
||||
Span<byte> vsVersionInfo = stackalloc byte[]
|
||||
{
|
||||
0x56, 0x00, 0x53, 0x00, 0x5F, 0x00, 0x56, 0x00,
|
||||
0x45, 0x00, 0x52, 0x00, 0x53, 0x00, 0x49, 0x00,
|
||||
0x4F, 0x00, 0x4E, 0x00, 0x5F, 0x00, 0x49, 0x00,
|
||||
0x4E, 0x00, 0x46, 0x00, 0x4F, 0x00
|
||||
};
|
||||
|
||||
for (var i = 0; i < buffer.Length; i++)
|
||||
{
|
||||
if (vsVersionInfo.SequenceEqual(buffer.AsSpan(i, 30)))
|
||||
{
|
||||
var offset = i + 42; // 30 + 12
|
||||
|
||||
var minorPart = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(offset));
|
||||
var majorPart = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(offset + 2));
|
||||
var privatePart = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(offset + 4));
|
||||
var buildPart = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(offset + 6));
|
||||
|
||||
return new ClientVersion(majorPart, minorPart, buildPart, privatePart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public record CUOSettings
|
||||
{
|
||||
[JsonPropertyName("clientversion")]
|
||||
public ClientVersion ClientVersion { get; set; }
|
||||
|
||||
[JsonPropertyName("ultimaonlinedirectory")]
|
||||
public string UltimaOnlineDirectory { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue