Skip to content

Commit

Permalink
refactor: CustomExfilService and ExfilPromptService
Browse files Browse the repository at this point in the history
  • Loading branch information
Trap committed Dec 23, 2024
1 parent 0552a2f commit f70498f
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 94 deletions.
22 changes: 22 additions & 0 deletions PTT-Plugin/Data/ExfilsTargets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using SPT.Common.Http;
using Newtonsoft.Json;
using EFT.Interactive;

namespace PTT.Data;

Expand All @@ -30,4 +31,25 @@ public class ExfilTarget
public string transitMapId; // transit only
public string transitSpawnPointId; // transit only
public string offraidPosition; // empty on transit

// TODO: i18n support (use the offraid position displayName)
public string GetCustomActionName()
{
if (isTransit)
{
return $"Transit to {transitMapId}";
}

return $"Extract to {offraidPosition}";
}

public string GetCustomExitName(ExfiltrationPoint exfil)
{
if (isTransit)
{
return $"{exfil.Settings.Name}.{transitMapId}.{transitSpawnPointId}";
}

return $"{exfil.Settings.Name}.{offraidPosition}";
}
}
8 changes: 6 additions & 2 deletions PTT-Plugin/Helpers/Transit.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using EFT.Interactive;
using PTT.Data;

namespace PTT.Helpers;

