mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
[+] ConfigUtility
[+] ConfigUtility
This commit is contained in:
65
Assets/GameScripts/HotFix/GameProto/ConfigUtility.cs
Normal file
65
Assets/GameScripts/HotFix/GameProto/ConfigUtility.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TEngine;
|
||||
|
||||
namespace GameProto
|
||||
{
|
||||
/// <summary>
|
||||
/// 指定Key委托。
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">键。</typeparam>
|
||||
/// <typeparam name="TValue">值。</typeparam>
|
||||
public delegate TKey ConvertDictionaryKey<out TKey, in TValue>(TValue val);
|
||||
|
||||
/// <summary>
|
||||
/// 配置表辅助工具。
|
||||
/// </summary>
|
||||
public static class ConfigUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成64long的主键。
|
||||
/// </summary>
|
||||
/// <param name="key1">键1。</param>
|
||||
/// <param name="key2">键2。</param>
|
||||
/// <returns>64long的主键。</returns>
|
||||
public static UInt64 Make64Key(uint key1, uint key2)
|
||||
{
|
||||
return ((UInt64)key1 << 32) | key2;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拷贝配置表字典。
|
||||
/// </summary>
|
||||
/// <param name="dict">拷贝地址。</param>
|
||||
/// <param name="source">拷贝源。</param>
|
||||
/// <param name="convKey">指定主键。</param>
|
||||
/// <typeparam name="TKey">键。</typeparam>
|
||||
/// <typeparam name="TValue">值。</typeparam>
|
||||
/// <returns>是否拷贝成功。</returns>
|
||||
public static bool CopyConfigDict<TKey, TValue>(ref Dictionary<TKey, TValue> dict,List<TValue> source, ConvertDictionaryKey<TKey, TValue> convKey)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
dict.Clear();
|
||||
|
||||
bool failed = false;
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
var data = source[i];
|
||||
TKey key = convKey(data);
|
||||
if (dict.ContainsKey(key))
|
||||
{
|
||||
Log.Fatal("Copy Config Failed: {0} IndexOf {1} Had Repeated Key: {2} ", typeof(TValue).Name, i + 1, key);
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
dict.Add(key, data);
|
||||
}
|
||||
return !failed;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e278e636820842f293e2a765962ad4f8
|
||||
timeCreated: 1683300170
|
Reference in New Issue
Block a user