Skip to content

Commit

Permalink
feat: Mod Databank entries (#397)
Browse files Browse the repository at this point in the history
* PR

* Update PDAEncyclopediaPatcher.cs

* Nitpicks.

* Other nitpicks.

* Fix else for Lee.

* GUID thing.

* Fixed BZ compile error.

* Fix something before bed.

* Whoops, forgot something.

* Stuff with docs.

* Forgot to make static haha.

* fix: string stuff.

* fix: forgot something.

* make ECM happy.

* Update PDAEncyclopediaTabPatcher.cs

* Update PDAEncyclopediaTabPatcher.cs

* Update and rename ModDataBankHandler.cs to ModDatabankHandler.cs

* Update OptionsPanelPatcher.cs

* depend on different PR.

* Update PDAHandler.cs

* Update PDAHandler.cs

* realization that I forgot to change something

* Update ModDatabankHandler.cs

* Update PDAEncyclopediaTabPatcher.cs

* Update ConfigExamples.cs

* compile error fixes

---------

Co-authored-by: Lee23 <[email protected]>
  • Loading branch information
jonahnm and LeeTwentyThree committed Jun 12, 2023
1 parent ee7d204 commit 2996e3f
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Example mod/ConfigExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Awake()
{

LogSource = Logger;

ModDatabankHandler.RegisterMod(Info);
/// Here, we are setting up a instance of <see cref="Config"/>, which will automatically generate an
/// options menu using Attributes. The values in this instance will be updated whenever the user changes
/// the corresponding option in the menu.
Expand Down Expand Up @@ -304,4 +304,4 @@ private void MyGameObjectCreatedEvent(GameObjectCreatedEventArgs e)
ConfigExamples.LogSource.LogInfo("GameObject was created");
ConfigExamples.LogSource.LogInfo($"{e.Id}: {e.Value}");
}
}
}
110 changes: 110 additions & 0 deletions Nautilus/Handlers/ModDatabankHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using BepInEx;
using Nautilus.Utility;
using System.Collections.Generic;
using UnityEngine;

namespace Nautilus.Handlers;
/// <summary>
/// A handler class for adding databank entries for mods.
/// </summary>
public static class ModDatabankHandler
{
private static bool _isInit;
private static List<PDAEncyclopedia.EntryData> _waitList = new();
internal static bool _isEnabled = true;
internal static void Initialize()
{
LanguageHandler.SetLanguageLine("EncyPath_Mods", "Mods");
_isInit = true;
foreach (var data in _waitList)
{
CompleteRegister(data);
}
}
private static void CompleteRegister(PDAEncyclopedia.EntryData data)
{
if (_isInit && _isEnabled)
{
InternalLogger.Info($"{data.key} entry added.");
PDAHandler.AddEncyclopediaEntry(data);
#if BZ_STABLE
PDAEncyclopedia.Add(data.key, true, false);
#else
PDAEncyclopedia.Add(data.key, true);
#endif
}
else
{
_waitList.Add(data);
}
}
/// <summary>
/// Automatically adds info about your mod to the game's databank under a tab named Mods using your mod's PluginInfo.
/// </summary>
/// <param name="info">The PluginInfo for your mod. Pass in Info from your BepInPlugin class</param>
public static void RegisterMod(BepInEx.PluginInfo info)
{
var bepinplugindata = info.Metadata;
var entrydata = new PDAEncyclopedia.EntryData()
{
image = null,
key = bepinplugindata.GUID,
unlocked = true,
path = "Mods",
nodes = PDAEncyclopedia.ParsePath("Mods")
};
LanguageHandler.SetLanguageLine($"Ency_{bepinplugindata.GUID}", $"{bepinplugindata.Name} {bepinplugindata.Version.ToString()}");
LanguageHandler.SetLanguageLine($"EncyDesc_{bepinplugindata.GUID}", "A BepInEx plugin using Nautilus.");
CompleteRegister(entrydata);
}
/// <summary>
/// Automatically adds info about your mod to the game's databank under a tab named Mods using supplied ModData instance.
/// </summary>
/// <param name="data"></param>
public static void RegisterMod(ModData data)
{
var entrydata = new PDAEncyclopedia.EntryData()
{
image = data.image,
key = data.guid,
unlocked = true,
path = "Mods",
nodes = PDAEncyclopedia.ParsePath("Mods")
};
if(data.desc != null)
{
LanguageHandler.SetLanguageLine($"EncyDesc_{data.guid}", data.desc);
}
if(data.name != null)
{
LanguageHandler.SetLanguageLine($"Ency_{data.guid}", $"{data.name} {data.version ?? ""}");
}
CompleteRegister(entrydata);
}
/// <summary>
/// Data for the encyclopedia entry of your mod.
/// </summary>
public record struct ModData
{
/// <summary>
/// Name of your mod, not optional.
/// </summary>
public required string name;
/// <summary>
/// GUID, just an identifier, not optional.
/// </summary>
public required string guid;
/// <summary>
/// Mod version, optional.
/// </summary>
public string version;
/// <summary>
/// Mod description, optional.
/// </summary>
public string desc;
/// <summary>
/// Databank image, optional.
/// </summary>
public Texture2D image;
}
}
4 changes: 2 additions & 2 deletions Nautilus/Handlers/PDAHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void EditFragmentScanTime(TechType techType, float scanTime)

