diff --git a/src/HexManiac.Core/Core/Autos/StubDataModel.cs b/src/HexManiac.Core/Core/Autos/StubDataModel.cs index 4b74d27f..b4484c07 100644 --- a/src/HexManiac.Core/Core/Autos/StubDataModel.cs +++ b/src/HexManiac.Core/Core/Autos/StubDataModel.cs @@ -32,6 +32,8 @@ namespace HavenSoft.HexManiac.Core.Models } } + public int ReferenceCount { get; set; } + public bool SpartanMode { get; set; } public delegate System.Collections.Generic.IEnumerable AllDelegate_() where T : Runs.IFormattedRun; diff --git a/src/HexManiac.Core/Models/IDataModel.cs b/src/HexManiac.Core/Models/IDataModel.cs index 3241156c..29e18036 100644 --- a/src/HexManiac.Core/Models/IDataModel.cs +++ b/src/HexManiac.Core/Models/IDataModel.cs @@ -20,6 +20,7 @@ namespace HavenSoft.HexManiac.Core.Models { byte[] RawData { get; } ModelCacheScope CurrentCacheScope { get; } + int ReferenceCount { get; set; } bool SpartanMode { get; set; } bool HasChanged(int index); int ChangeCount { get; } @@ -148,6 +149,8 @@ namespace HavenSoft.HexManiac.Core.Models { } } + public int ReferenceCount { get; set; } = 0; // used for tracking how many ViewModels currently use this Model + public BaseModel(byte[] data) { RawData = data; var code = data.GetGameCode(); diff --git a/src/HexManiac.Core/ViewModels/ChildViewPort.cs b/src/HexManiac.Core/ViewModels/ChildViewPort.cs index a0f25618..28030fde 100644 --- a/src/HexManiac.Core/ViewModels/ChildViewPort.cs +++ b/src/HexManiac.Core/ViewModels/ChildViewPort.cs @@ -22,6 +22,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { ScrollRegion.DeferHeader(); Parent = viewPort; Width = Parent.Width; + viewPort.Model.ReferenceCount -= 1; // don't reference count child viewports } public void RefreshHeaders() => ScrollRegion.UpdateDeferedHeader(); diff --git a/src/HexManiac.Core/ViewModels/ViewPort.cs b/src/HexManiac.Core/ViewModels/ViewPort.cs index 9993195b..25a97ea8 100644 --- a/src/HexManiac.Core/ViewModels/ViewPort.cs +++ b/src/HexManiac.Core/ViewModels/ViewPort.cs @@ -676,7 +676,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { } private void CloseExecuted(IFileSystem fileSystem) { - if (!history.IsSaved && ownsHistory) { + if (!history.IsSaved && Model.ReferenceCount == 1) { var metadata = Model.ExportMetadata(RefTable, Singletons.MetadataInfo); var result = fileSystem.TrySavePrompt(new LoadedFile(FileName, Model.RawData)); if (result == null) return; @@ -685,6 +685,8 @@ namespace HavenSoft.HexManiac.Core.ViewModels { history.TagAsSaved(); } } + Model.ReferenceCount -= 1; + Closed.Raise(this); } @@ -1041,6 +1043,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { } PythonTool = pythonTool; ownsHistory = changeHistory == null; + model.ReferenceCount += 1; history = changeHistory ?? new ChangeHistory(RevertChanges); history.PropertyChanged += HistoryPropertyChanged;