using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Unity.IL2CPP;
using CatLib.Config;
using CatLib.Core;
using CatLib.DevTools;
using CatLib.Diagnostics;
using CatLib.Game.Events;
using CatLib.Localization;
using CatLib.Logging;
using CatLib.Net;
using CatLib.Patching;
using CatLib.Threading;
using CatLib.UI;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppMicrosoft.Data.Sqlite;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using Il2CppSystem.Data.Common;
using Il2CppSystem.Reflection;
using Microsoft.CodeAnalysis;
using TMPro;
using TooLate.Logic;
using TooLate.Patches;
using TooLate.Research;
using TooLate.Scene;
using TooLate.Settings;
using UnityEngine;
using UnityEngine.UI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("CatLib contributors")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.2.0")]
[assembly: AssemblyInformationalVersion("0.2.2+5afca9e7564e9569248ff23a4a6de0000d21c4e4")]
[assembly: AssemblyProduct("CatLib")]
[assembly: AssemblyTitle("TooLate")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.2.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Embedded]
internal sealed class IsUnmanagedAttribute : Attribute
{
}
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace TooLate
{
[BepInPlugin("catlib.toolate", "Too Late", "0.2.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class TooLatePlugin : BasePlugin
{
public const string LanguageResourcePrefix = "TooLate.Lang.";
public const string DevGroup = "Too Late";
public static JoinCoordinator Joins { get; private set; }
public override void Load()
{
CatLogger val = CatLogger.From(((BasePlugin)this).Log);
CatSettings val2 = CatSettings.For((BasePlugin)(object)this);
int count = val2.Texts.Count;
CatNetwork.Declare((BasePlugin)(object)this, (SessionPolicy)2, (VersionRule)2);
JoinCoordinator joins = new JoinCoordinator(val, val2.Texts);
joins.Settings = new JoinSettings(val2, null);
Joins = joins;
MethodInfo methodInfo = typeof(Server).GetMethods().FirstOrDefault((MethodInfo method) => method.Name == "HandleClientConnect" && method.GetParameters().Length == 2 && method.GetParameters()[0].ParameterType == typeof(ulong));
CodePatch loading = CodePatch.Locate("catlib.toolate", "joining a level", val, (MethodBase)methodInfo, "44 38 A1 ?? 00 00 00 0F 85 ?? ?? ?? ??", 7, 6, 2048);
CodePatch limit = CodePatch.Locate("catlib.toolate", "player limit", val, (MethodBase)methodInfo, "E8 ?? ?? ?? ?? 83 F8 05 0F 8D", 7, 1, 2048);
joins.UseCodePatches(loading, limit);
LobbyOverflow lobby = new LobbyOverflow(val, val2.Texts);
MessageRecorder recorder = new MessageRecorder(val);
SpawnPoints.Initialize(val);
CatPatches patches = TooLatePatches.Install("catlib.toolate", joins, lobby, recorder, val);
patches.TurnedOff += delegate
{
MainThread.Post((Action)joins.ShutDown);
};
FrameLoop.Update += delegate
{
patches.Run("Update", (Action)joins.Update);
patches.Run("Recorder", (Action)recorder.Update);
};
NetworkEvents.ClientConnected += delegate(ulong id)
{
patches.Run("ClientConnected", (Action)delegate
{
joins.OnClientConnected(id);
});
};
NetworkEvents.ClientDisconnected += delegate(ulong id)
{
patches.Run("ClientDisconnected", (Action)delegate
{
joins.OnClientDisconnected(id);
});
};
BootstrapEvents.GameStartedFromLobby += delegate
{
patches.Run("GameStarted", (Action)joins.EnterLevel);
};
BootstrapEvents.LevelLoadStarted += delegate
{
patches.Run("LevelLoadStarted", (Action)joins.EnterLevel);
};
BootstrapEvents.GameRestartStarted += delegate
{
joins.LeaveLevel(disconnectWaiting: true);
};
BootstrapEvents.MainMenuLoaded += delegate
{
joins.LeaveLevel(disconnectWaiting: false);
lobby.Forget();
};
DevMenu.Toggle("Too Late", "Record game messages", (Func<bool>)(() => recorder.IsEnabled), (Action<bool>)recorder.SetEnabled, "Writes every network message of the game to the log: what the host sends to each player and what this game sends to the host.");
DevMenu.Command("Too Late", "Joining players in the log", (Func<string>)joins.Describe, "Writes whether players can join your level now, the player limit and every player who is joining.");
DevMenu.Command("Too Late", "Snapshot in the log", (Func<string>)joins.SnapshotProbe, "Writes the warehouse as it is now into a separate file in BepInEx/cache/TooLate, like for a joining player, and writes what it holds.");
val.Info($"Too Late {"0.2.2"} loaded with {count} translated text(s) in {string.Join(", ", val2.Texts.Languages)}, " + ((patches.IsActive && joins.CanJoinInLevel) ? "players can join your levels" : "joining a level stays closed like in the game") + (joins.CanRaiseLimit ? string.Empty : ", the player limit stays 4"));
}
}
public static class PluginMeta
{
public const string Guid = "catlib.toolate";
public const string Name = "Too Late";
public const string Version = "0.2.2";
}
}
namespace TooLate.Settings
{
public sealed class JoinSettings
{
public Setting<bool> Enabled { get; }
public Setting<int> MaxPlayers { get; }
public Setting<bool> Messages { get; }
public JoinSettings(CatSettings settings, Action changed)
{
Enabled = settings.Local<bool>("Join", "Enabled", true, "Players can join while you are already playing. They get the warehouse as it is right now. Only the host needs the mod.", (AcceptableValueBase)null);
MaxPlayers = settings.Local<int>("Join", "MaxPlayers", 4, "How many players fit in your game, you included. 4 is the game's limit. Above 4 the lobby shows the extra players as a count, and the counters stay four.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(4, 8));
Messages = settings.Local<bool>("Messages", "Enabled", true, "Tells you when a player starts joining, waits for the end of the day's results and is in the game. Only for you.", (AcceptableValueBase)null);
Enabled.Changed += delegate
{
changed?.Invoke();
};
MaxPlayers.Changed += delegate
{
changed?.Invoke();
};
}
}
}
namespace TooLate.Scene
{
public static class GameProtocol
{
[ThreadStatic]
private static bool _sending;
public static bool IsSending => _sending;
public static NetworkManager Network
{
get
{
if (!Singleton<NetworkManager>.HasInstance())
{
return null;
}
return Singleton<NetworkManager>.Instance;
}
}
public static Server Server
{
get
{
NetworkManager network = Network;
if (!((Object)(object)network == (Object)null) && network.IsServer)
{
return network._server;
}
return null;
}
}
public static bool IsHosting => Server != null;
public static ulong OwnClientId
{
get
{
NetworkManager network = Network;
if (network == null)
{
return 0uL;
}
return network.ClientId;
}
}
public static Il2CppReferenceArray<Object> Empty()
{
return new Il2CppReferenceArray<Object>(0L);
}
public static bool Send(int code, ulong target, Il2CppReferenceArray<Object> payload, bool reliable = true)
{
Server server = Server;
if (server == null)
{
return false;
}
bool sending = _sending;
_sending = true;
try
{
server.SendMessage((ProtocolCode)code, reliable, target, payload ?? Empty());
return true;
}
finally
{
_sending = sending;
}
}
public static void SendSave(ulong target, string saveName, byte[] data)
{
NetworkManager val = Network ?? throw new InvalidOperationException("The game has no network manager");
bool sending = _sending;
_sending = true;
try
{
val.SendSaveSynchronization(target, false, false, false, saveName, Il2CppStructArray<byte>.op_Implicit(data));
}
finally
{
_sending = sending;
}
}
public static void Disconnect(ulong target)
{
Server server = Server;
if (server != null)
{
server.DisconnectClient(target);
}
}
public static Il2CppReferenceArray<Object> Copy(Il2CppReferenceArray<Object> payload)
{
if (payload == null)
{
return Empty();
}
Il2CppReferenceArray<Object> val = new Il2CppReferenceArray<Object>((long)((Il2CppArrayBase<Object>)(object)payload).Length);
for (int i = 0; i < ((Il2CppArrayBase<Object>)(object)payload).Length; i++)
{
((Il2CppArrayBase<Object>)(object)val)[i] = ((Il2CppArrayBase<Object>)(object)payload)[i];
}
return val;
}
public static Il2CppReferenceArray<Object> Identifiers(IReadOnlyList<EntityPlace> places, uint nextIdentifier)
{
//IL_0055: Unknown result type (might be due to invalid IL or missing references)
Il2CppReferenceArray<Object> val = new Il2CppReferenceArray<Object>((long)(places.Count * 2 + 2));
int num = 0;
foreach (EntityPlace place in places)
{
((Il2CppArrayBase<Object>)(object)val)[num++] = Box(place.Id);
((Il2CppArrayBase<Object>)(object)val)[num++] = Box(new Vector3(place.X, place.Y, place.Z));
}
((Il2CppArrayBase<Object>)(object)val)[num++] = Box(uint.MaxValue);
((Il2CppArrayBase<Object>)(object)val)[num] = Box(nextIdentifier);
return val;
}
public static Il2CppReferenceArray<Object> Generic(uint entityId, string text)
{
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Expected O, but got Unknown
return new Il2CppReferenceArray<Object>(2L)
{
[0] = Box(entityId),
[1] = new Object(IL2CPP.ManagedStringToIl2Cpp(text ?? string.Empty))
};
}
public static bool TryReadGeneric(Il2CppReferenceArray<Object> payload, out uint entityId, out string text)
{
text = null;
if (TryReadId(payload, out entityId) && ((Il2CppArrayBase<Object>)(object)payload).Length >= 2 && ((Il2CppArrayBase<Object>)(object)payload)[1] != null)
{
Type il2CppType = ((Il2CppArrayBase<Object>)(object)payload)[1].GetIl2CppType();
if (!(((il2CppType != null) ? ((MemberInfo)il2CppType).Name : null) != "String"))
{
text = IL2CPP.Il2CppStringToManaged(((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)payload)[1]).Pointer);
return text != null;
}
}
return false;
}
public static bool TryReadSpawn(Il2CppReferenceArray<Object> payload, out uint entityId, out string key)
{
entityId = 0u;
key = null;
if (payload == null || ((Il2CppArrayBase<Object>)(object)payload).Length < 8 || !Is(((Il2CppArrayBase<Object>)(object)payload)[2], "String") || !Is(((Il2CppArrayBase<Object>)(object)payload)[7], "UInt32"))
{
return false;
}
key = IL2CPP.Il2CppStringToManaged(((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)payload)[2]).Pointer);
entityId = ((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)payload)[7]).Unbox<uint>();
return true;
}
public static bool TryReadClientEntity(Il2CppReferenceArray<Object> payload, out ulong clientId, out uint entityId)
{
clientId = 0uL;
entityId = 0u;
if (payload == null || ((Il2CppArrayBase<Object>)(object)payload).Length < 2 || !Is(((Il2CppArrayBase<Object>)(object)payload)[0], "UInt64") || !Is(((Il2CppArrayBase<Object>)(object)payload)[1], "UInt32"))
{
return false;
}
clientId = ((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)payload)[0]).Unbox<ulong>();
entityId = ((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)payload)[1]).Unbox<uint>();
return true;
}
public static Il2CppReferenceArray<Object> ClientEntity(ulong clientId, uint entityId)
{
return new Il2CppReferenceArray<Object>(2L)
{
[0] = Box(clientId),
[1] = Box(entityId)
};
}
public static bool IsPresent(ulong clientId)
{
Server server = Server;
if (server == null)
{
return false;
}
if (clientId != OwnClientId)
{
if (server._clients != null)
{
return server._clients.ContainsKey(clientId);
}
return false;
}
return true;
}
public static bool TryReadStored(Il2CppReferenceArray<Object> payload, out uint storeId, out uint entityId)
{
storeId = 0u;
entityId = 0u;
if (payload == null || ((Il2CppArrayBase<Object>)(object)payload).Length < 2 || !Is(((Il2CppArrayBase<Object>)(object)payload)[0], "UInt32") || !Is(((Il2CppArrayBase<Object>)(object)payload)[1], "UInt32"))
{
return false;
}
storeId = ((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)payload)[0]).Unbox<uint>();
entityId = ((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)payload)[1]).Unbox<uint>();
return true;
}
private static bool Is(Object value, string type)
{
if (value != null)
{
Type il2CppType = value.GetIl2CppType();
return ((il2CppType != null) ? ((MemberInfo)il2CppType).Name : null) == type;
}
return false;
}
public static bool TryReadId(Il2CppReferenceArray<Object> payload, out uint entityId)
{
entityId = 0u;
if (payload != null && ((Il2CppArrayBase<Object>)(object)payload).Length >= 1 && ((Il2CppArrayBase<Object>)(object)payload)[0] != null)
{
Type il2CppType = ((Il2CppArrayBase<Object>)(object)payload)[0].GetIl2CppType();
if (!(((il2CppType != null) ? ((MemberInfo)il2CppType).Name : null) != "UInt32"))
{
entityId = ((Il2CppObjectBase)((Il2CppArrayBase<Object>)(object)payload)[0]).Unbox<uint>();
return true;
}
}
return false;
}
public static Il2CppReferenceArray<Object> TimePeriod(int period)
{
return new Il2CppReferenceArray<Object>(1L) { [0] = Box((byte)period, ClassOf<byte, Byte>()) };
}
public static Object Box(uint value)
{
return Box(value, ClassOf<uint, UInt32>());
}
public static Object Box(ulong value)
{
return Box(value, ClassOf<ulong, UInt64>());
}
public static Object Box(Vector3 value)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
return Box<Vector3>(value, ClassOf<Vector3, Vector3>());
}
private unsafe static Object Box<T>(T value, IntPtr type) where T : unmanaged
{
//IL_003a: Unknown result type (might be due to invalid IL or missing references)
//IL_0040: Expected O, but got Unknown
if (type == IntPtr.Zero)
{
throw new InvalidOperationException("The game has no class for " + typeof(T).Name);
}
return new Object(IL2CPP.il2cpp_value_box(type, (IntPtr)(&value)));
}
private static IntPtr ClassOf<TManaged, TGame>()
{
RuntimeHelpers.RunClassConstructor(typeof(TGame).TypeHandle);
IntPtr nativeClassPtr = Il2CppClassPointerStore<TManaged>.NativeClassPtr;
if (!(nativeClassPtr != IntPtr.Zero))
{
return Il2CppClassPointerStore<TGame>.NativeClassPtr;
}
return nativeClassPtr;
}
public static string Describe(Il2CppReferenceArray<Object> payload, int most = 12)
{
if (payload == null || ((Il2CppArrayBase<Object>)(object)payload).Length == 0)
{
return "no values";
}
List<string> list = new List<string>();
for (int i = 0; i < ((Il2CppArrayBase<Object>)(object)payload).Length && i < most; i++)
{
list.Add(DescribeValue(((Il2CppArrayBase<Object>)(object)payload)[i]));
}
if (((Il2CppArrayBase<Object>)(object)payload).Length > most)
{
list.Add($"... {((Il2CppArrayBase<Object>)(object)payload).Length - most} more");
}
return $"{((Il2CppArrayBase<Object>)(object)payload).Length} value(s): {string.Join(", ", list)}";
}
private static string DescribeValue(Object value)
{
//IL_033d: Unknown result type (might be due to invalid IL or missing references)
//IL_0342: Unknown result type (might be due to invalid IL or missing references)
//IL_0346: Unknown result type (might be due to invalid IL or missing references)
//IL_034b: Unknown result type (might be due to invalid IL or missing references)
//IL_0364: Unknown result type (might be due to invalid IL or missing references)
//IL_0382: Unknown result type (might be due to invalid IL or missing references)
//IL_03a0: Unknown result type (might be due to invalid IL or missing references)
//IL_042a: Unknown result type (might be due to invalid IL or missing references)
//IL_042f: Unknown result type (might be due to invalid IL or missing references)
//IL_0448: Unknown result type (might be due to invalid IL or missing references)
//IL_0467: Unknown result type (might be due to invalid IL or missing references)
//IL_0486: Unknown result type (might be due to invalid IL or missing references)
//IL_03cb: Unknown result type (might be due to invalid IL or missing references)
//IL_03d0: Unknown result type (might be due to invalid IL or missing references)
if (value == null)
{
return "null";
}
try
{
Type il2CppType = value.GetIl2CppType();
string text = ((il2CppType == (Type)null) ? "?" : ((MemberInfo)il2CppType).Name);
switch (text)
{
case "String":
{
string text2 = IL2CPP.Il2CppStringToManaged(((Il2CppObjectBase)value).Pointer) ?? string.Empty;
return (text2.Length > 160) ? ("\"" + text2.Substring(0, 160) + "...\"") : ("\"" + text2 + "\"");
}
case "Boolean":
return ((Il2CppObjectBase)value).Unbox<bool>() ? "true" : "false";
case "Int32":
return ((Il2CppObjectBase)value).Unbox<int>().ToString();
case "UInt32":
return ((Il2CppObjectBase)value).Unbox<uint>().ToString();
case "UInt64":
return ((Il2CppObjectBase)value).Unbox<ulong>().ToString();
case "Single":
return ((Il2CppObjectBase)value).Unbox<float>().ToString("0.###");
case "UInt16":
return ((Il2CppObjectBase)value).Unbox<ushort>().ToString();
case "Byte":
return ((Il2CppObjectBase)value).Unbox<byte>().ToString();
case "Char":
return "'" + ((Il2CppObjectBase)value).Unbox<char>() + "'";
case "Quaternion":
{
Quaternion val3 = ((Il2CppObjectBase)value).Unbox<Quaternion>();
Vector3 eulerAngles = ((Quaternion)(ref val3)).eulerAngles;
return $"rotation({eulerAngles.x:0.#}, {eulerAngles.y:0.#}, {eulerAngles.z:0.#})";
}
case "Vector2Int":
{
Vector2Int val2 = ((Il2CppObjectBase)value).Unbox<Vector2Int>();
return $"[{((Vector2Int)(ref val2)).x}, {((Vector2Int)(ref val2)).y}]";
}
case "Vector3":
{
Vector3 val = ((Il2CppObjectBase)value).Unbox<Vector3>();
return $"({val.x:0.##}, {val.y:0.##}, {val.z:0.##})";
}
case "Byte[]":
return $"{((Il2CppArrayBase<byte>)(object)new Il2CppStructArray<byte>(((Il2CppObjectBase)value).Pointer)).Length} bytes";
default:
return text;
}
}
catch (Exception ex)
{
return "? " + ex.GetType().Name;
}
}
}
public sealed class JoinCoordinator
{
public const double LoadingWarningSeconds = 120.0;
public const double LoadingGiveUpSeconds = 360.0;
public const int FlushPerFrame = 200;
public const double AppearingGraceSeconds = 3.0;
public const double HoldDelaySeconds = 2.0;
public const string EventPrefix = "TooLate.";
private readonly CatLogger _log;
private readonly TextCatalog _texts;
private readonly Dictionary<ulong, JoinTicket<Il2CppReferenceArray<Object>>> _tickets = new Dictionary<ulong, JoinTicket<Il2CppReferenceArray<Object>>>();
private readonly HashSet<ulong> _warned = new HashSet<ulong>();
private readonly StateLedger _state = new StateLedger();
private readonly LastingMessages<Il2CppReferenceArray<Object>> _lasting = new LastingMessages<Il2CppReferenceArray<Object>>();
private readonly CustomerLedger<Il2CppReferenceArray<Object>> _customers = new CustomerLedger<Il2CppReferenceArray<Object>>();
private readonly HoldLedger _holds = new HoldLedger();
private CodePatch _loading;
private CodePatch _limit;
private bool _loadingOpen;
private bool _startWritten;
private bool _inLevel;
private int _limitWritten = 4;
public JoinSettings Settings { get; set; }
public IReadOnlyCollection<JoinTicket<Il2CppReferenceArray<Object>>> Tickets => _tickets.Values;
public bool CanJoinInLevel
{
get
{
CodePatch loading = _loading;
if (loading != null)
{
return loading.IsFound;
}
return false;
}
}
public bool CanRaiseLimit
{
get
{
CodePatch limit = _limit;
if (limit != null)
{
return limit.IsFound;
}
return false;
}
}
public bool InLevel => _inLevel;
public JoinCoordinator(CatLogger log, TextCatalog texts)
{
_log = log;
_texts = texts;
}
public void UseCodePatches(CodePatch loading, CodePatch limit)
{
_loading = loading;
_limit = limit;
}
public void Update()
{
HostState state = ReadHost();
UpdateCodePatches(state);
if (!state.Hosting)
{
if (_tickets.Count > 0)
{
_tickets.Clear();
}
return;
}
foreach (JoinTicket<Il2CppReferenceArray<Object>> item in _tickets.Values.ToList())
{
if (item.FailReason != null)
{
Fail(item, item.FailReason);
continue;
}
if (item.Flushing)
{
Flush(item);
continue;
}
if (item.StateSent && !item.HoldsSent && FrameLoop.Realtime >= item.HoldsDueAt)
{
SendHolds(item);
}
switch (item.Stage)
{
case JoinStage.Waiting:
if (JoinGate.ReadyToStart(state, FrameLoop.Realtime - item.ConnectedAt))
{
Start(item);
}
else if (!item.WaitingNoticeShown && JoinGate.IsRecap(state.TimePeriod))
{
item.WaitingNoticeShown = true;
Notify("message.waiting", item.Name);
_log.Info(item.Name + " waits in the lobby until the day's results are over");
}
break;
case JoinStage.Appearing:
if (IsSpawned(item.ClientId) && FrameLoop.Realtime - item.StageSince > 3.0)
{
_log.Warning(item.Name + " appeared without the game sending them the identifiers again, Too Late sends them itself");
GameProtocol.Send(21, item.ClientId, GameProtocol.Identifiers(item.Places, NextIdentifier()));
StartFlush(item);
break;
}
goto case JoinStage.Loading;
case JoinStage.Loading:
if (item.Overflowed > 0)
{
Fail(item, $"more than {20000} game messages piled up while the player was loading");
}
else if (FrameLoop.Realtime - item.StageSince > 360.0)
{
Fail(item, $"the player did not finish loading in {360.0:0} seconds");
}
else if (FrameLoop.Realtime - item.StageSince > 120.0 && _warned.Add(item.ClientId))
{
_log.Warning($"{item.Name} has been loading for {120.0:0} seconds, {item.Held.Count} game message(s) wait for them");
}
break;
case JoinStage.Synced:
if (IsSpawned(item.ClientId))
{
item.MoveTo(JoinStage.Playing, FrameLoop.Realtime);
Notify("message.joined", item.Name);
Publish("Joined", item);
_log.Info(item.Name + " is in the game, " + Seconds(FrameLoop.Realtime - item.ConnectedAt) + " after connecting");
}
break;
}
}
}
public void OnClientConnected(ulong clientId)
{
if (ShouldTakeOver(clientId))
{
Ensure(clientId);
}
else if (GameProtocol.IsHosting && CatNetwork.IsRemotePlayer(clientId) && !_tickets.ContainsKey(clientId))
{
_log.Info($"Player {clientId} connected and joins the usual way: {(_inLevel ? "joining a game in progress is off in the settings" : "you are in the lobby")}");
}
}
public void OnClientDisconnected(ulong clientId)
{
_warned.Remove(clientId);
_holds.ForgetClient(clientId);
if (_tickets.Remove(clientId, out var value))
{
Publish("Left", value);
_log.Info(value.Name + " left while " + value.Stage.ToString().ToLowerInvariant());
}
}
public bool TakesOverSave(ulong clientId)
{
if (!ShouldTakeOver(clientId))
{
return false;
}
Ensure(clientId);
return true;
}
public void Observe(int code, Il2CppReferenceArray<Object> payload)
{
bool flag = !_inLevel;
if (!flag)
{
bool flag2;
switch (code)
{
case 17:
case 18:
case 38:
case 40:
case 43:
flag2 = true;
break;
default:
flag2 = false;
break;
}
flag = !flag2 && !IsHoldCode(code) && !LastingMessages<Object>.IsLasting(code);
}
if (flag || !GameProtocol.IsHosting)
{
return;
}
switch (code)
{
case 38:
{
if (GameProtocol.TryReadGeneric(payload, out var entityId3, out var text))
{
_state.Record(entityId3, text);
_customers.RecordLine(entityId3, text);
}
return;
}
case 17:
{
if (GameProtocol.TryReadSpawn(payload, out var entityId6, out var key))
{
_customers.RecordSpawn(entityId6, key, GameProtocol.Copy(payload));
}
return;
}
case 40:
{
if (GameProtocol.TryReadStored(payload, out var storeId, out var entityId5))
{
_customers.RecordStored(storeId, entityId5, GameProtocol.Copy(payload));
}
return;
}
case 43:
{
if (GameProtocol.TryReadId(payload, out var entityId4))
{
_customers.RecordProperties(entityId4, GameProtocol.Copy(payload));
}
return;
}
case 24:
case 32:
case 34:
{
if (GameProtocol.TryReadId(payload, out var entityId2))
{
RecordHold(code, GameProtocol.OwnClientId, entityId2);
}
return;
}
case 25:
case 33:
case 35:
{
if (GameProtocol.TryReadClientEntity(payload, out var clientId, out var entityId))
{
RecordHold(code, clientId, entityId);
}
return;
}
}
uint entityId8;
if (code != 18)
{
uint entityId7;
if (!LastingMessages<Object>.IsPerEntity(code))
{
_lasting.Record(code, 0u, GameProtocol.Copy(payload));
}
else if (GameProtocol.TryReadId(payload, out entityId7))
{
_lasting.Record(code, entityId7, GameProtocol.Copy(payload));
}
}
else if (GameProtocol.TryReadId(payload, out entityId8))
{
_state.Forget(entityId8);
_lasting.Forget(entityId8);
_customers.Forget(entityId8);
_holds.Forget(entityId8);
}
}
private static bool IsHoldCode(int code)
{
if ((uint)(code - 24) <= 1u || (uint)(code - 32) <= 3u)
{
return true;
}
return false;
}
private void RecordHold(int code, ulong clientId, uint entityId)
{
switch (code)
{
case 24:
case 25:
_holds.RecordAuthority(clientId, entityId);
break;
case 32:
case 33:
_holds.RecordPickUp(clientId, entityId);
break;
default:
_holds.RecordDrop(clientId, entityId);
break;
}
}
public bool RouteOutgoing(int code, bool reliable, ulong target, Il2CppReferenceArray<Object> payload)
{
if (_tickets.Count == 0 || !_tickets.TryGetValue(target, out var value))
{
return true;
}
switch (MessageRoute.For(value.Stage, code))
{
case Route.Pass:
return true;
case Route.Replace:
try
{
SendIdentifiers(value);
}
catch (Exception ex)
{
_log.Error("Sending the identifiers to " + value.Name + " failed", ex);
value.FailReason = "the identifiers of the snapshot could not be sent";
}
return false;
case Route.Hold:
case Route.HoldLatest:
value.Accept(code, reliable, GameProtocol.Copy(payload));
return false;
default:
value.Accept(code, reliable, null);
return false;
}
}
public void ShutDown()
{
_log.Warning("Too Late turned itself off: players who are still joining are disconnected, joining a level and the player limit are like in the game again");
foreach (JoinTicket<Il2CppReferenceArray<Object>> item in _tickets.Values.Where(delegate(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
JoinStage stage = ticket.Stage;
return (uint)stage <= 2u;
}).ToList())
{
GameProtocol.Disconnect(item.ClientId);
}
_tickets.Clear();
_warned.Clear();
CodePatch loading = _loading;
if (loading != null && loading.IsFound)
{
_loading.Restore();
}
loading = _limit;
if (loading != null && loading.IsFound)
{
_limit.Restore();
}
_loadingOpen = false;
_limitWritten = 4;
}
public void EnterLevel()
{
if (!_inLevel)
{
_inLevel = true;
_startWritten = false;
ClearState();
WriteStarted();
}
}
private void WriteStarted()
{
if (!_startWritten && _inLevel && GameProtocol.IsHosting)
{
_startWritten = true;
_log.Info(CatNetwork.IsWithoutSteamNetwork ? "Your game started without the Steam network, it runs on this computer only and nobody can join it" : "Your game started, players who connect from now on join the game in progress");
}
}
public void LeaveLevel(bool disconnectWaiting)
{
_inLevel = false;
ClearState();
Forget(disconnectWaiting);
}
private void Forget(bool disconnectWaiting)
{
if (disconnectWaiting)
{
foreach (JoinTicket<Il2CppReferenceArray<Object>> item in _tickets.Values.Where(delegate(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
JoinStage stage = ticket.Stage;
return (uint)stage <= 2u;
}).ToList())
{
_log.Info(item.Name + " is disconnected, the game restarts before they could join");
GameProtocol.Disconnect(item.ClientId);
}
}
_tickets.Clear();
_warned.Clear();
UpdateCodePatches(default(HostState));
}
public string Describe()
{
HostState hostState = ReadHost();
List<string> list = new List<string>();
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(152, 12);
defaultInterpolatedStringHandler.AppendLiteral("Hosting ");
defaultInterpolatedStringHandler.AppendFormatted(hostState.Hosting);
defaultInterpolatedStringHandler.AppendLiteral(", in a level ");
defaultInterpolatedStringHandler.AppendFormatted(_inLevel);
defaultInterpolatedStringHandler.AppendLiteral(", state messages ");
defaultInterpolatedStringHandler.AppendFormatted(_state.Count);
defaultInterpolatedStringHandler.AppendLiteral(" + ");
defaultInterpolatedStringHandler.AppendFormatted(_lasting.Count);
defaultInterpolatedStringHandler.AppendLiteral(", customers ");
defaultInterpolatedStringHandler.AppendFormatted(_customers.CustomerCount);
defaultInterpolatedStringHandler.AppendLiteral(" with ");
defaultInterpolatedStringHandler.AppendFormatted(_customers.Count);
defaultInterpolatedStringHandler.AppendLiteral(" entities, held ");
defaultInterpolatedStringHandler.AppendFormatted(_holds.HeldCount);
defaultInterpolatedStringHandler.AppendLiteral(", level ready ");
defaultInterpolatedStringHandler.AppendFormatted(hostState.LevelReady);
defaultInterpolatedStringHandler.AppendLiteral(", restarting ");
defaultInterpolatedStringHandler.AppendFormatted(hostState.Restarting);
defaultInterpolatedStringHandler.AppendLiteral(", time period ");
defaultInterpolatedStringHandler.AppendFormatted<GameTimePeriod>((GameTimePeriod)hostState.TimePeriod);
defaultInterpolatedStringHandler.AppendLiteral(", ");
defaultInterpolatedStringHandler.AppendLiteral("joining in a level ");
object value;
if (_loadingOpen)
{
CodePatch loading = _loading;
if (loading != null && loading.IsFound)
{
value = "open";
goto IL_0171;
}
}
value = "closed";
goto IL_0171;
IL_0171:
defaultInterpolatedStringHandler.AppendFormatted((string?)value);
defaultInterpolatedStringHandler.AppendLiteral(", player limit ");
defaultInterpolatedStringHandler.AppendFormatted(_limitWritten);
list.Add(defaultInterpolatedStringHandler.ToStringAndClear());
List<string> list2 = list;
list2.AddRange(_tickets.Values.Select((JoinTicket<Il2CppReferenceArray<Object>> ticket) => $"{ticket.Name} ({ticket.ClientId}): {ticket.Stage} for {Seconds(FrameLoop.Realtime - ticket.StageSince)}, held {ticket.Held.Count}, dropped {ticket.Dropped}, places {ticket.Places?.Count ?? 0}"));
if (_tickets.Count == 0)
{
list2.Add("Nobody is joining");
}
foreach (string item in list2)
{
_log.Info("[Joins] " + item);
}
return list2[0];
}
public string SnapshotProbe()
{
SnapshotResult snapshotResult = SaveSnapshot.Write();
string text = "Snapshot written to " + SaveSnapshot.Folder + ": " + snapshotResult;
_log.Info(text);
return text;
}
private bool ShouldTakeOver(ulong clientId)
{
if (_inLevel && Settings != null && Settings.Enabled.Value && GameProtocol.IsHosting)
{
return CatNetwork.IsRemotePlayer(clientId);
}
return false;
}
private void Ensure(ulong clientId)
{
if (!_tickets.ContainsKey(clientId))
{
JoinTicket<Il2CppReferenceArray<Object>> joinTicket = new JoinTicket<Il2CppReferenceArray<Object>>(clientId, CatNetwork.PlayerName(clientId), FrameLoop.Realtime);
_tickets[clientId] = joinTicket;
Notify("message.joining", joinTicket.Name);
Publish("Connected", joinTicket);
_log.Info($"{joinTicket.Name} ({clientId}) connected while you are in a level, they wait in the lobby until the game lets them in");
}
}
private void Start(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
try
{
SnapshotResult snapshotResult = SaveSnapshot.Write();
ticket.Places = snapshotResult.Places;
ticket.SpawnMark = _customers.Mark;
ticket.HoldMark = _holds.Mark;
ticket.MoveTo(JoinStage.Loading, FrameLoop.Realtime);
GameProtocol.SendSave(ticket.ClientId, snapshotResult.SaveName, snapshotResult.Data);
GameProtocol.Send(10, ticket.ClientId, GameProtocol.Empty());
Publish("Loading", ticket, snapshotResult.ToString());
_log.Info($"{ticket.Name} gets the warehouse as it is now and starts loading. Snapshot {snapshotResult}");
}
catch (Exception ex)
{
_log.Error("The snapshot for " + ticket.Name + " could not be made", ex);
Fail(ticket, "the snapshot of the game could not be made");
}
}
private void SendIdentifiers(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
if (ticket.Places == null)
{
ticket.Accept(21, reliable: true, null);
return;
}
if (!ticket.PlacesMerged)
{
HashSet<uint> saved = new HashSet<uint>(ticket.Places.Select((EntityPlace place) => place.Id));
ticket.Places = EntityPlaces.Merge(SceneEntities(), ticket.Places);
ticket.ScenePlaces = ticket.Places.Where((EntityPlace place) => !saved.Contains(place.Id)).ToList();
ticket.PlacesMerged = true;
}
IReadOnlyList<EntityPlace> readOnlyList = ((ticket.Stage == JoinStage.Loading) ? ticket.ScenePlaces : ticket.Places);
GameProtocol.Send(21, ticket.ClientId, GameProtocol.Identifiers(readOnlyList, NextIdentifier()));
if (ticket.Stage == JoinStage.Appearing && !ticket.Flushing)
{
_log.Info($"{ticket.Name} appeared and has the identifiers of the saved entities now ({ticket.Places.Count}), the state of the warehouse and {ticket.Held.Count} held message(s) follow");
StartFlush(ticket);
return;
}
if (ticket.Stage != JoinStage.Loading)
{
_log.Info($"Sent {ticket.Name} the identifiers of the snapshot again ({ticket.Places.Count})");
return;
}
string text = string.Join(", ", from message in ticket.Held
group message by message.Code into @group
orderby @group.Key
select $"{MessageRecorder.Name(@group.Key)} x{@group.Count()}");
GameProtocol.Send(11, ticket.ClientId, GameProtocol.Empty());
ticket.MoveTo(JoinStage.Appearing, FrameLoop.Realtime);
_log.Info($"{ticket.Name} loaded the level: sent the {readOnlyList.Count} identifiers of the level's own entities, like the game does at this point, and the go to finish loading; {ticket.Held.Count} held message(s) wait until they appear ({((text.Length == 0) ? "none" : text)}), {ticket.Dropped} were dropped");
}
private void StartFlush(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
SendState(ticket);
ticket.Flushing = true;
}
private void SendState(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_041a: Unknown result type (might be due to invalid IL or missing references)
if (ticket.StateSent)
{
return;
}
ticket.StateSent = true;
GameTimeManager val = (Singleton<GameTimeManager>.HasInstance() ? Singleton<GameTimeManager>.Instance : null);
int num = (((Object)(object)val == (Object)null) ? (-1) : ((int)val.CurrentTimePeriod));
if (num >= 0)
{
GameProtocol.Send(45, ticket.ClientId, GameProtocol.TimePeriod(num));
}
HashSet<uint> hashSet = new HashSet<uint>((ticket.Places ?? Array.Empty<EntityPlace>()).Select((EntityPlace place) => place.Id));
IReadOnlyList<ReplayMessage<Il2CppReferenceArray<Object>>> readOnlyList = _customers.Before(ticket.SpawnMark, hashSet.Contains, 17, 40, 43);
foreach (ReplayMessage<Il2CppReferenceArray<Object>> item in readOnlyList)
{
GameProtocol.Send(item.Code, ticket.ClientId, item.Payload);
if (item.Code == 17)
{
ticket.Replayed.Add(item.EntityId);
}
}
hashSet.UnionWith(ticket.Replayed);
List<uint> list = _customers.Customers().Where(ticket.Replayed.Contains).ToList();
List<CustomerLine> list2 = (from line in list.SelectMany((uint customer) => _customers.LinesOf(customer, 0L))
orderby line.Order
select line).ToList();
foreach (CustomerLine item2 in list2)
{
GameProtocol.Send(38, ticket.ClientId, GameProtocol.Generic(item2.CustomerId, item2.Text));
}
ticket.StateMark = _customers.Mark;
ticket.HoldsDueAt = FrameLoop.Realtime + 2.0;
IReadOnlyList<LastingMessage<Il2CppReferenceArray<Object>>> readOnlyList2 = _lasting.For(hashSet.Contains);
foreach (LastingMessage<Il2CppReferenceArray<Object>> item3 in readOnlyList2)
{
GameProtocol.Send(item3.Code, ticket.ClientId, item3.Payload);
}
IReadOnlyList<StateMessage> readOnlyList3 = _state.For(hashSet.Contains);
foreach (StateMessage item4 in readOnlyList3)
{
GameProtocol.Send(38, ticket.ClientId, GameProtocol.Generic(item4.EntityId, item4.Text));
}
string text = string.Join(", ", from message in readOnlyList3
group message by message.Kind into @group
orderby @group.Count() descending, @group.Key
select $"{@group.Key} x{@group.Count()}");
string text2 = string.Join(", ", from message in readOnlyList2
group message by message.Code into @group
orderby @group.Key
select $"{MessageRecorder.Name(@group.Key)} x{@group.Count()}");
_log.Info($"Sent {ticket.Name} the time of day ({((num >= 0) ? ((object)(GameTimePeriod)num/*cast due to .constrained prefix*/).ToString() : "unknown")}) and the state of the warehouse the game sent before they joined: {readOnlyList2.Count} of {_lasting.Count} recorded game message(s){((text2.Length == 0) ? string.Empty : (" (" + text2 + ")"))}, {readOnlyList3.Count} of {_state.Count} entity message(s)" + ((text.Length == 0) ? string.Empty : (" (" + text + ")")));
if (ticket.Replayed.Count > 0 || _customers.CustomerCount > 0)
{
_log.Info($"Sent {ticket.Name} {list.Count} customer(s) waiting at a counter: {ticket.Replayed.Count} entities in {readOnlyList.Count} game message(s) and {list2.Count} customer line(s){((_customers.CustomerCount > list.Count) ? $", {_customers.CustomerCount - list.Count} customer(s) came while they were loading and follow with the held messages" : string.Empty)}");
}
}
private void SendHolds(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
ticket.HoldsSent = true;
HashSet<uint> hashSet = new HashSet<uint>((ticket.Places ?? Array.Empty<EntityPlace>()).Select((EntityPlace place) => place.Id));
hashSet.UnionWith(ticket.Replayed);
IReadOnlyList<HoldMessage> readOnlyList = _holds.Before(ticket.HoldMark, hashSet.Contains, (ulong client) => client != ticket.ClientId && GameProtocol.IsPresent(client));
foreach (HoldMessage item in readOnlyList)
{
GameProtocol.Send((item.Kind == HoldKind.Authority) ? 25 : 33, ticket.ClientId, GameProtocol.ClientEntity(item.ClientId, item.EntityId));
}
int num = readOnlyList.Count((HoldMessage hold) => hold.Kind == HoldKind.PickUp);
if (readOnlyList.Count > 0)
{
_log.Info($"Sent {ticket.Name} what {num} player(s) still hold since before the snapshot and who moves {readOnlyList.Count - num} entities, now that the other players stand in their game");
}
}
private void SendLateCustomers(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
List<CustomerLine> list = new List<CustomerLine>();
foreach (uint item in _customers.Customers())
{
list.AddRange(_customers.LinesOf(item, ticket.Replayed.Contains(item) ? ticket.StateMark : 0));
}
foreach (CustomerLine item2 in list.OrderBy((CustomerLine line) => line.Order))
{
GameProtocol.Send(38, ticket.ClientId, GameProtocol.Generic(item2.CustomerId, item2.Text));
}
HashSet<uint> hashSet = new HashSet<uint>(_customers.MembersSince(ticket.SpawnMark));
List<StateMessage> list2 = ((hashSet.Count == 0) ? new List<StateMessage>() : _state.For(hashSet.Contains).ToList());
foreach (StateMessage item3 in list2)
{
GameProtocol.Send(38, ticket.ClientId, GameProtocol.Generic(item3.EntityId, item3.Text));
}
if (list.Count > 0 || list2.Count > 0)
{
_log.Info($"Sent {ticket.Name} {list.Count} customer line(s) and {list2.Count} entity message(s) of customers and parcels that changed while they were loading");
}
}
private void Flush(JoinTicket<Il2CppReferenceArray<Object>> ticket)
{
foreach (HeldMessage<Il2CppReferenceArray<Object>> item in ticket.TakeHeld(200))
{
GameProtocol.Send(item.Code, ticket.ClientId, item.Payload, item.Reliable);
}
if (ticket.Held.Count <= 0)
{
ticket.Flushing = false;
SendLateCustomers(ticket);
ticket.MoveTo(JoinStage.Synced, FrameLoop.Realtime);
Publish("Synced", ticket, $"places={ticket.Places.Count} dropped={ticket.Dropped}");
_log.Info(ticket.Name + " got every held message, the game goes on for them as for everyone");
}
}
private void ClearState()
{
_state.Clear();
_lasting.Clear();
_customers.Clear();
_holds.Clear();
}
private static List<EntityPlace> SceneEntities()
{
//IL_0071: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
//IL_007f: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
List<EntityPlace> list = new List<EntityPlace>();
NetworkedEntityManager obj = (Singleton<NetworkedEntityManager>.HasInstance() ? Singleton<NetworkedEntityManager>.Instance : null);
List<EntityNetwork> val = ((obj != null) ? obj._initialNetworkedEntities : null);
if (val == null)
{
return list;
}
for (int i = 0; i < val.Count; i++)
{
EntityNetwork val2 = val[i];
if (!((Object)(object)val2 == (Object)null) && !((Il2CppObjectBase)val2).WasCollected && Object.op_Implicit((Object)(object)val2))
{
Entity linkedEntity = val2.LinkedEntity;
if (!((Object)(object)linkedEntity != (Object)null) || ((Il2CppObjectBase)linkedEntity).WasCollected || !linkedEntity.IsDisposed)
{
Vector3 initialPosition = val2.InitialPosition;
list.Add(new EntityPlace(val2.NetworkIdentifier, initialPosition.x, initialPosition.y, initialPosition.z));
}
}
}
return list;
}
private static uint NextIdentifier()
{
if (!Singleton<NetworkedEntityManager>.HasInstance())
{
return 0u;
}
return Singleton<NetworkedEntityManager>.Instance._nextIdentifier;
}
private void Fail(JoinTicket<Il2CppReferenceArray<Object>> ticket, string reason)
{
_tickets.Remove(ticket.ClientId);
Notify("message.failed", ticket.Name);
Publish("Failed", ticket, reason);
_log.Error(ticket.Name + " cannot join: " + reason + "; they are disconnected");
GameProtocol.Disconnect(ticket.ClientId);
}
private void UpdateCodePatches(HostState state)
{
WriteStarted();
int num;
int num2;
if (Settings != null)
{
num = (Settings.Enabled.Value ? 1 : 0);
if (num != 0)
{
num2 = (JoinGate.CanAccept(state) ? 1 : 0);
goto IL_002d;
}
}
else
{
num = 0;
}
num2 = 0;
goto IL_002d;
IL_002d:
bool flag = (byte)num2 != 0;
CodePatch loading = _loading;
if (loading != null && loading.IsFound && flag != _loadingOpen && (flag ? _loading.Write(PlayerLimit.SkipJump) : _loading.Restore()))
{
_loadingOpen = flag;
_log.Info((!flag) ? "Joining your level is closed, like in the game" : (CatNetwork.IsWithoutSteamNetwork ? "Joining your level is open, but without the Steam network nobody can reach it" : "Players can join your level now"));
}
int num3 = ((num != 0 && Settings != null) ? PlayerLimit.Clamp(Settings.MaxPlayers.Value) : 4);
loading = _limit;
if (loading != null && loading.IsFound && num3 != _limitWritten && ((num3 == 4) ? _limit.Restore() : _limit.Write(new byte[1] { PlayerLimit.ConnectionLimit(num3) })))
{
_limitWritten = num3;
_log.Info($"Up to {num3} players can be in your game");
}
}
private static HostState ReadHost()
{
//IL_0053: Unknown result type (might be due to invalid IL or missing references)
BootstrapManager val = (Singleton<BootstrapManager>.HasInstance() ? Singleton<BootstrapManager>.Instance : null);
GameTimeManager val2 = (Singleton<GameTimeManager>.HasInstance() ? Singleton<GameTimeManager>.Instance : null);
return new HostState(GameProtocol.IsHosting, (Object)(object)val != (Object)null && val.LoadFinalized, (Object)(object)val != (Object)null && val.IsRestarting, ((Object)(object)val2 == (Object)null) ? (-1) : ((int)val2.CurrentTimePeriod));
}
private static bool IsSpawned(ulong clientId)
{
PlayerManager val = (Singleton<PlayerManager>.HasInstance() ? Singleton<PlayerManager>.Instance : null);
PlayerEntityBase val2 = default(PlayerEntityBase);
if ((Object)(object)val != (Object)null && val.TryGetPlayer(clientId, ref val2))
{
return (Object)(object)val2 != (Object)null;
}
return false;
}
private void Notify(string key, string name)
{
if (Settings == null || Settings.Messages.Value)
{
Notifications.Show(_texts.Format(key, new object[1] { name }), (string)null);
}
}
private static void Publish(string step, JoinTicket<Il2CppReferenceArray<Object>> ticket, string details = null)
{
GameEventStream.Publish("TooLate." + step, $"client={ticket.ClientId}" + (string.IsNullOrEmpty(details) ? string.Empty : (" " + details)));
}
private static string Seconds(double seconds)
{
return seconds.ToString("0.0", CultureInfo.InvariantCulture) + " s";
}
}
public sealed class LobbyOverflow
{
public const string LabelName = "text_TooLateMorePlayers";
public const float Gap = 24f;
public const float LabelWidth = 260f;
public const float LabelHeight = 60f;
private readonly CatLogger _log;
private readonly TextCatalog _texts;
private readonly List<ulong> _hidden = new List<ulong>();
private IntPtr _lobby;
private TMP_Text _label;
public IReadOnlyList<ulong> Hidden => _hidden;
public LobbyOverflow(CatLogger log, TextCatalog texts)
{
_log = log;
_texts = texts;
}
public void OnJoined(MultiplayerLobbyInterface lobby, ulong steamId)
{
Track(lobby);
if (steamId == 0L || HasSlot(lobby, steamId) || _hidden.Contains(steamId))
{
Refresh(lobby);
return;
}
_hidden.Add(steamId);
_log.Info($"[Lobby] {steamId} has no card in the lobby, {_hidden.Count} player(s) shown as a count");
Refresh(lobby);
}
public void OnLeft(MultiplayerLobbyInterface lobby, ulong steamId)
{
Track(lobby);
_hidden.Remove(steamId);
if (_hidden.Count > 0 && HasEmptySlot(lobby))
{
ulong num = _hidden[0];
_hidden.RemoveAt(0);
lobby.ProcessClientJoin(num);
}
Refresh(lobby);
}
public void Forget()
{
_hidden.Clear();
_lobby = IntPtr.Zero;
_label = null;
}
private void Track(MultiplayerLobbyInterface lobby)
{
if ((Object)(object)lobby != (Object)null && ((Il2CppObjectBase)lobby).Pointer != _lobby)
{
Forget();
_lobby = ((Il2CppObjectBase)lobby).Pointer;
}
}
private static bool HasSlot(MultiplayerLobbyInterface lobby, ulong steamId)
{
List<ConnectedPlayerHelper> playerSlots = lobby._playerSlots;
int num = 0;
while (playerSlots != null && num < playerSlots.Count)
{
ConnectedPlayerHelper val = playerSlots[num];
if ((Object)(object)val != (Object)null && !val.IsEmpty && val.SteamId == steamId)
{
return true;
}
num++;
}
return false;
}
private static bool HasEmptySlot(MultiplayerLobbyInterface lobby)
{
List<ConnectedPlayerHelper> playerSlots = lobby._playerSlots;
int num = 0;
while (playerSlots != null && num < playerSlots.Count)
{
if ((Object)(object)playerSlots[num] != (Object)null && playerSlots[num].IsEmpty)
{
return true;
}
num++;
}
return false;
}
private void Refresh(MultiplayerLobbyInterface lobby)
{
if (_hidden.Count == 0)
{
if ((Object)(object)_label != (Object)null && !((Il2CppObjectBase)_label).WasCollected)
{
((Component)_label).gameObject.SetActive(false);
}
return;
}
TMP_Text val = Label(lobby);
if (!((Object)(object)val == (Object)null))
{
val.text = _texts.Plural("lobby.more", (long)_hidden.Count, new object[1] { _hidden.Count });
((Component)val).gameObject.SetActive(true);
}
}
private TMP_Text Label(MultiplayerLobbyInterface lobby)
{
//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
//IL_00da: Unknown result type (might be due to invalid IL or missing references)
//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
//IL_010e: Unknown result type (might be due to invalid IL or missing references)
//IL_0116: Unknown result type (might be due to invalid IL or missing references)
//IL_011b: Unknown result type (might be due to invalid IL or missing references)
//IL_0125: Unknown result type (might be due to invalid IL or missing references)
//IL_0130: Unknown result type (might be due to invalid IL or missing references)
//IL_013f: Unknown result type (might be due to invalid IL or missing references)
//IL_0144: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)_label != (Object)null && !((Il2CppObjectBase)_label).WasCollected)
{
return _label;
}
HorizontalLayoutGroup connectedPlayerParent = lobby.ConnectedPlayerParent;
TMP_Text waitingForHostText = lobby.WaitingForHostText;
if ((Object)(object)connectedPlayerParent == (Object)null || (Object)(object)waitingForHostText == (Object)null)
{
_log.Warning("[Lobby] The lobby has no player row or text to copy, the count of more players is not shown");
return null;
}
RectTransform component = ((Component)connectedPlayerParent).GetComponent<RectTransform>();
GameObject val = Object.Instantiate<GameObject>(((Component)waitingForHostText).gameObject, ((Transform)component).parent, false);
((Object)val).name = "text_TooLateMorePlayers";
_label = val.GetComponent<TMP_Text>();
_label.enableAutoSizing = false;
_label.alignment = (TextAlignmentOptions)4097;
_label.textWrappingMode = (TextWrappingModes)0;
RectTransform rectTransform = _label.rectTransform;
Vector2 anchorMin = (rectTransform.anchorMax = component.anchorMin);
rectTransform.anchorMin = anchorMin;
rectTransform.pivot = new Vector2(0f, 0.5f);
rectTransform.sizeDelta = new Vector2(260f, 60f);
Il2CppStructArray<Vector3> val2 = new Il2CppStructArray<Vector3>(4L);
component.GetWorldCorners(val2);
((Transform)rectTransform).position = (((Il2CppArrayBase<Vector3>)(object)val2)[2] + ((Il2CppArrayBase<Vector3>)(object)val2)[3]) * 0.5f;
rectTransform.anchoredPosition += new Vector2(24f, 0f);
return _label;
}
}
public sealed class SnapshotResult
{
public string SaveName { get; set; }
public byte[] Data { get; set; }
public IReadOnlyList<EntityPlace> Places { get; set; }
public int Stamps { get; set; }
public bool FromScratch { get; set; }
public long Milliseconds { get; set; }
public override string ToString()
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(33, 6);
defaultInterpolatedStringHandler.AppendFormatted(SaveName);
defaultInterpolatedStringHandler.AppendLiteral(": ");
byte[] data = Data;
defaultInterpolatedStringHandler.AppendFormatted((data != null) ? data.Length : 0);
defaultInterpolatedStringHandler.AppendLiteral(" bytes, ");
defaultInterpolatedStringHandler.AppendFormatted(Places?.Count ?? 0);
defaultInterpolatedStringHandler.AppendLiteral(" entities, ");
defaultInterpolatedStringHandler.AppendFormatted(Stamps);
defaultInterpolatedStringHandler.AppendLiteral(" stamps");
defaultInterpolatedStringHandler.AppendFormatted(FromScratch ? ", built without a save file" : string.Empty);
defaultInterpolatedStringHandler.AppendLiteral(", ");
defaultInterpolatedStringHandler.AppendFormatted(Milliseconds);
defaultInterpolatedStringHandler.AppendLiteral(" ms");
return defaultInterpolatedStringHandler.ToStringAndClear();
}
}
public static class SaveSnapshot
{
public const string FolderName = "TooLate";
public const string FileName = "snapshot.bin";
public static readonly string[] CopiedTables = new string[3] { "Metadata", "Generic", "ProgressionGeneric" };
public static string Folder => Path.Combine(Paths.CachePath, "TooLate");
public static SnapshotResult Write()
{
using (CrashTrail.During("Too Late writes the snapshot of the warehouse for a joining player"))
{
return WriteNow();
}
}
private static SnapshotResult WriteNow()
{
//IL_009c: Unknown result type (might be due to invalid IL or missing references)
//IL_00a3: Expected O, but got Unknown
Stopwatch stopwatch = Stopwatch.StartNew();
SaveManager val = (Singleton<SaveManager>.HasInstance() ? Singleton<SaveManager>.Instance : null);
if ((Object)(object)val == (Object)null)
{
throw new InvalidOperationException("The game has no save manager");
}
string currentSaveFileName = val._currentSaveFileName;
if (string.IsNullOrEmpty(currentSaveFileName))
{
throw new InvalidOperationException("The game has no current save");
}
Directory.CreateDirectory(Folder);
string text = Path.Combine(Folder, "snapshot.bin");
string gameSaveFullPath = val.GetGameSaveFullPath(currentSaveFileName);
SqliteConnection.ClearAllPools();
if (File.Exists(text))
{
File.Delete(text);
}
bool flag = !File.Exists(gameSaveFullPath);
List<EntityPlace> list = new List<EntityPlace>();
int stamps = 0;
SqliteConnection val2 = new SqliteConnection("Data Source=" + text);
try
{
((DbConnection)val2).Open();
SqliteCommand command = val2.CreateCommand();
CreateTables(command);
if (flag)
{
AddMetadata(command, val);
}
else
{
CopyProgress(command, gameSaveFullPath);
}
SqliteTransaction val3 = val2.BeginTransaction();
SqliteCommand val4 = val2.CreateCommand();
val4.Transaction = val3;
if (!val.ExecuteSaveEntities(val4))
{
throw new InvalidOperationException("The game could not write its entities into the snapshot");
}
if (!val.ExecuteSaveStamps(val4))
{
throw new InvalidOperationException("The game could not write its stamps into the snapshot");
}
if (!val.ExecuteEnqueuedGenericSaveData(val4))
{
throw new InvalidOperationException("The game could not write its progress into the snapshot");
}
((DbTransaction)val3).Commit();
SqliteCommand val5 = val2.CreateCommand();
((DbCommand)val5).CommandText = "SELECT Id, PositionX, PositionY, PositionZ FROM Entity";
SqliteDataReader val6 = val5.ExecuteReader();
while (((DbDataReader)val6).Read())
{
if (!((DbDataReader)val6).IsDBNull(0))
{
list.Add(new EntityPlace((uint)Number(((DbDataReader)val6).GetValue(0)), (float)Number(((DbDataReader)val6).GetValue(1)), (float)Number(((DbDataReader)val6).GetValue(2)), (float)Number(((DbDataReader)val6).GetValue(3))));
}
}
((DbDataReader)val6).Close();
((DbCommand)val5).CommandText = "SELECT COUNT(*) FROM Stamps";
SqliteDataReader val7 = val5.ExecuteReader();
if (((DbDataReader)val7).Read())
{
stamps = (int)Number(((DbDataReader)val7).GetValue(0));
}
((DbDataReader)val7).Close();
}
finally
{
((DbConnection)val2).Close();
SqliteConnection.ClearAllPools();
}
return new SnapshotResult
{
SaveName = currentSaveFileName,
Data = File.ReadAllBytes(text),
Places = list,
Stamps = stamps,
FromScratch = flag,
Milliseconds = stopwatch.ElapsedMilliseconds
};
}
private static void CreateTables(SqliteCommand command)
{
Execute(command, SaveManager.CreateMetadataTable);
Execute(command, SaveManager.CreateEntityTable);
Execute(command, SaveManager.CreateStampsTable);
Execute(command, SaveManager.CreateGameGenericTable);
Execute(command, SaveManager.CreateGameProgressionGenericTable);
}
private static void CopyProgress(SqliteCommand command, string source)
{
Execute(command, "ATTACH DATABASE '" + Quote(source) + "' AS original");
try
{
Execute(command, "INSERT INTO Metadata (CreationGameVersion, SaveName) SELECT CreationGameVersion, SaveName FROM original.Metadata");
Execute(command, "INSERT INTO Generic (Key, ValueNumeric, ValueString, ValueBlob) SELECT Key, ValueNumeric, ValueString, ValueBlob FROM original.Generic");
Execute(command, "INSERT INTO ProgressionGeneric (Key, ValueNumeric, ValueString, ValueBlob) SELECT Key, ValueNumeric, ValueString, ValueBlob FROM original.ProgressionGeneric");
}
finally
{
Execute(command, "DETACH DATABASE original");
}
}
private static void AddMetadata(SqliteCommand command, SaveManager save)
{
string text = (Singleton<BootstrapManager>.HasInstance() ? Singleton<BootstrapManager>.Instance.GameVersion : string.Empty);
((DbCommand)command).CommandText = "INSERT INTO Metadata (CreationGameVersion, SaveName) VALUES ('" + Quote(text) + "', '" + Quote(save._currentSaveDisplayName) + "')";
((DbCommand)command).ExecuteNonQuery();
}
private static void Execute(SqliteCommand command, string text)
{
((DbCommand)command).CommandText = text;
((DbCommand)command).ExecuteNonQuery();
}
private static string Quote(string text)
{
return (text ?? string.Empty).Replace("'", "''");
}
private static double Number(Object value)
{
if (value == null)
{
return 0.0;
}
Type il2CppType = value.GetIl2CppType();
return ((il2CppType != null) ? ((MemberInfo)il2CppType).Name : null) switch
{
"DBNull" => 0.0,
"Int64" => ((Il2CppObjectBase)value).Unbox<long>(),
"Int32" => ((Il2CppObjectBase)value).Unbox<int>(),
"Double" => ((Il2CppObjectBase)value).Unbox<double>(),
"Single" => ((Il2CppObjectBase)value).Unbox<float>(),
_ => double.Parse(value.ToString(), CultureInfo.InvariantCulture),
};
}
}
public static class SpawnPoints
{
private static CatLogger _log;
private static IntPtr _extended;
public static void Initialize(CatLogger log)
{
_log = log;
}
public static void Ensure(PlayerManager manager)
{
//IL_006f: Unknown result type (might be due to invalid IL or missing references)
//IL_0074: Unknown result type (might be due to invalid IL or missing references)
//IL_0077: Unknown result type (might be due to invalid IL or missing references)
//IL_007e: Unknown result type (might be due to invalid IL or missing references)
//IL_0085: Unknown result type (might be due to invalid IL or missing references)
//IL_0153: Unknown result type (might be due to invalid IL or missing references)
//IL_0180: Unknown result type (might be due to invalid IL or missing references)
//IL_018e: Unknown result type (might be due to invalid IL or missing references)
//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)manager == (Object)null || ((Il2CppObjectBase)manager).Pointer == _extended)
{
return;
}
Il2CppReferenceArray<Transform> spawnPoints = manager._spawnPoints;
if (spawnPoints == null || ((Il2CppArrayBase<Transform>)(object)spawnPoints).Length < 2)
{
return;
}
if (((Il2CppArrayBase<Transform>)(object)spawnPoints).Length >= 8)
{
_extended = ((Il2CppObjectBase)manager).Pointer;
return;
}
List<(float, float, float)> list = new List<(float, float, float)>();
foreach (Transform item in (Il2CppArrayBase<Transform>)(object)spawnPoints)
{
if ((Object)(object)item == (Object)null)
{
return;
}
Vector3 position = item.position;
list.Add((position.x, position.y, position.z));
}
IReadOnlyList<SpawnSpot> readOnlyList = SpawnSpots.Extra(list, 8);
Il2CppReferenceArray<Transform> val = new Il2CppReferenceArray<Transform>((long)(list.Count + readOnlyList.Count));
for (int i = 0; i < ((Il2CppArrayBase<Transform>)(object)spawnPoints).Length; i++)
{
((Il2CppArrayBase<Transform>)(object)val)[i] = ((Il2CppArrayBase<Transform>)(object)spawnPoints)[i];
}
Transform parent = ((Il2CppArrayBase<Transform>)(object)spawnPoints)[0].parent;
List<string> list2 = new List<string>();
for (int j = 0; j < readOnlyList.Count; j++)
{
SpawnSpot spawnSpot = readOnlyList[j];
Transform val2 = ((Il2CppArrayBase<Transform>)(object)spawnPoints)[spawnSpot.Nearest];
Transform transform = new GameObject($"TooLate spawn point {list.Count + j + 1}").transform;
transform.SetParent(parent, false);
transform.position = new Vector3(spawnSpot.X, spawnSpot.Y, spawnSpot.Z);
transform.rotation = val2.rotation;
((Il2CppArrayBase<Transform>)(object)val)[list.Count + j] = transform;
list2.Add($"{list.Count + j + 1} at {transform.position}");
}
manager._spawnPoints = val;
_extended = ((Il2CppObjectBase)manager).Pointer;
CatLogger log = _log;
if (log != null)
{
log.Info($"Spawn points for players {list.Count + 1} to {((Il2CppArrayBase<Transform>)(object)val).Length} added between the game's {list.Count}: {string.Join(", ", list2)}");
}
}
}
}
namespace TooLate.Research
{
public sealed class MessageRecorder
{
public const double SummarySeconds = 5.0;
private readonly CatLogger _log;
private readonly Dictionary<string, int> _quiet = new Dictionary<string, int>(StringComparer.Ordinal);
private double _summaryAt;
public bool IsEnabled { get; private set; }
public MessageRecorder(CatLogger log)
{
_log = log;
}
public void SetEnabled(bool enabled)
{
if (IsEnabled != enabled)
{
if (!enabled)
{
Summarize();
}
IsEnabled = enabled;
_summaryAt = FrameLoop.Realtime + 5.0;
_log.Info(enabled ? "[Messages] Recording the game's network messages: every message once, moving and animation messages as counts every 5 seconds" : "[Messages] Recording stopped");
}
}
public void HostSent(int code, bool reliable, ulong target, Il2CppReferenceArray<Object> payload)
{
Record($"host -> {target}", code, reliable, payload);
}
public void ClientSent(int code, bool reliable, Il2CppReferenceArray<Object> payload)
{
Record("client -> host", code, reliable, payload);
}
public void Update()
{
if (IsEnabled && FrameLoop.Realtime >= _summaryAt)
{
Summarize();
_summaryAt = FrameLoop.Realtime + 5.0;
}
}
private void Record(string direction, int code, bool reliable, Il2CppReferenceArray<Object> payload)
{
if (MessageRoute.IsContinuous(code) || code == 44)
{
string key = direction + " " + Name(code);
_quiet[key] = ((!_quiet.TryGetValue(key, out var value)) ? 1 : (value + 1));
return;
}
_log.Info(string.Format(CultureInfo.InvariantCulture, "[Messages] {0:0.000} f{1} {2}: {3} ({4}){5}, {6}", FrameLoop.Realtime, FrameLoop.FrameCount, direction, Name(code), code, reliable ? string.Empty : " unreliable", GameProtocol.Describe(payload)));
}
private void Summarize()
{
if (_quiet.Count != 0)
{
_log.Info("[Messages] Last seconds: " + string.Join(", ", from pair in _quiet.OrderBy<KeyValuePair<string, int>, string>((KeyValuePair<string, int> pair) => pair.Key, StringComparer.Ordinal)
select $"{pair.Key} x{pair.Value}"));
_quiet.Clear();
}
}
public static string Name(int code)
{
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
if (!Enum.IsDefined(typeof(ProtocolCode), code))
{
return "Code" + code;
}
return ((object)(ProtocolCode)code/*cast due to .constrained prefix*/).ToString();
}
}
}
namespace TooLate.Patches
{
public static class TooLatePatches
{
private static JoinCoordinator _joins;
private static LobbyOverflow _lobby;
private static MessageRecorder _recorder;
public static CatPatches Group { get; private set; }
public static CatPatches Install(string ownerId, JoinCoordinator joins, LobbyOverflow lobby, MessageRecorder recorder, CatLogger log)
{
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
_joins = joins;
_lobby = lobby;
_recorder = recorder;
Group = new CatPatches(ownerId, log).Prefix(typeof(Server), "SendMessage", new Type[4]
{
typeof(ProtocolCode),
typeof(bool),
typeof(ulong),
typeof(Il2CppReferenceArray<Object>)
}, typeof(TooLatePatches), "ServerSendPrefix").Prefix(typeof(Server), "BroadcastMessage", new Type[4]
{
typeof(ProtocolCode),
typeof(bool),
typeof(Nullable<ulong>),
typeof(Il2CppReferenceArray<Object>)
}, typeof(TooLatePatches), "ServerBroadcastPrefix").Prefix(typeof(Client), "SendMessage", new Type[3]
{
typeof(ProtocolCode),
typeof(bool),
typeof(Il2CppReferenceArray<Object>)
}, typeof(TooLatePatches), "ClientSendPrefix")
.Prefix(typeof(SaveManager), "NetworkManager_OnClientConnected", new Type[1] { typeof(ulong) }, typeof(TooLatePatches), "SaveOnConnectPrefix")
.Postfix(typeof(MultiplayerLobbyInterface), "ProcessClientJoin", new Type[1] { typeof(ulong) }, typeof(TooLatePatches), "LobbyJoinPostfix")
.Postfix(typeof(MultiplayerLobbyInterface), "NetworkManager_OnClientDisconnected", new Type[1] { typeof(ulong) }, typeof(TooLatePatches), "LobbyLeavePostfix")
.Prefix(typeof(PlayerManager), "SpawnLocalPlayer", Type.EmptyTypes, typeof(TooLatePatches), "SpawnPointsPrefix")
.Prefix(typeof(PlayerManager), "GetPlayerSpawnPoint", new Type[1] { typeof(uint) }, typeof(TooLatePatches), "SpawnPointsPrefix");
Group.Apply();
return Group;
}
private static void SpawnPointsPrefix(PlayerManager __instance)
{
if (Group == null || !Group.IsActive)
{
return;
}
try
{
SpawnPoints.Ensure(__instance);
}
catch (Exception ex)
{
Group.Fault("SpawnPointsPrefix", ex);
}
}
private static bool ServerSendPrefix(ProtocolCode __0, bool __1, ulong __2, Il2CppReferenceArray<Object> __3)
{
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_0018: Expected I4, but got Unknown
if (Group == null || !Group.IsActive)
{
return true;
}
try
{
int code = (int)__0;
if (_recorder.IsEnabled)
{
_recorder.HostSent(code, __1, __2, __3);
}
if (GameProtocol.IsSending)
{
return true;
}
_joins.Observe(code, __3);
return _joins.RouteOutgoing(code, __1, __2, __3);
}
catch (Exception ex)
{
Group.Fault("ServerSendPrefix", ex);
return true;
}
}
private static void ServerBroadcastPrefix(ProtocolCode __0, Il2CppReferenceArray<Object> __3)
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0028: Expected I4, but got Unknown
if (Group == null || !Group.IsActive || GameProtocol.IsSending)
{
return;
}
try
{
_joins.Observe((int)__0, __3);
}
catch (Exception ex)
{
Group.Fault("ServerBroadcastPrefix", ex);
}
}
private static void ClientSendPrefix(ProtocolCode __0, bool __1, Il2CppReferenceArray<Object> __2)
{
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
//IL_003a: Expected I4, but got Unknown
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
//IL_002e: Expected I4, but got Unknown
if (Group == null || !Group.IsActive)
{
return;
}
try
{
if (_recorder.IsEnabled)
{
_recorder.ClientSent((int)__0, __1, __2);
}
_joins.Observe((int)__0, __2);
}
catch (Exception ex)
{
Group.Fault("ClientSendPrefix", ex);
}
}
private static bool SaveOnConnectPrefix(ulong __0)
{
if (Group == null || !Group.IsActive)
{
return true;
}
try
{
return !_joins.TakesOverSave(__0);
}
catch (Exception ex)
{
Group.Fault("SaveOnConnectPrefix", ex);
return true;
}
}
private static void LobbyJoinPostfix(MultiplayerLobbyInterface __instance, ulong __0)
{
if (Group == null || !Group.IsActive)
{
return;
}
try
{
_lobby.OnJoined(__instance, __0);
}
catch (Exception ex)
{
Group.Fault("LobbyJoinPostfix", ex);
}
}
private static void LobbyLeavePostfix(MultiplayerLobbyInterface __instance, ulong __0)
{
if (Group == null || !Group.IsActive)
{
return;
}
try
{
_lobby.OnLeft(__instance, __0);
}
catch (Exception ex)
{
Group.Fault("LobbyLeavePostfix", ex);
}
}
}
}
namespace TooLate.Logic
{
public readonly struct ReplayMessage<T>
{
public long Order { get; }
public int Code { get; }
public uint EntityId { get; }
public T Payload { get; }
public ReplayMessage(long order, int code, uint entityId, T payload)
{
Order = order;
Code = code;
EntityId = entityId;
Payload = payload;
}
}
public readonly struct CustomerLine
{
public long Order { get; }
public uint CustomerId { get; }
public string Text { get; }
public CustomerLine(long order, uint customerId, string text)
{
Order = order;
CustomerId = customerId;
Text = text;
}
}
public sealed class CustomerLedger<T>
{
public enum Slot
{
None,
Appearance,
Request,
Carry,
Farewell
}
private sealed class Entry
{
public uint Id { get; }
public long Created { get; }
public string Key { get; set; }
public bool Member { get; set; }
public bool Customer { get; set; }
public bool HasSpawn { get; set; }
public T Spawn { get; set; }
public long SpawnOrder { get; set; }
public bool HasProperties { get; set; }
public T Properties { get; set; }
public long PropertiesOrder { get; set; }
public bool HasStored { get; set; }
public T Stored { get; set; }
public long StoredOrder { get; set; }
public uint Store { get; set; }
public Dictionary<Slot, CustomerLine> Lines { get; } = new Dictionary<Slot, CustomerLine>();
public Entry(uint id, long created)
{
Id = id;
Created = created;
}
}
public const string CustomerKey = "p_EntityCustomer";
public const string StoragePrefix = "p_Entity_Storage_CustomerParcels";
public const string AppearanceKind = "CustomerAppearance";
public const string CustomerKind = "Customer";
public const string CarryKind = "CarryParcel";
public const int MostRecent = 128;
public const int MostMembers = 1000;
private readonly Dictionary<uint, Entry> _entries = new Dictionary<uint, Entry>();
private long _order;
private long _created;
public long Mark => _order;
public int Count => _entries.Values.Count((Entry entry) => entry.Member);
public int CustomerCount => _entries.Values.Count((Entry entry) => entry.Customer && entry.HasSpawn);
public static bool IsCustomer(string key)
{
return key == "p_EntityCustomer";
}
public static bool IsCustomerStorage(string key)
{
return key?.StartsWith("p_Entity_Storage_CustomerParcels", StringComparison.Ordinal) ?? false;
}
public static Slot SlotOf(string text, out uint carried)
{
carried = 0u;
if (string.IsNullOrEmpty(text))
{
return Slot.None;
}
string[] array = text.Split(';');
switch (array[0])
{
case "CustomerAppearance":
return Slot.Appearance;
case "CarryParcel":
if (array.Length <= 1 || !uint.TryParse(array[1], out carried))
{
return Slot.None;
}
return Slot.Carry;
case "Customer":
if (array.Length > 1)
{
switch (array[1])
{
case "1":
case "2":
case "3":
return Slot.Request;
case "6":
case "7":
return Slot.Farewell;
default:
return Slot.None;
}
}
break;
}
return Slot.None;
}
public void RecordSpawn(uint entityId, string key, T payload)
{
if (entityId == 0)
{
return;
}
Entry entry = Get(entityId);
if (entry != null)
{
entry.Key = key;
entry.Spawn = payload;
entry.SpawnOrder = _order++;
entry.HasSpawn = true;
if (IsCustomer(key))
{
entry.Customer = true;
Promote(entry);
}
else if (IsCustomerStorage(key))
{
Promote(entry);
}
}
}
public void RecordProperties(uint entityId, T payload)
{
if (entityId != 0)
{
Entry entry = Get(entityId);
if (entry != null)
{
entry.Properties = payload;
entry.PropertiesOrder = _order++;
entry.HasProperties = true;
}
}
}
public void RecordStored(uint storeId, uint entityId, T payload)
{
if (entityId == 0)
{
return;
}
Entry entry = Get(entityId);
if (entry != null)
{
entry.Stored = payload;
entry.StoredOrder = _order++;
entry.Store = storeId;
entry.HasStored = true;
if (_entries.TryGetValue(storeId, out var value) && value.Member)
{
Promote(entry);
}
}
}
public bool RecordLine(uint entityId, string text)
{
if (!_entries.TryGetValue(entityId, out var value) || !value.Customer)
{
return false;
}
uint carried;
Slot slot = SlotOf(text, out carried);
if (slot == Slot.None)
{
return false;
}
value.Lines[slot] = new CustomerLine(_order++, entityId, text);
if (slot == Slot.Carry)
{
Entry entry = Get(carried);
if (entry != null)
{
Promote(entry);
}
}
return true;
}
public bool Forget(uint entityId)
{
if (!_entries.TryGetValue(entityId, out var value))
{
return false;
}
_entries.Remove(entityId);
if (value.Member)
{
Release(value);
}
return true;
}
public void Clear()
{
_entries.Clear();
_order = 0L;
_created = 0L;
}
public IReadOnlyList<ReplayMessage<T>> Before(long mark, Func<uint, bool> known, int spawnCode, int storedCode, int propertiesCode)
{
HashSet<uint> hashSet = new HashSet<uint>(from entry2 in _entries.Values
where entry2.Member && entry2.HasSpawn && entry2.SpawnOrder < mark && (known == null || !known(entry2.Id))
select entry2.Id);
List<ReplayMessage<T>> list = new List<ReplayMessage<T>>();
foreach (uint item in hashSet)
{
Entry entry = _entries[item];
list.Add(new ReplayMessage<T>(entry.SpawnOrder, spawnCode, item, entry.Spawn));
if (entry.HasProperties && entry.PropertiesOrder < mark)
{
list.Add(new ReplayMessage<T>(Math.Max(entry.PropertiesOrder, entry.SpawnOrder), propertiesCode, item, entry.Properties));
}
if (entry.HasStored && entry.StoredOrder < mark && (hashSet.Contains(entry.Store) || (known != null && known(entry.Store))))
{
long val = (hashSet.Contains(entry.Store) ? Math.Max(entry.SpawnOrder, _entries[entry.Store].SpawnOrder) : entry.SpawnOrder);
list.Add(new ReplayMessage<T>(Math.Max(entry.StoredOrder, val), storedCode, item, entry.Stored));
}
}
return (from message in list
orderby message.Order, (message.Code != spawnCode) ? ((message.Code == storedCode) ? 1 : 2) : 0
select message).ToList();
}
public IReadOnlyList<uint> Customers()
{
return (from entry in _entries.Values
where entry.Customer && entry.HasSpawn
orderby entry.SpawnOrder
select entry.Id).ToList();
}
public IReadOnlyList<uint> MembersSince(long mark)
{
return (from entry in _entries.Values
where entry.Member && entry.HasSpawn && entry.SpawnOrder >= mark
orderby entry.SpawnOrder
select entry.Id).ToList();
}
public IReadOnlyList<CustomerLine> LinesOf(uint customerId, long since)
{
if (!_entries.TryGetValue(customerId, out var value) || !value.Customer)
{
return new List<CustomerLine>();
}
return (from line in value.Lines.Values
where line.Order >= since
orderby line.Order
select line).ToList();
}
private Entry Get(uint entityId)
{
if (_entries.TryGetValue(entityId, out var value))
{
return value;
}
value = new Entry(entityId, _created++);
_entries[entityId] = value;
Trim();
return value;
}
private void Release(Entry gone)
{
Queue<Entry> queue = new Queue<Entry>();
queue.Enqueue(gone);
while (queue.Count > 0)
{
Entry holder = queue.Dequeue();
CustomerLine value;
uint carried2;
uint carried = ((holder.Customer && holder.Lines.TryGetValue(Slot.Carry, out value) && SlotOf(value.Text, out carried2) == Slot.Carry) ? carried2 : 0u);
foreach (Entry item in _entries.Values.Where((Entry entry) => entry.Member && !entry.Customer && !IsCustomerStorage(entry.Key) && ((entry.HasStored && entry.Store == holder.Id) || entry.Id == carried)).ToList())
{
item.Member = false;
queue.Enqueue(item);
}
}
Trim();
}
private void Promote(Entry entry)
{
if (!entry.Member && Count < 1000)
{
entry.Member = true;
}
}
private void Trim()
{
List<Entry> list = _entries.Values.Where((Entry entry) => !entry.Member).ToList();
if (list.Count <= 128)
{
return;
}
foreach (Entry item in list.OrderBy((Entry entry) => entry.Created).Take(list.Count - 128))
{
_entries.Remove(item.Id);
}
}
}
public readonly struct EntityPlace
{
public uint Id { get; }
public float X { get; }
public float Y { get; }
public float Z { get; }
public EntityPlace(uint id, float x, float y, float z)
{
Id = id;
X = x;
Y = y;
Z = z;
}
public override string ToString()
{
return $"{Id} ({X:0.###}, {Y:0.###}, {Z:0.###})";
}
}
public static class EntityPlaces
{
public const uint EndMark = uint.MaxValue;
public static IReadOnlyList<EntityPlace> Merge(IEnumerable<EntityPlace> scene, IReadOnlyList<EntityPlace> saved)
{
HashSet<uint> hashSet = new HashSet<uint>(saved.Select((EntityPlace place) => place.Id));
List<EntityPlace> list = new List<EntityPlace>();
HashSet<uint> hashSet2 = new HashSet<uint>();
foreach (EntityPlace item in scene)
{
if (item.Id != 0 && item.Id != uint.MaxValue && !hashSet.Contains(item.Id) && hashSet2.Add(item.Id))
{
list.Add(item);
}
}
HashSet<uint> hashSet3 = new HashSet<uint>();
foreach (EntityPlace item2 in saved)
{
if (item2.Id != 0 && item2.Id != uint.MaxValue && hashSet3.Add(item2.Id))
{
list.Add(item2);
}
}
return list;
}
}
public enum HoldKind
{
Authority,
PickUp
}
public readonly struct HoldMessage
{
public long Order { get; }
public HoldKind Kind { get; }
public ulong ClientId { get; }
public uint EntityId { get; }
public HoldMessage(long order, HoldKind kind, ulong clientId, uint entityId)
{
Order = order;
Kind = kind;
ClientId = clientId;
EntityId = entityId;
}
}
public sealed class HoldLedger
{
public const int MostAuthorities = 20000;
private readonly Dictionary<uint, (long Order, ulong ClientId)> _authority = new Dictionary<uint, (long, ulong)>();
private readonly Dictionary<ulong, (long Order, uint EntityId)> _held = new Dictionary<ulong, (long, uint)>();
private long _order;
public long Mark => _order;
public int AuthorityCount => _authority.Count;
public int HeldCount => _held.Count;
public bool RecordAuthority(ulong clientId, uint entityId)
{
if (entityId == 0 || (!_authority.ContainsKey(entityId) && _authority.Count >= 20000))
{
return false;
}
_authority[entityId] = (_order++, clientId);
return true;
}
public bool RecordPickUp(ulong clientId, uint entityId)
{
if (entityId == 0)
{
return false;
}
foreach (ulong item in (from pair in _held
where pair.Key != clientId && pair.Value.EntityId == entityId
select pair.Key).ToList())
{
_held.Remove(item);
}
_held[clientId] = (_order++, entityId);
return true;
}
public bool RecordDrop(ulong clientId, uint entityId)
{
if (!_held.TryGetValue(clientId, out (long, uint) value) || (entityId != 0 && value.Item2 != entityId))
{
return false;
}
_held.Remove(clientId);
return true;
}
public int Forget(uint entityId)
{
int num = (_authority.Remove(entityId) ? 1 : 0);
foreach (ulong item in (from pair in _held
where pair.Value.EntityId == entityId
select pair.Key).ToList())
{
_held.Remove(item);
num++;
}
return num;
}
public int ForgetClient(ulong clientId)
{
int num = (_held.Remove(clientId) ? 1 : 0);
foreach (uint item in (from pair in _authority
where pair.Value.ClientId == clientId
select pair.Key).ToList())
{
_authority.Remove(item);
num++;
}
return num;
}
public void Clear()
{
_authority.Clear();
_held.Clear();
_order = 0L;
}
public IReadOnlyList<HoldMessage> Before(long mark, Func<uint, bool> known, Func<ulong, bool> present)
{
return (from message in _authority.Select((KeyValuePair<uint, (long Order, ulong ClientId)> pair) => new HoldMessage(pair.Value.Order, HoldKind.Authority, pair.Value.ClientId, pair.Key)).Concat(_held.Select((KeyValuePair<ulong, (long Order, uint EntityId)> pair) => new HoldMessage(pair.Value.Order, HoldKind.PickUp, pair.Key, pair.Value.EntityId)))
where message.Order < mark && (known == null || known(message.EntityId)) && (present == null || present(message.ClientId))
orderby (message.Kind != HoldKind.Authority) ? 1 : 0, message.Order
select message).ToList();
}
}
public readonly struct HostState
{
public bool Hosting { get; }
public bool LevelReady { get; }
public bool Restarting { get; }
public int TimePeriod { get; }
public HostState(bool hosting, bool levelReady, bool restarting, int timePeriod)
{
Hosting = hosting;
LevelReady = levelReady;
Restarting = restarting;
TimePeriod = timePeriod;
}
}
public static class JoinGate
{
public const int EveningRecap = 2;
public const int DawnRecap = 5;
public const double SettleSeconds = 3.0;
public static bool CanAccept(HostState state)
{
if (state.Hosting && state.LevelReady)
{
return !state.Restarting;
}
return false;
}
public static bool CanStart(HostState state)
{
if (CanAccept(state))
{
return !IsRecap(state.TimePeriod);
}
return false;
}
public static bool ReadyToStart(HostState state, double secondsConnected)
{
if (CanStart(state))
{
return secondsConnected >= 3.0;
}
return false;
}
public static bool IsRecap(int timePeriod)
{
if (timePeriod == 2 || timePeriod == 5)
{
return true;
}
return false;
}
}
public enum JoinStage
{
Waiting,
Loading,
Appearing,
Synced,
Playing
}
public readonly struct HeldMessage<T>
{
public int Code { get; }
public bool Reliable { get; }
public T Payload { get; }
public HeldMessage(int code, bool reliable, T payload)
{
Code = code;
Reliable = reliable;
Payload = payload;
}
}
public sealed class JoinTicket<T>
{
public const int MostHeldMessages = 20000;
private readonly List<HeldMessage<T>> _held = new List<HeldMessage<T>>();
public ulong ClientId { get; }
public string Name { get; }
public double ConnectedAt { get; }
public JoinStage Stage { get; private set; }
public double StageSince { get; private set; }
public IReadOnlyList<EntityPlace> Places { get; set; }
public int Dropped { get; private set; }
public int Overflowed { get; private set; }
public bool WaitingNoticeShown { get; set; }
public bool Flushing { get; set; }
public bool PlacesMerged { get; set; }
public bool StateSent { get; set; }
public long SpawnMark { get; set; }
public long StateMark { get; set; }
public HashSet<uint> Replayed { get; } = new HashSet<uint>();
public long HoldMark { get; set; }
public double HoldsDueAt { get; set; } = double.PositiveInfinity;
public bool HoldsSent { get; set; }
public IReadOnlyList<EntityPlace> ScenePlaces { get; set; }
public string FailReason { get; set; }
public IReadOnlyList<HeldMessage<T>> Held => _held;
public JoinTicket(ulong clientId, string name, double now)
{
ClientId = clientId;
Name = name;
ConnectedAt = now;
StageSince = now;
}
public void MoveTo(JoinStage stage, double now)
{
Stage = stage;
StageSince = now;
}
public Route Accept(int code, bool reliable, T payload)
{
Route route = MessageRoute.For(Stage, code);
switch (route)
{
case Route.Drop:
Dropped++;
break;
case Route.Hold:
Hold(code, reliable, payload);
break;
case Route.HoldLatest:
_held.RemoveAll((HeldMessage<T> message) => message.Code == code);
Hold(code, reliable, payload);
break;
}
return route;
}
public List<HeldMessage<T>> TakeHeld()
{
return TakeHeld(int.MaxValue);
}
public List<HeldMessage<T>> TakeHeld(int most)
{
int count = Math.Min(Math.Max(0, most), _held.Count);
List<HeldMessage<T>> range = _held.GetRange(0, count);
_held.RemoveRange(0, count);
return range;
}
private void Hold(int code, bool reliable, T payload)
{
if (_held.Count >= 20000)
{
Overflowed++;
}
else
{
_held.Add(new HeldMessage<T>(code, reliable, payload));
}
}
}
public readonly struct LastingMessage<T>
{
public int Code { get; }
public uint EntityId { get; }
public T Payload { get; }
public LastingMessage(int code, uint entityId, T payload)
{
Code = code;
EntityId = entityId;
Payload = payload;
}
}
public sealed class LastingMessages<T>
{
public const int MostMessages = 20000;
private readonly Dictionary<(int Code, uint EntityId), (long Order, LastingMessage<T> Message)> _latest = new Dictionary<(int, uint), (long, LastingMessage<T>)>();
private long _order;
public int Count => _latest.Count;
public static bool IsLasting(int code)
{
if ((uint)(code - 48) <= 2u)
{
return true;
}
return false;
}
public static bool IsPerEntity(int code)
{
return code != 50;
}
public bool Record(int code, uint entityId, T payload)
{
if (!IsLasting(code))
{
return false;
}
if (!IsPerEntity(code))
{
entityId = 0u;
}
else if (entityId == 0)
{
return false;
}
(int, uint) key = (code, entityId);
if (!_latest.ContainsKey(key) && _latest.Count >= 20000)
{
return false;
}
_latest[key] = (_order++, new LastingMessage<T>(code, entityId, payload));
return true;
}
public int Forget(uint entityId)
{
if (entityId == 0)
{
return 0;
}
List<(int, uint)> list = _latest.Keys.Where(((int Code, uint EntityId) key) => key.EntityId == entityId).ToList();
foreach (var item in list)
{
_latest.Remove(item);
}
return list.Count;
}
public void Clear()
{
_latest.Clear();
_order = 0L;
}
public IReadOnlyList<LastingMessage<T>> For(Func<uint, bool> known)
{
return (from entry in _latest.Values
where entry.Message.EntityId == 0 || known == null || known(entry.Message.EntityId)
orderby entry.Order
select entry.Message).ToList();
}
}
public enum Route
{
Pass,
Drop,
Hold,
HoldLatest,
Replace
}
public static class MessageRoute
{
public static Route For(JoinStage stage, int code)
{
if (code == 21)
{
return Route.Replace;
}
bool flag = (uint)(stage - 3) <= 1u;
if (flag || IsJoinFlow(code))
{
return Route.Pass;
}
if (stage == JoinStage.Appearing && code == 20)
{
return Route.Pass;
}
flag = IsContinuous(code);
if (!flag)
{
bool flag2 = ((code == 20 || code == 38 || code == 51) ? true : false);
flag = flag2;
}
if (flag)
{
return Route.Drop;
}
if (stage == JoinStage.Waiting)
{
return Route.Drop;
}
if (code != 44)
{
return Route.Hold;
}
return Route.HoldLatest;
}
public static bool IsJoinFlow(int code)
{
switch (code)
{
case 3:
case 4:
case 5:
case 6:
case 9:
case 10:
case 11:
case 13:
case 28:
case 29:
return true;
default:
return false;
}
}
public static bool IsContinuous(int code)
{
if ((uint)(code - 22) <= 1u || (uint)(code - 26) <= 1u)
{
return true;
}
return false;
}
}
public static class PlayerLimit
{
public const int GamePlayers = 4;
public const int MostPlayers = 8;
public const string CountPattern = "E8 ?? ?? ?? ?? 83 F8 05 0F 8D";
public const int CountOffset = 7;
public const string LoadingPattern = "44 38 A1 ?? 00 00 00 0F 85 ?? ?? ?? ??";
public const int LoadingOffset = 7;
public const int LoadingLength = 6;
public static readonly byte[] SkipJump = new byte[6] { 102, 15, 31, 68, 0, 0 };
public static int Clamp(int players)
{
return Math.Max(4, Math.Min(8, players));
}
public static byte ConnectionLimit(int players)
{
return (byte)(Clamp(players) + 1);
}
}
public static class ProtocolCodes
{
public const int ConnectAck = 3;
public const int ClientConnected = 4;
public const int ClientDisconnect = 5;
public const int ClientDisconnectBroadcast = 6;
public const int ServerDisconnect = 9;
public const int ServerGameStart = 10;
public const int ServerFinalizeLoad = 11;
public const int GrantPlayerSpawn = 13;
public const int EntitySpawn = 17;
public const int EntityDisposed = 18;
public const int PlayerSpawnedBroadcast = 20;
public const int IdentifiersSynchronization = 21;
public const int MovementAndRotation = 22;
public const int MovementAndRotationBroadcast = 23;
public const int EntityAuthority = 24;
public const int EntityAuthorityBroadcast = 25;
public const int AnimationParameter = 26;
public const int AnimationParameterBroadcast = 27;
public const int PlayerName = 28;
public const int PlayerNameBroadcast = 29;
public const int PlayerItemPickUp = 32;
public const int PlayerItemPickUpBroadcast = 33;
public const int PlayerItemDropped = 34;
public const int PlayerItemDroppedBroadcast = 35;
public const int GenericMessage = 38;
public const int EntityStored = 40;
public const int EntityProperties = 43;
public const int GameTimeSynchronization = 44;
public const int GameTimePeriod = 45;
public const int SetInteractionsLocked = 48;
public const int SetParcelDamaged = 49;
public const int BoatDestinations = 50;
public const int SaveSynchronization = 51;
}
public readonly struct SpawnSpot
{
public float X { get; }
public float Y { get; }
public float Z { get; }
public int Nearest { get; }
public SpawnSpot(float x, float y, float z, int nearest)
{
X = x;
Y = y;
Z = z;
Nearest = nearest;
}
}
public static class SpawnSpots
{
public static IReadOnlyList<SpawnSpot> Extra(IReadOnlyList<(float X, float Y, float Z)> game, int wanted)
{
List<SpawnSpot> list = new List<SpawnSpot>();
int num = game?.Count ?? 0;
if (num < 2 || wanted <= num)
{
return list;
}
for (int i = 0; i + 1 < num; i++)
{
if (num + list.Count >= wanted)
{
break;
}
(float, float, float) tuple = game[i];
(float, float, float) tuple2 = game[i + 1];
list.Add(new SpawnSpot((tuple.Item1 + tuple2.Item1) / 2f, (tuple.Item2 + tuple2.Item2) / 2f, (tuple.Item3 + tuple2.Item3) / 2f, i));
}
(float, float, float) tuple3 = game[0];
(float, float, float) tuple4 = game[1];
(float, float, float) tuple5 = game[num - 1];
(float, float, float) tuple6 = game[num - 2];
int num2 = 1;
while (num + list.Count < wanted)
{
list.Add(new SpawnSpot(tuple5.Item1 + (tuple5.Item1 - tuple6.Item1) * (float)num2 / 2f, tuple5.Item2, tuple5.Item3 + (tuple5.Item3 - tuple6.Item3) * (float)num2 / 2f, num - 1));
if (num + list.Count < wanted)
{
list.Add(new SpawnSpot(tuple3.Item1 - (tuple4.Item1 - tuple3.Item1) * (float)num2 / 2f, tuple3.Item2, tuple3.Item3 - (tuple4.Item3 - tuple3.Item3) * (float)num2 / 2f, 0));
}
num2++;
}
return list;
}
}
public readonly struct StateMessage
{
public uint EntityId { get; }
public string Text { get; }
public string Kind { get; }
public StateMessage(uint entityId, string text, string kind)
{
EntityId = entityId;
Text = text;
Kind = kind;
}
}
public sealed class StateLedger
{
public const int MostMessages = 20000;
public const string CounterKind = "CustomerCounter";
public const string CounterOpenKind = "CustomerCounter;OpenState";
private static readonly char[] Separators = new char[2] { ';', ':' };
private static readonly HashSet<string> MomentKinds = new HashSet<string>(StringComparer.Ordinal) { "Captain", "Customer", "CustomerAudio", "CustomerAppearance", "CarryParcel" };
private readonly Dictionary<(uint EntityId, string Kind), (long Order, StateMessage Message)> _latest = new Dictionary<(uint, string), (long, StateMessage)>();
private long _order;
public int Count => _latest.Count;
public static string KindOf(string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
int num = text.IndexOfAny(Separators);
if (num < 0)
{
return text;
}
string text2 = text.Substring(0, num);
if (text2 != "CustomerCounter")
{
return text2;
}
int num2 = text.IndexOfAny(Separators, num + 1);
if (num2 >= 0)
{
return text.Substring(0, num2);
}
return text;
}
public static bool IsLasting(string kind)
{
if (string.IsNullOrEmpty(kind) || MomentKinds.Contains(kind))
{
return false;
}
if (kind.StartsWith("CustomerCounter", StringComparison.Ordinal))
{
return kind == "CustomerCounter;OpenState";
}
return true;
}
public bool Record(uint entityId, string text)
{
if (entityId == 0 || text == null)
{
return false;
}
string text2 = KindOf(text);
if (!IsLasting(text2))
{
return false;
}
(uint, string) key = (entityId, text2);
if (!_latest.ContainsKey(key) && _latest.Count >= 20000)
{
return false;
}
_latest[key] = (_order++, new StateMessage(entityId, text, text2));
return true;
}
public int Forget(uint entityId)
{
List<(uint, string)> list = _latest.Keys.Where(((uint EntityId, string Kind) key) => key.EntityId == entityId).ToList();
foreach (var item in list)
{
_latest.Remove(item);
}
return list.Count;
}
public void Clear()
{
_latest.Clear();
_order = 0L;
}
public IReadOnlyList<StateMessage> For(Func<uint, bool> known)
{
return (from entry in _latest.Values
where known == null || known(entry.Message.EntityId)
orderby entry.Order
select entry.Message).ToList();
}
}
}