Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/engine/CheatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,30 @@ public static void OnCheatsDisabled()
DisableAllCheats();
HideCheatMenus();
}

/// <summary>
/// Captures the current state of all cheats for saving
/// </summary>
public static CheatManagerState CaptureState()
{
return new CheatManagerState(InfiniteCompounds, GodMode, NoAI, UnlimitedGrowthSpeed,
LockTime, ManuallySetTime, Speed, InfiniteMP, MoveToAnyPatch, DayNightFraction);
}

/// <summary>
/// Restores cheat state from saved data
/// </summary>
public static void RestoreState(CheatManagerState state)
{
InfiniteCompounds = state.InfiniteCompounds;
GodMode = state.GodMode;
NoAI = state.NoAI;
UnlimitedGrowthSpeed = state.UnlimitedGrowthSpeed;
LockTime = state.LockTime;
ManuallySetTime = state.ManuallySetTime;
Speed = state.Speed;
InfiniteMP = state.InfiniteMP;
MoveToAnyPatch = state.MoveToAnyPatch;
DayNightFraction = state.DayNightFraction;
}
}
80 changes: 80 additions & 0 deletions src/engine/CheatManagerState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using SharedBase.Archive;

/// <summary>
/// Serializable state of all cheats managed by <see cref="CheatManager"/>
/// </summary>
public class CheatManagerState : IArchivable
{
public const ushort SERIALIZATION_VERSION = 1;

public CheatManagerState()
{
}

public CheatManagerState(bool infiniteCompounds, bool godMode, bool noAI, bool unlimitedGrowthSpeed,
bool lockTime, bool manuallySetTime, float speed, bool infiniteMP, bool moveToAnyPatch,
float dayNightFraction)
{
InfiniteCompounds = infiniteCompounds;
GodMode = godMode;
NoAI = noAI;
UnlimitedGrowthSpeed = unlimitedGrowthSpeed;
LockTime = lockTime;
ManuallySetTime = manuallySetTime;
Speed = speed;
InfiniteMP = infiniteMP;
MoveToAnyPatch = moveToAnyPatch;
DayNightFraction = dayNightFraction;
}

public bool InfiniteCompounds { get; set; }
public bool GodMode { get; set; }
public bool NoAI { get; set; }
public bool UnlimitedGrowthSpeed { get; set; }
public bool LockTime { get; set; }
public bool ManuallySetTime { get; set; }
public float Speed { get; set; } = 1.0f;
public bool InfiniteMP { get; set; }
public bool MoveToAnyPatch { get; set; }
public float DayNightFraction { get; set; }

public ushort CurrentArchiveVersion => SERIALIZATION_VERSION;
public ArchiveObjectType ArchiveObjectType => (ArchiveObjectType)ThriveArchiveObjectType.CheatManagerState;
public bool CanBeReferencedInArchive => false;

public static CheatManagerState ReadFromArchive(ISArchiveReader reader, ushort version, int referenceId)
{
if (version is > SERIALIZATION_VERSION or <= 0)
throw new InvalidArchiveVersionException(version, SERIALIZATION_VERSION);

var instance = new CheatManagerState
{
InfiniteCompounds = reader.ReadBool(),
GodMode = reader.ReadBool(),
NoAI = reader.ReadBool(),
UnlimitedGrowthSpeed = reader.ReadBool(),
LockTime = reader.ReadBool(),
ManuallySetTime = reader.ReadBool(),
Speed = reader.ReadFloat(),
InfiniteMP = reader.ReadBool(),
MoveToAnyPatch = reader.ReadBool(),
DayNightFraction = reader.ReadFloat(),
};

return instance;
}

public void WriteToArchive(ISArchiveWriter writer)
{
writer.Write(InfiniteCompounds);
writer.Write(GodMode);
writer.Write(NoAI);
writer.Write(UnlimitedGrowthSpeed);
writer.Write(LockTime);
writer.Write(ManuallySetTime);
writer.Write(Speed);
writer.Write(InfiniteMP);
writer.Write(MoveToAnyPatch);
writer.Write(DayNightFraction);
}
}
1 change: 1 addition & 0 deletions src/engine/CheatManagerState.cs.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://bdt6hwmh2mt8y
16 changes: 15 additions & 1 deletion src/general/GameProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// </summary>
public class GameProperties : IArchivable
{
public const int SERIALIZATION_VERSION = 2;
public const int SERIALIZATION_VERSION = 3;

private readonly Dictionary<string, bool> setBoolStatuses;

Expand Down Expand Up @@ -47,6 +47,11 @@ private GameProperties(GameWorld gameWorld, TutorialState tutorialState, Diction
/// </summary>
public bool CheatsUsed { get; private set; }

/// <summary>
/// Holds the state of all cheats used in this game
/// </summary>
public CheatManagerState SavedCheats { get; set; } = new();

/// <summary>
/// True when the player is currently ascended and should be allowed to do anything
/// </summary>
Expand Down Expand Up @@ -324,6 +329,12 @@ public static GameProperties ReadFromArchive(ISArchiveReader reader, ushort vers
if (version > 1)
instance.ThriveopediaData = reader.ReadObject<ThriveopediaGameData>();

if (version > 2)
{
instance.SavedCheats = reader.ReadObject<CheatManagerState>();
CheatManager.RestoreState(instance.SavedCheats);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't apply here, there are situations to load a save's data without immediately loading it. So the cheats state should be reapplied in InProgressLoad I think.

}

return instance;
}

Expand All @@ -345,6 +356,9 @@ public void WriteToArchive(ISArchiveWriter writer)
// writer.WriteObject(TechWeb);

writer.WriteObject(ThriveopediaData);

SavedCheats = CheatManager.CaptureState();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this is also an unsafe assumption, so again the save capturing code should snapshot the cheats when it starts to make a save.

writer.WriteObject(SavedCheats);
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/general/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public partial class MainMenu : NodeWithInput

public static void OnEnteringGame(bool cheatsUsed, bool freebuild)
{
CheatManager.OnCheatsDisabled();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not safe to remove... at least I expect there to be a chance to "smuggle" cheat state into a save without getting marked as having cheated in the game.

SaveHelper.ClearLastSaveTime();
LastPlayedVersion.MarkCurrentVersionAsPlayed();
AchievementsManager.ReportNewGameStarted(cheatsUsed);
Expand Down
4 changes: 4 additions & 0 deletions src/microbe_stage/editor/MicrobeEditorCheatMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ public partial class MicrobeEditorCheatMenu : CheatMenu
#pragma warning disable CA2213
[Export]
private CheckBox infiniteMp = null!;

[Export]
private CheckBox moveToAnyPatch = null!;
#pragma warning restore CA2213

public override void ReloadGUI()
{
infiniteMp.ButtonPressed = CheatManager.InfiniteMP;
moveToAnyPatch.ButtonPressed = CheatManager.MoveToAnyPatch;
}

private void OnRevealAllPatchesPressed()
Expand Down
3 changes: 2 additions & 1 deletion src/microbe_stage/editor/MicrobeEditorCheatMenu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
[ext_resource type="PackedScene" uid="uid://du8sc8kjirguk" path="res://src/gui_common/dialogs/CustomWindow.tscn" id="1"]
[ext_resource type="Script" uid="uid://b8pglhtmiphys" path="res://src/microbe_stage/editor/MicrobeEditorCheatMenu.cs" id="2"]

[node name="MicrobeEditorCheatMenu" unique_id=1599049517 node_paths=PackedStringArray("infiniteMp") instance=ExtResource("1")]
[node name="MicrobeEditorCheatMenu" unique_id=1599049517 node_paths=PackedStringArray("infiniteMp", "moveToAnyPatch") instance=ExtResource("1")]
offset_right = 0.0
offset_bottom = 0.0
script = ExtResource("2")
infiniteMp = NodePath("VBoxContainer/InfiniteMP")
moveToAnyPatch = NodePath("VBoxContainer/MoveToAnyPatch")
WindowTitle = "CHEATS"
Resizable = true

Expand Down
1 change: 1 addition & 0 deletions src/saving/ThriveArchiveObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ public enum ThriveArchiveObjectType : uint
TutorialMicrobeSpecializationTutorial = 4402,
TutorialMulticellularSpecializationTutorial = 4403,
MassBuddingCellCountActionData = 4404,
CheatManagerState = 4405,

// Special flag types
ExtendedOrganelleLayout = OrganelleLayout | ArchiveObjectType.ExtendedTypeFlag,
Expand Down
4 changes: 3 additions & 1 deletion src/saving/serializers/ThriveArchiveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ private void RegisterOtherObjects()
{
RegisterObjectType((ArchiveObjectType)ThriveArchiveObjectType.GameProperties, typeof(GameProperties),
GameProperties.ReadFromArchive);
RegisterObjectType((ArchiveObjectType)ThriveArchiveObjectType.CheatManagerState, typeof(CheatManagerState),
CheatManagerState.ReadFromArchive);
RegisterObjectType((ArchiveObjectType)ThriveArchiveObjectType.GameWorld, typeof(GameWorld),
GameWorld.ReadFromArchive);
GameWorld.ReadFromArchive);

RegisterObjectType((ArchiveObjectType)ThriveArchiveObjectType.WorldGenerationSettings,
typeof(WorldGenerationSettings),
Expand Down