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

Allow Dynamic DNS URL #2

Merged
merged 7 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 9 additions & 7 deletions Multiplayer/Components/MainMenu/HostGamePane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,36 @@ private void BuildUI()
GameObject dividerPrefab = goMMC.FindChildByName("Divider");
if (dividerPrefab == null)
{
Debug.Log("Divider not found!");
Multiplayer.LogError("Divider not found!");
return;
}

GameObject cbPrefab = goMMC.FindChildByName("CheckboxFreeCam");
if (cbPrefab == null)
{
Debug.Log("CheckboxFreeCam not found!");
Multiplayer.LogError("CheckboxFreeCam not found!");
return;
}

GameObject sliderPrefab = goMMC.FindChildByName("SliderLimitSession");
if (sliderPrefab == null)
{
Debug.Log("SliderLimitSession not found!");
Multiplayer.LogError("SliderLimitSession not found!");
return;
}

GameObject inputPrefab = MainMenuThingsAndStuff.Instance.renamePopupPrefab.gameObject.FindChildByName("TextFieldTextIcon");
if (inputPrefab == null)
{
Debug.Log("TextFieldTextIcon not found!");
Multiplayer.LogError("TextFieldTextIcon not found!");
return;
}


lcInstance = goMMC.FindChildByName("PaneRight Launcher").GetComponent<LauncherController>();
if (lcInstance == null)
{
Debug.Log("No Run Button");
Multiplayer.LogError("No Run Button");
return;
}
Sprite playSprite = lcInstance.runButton.FindChildByName("[icon]").GetComponent<Image>().sprite;
Expand Down Expand Up @@ -331,7 +331,8 @@ private void ValidateInputs(string text)

startButton.ToggleInteractable(valid);

Debug.Log($"Validated: {valid}");
//Multiplayer.Log($"HostPane validated: {valid}");

}


Expand Down Expand Up @@ -391,7 +392,8 @@ private void StartClick()


var ContinueGameRequested = lcInstance.GetType().GetMethod("OnRunClicked", BindingFlags.NonPublic | BindingFlags.Instance);
Debug.Log($"OnRunClicked exists: {ContinueGameRequested != null}");

//Multiplayer.Log($"OnRunClicked exists: {ContinueGameRequested != null}");
ContinueGameRequested?.Invoke(lcInstance, null);
}

Expand Down
2 changes: 1 addition & 1 deletion Multiplayer/Components/MainMenu/MainMenuThingsAndStuff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void SwitchToMenu(byte index)
[CanBeNull]
public Popup ShowRenamePopup()
{
Debug.Log("public Popup ShowRenamePopup() ...");
Multiplayer.Log("public Popup ShowRenamePopup() ...");
return ShowPopup(renamePopupPrefab);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void HandleAction(PopupClosedByAction action)
RequestAbortion();
return;
default:
Debug.LogError(string.Format("Unhandled action {0}", action), this);
Multiplayer.LogError(string.Format("Unhandled action {0}", action));
break;
}
}
Expand Down
148 changes: 80 additions & 68 deletions Multiplayer/Components/MainMenu/ServerBrowserPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
using Multiplayer.Networking.Data;
using DV;
using Multiplayer.Components.Networking.UI;


using System.Net;

