-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingletonNetworkComponent.cs
More file actions
57 lines (51 loc) · 2.31 KB
/
SingletonNetworkComponent.cs
File metadata and controls
57 lines (51 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using MyceliumNetworking;
using System;
using UnityEngine;
using Steamworks;
namespace CWAPI
{
public abstract class SingletonNetworkComponent<THandler> : MonoBehaviour where THandler : SingletonNetworkComponent<THandler>
{
private ManualLogSource? _logger;
protected abstract BepInEx.Logging.ManualLogSource LogSource { get; }
protected ManualLogSource Logger => _logger ??= new(LogSource, GetType().Name);
protected abstract uint MOD_ID { get; }
public static SingletonNetworkComponent<THandler>? Instance { get; private set; }
public static THandler? TypedInstance => Instance as THandler;
protected virtual void Awake()
{
if (Instance != null)
{
Logger.LogWarning($"This singleton already exists. Destroying self");
Destroy(gameObject);
return;
}
transform.SetParent(null);
DontDestroyOnLoad(gameObject);
gameObject.hideFlags |= HideFlags.DontSave;
Instance = this;
MyceliumNetwork.RegisterNetworkObject(this, MOD_ID);
SuccessfulAwake();
}
protected virtual void SuccessfulAwake() { }
protected virtual void OnDestroy()
{
if (Instance == this)
{
Instance = null;
MyceliumNetwork.DeregisterNetworkObject(this, MOD_ID);
}
}
protected static bool Send(string methodName, ReliableType reliable, params object[] parameters)
{
MyceliumNetwork.RPC(Instance?.MOD_ID ?? throw new InvalidOperationException($"[{typeof(THandler).Name}] Not initialized. Cannot send"), methodName, reliable, parameters);
return true;
}
protected static bool SendTarget(string methodName, CSteamID target, ReliableType reliable, params object[] parameters)
{
MyceliumNetwork.RPCTarget(Instance?.MOD_ID ?? throw new InvalidOperationException($"[{typeof(THandler).Name}] Not initialized. Cannot send"), methodName, target, reliable, parameters);
return true;
}
protected static bool Send(string methodName, CSteamID target, ReliableType reliable, params object[] parameters) => SendTarget(methodName, target, reliable, parameters);
}
}