Skip to content

Commit

Permalink
去掉ISingletonLoad接口,需要Reload的Singleton全部由CodeTypes的CreateCodeSingleton创建
Browse files Browse the repository at this point in the history
  • Loading branch information
egametang committed Jul 16, 2023
1 parent deaf16c commit eed4d81
Show file tree
Hide file tree
Showing 27 changed files with 90 additions and 140 deletions.
12 changes: 6 additions & 6 deletions DotNet/Loader/CodeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ public void Awake()
break;
}
}
Assembly hotfixAssembly = this.LoadHotfix();

World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly });
Assembly hotfixAssembly = this.LoadHotfix();

World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly }, true);

IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
start.Run();
}
Expand All @@ -46,10 +47,9 @@ public void Reload()
{
Assembly hotfixAssembly = this.LoadHotfix();

World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly }, true);

World.Instance.Load();

CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly }, true);

codeTypes.CreateCodeSingleton();
Log.Debug($"reload dll finish!");
}
}
Expand Down
1 change: 1 addition & 0 deletions DotNet/Loader/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void Start()
.WithParsed((o)=>World.Instance.AddSingleton(o));
World.Instance.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
ETTask.ExceptionHandler += Log.Error;

World.Instance.AddSingleton<CodeLoader>();
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion Unity/Assets/Resources/GlobalConfig.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 36527db572638af47b03c805671cba75, type: 3}
m_Name: GlobalConfig
m_EditorClassIdentifier:
CodeMode: 1
CodeMode: 3
BuildType: 1
AppType: 7
8 changes: 2 additions & 6 deletions Unity/Assets/Scripts/Core/Entity/EntitySystemSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace ET
{
public class EntitySystemSingleton: Singleton<EntitySystemSingleton>, ISingletonAwake, ISingletonLoad
[Code]
public class EntitySystemSingleton: Singleton<EntitySystemSingleton>, ISingletonAwake
{
public TypeSystems TypeSystems { get; private set; }

Expand All @@ -28,11 +29,6 @@ public void Awake()
}
}

public void Load()
{
World.Instance.AddSingleton<EntitySystemSingleton>(true);
}

public void Serialize(Entity component)
{
if (component is not ISerialize)
Expand Down
7 changes: 0 additions & 7 deletions Unity/Assets/Scripts/Core/World/ISingletonLoad.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Unity/Assets/Scripts/Core/World/ISingletonLoad.cs.meta

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public ActorMessageDispatcherInfo(SceneType sceneType, IMActorHandler imActorHan
/// <summary>
/// Actor消息分发组件
/// </summary>
public class ActorMessageDispatcherComponent: Singleton<ActorMessageDispatcherComponent>, ISingletonAwake, ISingletonLoad
[Code]
public class ActorMessageDispatcherComponent: Singleton<ActorMessageDispatcherComponent>, ISingletonAwake
{
private readonly Dictionary<Type, List<ActorMessageDispatcherInfo>> ActorMessageHandlers = new();

Expand All @@ -40,11 +41,6 @@ public void Awake()
}
}

public void Load()
{
World.Instance.AddSingleton<ActorMessageDispatcherComponent>(true);
}

private void Register(Type type)
{
object obj = Activator.CreateInstance(type);
Expand Down
7 changes: 7 additions & 0 deletions Unity/Assets/Scripts/Core/World/Module/Code/CodeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ET
{
public class CodeAttribute: BaseAttribute
{

}
}
11 changes: 11 additions & 0 deletions Unity/Assets/Scripts/Core/World/Module/Code/CodeAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Unity/Assets/Scripts/Core/World/Module/Code/CodeTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,16 @@ public Type GetType(string typeName)
{
return this.allTypes[typeName];
}

public void CreateCodeSingleton()
{
var hashSet = this.GetTypes(typeof (CodeAttribute));
foreach (Type type in hashSet)
{
object obj = Activator.CreateInstance(type);
((ISingletonAwake)obj).Awake();
World.Instance.AddSingleton((ASingleton)obj, true);
}
}
}
}
4 changes: 2 additions & 2 deletions Unity/Assets/Scripts/Core/World/Module/Config/ConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void Reload(Type configType)
ASingleton singleton = category as ASingleton;
this.allConfig[configType] = singleton;

singleton.Register();
World.Instance.AddSingleton(singleton, true);
}

public void Load()
Expand Down Expand Up @@ -75,7 +75,7 @@ private void LoadOneInThread(Type configType, byte[] oneConfigBytes)
ASingleton singleton = category as ASingleton;
this.allConfig[configType] = singleton;

singleton.Register();
World.Instance.AddSingleton(singleton, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace ET
{
public class EventSystem: Singleton<EventSystem>, ISingletonAwake, ISingletonLoad
[Code]
public class EventSystem: Singleton<EventSystem>, ISingletonAwake
{
private class EventInfo
{
Expand Down Expand Up @@ -82,11 +83,6 @@ public void Awake()
}
}

public void Load()
{
World.Instance.AddSingleton<EventSystem>(true);
}

public async ETTask PublishAsync<S, T>(S scene, T a) where S: class, IScene where T : struct
{
List<EventInfo> iEvents;
Expand Down
4 changes: 2 additions & 2 deletions Unity/Assets/Scripts/Core/World/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public abstract class ASingleton: DisposeObject
{
public abstract void Register();
internal abstract void Register();
}

public abstract class Singleton<T>: ASingleton where T: Singleton<T>, new()
Expand All @@ -24,7 +24,7 @@ private set
}
}

public override void Register()
internal override void Register()
{
Instance = (T)this;
}
Expand Down
38 changes: 10 additions & 28 deletions Unity/Assets/Scripts/Core/World/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,48 @@ public void Dispose()
}
}

