Skip to content
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

Implement server types and P2P connection helper #123

Merged
merged 37 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
50bba72
Implement server types and P2P connection helper
trippyone Jan 10, 2024
96cacbf
Config changes
trippyone Jan 10, 2024
ad89149
Changed Local port used for nat punching and hosting.
trippyone Jan 11, 2024
72fce53
Streamline nat punching process + code refactoring
trippyone Jan 14, 2024
5c60fae
Punch before responding to client
trippyone Jan 14, 2024
9ba95a0
Improvements and refactoring
trippyone Jan 18, 2024
bc4674a
Use ipv4 for nat punching
trippyone Jan 19, 2024
a524757
Make sure to get the ipv4 address from STUN hostname
trippyone Jan 19, 2024
25460f7
Removed port from STUN's hostname
trippyone Jan 19, 2024
03797ad
Merge branch 'master' into server-config-and-p2p-helper
trippyone Jan 24, 2024
eb7afbb
(WIP/DRAFT) Changes for P2P
trippyone Jan 24, 2024
4bc4222
Merge branch 'master' into server-config-and-p2p-helper
trippyone Jan 28, 2024
6249bf0
First working implementation of nat punch and upnp + refactoring
trippyone Jan 28, 2024
b467984
(WIP) Support all methods of nat traversal through NatHelper
trippyone Jan 30, 2024
d5541eb
NatHelper stuff + refactoring
trippyone Jan 30, 2024
e82910f
Changes to NatHelper
trippyone Jan 31, 2024
7fd94cc
Merge of master to p2p helper
paulov-t Feb 1, 2024
1675b79
Fixed NatHelper
trippyone Feb 2, 2024
667132d
Revert "Fixed NatHelper"
trippyone Feb 2, 2024
b060321
Indentation fix
trippyone Feb 2, 2024
10913f8
Remove redundant null check
trippyone Feb 2, 2024
f68c536
Typo fix
trippyone Feb 2, 2024
c59dd1e
Add profileId check
trippyone Feb 2, 2024
e865734
Change External IP to use Open NAT
paulov-t Feb 2, 2024
fab1a3d
Re-arrange Matchmaker Host screen to use columns and rows properly
paulov-t Feb 2, 2024
2a72966
Comment out OnGUI_DrawPlayerFriendlyTags, it is causing errors
paulov-t Feb 2, 2024
ad370f4
Renable PeriodicEnableDisableGC
paulov-t Feb 2, 2024
81182c5
Increase MemoryThreshold slightly
paulov-t Feb 2, 2024
5b27844
Number of players to wait for message to "Number of Players"
paulov-t Feb 2, 2024
0614a3f
Merge branch 'server-config-and-p2p-helper' of https://github.com/sta…
paulov-t Feb 2, 2024
e2ccb54
Merge latest PRs into SC&P2PHelper branch
paulov-t Feb 5, 2024
dce5a7a
Get rid of GetHashCode warning
paulov-t Feb 5, 2024
1272c34
Add ability for Host to select server/client protocol
paulov-t Feb 5, 2024
24077d0
Fix Host not using the selected Protocol in Matchmaker
paulov-t Feb 5, 2024
367165a
Improve External IP Address finding
paulov-t Feb 5, 2024
f9f9ae4
Add P2P IPAddress options & Error handling if UDP cannot communicate …
paulov-t Feb 5, 2024
b240d29
Multiple changes
trippyone Feb 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions Source/Configuration/PluginConfigSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BepInEx.Configuration;
using BepInEx.Logging;
using StayInTarkov.Networking;
using System;
using System.Net;

Expand All @@ -9,6 +10,7 @@ namespace StayInTarkov.Configuration
/// Created by: Paulov
/// Description: Stores and Loads all of the Plugin config settings
/// </summary>

