Skip to content

Commit

Permalink
mod options
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexejhero committed Oct 31, 2023
1 parent 3bf7a90 commit 8406ff7
Show file tree
Hide file tree
Showing 68 changed files with 911 additions and 253 deletions.
36 changes: 0 additions & 36 deletions SCHIZO/Config.cs

This file was deleted.

4 changes: 1 addition & 3 deletions SCHIZO/Events/GameEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public record StoryGoals(GameEvent evt)
public bool IsFirstTime { get; private set; } = true;

protected Player player;
protected GameEventsManager config;
protected string RequiredStoryGoal => RetargetHelpers.Pick(requiredStoryGoal_SN, requiredStoryGoal_BZ);
protected bool IsUnlocked { get; private set; }
public StoryGoals Goals { get; private set; }
Expand All @@ -23,7 +22,6 @@ protected void Awake()
{
Goals = new StoryGoals(this);
player = Player.main;
config = GetComponent<GameEventsManager>();

StoryGoalManager.main.AddListener(this);
if (StoryGoalHelpers.IsCompleted(Goals.FirstTime)) IsFirstTime = false;
Expand Down Expand Up @@ -97,7 +95,7 @@ protected void FixedUpdate()
else
{
bool ableToAutoStart = canAutoStart
&& config.AutoStartEvents
&& GameEventsManager.AutoStart
&& IsUnlocked;
if (ableToAutoStart && ShouldStartEvent()) StartEvent();
}
Expand Down
41 changes: 16 additions & 25 deletions SCHIZO/Events/GameEventsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,42 @@
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Nautilus.Commands;
using Nautilus.Utility;
using SCHIZO.ConsoleCommands;
using SCHIZO.Helpers;
using UnityEngine;
using SCHIZO.Resources;

namespace SCHIZO.Events;

[RegisterConsoleCommands]
public partial class GameEventsManager
{
public static GameEventsManager Instance { get; private set; }
public static List<GameEvent> Events { get; private set; } = new();

private const string AUTOEVENTS_PREFS_KEY = "SCHIZO_Events_AutoEvents";
public static bool HasBeenSet => PlayerPrefs.HasKey(AUTOEVENTS_PREFS_KEY);
public bool AutoStartEvents
public static bool AutoStart
{
get => GetAutoStart(autoStartEvents);
set => SetAutoStart(value);
get => Assets.Options_EnableAutomaticEvents.Value;
set => Assets.Options_EnableAutomaticEvents.Value = value;
}

public static bool GetAutoStart(bool defaultVal = true) => PlayerPrefsExtra.GetBool(AUTOEVENTS_PREFS_KEY, defaultVal);
public static void SetAutoStart(bool value) => PlayerPrefsExtra.SetBool(AUTOEVENTS_PREFS_KEY, value);
private static GameEventsManager Instance { get; set; }
private static List<GameEvent> Events { get; set; } = new();

private void Awake()
{
if (Instance) Destroy(Instance);
Instance = this;
gameObject.GetComponents(Events);

if (!HasBeenSet || overridePlayerPrefs) SetAutoStart(autoStartEvents);
}

[ConsoleCommand("autoevents"), UsedImplicitly]
public static void OnConsoleCommand_autoevents(bool? action = null)
{
if (action == null)
{
MessageHelpers.WriteCommandOutput($"Events are currently {FormatAutoStart(GetAutoStart())}");
MessageHelpers.WriteCommandOutput($"Events are currently {FormatAutoStart(AutoStart)}");
return;
}

SetAutoStart(action.Value);
AutoStart = action.Value;

MessageHelpers.WriteCommandOutput($"Events are now {FormatAutoStart(action.Value)}");
}

Expand All @@ -62,21 +55,19 @@ public void Update()
}
#endif

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string FormatAutoStart(bool value) => value ? "automatic" : "manual";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string FormatStartEnd(bool start) => start ? "start" : "end";

[ConsoleCommand("event"), UsedImplicitly]
public static string OnConsoleCommand_event(string eventName, bool start)
{
GameEvent @event = Events.FirstOrDefault(e => e.Name.Equals(eventName, System.StringComparison.OrdinalIgnoreCase));
if (!@event) return MessageHelpers.GetCommandOutput($"No event named '{eventName}'");

if (start)
@event.StartEvent();
else
@event.EndEvent();
if (start) @event.StartEvent();
else @event.EndEvent();
return MessageHelpers.GetCommandOutput($"{FormatStartEnd(start)}ed event {@event.GetType().Name}");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string FormatAutoStart(bool value) => value ? "automatic" : "manual";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string FormatStartEnd(bool start) => start ? "start" : "end";
}
9 changes: 9 additions & 0 deletions SCHIZO/Options/ConfigurableValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SCHIZO.Options;

partial class ConfigurableValue<TRaw, TModOption>
{
public virtual TRaw GetValue()
{
return isHardCoded ? value : modOption.Value;
}
}
18 changes: 18 additions & 0 deletions SCHIZO/Options/ConfigurableValueFloat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using UnityEngine;

namespace SCHIZO.Options;

partial class ConfigurableValueFloat
{
public override float GetValue()
{
return calculateMode switch
{
CalculateMode.OneOf => base.GetValue(),
CalculateMode.Min => Mathf.Min(value, modOption.Value),
CalculateMode.Max => Mathf.Max(value, modOption.Value),
_ => throw new ArgumentOutOfRangeException()
};
}
}
62 changes: 62 additions & 0 deletions SCHIZO/Options/ModOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using Nautilus.Handlers;
using Nautilus.Options;
using UnityEngine;

namespace SCHIZO.Options;

partial class ModOption<T>
{
protected string PlayerPrefsKey => $"SCHIZO_ModOption_{id}";

public T Value
{
get => ValueInternal;
set
{
ValueInternal = value;
OnValueChanged?.Invoke(value);
}
}
protected abstract T ValueInternal { get; set; }

public event Action<T> OnValueChanged;
}

partial class ModOption
{
public static Dictionary<OptionItem, ModOption> OptionItems { get; } = new();

public OptionItem GetOptionItem()
{
OptionItem result = GetOptionItemInternal();
OptionItems[result] = this;
return result;
}

protected abstract OptionItem GetOptionItemInternal();

public virtual void AddRealtimeUpdater(GameObject optionObject)
{
RealtimeOptionUpdater updater = optionObject.AddComponent<RealtimeOptionUpdater>();
updater.modOption = this;
updater.OnEnable();

foreach (ToggleModOption toggleModOption in disableIfAnyFalse)
{
toggleModOption.OnValueChanged += _ =>
{
if (updater) updater.OnEnable();
};
}

foreach (ToggleModOption toggleModOption in disableIfAnyTrue)
{
toggleModOption.OnValueChanged += _ =>
{
if (updater) updater.OnEnable();
};
}
}
}
11 changes: 11 additions & 0 deletions SCHIZO/Options/ModOptionPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Nautilus.Handlers;

namespace SCHIZO.Options;

partial class ModOptionPanel
{
protected override void Register()
{
OptionsPanelHandler.RegisterModOptions(new RuntimeModOptions(options));
}
}
17 changes: 17 additions & 0 deletions SCHIZO/Options/RealtimeOptionUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Linq;
using UnityEngine;

namespace SCHIZO.Options;

public sealed class RealtimeOptionUpdater : MonoBehaviour
{
public ModOption modOption;

public void OnEnable()
{
if (!modOption) return;

if (modOption.disableIfAnyFalse.Any(o => !o.Value) || modOption.disableIfAnyTrue.Any(o => o.Value)) gameObject.SetActive(false);
else gameObject.SetActive(true);
}
}
23 changes: 23 additions & 0 deletions SCHIZO/Options/RuntimeModOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using Nautilus.Options;

namespace SCHIZO.Options;

public sealed class RuntimeModOptions : ModOptions
{
public RuntimeModOptions(List<ModOption> options) : base("Neuro-sama Mod")
{
foreach (ModOption option in options)
{
AddItem(option.GetOptionItem());
}

GameObjectCreated += OnGameObjectCreated;
}

private void OnGameObjectCreated(object _, GameObjectCreatedEventArgs evt)
{
ModOption modOption = ModOption.OptionItems[_options[evt.Id]];
modOption.AddRealtimeUpdater(evt.Value);
}
}
26 changes: 26 additions & 0 deletions SCHIZO/Options/SliderModOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Nautilus.Options;
using UnityEngine;

namespace SCHIZO.Options;

partial class SliderModOption
{
protected override float ValueInternal
{
get => PlayerPrefs.GetFloat(PlayerPrefsKey, defaultValue);
set => PlayerPrefs.SetFloat(PlayerPrefsKey, value);
}

protected override OptionItem GetOptionItemInternal()
{
ModSliderOption option = ModSliderOption.Create(id, label, min, max, Value, defaultValue, valueFormat, step, tooltip);
option.OnChanged += (_, args) => Value = args.Value;

OnValueChanged += value =>
{
if (option.OptionGameObject) option.OptionGameObject.GetComponentInChildren<uGUI_SnappingSlider>().SetValueWithoutNotify(value);
};

return option;
}
}
27 changes: 27 additions & 0 deletions SCHIZO/Options/ToggleModOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Nautilus.Options;
using Nautilus.Utility;
using UnityEngine.UI;

namespace SCHIZO.Options;

partial class ToggleModOption
{
protected override bool ValueInternal
{
get => PlayerPrefsExtra.GetBool(PlayerPrefsKey, defaultValue);
set => PlayerPrefsExtra.SetBool(PlayerPrefsKey, value);
}

protected override OptionItem GetOptionItemInternal()
{
ModToggleOption option = ModToggleOption.Create(id, label, Value, tooltip);
option.OnChanged += (_, args) => Value = args.Value;

OnValueChanged += value =>
{
if (option.OptionGameObject) option.OptionGameObject.GetComponentInChildren<Toggle>().SetIsOnWithoutNotify(value);
};

return option;
}
}
4 changes: 0 additions & 4 deletions SCHIZO/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public sealed class Plugin : BaseUnityPlugin
public static ManualLogSource LOGGER { get; private set; }
public static Harmony HARMONY { get; private set; }

public static readonly Config CONFIG = OptionsPanelHandler.RegisterModOptions<Config>();

private void Awake()
{
PLUGIN_ASSEMBLY = Assembly.GetExecutingAssembly();
Expand All @@ -32,8 +30,6 @@ private void Awake()
HARMONY = new Harmony("SCHIZO");

ResourceManager.InjectAssemblies();

SoundConfig.Provider = CONFIG;
}

private IEnumerator Start()
Expand Down
4 changes: 3 additions & 1 deletion SCHIZO/Resources/AssetBundles/Assets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SCHIZO.Resources;

public static class Assets
{
private const int _rnd = -638500889;
private const int _rnd = -1806460644;

private static readonly UnityEngine.AssetBundle _a = ResourceManager.GetAssetBundle("assets");

Expand All @@ -30,5 +30,7 @@ public static class Assets
public static SCHIZO.Items.Data.CloneItemData Gymbag_GymbagBZ = _a.LoadAsset<SCHIZO.Items.Data.CloneItemData>("Assets/Gymbag/Gymbag BZ.asset");
public static SCHIZO.Items.Data.CloneItemData Gymbag_GymbagSN = _a.LoadAsset<SCHIZO.Items.Data.CloneItemData>("Assets/Gymbag/Gymbag SN.asset");
public static UnityEngine.Texture2D Loading_Icon_LoadingIcon = _a.LoadAsset<UnityEngine.Texture2D>("Assets/Loading/Icon/loading icon.png");
public static SCHIZO.Options.ToggleModOption Options_DisableAllSounds = _a.LoadAsset<SCHIZO.Options.ToggleModOption>("Assets/Options/Disable all sounds.asset");
public static SCHIZO.Options.ToggleModOption Options_EnableAutomaticEvents = _a.LoadAsset<SCHIZO.Options.ToggleModOption>("Assets/Options/Enable automatic events.asset");
public static SCHIZO.Registering.ModRegistry Registry = _a.LoadAsset<SCHIZO.Registering.ModRegistry>("Assets/Registry.asset");
}
Binary file modified SCHIZO/Resources/AssetBundles/assets
Binary file not shown.
1 change: 1 addition & 0 deletions SCHIZO/SCHIZO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Publicize Include="ECCLibrary:ECCLibrary.ObjectReferences" />
<Publicize Include="Nautilus:Nautilus.Commands.Parameter" />
<Publicize Include="Nautilus:Nautilus.Commands.Parameter.TypeConverters" />
<Publicize Include="Nautilus:Nautilus.Options.ModOptions._options" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 8406ff7

Please sign in to comment.