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

Add Module/Network Layer Settings #122

Open
wants to merge 1 commit into
base: v1.10.0
Choose a base branch
from
Open
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
63 changes: 61 additions & 2 deletions LabFusion/src/Menu/Pages/MenuSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

using LabFusion.Data;
using LabFusion.Downloading.ModIO;
using LabFusion.Entities;
using LabFusion.Extensions;
using LabFusion.Marrow;
using LabFusion.Marrow.Proxies;
using LabFusion.Network;
using LabFusion.Preferences.Client;
using LabFusion.Representation;
using LabFusion.SDK.Modules;
using LabFusion.Utilities;
using LabFusion.Voice;

Expand All @@ -20,12 +23,11 @@ public static void PopulateSettings(GameObject settingsPage)
{
var rootPage = settingsPage.transform.Find("scrollRect_Options/Viewport/Content").GetComponent<PageElement>();

// Base Fusion Settings
var clientPage = rootPage.AddPage();

PopulateClientSettings(clientPage);

var downloadingPage = rootPage.AddPage();

PopulateDownloadingSettings(downloadingPage);

#if DEBUG
Expand All @@ -34,12 +36,69 @@ public static void PopulateSettings(GameObject settingsPage)
PopulateDebugSettings(debugPage);
#endif

// Network Layer Settings
var networkLayerPage = rootPage.AddPage();
foreach (var networkLayer in NetworkLayer.Layers)
{
FusionLogger.Log($"Created settings for {networkLayer.Title}");
var networkLayerGroup = networkLayerPage.AddElement<GroupElement>(networkLayer.Title);
// If exception is thrown, then settings arent implemented, and a category should not be added
try
{
networkLayer.OnCreateSettings(networkLayerGroup);
}
catch (Exception ex)
{
networkLayerPage.RemoveElement(networkLayerGroup);

if (ex is NotImplementedException)
{
#if DEBUG
FusionLogger.Log($"Layer {networkLayer.Title} has no settings to implement.");
#endif
}
else
{
FusionLogger.LogException($"creating {networkLayer.Title} settings", ex);
}
}
}

// Module Settings
var modulePage = rootPage.AddPage();
foreach (var module in ModuleManager.Modules)
{
var moduleGroup = modulePage.AddElement<GroupElement>(module.Name);
// If exception is thrown, then settings arent implemented, and a category should not be added
try
{
module.OnCreateSettings(moduleGroup);
}
catch (Exception ex)
{
modulePage.RemoveElement(moduleGroup);

if (ex is NotImplementedException)
{
#if DEBUG
FusionLogger.Log($"Layer {module.Name} has no settings to implement.");
#endif
}
else
{
FusionLogger.LogException($"creating {module.Name} settings", ex);
}
}
}

// Categories
var categoriesRoot = settingsPage.transform.Find("scrollRect_Categories/Viewport/Content").GetComponent<PageElement>();
var categoriesPage = categoriesRoot.AddPage("Default");

categoriesPage.AddElement<FunctionElement>("Client").Link(clientPage).WithColor(Color.white);
categoriesPage.AddElement<FunctionElement>("Downloading").Link(downloadingPage).WithColor(Color.cyan);
categoriesPage.AddElement<FunctionElement>("Network Layers").Link(networkLayerPage).WithColor(Color.white);
categoriesPage.AddElement<FunctionElement>("Modules").Link(modulePage).WithColor(Color.white);

#if DEBUG
categoriesPage.AddElement<FunctionElement>("Debug").Link(debugPage).WithColor(Color.red);
Expand Down
11 changes: 11 additions & 0 deletions LabFusion/src/Network/Layers/NetworkLayer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;

using LabFusion.Data;
using LabFusion.Marrow.Proxies;
using LabFusion.Player;
using LabFusion.Utilities;
using LabFusion.Voice;
Expand Down Expand Up @@ -133,6 +134,16 @@ public virtual bool TryGetFallback(out NetworkLayer fallback)
/// <returns></returns>
public virtual bool IsFriend(ulong userId) => false;

/// <summary>
/// Allows the creation of elements in the Network Layers category of the Settings menu.
/// Added elements are placed in a premade group with the layers's name.
/// </summary>
/// <param name="layerGroup"></param>
public virtual void OnCreateSettings(GroupElement layerGroup)
{
throw new NotImplementedException();
}

/// <summary>
/// Sends the message to the specified user if this is a server.
/// </summary>
Expand Down
23 changes: 21 additions & 2 deletions LabFusion/src/Network/Layers/Proxy/ProxyNetworkLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
using LabFusion.Preferences.Client;
using LabFusion.Voice;
using LabFusion.Voice.Unity;
using LabFusion.Marrow.Proxies;
using LabFusion.Menu;
using LabFusion.Senders;

using MelonLoader;

using LabFusion.Senders;

using Steamworks;

using LiteNetLib;
Expand Down Expand Up @@ -78,6 +79,24 @@ public override void OnInitializeLayer()
_matchmaker = new ProxyMatchmaker(_lobbyManager);
}

public override void OnCreateSettings(GroupElement layerGroup)
{
// Don't create UI if on PC
if (!PlatformHelper.IsAndroid)
base.OnCreateSettings(layerGroup);

var portElement = layerGroup.AddElement<IntElement>("Proxy Port")
.AsPref(ClientSettings.ProxyPort)
.WithIncrement(1)
.WithLimits(0, 65535);

layerGroup.AddElement<IntElement>("Increment")
.WithIncrement(1)
.WithLimits(0, 200)
.WithValue(10)
.OnValueChanged += (val) => portElement.Increment = val;
}

public IEnumerator DiscoverServer()
{
int port = ClientSettings.ProxyPort.Value;
Expand Down
14 changes: 13 additions & 1 deletion LabFusion/src/SDK/Modules/Module.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace LabFusion.SDK.Modules;
using LabFusion.Marrow.Proxies;

namespace LabFusion.SDK.Modules;

/// <summary>
/// The base class for a Fusion module.
Expand Down Expand Up @@ -32,4 +34,14 @@ protected virtual void OnModuleRegistered() { }
/// Called when the module is unregistered. Use this to clean up anything affected by the module.
/// </summary>
protected virtual void OnModuleUnregistered() { }

/// <summary>
/// Allows the creation of elements in the Modules category of the Settings menu.
/// Added elements are placed in a premade group with the module's name.
/// </summary>
/// <param name="moduleGroup"></param>
public virtual void OnCreateSettings(GroupElement moduleGroup)
{
throw new NotImplementedException();
}
}