77using CommunityToolkit . Mvvm . ComponentModel ;
88using CommunityToolkit . Mvvm . Input ;
99using CommunityToolkit . Mvvm . Messaging ;
10+ using Newtonsoft . Json . Linq ;
1011using SOTFEdit . Infrastructure ;
1112using SOTFEdit . Model ;
1213using SOTFEdit . Model . Events ;
@@ -17,6 +18,8 @@ namespace SOTFEdit.ViewModel;
1718
1819public partial class InventoryPageViewModel : ObservableObject
1920{
21+ private const int ItemPlatingItemId = 665 ;
22+ private const int PoweredCrossItemId = 666 ;
2023 private readonly ObservableCollectionEx < InventoryItem > _inventory = new ( ) ;
2124
2225 private readonly DispatcherTimer _inventoryFilterTimer = new ( )
@@ -225,7 +228,81 @@ public bool Update(Savegame savegame)
225228
226229 var selectedItems = new List < InventoryItem > ( _inventory ) ;
227230
228- return PlayerInventoryDataModel . Merge ( saveDataWrapper , selectedItems ) ;
231+ var hasChanged = PlayerInventoryDataModel . Merge ( saveDataWrapper , selectedItems ) ;
232+ return EnsureBlueprintStatusUpdated ( savegame . SavegameStore , selectedItems ) || hasChanged ;
233+ }
234+
235+ private bool EnsureBlueprintStatusUpdated ( SavegameStore savegameStore , List < InventoryItem > inventoryItems )
236+ {
237+ if ( savegameStore . LoadJsonRaw ( SavegameStore . FileType . PlayerStateSaveData ) is not { } saveDataWrapper )
238+ {
239+ return false ;
240+ }
241+
242+ if ( saveDataWrapper . GetJsonBasedToken ( Constants . JsonKeys . PlayerState ) is not { } playerState ||
243+ playerState [ "_entries" ] is not JArray entries )
244+ {
245+ return false ;
246+ }
247+
248+ var blueprintItemIds = new HashSet < int >
249+ {
250+ ItemPlatingItemId , PoweredCrossItemId
251+ } ;
252+
253+ var addedBlueprintItems = new HashSet < int > ( ) ;
254+
255+ var hasChanges = false ;
256+
257+ foreach ( var inventoryItem in
258+ inventoryItems . Where ( inventoryItem => blueprintItemIds . Contains ( inventoryItem . Id ) ) )
259+ {
260+ addedBlueprintItems . Add ( inventoryItem . Id ) ;
261+ }
262+
263+ var blueprintTokensExisting = new HashSet < int > ( ) ;
264+
265+ foreach ( var jToken in entries )
266+ {
267+ var name = jToken [ "Name" ] ? . Value < string > ( ) ;
268+
269+ foreach ( var itemId in blueprintItemIds . Where ( itemId => name == $ "DiscoverablePageUnlocked_{ itemId } ") )
270+ {
271+ blueprintTokensExisting . Add ( itemId ) ;
272+
273+ var isEnabled = jToken [ "BoolValue" ] ? . Value < bool > ( ) ?? false ;
274+ var isAdded = addedBlueprintItems . Contains ( itemId ) ;
275+ if ( isEnabled != isAdded )
276+ {
277+ jToken [ "BoolValue" ] = isAdded ;
278+ hasChanges = true ;
279+ }
280+
281+ break ;
282+ }
283+ }
284+
285+ foreach ( var blueprintItemId in blueprintItemIds )
286+ {
287+ if ( blueprintTokensExisting . Contains ( blueprintItemId ) || ! addedBlueprintItems . Contains ( blueprintItemId ) )
288+ {
289+ continue ;
290+ }
291+
292+ entries . Add ( new JObject
293+ {
294+ { "Name" , $ "DiscoverablePageUnlocked_{ blueprintItemId } " } ,
295+ { "BoolValue" , true }
296+ } ) ;
297+ hasChanges = true ;
298+ }
299+
300+ if ( hasChanges )
301+ {
302+ saveDataWrapper . MarkAsModified ( Constants . JsonKeys . PlayerState ) ;
303+ }
304+
305+ return hasChanges ;
229306 }
230307
231308 [ RelayCommand ( CanExecute = nameof ( HasInventoryItems ) ) ]
0 commit comments