Skip to content

Commit

Permalink
Fixed NexusId NRE
Browse files Browse the repository at this point in the history
  • Loading branch information
Digitalroot committed Oct 18, 2021
1 parent 76519ff commit 81a0a0e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 5 deletions.
106 changes: 106 additions & 0 deletions src/Digitalroot.Valheim.PluginInfo/ConfigurationManagerAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
namespace Digitalroot.Valheim.PluginInfo
{
/// <summary>
/// Class that specifies how a setting should be displayed inside the ConfigurationManager settings window.
///
/// Usage:
/// This class template has to be copied inside the plugin's project and referenced by its code directly.
/// make a new instance, assign any fields that you want to override, and pass it as a tag for your setting.
///
/// If a field is null (default), it will be ignored and won't change how the setting is displayed.
/// If a field is non-null (you assigned a value to it), it will override default behavior.
/// </summary>
///
/// <example>
/// Here's an example of overriding order of settings and marking one of the settings as advanced:
/// <code>
/// // Override IsAdvanced and Order
/// Config.AddSetting("X", "1", 1, new ConfigDescription("", null, new ConfigurationManagerAttributes { IsAdvanced = true, Order = 3 }));
/// // Override only Order, IsAdvanced stays as the default value assigned by ConfigManager
/// Config.AddSetting("X", "2", 2, new ConfigDescription("", null, new ConfigurationManagerAttributes { Order = 1 }));
/// Config.AddSetting("X", "3", 3, new ConfigDescription("", null, new ConfigurationManagerAttributes { Order = 2 }));
/// </code>
/// </example>
///
/// <remarks>
/// You can read more and see examples in the readme at https://github.com/BepInEx/BepInEx.ConfigurationManager
/// You can optionally remove fields that you won't use from this class, it's the same as leaving them null.
/// </remarks>
#pragma warning disable 0169, 0414, 0649
internal sealed class ConfigurationManagerAttributes
{
/// <summary>
/// Should the setting be shown as a percentage (only use with value range settings).
/// </summary>
public bool? ShowRangeAsPercent;

/// <summary>
/// Custom setting editor (OnGUI code that replaces the default editor provided by ConfigurationManager).
/// See below for a deeper explanation. Using a custom drawer will cause many of the other fields to do nothing.
/// </summary>
public System.Action<BepInEx.Configuration.ConfigEntryBase> CustomDrawer;

/// <summary>
/// Show this setting in the settings screen at all? If false, don't show.
/// </summary>
public bool? Browsable;

/// <summary>
/// Category the setting is under. Null to be directly under the plugin.
/// </summary>
public string Category;

/// <summary>
/// If set, a "Default" button will be shown next to the setting to allow resetting to default.
/// </summary>
public object DefaultValue;

/// <summary>
/// Force the "Reset" button to not be displayed, even if a valid DefaultValue is available.
/// </summary>
public bool? HideDefaultButton;

/// <summary>
/// Force the setting name to not be displayed. Should only be used with a <see cref="CustomDrawer"/> to get more space.
/// Can be used together with <see cref="HideDefaultButton"/> to gain even more space.
/// </summary>
public bool? HideSettingName;

/// <summary>
/// Optional description shown when hovering over the setting.
/// Not recommended, provide the description when creating the setting instead.
/// </summary>
public string Description;

/// <summary>
/// Name of the setting.
/// </summary>
public string DispName;

/// <summary>
/// Order of the setting on the settings list relative to other settings in a category.
/// 0 by default, higher number is higher on the list.
/// </summary>
public int? Order;

/// <summary>
/// Only show the value, don't allow editing it.
/// </summary>
public bool? ReadOnly;

/// <summary>
/// If true, don't show the setting by default. User has to turn on showing advanced settings or search for it.
/// </summary>
public bool? IsAdvanced;

/// <summary>
/// Custom converter from setting type to string for the built-in editor textboxes.
/// </summary>
public System.Func<object, string> ObjToStr;

/// <summary>
/// Custom converter from string to setting type for the built-in editor textboxes.
/// </summary>
public System.Func<string, object> StrToObj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<ItemGroup>
<Reference Include="0Harmony, Version=2.5.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\HarmonyX.2.5.5\lib\net45\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Pfhoenix.Valheim.ModProjectReferences.1.0.9\lib\net46\Assembly-CSharp.dll</HintPath>
Expand Down Expand Up @@ -391,6 +392,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ConfigurationManagerAttributes.cs" />
<Compile Include="Main.cs" />
<Compile Include="Patch.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
18 changes: 13 additions & 5 deletions src/Digitalroot.Valheim.PluginInfo/Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using Digitalroot.Valheim.Common;
using HarmonyLib;
Expand All @@ -11,17 +12,18 @@
namespace Digitalroot.Valheim.PluginInfo
{
[BepInPlugin(Guid, Name, Version)]
[BepInDependency(Jotunn.Main.ModGuid, BepInDependency.DependencyFlags.SoftDependency)]
[BepInDependency(JVLGuid, BepInDependency.DependencyFlags.SoftDependency)]
public class Main : BaseUnityPlugin, ITraceableLogging
{
private Harmony _harmony;
public const string Version = "1.3.0";
public const string Version = "1.3.1";
public const string Name = "Digitalroot Plug-in Info";
// ReSharper disable once MemberCanBePrivate.Global
public const string Guid = "digitalroot.mods.plugininfo";
public const string Namespace = "Digitalroot.Valheim." + nameof(PluginInfo);
[UsedImplicitly] public static ConfigEntry<int> NexusId;
public static Main Instance;
private const string JVLGuid = "com.jotunn.jotunn";

#region Implementation of ITraceableLogging

Expand All @@ -43,7 +45,7 @@ public Main()
EnableTrace = false;
#endif
Instance = this;
NexusId = Config.Bind("General", "NexusID", 1302, new ConfigDescription("Nexus mod ID for updates", null, new ConfigurationManagerAttributes { IsAdminOnly = false, Browsable = false, ReadOnly = true }));
NexusId = Config.Bind("General", "NexusID", 1302, new ConfigDescription("Nexus mod ID for updates", null, new ConfigurationManagerAttributes { Browsable = false, ReadOnly = true }));
Log.RegisterSource(Instance);
Log.Trace(Instance, $"{GetType().Namespace}.{GetType().Name}.{MethodBase.GetCurrentMethod().Name}()");
}
Expand Down Expand Up @@ -121,20 +123,26 @@ public void OnFejdStartupStart()
}

Log.Debug(Instance, $"***************************************");
HandleJVLData();

if (Common.Utils.DoesPluginExist(JVLGuid))
{
HandleJVLData();
}
}
catch (Exception e)
{
Log.Error(Instance, e);
}
}

public static bool DoesPluginExist(string pluginGuid) => Chainloader.PluginInfos.Any(keyValuePair => keyValuePair.Value.Metadata.GUID == pluginGuid);

private void HandleJVLData()
{
try
{
Log.Trace(Instance, $"{GetType().Namespace}.{GetType().Name}.{MethodBase.GetCurrentMethod().Name}()");
if (!Common.Utils.DoesPluginExist(Jotunn.Main.ModGuid)) return;

Log.Debug(Instance, "******* [Digitalroot JVL Info ] *******");
foreach (var modInfo in Jotunn.Utils.ModRegistry.GetMods())
{
Expand Down

0 comments on commit 81a0a0e

Please sign in to comment.