Skip to content

Commit

Permalink
Improve patch and allow metabolism disable
Browse files Browse the repository at this point in the history
- Host can now disable metabolism if they wish
  • Loading branch information
Lacyway committed May 10, 2024
1 parent 4e60392 commit c272fbf
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override MethodBase GetTargetMethod()

[PatchPostfix]
private static void PatchPostfix(RaidSettingsWindow __instance, UiElementBlocker ____coopModeBlocker, List<CanvasGroup> ____weatherCanvasGroups,
UpdatableToggle ____randomTimeToggle, UpdatableToggle ____randomWeatherToggle)
UpdatableToggle ____randomTimeToggle, UpdatableToggle ____randomWeatherToggle, List<CanvasGroup> ____waterAndFoodCanvasGroups)
{
// Always disable the Coop Mode checkbox
____coopModeBlocker.SetBlock(true, "Co-op is always enabled in Fika");
Expand All @@ -31,6 +31,11 @@ private static void PatchPostfix(RaidSettingsWindow __instance, UiElementBlocker
weatherCanvasGroups = ____weatherCanvasGroups;
}

foreach (CanvasGroup canvasGroup in ____waterAndFoodCanvasGroups)
{
canvasGroup.SetUnlockStatus(true, true);
}

instance = __instance;

____randomWeatherToggle.Bind(new Action<bool>(ToggleWeather));
Expand Down
18 changes: 16 additions & 2 deletions Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ internal sealed class CoopGame : BaseLocalGame<GamePlayerOwner>, IBotGame, IFika
private CoopExfilManager exfilManager;
private GameObject fikaStartButton;

public RaidSettings RaidSettings { get; private set; }

//WildSpawnType for sptUsec and sptBear
const int sptUsecValue = 47;
const int sptBearValue = 48;
Expand Down Expand Up @@ -91,7 +93,8 @@ public IWeatherCurve WeatherCurve

internal static CoopGame Create(InputTree inputTree, Profile profile, GameDateTime backendDateTime, InsuranceCompanyClass insurance, MenuUI menuUI,
CommonUI commonUI, PreloaderUI preloaderUI, GameUI gameUI, LocationSettingsClass.Location location, TimeAndWeatherSettings timeAndWeather,
WavesSettings wavesSettings, EDateTime dateTime, Callback<ExitStatus, TimeSpan, MetricsClass> callback, float fixedDeltaTime, EUpdateQueue updateQueue, ISession backEndSession, TimeSpan sessionTime)
WavesSettings wavesSettings, EDateTime dateTime, Callback<ExitStatus, TimeSpan, MetricsClass> callback, float fixedDeltaTime, EUpdateQueue updateQueue,
ISession backEndSession, TimeSpan sessionTime, RaidSettings raidSettings)
{
Logger = BepInEx.Logging.Logger.CreateLogSource("Coop Game Mode");
Logger.LogInfo("CoopGame::Create");
Expand All @@ -113,7 +116,7 @@ internal static CoopGame Create(InputTree inputTree, Profile profile, GameDateTi
BossLocationSpawn[] bossSpawns = EFT.LocalGame.smethod_8(wavesSettings, location.BossLocationSpawn);
coopGame.GClass579 = GClass579.smethod_0(bossSpawns, new Action<BossLocationSpawn>(coopGame.botsController_0.ActivateBotsByWave));

if (useCustomWeather)
if (useCustomWeather && MatchmakerAcceptPatches.IsServer)
{
Logger.LogInfo("Custom weather enabled, initializing curves");
coopGame.SetupCustomWeather(timeAndWeather);
Expand All @@ -124,6 +127,8 @@ internal static CoopGame Create(InputTree inputTree, Profile profile, GameDateTi
Singleton<IFikaGame>.Create(coopGame);
FikaEventDispatcher.DispatchEvent(new FikaGameCreatedEvent(coopGame));

coopGame.RaidSettings = raidSettings;

return coopGame;
}

Expand Down Expand Up @@ -666,6 +671,15 @@ public override async Task<LocalPlayer> vmethod_2(int playerId, Vector3 position
await Task.Delay(5000);
}

if (MatchmakerAcceptPatches.IsServer)
{
if (RaidSettings.MetabolismDisabled)
{
myPlayer.HealthController.DisableMetabolism();
NotificationManagerClass.DisplayMessageNotification("Metabolism disabled", iconType: EFT.Communications.ENotificationIconType.Alert);
}
}

CoopPlayer coopPlayer = (CoopPlayer)myPlayer;
coopHandler.Players.Add(coopPlayer.NetId, coopPlayer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,14 @@ public static async Task Postfix(Task __result, TarkovApplication __instance, Ti

await Task.Delay(1000);

StartHandler startHandler = new(__instance, session.Profile, session.ProfileOfPet, ____raidSettings.SelectedLocation, timeHasComeScreenController);

CoopGame localGame = CoopGame.Create(____inputTree, profile, ____localGameDateTime,
session.InsuranceCompany, MonoBehaviourSingleton<MenuUI>.Instance,
MonoBehaviourSingleton<CommonUI>.Instance, MonoBehaviourSingleton<PreloaderUI>.Instance,
MonoBehaviourSingleton<GameUI>.Instance, ____raidSettings.SelectedLocation, timeAndWeather,
____raidSettings.WavesSettings, ____raidSettings.SelectedDateTime, new Callback<ExitStatus, TimeSpan, MetricsClass>((r) =>
{
typeof(TarkovApplication).GetMethod(nameof(TarkovApplication.method_46))
.Invoke(__instance, [session.Profile.Id, session.ProfileOfPet, ____raidSettings.SelectedLocation, r, timeHasComeScreenController]);

}), ____fixedDeltaTime, EUpdateQueue.Update, session, TimeSpan.FromSeconds(60 * ____raidSettings.SelectedLocation.EscapeTimeLimit)
____raidSettings.WavesSettings, ____raidSettings.SelectedDateTime, new Callback<ExitStatus, TimeSpan, MetricsClass>(startHandler.HandleStart),
____fixedDeltaTime, EUpdateQueue.Update, session, TimeSpan.FromSeconds(60 * ____raidSettings.SelectedLocation.EscapeTimeLimit), ____raidSettings
);
Singleton<AbstractGame>.Create(localGame);
FikaEventDispatcher.DispatchEvent(new AbstractGameCreatedEvent(localGame));
Expand All @@ -126,20 +124,35 @@ public static async Task Postfix(Task __result, TarkovApplication __instance, Ti
timeHasComeScreenController.ChangeStatus("Created Coop Game");
}

Task initTask = localGame.method_4(____raidSettings.BotSettings, ____backendUrl, null, new Callback((r) =>
Task finishTask = localGame.method_4(____raidSettings.BotSettings, ____backendUrl, null, new Callback(startHandler.HandleLoadComplete));
__result = Task.WhenAll(finishTask);
}

private class StartHandler(TarkovApplication tarkovApplication, Profile pmcProfile, Profile scavProfile, LocationSettingsClass.Location location, MatchmakerTimeHasCome.GClass3163 timeHasComeScreenController)
{
private readonly TarkovApplication tarkovApplication = tarkovApplication;
private readonly Profile pmcProfile = pmcProfile;
private readonly Profile scavProfile = scavProfile;
private readonly LocationSettingsClass.Location location = location;
private readonly MatchmakerTimeHasCome.GClass3163 timeHasComeScreenController = timeHasComeScreenController;

public void HandleStart(Result<ExitStatus, TimeSpan, MetricsClass> result)
{
tarkovApplication.method_46(pmcProfile.Id, scavProfile, location, result, timeHasComeScreenController);
}

public void HandleLoadComplete(IResult error)
{
using (GClass21.StartWithToken("LoadingScreen.LoadComplete"))
{
UnityEngine.Object.DestroyImmediate(MonoBehaviourSingleton<MenuUI>.Instance.gameObject);
MainMenuController mmc = (MainMenuController)typeof(TarkovApplication).GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Where(x => x.FieldType == typeof(MainMenuController)).FirstOrDefault().GetValue(__instance);
mmc.Unsubscribe();
MainMenuController mmc = (MainMenuController)typeof(TarkovApplication).GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Where(x => x.FieldType == typeof(MainMenuController)).FirstOrDefault().GetValue(tarkovApplication);
mmc?.Unsubscribe();
GameWorld gameWorld = Singleton<GameWorld>.Instance;
gameWorld.OnGameStarted();
FikaEventDispatcher.DispatchEvent(new GameWorldStartedEvent(gameWorld));
}
}));

__result = Task.WhenAll(initTask);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MatchmakerAcceptScreenShowPatch : ModulePatch
private static GameObject MatchmakerObject { get; set; }

[PatchPrefix]
private static void Pre(ref ISession session, ref RaidSettings raidSettings, Profile ___profile_0, MatchMakerAcceptScreen __instance,
private static void PreFix(ref ISession session, ref RaidSettings raidSettings, Profile ___profile_0, MatchMakerAcceptScreen __instance,
DefaultUIButton ____acceptButton, DefaultUIButton ____backButton, MatchMakerPlayerPreview ____playerModelView)
{
if (MatchmakerObject == null)
Expand All @@ -29,14 +29,14 @@ private static void Pre(ref ISession session, ref RaidSettings raidSettings, Pro
raidSettings.RaidMode = ERaidMode.Local;
}

var newMatchMaker = MatchmakerObject.GetOrAddComponent<MatchMakerUIScript>();
MatchMakerUIScript newMatchMaker = MatchmakerObject.GetOrAddComponent<MatchMakerUIScript>();
newMatchMaker.RaidSettings = raidSettings;
newMatchMaker.AcceptButton = ____acceptButton;
newMatchMaker.BackButton = ____backButton;
}

[PatchPostfix]
private static void Post(ref ISession session, Profile ___profile_0, MatchMakerAcceptScreen __instance)
private static void PostFix(ref ISession session, Profile ___profile_0, MatchMakerAcceptScreen __instance)
{
MatchmakerAcceptPatches.MatchMakerAcceptScreenInstance = __instance;
MatchmakerAcceptPatches.Profile = ___profile_0;
Expand Down
9 changes: 9 additions & 0 deletions Fika.Core/Coop/PacketHandlers/ClientPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ private void Update()

private IEnumerator SyncWorld()
{
while (Client.NetClient.FirstPeer == null)
{
yield return null;
}

Writer?.Reset();
SessionSettingsPacket settingsPacket = new(true);
Client?.SendData(Writer, ref settingsPacket, DeliveryMethod.ReliableOrdered);

CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;

if (coopGame == null)
Expand Down
13 changes: 13 additions & 0 deletions Fika.Core/Networking/FikaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected void Start()
packetProcessor.SubscribeNetSerializable<AssignNetIdPacket, NetPeer>(OnAssignNetIdPacketReceived);
packetProcessor.SubscribeNetSerializable<SyncNetIdPacket, NetPeer>(OnSyncNetIdPacketReceived);
packetProcessor.SubscribeNetSerializable<OperationCallbackPacket, NetPeer>(OnOperationCallbackPacketReceived);
packetProcessor.SubscribeNetSerializable<SessionSettingsPacket, NetPeer>(OnSessionSettingsPacketReceived);

_netClient = new NetManager(this)
{
Expand Down Expand Up @@ -116,6 +117,18 @@ protected void Start()
ClientReady = true;
}

private void OnSessionSettingsPacketReceived(SessionSettingsPacket packet, NetPeer peer)
{
if (!packet.IsRequest)
{
if (packet.MetabolismDisabled)
{
Singleton<GameWorld>.Instance.MainPlayer.HealthController.DisableMetabolism();
NotificationManagerClass.DisplayMessageNotification("Metabolism disabled", iconType: EFT.Communications.ENotificationIconType.Alert);
}
}
}

private void OnOperationCallbackPacketReceived(OperationCallbackPacket packet, NetPeer peer)
{
if (Players.TryGetValue(packet.NetId, out CoopPlayer player) && player.IsYourPlayer)
Expand Down
18 changes: 18 additions & 0 deletions Fika.Core/Networking/FikaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public async void Start()
packetProcessor.SubscribeNetSerializable<MinePacket, NetPeer>(OnMinePacketReceived);
packetProcessor.SubscribeNetSerializable<BorderZonePacket, NetPeer>(OnBorderZonePacketReceived);
packetProcessor.SubscribeNetSerializable<SendCharacterPacket, NetPeer>(OnSendCharacterPacketReceived);
packetProcessor.SubscribeNetSerializable<SessionSettingsPacket, NetPeer>(OnSessionSettingsPacketReceived);

_netServer = new NetManager(this)
{
Expand Down Expand Up @@ -161,6 +162,23 @@ await Task.Run(async () =>
ServerReady = true;
}

private void OnSessionSettingsPacketReceived(SessionSettingsPacket packet, NetPeer peer)
{
if (packet.IsRequest)
{
CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;
if (coopGame != null)
{
SessionSettingsPacket returnPacket = new(false)
{
MetabolismDisabled = coopGame.RaidSettings.MetabolismDisabled
};

SendDataToPeer(peer, new(), ref returnPacket, DeliveryMethod.ReliableUnordered);
}
}
}

public int PopNetId()
{
int netId = _currentNetId;
Expand Down
28 changes: 28 additions & 0 deletions Fika.Core/Networking/Packets/Backend/SessionSettingsPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using LiteNetLib.Utils;

namespace Fika.Core.Networking
{
public struct SessionSettingsPacket(bool isRequest) : INetSerializable
{
public bool IsRequest = isRequest;
public bool MetabolismDisabled;

public void Deserialize(NetDataReader reader)
{
IsRequest = reader.GetBool();
if (!IsRequest)
{
MetabolismDisabled = reader.GetBool();
}
}

public void Serialize(NetDataWriter writer)
{
writer.Put(IsRequest);
if (!IsRequest)
{
writer.Put(MetabolismDisabled);
}
}
}
}

0 comments on commit c272fbf

Please sign in to comment.