PDAPatcher.FragmentScanTime[techType] = scanTime;

if (uGUI.isMainLevel)
if(uGUI.isMainLevel)
PDAPatcher.InitializePostfix();
}

Expand Down Expand Up @@ -220,7 +220,7 @@ public static void AddEncyclopediaEntry(PDAEncyclopedia.EntryData entry)
}

PDAEncyclopediaPatcher.CustomEntryData[entry.key] = entry;

if(PDAEncyclopedia.initialized)
PDAEncyclopediaPatcher.InitializePostfix();
}
Expand Down
2 changes: 1 addition & 1 deletion Nautilus/Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class Initializer : BaseUnityPlugin
#elif BELOWZERO
InternalLogger.Info($"Loading v{PluginInfo.PLUGIN_VERSION} for BelowZero");
#endif

PrefabDatabasePatcher.PrePatch(_harmony);
EnumPatcher.Patch(_harmony);
CraftDataPatcher.Patch(_harmony);
Expand All @@ -57,6 +56,7 @@ public class Initializer : BaseUnityPlugin
EatablePatcher.Patch(_harmony);
MaterialUtils.Patch();
FontReferencesPatcher.Patch(_harmony);
PDAEncyclopediaTabPatcher.Patch(_harmony);
StoryGoalPatcher.Patch(_harmony); // TO-DO: Story goal handling for Below Zero
}
}
4 changes: 3 additions & 1 deletion Nautilus/Patchers/OptionsPanelPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Reflection;
using HarmonyLib;
using Nautilus.Handlers;
using Nautilus.Options;
using Nautilus.Utility;
using Newtonsoft.Json;
Expand Down Expand Up @@ -91,6 +92,7 @@ internal static void AddTabs_Postfix(uGUI_OptionsPanel __instance)
// 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)",
Expand Down Expand Up @@ -407,4 +409,4 @@ private static void SetVisibleTab_Postfix(uGUI_TabbedControlsPanel __instance, i
RestorePos(__instance, tabIndex);
}
}
}
}
2 changes: 1 addition & 1 deletion Nautilus/Patchers/PDAEncyclopediaPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ internal static void InitializePostfix()
}
}
}
}
}
19 changes: 19 additions & 0 deletions Nautilus/Patchers/PDAEncyclopediaTabPatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using HarmonyLib;
using Nautilus.Handlers;

namespace Nautilus.Patchers;

internal class PDAEncyclopediaTabPatcher
{
internal static void Patch(Harmony harmony)
{
harmony.PatchAll(typeof(PDAEncyclopediaTabPatcher));
}
[HarmonyPatch(typeof(uGUI_EncyclopediaTab))]
[HarmonyPatch(nameof(uGUI_EncyclopediaTab.Awake))]
[HarmonyPostfix]
internal static void EncyTabAwakePostfix(uGUI_EncyclopediaTab __instance)
{
ModDatabankHandler.Initialize();
}
}

0 comments on commit 2996e3f

Please sign in to comment.