diff --git a/BepInEx.sln b/BepInEx.sln index 855a1c2..603c9dc 100644 --- a/BepInEx.sln +++ b/BepInEx.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27130.2027 +VisualStudioVersion = 15.0.27130.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BepInEx", "BepInEx\BepInEx.csproj", "{4FFBA620-F5ED-47F9-B90C-DAD1316FD9B9}" EndProject @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceRedirector", "Plugi EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeveloperConsole", "Plugins\DeveloperConsole\DeveloperConsole.csproj", "{434F00D0-BD2A-4C81-A2FE-8DBFBBD9D9AB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensibleSaveFormat", "Plugins\ExtensibleSaveFormat\ExtensibleSaveFormat.csproj", "{8EBE81B9-9ED9-466D-864A-4C644BFD8827}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution BepInEx.Common\BepInEx.Common.projitems*{4ffba620-f5ed-47f9-b90c-dad1316fd9b9}*SharedItemsImports = 4 @@ -72,6 +74,10 @@ Global {434F00D0-BD2A-4C81-A2FE-8DBFBBD9D9AB}.Debug|Any CPU.Build.0 = Debug|Any CPU {434F00D0-BD2A-4C81-A2FE-8DBFBBD9D9AB}.Release|Any CPU.ActiveCfg = Release|Any CPU {434F00D0-BD2A-4C81-A2FE-8DBFBBD9D9AB}.Release|Any CPU.Build.0 = Release|Any CPU + {8EBE81B9-9ED9-466D-864A-4C644BFD8827}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8EBE81B9-9ED9-466D-864A-4C644BFD8827}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8EBE81B9-9ED9-466D-864A-4C644BFD8827}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8EBE81B9-9ED9-466D-864A-4C644BFD8827}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -84,6 +90,7 @@ Global {6A9D08C0-4482-4D1A-9368-9F41B7126F9E} = {FF8CE2C8-C185-4EA9-8D63-E2542F0C2FCF} {3EA76CA3-45B6-4A1A-B04F-7A1A8C978AF6} = {FF8CE2C8-C185-4EA9-8D63-E2542F0C2FCF} {434F00D0-BD2A-4C81-A2FE-8DBFBBD9D9AB} = {FF8CE2C8-C185-4EA9-8D63-E2542F0C2FCF} + {8EBE81B9-9ED9-466D-864A-4C644BFD8827} = {FF8CE2C8-C185-4EA9-8D63-E2542F0C2FCF} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {55AC11EF-F568-4C79-A356-7ED9510145B1} diff --git a/Plugins/ExtensibleSaveFormat/ExtensibleSaveFormat.cs b/Plugins/ExtensibleSaveFormat/ExtensibleSaveFormat.cs new file mode 100644 index 0000000..d9a27cc --- /dev/null +++ b/Plugins/ExtensibleSaveFormat/ExtensibleSaveFormat.cs @@ -0,0 +1,36 @@ +using BepInEx; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ExtensibleSaveFormat +{ + public class ExtensibleSaveFormat : BaseUnityPlugin + { + public override string ID => "extendedsave"; + + public override string Name => "Extensible Save Format"; + + public override Version Version => new Version("1.0"); + + + internal static Dictionary> internalDictionary = new Dictionary>(); + + public ExtensibleSaveFormat() + { + Hooks.InstallHooks(); + } + + + public static bool TryGetExtendedFormat(ChaFile file, out Dictionary extendedFormatData) + { + return internalDictionary.TryGetValue(file, out extendedFormatData); + } + + public static void SetExtendedFormat(ChaFile file, Dictionary extendedFormatData) + { + internalDictionary[file] = extendedFormatData; + } + } +} diff --git a/Plugins/ExtensibleSaveFormat/ExtensibleSaveFormat.csproj b/Plugins/ExtensibleSaveFormat/ExtensibleSaveFormat.csproj new file mode 100644 index 0000000..8e1d2ca --- /dev/null +++ b/Plugins/ExtensibleSaveFormat/ExtensibleSaveFormat.csproj @@ -0,0 +1,69 @@ + + + + + Debug + AnyCPU + {8EBE81B9-9ED9-466D-864A-4C644BFD8827} + Library + Properties + ExtensibleSaveFormat + ExtensibleSaveFormat + v3.5 + 512 + + + true + full + false + K:\BepInEx\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + K:\BepInEx\ + TRACE + prompt + 4 + + + + ..\..\lib\0Harmony.dll + False + + + ..\..\lib\Assembly-CSharp.dll + False + + + ..\..\lib\Assembly-CSharp-firstpass.dll + False + + + + + + + + + ..\..\lib\UnityEngine.dll + False + + + + + + + + + + {4ffba620-f5ed-47f9-b90c-dad1316fd9b9} + BepInEx + False + + + + \ No newline at end of file diff --git a/Plugins/ExtensibleSaveFormat/Hooks.cs b/Plugins/ExtensibleSaveFormat/Hooks.cs new file mode 100644 index 0000000..a96a85f --- /dev/null +++ b/Plugins/ExtensibleSaveFormat/Hooks.cs @@ -0,0 +1,71 @@ +using Harmony; +using MessagePack; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; + +namespace ExtensibleSaveFormat +{ + public static class Hooks + { + public static void InstallHooks() + { + var harmony = HarmonyInstance.Create("com.bepis.bepinex.extensiblesaveformat"); + + + MethodInfo original = AccessTools.Method(typeof(ChaFile), "SaveFile", new[] { typeof(BinaryWriter), typeof(bool) }); + + HarmonyMethod postfix = new HarmonyMethod(typeof(Hooks).GetMethod("SaveFileHook")); + + harmony.Patch(original, null, postfix); + + + original = AccessTools.Method(typeof(ChaFile), "LoadFile", new[] { typeof(BinaryReader), typeof(bool), typeof(bool) }); + + postfix = new HarmonyMethod(typeof(Hooks).GetMethod("LoadFileHook")); + + harmony.Patch(original, null, postfix); + } + + public static void SaveFileHook(ChaFile __instance, bool __result, BinaryWriter bw, bool savePng) + { + if (!__result) + return; + + if (!ExtensibleSaveFormat.TryGetExtendedFormat(__instance, out Dictionary extendedData)) + return; + + byte[] bytes = MessagePackSerializer.Serialize(extendedData); + + bw.Write((int)bytes.Length); + bw.Write(bytes); + } + + public static void LoadFileHook(ChaFile __instance, bool __result, BinaryReader br, bool noLoadPNG, bool noLoadStatus) + { + if (!__result) + return; + + try + { + int length = br.ReadInt32(); + + if (length > 0) + { + byte[] bytes = br.ReadBytes(length); + + ExtensibleSaveFormat.internalDictionary[__instance] = MessagePackSerializer.Deserialize>(bytes); + + return; + } + } + catch (EndOfStreamException) { } + + //initialize a new dictionary since it doesn't exist + ExtensibleSaveFormat.internalDictionary[__instance] = new Dictionary(); + } + } +} diff --git a/Plugins/ExtensibleSaveFormat/Properties/AssemblyInfo.cs b/Plugins/ExtensibleSaveFormat/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2b0ad7d --- /dev/null +++ b/Plugins/ExtensibleSaveFormat/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ExtensibleSaveFormat")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ExtensibleSaveFormat")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("8ebe81b9-9ed9-466d-864a-4c644bfd8827")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]