public T AddSingleton<T>(bool replace = false) where T : ASingleton, ISingletonAwake, new()
public T AddSingleton<T>(bool noStack = false) where T : ASingleton, ISingletonAwake, new()
{
T singleton = new();
singleton.Awake();

AddSingleton(singleton, replace);
AddSingleton(singleton, noStack);
return singleton;
}

public T AddSingleton<T, A>(A a, bool replace = false) where T : ASingleton, ISingletonAwake<A>, new()
public T AddSingleton<T, A>(A a, bool noStack = false) where T : ASingleton, ISingletonAwake<A>, new()
{
T singleton = new();
singleton.Awake(a);

AddSingleton(singleton, replace);
AddSingleton(singleton, noStack);
return singleton;
}

public T AddSingleton<T, A, B>(A a, B b, bool replace = false) where T : ASingleton, ISingletonAwake<A, B>, new()
public T AddSingleton<T, A, B>(A a, B b, bool noStack = false) where T : ASingleton, ISingletonAwake<A, B>, new()
{
T singleton = new();
singleton.Awake(a, b);

AddSingleton(singleton, replace);
AddSingleton(singleton, noStack);
return singleton;
}

public T AddSingleton<T, A, B, C>(A a, B b, C c, bool replace = false) where T : ASingleton, ISingletonAwake<A, B, C>, new()
public T AddSingleton<T, A, B, C>(A a, B b, C c, bool noStack = false) where T : ASingleton, ISingletonAwake<A, B, C>, new()
{
T singleton = new();
singleton.Awake(a, b, c);

AddSingleton(singleton, replace);
AddSingleton(singleton, noStack);
return singleton;
}

public void AddSingleton(ASingleton singleton, bool replace = false)
public void AddSingleton(ASingleton singleton, bool noStack = false)
{
lock (this)
{
Type type = singleton.GetType();
if (!replace)
if (!noStack)
{
this.stack.Push(type);
}
Expand All @@ -87,23 +87,5 @@ public void AddSingleton(ASingleton singleton, bool replace = false)

singleton.Register();
}

public void Load()
{
lock (this)
{
foreach (Type type in this.stack)
{
ASingleton singleton = this.singletons[type];

if (singleton is not ISingletonLoad iSingletonLoad)
{
continue;
}

iSingletonLoad.Load();
}
}
}
}
}
4 changes: 1 addition & 3 deletions Unity/Assets/Scripts/Editor/Helper/EditorLogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ private static void CreateLog()
return;
}

Logger log = new();
log.Register();
log.ILog = new UnityLogger();
World.Instance.AddSingleton<Logger>().ILog = new UnityLogger();
}

private static void DestroyLog()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ public class EntryEvent2_InitServer: AEvent<Scene, ET.EventType.EntryEvent2>
{
protected override async ETTask Run(Scene root, ET.EventType.EntryEvent2 args)
{
World.Instance.AddSingleton<HttpDispatcher>();
World.Instance.AddSingleton<ConsoleDispatcher>();

switch (Options.Instance.AppType)
{
case AppType.Server:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class EntryEvent3_InitClient: AEvent<Scene, EventType.EntryEvent3>
{
protected override async ETTask Run(Scene root, EventType.EntryEvent3 args)
{
World.Instance.AddSingleton<UIEventComponent>();

root.AddComponent<TimerComponent>();
root.AddComponent<CoroutineLockComponent>();

Expand Down
25 changes: 12 additions & 13 deletions Unity/Assets/Scripts/Loader/CodeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public void Start()

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies);

foreach (Assembly ass in assemblies)
{
string name = ass.GetName().Name;
Expand All @@ -37,6 +35,8 @@ public void Start()
this.assembly = ass;
}
}

World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies);
}
else
{
Expand Down Expand Up @@ -64,16 +64,13 @@ public void Start()
}

this.assembly = Assembly.Load(assBytes, pdbBytes);
}

{
Assembly hotfixAssembly = this.LoadHotfix();

World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});

IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
start.Run();
World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);
}

IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
start.Run();
}

private Assembly LoadHotfix()
Expand Down Expand Up @@ -105,11 +102,13 @@ public void Reload()
{
Assembly hotfixAssembly = this.LoadHotfix();

World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);
World.Instance.Load();
CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);

codeTypes.CreateCodeSingleton();

Log.Debug($"reload dll finish!");
}


}
}
1 change: 1 addition & 0 deletions Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private void Start()

World.Instance.AddSingleton<Logger>().ILog = new UnityLogger();
ETTask.ExceptionHandler += Log.Error;

World.Instance.AddSingleton<CodeLoader>().Start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@

namespace ET.Server
{
public class HttpDispatcher: Singleton<HttpDispatcher>, ISingletonAwake, ISingletonLoad
[Code]
public class HttpDispatcher: Singleton<HttpDispatcher>, ISingletonAwake
{
private readonly Dictionary<string, Dictionary<int, IHttpHandler>> dispatcher = new();

public void Load()
{
World.Instance.AddSingleton<HttpDispatcher>(true);
}

public void Awake()
{
HashSet<Type> types = CodeTypes.Instance.GetTypes(typeof (HttpHandlerAttribute));
Expand Down
Loading

0 comments on commit eed4d81

Please sign in to comment.