|
| 1 | +using System; |
| 2 | +using System.Windows; |
| 3 | + |
| 4 | +using SatisfactorySaveEditor.Model; |
| 5 | +using SatisfactorySaveEditor.ViewModel.Property; |
| 6 | +using SatisfactorySaveEditor.ViewModel.Struct; |
| 7 | + |
| 8 | +using SatisfactorySaveParser; |
| 9 | +using SatisfactorySaveParser.PropertyTypes; |
| 10 | + |
| 11 | +using Vector = SatisfactorySaveParser.PropertyTypes.Structs.Vector; |
| 12 | +using Vector3 = SatisfactorySaveParser.Structures.Vector3; |
| 13 | + |
| 14 | +namespace SatisfactorySaveEditor.Cheats |
| 15 | +{ |
| 16 | + public class UndoDeleteEnemiesCheat : ICheat |
| 17 | + { |
| 18 | + public string Name => "Undo Delete enemies"; |
| 19 | + |
| 20 | + private SaveObjectModel FindOrCreatePath(SaveObjectModel start, string[] path, int index = 0) |
| 21 | + { |
| 22 | + if (index == path.Length) |
| 23 | + return start; |
| 24 | + if (start.FindChild(path[index], false) == null) |
| 25 | + start.Items.Add(new SaveObjectModel(path[index])); |
| 26 | + return FindOrCreatePath(start.FindChild(path[index], false), path, index + 1); |
| 27 | + } |
| 28 | + |
| 29 | + public bool Apply(SaveObjectModel rootItem, SatisfactorySave save) |
| 30 | + { |
| 31 | + var animalSpawners = rootItem.FindChild("BP_CreatureSpawner.BP_CreatureSpawner_C", false); |
| 32 | + if (animalSpawners == null) |
| 33 | + { |
| 34 | + MessageBox.Show("This save does not contain animals or it is corrupt.", "Cannot find animals", MessageBoxButton.OK, MessageBoxImage.Error); |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + float offset = -50000; |
| 39 | + var hostPlayerModel = rootItem.FindChild("Char_Player.Char_Player_C", false); |
| 40 | + if (hostPlayerModel == null || hostPlayerModel.Items.Count < 1) |
| 41 | + { |
| 42 | + MessageBox.Show("This save does not contain a host player or it is corrupt.", "Cannot find host player", MessageBoxButton.OK, MessageBoxImage.Error); |
| 43 | + return false; |
| 44 | + } |
| 45 | + Vector3 playerPosition = ((SaveEntityModel)hostPlayerModel.Items[0]).Position; |
| 46 | + |
| 47 | + foreach (SaveObjectModel animalSpawner in animalSpawners.DescendantSelfViewModel) |
| 48 | + { |
| 49 | + var probablyEdited = ((SaveEntityModel)animalSpawner).Position.Z < -14200f; |
| 50 | + |
| 51 | + // Some crab hatchers are marked as CreatureSpawner instead of EnemySpawner and there is no other trace of the difference between enemy and friendly in the savefile |
| 52 | + //if (animalSpawner.Title.ToLower().Contains("enemy")) |
| 53 | + //{ |
| 54 | + if (probablyEdited) |
| 55 | + ((SaveEntityModel)animalSpawner).Position.Z -= offset; // Move the spawn under the map |
| 56 | + |
| 57 | + animalSpawner.FindField("mSpawnData", (ArrayPropertyViewModel arrayProperty) => |
| 58 | + { |
| 59 | + foreach (StructPropertyViewModel elem in arrayProperty.Elements) |
| 60 | + { |
| 61 | + if (probablyEdited) |
| 62 | + ((Vector)((StructProperty)((DynamicStructDataViewModel)elem.StructData).Fields[0].Model).Data).Data.Z -= offset; // Move the spawn point under the map |
| 63 | + |
| 64 | + // Set WasKilled to true so they don't respawn after deleting them |
| 65 | + ((BoolPropertyViewModel)((DynamicStructDataViewModel)elem.StructData).Fields[2]).Value = false; |
| 66 | + // Set KilledOnDayNumber to a huge number (some far away animals respawn if the number is too small) |
| 67 | + ((IntPropertyViewModel)((DynamicStructDataViewModel)elem.StructData).Fields[3]).Value = (int)0; |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments