using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using Il2CppSystem.Collections;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using UnityEngine;
using WildSkies.Database;
using WildSkies.Service;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("WorldSave5To6Fix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("Nines (Natives)")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WorldSave5To6")]
[assembly: AssemblyTitle("WorldSave5To6Fix")]
[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 WorldSave5To6Fix
{
[BepInPlugin("WorldSave5To6Fix", "WorldSave5To6", "1.0.0")]
public class Plugin : BasePlugin
{
internal static ManualLogSource Log;
internal static Harmony harmonyInstance;
public override void Load()
{
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
//IL_001b: Expected O, but got Unknown
Log = ((BasePlugin)this).Log;
ManualLogSource log = Log;
bool flag = default(bool);
BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(18, 1, ref flag);
if (flag)
{
((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin ");
((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("WorldSave5To6Fix");
((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!");
}
log.LogInfo(val);
harmonyInstance = Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
}
public override bool Unload()
{
Harmony obj = harmonyInstance;
if (obj != null)
{
obj.UnpatchSelf();
}
return true;
}
private static void Combinations(ref List<List<int>> results, ref List<int> input, int size)
{
if (size == 1)
{
results.Add(input.ToList());
return;
}
for (int i = 0; i < size; i++)
{
Combinations(ref results, ref input, size - 1);
if (size % 2 == 1)
{
List<int> list = input;
int index = size - 1;
List<int> obj = input;
int value = input[0];
int value2 = input[size - 1];
list[index] = value;
obj[0] = value2;
}
else
{
List<int> list = input;
int index = size - 1;
List<int> obj2 = input;
int value2 = i;
int value = input[i];
int value3 = input[size - 1];
list[index] = value;
obj2[value2] = value3;
}
}
}
private static Il2CppStructArray<byte> FixDataBuffer(Il2CppStructArray<byte> compressedData, string id)
{
int num = 1010;
if ((((Il2CppArrayBase<byte>)(object)compressedData)?.Count ?? 0) <= num)
{
return null;
}
try
{
int num2 = BitConverter.ToInt32(Il2CppArrayBase<byte>.op_Implicit((Il2CppArrayBase<byte>)(object)compressedData), ((Il2CppArrayBase<byte>)(object)compressedData).Length - 4);
if (num2 < 0)
{
throw new Exception();
}
if (num2 > 0 && ((Il2CppArrayBase<byte>)(object)CompressionHelper.Decompress(compressedData)).Length != num2)
{
throw new Exception();
}
}
catch
{
List<List<int>> results = new List<List<int>>();
List<int> input = new List<int>();
for (int i = 0; (double)i < Math.Ceiling((float)((Il2CppArrayBase<byte>)(object)compressedData).Length / (float)num); i++)
{
input.Add(i);
}
Combinations(ref results, ref input, input.Count);
foreach (List<int> item in results)
{
using MemoryStream memoryStream = new MemoryStream();
int num3 = ((Il2CppArrayBase<byte>)(object)compressedData).Length - (item.Count - 1) * num;
foreach (int item2 in item)
{
memoryStream.Write(Il2CppArrayBase<byte>.op_Implicit((Il2CppArrayBase<byte>)(object)compressedData), item2 * num + ((item2 > item.Last()) ? (num3 - num) : 0), (item2 == item.Last()) ? num3 : num);
}
byte[] array = memoryStream.ToArray();
try
{
int num4 = BitConverter.ToInt32(array, array.Length - 4);
if (num4 < 0)
{
throw new Exception();
}
if (num4 > 0 && ((Il2CppArrayBase<byte>)(object)CompressionHelper.Decompress(Il2CppStructArray<byte>.op_Implicit(array))).Length != num4)
{
throw new Exception();
}
}
catch
{
array = null;
}
finally
{
memoryStream.Close();
}
if (array != null)
{
Debug.LogWarning(Object.op_Implicit("LDS Container " + id + " has a corrupt databuffer. Fixed."));
return Il2CppStructArray<byte>.op_Implicit(array);
}
}
Debug.LogError(Object.op_Implicit("LDS Container " + id + " has a corrupt databuffer. Failed to repair."));
}
return null;
}
[HarmonyPatch(typeof(WorldDBService), "InitDatabase")]
[HarmonyPostfix]
public static void WorldDBService_InitDatabase(WorldDBService __instance)
{
//IL_0032: Unknown result type (might be due to invalid IL or missing references)
if (__instance._worldData.Version != 6)
{
return;
}
foreach (Object item in CollectionExtensions.WrapToManaged(((Il2CppObjectBase)__instance._ldsContainers.FindAll()).Cast<IEnumerable>()))
{
LDSEntityEntry val = ((Il2CppObjectBase)item).Cast<LDSEntityEntry>();
Dictionary<string, string> val2 = JsonConvert.DeserializeObject<Dictionary<string, string>>(val.InstanceData);
V6LDSInstanceData val3 = JsonConvert.DeserializeObject<V6LDSInstanceData>(val2["_5a574b2b6764eb9428c646bf7c778da0_7229481547701622468"]);
val3._dataBuffer = FixDataBuffer(val3._dataBuffer, val._id);
if (val3._dataBuffer != null)
{
val2["_5a574b2b6764eb9428c646bf7c778da0_7229481547701622468"] = JsonConvert.SerializeObject((Object)(object)val3);
val.InstanceData = JsonConvert.SerializeObject((Object)(object)val2);
__instance.UpdateLdsContainer(val);
}
}
}
}
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "WorldSave5To6Fix";
public const string PLUGIN_NAME = "WorldSave5To6";
public const string PLUGIN_VERSION = "1.0.0";
}
}