using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using LCBetterSaves;
using Microsoft.CodeAnalysis;
using UnityEngine;
using com.github.zehsteam.LCBetterSavesFix.Patches;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.github.zehsteam.LCBetterSavesFix")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LCBetterSavesFix")]
[assembly: AssemblyTitle("com.github.zehsteam.LCBetterSavesFix")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace com.github.zehsteam.LCBetterSavesFix
{
[BepInPlugin("com.github.zehsteam.LCBetterSavesFix", "LCBetterSavesFix", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
private readonly Harmony harmony = new Harmony("com.github.zehsteam.LCBetterSavesFix");
internal static Plugin Instance;
internal static ManualLogSource logger;
private void Awake()
{
if ((Object)(object)Instance == (Object)null)
{
Instance = this;
}
logger = Logger.CreateLogSource("com.github.zehsteam.LCBetterSavesFix");
logger.LogInfo((object)"LCBetterSavesFix has awoken!");
harmony.PatchAll(typeof(PluginPatch));
harmony.PatchAll(typeof(NewFileUISlot_BetterSavesPatch));
harmony.PatchAll(typeof(SaveFileUISlot_BetterSavesPatch));
harmony.PatchAll(typeof(DeleteFileButton_BetterSavesPatch));
}
}
internal class Utils
{
public static int GetNewSaveFileNum()
{
int num = 1;
string[] files = ES3.GetFiles();
foreach (string text in files)
{
if (ES3.FileExists(text) && Regex.IsMatch(text, "^LCSaveFile\\d+$"))
{
num++;
}
}
return num;
}
}
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "com.github.zehsteam.LCBetterSavesFix";
public const string PLUGIN_NAME = "LCBetterSavesFix";
public const string PLUGIN_VERSION = "1.0.0";
}
}
namespace com.github.zehsteam.LCBetterSavesFix.Patches
{
[HarmonyPatch(typeof(DeleteFileButton_BetterSaves))]
internal class DeleteFileButton_BetterSavesPatch
{
[HarmonyPatch("DeleteFile")]
[HarmonyPrefix]
private static void DeleteFilePatch(ref DeleteFileButton_BetterSaves __instance)
{
string text = $"LCSaveFile{__instance.fileToDelete}.bac";
if (ES3.FileExists(text))
{
ES3.DeleteFile(text);
Plugin.logger.LogInfo((object)("Deleted LethalModDataLib " + text + " save file."));
}
}
}
[HarmonyPatch(typeof(NewFileUISlot_BetterSaves))]
internal class NewFileUISlot_BetterSavesPatch
{
[HarmonyPatch("SetFileToThis")]
[HarmonyPostfix]
private static void SetFileToThisPatch()
{
int newSaveFileNum = Utils.GetNewSaveFileNum();
string text = $"LCSaveFile{newSaveFileNum}";
GameNetworkManager.Instance.currentSaveFileName = text;
GameNetworkManager.Instance.saveFileNum = newSaveFileNum;
Plugin.logger.LogInfo((object)("Set new save file name to " + text));
}
}
[HarmonyPatch(typeof(Plugin))]
internal class PluginPatch
{
private const string _placeHolderFileName = "PlaceHolder";
private const string _tempFileFormat = "Temp{0}";
[HarmonyPatch("NormalizeFileNames")]
[HarmonyPrefix]
private static void NormalizeFileNamesPatch()
{
Plugin.logger.LogInfo((object)"Normalizing LethalModDataLib save files.");
List<string> files = GetFiles("LCSaveFile{0}.moddata");
RenameFilesToTempFiles(files);
RenameTempFilesToNormalizedFiles(files, "LCSaveFile{0}.moddata");
Plugin.logger.LogInfo((object)"Normalizing LethalModDataLib save backup files.");
List<string> files2 = GetFiles("LCSaveFile{0}.bac");
RenameFilesToTempFiles(files2);
RenameTempFilesToNormalizedFiles(files2, "LCSaveFile{0}.bac");
}
private static List<string> GetFiles(string fileFormat)
{
List<string> list = new List<string>();
string[] files = ES3.GetFiles();
foreach (string text in files)
{
if (ES3.FileExists(text) && Regex.IsMatch(text, "^LCSaveFile\\d+$"))
{
Plugin.logger.LogInfo((object)("Found Lethal Company save file: " + text));
int num = int.Parse(text.Replace("LCSaveFile", ""));
string text2 = string.Format(fileFormat, num);
if (ES3.FileExists(text2))
{
Plugin.logger.LogInfo((object)("Found save file: " + text2));
list.Add(text2);
}
else
{
list.Add("PlaceHolder");
}
}
}
return list;
}
private static void RenameFilesToTempFiles(List<string> fileNames)
{
int num = 1;
foreach (string fileName in fileNames)
{
if (fileName == "PlaceHolder")
{
num++;
continue;
}
string text = $"Temp{fileName}";
ES3.RenameFile(fileName, text);
Plugin.logger.LogInfo((object)("Renamed " + fileName + " to " + text));
num++;
}
}
private static void RenameTempFilesToNormalizedFiles(List<string> fileNames, string fileFormat)
{
int num = 1;
foreach (string fileName in fileNames)
{
string text = $"Temp{fileName}";
string text2 = string.Format(fileFormat, num);
if (fileName == "PlaceHolder")
{
num++;
continue;
}
if (ES3.FileExists(text))
{
ES3.RenameFile(text, text2);
Plugin.logger.LogInfo((object)("Renamed " + text + " to " + text2));
}
else
{
Plugin.logger.LogInfo((object)("Temporary file " + text + " not found. It might have been moved or deleted."));
}
num++;
}
}
}
[HarmonyPatch(typeof(SaveFileUISlot_BetterSaves))]
internal class SaveFileUISlot_BetterSavesPatch
{
[HarmonyPatch("SetFileToThis")]
[HarmonyPostfix]
private static void SetFileToThisPatch()
{
Plugin.logger.LogInfo((object)$"currentSaveFileName: {GameNetworkManager.Instance.currentSaveFileName}, saveFileNum: {GameNetworkManager.Instance.saveFileNum}");
}
}
}
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
internal sealed class IgnoresAccessChecksToAttribute : Attribute
{
public IgnoresAccessChecksToAttribute(string assemblyName)
{
}
}
}