Skip to content

Commit 08d457b

Browse files
committed
1.2.1.0
Fix of initializations.
1 parent 7084c9b commit 08d457b

File tree

7 files changed

+91
-25
lines changed

7 files changed

+91
-25
lines changed

SZones.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<TargetFrameworks>net472</TargetFrameworks>
66
<AssemblyName>SZones</AssemblyName>
77
<RootNamespace>SZones</RootNamespace>
8-
<Version>1.2.0.1-beta</Version>
8+
<Version>1.2.1.0</Version>
99
<NoWarn>$(NoWarn);CS0436</NoWarn>
1010
<RunPostBuildEvent>Always</RunPostBuildEvent>
1111
<Nullable>annotations</Nullable>

Utilities/Utils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public static partial class Utils
5959
{
6060
// just shorthands, you can simply remove/rename them if you want to.
6161
public static ZoneManager inst => ZoneManager.Instance;
62-
public static Config conf => ZoneManager.Instance.Configuration.Instance;
62+
static Config config;
63+
public static Config conf => config ??= ZoneManager.Instance?.Configuration?.Instance;
6364
//
6465

6566
public static T FindByName<T>(this IEnumerable<T> enumerable, Func<T, string> nameGetter, string name, bool fullName = false)

ZoneManager.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ public static bool Create(Zone zone)
88
{
99
if (zone is null || Get(zone.Name) is not null) return false;
1010
Zones.Add(zone);
11-
zone.Initialize();
1211
Save();
12+
SafeInitialize(zone);
1313
return true;
1414
}
1515
public static void Delete(Zone zone)
1616
{
1717
if (zone is null) return;
18-
zone.Dispose();
18+
SafeFinalize(zone);
1919
Zones.Remove(zone);
2020
Save();
2121
}
@@ -25,15 +25,32 @@ public static void Save()
2525
lock (config) config.Save();
2626
}
2727

28+
public static void SafeFinalize(Zone zone)
29+
{
30+
try
31+
{
32+
zone.Finalize();
33+
}
34+
catch (Exception ex) { Logger.Log(ex); }
35+
}
36+
public static void SafeInitialize(Zone zone)
37+
{
38+
try
39+
{
40+
zone.Initialize();
41+
}
42+
catch (Exception ex) { Logger.Log(ex); }
43+
}
44+
2845
protected override void Unload()
2946
{
3047
foreach (var zone in Zones)
31-
zone.Dispose();
48+
SafeFinalize(zone);
3249
}
3350
protected override void Load()
3451
{
3552
foreach (var zone in Zones)
36-
zone.Initialize();
53+
SafeInitialize(zone);
3754
}
3855

3956
public static Zone Get(string name) => conf.Zones.FindByName(x => x.Name, name);

Zones/BoxColliderZone.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ public override SVector3 Position
77
{
88
get
99
{
10-
if(Controller is null) return base.Position;
10+
if(!controller) return base.Position;
1111
var center = Controller.Collider.center;
1212
center.y = Controller.Collider.bounds.min.y;
1313
return center;
1414
}
1515
set
1616
{
1717
base.Position = value;
18-
if (Controller is null) return;
18+
if (!controller) return;
1919
Controller.Collider.center = Position;
2020
}
2121
}

Zones/CuboidZone.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public virtual SVector3 Size
1313
set
1414
{
1515
size = value;
16-
if (Controller is null) return;
16+
if (!controller) return;
1717
Controller.Collider.size = size * SizeModifier;
1818
}
1919
}

Zones/SpheroidZone.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public virtual float Radius
1313
set
1414
{
1515
radius = value;
16-
if (Controller is null) return;
16+
if (!controller) return;
1717
Controller.Collider.radius = radius * RadiusModifier;
1818
}
1919
}

Zones/Zone.cs

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ public virtual SVector3 Position
1616
set
1717
{
1818
position = value;
19-
if (Object is null) return;
19+
if (!Object) return;
2020
Object.transform.position = value;
2121
}
2222
}
2323
public virtual bool ShouldSerializePosition() => true;
2424

25+
protected ZoneController controller;
2526
[XmlIgnore, JsonIgnore]
26-
public virtual ZoneController Controller { get; protected set; }
27+
public virtual ZoneController Controller
28+
{
29+
get
30+
{
31+
TryReinitialize();
32+
return controller;
33+
}
34+
protected set => controller = value;
35+
}
2736

2837
protected GameObject Object;
2938
private static GameObject _prefab;
@@ -33,37 +42,76 @@ internal virtual void Initialize()
3342
{
3443
if (Object) return;
3544
UnityObject.DontDestroyOnLoad(Object = UnityObject.Instantiate(Prefab));
45+
3646
foreach(var body in Object.GetComponents<Rigidbody>())
3747
UnityObject.Destroy(body);
48+
49+
InitializeController();
50+
3851
Position = position; // update position
3952
}
40-
internal virtual void Dispose()
53+
54+
internal virtual void InitializeController() { }
55+
56+
internal virtual void Finalize()
57+
{
58+
FinalizeController();
59+
UnityObject.Destroy(Object);
60+
}
61+
62+
internal virtual void FinalizeController()
4163
{
42-
if (Controller) UnityObject.Destroy(Controller);
43-
if (Object) UnityObject.Destroy(Object);
64+
UnityObject.Destroy(controller);
4465
}
4566

67+
public virtual void Reinitialize()
68+
{
69+
Finalize();
70+
Initialize();
71+
}
72+
73+
public virtual void ReinitializeController()
74+
{
75+
FinalizeController();
76+
InitializeController();
77+
}
78+
79+
public void TryReinitialize()
80+
{
81+
if (!Object) Reinitialize();
82+
else if (!controller) ReinitializeController();
83+
}
84+
85+
public virtual bool IsValid() => Object && controller;
86+
4687
public override string ToString() => ToJsonString(this, true, new ValueTypeToStringJsonConverter());
4788
}
4889
public abstract class Zone<TController> : Zone
4990
where TController : ZoneController
5091
{
5192
[XmlIgnore, JsonIgnore]
52-
public virtual new TController Controller { get => (TController)base.Controller; private set => base.Controller = value; }
93+
public virtual new TController Controller
94+
{
95+
get => (TController)base.Controller;
96+
private set => base.Controller = value;
97+
}
5398

54-
internal override void Initialize()
99+
internal override void InitializeController()
55100
{
56-
base.Initialize();
57-
(Controller = Object.GetOrAddComponent<TController>()).Initialize(this);
58-
Controller.OnPlayerEnter += Debug_PlayerEnterHandler;
59-
Controller.OnPlayerExit += Debug_PlayerExitHandler;
101+
base.InitializeController();
102+
if (controller) return;
103+
(controller = Object.GetOrAddComponent<TController>()).Initialize(this);
104+
controller.OnPlayerEnter += Debug_PlayerEnterHandler;
105+
controller.OnPlayerExit += Debug_PlayerExitHandler;
60106
}
61-
internal override void Dispose()
107+
108+
internal override void FinalizeController()
62109
{
63-
Controller.OnPlayerEnter -= Debug_PlayerEnterHandler;
64-
Controller.OnPlayerExit -= Debug_PlayerExitHandler;
65-
base.Dispose();
110+
if (!controller) return;
111+
controller.OnPlayerEnter -= Debug_PlayerEnterHandler;
112+
controller.OnPlayerExit -= Debug_PlayerExitHandler;
66113
}
114+
67115
private void Debug_PlayerEnterHandler(Player player)
68116
{
69117
if (!conf.DebugInformation) return;

0 commit comments

Comments
 (0)