public class PluginConfigSettings
{
public ConfigFile Config { get; }
Expand Down Expand Up @@ -60,7 +62,7 @@ public void GetSettings()
("Advanced", "UseSITGarbageCollector", true, new ConfigDescription("Whether to use the Garbage Collector developed in to SIT OR leave it to BSG/Unity")).Value;

SITGCMemoryThreshold = StayInTarkovPlugin.Instance.Config.Bind
("Advanced", "SITGarbageCollectorMemoryThreshold", 512, new ConfigDescription("The SIT Garbage Collector memory threshold (in megabytes) between ticks before forcing a garbage collection")).Value;
("Advanced", "SITGarbageCollectorMemoryThreshold", 768, new ConfigDescription("The SIT Garbage Collector memory threshold (in megabytes) between ticks before forcing a garbage collection")).Value;

SITGCClearAssets = StayInTarkovPlugin.Instance.Config.Bind
("Advanced", "SITGarbageCollectorClearAssets", false, new ConfigDescription("Set SIT Garbage Collector to clear Unity assets. Reduces RAM usage but can be unstable!")).Value;
Expand Down Expand Up @@ -103,9 +105,11 @@ public bool SETTING_DEBUGShowPlayerList
public bool SETTING_ShowFeed { get; set; } = true;
public bool SETTING_ShowSITStatistics { get; set; } = true;
public bool ShowPing { get; set; } = true;
public HostProtocol SITHostProtocol { get; private set; }
public int SITWebSocketPort { get; set; } = 6970;
public int SITUDPPort { get; set; } = 6971;
public int SITNatHelperPort { get; set; } = 6971;
public int SITUdpPort { get; set; } = 6972;
//public ServerType SITServerType { get; set; } = ServerType.Relay;
public NatTraversalMethod SITNatTraversalMethod { get; set; } = NatTraversalMethod.Upnp;

public bool AllPlayersSpawnTogether { get; set; } = true;
public bool ArenaMode { get; set; } = false;
Expand All @@ -123,9 +127,6 @@ public int BlackScreenOnDeathTime
}
}

public string SITUDPHostIPV4 { get; set; }
public string SITUDPHostIPV6 { get; set; }

public void GetSettings()
{
SETTING_DEBUGSpawnDronesOnServer = StayInTarkovPlugin.Instance.Config.Bind
Expand Down Expand Up @@ -171,13 +172,10 @@ public void GetSettings()
Logger.LogDebug($"ArenaMode: {ArenaMode}");
Logger.LogDebug($"ForceHighPingMode: {ForceHighPingMode}");

SITHostProtocol = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITHostProtocol", HostProtocol.TCP, new ConfigDescription("SIT Host Protocol.")).Value;
SITWebSocketPort = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITPort", 6970, new ConfigDescription("SIT TCP/Websocket Port DEFAULT = 6970")).Value;
SITUDPPort = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITUDPPort", 6971, new ConfigDescription("SIT UDP Port DEFAULT = 6971")).Value;
SITUDPHostIPV4 = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITUDPHostIPV4", "127.0.0.1", new ConfigDescription("The IPv4 to use when hosting a UDP Coop Session")).Value;
SITUDPHostIPV6 = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITUDPHostIPV6", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", new ConfigDescription("The IPv6 to use when hosting a UDP Coop Session")).Value;

Logger.LogDebug($"SITWebSocketPort: {SITWebSocketPort}");
SITWebSocketPort = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITPort", 6970, new ConfigDescription("SIT TCP/Websocket Port")).Value;
SITNatHelperPort = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITNatHelperPort", 6971, new ConfigDescription("SIT Nat Helper Port")).Value;
//SITUdpPort = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITUdpPort", 6972, new ConfigDescription("SIT UDP port for P2P connection.")).Value;
//SITServerType = StayInTarkovPlugin.Instance.Config.Bind("Coop", "SITServerType", ServerType.Relay, new ConfigDescription("SIT Server Type (when hosting a match). Possible values: Relay, P2P")).Value;

if (ArenaMode)
{
Expand All @@ -186,14 +184,6 @@ public void GetSettings()
EnableAISpawnWaveSystem = false;
}
}

public enum HostProtocol
{
TCP,
UDP,
//Both
}
}

}
}
14 changes: 7 additions & 7 deletions Source/Coop/Airdrops/SITAirdropsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async void Start()
}

// If this is not the server, then this manager will have to wait for the packet to initialize stuff.
if (MatchmakerAcceptPatches.IsClient)
if (SITMatchmaking.IsClient)
return;

// The server will generate stuff ready for the packet
Expand Down Expand Up @@ -102,7 +102,7 @@ public async void Start()

