From ba80c7d17360068e2362049117064dc7c333692f Mon Sep 17 00:00:00 2001
From: MaxED <j.maxed@gmail.com>
Date: Wed, 6 Jul 2016 00:15:19 +0000
Subject: [PATCH] Fixed several cases when sector/linedef tag changes were
 incorrectly recorded by undo system.

---
 Source/Core/Controls/TagsSelector.cs          |  3 +--
 Source/Core/Map/MapSet.cs                     | 24 +++++++++++--------
 .../FindReplace/FindLinedefTags.cs            |  6 +++--
 .../FindReplace/FindSectorTags.cs             |  6 +++--
 4 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/Source/Core/Controls/TagsSelector.cs b/Source/Core/Controls/TagsSelector.cs
index b30f7e37b..412a7d6b7 100644
--- a/Source/Core/Controls/TagsSelector.cs
+++ b/Source/Core/Controls/TagsSelector.cs
@@ -209,8 +209,7 @@ namespace CodeImp.DoomBuilder.Controls
 				if(newtags.Count == 0) newtags.Add(0);
 
 				// Apply it
-				me.Tags.Clear();
-				me.Tags.AddRange(newtags);
+				me.Tags = new List<int>(newtags);
 
 				// We are making progress...
 				offset++;
diff --git a/Source/Core/Map/MapSet.cs b/Source/Core/Map/MapSet.cs
index 8deb24fcf..f37f997bb 100644
--- a/Source/Core/Map/MapSet.cs
+++ b/Source/Core/Map/MapSet.cs
@@ -3543,18 +3543,20 @@ namespace CodeImp.DoomBuilder.Map
 				{
 					//mxd. Multiple tags support...
 					bool changed = false;
-					for(int i = 0; i < s.Tags.Count; i++)
+					// Make a copy of tags, otherwise BeforePropsChange will be triggered after tag changes
+					List<int> tags = new List<int>(s.Tags);
+					for(int i = 0; i < tags.Count; i++)
 					{
-						int tag = s.Tags[i];
+						int tag = tags[i];
 						handler(s, false, UniversalType.SectorTag, ref tag, obj);
-						if(tag != s.Tags[i])
+						if(tag != tags[i])
 						{
-							s.Tags[i] = tag;
+							tags[i] = tag;
 							changed = true;
 						}
 					}
 
-					if(changed) s.Tags = s.Tags.Distinct().ToList();
+					if(changed) s.Tags = tags.Distinct().ToList();
 				}
 			}
 
@@ -3602,18 +3604,20 @@ namespace CodeImp.DoomBuilder.Map
 					{
 						//mxd. Multiple tags support...
 						bool changed = false;
-						for(int i = 0; i < l.Tags.Count; i++)
+						// Make a copy of tags, otherwise BeforePropsChange will be triggered after tag changes
+						List<int> tags = new List<int>(l.Tags);
+						for(int i = 0; i < tags.Count; i++)
 						{
-							int tag = l.Tags[i];
+							int tag = tags[i];
 							handler(l, false, UniversalType.LinedefTag, ref tag, obj);
-							if(tag != l.Tags[i])
+							if(tag != tags[i])
 							{
-								l.Tags[i] = tag;
+								tags[i] = tag;
 								changed = true;
 							}
 						}
 
-						if(changed) l.Tags = l.Tags.Distinct().ToList();
+						if(changed) l.Tags = tags.Distinct().ToList();
 					}
 				}
 			}
diff --git a/Source/Plugins/BuilderModes/FindReplace/FindLinedefTags.cs b/Source/Plugins/BuilderModes/FindReplace/FindLinedefTags.cs
index 8215a3b34..fd02a6dbe 100644
--- a/Source/Plugins/BuilderModes/FindReplace/FindLinedefTags.cs
+++ b/Source/Plugins/BuilderModes/FindReplace/FindLinedefTags.cs
@@ -92,8 +92,10 @@ namespace CodeImp.DoomBuilder.BuilderModes
 						// Replace
 						if(replace)
 						{
-							l.Tags[index] = replacetag; //mxd
-							l.Tags = l.Tags.Distinct().ToList(); //mxd. We don't want duplicates
+							//mxd. Make a copy of tags, otherwise BeforePropsChange will be triggered after tag changes
+							List<int> tags = new List<int>(l.Tags);
+							tags[index] = replacetag;
+							l.Tags = tags.Distinct().ToList(); // We don't want duplicates
 						}
 
 						// Add to list
diff --git a/Source/Plugins/BuilderModes/FindReplace/FindSectorTags.cs b/Source/Plugins/BuilderModes/FindReplace/FindSectorTags.cs
index add09a8e1..f8932928b 100644
--- a/Source/Plugins/BuilderModes/FindReplace/FindSectorTags.cs
+++ b/Source/Plugins/BuilderModes/FindReplace/FindSectorTags.cs
@@ -86,8 +86,10 @@ namespace CodeImp.DoomBuilder.BuilderModes
 						// Replace
 						if(replace)
 						{
-							s.Tags[index] = replacetag; //mxd
-							s.Tags = s.Tags.Distinct().ToList(); //mxd. We don't want duplicates
+							//mxd. Make a copy of tags, otherwise BeforePropsChange will be triggered after tag changes
+							List<int> tags = new List<int>(s.Tags);
+							tags[index] = replacetag;
+							s.Tags = tags.Distinct().ToList(); // We don't want duplicates
 						}
 
 						// Add to list
-- 
GitLab