Skip to content

Commit 95b5286

Browse files
committed
add tool at "Game State" to reset containers, crates and pickups in caves and open world
1 parent 9f3dc74 commit 95b5286

5 files changed

Lines changed: 76 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v0.5.1
4+
- add tool at "Game State" to reset containers, crates and pickups in caves and open world
5+
36
## v0.5.0
47
- add storage editing (unlimited logs, sticks etc.)
58
- add selection of Kelvin's and Virginia's outfit

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This project is in no way or form associated with the developers of the game. It
4545
- Set stats for Virginia & Kelvin
4646
- Move Kelvin & Virginia to Player or each other
4747
- Regrow Trees selectively (All, Removed, Half-Chopped, Stumps)
48+
- Reset containers, crates and pickups in caves and open world
4849
- Backup changed files automatically
4950
- ... more features are planned
5051

SOTFEdit/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
// app, or any theme specific resource dictionaries)
1313
)]
1414
[assembly: AssemblyCompany("codengine")]
15-
[assembly: AssemblyFileVersion("0.5.0")]
16-
[assembly: AssemblyInformationalVersion("0.5.0")]
15+
[assembly: AssemblyFileVersion("0.5.1")]
16+
[assembly: AssemblyInformationalVersion("0.5.1")]
1717
[assembly: AssemblyProduct("SOTFEdit")]
1818
[assembly: AssemblyTitle("SOTFEdit")]
19-
[assembly: AssemblyVersion("0.5.0")]
19+
[assembly: AssemblyVersion("0.5.1")]
2020
[assembly: TargetPlatform("Windows7.0")]
2121
[assembly: SupportedOSPlatform("Windows7.0")]
2222
[assembly: Guid("d59ec208-5fc6-4336-a9db-dbeb36938f78")]

SOTFEdit/View/GameStatePage.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@
5050
</DataGrid>
5151
</DockPanel>
5252
</GroupBox>
53+
<GroupBox Grid.Row="0" Grid.Column="1" Header="Tools" VerticalAlignment="Top" Margin="0 0 5 0" Padding="15">
54+
<Button ToolTip="Resets crates, boxes and cases in caves and the open world" Command="{Binding ResetContainersCommand}" FontSize="14">Reset Crates and Containers</Button>
55+
</GroupBox>
5356
</Grid>
5457
</UserControl>

SOTFEdit/ViewModel/GameStatePageViewModel.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
33
using System.Linq;
4+
using System.Text.RegularExpressions;
5+
using CommunityToolkit.Mvvm.ComponentModel;
6+
using CommunityToolkit.Mvvm.DependencyInjection;
7+
using CommunityToolkit.Mvvm.Input;
48
using CommunityToolkit.Mvvm.Messaging;
59
using Newtonsoft.Json.Linq;
610
using SOTFEdit.Infrastructure;
@@ -9,15 +13,21 @@
913

1014
namespace SOTFEdit.ViewModel;
1115

12-
public class GameStatePageViewModel
16+
public partial class GameStatePageViewModel : ObservableObject
1317
{
18+
private readonly Regex _resetCrateNameIdPattern =
19+
new(@"(\..*Crate.*\.)|(\..*Storage.*\.)|(\..*Case.*\.)|(.*\.Meds\..*)");
20+
1421
public GameStatePageViewModel()
1522
{
1623
SetupListeners();
1724
}
1825

1926
public ObservableCollection<GenericSetting> Settings { get; } = new();
2027

28+
[NotifyCanExecuteChangedFor(nameof(ResetContainersCommand))] [ObservableProperty]
29+
private Savegame? _selectedSavegame;
30+
2131
private void SetupListeners()
2232
{
2333
WeakReferenceMessenger.Default.Register<SelectedSavegameChangedEvent>(this,
@@ -26,6 +36,7 @@ private void SetupListeners()
2636

2737
private void OnSelectedSavegameChanged(SelectedSavegameChangedEvent message)
2838
{
39+
SelectedSavegame = message.SelectedSavegame;
2940
Settings.Clear();
3041
var gameStateData =
3142
message.SelectedSavegame?.SavegameStore.LoadJsonRaw(SavegameStore.FileType.GameStateSaveData);
@@ -39,6 +50,60 @@ private void OnSelectedSavegameChanged(SelectedSavegameChangedEvent message)
3950
LoadSettings(gameState);
4051
}
4152

53+
private bool HasSavegame()
54+
{
55+
return _selectedSavegame != null;
56+
}
57+
58+
[RelayCommand(CanExecute = nameof(HasSavegame))]
59+
public void ResetContainers()
60+
{
61+
if (SelectedSavegame is not { } savegame)
62+
{
63+
return;
64+
}
65+
66+
var gameStateData = savegame.SavegameStore.LoadJsonRaw(SavegameStore.FileType.GameStateSaveData);
67+
68+
if (gameStateData?.SelectToken("Data.GameState") is not { } gameStateToken ||
69+
gameStateToken.ToObject<string>() is not { } gameStateJson ||
70+
JsonConverter.DeserializeRaw(gameStateJson) is not { } gameState ||
71+
gameState["NamedIntDatas"] is not { } namedIntDatas)
72+
{
73+
WeakReferenceMessenger.Default.Send(new SavegameStoredEvent("Nothing to be reset", false));
74+
return;
75+
}
76+
77+
var countFixed = 0;
78+
79+
foreach (var data in namedIntDatas)
80+
{
81+
if (data["SaveObjectNameId"]?.ToObject<string>() is not { } nameId ||
82+
!_resetCrateNameIdPattern.Match(nameId).Success || data["SaveValue"] is not { } saveValueToken)
83+
{
84+
continue;
85+
}
86+
87+
var oldSaveValue = saveValueToken.ToObject<int>();
88+
89+
if (oldSaveValue != 1)
90+
{
91+
continue;
92+
}
93+
94+
saveValueToken.Replace(0);
95+
countFixed++;
96+
}
97+
98+
gameStateToken.Replace(JsonConverter.Serialize(gameState));
99+
100+
var createBackups = Ioc.Default.GetRequiredService<MainViewModel>().BackupFiles;
101+
savegame.SavegameStore.StoreJson(SavegameStore.FileType.GameStateSaveData, gameStateData, createBackups);
102+
103+
WeakReferenceMessenger.Default.Send(
104+
new SavegameStoredEvent($"Reset {countFixed} containers, creates and pickups", true));
105+
}
106+
42107
private void LoadSettings(JToken gameState)
43108
{
44109
var children = gameState.Children();

0 commit comments

Comments
 (0)