public IEnumerator SendParamsToClients()
{
if (!MatchmakerAcceptPatches.IsServer)
if (!SITMatchmaking.IsServer)
yield break;

yield return new WaitForSeconds(AirdropParameters.TimeToStart);
Expand All @@ -123,11 +123,11 @@ public async void FixedUpdate()
return;

// If we are a client. Wait until the server has sent all the data.
if (MatchmakerAcceptPatches.IsClient && (ClientAirdropLootResultModel == null || ClientAirdropConfigModel == null))
if (SITMatchmaking.IsClient && (ClientAirdropLootResultModel == null || ClientAirdropConfigModel == null))
return;

// If we have all the parameters sent from the Server. Lets build the plane, box, container and loot
if (MatchmakerAcceptPatches.IsClient && !ClientLootBuilt)
if (SITMatchmaking.IsClient && !ClientLootBuilt)
{
ClientLootBuilt = true;
Logger.LogInfo("Client::Building Plane, Box, Factory and Loot.");
Expand All @@ -150,7 +150,7 @@ public async void FixedUpdate()
if (airdropPlane == null || AirdropBox == null || factory == null)
return;

if (MatchmakerAcceptPatches.IsServer || MatchmakerAcceptPatches.IsSinglePlayer)
if (SITMatchmaking.IsServer || SITMatchmaking.IsSinglePlayer)
{
AirdropParameters.Timer += 0.02f;

Expand Down Expand Up @@ -217,13 +217,13 @@ private void StartBox()

private void BuildLootContainer(AirdropConfigModel config)
{
if (MatchmakerAcceptPatches.IsClient)
if (SITMatchmaking.IsClient)
return;

var lootData = factory.GetLoot();

// Get the lootData. Sent to Clients.
if (MatchmakerAcceptPatches.IsServer)
if (SITMatchmaking.IsServer)
{
var packet = new Dictionary<string, object>();
packet.Add("serverId", CoopGameComponent.GetServerId());
Expand Down
6 changes: 3 additions & 3 deletions Source/Coop/Components/ActionPacketHandlerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ void ReplicateRaidTimer(Dictionary<string, object> packet)
if (coopGameComponent == null)
return;

if (MatchmakerAcceptPatches.IsClient)
if (SITMatchmaking.IsClient)
{
var sessionTime = new TimeSpan(long.Parse(packet["sessionTime"].ToString()));
Logger.LogInfo($"RaidTimer: Remaining session time {sessionTime.TraderFormat()}");
Expand Down Expand Up @@ -450,7 +450,7 @@ void ReplicateTimeAndWeather(Dictionary<string, object> packet)
if (coopGameComponent == null)
return;

if (MatchmakerAcceptPatches.IsClient)
if (SITMatchmaking.IsClient)
{
Logger.LogDebug(packet.ToJson());

Expand Down Expand Up @@ -548,7 +548,7 @@ void ReplicateArmoredTrainTime(Dictionary<string, object> packet)
if (coopGameComponent == null)
return;

if (MatchmakerAcceptPatches.IsClient)
if (SITMatchmaking.IsClient)
{
DateTime utcTime = new(long.Parse(packet["utcTime"].ToString()));

Expand Down
18 changes: 9 additions & 9 deletions Source/Coop/Components/CoopGameComponents/CoopGameComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
{
if (LocalGameInstance is CoopGame coopGame)
{
if (MatchmakerAcceptPatches.IsClient || coopGame.Bots.Count == 0)
if (SITMatchmaking.IsClient || coopGame.Bots.Count == 0)
return Players
.Values
.Where(x => ProfileIdsAI.Contains(x.ProfileId) && x.ActiveHealthController.IsAlive)
Expand Down Expand Up @@ -233,7 +233,7 @@
StartCoroutine(SendPlayerStatePacket());

// Start the SIT Garbage Collector
//StartCoroutine(PeriodicEnableDisableGC());
StartCoroutine(PeriodicEnableDisableGC());
GCHelpers.ClearGarbage(true, PluginConfigSettings.Instance.AdvancedSettings.SITGCClearAssets);

// Get a List of Interactive Objects (this is a slow method), so run once here to maintain a reference
Expand Down Expand Up @@ -550,9 +550,9 @@
}
else if (!world.MainPlayer.HealthController.IsAlive)
{
if (MatchmakerAcceptPatches.IsClient)
if (SITMatchmaking.IsClient)
quitState = EQuitState.YouAreDeadAsClient;
else if (MatchmakerAcceptPatches.IsServer)
else if (SITMatchmaking.IsServer)
quitState = EQuitState.YouAreDeadAsHost;
}
}
Expand All @@ -565,9 +565,9 @@
// Extractions
if (coopGame.ExtractedPlayers.Contains(world.MainPlayer.ProfileId))
{
if (MatchmakerAcceptPatches.IsClient)
if (SITMatchmaking.IsClient)
quitState = EQuitState.YouHaveExtractedOnlyAsClient;
else if (MatchmakerAcceptPatches.IsServer)
else if (SITMatchmaking.IsServer)
quitState = EQuitState.YouHaveExtractedOnlyAsHost;
}

Expand Down Expand Up @@ -597,7 +597,7 @@
RequestQuitGame = true;

// If you are the server / host
if (MatchmakerAcceptPatches.IsServer)
if (SITMatchmaking.IsServer)
{
// A host needs to wait for the team to extract or die!
if (PlayerUsers.Count() > 1 && (quitState == EQuitState.YouAreDeadAsHost || quitState == EQuitState.YouHaveExtractedOnlyAsHost))
Expand Down Expand Up @@ -1138,7 +1138,7 @@
var prc = otherPlayer.GetOrAddComponent<PlayerReplicatedComponent>();
prc.IsClientDrone = true;

if (!MatchmakerAcceptPatches.IsClient)
if (!SITMatchmaking.IsClient)
{
if (otherPlayer.ProfileId.StartsWith("pmc"))
{
Expand Down Expand Up @@ -1326,8 +1326,8 @@

public BaseLocalGame<GamePlayerOwner> LocalGameInstance { get; internal set; }

int GuiX = 10;

Check warning on line 1329 in Source/Coop/Components/CoopGameComponents/CoopGameComponent.cs

View workflow job for this annotation

GitHub Actions / Build-SIT (Debug)

The field 'CoopGameComponent.GuiX' is assigned but its value is never used
int GuiWidth = 400;

Check warning on line 1330 in Source/Coop/Components/CoopGameComponents/CoopGameComponent.cs

View workflow job for this annotation

GitHub Actions / Build-SIT (Debug)

The field 'CoopGameComponent.GuiWidth' is assigned but its value is never used

//public const int PING_LIMIT_HIGH = 125;
//public const int PING_LIMIT_MID = 100;
Expand All @@ -1339,9 +1339,9 @@
public bool ServerHasStopped { get; set; }
private bool ServerHasStoppedActioned { get; set; }

GUIStyle middleLabelStyle;

Check warning on line 1342 in Source/Coop/Components/CoopGameComponents/CoopGameComponent.cs

View workflow job for this annotation

GitHub Actions / Build-SIT (Debug)

Field 'CoopGameComponent.middleLabelStyle' is never assigned to, and will always have its default value null
GUIStyle middleLargeLabelStyle;

Check warning on line 1343 in Source/Coop/Components/CoopGameComponents/CoopGameComponent.cs

View workflow job for this annotation

GitHub Actions / Build-SIT (Debug)

The field 'CoopGameComponent.middleLargeLabelStyle' is never used
GUIStyle normalLabelStyle;

Check warning on line 1344 in Source/Coop/Components/CoopGameComponents/CoopGameComponent.cs

View workflow job for this annotation

GitHub Actions / Build-SIT (Debug)

The field 'CoopGameComponent.normalLabelStyle' is never used

//void OnGUI()
//{
Expand Down Expand Up @@ -1442,7 +1442,7 @@
return rect;

rect.y = 5;
GUI.Label(rect, $"SIT Coop: " + (MatchmakerAcceptPatches.IsClient ? "CLIENT" : "SERVER"));
GUI.Label(rect, $"SIT Coop: " + (SITMatchmaking.IsClient ? "CLIENT" : "SERVER"));
rect.y += 15;

// PING ------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,30 @@ void OnGUI()
switch (quitState)
{
case EQuitState.YourTeamIsDead:
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_TEAM_DEAD"], middleLargeLabelStyle);
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_TEAM_DEAD"].ToString(), middleLargeLabelStyle);
break;
case EQuitState.YouAreDead:
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_DEAD_SOLO"], middleLargeLabelStyle);
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_DEAD_SOLO"].ToString(), middleLargeLabelStyle);
break;
case EQuitState.YouAreDeadAsHost:
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_DEAD_HOST"], middleLargeLabelStyle);
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_DEAD_HOST"].ToString(), middleLargeLabelStyle);
break;
case EQuitState.YouAreDeadAsClient:
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_DEAD_CLIENT"], middleLargeLabelStyle);
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_DEAD_CLIENT"].ToString(), middleLargeLabelStyle);
break;
case EQuitState.YourTeamHasExtracted:
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_TEAM_EXTRACTED"], middleLargeLabelStyle);
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_TEAM_EXTRACTED"].ToString(), middleLargeLabelStyle);
break;
case EQuitState.YouHaveExtractedOnlyAsHost:
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_EXTRACTED_HOST"], middleLargeLabelStyle);
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_EXTRACTED_HOST"].ToString(), middleLargeLabelStyle);
break;
case EQuitState.YouHaveExtractedOnlyAsClient:
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_EXTRACTED_CLIENT"], middleLargeLabelStyle);
GUI.Label(rectEndOfGameMessage, StayInTarkovPlugin.LanguageDictionary["RAID_PLAYER_EXTRACTED_CLIENT"].ToString(), middleLargeLabelStyle);
break;
}

//OnGUI_DrawPlayerList(rect);
OnGUI_DrawPlayerFriendlyTags(rect);
//OnGUI_DrawPlayerFriendlyTags(rect);
//OnGUI_DrawPlayerEnemyTags(rect);

}
Expand All @@ -144,7 +144,7 @@ private Rect DrawPing(Rect rect)
return rect;

rect.y = 5;
GUI.Label(rect, $"SIT Coop: " + (MatchmakerAcceptPatches.IsClient ? "CLIENT" : "SERVER"));
GUI.Label(rect, $"SIT Coop: " + (SITMatchmaking.IsClient ? "CLIENT" : "SERVER"));
rect.y += 15;

// PING ------
Expand Down
Loading
Loading