Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Measurity committed Apr 28, 2023
2 parents a707e79 + 03a221b commit 5b98ded
Show file tree
Hide file tree
Showing 62 changed files with 1,808 additions and 979 deletions.
47 changes: 41 additions & 6 deletions Nitrox.BuildTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ public static class Program

public static string GeneratedOutputDir => Path.Combine(ProcessDir, "generated_files");

private const int LEGACY_BRANCH_SUBNAUTICA_VERSION = 68598;

public static async Task Main(string[] args)
{

AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) =>
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(eventArgs.ExceptionObject);
Console.ResetColor();
LogError(eventArgs.ExceptionObject.ToString());
Exit((eventArgs.ExceptionObject as Exception)?.HResult ?? 1);
};
Log.Setup(false, null, true, true, false);

GameInstallData game = await Task.Factory.StartNew(EnsureGame).ConfigureAwait(false);
GameInstallData game = await Task.Run(EnsureGame);
Console.WriteLine($"Found game at {game.InstallDir}");
await EnsurePublicizedAssembliesAsync(game).ConfigureAwait(false);
AbortIfInvalidGameVersion(game);
await EnsurePublicizedAssembliesAsync(game);

Exit();
}
Expand All @@ -50,6 +51,40 @@ private static void Exit(int exitCode = 0)
Environment.Exit(exitCode);
}

private static void LogError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
}

private static void AbortIfInvalidGameVersion(GameInstallData game)
{
string gameVersionFile = Path.Combine(game.InstallDir, "Subnautica_Data", "StreamingAssets", "SNUnmanagedData", "plastic_status.ignore");
if (!File.Exists(gameVersionFile))
{
return;
}
if (!int.TryParse(File.ReadAllText(gameVersionFile), out int version))
{
return;
}
if (version == -1)
{
return;
}
if (version > LEGACY_BRANCH_SUBNAUTICA_VERSION)
{
return;
}

LogError($"""
Game version is {version}, which is not supported by Nitrox.
Please update your game to the latest version.
""");
Exit(2);
}

private static GameInstallData EnsureGame()
{
static bool ValidateUnityGame(GameInstallData game, out string error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void TestInitialize()
public void NonActionPacket()
{
TestNonActionPacket packet = new TestNonActionPacket(PLAYER_ID);
packetReceiver.PacketReceived(packet, 0);
packetReceiver.PacketReceived(packet);

Queue<Packet> packets = packetReceiver.GetReceivedPackets();

Expand Down
7 changes: 7 additions & 0 deletions Nitrox.Test/Helper/AssertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public static class AssertHelper
{
public static void IsListEqual<TSource>(IOrderedEnumerable<TSource> first, IOrderedEnumerable<TSource> second, Action<TSource, TSource> assertComparer)
{
Assert.IsNotNull(first);
Assert.IsNotNull(second);

List<TSource> firstList = first.ToList();
List<TSource> secondList = second.ToList();

Expand All @@ -22,6 +25,8 @@ public static void IsListEqual<TSource>(IOrderedEnumerable<TSource> first, IOrde

public static void IsDictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
{
Assert.IsNotNull(first);
Assert.IsNotNull(second);
Assert.AreEqual(first.Count, second.Count);

for (int index = 0; index < first.Count; index++)
Expand All @@ -34,6 +39,8 @@ public static void IsDictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue> fir

public static void IsDictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second, Action<KeyValuePair<TKey, TValue>, KeyValuePair<TKey, TValue>> assertComparer)
{
Assert.IsNotNull(first);
Assert.IsNotNull(second);
Assert.AreEqual(first.Count, second.Count);

for (int index = 0; index < first.Count; index++)
Expand Down
70 changes: 70 additions & 0 deletions Nitrox.Test/Helper/Faker/NitroxAbstractFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NitroxModel_Subnautica.Logger;
using NitroxModel.Packets;
using NitroxModel.Packets.Processors.Abstract;
using NitroxServer;
using NitroxServer_Subnautica;
using NitroxServer.ConsoleCommands.Abstract;

namespace Nitrox.Test.Helper.Faker;

public class NitroxAbstractFaker : NitroxFaker, INitroxFaker
{
private static readonly Dictionary<Type, Type[]> subtypesByBaseType;

static NitroxAbstractFaker()
{
Assembly[] assemblies = { typeof(Packet).Assembly, typeof(SubnauticaInGameLogger).Assembly, typeof(ServerAutoFacRegistrar).Assembly, typeof(SubnauticaServerAutoFacRegistrar).Assembly };
HashSet<Type> blacklistedTypes = new() { typeof(Packet), typeof(CorrelatedPacket), typeof(Command), typeof(PacketProcessor) };

List<Type> types = new();
foreach (Assembly assembly in assemblies)
{
types.AddRange(assembly.GetTypes());
}

subtypesByBaseType = types.Where(type => type.IsAbstract && !type.IsSealed && !blacklistedTypes.Contains(type))
.ToDictionary(type => type, type => types.Where(t => type.IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).ToArray())
.Where(dict => dict.Value.Length > 0)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}

public readonly int AssignableTypesCount;
private readonly Queue<INitroxFaker> assignableFakers = new();

public NitroxAbstractFaker(Type type)
{
if (!type.IsAbstract)
{
throw new ArgumentException("Argument is not abstract", nameof(type));
}

if (!subtypesByBaseType.TryGetValue(type, out Type[] subTypes))
{
throw new ArgumentException($"Argument is not contained in {nameof(subtypesByBaseType)}", nameof(type));
}

OutputType = type;
AssignableTypesCount = subTypes.Length;
FakerByType.Add(type, this);
foreach (Type subType in subTypes)
{
assignableFakers.Enqueue(GetOrCreateFaker(subType));
}
}

public INitroxFaker[] GetSubFakers() => assignableFakers.ToArray();

/// <summary>
/// Selects an implementing type in a round-robin fashion of the abstract type of this faker. Then creates an instance of it.
/// </summary>
public object GenerateUnsafe(HashSet<Type> typeTree)
{
INitroxFaker assignableFaker = assignableFakers.Dequeue();
assignableFakers.Enqueue(assignableFaker);
return assignableFaker.GenerateUnsafe(typeTree);
}
}
19 changes: 19 additions & 0 deletions Nitrox.Test/Helper/Faker/NitroxActionFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;

namespace Nitrox.Test.Helper.Faker;

public class NitroxActionFaker : NitroxFaker, INitroxFaker
{
private readonly Func<Bogus.Faker, object> generateAction;

public NitroxActionFaker(Type type, Func<Bogus.Faker, object> action)
{
OutputType = type;
generateAction = action;
}

public INitroxFaker[] GetSubFakers() => Array.Empty<INitroxFaker>();

public object GenerateUnsafe(HashSet<Type> _) => generateAction.Invoke(Faker);
}
Loading

0 comments on commit 5b98ded

Please sign in to comment.