Skip to content

Commit

Permalink
Swap to using nautilus's own config systems
Browse files Browse the repository at this point in the history
This system is more expandable, gives more examples to users looking to use the system themselves, and you should always use your own API
  • Loading branch information
EldritchCarMaker committed Dec 29, 2024
1 parent a1b4e2a commit 40dfdb5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 66 deletions.
1 change: 1 addition & 0 deletions Nautilus/Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Nautilus;
public class Initializer : BaseUnityPlugin
{
private static readonly Harmony _harmony = new(PluginInfo.PLUGIN_GUID);
internal static NautilusConfig ConfigFile { get; private set; } = OptionsPanelHandler.RegisterModOptions<NautilusConfig>();

#if BELOWZERO
static Initializer()
Expand Down
29 changes: 29 additions & 0 deletions Nautilus/NautilusConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Nautilus.Json;
using Nautilus.Options;
using Nautilus.Options.Attributes;
using Nautilus.Patchers;

namespace Nautilus;
[ConfigFile("NautilusConfig")]
internal class NautilusConfig : ConfigFile
{
[Toggle(Label = "Enable debug logs")]
[OnChange(nameof(OnDebugLogChange))]
public bool enableDebugLogs = false;

[Toggle(Label = "Enable mod databank entries", Tooltip = "No seriously this needs a tooltip. I don't even know what this one does. And from a quick glance it looks like nothing")]
public bool enableModDatabankEntries = true;//This whole option doesn't do anything now, because it didn't look like it did anything before either.

[Choice(Label = "Extra item info", Options = new[] { "Mod name (default)", "Mod name and item ID", "Nothing", }/*, Tooltip = "Wait there's no tooltip for this? Wild, there should probably be one"*/)]
[OnChange(nameof(OnItemInfoChange))]
public string extraItemInfo = "Mod name (default)";

private void OnDebugLogChange(ToggleChangedEventArgs args)
{
Utility.InternalLogger.SetDebugging(args.Value);
}
private void OnItemInfoChange(ChoiceChangedEventArgs<string> args)
{
TooltipPatcher.RefreshExtraItemInfo(args.Value);
}
}
11 changes: 0 additions & 11 deletions Nautilus/Patchers/OptionsPanelPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,6 @@ internal static void AddTabs_Postfix(uGUI_OptionsPanel __instance)
modsTab = optionsPanel.AddTab("Mods");
}

// Maybe this could be split into its own file to handle nautilus options, or maybe it could be removed alltogether
optionsPanel.AddHeading(modsTab, "Nautilus");
optionsPanel.AddToggleOption(modsTab, "Enable debug logs", Utility.InternalLogger.EnableDebugging, Utility.InternalLogger.SetDebugging);
optionsPanel.AddToggleOption(modsTab, "Enable mod databank entries", ModDatabankHandler._isEnabled);
optionsPanel.AddChoiceOption(modsTab, "Extra item info", new string[]
{
"Mod name (default)",
"Mod name and item ID",
"Nothing"
}, (int)TooltipPatcher.ExtraItemInfoOption, (i) => TooltipPatcher.SetExtraItemInfo((TooltipPatcher.ExtraItemInfo)i));

// adding all other options here
modOptions.Values.ForEach(options => options.AddOptionsToPanel(optionsPanel, modsTab));
}
Expand Down
68 changes: 13 additions & 55 deletions Nautilus/Patchers/TooltipPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -111,30 +112,22 @@ internal enum ExtraItemInfo

internal static ExtraItemInfo ExtraItemInfoOption { get; private set; }

internal static void SetExtraItemInfo(ExtraItemInfo value)
internal static void RefreshExtraItemInfo(string configValue)
{
string configPath =
Path.Combine(Path.Combine(BepInEx.Paths.ConfigPath, Assembly.GetExecutingAssembly().GetName().Name),
"ExtraItemInfo.txt");
//We store strings and swap to an enum so that the user gets a more friendly string, while enums are faster to operate on
var stringToEnum = new Dictionary<string, ExtraItemInfo>()
{
{ "Mod name (default)", ExtraItemInfo.ModName },
{ "Mod name and item ID", ExtraItemInfo.ModNameAndItemID },
{ "Nothing", ExtraItemInfo.Nothing },
};

string text;
switch (value)
if(!stringToEnum.TryGetValue(configValue, out ExtraItemInfo extraItemInfo))
{
case ExtraItemInfo.ModName:
text = "Mod name (default)";
break;
case ExtraItemInfo.ModNameAndItemID:
text = "Mod name and item ID";
break;
case ExtraItemInfo.Nothing:
text = "Nothing";
break;
default:
return;
throw new System.NotImplementedException("tooltip patcher value unrecognized. This error should never happen but should still have proper handling");
}

File.WriteAllText(configPath, text);
ExtraItemInfoOption = value;
ExtraItemInfoOption = extraItemInfo;
}

internal static bool Initialized = false;
Expand All @@ -148,42 +141,7 @@ internal static void Initialize()

Initialized = true;

var nautilusFolder = Path.Combine(BepInEx.Paths.ConfigPath, Assembly.GetExecutingAssembly().GetName().Name);
Directory.CreateDirectory(nautilusFolder);

var configPath = Path.Combine(nautilusFolder, "ExtraItemInfo.txt");

if (!File.Exists(configPath))
{
File.WriteAllText(configPath, "Mod name (default)");
ExtraItemInfoOption = ExtraItemInfo.ModName;

return;
}

var fileContents = File.ReadAllText(configPath);

switch (fileContents)
{
case "Mod name (default)":
ExtraItemInfoOption = ExtraItemInfo.ModName;
InternalLogger.Log($"Extra item info set to: {fileContents}", LogLevel.Info);
break;
case "Mod name and item ID":
ExtraItemInfoOption = ExtraItemInfo.ModNameAndItemID;
InternalLogger.Log($"Extra item info set to: {fileContents}", LogLevel.Info);
break;
case "Nothing":
ExtraItemInfoOption = ExtraItemInfo.Nothing;
InternalLogger.Log($"Extra item info set to: {fileContents}", LogLevel.Info);
break;
default:
File.WriteAllText(configPath, "Mod name (default)");
ExtraItemInfoOption = ExtraItemInfo.ModName;
InternalLogger.Log("Error reading ExtraItemInfo.txt configuration file. Defaulted to mod name.",
LogLevel.Warning);
break;
}
RefreshExtraItemInfo(Initializer.ConfigFile.extraItemInfo);
}

#endregion
Expand Down

0 comments on commit 40dfdb5

Please sign in to comment.