Files

186 lines
6.7 KiB
C#

// Made by MarC0 / ManlyMarco
// Copyright 2018 GNU General Public License v3.0
using BepInEx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using BepInEx.Logging;
using UnityEngine;
using Object = UnityEngine.Object;
namespace ConfigurationManager.Utilities
{
internal static class Utils
{
public static string ToProperCase(this string str)
{
if (string.IsNullOrEmpty(str)) return string.Empty;
if (str.Length < 2) return str;
// Start with the first character.
string result = str.Substring(0, 1).ToUpper();
// Add the remaining characters.
for (int i = 1; i < str.Length; i++)
{
if (char.IsUpper(str[i])) result += " ";
result += str[i];
}
return result;
}
/// <summary>
/// Return items with browsable attribute same as expectedBrowsable, and optionally items with no browsable attribute
/// </summary>
public static IEnumerable<T> FilterBrowsable<T>(this IEnumerable<T> props, bool expectedBrowsable,
bool includeNotSet = false) where T : MemberInfo
{
if (includeNotSet)
return props.Where(p => p.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>().All(x => x.Browsable == expectedBrowsable));
return props.Where(p => p.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>().Any(x => x.Browsable == expectedBrowsable));
}
public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic)
{
while (toCheck != null && toCheck != typeof(object))
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur)
return true;
toCheck = toCheck.BaseType;
}
return false;
}
// Additionally search for BaseUnityPlugin to find dynamically loaded plugins
public static BaseUnityPlugin[] FindPlugins() => BepInEx.Bootstrap.Chainloader.Plugins.Concat(Object.FindObjectsOfType(typeof(BaseUnityPlugin)).Cast<BaseUnityPlugin>()).Distinct().ToArray();
public static bool IsNumber(this object value) => value is sbyte
|| value is byte
|| value is short
|| value is ushort
|| value is int
|| value is uint
|| value is long
|| value is ulong
|| value is float
|| value is double
|| value is decimal;
public static string AppendZero(this string s)
{
return !s.Contains(".") ? s + ".0" : s;
}
public static string AppendZeroIfFloat(this string s, Type type)
{
return type == typeof(float) || type == typeof(double) || type == typeof(decimal) ? s.AppendZero() : s;
}
public static void FillTexture(this Texture2D tex, Color color)
{
for (var x = 0; x < tex.width; x++)
for (var y = 0; y < tex.height; y++)
tex.SetPixel(x, y, color);
tex.Apply(false);
}
public static void OpenLog()
{
bool TryOpen(string path)
{
if (path == null) return false;
try
{
Process.Start(path);
return true;
}
catch
{
return false;
}
}
var candidates = new List<string>();
// Redirected by preloader to game root
var rootDir = Path.Combine(Application.dataPath, "..");
candidates.Add(Path.Combine(rootDir, "output_log.txt"));
// Generated in most versions unless disabled
candidates.Add(Path.Combine(Application.dataPath, "output_log.txt"));
// Available since 2018.3
var prop = typeof(Application).GetProperty("consoleLogPath", BindingFlags.Static | BindingFlags.Public);
if (prop != null)
{
var path = prop.GetValue(null, null) as string;
candidates.Add(path);
}
if (Directory.Exists(Application.persistentDataPath))
{
var file = Directory.GetFiles(Application.persistentDataPath, "output_log.txt", SearchOption.AllDirectories).FirstOrDefault();
candidates.Add(file);
}
var latestLog = candidates.Where(File.Exists).OrderByDescending(File.GetLastWriteTimeUtc).FirstOrDefault();
if (TryOpen(latestLog)) return;
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));
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}");
}
}
}
}