提交 b09edc2e 编写于 作者: T tanghai

ConfigComponent加载protobuf配置

上级 fff1281a
......@@ -49,4 +49,4 @@ Server/.DS_Store
.objs/
/Unity/.gradle
/*.user
/ToolApp
/ToolsApp/
......@@ -377,6 +377,10 @@ II.2.12 <HandlesEvent />
<s:Int64 x:Key="/Default/Environment/Hierarchy/GeneratedFilesCacheKey/Timestamp/@EntryValue">79</s:Int64>
<s:Boolean x:Key="/Default/Environment/Hierarchy/NuGetOptions/GlobalEnabled/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/Hierarchy/NuGetOptions/IncludePrerelease/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/Environment/InlayHints/CppParameterNameHintsOptions2/ShowParameterNameHints/@EntryValue">Never</s:String>
<s:String x:Key="/Default/Environment/InlayHints/CSharpParameterNameHintsOptions/ShowParameterNameHints/@EntryValue">Never</s:String>
<s:Boolean x:Key="/Default/Environment/InlayHints/GeneralInlayHintsOptions/EnableInlayHints/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/Environment/InlayHints/VBParameterNameHintsOptions/ShowParameterNameHints/@EntryValue">Never</s:String>
<s:Boolean x:Key="/Default/Environment/InlayHintsOptions/ShowInlayHints/@EntryValue">False</s:Boolean>
......@@ -400,6 +404,10 @@ II.2.12 &lt;HandlesEvent /&gt;&#xD;
<s:Boolean x:Key="/Default/Environment/SearchAndNavigation/AutoExpandResults/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SearchAndNavigation/MergeOccurences/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SearchAndNavigation/OpenPreviewTabForSelectedItemInFindResults/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECpp_002EDaemon_002EInlayHints_002EParameterHints_002ECppParameterNameHintsOptionsOptionsMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECSharp_002EParameterNameHints_002ECSharpParameterNameHintsOptionsMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002EInlayHints_002EInlayHintsOptionsMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002EVB_002EParameterNameHints_002EVBParameterNameHintsOptionsMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpFileLayoutPatternsUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
......
......@@ -8,7 +8,10 @@ namespace ET
{
public override async ETTask Run(EventType.AppStart args)
{
FunctionCallback.GetAllConfigBytes = LoadConfigHelper.LoadAllConfigBytes;
Game.Scene.AddComponent<ConfigComponent>();
await ConfigComponent.Instance.LoadAsync();
StartProcessConfig processConfig = StartProcessConfigCategory.Instance.Get(Game.Options.Process);
......
using System.Collections.Generic;
using System.IO;
namespace ET
{
public static class LoadConfigHelper
{
public static void LoadAllConfigBytes(Dictionary<string, byte[]> output)
{
foreach (string file in Directory.GetFiles($"../Generate/Server/Proto", "*.bytes"))
{
string key = $"{Path.GetFileName(file)}.bytes";
output[key] = File.ReadAllBytes(file);
}
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
namespace ET
{
public class ConfigAwakeSystem : AwakeSystem<ConfigComponent>
{
public override void Awake(ConfigComponent self)
{
ConfigComponent.Instance = self;
self.Awake();
}
}
public class ConfigLoadSystem : LoadSystem<ConfigComponent>
{
public override void Load(ConfigComponent self)
{
self.Load();
}
}
public class ConfigDestroySystem : DestroySystem<ConfigComponent>
{
public override void Destroy(ConfigComponent self)
{
ConfigComponent.Instance = null;
}
}
public static class ConfigComponentSystem
{
public static void Awake(this ConfigComponent self)
{
self.Load();
}
public static void Load(this ConfigComponent self)
{
self.AllConfig.Clear();
HashSet<Type> types = Game.EventSystem.GetTypes(typeof(ConfigAttribute));
foreach (Type type in types)
{
object obj = Activator.CreateInstance(type);
ACategory iCategory = obj as ACategory;
if (iCategory == null)
{
throw new Exception($"class: {type.Name} not inherit from ACategory");
}
iCategory.BeginInit();
iCategory.EndInit();
self.AllConfig[iCategory.ConfigType] = iCategory;
}
}
}
}
\ No newline at end of file
......@@ -20,6 +20,9 @@
</PropertyGroup>
<ItemGroup>
<Compile Remove="Other\**" />
<Compile Include="..\..\Unity\Assets\Hotfix\Module\Config\ConfigComponentSystem.cs">
<Link>Module\Config\ConfigComponentSystem.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Hotfix\Module\Message\MessageDispatcherComponentSystem.cs">
<Link>Module\Message\MessageDispatcherComponentSystem.cs</Link>
</Compile>
......
using System;
using System.Collections.Generic;
namespace ET
{
/// <summary>
/// Config组件会扫描所有的有ConfigAttribute标签的配置,加载进来
/// </summary>
public class ConfigComponent: Entity
{
public static ConfigComponent Instance;
public Dictionary<Type, ACategory> AllConfig = new Dictionary<Type, ACategory>();
}
}
\ No newline at end of file
using System;
using System.IO;
namespace ET
{
public static class ConfigHelper
{
public static string GetText(string key)
{
string path = $"../Config/{key}.txt";
try
{
string configStr = File.ReadAllText(path);
return configStr;
}
catch (Exception e)
{
throw new Exception($"load config file fail, path: {path} {e}");
}
}
public static T ToObject<T>(string str)
{
return JsonHelper.FromJson<T>(str);
}
}
}
......@@ -29,6 +29,10 @@
<Compile Include="..\..\Unity\Assets\Model\Core\**\*.cs">
<Link>Core\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Module\Config\**\*.cs">
<Link>Module\Config\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Compile>
<Compile Remove="Libs\**" />
......@@ -41,9 +45,6 @@
<Compile Include="..\..\Unity\Assets\Model\Module\Actor\IActorMessage.cs">
<Link>Module\Actor\IActorMessage.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Module\Config\AConfigComponent.cs">
<Link>Module\Config\AConfigComponent.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Module\CoroutineLock\CoroutineLock.cs">
<Link>Module\CoroutineLock\CoroutineLock.cs</Link>
</Compile>
......@@ -92,11 +93,13 @@
<Compile Remove="Base\RecyclableMemoryStream\**" />
<EmbeddedResource Remove="Base\RecyclableMemoryStream\**" />
<None Remove="Base\RecyclableMemoryStream\**" />
<Compile Remove="Module\Config\ConfigHelper.cs" />
<Compile Remove="Module\Config\ConfigComponent.cs" />
<Compile Remove="Module\Config\**" />
<EmbeddedResource Remove="Module\Config\**" />
<None Remove="Module\Config\**" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Unity\Assets\Model\Module\Config\ACategory.cs" Link="Module\Config\ACategory.cs" />
<Compile Include="..\..\Unity\Assets\Model\Module\Config\ConfigAttribute.cs" Link="Module\Config\ConfigAttribute.cs" />
<Compile Include="..\..\Unity\Assets\Model\Module\Config\IConfig.cs" Link="Module\Config\IConfig.cs" />
<Compile Include="..\..\Unity\Assets\Model\Module\Message\AMHandler.cs" Link="Module\Message\AMHandler.cs" />
<Compile Include="..\..\Unity\Assets\Model\Module\Message\ErrorCode.cs" Link="Module\Message\ErrorCode.cs" />
<Compile Include="..\..\Unity\Assets\Model\Module\Message\IMessage.cs" Link="Module\Message\IMessage.cs" />
......
......@@ -310,7 +310,7 @@ namespace ET
string json = File.ReadAllText(Path.Combine(string.Format(jsonDir, configType), $"{protoName}.txt"));
object deserialize = BsonSerializer.Deserialize(json, type);
string path = Path.Combine(dir, $"{protoName}.bytes");
string path = Path.Combine(dir, $"{protoName}Category.bytes");
using FileStream file = File.Create(path);
Serializer.Serialize(file, deserialize);
......
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ET
{
......@@ -21,7 +21,6 @@ namespace ET
}
}
public class ConfigDestroySystem : DestroySystem<ConfigComponent>
{
public override void Destroy(ConfigComponent self)
......@@ -40,21 +39,51 @@ namespace ET
public static void Load(this ConfigComponent self)
{
self.AllConfig.Clear();
HashSet<Type> types = Game.EventSystem.GetTypes(typeof(ConfigAttribute));
HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
FunctionCallback.GetAllConfigBytes(configBytes);
List<Task> listTasks = new List<Task>();
foreach (Type type in types)
{
Task task = Task.Run(() => self.LoadOneInThread(type, configBytes));
listTasks.Add(task);
}
Task.WaitAll(listTasks.ToArray());
}
public static async ETTask LoadAsync(this ConfigComponent self)
{
self.AllConfig.Clear();
HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
FunctionCallback.GetAllConfigBytes(configBytes);
List<Task> listTasks = new List<Task>();
foreach (Type type in types)
{
object obj = Activator.CreateInstance(type);
Task task = Task.Run(() => self.LoadOneInThread(type, configBytes));
listTasks.Add(task);
}
ACategory iCategory = obj as ACategory;
if (iCategory == null)
{
throw new Exception($"class: {type.Name} not inherit from ACategory");
}
iCategory.BeginInit();
iCategory.EndInit();
await Task.WhenAll(listTasks.ToArray());
}
self.AllConfig[iCategory.ConfigType] = iCategory;
private static void LoadOneInThread(this ConfigComponent self, Type configType, Dictionary<string, byte[]> configBytes)
{
byte[] oneConfigBytes = configBytes[configType.Name];
object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
lock (self)
{
self.AllConfig[configType] = category;
}
}
}
......
......@@ -4,6 +4,8 @@ namespace ET
{
public override async ETTask Run(EventType.AppStart args)
{
FunctionCallback.GetAllConfigBytes = LoadConfigHelper.LoadAllConfigBytes;
Game.Scene.AddComponent<TimerComponent>();
......@@ -16,6 +18,7 @@ namespace ET
ResourcesComponent.Instance.LoadBundle("config.unity3d");
Game.Scene.AddComponent<ConfigComponent>();
ResourcesComponent.Instance.UnloadBundle("config.unity3d");
await ConfigComponent.Instance.LoadAsync();
Game.Scene.AddComponent<OpcodeTypeComponent>();
Game.Scene.AddComponent<MessageDispatcherComponent>();
......
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace ET
{
public static class LoadConfigHelper
{
public static void LoadAllConfigBytes(Dictionary<string, byte[]> output)
{
Dictionary<string, UnityEngine.Object> keys = ResourcesComponent.Instance.GetBundleAll("config.unity3d");
foreach (var kv in keys)
{
TextAsset v = kv.Value as TextAsset;
string key = $"{kv.Key}.bytes";
output[key] = v.bytes;
}
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
namespace ET
{
public static class FunctionCallback
{
public static Action<Dictionary<string, byte[]>> GetAllConfigBytes;
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace ET
{
public abstract class ACategory: ProtoObject
{
public abstract Type ConfigType { get; }
}
/// <summary>
/// 管理该所有的配置
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class ACategory<T> : ACategory where T : IConfig
{
protected Dictionary<long, T> dict;
public override void BeginInit()
{
string configStr = ConfigHelper.GetText(typeof(T).Name);
try
{
this.dict = ConfigHelper.ToObject<Dictionary<long, T>>(configStr);
}
catch (Exception e)
{
throw new Exception($"parser json fail: {configStr}", e);
}
}
public override Type ConfigType
{
get
{
return typeof(T);
}
}
public override void EndInit()
{
}
public T Get(int id)
{
T t;
if (!this.dict.TryGetValue(id, out t))
{
throw new Exception($"not found config: {typeof(T)} id: {id}");
}
return t;
}
public Dictionary<long, T> GetAll()
{
return this.dict;
}
public T GetOne()
{
return this.dict.Values.First();
}
}
}
\ No newline at end of file
#if !SERVER
using UnityEngine;
#endif
namespace ET
{
/// <summary>
/// 每个Config的基类
/// </summary>
#if !SERVER
[HideInHierarchy]
#endif
public abstract class AConfigComponent: Entity, ISerializeToEntity
{
}
}
\ No newline at end of file
......@@ -10,6 +10,6 @@ namespace ET
{
public static ConfigComponent Instance;
public Dictionary<Type, ACategory> AllConfig = new Dictionary<Type, ACategory>();
public Dictionary<Type, object> AllConfig = new Dictionary<Type, object>();
}
}
\ No newline at end of file
using System;
using UnityEngine;
namespace ET
{
public static class ConfigHelper
{
public static string GetText(string key)
{
try
{
string configStr = ((TextAsset)Game.Scene.GetComponent<ResourcesComponent>().GetAsset("config.unity3d", key)).text;
return configStr;
}
catch (Exception e)
{
throw new Exception($"load config file fail, key: {key}", e);
}
}
public static T ToObject<T>(string str)
{
return JsonHelper.FromJson<T>(str);
}
}
}
\ No newline at end of file
......@@ -52,11 +52,11 @@
<ImplicitlyExpandDesignTimeFacades>false</ImplicitlyExpandDesignTimeFacades>
</PropertyGroup>
<ItemGroup>
<Compile Include="Assets\Hotfix\Module\Config\ConfigComponentSystem.cs" />
<Compile Include="Assets\Hotfix\Module\Message\NetThreadComponentSystem.cs" />
<Compile Include="Assets\Hotfix\Scene\LoginHelper.cs" />
<Compile Include="Assets\Hotfix\Module\Numeric\NumericChangeEvent_NotifyWatcher.cs" />
<Compile Include="Assets\Hotfix\Unit\M2C_CreateUnitsHandler.cs" />
<Compile Include="Assets\Hotfix\Module\Config\ConfigComponentSystem.cs" />
<Compile Include="Assets\Hotfix\Module\Message\SessionIdleCheckerComponentSystem.cs" />
<Compile Include="Assets\Hotfix\Module\Message\NetKcpComponentSystem.cs" />
<Compile Include="Assets\Hotfix\Module\Numeric\NumericWatcher_Hp_ShowUI.cs" />
......
......@@ -52,6 +52,7 @@
<ImplicitlyExpandDesignTimeFacades>false</ImplicitlyExpandDesignTimeFacades>
</PropertyGroup>
<ItemGroup>
<Compile Include="Assets\HotfixView\Helper\LoadConfigHelper.cs" />
<Compile Include="Assets\HotfixView\UI\UILogin\LoginFinish_RemoveLoginUI.cs" />
<Compile Include="Assets\HotfixView\Scene\SceneFactory.cs" />
<Compile Include="Assets\HotfixView\UI\UILobby\UILobbyEvent.cs" />
......
......@@ -52,6 +52,7 @@
<ImplicitlyExpandDesignTimeFacades>false</ImplicitlyExpandDesignTimeFacades>
</PropertyGroup>
<ItemGroup>
<Compile Include="Assets\Model\Core\FunctionCallback.cs" />
<Compile Include="Assets\Model\Helper\PathHelper.cs" />
<Compile Include="Assets\Model\Unit\TurnComponent.cs" />
<Compile Include="Assets\Model\Core\DoubleMap.cs" />
......@@ -91,7 +92,6 @@
<Compile Include="Assets\Model\Core\Object\HideInHierarchy.cs" />
<Compile Include="Assets\Model\Module\Config\ConfigAttribute.cs" />
<Compile Include="Assets\Model\Core\Async\AsyncETVoidMethodBuilder.cs" />
<Compile Include="Assets\Model\Module\Config\ACategory.cs" />
<Compile Include="Assets\Model\Module\Message\PingComponent.cs" />
<Compile Include="Assets\Model\Core\Object\EntityCreateComponet.cs" />
<Compile Include="Assets\Model\Module\Message\RpcException.cs" />
......@@ -165,12 +165,10 @@
<Compile Include="Assets\Model\Core\MultiMap.cs" />
<Compile Include="Assets\Model\Module\Numeric\NumericWatcherAttribute.cs" />
<Compile Include="Assets\Model\Core\LogType.cs" />
<Compile Include="Assets\Model\Module\Config\AConfigComponent.cs" />
<Compile Include="Assets\Model\Core\Async\ETTaskCompleted.cs" />
<Compile Include="Assets\Model\Core\HashSetComponent.cs" />
<Compile Include="Assets\Model\Core\Pool.cs" />
<Compile Include="Assets\Model\Core\Object\ObjectSystemAttribute.cs" />
<Compile Include="Assets\Model\Module\Config\ConfigHelper.cs" />
<Compile Include="Assets\Model\Core\Entity\Game.cs" />
<Compile Include="Assets\Model\Core\StructBsonSerialize.cs" />
<Compile Include="Assets\Model\Module\CoroutineLock\CoroutineLock.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册