Eradicate ☠ Mono ☠

Co-Authored-By: Gustas <37534529+punkpun@users.noreply.github.com>
This commit is contained in:
michaeldgg2
2024-09-13 23:01:58 +02:00
committed by Pavel Penev
parent 5450572e0a
commit c7cc9a68fd
35 changed files with 89 additions and 634 deletions

View File

@@ -14,6 +14,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using OpenRA.Primitives;
using OpenRA.Support;
@@ -119,17 +120,11 @@ namespace OpenRA
public static V GetOrAdd<K, V>(this Dictionary<K, V> d, K k, V v)
{
#if NET5_0_OR_GREATER
// SAFETY: Dictionary cannot be modified whilst the ref is alive.
ref var value = ref System.Runtime.InteropServices.CollectionsMarshal.GetValueRefOrAddDefault(d, k, out var exists);
ref var value = ref CollectionsMarshal.GetValueRefOrAddDefault(d, k, out var exists);
if (!exists)
value = v;
return value;
#else
if (!d.TryGetValue(k, out var ret))
d.Add(k, ret = v);
return ret;
#endif
}
public static V GetOrAdd<K, V>(this Dictionary<K, V> d, K k, Func<K, V> createFn)
@@ -510,39 +505,6 @@ namespace OpenRA
return result;
}
public static T[,] ResizeArray<T>(T[,] ts, T t, int width, int height)
{
var result = new T[width, height];
for (var i = 0; i < width; i++)
{
for (var j = 0; j < height; j++)
{
// Workaround for broken ternary operators in certain versions of mono
// (3.10 and certain versions of the 3.8 series): https://bugzilla.xamarin.com/show_bug.cgi?id=23319
if (i <= ts.GetUpperBound(0) && j <= ts.GetUpperBound(1))
result[i, j] = ts[i, j];
else
result[i, j] = t;
}
}
return result;
}
public static int ToBits(this IEnumerable<bool> bits)
{
var i = 0;
var result = 0;
foreach (var b in bits)
if (b)
result |= 1 << i++;
else
i++;
if (i > 33)
throw new InvalidOperationException("ToBits only accepts up to 32 values.");
return result;
}
public static byte ParseByteInvariant(string s)
{
return byte.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);

View File

