Skip to content

Commit

Permalink
Added URL button to open plugin websites if available
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Sep 1, 2021
1 parent e7117c9 commit 183bdbf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
43 changes: 34 additions & 9 deletions ConfigurationManager/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using BepInEx.Logging;
using UnityEngine;
using BepInEx.Configuration;
using ConfigurationManager.Utilities;

namespace ConfigurationManager
{
Expand Down Expand Up @@ -214,7 +215,15 @@ string GetCategory(SettingEntryBase eb)
.ThenBy(x => x.Key)
.Select(x => new PluginSettingsData.PluginSettingsGroupData { Name = x.Key, Settings = x.OrderByDescending(set => set.Order).ThenBy(set => set.DispName).ToList() });
return new PluginSettingsData { Info = pluginSettings.Key, Categories = categories.ToList(), Collapsed = nonDefaultCollpasingStateByPluginName.Contains(pluginSettings.Key.Name) ? !settingsAreCollapsed : settingsAreCollapsed };
var website = Utils.GetWebsite(pluginSettings.First().PluginInstance);
return new PluginSettingsData
{
Info = pluginSettings.Key,
Categories = categories.ToList(),
Collapsed = nonDefaultCollpasingStateByPluginName.Contains(pluginSettings.Key.Name) ? !settingsAreCollapsed : settingsAreCollapsed,
Website = website
};
})
.OrderBy(x => x.Info.Name)
.ToList();
Expand Down Expand Up @@ -491,8 +500,24 @@ private void DrawSinglePlugin(PluginSettingsData plugin)

var isSearching = !string.IsNullOrEmpty(SearchString);

if (SettingFieldDrawer.DrawPluginHeader(categoryHeader, plugin.Collapsed && !isSearching) && !isSearching)
plugin.Collapsed = !plugin.Collapsed;
{
var hasWebsite = plugin.Website != null;
if (hasWebsite)
{
GUILayout.BeginHorizontal();
GUILayout.Space(27); // Same as the URL button to keep the plugin name centered
}

if (SettingFieldDrawer.DrawPluginHeader(categoryHeader, plugin.Collapsed && !isSearching) && !isSearching)
plugin.Collapsed = !plugin.Collapsed;

if (hasWebsite)
{
if (GUILayout.Button(new GUIContent("URL", plugin.Website), GUI.skin.label, GUILayout.ExpandWidth(false)))
Utils.OpenWebsite(plugin.Website);
GUILayout.EndHorizontal();
}
}

if (isSearching || !plugin.Collapsed)
{
Expand Down Expand Up @@ -596,7 +621,7 @@ private void Start()
if (_curLockState == null && _curVisible == null)
{
_obsoleteCursor = true;

_curLockState = typeof(Screen).GetProperty("lockCursor", BindingFlags.Static | BindingFlags.Public);
_curVisible = typeof(Screen).GetProperty("showCursor", BindingFlags.Static | BindingFlags.Public);
}
Expand Down Expand Up @@ -628,11 +653,11 @@ private void SetUnlockCursor(int lockState, bool cursorVisible)
// Do through reflection for unity 4 compat
//Cursor.lockState = CursorLockMode.None;
//Cursor.visible = true;
if(_obsoleteCursor)
if (_obsoleteCursor)
_curLockState.SetValue(null, Convert.ToBoolean(lockState), null);
else
_curLockState.SetValue(null, lockState, null);

_curVisible.SetValue(null, cursorVisible, null);
}
}
Expand All @@ -641,8 +666,10 @@ private sealed class PluginSettingsData
{
public BepInPlugin Info;
public List<PluginSettingsGroupData> Categories;
private bool _collapsed;
public int Height;
public string Website;

private bool _collapsed;
public bool Collapsed
{
get => _collapsed;
Expand All @@ -658,8 +685,6 @@ public sealed class PluginSettingsGroupData
public string Name;
public List<SettingEntryBase> Settings;
}

public int Height { get; set; }
}
}
}
41 changes: 39 additions & 2 deletions ConfigurationManager/Utilities/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using BepInEx.Logging;
using UnityEngine;
using Object = UnityEngine.Object;

Expand Down Expand Up @@ -137,12 +138,48 @@ bool TryOpen(string path)
candidates.Clear();
// Fall back to more aggresive brute search
// BepInEx 5.x log file, can be "LogOutput.log.1" or higher if multiple game instances run
candidates.AddRange(Directory.GetFiles(rootDir,"LogOutput.log*", SearchOption.AllDirectories));
candidates.AddRange(Directory.GetFiles(rootDir,"output_log.txt", SearchOption.AllDirectories));
candidates.AddRange(Directory.GetFiles(rootDir, "LogOutput.log*", SearchOption.AllDirectories));
candidates.AddRange(Directory.GetFiles(rootDir, "output_log.txt", SearchOption.AllDirectories));
latestLog = candidates.Where(File.Exists).OrderByDescending(File.GetLastWriteTimeUtc).FirstOrDefault();
if (TryOpen(latestLog)) return;

throw new FileNotFoundException("No log files were found");
}

public static string GetWebsite(BaseUnityPlugin bepInPlugin)
{
try
{
var fileName = bepInPlugin.GetType().Assembly.Location;
if (!File.Exists(fileName)) return null;
var fi = FileVersionInfo.GetVersionInfo(fileName);
return new[]
{
fi.CompanyName,
fi.FileDescription,
fi.Comments,
fi.LegalCopyright,
fi.LegalTrademarks
}.FirstOrDefault(x => Uri.IsWellFormedUriString(x, UriKind.Absolute));
}
catch (Exception e)
{
ConfigurationManager.Logger.LogWarning("Failed to get Uri info - " + e.Message);
return null;
}
}

public static void OpenWebsite(string url)
{
try
{
if (string.IsNullOrEmpty(url)) throw new Exception("Empty URL");
Process.Start(url);
}
catch (Exception ex)
{
ConfigurationManager.Logger.Log(LogLevel.Message | LogLevel.Warning, $"Failed to open URL {url}\nCause: {ex.Message}");
}
}
}
}

0 comments on commit 183bdbf

Please sign in to comment.
  翻译: