Skip to content

Commit

Permalink
feat: moved piece settings to own class
Browse files Browse the repository at this point in the history
  • Loading branch information
MSchmoecker committed Nov 21, 2024
1 parent c0623fc commit ecb81ed
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 47 deletions.
59 changes: 14 additions & 45 deletions JotunnLib/Entities/CustomPiece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reflection;
using Jotunn.Configs;
using Jotunn.Managers;
using Jotunn.Settings;
using Jotunn.Utils;
using UnityEngine;

Expand Down Expand Up @@ -66,11 +65,10 @@ public string Category
/// </summary>
public bool FixReference { get; set; }

public Setting<bool> SettingsEnabled { get; set; }

public Setting<string> CategorySetting { get; set; }

public Setting<string> PieceTableSetting { get; set; }
/// <summary>
/// The in-game settings for this custom piece.
/// </summary>
public CustomPieceSettings Settings { get; set; }

/// <summary>
/// Indicator if references from configs should get replaced
Expand Down Expand Up @@ -104,7 +102,7 @@ public CustomPiece(GameObject piecePrefab, string pieceTable, bool fixReference)
Piece = piecePrefab.GetComponent<Piece>();
PieceTable = pieceTable;
FixReference = fixReference;
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand All @@ -122,9 +120,8 @@ public CustomPiece(GameObject piecePrefab, PieceConfig pieceConfig) : base(Assem
FixReference = false;
FixConfig = true;
Category = pieceConfig.Category;

pieceConfig.Apply(piecePrefab);
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand All @@ -142,9 +139,8 @@ public CustomPiece(GameObject piecePrefab, bool fixReference, PieceConfig pieceC
FixReference = fixReference;
FixConfig = true;
Category = pieceConfig.Category;

pieceConfig.Apply(piecePrefab);
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand Down Expand Up @@ -172,7 +168,7 @@ public CustomPiece(AssetBundle assetBundle, string assetName, string pieceTable,
Piece = PiecePrefab.GetComponent<Piece>();
PieceTable = pieceTable;
FixReference = fixReference;
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand All @@ -198,9 +194,8 @@ public CustomPiece(AssetBundle assetBundle, string assetName, PieceConfig pieceC
FixReference = false;
FixConfig = true;
Category = pieceConfig.Category;

pieceConfig.Apply(PiecePrefab);
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand All @@ -226,9 +221,8 @@ public CustomPiece(AssetBundle assetBundle, string assetName, bool fixReference,
FixReference = fixReference;
FixConfig = true;
Category = pieceConfig.Category;

pieceConfig.Apply(PiecePrefab);
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand All @@ -254,7 +248,7 @@ public CustomPiece(string name, bool addZNetView, string pieceTable) : base(Asse
Piece = PiecePrefab.AddComponent<Piece>();
Piece.m_name = name;
PieceTable = pieceTable;
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand All @@ -278,9 +272,8 @@ public CustomPiece(string name, bool addZNetView, PieceConfig pieceConfig) : bas
PieceTable = pieceConfig.PieceTable;
FixConfig = true;
Category = pieceConfig.Category;

pieceConfig.Apply(PiecePrefab);
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand All @@ -305,7 +298,7 @@ public CustomPiece(string name, string baseName, string pieceTable) : base(Assem

Piece = PiecePrefab.GetComponent<Piece>();
PieceTable = pieceTable;
CreateSettings();
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand All @@ -329,25 +322,8 @@ public CustomPiece(string name, string baseName, PieceConfig pieceConfig) : base
PieceTable = pieceConfig.PieceTable;
FixConfig = true;
Category = pieceConfig.Category;

pieceConfig.Apply(PiecePrefab);
CreateSettings();
}

private void CreateSettings()
{
SettingsEnabled = new BepInExSetting<bool>(SourceMod, PiecePrefab.name, "Enabled", false, $"Enable settings for {PiecePrefab.name}", 10);
SettingsEnabled.OnChanged += () =>
{
BindSettings();
ConfigManagerUtils.BuildSettingList();
};

CategorySetting = new BepInExDropdownSetting<string>(SourceMod, PiecePrefab.name, "Category", Category, PieceCategories.GetNames().Keys, $"Tool Category of {PiecePrefab.name}", 9);
CategorySetting.OnChanged += () => Category = CategorySetting.Value;

PieceTableSetting = new BepInExDropdownSetting<string>(SourceMod, PiecePrefab.name, "Tool", PieceTables.GetDisplayName(PieceTable), PieceTables.GetNames().Keys, $"Tool of of {PiecePrefab.name}", 8);
PieceTableSetting.OnChanged += () => PieceTable = PieceTableSetting.Value;
Settings = new CustomPieceSettings(this);
}

/// <summary>
Expand Down Expand Up @@ -391,13 +367,6 @@ public bool IsValid()
return valid;
}

internal void BindSettings()
{
SettingsEnabled?.Bind();
CategorySetting?.UpdateBinding(SettingsEnabled?.Value ?? false);
PieceTableSetting?.UpdateBinding(SettingsEnabled?.Value ?? false);
}

/// <summary>
/// Helper method to determine if a prefab with a given name is a custom piece created with Jötunn.
/// </summary>
Expand Down
58 changes: 58 additions & 0 deletions JotunnLib/Entities/CustomPieceSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Jotunn.Configs;
using Jotunn.Settings;
using Jotunn.Utils;
using UnityEngine;

namespace Jotunn.Entities
{
/// <summary>
/// Class for in-game settings for custom pieces
/// </summary>
public class CustomPieceSettings
{
/// <summary>
/// Setting to enable or disable the configuration for this piece
/// </summary>
public Setting<bool> SettingsEnabled { get; set; }

/// <summary>
/// Setting for the category of this piece
/// </summary>
public Setting<string> Category { get; set; }

/// <summary>
/// Setting for the piece table of this piece
/// </summary>
public Setting<string> PieceTable { get; set; }

/// <summary>
/// Create a new settings object for a custom piece
/// </summary>
/// <param name="piece"></param>
public CustomPieceSettings(CustomPiece piece)
{
var sourceMod = piece.SourceMod;
var prefabName = piece.PiecePrefab.name;

SettingsEnabled = new BepInExSetting<bool>(sourceMod, prefabName, "Enabled", false, $"Enable settings for {prefabName}", 10);
SettingsEnabled.OnChanged += () =>
{
Bind();
ConfigManagerUtils.BuildSettingList();
};

Category = new BepInExDropdownSetting<string>(sourceMod, prefabName, "Category", piece.Category, PieceCategories.GetNames().Keys, $"Tool Category", 9);
Category.OnChanged += () => piece.Category = Category.Value;

PieceTable = new BepInExDropdownSetting<string>(sourceMod, prefabName, "Tool", PieceTables.GetDisplayName(piece.PieceTable), PieceTables.GetNames().Keys, $"Tool prefab", 8);
PieceTable.OnChanged += () => piece.PieceTable = PieceTable.Value;
}

internal void Bind()
{
SettingsEnabled?.Bind();
Category?.UpdateBinding(SettingsEnabled?.Value ?? false);
PieceTable?.UpdateBinding(SettingsEnabled?.Value ?? false);
}
}
}
5 changes: 3 additions & 2 deletions JotunnLib/Managers/PieceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ private void BindSettings()
plugin.Instance.Config.SaveOnConfigSet = false;
}

piece.BindSettings();
piece.Settings?.Bind();
}

foreach (var sourceMod in saveOnConfigSet.Keys)
Expand Down Expand Up @@ -653,7 +653,8 @@ private void RegisterPieceInPieceTable(GameObject prefab, string pieceTable, str
{
if (!GetPieceTable(pieceTable))
{
Logger.LogWarning($"Could not find PieceTable {pieceTable}");
Logger.LogWarning(sourceMod, $"Could not find PieceTable {pieceTable}, assigning piece to Hammer instead");
AddToPieceTable(piece, Jotunn.Configs.PieceTables.Hammer);
}
}

Expand Down
10 changes: 10 additions & 0 deletions JotunnLib/Settings/BepInExSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Jotunn.Settings
{
/// <summary>
/// Base class for in-game BepInEx settings
/// </summary>
/// <typeparam name="T"></typeparam>
public class BepInExSetting<T> : Setting<T>
{
public string Section { get; set; }
Expand All @@ -25,6 +29,9 @@ public BepInExSetting(BepInPlugin sourceMod, string section, string key, T defau
AdminOnly = adminOnly;
}

/// <summary>
/// Bind this setting to the BepInEx configuration system
/// </summary>
public override void Bind()
{
if (entry != null)
Expand All @@ -38,6 +45,9 @@ public override void Bind()
Value = entry.Value;
}

/// <summary>
/// Removes this setting from the BepInEx configuration system
/// </summary>
public override void Unbind()
{
if (entry == null)
Expand Down
32 changes: 32 additions & 0 deletions JotunnLib/Settings/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@

namespace Jotunn.Settings
{
/// <summary>
/// Base class for in-game settings
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class Setting<T>
{
/// <summary>
/// The mod, where the object this setting applies to
/// </summary>
public BepInPlugin SourceMod { get; set; }

private T value;

/// <summary>
/// The value of this setting.
/// When set, the <see cref="OnChanged"/> event is invoked.<br />
/// <br />
/// The value depends on the underlying configuration system and might be not initialized immediately.
/// </summary>
public T Value
{
get => value;
Expand All @@ -19,13 +32,24 @@ public T Value
}
}

/// <summary>
/// Event that is invoked when the value of this setting changes
/// </summary>
public event Action OnChanged;

/// <summary>
/// Create a new setting object
/// </summary>
/// <param name="sourceMod"></param>
public Setting(BepInPlugin sourceMod)
{
this.SourceMod = sourceMod;
}

/// <summary>
/// Update the binding of this setting and applies it to the underlying configuration system
/// </summary>
/// <param name="enabled">whether the setting should be bound</param>
public void UpdateBinding(bool enabled)
{
if (enabled)
Expand All @@ -38,8 +62,16 @@ public void UpdateBinding(bool enabled)
}
}

/// <summary>
/// Bind this setting to the underlying configuration system.
/// E.g. maps and saves the value to a config file
/// </summary>
public abstract void Bind();

/// <summary>
/// Unbind this setting from the underlying configuration system.
/// E.g. removes the value from a config file
/// </summary>
public abstract void Unbind();
}
}

0 comments on commit ecb81ed

Please sign in to comment.