Skip to content

Commit

Permalink
Port to KKS
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Jan 4, 2022
1 parent 064577e commit a80f45b
Show file tree
Hide file tree
Showing 27 changed files with 2,204 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/KKS_PantyFairy/Data/PantyFairyGameController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using ActionGame;
using KK_PantyFairy.Events;
using KK_PantyFairy.Functions;
using KKAPI.MainGame;

namespace KK_PantyFairy.Data
{
public class PantyFairyGameController : GameCustomFunctionController
{
protected override void OnGameLoad(GameSaveLoadEventArgs args)
{
var data = GetExtendedData();
CustomEvents.SaveData = PantyFairySaveData.Deserialize(data);
}

protected override void OnGameSave(GameSaveLoadEventArgs args)
{
var data = CustomEvents.SaveData.Serialize();
SetExtendedData(data);
}

protected override void OnDayChange(Cycle.Week day)
{
PantyStealFeat.ClearDepantified();
}

protected override void OnNewGame()
{
CustomEvents.SaveData = new PantyFairySaveData() { Progress = StoryProgress.E1_Initial };
}
}
}
73 changes: 73 additions & 0 deletions src/KKS_PantyFairy/Data/PantyFairySaveData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Reflection;
using ExtensibleSaveFormat;

namespace KK_PantyFairy.Data
{
public sealed class PantyFairySaveData
{
public StoryProgress Progress;
public int EventProgress;
public int PantiesStolenHeld;
public int PantiesStolenTotal;
public int UniformsStolenTotal;
//public byte[] FairyHeroineData;

#region SaveLoad

private static readonly PantyFairySaveData _default = new PantyFairySaveData();
private static readonly FieldInfo[] _serializedFields = typeof(PantyFairySaveData).GetFields(BindingFlags.Public | BindingFlags.Instance);

public static PantyFairySaveData Deserialize(PluginData data)
{
var result = new PantyFairySaveData();
if (data != null)
{
foreach (var fieldInfo in _serializedFields)
{
if (data.data.TryGetValue(fieldInfo.Name, out var val))
{
try
{
if (fieldInfo.FieldType.IsEnum) val = (int)val;
fieldInfo.SetValue(result, val);
}
catch (Exception ex)
{
PantyFairyPlugin.Logger.LogError($"Could not deserialize field {fieldInfo.Name} because of error: {ex.Message}");
}
}
}

if (result.Progress == StoryProgress.Unknown || !Enum.IsDefined(typeof(StoryProgress), result.Progress))
{
result.Progress = StoryProgress.E1_Initial;
PantyFairyPlugin.Logger.LogInfo("Resetting Story Progress to E1_Initial from " + result.Progress);
}
}
else
{
result.Progress = StoryProgress.E1_Initial;
}

return result;
}

public PluginData Serialize()
{
var result = new PluginData { version = 1 };
foreach (var fieldInfo in _serializedFields)
{
var value = fieldInfo.GetValue(this);
// Check if any value is different than default, if not then don't save any data
var defaultValue = fieldInfo.GetValue(_default);
if (!Equals(defaultValue, value))
result.data.Add(fieldInfo.Name, value);
}

return result.data.Count > 0 ? result : null;
}

#endregion
}
}
18 changes: 18 additions & 0 deletions src/KKS_PantyFairy/Data/StoryProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace KK_PantyFairy.Data
{
/// <summary>
/// Used to keep track what events to show and what skills to unlock
/// </summary>
public enum StoryProgress
{
Unknown = 0,
E1_Initial = 1,
E2_Meeting = 2,
E3_FirstRaid = 3,
E4_SecondRaid = 4,
E5_Steal = 5,
E6_GustOfWind = 6,
E7_Uniform = 7,
Complete = 8
}
}
50 changes: 50 additions & 0 deletions src/KKS_PantyFairy/Events/CustomEventData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using KK_PantyFairy.Data;

namespace KK_PantyFairy.Events
{
public class CustomEventData
{
public bool Running { get; private set; }
public bool Initialized { get; private set; }

public readonly StoryProgress Index;
private readonly Func<IDisposable> _initialize;
private readonly Action<bool> _runningChanged;

private IDisposable _cleanupCallback;

public CustomEventData(StoryProgress index,
Func<IDisposable> initialize,
Action<bool> runningChanged = null)
{
Index = index;
_initialize = initialize;
_runningChanged = runningChanged;
}

public void SetRunning(bool running)
{
if (Running != running)
{
if (running)
{
if (!Initialized)
{
_cleanupCallback = _initialize();
Initialized = true;
}
}
else
{
_cleanupCallback?.Dispose();
Initialized = false;
}

_runningChanged?.Invoke(running);

Running = running;
}
}
}
}
Loading

0 comments on commit a80f45b

Please sign in to comment.