-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerObjectSystem.cs
More file actions
120 lines (102 loc) · 3.71 KB
/
ServerObjectSystem.cs
File metadata and controls
120 lines (102 loc) · 3.71 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Concurrent;
using UnityEngine;
using Utils;
namespace 简单战斗.ServiceLocator
{
public static class CanGetService
{
public static T GetServiceObject<T>(this IGetService getService) where T : class
{
return (T)IService.Get(typeof(T));
}
}
public static class CanRegisterService
{
public static void RegisterService(this IRegisterService registerService, MonoBehaviour service)
{
// 获取该类实现的所有接口
var interfaces = service.GetType().GetInterfaces();
// 注册继承自IServiceBase的接口
foreach (var serviceInterface in interfaces)
{
//如果接口/非接口的实例已经存储
if (IService.HaveRegistered(serviceInterface) || IService.HaveRegistered(service.GetType())) return;
if (typeof(IServiceBase).IsAssignableFrom(serviceInterface) && serviceInterface != typeof(IServiceBase))
{
IService.Add(serviceInterface, service);
DebugSystem.Log($"Service {service.GetType().Name} registered as {serviceInterface.Name}","乐酌");
return;
}
}
if (IService.HaveRegistered(service.GetType())) return;
IService.Add(service.GetType(), service);
DebugSystem.Log($"Service {service.GetType().Name} registered as {service.GetType().Name}","乐酌");
}
}
public interface IGetService
{
}
public interface IRegisterService
{
}
public interface IServiceBase
{
}
public class ServiceObject<T> : MonoBehaviour, IRegisterService where T : class
{
private static T _instance;
public static T Instance => _instance;
protected virtual void Awake()
{
if (_instance == null)
_instance = this as T;
else
Destroy(this);
this.RegisterService(this);
}
protected virtual void OnDestroy()
{
if (_instance == this as T)
{
_instance = null;
IService.Remove(this.GetType(),this);
}
}
}
public interface IService
{
public static readonly ConcurrentDictionary<Type, object> ServiceDic = new ConcurrentDictionary<Type, object>();
public static void Add(Type service, object instance)
{
if (!ServiceDic.TryAdd(service, instance))
throw new Exception($"{service} already registered");
}
public static object Get(Type service)
{
if (ServiceDic.TryGetValue(service, out object instance))
return instance;
throw new NullReferenceException($"{service} is not registered");
}
public static void Remove(Type service,MonoBehaviour serviceMono)
{
if(ServiceDic.TryRemove(service,out object instance))return;
var interfaces = serviceMono.GetType().GetInterfaces();
foreach (var @serviceInterface in interfaces)
{
if (typeof(IServiceBase).IsAssignableFrom(serviceInterface) && serviceInterface != typeof(IServiceBase))
{
if(ServiceDic.TryRemove(serviceInterface,out object instance1))return;
}
}
throw new Exception($"{service} removed failed");
}
public static bool HaveRegistered(Type service)
{
return ServiceDic.ContainsKey(service);
}
}
public interface IServiceSystem : IGetService, IRegisterService, IService
{
}
}