-
-
Notifications
You must be signed in to change notification settings - Fork 135
Add SpawnLocker (code provided by BitMagnet) #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
987b68f
Add SpawnLocker (code provided by BitMagnet)
ManlyMarco f99adff
Apply repo code style; Refactoring
ManlyMarco cf8eea4
Relax package requirements
ManlyMarco ff09d68
Add to readme
ManlyMarco b2bb3f2
Merge branch 'master' into SpawnLocker
ManlyMarco a43c296
Fix namespace
ManlyMarco 731ece3
Update packages
ManlyMarco cee678c
Fix wrong guid
ManlyMarco 39c88ef
Minor fixes
ManlyMarco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using KK_Plugins; | ||
| using System.Reflection; | ||
| using static SpawnLocker.SpawnLockMain; | ||
|
|
||
| [assembly: AssemblyTitle(PluginName)] | ||
| [assembly: AssemblyProduct(PluginName)] | ||
| [assembly: AssemblyDescription(GUID + " for " + Constants.GameName)] | ||
| [assembly: AssemblyVersion(Version)] | ||
| [assembly: AssemblyFileVersion(Version)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <PropertyGroup Label="Globals"> | ||
| <ProjectGuid>{A545658E-D416-40B6-90C6-BFD776147D3E}</ProjectGuid> | ||
| <MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion> | ||
| </PropertyGroup> | ||
| <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" /> | ||
| <PropertyGroup /> | ||
| <Import Project="SpawnLocker.Core.projitems" Label="Shared" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" /> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| using HarmonyLib; | ||
| using KKAPI.Utilities; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Reflection.Emit; | ||
|
|
||
| namespace SpawnLocker | ||
| { | ||
| internal static class Hooks | ||
| { | ||
| public static void Apply() | ||
| { | ||
| var harmony = Harmony.CreateAndPatchAll(typeof(Hooks), SpawnLockMain.GUID); | ||
|
|
||
| var transpiler = new HarmonyMethod(typeof(Hooks), nameof(Hooks.NPCLoadAllTranspile)); | ||
|
|
||
| foreach (var targetMethod in typeof(ActionScene).GetMethods(AccessTools.all).Where(x => x.Name == nameof(ActionScene.NPCLoadAll))) | ||
| { | ||
| SpawnLockMain.Logger.LogDebug("Patching: " + targetMethod.FullDescription()); | ||
| harmony.PatchMoveNext(targetMethod, transpiler: transpiler); | ||
| } | ||
| } | ||
|
|
||
| private static IEnumerable<CodeInstruction> NPCLoadAllTranspile(IEnumerable<CodeInstruction> instructions) | ||
| { | ||
| var targetMethod = typeof(Enumerable).GetMethod(nameof(Enumerable.Take)).MakeGenericMethod(typeof(SaveData.Heroine)); | ||
| var newTakeMethod = AccessTools.Method(typeof(SpawnLockMain), nameof(SpawnLockMain._SpawnLockTake)); | ||
|
|
||
| foreach (var instruction in instructions) | ||
| { | ||
| if (instruction.opcode != OpCodes.Call || instruction.operand as MethodInfo != targetMethod) | ||
| { | ||
| yield return instruction; | ||
| } | ||
| else | ||
| { | ||
| yield return new CodeInstruction(OpCodes.Call, newTakeMethod); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPostfix] | ||
| [HarmonyPatch(typeof(ActionGame.PreviewClassData), nameof(ActionGame.PreviewClassData.Set))] | ||
| private static void PreviewClassDataSetPostfix(ActionGame.PreviewClassData __instance, SaveData.CharaData charaData) | ||
| { | ||
| _UpdateStatus(__instance); | ||
| } | ||
|
|
||
| [HarmonyPostfix] | ||
| [HarmonyPatch(typeof(ActionGame.PreviewClassData), nameof(ActionGame.PreviewClassData.Clear))] | ||
| private static void PreviewClassDataClearPostfix(ActionGame.PreviewClassData __instance) | ||
| { | ||
| _UpdateStatus(__instance); | ||
| } | ||
|
|
||
| static void _UpdateStatus(ActionGame.PreviewClassData __instance) | ||
| { | ||
| var observer = __instance.button.GetComponent<ClickObserver>(); | ||
|
|
||
| if (observer != null) | ||
| { | ||
| observer.UpdateStatus(); | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPostfix] | ||
| #if KK | ||
| [HarmonyPatch(typeof(ActionGame.PreviewClassData), MethodType.Constructor, new System.Type[] { typeof(UnityEngine.GameObject) })] | ||
| private static void PreviewClassDataConstructorPostfix(ActionGame.PreviewClassData __instance) | ||
| #elif KKS | ||
| [HarmonyPatch(typeof(ActionGame.PreviewClassData), nameof(ActionGame.PreviewClassData.Awake))] | ||
| private static void PreviewClassAwakePostfix(ActionGame.PreviewClassData __instance) | ||
| #endif | ||
| { | ||
| var observer = __instance.button.gameObject.GetOrAddComponent<ClickObserver>(); | ||
| observer.previewData = __instance; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using ExtensibleSaveFormat; | ||
| using MessagePack; | ||
| using MessagePack.Formatters; | ||
|
|
||
| namespace SpawnLocker | ||
| { | ||
| [MessagePackObject] | ||
| public class SpawnLockData | ||
| { | ||
| [Key(0)] | ||
| public bool isLocked; | ||
|
|
||
| [IgnoreMember] | ||
| private static string PluginKey = "SpawnLock"; | ||
ManlyMarco marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [IgnoreMember] | ||
| private static IFormatterResolver resolver = UnityEngineTypeFormatterResolver.Instance; | ||
ManlyMarco marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static SpawnLockData Load(PluginData data) | ||
| { | ||
| if (data?.data == null) | ||
| return null; | ||
|
|
||
| if (!data.data.TryGetValue(PluginKey, out object bytesObj)) | ||
| return null; | ||
|
|
||
| if (bytesObj is byte[] bytes) | ||
| return LZ4MessagePackSerializer.Deserialize<SpawnLockData>(bytes, resolver); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public PluginData Save() | ||
| { | ||
| PluginData pluginData = new PluginData(); | ||
|
|
||
| var bytes = LZ4MessagePackSerializer.Serialize(this, resolver); | ||
| pluginData.data.Add(PluginKey, bytes); | ||
|
|
||
| return pluginData; | ||
| } | ||
| } | ||
|
|
||
| public class UnityEngineTypeFormatterResolver : IFormatterResolver | ||
| { | ||
| public IMessagePackFormatter<T> GetFormatter<T>() | ||
| { | ||
| return MessagePack.Unity.UnityResolver.Instance.GetFormatter<T>() ?? MessagePack.Resolvers.StandardResolver.Instance.GetFormatter<T>(); | ||
| } | ||
|
|
||
| public static UnityEngineTypeFormatterResolver Instance = new UnityEngineTypeFormatterResolver(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <PropertyGroup> | ||
| <MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' < '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects> | ||
| <HasSharedItems>true</HasSharedItems> | ||
| <SharedGUID>a27f5e6b-5e24-4609-a634-1a704ebbf1d9</SharedGUID> | ||
ManlyMarco marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </PropertyGroup> | ||
| <PropertyGroup Label="Configuration"> | ||
| <Import_RootNamespace>ShaderSwapper.Core</Import_RootNamespace> | ||
ManlyMarco marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="$(MSBuildThisFileDirectory)AssemblyInfo.cs" /> | ||
| <Compile Include="$(MSBuildThisFileDirectory)Hooks.cs" /> | ||
| <Compile Include="$(MSBuildThisFileDirectory)SpawnLockData.cs" /> | ||
| <Compile Include="$(MSBuildThisFileDirectory)SpawnLockerMain.cs" /> | ||
| </ItemGroup> | ||
| </Project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // v1.0.0 code was provided by BitMagnet under GPL-3.0 license. | ||
|
|
||
| using BepInEx; | ||
| using BepInEx.Logging; | ||
| using ExtensibleSaveFormat; | ||
| using Illusion.Extensions; | ||
| using KKAPI; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace SpawnLocker | ||
| { | ||
| [BepInPlugin(GUID, GUID, Version)] | ||
| [BepInProcess(KoikatuAPI.GameProcessName)] | ||
| #if KK | ||
| [BepInProcess(KoikatuAPI.GameProcessNameSteam)] | ||
| #endif | ||
| [BepInDependency(KoikatuAPI.GUID, KoikatuAPI.VersionConst)] | ||
| public class SpawnLockMain : BaseUnityPlugin | ||
| { | ||
| public const string PluginName = "Koikatsu Spawn Locker"; | ||
| public const string GUID = "SpawnLocker"; | ||
| public const string Version = "1.0.0"; | ||
|
|
||
| internal static new ManualLogSource Logger; | ||
|
|
||
| private void Main() | ||
ManlyMarco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| Logger = base.Logger; | ||
|
|
||
ManlyMarco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Hooks.Apply(); | ||
| } | ||
|
|
||
| public static bool IsLocked(ChaFileControl heroine) | ||
| { | ||
| if (heroine == null) | ||
| return false; | ||
|
|
||
| var pluginData = ExtendedSave.GetExtendedDataById(heroine, GUID); | ||
| return SpawnLockData.Load(pluginData)?.isLocked == true; | ||
ManlyMarco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public static bool IsLocked(SaveData.Heroine heroine) | ||
| { | ||
| return IsLocked(heroine?.charFile); | ||
| } | ||
|
|
||
| public static bool ToggleLock(ChaFileControl heroine) | ||
| { | ||
| if (heroine == null) | ||
| return false; | ||
|
|
||
| var data = new SpawnLockData(); | ||
| data.isLocked = !IsLocked(heroine); | ||
| ExtendedSave.SetExtendedDataById(heroine, GUID, data.Save()); | ||
|
|
||
| Logger.LogMessage("Number of locked heroines: " + _GetLockedHeroines()); | ||
|
|
||
| return data.isLocked; | ||
| } | ||
|
|
||
| public static bool ToggleLock(SaveData.Heroine heroine) | ||
| { | ||
| return ToggleLock(heroine?.charFile); | ||
| } | ||
|
|
||
| private static int _GetLockedHeroines() | ||
| { | ||
| #if KK | ||
| return Manager.Game.Instance.HeroineList.Count(IsLocked); | ||
| #elif KKS | ||
| return Manager.Game.HeroineList.Count(IsLocked); | ||
| #endif | ||
| } | ||
|
|
||
| internal static IEnumerable<SaveData.Heroine> _SpawnLockTake(IEnumerable<SaveData.Heroine> heroines, int n) | ||
| { | ||
| var lockedHeroines = new List<SaveData.Heroine>(); | ||
| var notLockedHeroines = new List<SaveData.Heroine>(); | ||
|
|
||
| foreach (var heroine in heroines) | ||
| { | ||
| if (IsLocked(heroine)) | ||
| lockedHeroines.Add(heroine); | ||
| else | ||
| notLockedHeroines.Add(heroine); | ||
| } | ||
|
|
||
| Logger.LogInfo($"Number of locked heroines: {lockedHeroines.Count}, Number of unlocked heroines: {notLockedHeroines.Count}"); | ||
|
|
||
| if (lockedHeroines.Count < n) | ||
| { | ||
| lockedHeroines.AddRange(notLockedHeroines.Take(n - lockedHeroines.Count)); | ||
| } | ||
|
|
||
| return lockedHeroines.Shuffle().Take(n); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.