using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Preloader.Core.Patching;
using HarmonyLib;
using Microsoft.CodeAnalysis;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("GlobalConfigRedirect")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("GlobalConfigRedirect")]
[assembly: AssemblyTitle("GlobalConfigRedirect")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
}
namespace GlobalConfigRedirect
{
[PatcherPluginInfo("io.takina.gtfo.GlobalConfigRedirect", "GlobalConfigRedirect", "0.1.1")]
public class Patcher : BasePatcher
{
public const string NAME = "GlobalConfigRedirect";
public const string GUID = "io.takina.gtfo.GlobalConfigRedirect";
public const string VERSION = "0.1.1";
public const string FolderName = "GTFO.GlobalConfigRedirect";
public static readonly string DefaultGlobalFolderName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GTFO.GlobalConfigRedirect", "config");
public override void Initialize()
{
//IL_0112: Unknown result type (might be due to invalid IL or missing references)
//IL_0164: Unknown result type (might be due to invalid IL or missing references)
//IL_0172: Expected O, but got Unknown
//IL_017e: Unknown result type (might be due to invalid IL or missing references)
//IL_0185: Expected O, but got Unknown
bool value = ((BasePatcher)this).Config.Bind<bool>("General", "Enabled", true, "Redirect config files to the global folder.").Value;
string value2 = ((BasePatcher)this).Config.Bind<string>("General", "Global Folder", "", "Global config folder. Environment variables are expanded. Empty = " + DefaultGlobalFolderName).Value;
string value3 = ((BasePatcher)this).Config.Bind<string>("General", "Include", "**", "Comma-separated globs matched against the path relative to this profile's config folder. * and ? stay within one folder, ** spans folders. e.g. *.cfg (top level only), Hikaria/**, **").Value;
string value4 = ((BasePatcher)this).Config.Bind<string>("General", "Exclude", "", "Comma-separated globs; files matching these stay in the profile. Takes priority over Include.").Value;
bool value5 = ((BasePatcher)this).Config.Bind<bool>("General", "Copy Existing", true, "When a redirected file doesn't exist globally yet, seed it with this profile's copy.").Value;
((BasePatcher)this).Config.Save();
if (!value)
{
((BasePatcher)this).Log.LogInfo((object)"Disabled");
return;
}
value2 = (string.IsNullOrWhiteSpace(value2) ? DefaultGlobalFolderName : Environment.ExpandEnvironmentVariables(value2.Trim()));
Redirect.Setup(((BasePatcher)this).Log, value2, value3, value4, value5, ((BasePatcher)this).Config.ConfigFilePath);
new Harmony("io.takina.gtfo.GlobalConfigRedirect").Patch((MethodBase)AccessTools.Constructor(typeof(ConfigFile), new Type[3]
{
typeof(string),
typeof(bool),
typeof(BepInPlugin)
}, false), new HarmonyMethod(typeof(Redirect), "ConfigFileCtorPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
ManualLogSource log = ((BasePatcher)this).Log;
bool flag = default(bool);
BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(39, 4, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Redirecting ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(Redirect.ProfileFolder);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" -> ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(Redirect.GlobalFolder);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" (include: ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(value3);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("; exclude: ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(value4);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
}
log.LogInfo(val);
}
}
internal static class Redirect
{
private static ManualLogSource _log = null;
private static Regex[] _include = Array.Empty<Regex>();
private static Regex[] _exclude = Array.Empty<Regex>();
private static bool _copyExisting;
private static string _ownConfigPath = "";
public static string ProfileFolder { get; private set; } = "";
public static string GlobalFolder { get; private set; } = "";
public static void Setup(ManualLogSource log, string globalFolder, string include, string exclude, bool copyExisting, string ownConfigPath)
{
_log = log;
ProfileFolder = WithSeparator(Path.GetFullPath(Paths.ConfigPath));
GlobalFolder = Path.GetFullPath(globalFolder);
_include = ParseGlobs(include);
_exclude = ParseGlobs(exclude);
_copyExisting = copyExisting;
_ownConfigPath = Path.GetFullPath(ownConfigPath);
}
public static void ConfigFileCtorPrefix(ref string configPath)
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Expected O, but got Unknown
if (configPath == null)
{
return;
}
try
{
if (TryMap(configPath, out string target))
{
configPath = target;
}
}
catch (Exception ex)
{
ManualLogSource log = _log;
bool flag = default(bool);
BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(42, 2, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Failed to redirect ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(configPath);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", leaving it in place: ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
}
log.LogError(val);
}
}
private static bool TryMap(string configPath, out string target)
{
//IL_0127: Unknown result type (might be due to invalid IL or missing references)
//IL_012e: Expected O, but got Unknown
//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
//IL_00f3: Expected O, but got Unknown
target = "";
string fullPath = Path.GetFullPath(configPath);
if (!fullPath.StartsWith(ProfileFolder, StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (string.Equals(fullPath, _ownConfigPath, StringComparison.OrdinalIgnoreCase) || string.Equals(fullPath, Path.GetFullPath(Paths.BepInExConfigPath), StringComparison.OrdinalIgnoreCase))
{
return false;
}
string rel = fullPath.Substring(ProfileFolder.Length).Replace('\\', '/');
if (!_include.Any((Regex r) => r.IsMatch(rel)) || _exclude.Any((Regex r) => r.IsMatch(rel)))
{
return false;
}
target = Path.Combine(GlobalFolder, rel.Replace('/', Path.DirectorySeparatorChar));
bool flag = default(bool);
if (_copyExisting && !File.Exists(target) && File.Exists(fullPath))
{
Directory.CreateDirectory(Path.GetDirectoryName(target));
File.Copy(fullPath, target);
ManualLogSource log = _log;
BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(27, 1, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Seeded global ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(rel);
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" from profile");
}
log.LogInfo(val);
}
ManualLogSource log2 = _log;
BepInExDebugLogInterpolatedStringHandler val2 = new BepInExDebugLogInterpolatedStringHandler(4, 2, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(rel);
((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" -> ");
((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(target);
}
log2.LogDebug(val2);
return true;
}
private static Regex[] ParseGlobs(string globs)
{
return (from g in globs.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
select new Regex(GlobToRegex(g), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)).ToArray();
}
internal static string GlobToRegex(string glob)
{
return "^" + Regex.Escape(glob.Replace('\\', '/')).Replace("\\*\\*/", "(?:.*/)?").Replace("\\*\\*", ".*")
.Replace("\\*", "[^/]*")
.Replace("\\?", "[^/]") + "$";
}
private static string WithSeparator(string dir)
{
if (!dir.EndsWith(Path.DirectorySeparatorChar))
{
return dir + Path.DirectorySeparatorChar;
}
return dir;
}
}
}