Skip to content

Commit

Permalink
Add debug mode flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvinas01 committed Sep 8, 2024
1 parent 693f323 commit 4bbabf3
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 3 deletions.
1 change: 1 addition & 0 deletions Packages/com.chark.game-management/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- `GameManager.ReadResourceAsync` and `GameManager.ReadResourceStreamAsync` methods which can be used to retrieve resources from StreamingAssets directory.
- `GameManager.ReadDataStream` and `GameManager.ReadDataStreamAsync` methods which can be used to read a `Stream` from a file on disk.
- `GameManager.SaveDataStream` and `GameManager.SaveDataStreamAsync` methods which can be used to persist a `Stream` to disk.
- `GameManager.IsDebuggingEnabled` flag which can be used to turn on if debug mode is on for the game manager.

### Changed

Expand Down
7 changes: 7 additions & 0 deletions Packages/com.chark.game-management/Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ GameManager.SetEditorValue("some-key", value);

// Delete an Editor value synchronously
GameManager.DeleteEditorValue("some-key");

// Change debugging mode state
GameManager.IsDebuggingEnabled = true;
GameManager.IsDebuggingEnabled = false;

// Check for debug mode state changes and react accordingly
GameManager.AddListener<DebuggingChangedMessage>(message => { });
```

### Systems
Expand Down
1 change: 1 addition & 0 deletions Packages/com.chark.game-management/Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("CHARK.GameManagement.Tests.Editor")]
[assembly: InternalsVisibleTo("CHARK.GameManagement.Tests.Runtime")]
[assembly: InternalsVisibleTo("CHARK.GameManagement.Editor")]
50 changes: 50 additions & 0 deletions Packages/com.chark.game-management/Runtime/GameManager.External.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,62 @@
using CHARK.GameManagement.Serialization;
using CHARK.GameManagement.Storage;
using CHARK.GameManagement.Systems;
using UnityEngine;
using Object = UnityEngine.Object;

namespace CHARK.GameManagement
{
public abstract partial class GameManager
{
/// <summary>
/// <c>true</c> if debug mode is enabled or <c>false</c> otherwise. When this value changes, a
/// <see cref="DebuggingChangedMessage"/> is raised.
/// </summary>
public static bool IsDebuggingEnabled
{
get
{
#if UNITY_EDITOR
if (Application.isPlaying)
{
return isDebuggingEnabled;
}

if (TryReadEditorData<bool>(IsDebuggingEnabledKey, out var value))
{
return value;
}
#endif
return isDebuggingEnabled;
}
set
{
var oldValue = IsDebuggingEnabled;
var newValue = value;

#if UNITY_EDITOR
SaveEditorData(IsDebuggingEnabledKey, newValue);
#endif

isDebuggingEnabled = newValue;

#if UNITY_EDITOR
if (Application.isPlaying == false)
{
return;
}
#endif

if (oldValue == newValue)
{
return;
}

var message = new DebuggingChangedMessage(oldValue, newValue);
Publish(message);
}
}

/// <inheritdoc cref="IResourceLoader.GetResources{TResource}"/>
public static IEnumerable<TResource> GetResources<TResource>(string path = null)
where TResource : Object
Expand Down
12 changes: 12 additions & 0 deletions Packages/com.chark.game-management/Runtime/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public abstract partial class GameManager : MonoBehaviour

private static GameManagerSettings Settings => GameManagerSettings.Instance;

private const string IsDebuggingEnabledKey = nameof(GameManager) + "." + nameof(IsDebuggingEnabled);
private static bool isDebuggingEnabled;

private bool isSystemsInitialized;

private ISerializer serializer;
Expand All @@ -44,6 +47,15 @@ private void Awake()
return;
}

#if UNITY_EDITOR
if (TryReadEditorData(IsDebuggingEnabledKey, out bool value))
{
isDebuggingEnabled = value;
}
#else
isDebuggingEnabled = Debug.isDebugBuild;
#endif

var isInitialized = currentGameManager == true;

currentGameManager = this;
Expand Down
26 changes: 26 additions & 0 deletions Packages/com.chark.game-management/Runtime/Messages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CHARK.GameManagement.Messaging;

namespace CHARK.GameManagement
{
/// <summary>
/// Raised when <see cref="GameManager.IsDebuggingEnabled"/> changes.
/// </summary>
public readonly struct DebuggingChangedMessage : IMessage
{
/// <summary>
/// Debugging mode state the game manager transitioned from.
/// </summary>
public bool IsDebuggingEnabledPrev { get; }

/// <summary>
/// Debugging mode state the game manager is currently in.
/// </summary>
public bool IsDebuggingEnabled { get; }

public DebuggingChangedMessage(bool isDebuggingEnabledPrev, bool isDebuggingEnabledNew)
{
IsDebuggingEnabledPrev = isDebuggingEnabledPrev;
IsDebuggingEnabled = isDebuggingEnabledNew;
}
}
}
3 changes: 3 additions & 0 deletions Packages/com.chark.game-management/Runtime/Messages.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ namespace CHARK.GameManagement.Settings
internal sealed class GameManagerSettings : ScriptableObject
{
[Tooltip(
"List of available settings profiles, the first active profile will be picked during " +
"Game Manager initialization"
"List of available settings profiles, the first active profile will be picked during "
+ "Game Manager initialization"
)]
[SerializeField]
private List<GameManagerSettingsProfile> profiles = new();

private IGameManagerSettingsProfile profileOverride;
private static GameManagerSettings currentSettings;

/// <summary>
Expand All @@ -31,7 +32,11 @@ internal static GameManagerSettings Instance
/// <summary>
/// Currently active settings instance. Never <c>null</c>.
/// </summary>
internal IGameManagerSettingsProfile ActiveProfile => GetActiveProfile();
internal IGameManagerSettingsProfile ActiveProfile
{
get => GetActiveProfile();
set => profileOverride = value;
}

/// <summary>
/// Add a set of new profiles to the <see cref="profiles"/> list. Duplicated profiles will
Expand Down Expand Up @@ -90,6 +95,11 @@ private static bool TryGetSettings(out GameManagerSettings settings)

private IGameManagerSettingsProfile GetActiveProfile()
{
if (profileOverride != default)
{
return profileOverride;
}

if (profiles.Count == 0)
{
return DefaultGameManagerSettingsProfile.Instance;
Expand Down

0 comments on commit 4bbabf3

Please sign in to comment.