namespace Multiplayer.Components.MainMenu
{
Expand Down Expand Up @@ -60,7 +59,7 @@ public class ServerBrowserPane : MonoBehaviour
private const int REFRESH_MIN_TIME = 10; //Stop refresh spam

//connection parameters
private string ipAddress;
private string address;
private int portNumber;
string password = null;
bool direct = false;
Expand All @@ -71,44 +70,7 @@ public class ServerBrowserPane : MonoBehaviour

private void Awake()
{
Multiplayer.Log("MultiplayerPane Awake()");
/*
*
* Temp testing code
*
*/

//GameObject chat = new GameObject("ChatUI", typeof(ChatGUI));
//chat.transform.SetParent(GameObject.Find("MenuOpeningScene").transform,false);

//////////Debug.Log("Instantiating Overlay");
//////////GameObject overlay = new GameObject("Overlay", typeof(ChatGUI));
//////////GameObject parent = GameObject.Find("MenuOpeningScene");
//////////if (parent != null)
//////////{
////////// overlay.transform.SetParent(parent.transform, false);
////////// Debug.Log("Overlay parent set to MenuOpeningScene");
//////////}
//////////else
//////////{
////////// Debug.LogError("MenuOpeningScene not found");
//////////}

//////////Debug.Log("Overlay instantiated with components:");
//////////foreach (Transform child in overlay.transform)
//////////{
////////// Debug.Log("Child: " + child.name);
////////// foreach (Transform grandChild in child)
////////// {
////////// Debug.Log("GrandChild: " + grandChild.name);
////////// }
//////////}

/*
*
* End Temp testing code
*
*/
//Multiplayer.Log("MultiplayerPane Awake()");
CleanUI();
BuildUI();

Expand All @@ -119,12 +81,12 @@ private void Awake()

private void OnEnable()
{
Multiplayer.Log("MultiplayerPane OnEnable()");
//Multiplayer.Log("MultiplayerPane OnEnable()");
if (!this.parentScroller)
{
Multiplayer.Log("Find ScrollRect");
//Multiplayer.Log("Find ScrollRect");
this.parentScroller = this.gridView.GetComponentInParent<ScrollRect>();
Multiplayer.Log("Found ScrollRect");
//Multiplayer.Log("Found ScrollRect");
}
this.SetupListeners(true);
this.serverIDOnRefresh = "";
Expand Down Expand Up @@ -361,7 +323,8 @@ private void JoinAction()
{
//not making a direct connection
direct = false;
ipAddress = selectedServer.ip;

address = selectedServer.ip;
portNumber = selectedServer.port;

ShowPasswordPopup();
Expand All @@ -376,7 +339,7 @@ private void JoinAction()

private void DirectAction()
{
Debug.Log($"DirectAction()");
//Debug.Log($"DirectAction()");
buttonDirectIP.ToggleInteractable(false);
buttonJoin.ToggleInteractable(false) ;

Expand All @@ -388,23 +351,22 @@ private void DirectAction()

private void IndexChanged(AGridView<IServerBrowserGameDetails> gridView)
{
Debug.Log($"Index: {gridView.SelectedModelIndex}");
//Debug.Log($"Index: {gridView.SelectedModelIndex}");
if (serverRefreshing)
return;

if (gridView.SelectedModelIndex >= 0)
{
Debug.Log($"Selected server: {gridViewModel[gridView.SelectedModelIndex].Name}");
//Multiplayer.Log($"Selected server: {gridViewModel[gridView.SelectedModelIndex].Name}");

selectedServer = gridViewModel[gridView.SelectedModelIndex];

UpdateDetailsPane();

//Check if we can connect to this server

Debug.Log($"server: \"{selectedServer.GameVersion}\" \"{selectedServer.MultiplayerVersion}\"");
Debug.Log($"client: \"{BuildInfo.BUILD_VERSION_MAJOR.ToString()}\" \"{Multiplayer.ModEntry.Version.ToString()}\"");
Debug.Log($"result: \"{selectedServer.GameVersion == BuildInfo.BUILD_VERSION_MAJOR.ToString()}\" \"{selectedServer.MultiplayerVersion == Multiplayer.ModEntry.Version.ToString()}\"");
Multiplayer.Log($"Server: \"{selectedServer.GameVersion}\" \"{selectedServer.MultiplayerVersion}\"");
Multiplayer.Log($"Client: \"{BuildInfo.BUILD_VERSION_MAJOR.ToString()}\" \"{Multiplayer.ModEntry.Version.ToString()}\"");
Multiplayer.Log($"Result: \"{selectedServer.GameVersion == BuildInfo.BUILD_VERSION_MAJOR.ToString()}\" \"{selectedServer.MultiplayerVersion == Multiplayer.ModEntry.Version.ToString()}\"");

bool canConnect = selectedServer.GameVersion == BuildInfo.BUILD_VERSION_MAJOR.ToString() &&
selectedServer.MultiplayerVersion == Multiplayer.ModEntry.Version.ToString();
Expand All @@ -425,7 +387,7 @@ private void UpdateDetailsPane()

if (selectedServer != null)
{
Debug.Log("Prepping Data");
//Multiplayer.Log("Prepping Data");
serverName.text = selectedServer.Name;

//note: built-in localisations have a trailing colon e.g. 'Game mode:'
Expand All @@ -442,14 +404,13 @@ private void UpdateDetailsPane()
details += "<br>";
details += selectedServer.ServerDetails;

Debug.Log("Finished Prepping Data");
//Multiplayer.Log("Finished Prepping Data");
detailsPane.text = details;
}
}

private void ShowIpPopup()
{
Debug.Log("In ShowIpPpopup");
var popup = MainMenuThingsAndStuff.Instance.ShowRenamePopup();
if (popup == null)
{
Expand All @@ -471,11 +432,46 @@ private void ShowIpPopup()

if (!IPv4Regex.IsMatch(result.data) && !IPv6Regex.IsMatch(result.data))
{
string inputUrl = result.data;

if (!inputUrl.StartsWith("http://") && !inputUrl.StartsWith("https://"))
{
inputUrl = "http://" + inputUrl;
}

bool isValidURL = Uri.TryCreate(inputUrl, UriKind.Absolute, out Uri uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

if (isValidURL)
{
string domainName = ExtractDomainName(result.data);
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(domainName);
IPAddress[] addresses = hostEntry.AddressList;

if (addresses.Length > 0)
{
string address2 = addresses[0].ToString();

address = address2;
Multiplayer.Log(address);

ShowPortPopup();
return;
}
}
catch (Exception ex)
{
Multiplayer.LogError($"An error occurred: {ex.Message}");
}
}

ShowOkPopup(Locale.SERVER_BROWSER__IP_INVALID, ShowIpPopup);
}
else
{
ipAddress = result.data;
address = result.data;
ShowPortPopup();
}
};
Expand Down Expand Up @@ -550,13 +546,13 @@ private void ShowPasswordPopup()
if (direct)
{
//store params for later
Multiplayer.Settings.LastRemoteIP = ipAddress;
Multiplayer.Settings.LastRemoteIP = address;
Multiplayer.Settings.LastRemotePort = portNumber;
Multiplayer.Settings.LastRemotePassword = result.data;

}

SingletonBehaviour<NetworkLifecycle>.Instance.StartClient(ipAddress, portNumber, result.data, false);
SingletonBehaviour<NetworkLifecycle>.Instance.StartClient(address, portNumber, result.data, false);

//ShowConnectingPopup(); // Show a connecting message
//SingletonBehaviour<NetworkLifecycle>.Instance.ConnectionFailed += HandleConnectionFailed;
Expand All @@ -568,15 +564,15 @@ private void ShowPasswordPopup()
private void HandleConnectionEstablished()
{
// Connection established, handle the UI or game state accordingly
Debug.Log("Connection established!");
Multiplayer.Log("Connection established!");
// HideConnectingPopup(); // Hide the connecting message
}

// Example of handling connection failure
private void HandleConnectionFailed()
{
// Connection failed, show an error message or handle the failure scenario
Debug.LogError("Connection failed!");
Multiplayer.LogError("Connection failed!");
// ShowConnectionFailedPopup();
}

Expand All @@ -592,21 +588,21 @@ IEnumerator GetRequest(string uri)

if (webRequest.isNetworkError)
{
Debug.Log(pages[page] + ": Error: " + webRequest.error);
Multiplayer.LogError(pages[page] + ": Error: " + webRequest.error);
}
else
{
Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
Multiplayer.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);

LobbyServerData[] response;

response = Newtonsoft.Json.JsonConvert.DeserializeObject<LobbyServerData[]>(webRequest.downloadHandler.text);

Debug.Log($"servers: {response.Length}");
Multiplayer.Log($"Serverbrowser servers: {response.Length}");

foreach (LobbyServerData server in response)
{
Debug.Log($"Name: {server.Name}\tIP: {server.ip}");
Multiplayer.Log($"Server name: {server.Name}\tIP: {server.ip}");
}

if (response.Length == 0)
Expand Down Expand Up @@ -685,14 +681,30 @@ private void FillDummyServers()
item.GameVersion = UnityEngine.Random.Range(1, 10) > 3 ? BuildInfo.BUILD_VERSION_MAJOR.ToString() : "97";
item.MultiplayerVersion = UnityEngine.Random.Range(1, 10) > 3 ? Multiplayer.ModEntry.Version.ToString() : "0.1.0";


Debug.Log(item.HasPassword);
gridViewModel.Add(item);
}

gridView.SetModel(gridViewModel);
}
}


private string ExtractDomainName(string input)
{
if (input.StartsWith("http://"))
{
input = input.Substring(7);
}
else if (input.StartsWith("https://"))
{
input = input.Substring(8);
}

int portIndex = input.IndexOf(':');
if (portIndex != -1)
{
input = input.Substring(0, portIndex);
}

return input;
}
}
}
Loading