static class Transit
{
static public TransitPoint Create(string locationId, string customTransitName)
static public TransitPoint Create(ExfiltrationPoint exfil, ExfilTarget exfilTarget)
{
string locationId = exfilTarget.transitMapId;
string customTransitName = exfilTarget.GetCustomExitName(exfil);

return new TransitPoint
{
Enabled = true,
Expand All @@ -18,7 +22,7 @@ static public TransitPoint Create(string locationId, string customTransitName)
name = customTransitName,
description = customTransitName,
conditions = string.Empty,
target = locationId, // should be "_Id" of the corresponding location base but not needed here
target = "", // should be "_Id" of the corresponding location base but not needed here
location = locationId,
}
};
Expand Down
4 changes: 2 additions & 2 deletions PTT-Plugin/Patches/InitAllExfiltrationPointsPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected static bool PatchPrefix(ref ExfiltrationControllerClass __instance, Mo

foreach (ScavExfiltrationPoint scavExfiltrationPoint in __instance.ScavExfiltrationPoints)
{
Plugin.LogSource.LogWarning("Scav Exfil name = " + scavExfiltrationPoint.Settings.Name);
Plugin.LogSource.LogInfo("[PTT] Scav Exfil name = " + scavExfiltrationPoint.Settings.Name);
SharedExfiltrationPoint sharedExfiltrationPoint = scavExfiltrationPoint as SharedExfiltrationPoint;
if (sharedExfiltrationPoint != null && sharedExfiltrationPoint.IsMandatoryForScavs)
{
Expand All @@ -85,7 +85,7 @@ protected static bool PatchPrefix(ref ExfiltrationControllerClass __instance, Mo

foreach (ExfiltrationPoint exfiltrationPoint in __instance.ExfiltrationPoints)
{
Plugin.LogSource.LogWarning("PMC Exfil name = " + exfiltrationPoint.Settings.Name);
Plugin.LogSource.LogInfo("[PTT] PMC Exfil name = " + exfiltrationPoint.Settings.Name);
exitName = exfiltrationPoint.Settings.Name;
LocationExitClass locationExit = settings.FirstOrDefault(new Func<LocationExitClass, bool>(NameMatches));
int num = Array.IndexOf(source, exfiltrationPoint) + 1;
Expand Down
6 changes: 2 additions & 4 deletions PTT-Plugin/Patches/OnGameStartedPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ protected override MethodBase GetTargetMethod()
[PatchPrefix]
public static bool PatchPrefix()
{
var exfilsTargetsService = Singleton<ExfilsTargetsService>.Instance;

if (exfilsTargetsService != null)
if (Plugin.ExfilsTargetsService != null)
{
exfilsTargetsService.Init();
Plugin.ExfilsTargetsService.Init();
}
else
{
Expand Down
15 changes: 10 additions & 5 deletions PTT-Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ public class Plugin : BaseUnityPlugin
{
public static bool FikaIsInstalled { get; private set; }
public static ManualLogSource LogSource { get; private set; }
public static ExfilsTargetsService ExfilsTargetsService;

protected void Awake()
{
LogSource = Logger;
Logger.LogInfo($"[PTT] Plugin {PluginInfo.PLUGIN_GUID} is loading...");

LogSource = Logger;
FikaIsInstalled = Chainloader.PluginInfos.ContainsKey("com.fika.core");
ExfilsTargetsService = new ExfilsTargetsService();

Settings.Config.Init(Config);
Singleton<ExfilsTargetsService>.Create(new ExfilsTargetsService());
if (FikaIsInstalled)
{
Logger.LogInfo($"[PTT] Fika.Core detected");
}

Settings.Config.Init(Config);
new Patches.HideLockedTraderCardPatch().Enable();
new Patches.HideLockedTraderPanelPatch().Enable();
new Patches.InitAllExfiltrationPointsPatch().Enable();
Expand All @@ -43,11 +48,11 @@ protected void Start()
{
var exfilPromptService = new ExfilPromptService(interactableExfilsService, exfilsTargetsService);
exfilPromptService.InitPromptHandlers();
Logger.LogInfo($"[PTT] Interactable Exfils API: initialized exfils prompt handlers");
Logger.LogInfo($"[PTT] Jehree's Interactable Exfils API: initialized exfils prompt service");
}
else
{
Logger.LogError($"[PTT] Interactable exfils API: not found");
Logger.LogError($"[PTT] Jehree's Interactable Exfils API: not found");
}
}
}
33 changes: 20 additions & 13 deletions PTT-Plugin/Services/CustomExfilService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,67 @@
using System.Collections.Generic;

using PTT.Helpers;
using PTT.Data;

namespace PTT.Services;

public static class CustomExfilService
{
public static bool ExtractTo(ExfiltrationPoint exfiltrationPoint, string customExtractName)
public static void ExtractTo(ExfiltrationPoint exfil, ExfilTarget exfilTarget)
{
if (Plugin.FikaIsInstalled)
{
return CustomExfilServiceFika.ExtractTo(exfiltrationPoint, customExtractName);
CustomExfilServiceFika.ExtractTo(exfil, exfilTarget);
return;
}

string customExtractName = exfilTarget.GetCustomExitName(exfil);
LocalGame localGame = Singleton<AbstractGame>.Instance as LocalGame;
Player player = Singleton<GameWorld>.Instance.MainPlayer;

// TODO: log -> started extraction on customExtractName

if (localGame == null)
{
// TODO: log error
return false;
return;
}

if (player == null)
{
// TODO: log error
return false;
return;
}

localGame.Stop(player.ProfileId, ExitStatus.Survived, customExtractName, 0f);
return true;
// TODO: log -> local game stopped
}

// TODO: fix this code smell (the 2 string params)
public static bool TransitTo(string locationId, string customTransitName)
public static void TransitTo(ExfiltrationPoint exfil, ExfilTarget exfilTarget)
{
if (Plugin.FikaIsInstalled)
{
return CustomExfilServiceFika.TransitTo(locationId, customTransitName);
CustomExfilServiceFika.TransitTo(exfil, exfilTarget);
return;
}

TransitPoint transit = Transit.Create(exfil, exfilTarget);
string customTransitName = transit.parameters.name;
// TODO: log -> started transit on customTransitName

if (!TransitControllerAbstractClass.Exist(out GClass1642 vanillaTransitController))
{
// TODO: log error
return false;
return;
}

Player player = Singleton<GameWorld>.Instance.MainPlayer;
if (player == null)
{
// TODO: log error
return false;
return;
}


Dictionary<string, ProfileKey> profiles = [];
profiles.Add(player.ProfileId, new()
{
Expand All @@ -69,8 +77,7 @@ public static bool TransitTo(string locationId, string customTransitName)
string transitHash = Guid.NewGuid().ToString();
int playersCount = 1;

TransitPoint transit = Transit.Create(locationId, customTransitName);
vanillaTransitController.Transit(transit, playersCount, transitHash, profiles, player);
return true;
// TODO: log -> transit done
}
}
32 changes: 21 additions & 11 deletions PTT-Plugin/Services/CustomExfilServiceFika.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,50 @@
using System;
using System.Collections.Generic;
using PTT.Helpers;
using PTT.Data;

namespace PTT.Services;

public static class CustomExfilServiceFika
{
public static bool ExtractTo(ExfiltrationPoint exfiltrationPoint, string customExtractName)
public static void ExtractTo(ExfiltrationPoint exfil, ExfilTarget exfilTarget)
{
CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;
CoopPlayer coopPlayer = (CoopPlayer)Singleton<GameWorld>.Instance.MainPlayer;

string customExtractName = exfilTarget.GetCustomExitName(exfil);
// TODO: log -> started fika extraction on customExtractName

if (coopGame == null || coopPlayer == null)
{
return false;
// TODO: log error
return;
}

coopGame.ExitLocation = customExtractName;
coopGame.Extract(coopPlayer, exfiltrationPoint, null);
return true;
coopGame.ExitLocation = customExtractName; // not sure if it's needed
coopGame.Extract(coopPlayer, exfil, null);

// TODO: log -> fika extraction done
}

// TODO: fix this code smell (the 2 string params)
public static bool TransitTo(string locationId, string customTransitName)
public static void TransitTo(ExfiltrationPoint exfil, ExfilTarget exfilTarget)
{
CoopPlayer coopPlayer = (CoopPlayer)Singleton<GameWorld>.Instance.MainPlayer;

TransitPoint transit = Transit.Create(exfil, exfilTarget);
string customTransitName = transit.parameters.name;
// TODO: log -> started fika transit on customTransitName

if (coopPlayer == null)
{
// TODO: log error
return false;
return;
}

if (!TransitControllerAbstractClass.Exist(out GClass1642 vanillaTransitController))
{
// TODO: log error
return false;
return;
}

Dictionary<string, ProfileKey> profiles = [];
Expand All @@ -53,8 +63,8 @@ public static bool TransitTo(string locationId, string customTransitName)
string transitHash = Guid.NewGuid().ToString();
int playersCount = 1;

TransitPoint transit = Transit.Create(locationId, customTransitName);
vanillaTransitController.Transit(transit, playersCount, transitHash, profiles, coopPlayer);
return true;

// TODO: log -> fika transit done
}
}
Loading

0 comments on commit f70498f

Please sign in to comment.