@@ -429,16 +429,9 @@ namespace OpenRA
{
var rendererPath = Path.Combine(Platform.BinDir, "OpenRA.Platforms." + platformName + ".dll");
#if NET5_0_OR_GREATER
var loader = new AssemblyLoader(rendererPath);
var platformType = loader.LoadDefaultAssembly().GetTypes().SingleOrDefault(t => typeof(IPlatform).IsAssignableFrom(t));
#else
// NOTE: This is currently the only use of System.Reflection in this file, so would give an unused using error if we import it above
var assembly = System.Reflection.Assembly.LoadFile(rendererPath);
var platformType = assembly.GetTypes().SingleOrDefault(t => typeof(IPlatform).IsAssignableFrom(t));
#endif
if (platformType == null)
throw new InvalidOperationException("Platform dll must include exactly one IPlatform implementation.");

View File

@@ -26,11 +26,7 @@ namespace OpenRA
var total = response.Content.Headers.ContentLength ?? -1;
var canReportProgress = total > 0;
#if NET5_0_OR_GREATER
using (var contentStream = await response.Content.ReadAsStreamAsync(token))
#else
using (var contentStream = await response.Content.ReadAsStreamAsync())
#endif
{
var totalRead = 0L;
var buffer = new byte[8192];

View File

@@ -55,26 +55,9 @@ namespace OpenRA
if (!ResolvedAssemblies.TryGetValue(hash, out var assembly))
{
#if NET5_0_OR_GREATER
var loader = new Support.AssemblyLoader(resolvedPath);
assembly = loader.LoadDefaultAssembly();
ResolvedAssemblies.Add(hash, assembly);
#else
assembly = Assembly.LoadFile(resolvedPath);
ResolvedAssemblies.Add(hash, assembly);
// Allow mods to use libraries.
var assemblyPath = Path.GetDirectoryName(resolvedPath);
if (assemblyPath != null)
{
foreach (var referencedAssembly in assembly.GetReferencedAssemblies())
{
var depedencyPath = Path.Combine(assemblyPath, referencedAssembly.Name + ".dll");
if (File.Exists(depedencyPath))
LoadAssembly(assemblyList, depedencyPath);
}
}
#endif
}
assemblyList.Add(assembly);

View File

@@ -2,14 +2,9 @@
<PropertyGroup>
<RootNamespace>OpenRA</RootNamespace>
</PropertyGroup>
<ItemGroup Condition="'$(MSBuildRuntimeType)'!='Mono'">
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.2" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
</ItemGroup>
<ItemGroup Condition="'$(MSBuildRuntimeType)'=='Mono'">
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Linguini.Bundle" Version="0.8.1" />
<PackageReference Include="OpenRA-Eluant" Version="1.0.22" />
<PackageReference Include="Mono.NAT" Version="3.0.4" />

View File

@@ -12,7 +12,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace OpenRA
@@ -63,21 +62,7 @@ namespace OpenRA
return PlatformType.Unknown;
}
public static string RuntimeVersion
{
get
{
var mono = Type.GetType("Mono.Runtime");
if (mono == null)
return $".NET CLR {Environment.Version}";
var version = mono.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
if (version == null)
return $"Mono (unknown version) CLR {Environment.Version}";
return $"Mono {version.Invoke(null, null)} CLR {Environment.Version}";
}
}
public static string RuntimeVersion => $".NET CLR {Environment.Version}";
public static string OperatingSystem
{

View File

@@ -24,10 +24,6 @@ namespace OpenRA.Scripting
protected readonly ScriptContext Context;
readonly Dictionary<string, ScriptMemberWrapper> members = new();
#if !NET5_0_OR_GREATER
readonly List<string> membersToRemove = new List<string>();
#endif
protected ScriptObjectWrapper(ScriptContext context)
{
Context = context;
@@ -67,22 +63,10 @@ namespace OpenRA.Scripting
protected void Unbind(Type targetType)
{
#if NET5_0_OR_GREATER
// NOTE: In newer versions of .NET modifying the collection by calling Remove while iterating over it is valid
foreach (var m in members)
if (targetType == m.Value.Target.GetType())
members.Remove(m.Key);
#else
// PERF: Re-use instead of allocating a new list on each unbind
membersToRemove.Clear();
foreach (var m in members)
if (targetType == m.Value.Target.GetType())
membersToRemove.Add(m.Key);
foreach (var m in membersToRemove)
members.Remove(m);
#endif
}
public bool ContainsKey(string key) { return members.ContainsKey(key); }

View File

@@ -14,6 +14,7 @@ using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace OpenRA
@@ -160,11 +161,7 @@ namespace OpenRA
while ((b = s.ReadUInt8()) != 0)
bytes.Add(b);
#if NET5_0_OR_GREATER
return Encoding.ASCII.GetString(System.Runtime.InteropServices.CollectionsMarshal.AsSpan(bytes));
#else
return Encoding.ASCII.GetString(bytes.ToArray());
#endif
return Encoding.ASCII.GetString(CollectionsMarshal.AsSpan(bytes));
}
public static string ReadAllText(this Stream s)

View File

@@ -9,9 +9,7 @@
*/
#endregion
// Not used/usable on Mono. Only used for Dotnet Core.
// Based on https://github.com/natemcmaster/DotNetCorePlugins and used under the terms of the Apache 2.0 license
#if NET5_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.IO;
@@ -362,4 +360,3 @@ namespace OpenRA.Support
}
}
}
#endif

View File

@@ -16,10 +16,8 @@ namespace OpenRA.Support
{
public static class HttpClientFactory
{
#if NET5_0_OR_GREATER
const int MaxConnectionPerServer = 20;
static readonly TimeSpan ConnectionLifeTime = TimeSpan.FromMinutes(1);
#endif
static readonly Lazy<HttpMessageHandler> Handler = new(GetHandler);
@@ -30,7 +28,6 @@ namespace OpenRA.Support
static HttpMessageHandler GetHandler()
{
#if NET5_0_OR_GREATER
return new SocketsHttpHandler
{
// https://github.com/dotnet/corefx/issues/26895
@@ -40,9 +37,6 @@ namespace OpenRA.Support
PooledConnectionIdleTimeout = ConnectionLifeTime,
MaxConnectionsPerServer = MaxConnectionPerServer
};
#else
return new HttpClientHandler();
#endif
}
}
}