forked from Exiled-Official/EXILED
-
Notifications
You must be signed in to change notification settings - Fork 111
Offline mode support #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c5c016d
Fix Offline-mode breaking everything
x3rt 87c2772
Add `offline` authentication type and append `@offline` to UserIds du…
x3rt 54371a1
Merge remote-tracking branch 'origin/offline-mode' into offline-mode
x3rt 1f80cd5
Add offline id support to Player.Get
x3rt 5eae4a9
Comment transpilers
x3rt cacc476
Merge branch 'dev' into offline-mode
louis1706 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="OfflineModeIds.cs" company="Exiled Team"> | ||
| // Copyright (c) Exiled Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.Events.Patches.Generic | ||
| { | ||
| #pragma warning disable SA1402 // File may only contain a single type | ||
| using System.Collections.Generic; | ||
| using System.Reflection.Emit; | ||
|
|
||
| using API.Features.Pools; | ||
| using CentralAuth; | ||
| using HarmonyLib; | ||
| using PluginAPI.Core.Interfaces; | ||
| using PluginAPI.Events; | ||
|
|
||
| using static HarmonyLib.AccessTools; | ||
|
|
||
| /// <summary> | ||
| /// Patches <see cref="PlayerAuthenticationManager.Start"/> to add an @offline suffix to UserIds in Offline Mode. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(PlayerAuthenticationManager), nameof(PlayerAuthenticationManager.Start))] | ||
| internal static class OfflineModeIds | ||
| { | ||
| private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
| { | ||
| List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions); | ||
|
|
||
| const int offset = -1; | ||
| int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Call && instruction.OperandIs(PropertySetter(typeof(PlayerAuthenticationManager), nameof(PlayerAuthenticationManager.UserId)))) + offset; | ||
|
|
||
| newInstructions.InsertRange( | ||
| index, | ||
| new[] | ||
| { | ||
| new CodeInstruction(OpCodes.Call, Method(typeof(OfflineModeIds), nameof(BuildUserId))), | ||
| }); | ||
|
|
||
| for (int i = 0; i < newInstructions.Count; i++) | ||
| yield return newInstructions[i]; | ||
|
|
||
| ListPool<CodeInstruction>.Pool.Return(newInstructions); | ||
| } | ||
|
|
||
| private static string BuildUserId(string userId) => $"{userId}@offline"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Patches <see cref="PlayerAuthenticationManager.Start"/> to add the player's UserId to the <see cref="PluginAPI.Core.Player.PlayersUserIds"/> dictionary. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(PlayerAuthenticationManager), nameof(PlayerAuthenticationManager.Start))] | ||
| internal static class OfflineModePlayerIds | ||
| { | ||
| private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
| { | ||
| List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions); | ||
|
|
||
| Label skipLabel = generator.DefineLabel(); | ||
|
|
||
| const int offset = 1; | ||
| int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Call && instruction.OperandIs(PropertySetter(typeof(PlayerAuthenticationManager), nameof(PlayerAuthenticationManager.UserId)))) + offset; | ||
|
|
||
| // if (!Player.PlayersUserIds.ContainsKey(this.UserId)) | ||
| // Player.PlayersUserIds.Add(this.UserId, this._hub); | ||
| newInstructions.InsertRange( | ||
| index, | ||
| new[] | ||
| { | ||
| new(OpCodes.Ldsfld, Field(typeof(PluginAPI.Core.Player), nameof(PluginAPI.Core.Player.PlayersUserIds))), | ||
| new(OpCodes.Ldarg_0), | ||
| new(OpCodes.Call, PropertyGetter(typeof(PlayerAuthenticationManager), nameof(PlayerAuthenticationManager.UserId))), | ||
| new(OpCodes.Callvirt, Method(typeof(Dictionary<string, IGameComponent>), nameof(Dictionary<string, IGameComponent>.ContainsKey))), | ||
| new(OpCodes.Brtrue_S, skipLabel), | ||
| new(OpCodes.Ldsfld, Field(typeof(PluginAPI.Core.Player), nameof(PluginAPI.Core.Player.PlayersUserIds))), | ||
| new(OpCodes.Ldarg_0), | ||
| new(OpCodes.Call, PropertyGetter(typeof(PlayerAuthenticationManager), nameof(PlayerAuthenticationManager.UserId))), | ||
| new(OpCodes.Ldarg_0), | ||
| new(OpCodes.Ldfld, Field(typeof(PlayerAuthenticationManager), nameof(PlayerAuthenticationManager._hub))), | ||
| new(OpCodes.Callvirt, Method(typeof(Dictionary<string, IGameComponent>), nameof(Dictionary<string, IGameComponent>.Add))), | ||
| new CodeInstruction(OpCodes.Nop).WithLabels(skipLabel), | ||
|
x3rt marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| for (int i = 0; i < newInstructions.Count; i++) | ||
| yield return newInstructions[i]; | ||
|
|
||
| ListPool<CodeInstruction>.Pool.Return(newInstructions); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Patches <see cref="ReferenceHub.Start"/> to prevent it from executing the <see cref="PluginAPI.Events.PlayerLeftEvent"/> event when the server is in offline mode. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(ReferenceHub), nameof(ReferenceHub.Start))] | ||
| internal static class OfflineModeReferenceHub | ||
| { | ||
| private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
| { | ||
| List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions); | ||
|
|
||
| const int offset = 1; | ||
| int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Callvirt) + offset; | ||
|
|
||
| Label returnLabel = generator.DefineLabel(); | ||
|
|
||
| newInstructions.InsertRange( | ||
| index, | ||
| new[] | ||
| { | ||
| new CodeInstruction(OpCodes.Br_S, returnLabel), | ||
| }); | ||
|
|
||
| newInstructions[newInstructions.Count - 1].WithLabels(returnLabel); | ||
|
|
||
| for (int i = 0; i < newInstructions.Count; i++) | ||
| yield return newInstructions[i]; | ||
|
|
||
| ListPool<CodeInstruction>.Pool.Return(newInstructions); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Patches <see cref="NicknameSync.UserCode_CmdSetNick__String"/> to execute the <see cref="PlayerJoinedEvent"/> event when the server is in offline mode. | ||
| /// </summary> | ||
| [HarmonyPatch(typeof(NicknameSync), nameof(NicknameSync.UserCode_CmdSetNick__String))] | ||
| internal static class OfflineModeJoin | ||
| { | ||
| private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
| { | ||
| List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions); | ||
|
|
||
| const int offset = 1; | ||
| int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Callvirt && x.OperandIs(Method(typeof(CharacterClassManager), nameof(CharacterClassManager.SyncServerCmdBinding)))) + offset; | ||
|
|
||
| // EventManager.ExecuteEvent(new PlayerJoinedEvent(this._hub)); | ||
| newInstructions.InsertRange( | ||
| index, | ||
| new[] | ||
| { | ||
| new CodeInstruction(OpCodes.Ldarg_0), | ||
| new CodeInstruction(OpCodes.Ldfld, Field(typeof(NicknameSync), nameof(NicknameSync._hub))), | ||
| new CodeInstruction(OpCodes.Call, Method(typeof(OfflineModeJoin), nameof(ExecuteNwEvent))), | ||
|
x3rt marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| for (int i = 0; i < newInstructions.Count; i++) | ||
| yield return newInstructions[i]; | ||
|
|
||
| ListPool<CodeInstruction>.Pool.Return(newInstructions); | ||
| } | ||
|
|
||
| private static void ExecuteNwEvent(ReferenceHub hub) | ||
| { | ||
| EventManager.ExecuteEvent(new PlayerJoinedEvent(hub)); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.