Skip to content

Commit 696013d

Browse files
authored
feat: add generic singleton Instance pattern for CustomItem and CustomRole (#775)
* generic singleton instance pattern * Update CustomItem{T}.cs * new for someone
1 parent a1f8bf2 commit 696013d

2 files changed

Lines changed: 219 additions & 0 deletions

File tree

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="CustomItem{T}.cs" company="ExMod Team">
3+
// Copyright (c) ExMod Team. All rights reserved.
4+
// Licensed under the CC BY-SA 3.0 license.
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
#pragma warning disable SA1402 // File may only contain a single type
8+
namespace Exiled.CustomItems.API.Features
9+
{
10+
using YamlDotNet.Serialization;
11+
12+
/// <summary>
13+
/// A generic base class for <see cref="CustomItem"/> that provides a typed singleton <see cref="Instance"/>.
14+
/// </summary>
15+
/// <typeparam name="T">The concrete <see cref="CustomItem"/> type.</typeparam>
16+
public abstract class CustomItem<T> : CustomItem
17+
where T : CustomItem<T>, new()
18+
{
19+
/// <summary>
20+
/// Gets the singleton instance of this <see cref="CustomItem"/>.
21+
/// </summary>
22+
[YamlIgnore]
23+
public static T? Instance { get; private set; }
24+
25+
/// <inheritdoc/>
26+
public override void Init()
27+
{
28+
base.Init();
29+
Instance = this as T;
30+
}
31+
32+
/// <inheritdoc/>
33+
public override void Destroy()
34+
{
35+
Instance = null;
36+
base.Destroy();
37+
}
38+
}
39+
40+
/// <summary>
41+
/// A generic base class for <see cref="CustomWeapon"/> that provides a typed singleton <see cref="Instance"/>.
42+
/// </summary>
43+
/// <typeparam name="T">The concrete <see cref="CustomWeapon"/> type.</typeparam>
44+
public abstract class CustomWeapon<T> : CustomWeapon
45+
where T : CustomWeapon<T>, new()
46+
{
47+
/// <summary>
48+
/// Gets the singleton instance of this <see cref="CustomWeapon"/>.
49+
/// </summary>
50+
[YamlIgnore]
51+
public static T? Instance { get; private set; }
52+
53+
/// <inheritdoc/>
54+
public override void Init()
55+
{
56+
base.Init();
57+
Instance = this as T;
58+
}
59+
60+
/// <inheritdoc/>
61+
public override void Destroy()
62+
{
63+
Instance = null;
64+
base.Destroy();
65+
}
66+
}
67+
68+
/// <summary>
69+
/// A generic base class for <see cref="CustomKeycard"/> that provides a typed singleton <see cref="Instance"/>.
70+
/// </summary>
71+
/// <typeparam name="T">The concrete <see cref="CustomKeycard"/> type.</typeparam>
72+
public abstract class CustomKeycard<T> : CustomKeycard
73+
where T : CustomKeycard<T>, new()
74+
{
75+
/// <summary>
76+
/// Gets the singleton instance of this <see cref="CustomKeycard"/>.
77+
/// </summary>
78+
[YamlIgnore]
79+
public static T? Instance { get; private set; }
80+
81+
/// <inheritdoc/>
82+
public override void Init()
83+
{
84+
base.Init();
85+
Instance = this as T;
86+
}
87+
88+
/// <inheritdoc/>
89+
public override void Destroy()
90+
{
91+
Instance = null;
92+
base.Destroy();
93+
}
94+
}
95+
96+
/// <summary>
97+
/// A generic base class for <see cref="CustomGrenade"/> that provides a typed singleton <see cref="Instance"/>.
98+
/// </summary>
99+
/// <typeparam name="T">The concrete <see cref="CustomGrenade"/> type.</typeparam>
100+
public abstract class CustomGrenade<T> : CustomGrenade
101+
where T : CustomGrenade<T>, new()
102+
{
103+
/// <summary>
104+
/// Gets the singleton instance of this <see cref="CustomGrenade"/>.
105+
/// </summary>
106+
[YamlIgnore]
107+
public static T? Instance { get; private set; }
108+
109+
/// <inheritdoc/>
110+
public override void Init()
111+
{
112+
base.Init();
113+
Instance = this as T;
114+
}
115+
116+
/// <inheritdoc/>
117+
public override void Destroy()
118+
{
119+
Instance = null;
120+
base.Destroy();
121+
}
122+
}
123+
124+
/// <summary>
125+
/// A generic base class for <see cref="CustomArmor"/> that provides a typed singleton <see cref="Instance"/>.
126+
/// </summary>
127+
/// <typeparam name="T">The concrete <see cref="CustomArmor"/> type.</typeparam>
128+
public abstract class CustomArmor<T> : CustomArmor
129+
where T : CustomArmor<T>, new()
130+
{
131+
/// <summary>
132+
/// Gets the singleton instance of this <see cref="CustomArmor"/>.
133+
/// </summary>
134+
[YamlIgnore]
135+
public static T? Instance { get; private set; }
136+
137+
/// <inheritdoc/>
138+
public override void Init()
139+
{
140+
base.Init();
141+
Instance = this as T;
142+
}
143+
144+
/// <inheritdoc/>
145+
public override void Destroy()
146+
{
147+
Instance = null;
148+
base.Destroy();
149+
}
150+
}
151+
152+
/// <summary>
153+
/// A generic base class for <see cref="CustomGoggles"/> that provides a typed singleton <see cref="Instance"/>.
154+
/// </summary>
155+
/// <typeparam name="T">The concrete <see cref="CustomGoggles"/> type.</typeparam>
156+
public abstract class CustomGoggles<T> : CustomGoggles
157+
where T : CustomGoggles<T>, new()
158+
{
159+
/// <summary>
160+
/// Gets the singleton instance of this <see cref="CustomGoggles"/>.
161+
/// </summary>
162+
[YamlIgnore]
163+
public static T? Instance { get; private set; }
164+
165+
/// <inheritdoc/>
166+
public override void Init()
167+
{
168+
base.Init();
169+
Instance = this as T;
170+
}
171+
172+
/// <inheritdoc/>
173+
public override void Destroy()
174+
{
175+
Instance = null;
176+
base.Destroy();
177+
}
178+
}
179+
}
180+
#pragma warning restore SA1402 // File may only contain a single type
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="CustomRole{T}.cs" company="ExMod Team">
3+
// Copyright (c) ExMod Team. All rights reserved.
4+
// Licensed under the CC BY-SA 3.0 license.
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
8+
namespace Exiled.CustomRoles.API.Features
9+
{
10+
using YamlDotNet.Serialization;
11+
12+
/// <summary>
13+
/// A generic base class for <see cref="CustomRole"/> that provides a typed singleton <see cref="Instance"/>.
14+
/// </summary>
15+
/// <typeparam name="T">The concrete <see cref="CustomRole"/> type.</typeparam>
16+
public abstract class CustomRole<T> : CustomRole
17+
where T : CustomRole<T>, new()
18+
{
19+
/// <summary>
20+
/// Gets the singleton instance of this <see cref="CustomRole"/>.
21+
/// </summary>
22+
[YamlIgnore]
23+
public static T? Instance { get; private set; }
24+
25+
/// <inheritdoc/>
26+
public override void Init()
27+
{
28+
base.Init();
29+
Instance = this as T;
30+
}
31+
32+
/// <inheritdoc/>
33+
public override void Destroy()
34+
{
35+
Instance = null;
36+
base.Destroy();
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)