Skip to content

Commit 0bf9fde

Browse files
committed
bugfix: added locks on external methods
1 parent d4b6f61 commit 0bf9fde

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

Penumbra/Collections/Manager/CollectionStorage.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,35 @@ private LocalCollectionId AllocateNextId ()
9494

9595
/// <summary> Default enumeration skips the empty collection. </summary>
9696
public IEnumerator<ModCollection> GetEnumerator()
97-
=> _collections.Skip(1).GetEnumerator();
97+
=> GetModSnapShot().ToList().GetEnumerator();
9898

9999
IEnumerator IEnumerable.GetEnumerator()
100100
=> GetEnumerator();
101101

102102
public int Count
103-
=> _collections.Count;
103+
{
104+
get
105+
{
106+
lock(_collectionsLock)
107+
return _collections.Count;
108+
}
109+
}
104110

105111
public ModCollection this[int index]
106-
=> _collections[index];
112+
{
113+
get
114+
{
115+
lock(_collectionsLock)
116+
return _collections[index];
117+
}
118+
}
107119

108120
/// <summary> Find a collection by its name. If the name is empty or None, the empty collection is returned. </summary>
109121
public bool ByName(string name, [NotNullWhen(true)] out ModCollection? collection)
110122
{
111123
if (name.Length != 0)
112-
return _collections.FindFirst(c => string.Equals(c.Identity.Name, name, StringComparison.OrdinalIgnoreCase), out collection);
113-
124+
lock(_collectionsLock)
125+
return _collections.FindFirst(c => string.Equals(c.Identity.Name, name, StringComparison.OrdinalIgnoreCase), out collection);
114126
collection = ModCollection.Empty;
115127
return true;
116128
}
@@ -119,8 +131,8 @@ public bool ByName(string name, [NotNullWhen(true)] out ModCollection? collectio
119131
public bool ById(Guid id, [NotNullWhen(true)] out ModCollection? collection)
120132
{
121133
if (id != Guid.Empty)
122-
return _collections.FindFirst(c => c.Identity.Id == id, out collection);
123-
134+
lock(_collectionsLock)
135+
return _collections.FindFirst(c => c.Identity.Id == id, out collection);
124136
collection = ModCollection.Empty;
125137
return true;
126138
}
@@ -189,7 +201,7 @@ public bool AddCollection(string name, ModCollection? duplicate)
189201
/// </summary>
190202
public bool RemoveCollection(ModCollection collection)
191203
{
192-
if (collection.Identity.Index <= ModCollection.Empty.Identity.Index || collection.Identity.Index >= _collections.Count)
204+
if (collection.Identity.Index <= ModCollection.Empty.Identity.Index || collection.Identity.Index >= Count)
193205
{
194206
Penumbra.Messager.NotificationMessage("Can not remove the empty collection.", NotificationType.Error, false);
195207
return false;
@@ -203,11 +215,11 @@ public bool RemoveCollection(ModCollection collection)
203215

204216
Delete(collection);
205217
_saveService.ImmediateDelete(new ModCollectionSave(_modStorage, collection));
206-
lock (_collectionsLock)
218+
lock (_collectionsLock)
207219
{
208220
_collections.RemoveAt(collection.Identity.Index);
209221
// Update indices.
210-
for (var i = collection.Identity.Index; i < Count; ++i)
222+
for (var i = collection.Identity.Index; i < _collections.Count; ++i)
211223
_collections[i].Identity.Index = i;
212224
}
213225

@@ -322,12 +334,12 @@ private ModCollection SetDefaultNamedCollection()
322334
return collection;
323335

324336
if (AddCollection(ModCollectionIdentity.DefaultCollectionName, null))
325-
return _collections[^1];
337+
return this[^1];
326338

327339
Penumbra.Messager.NotificationMessage(
328340
$"Unknown problem creating a collection with the name {ModCollectionIdentity.DefaultCollectionName}, which is required to exist.",
329341
NotificationType.Error);
330-
return Count > 1 ? _collections[1] : _collections[0];
342+
return Count > 1 ? this[1] : this[0];
331343
}
332344

333345
/// <summary> Move all settings in all collections to unused settings. </summary>

0 commit comments

Comments
 (0)