feat: initial commit - HJHA game server full source
6 major server modules (PdkFriendServer/GlobalSever/ServerCore/GameModule/GameNetModule) + game logic (GameFix/GameDAL/ServerData) + network layer (NetWorkMessage) + data layer (ObjectModel) + utilities (MrWu/Core/Config/CloudAPI/dll) + adapters (zyxAdapter/base) .NET 8.0 C# solution, 16 projects, 958 source files
This commit is contained in:
71
ServerCore/Core/ByteHelper.cs
Normal file
71
ServerCore/Core/ByteHelper.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using ActorCore;
|
||||
|
||||
namespace Server.Core
|
||||
{
|
||||
public static class ByteHelper
|
||||
{
|
||||
public static ActorId ReadActorId(this byte[] bytes, int offset)
|
||||
{
|
||||
return new ActorId(bytes[offset], bytes[offset + 1], bytes[offset + 2]);
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, ActorId actorId)
|
||||
{
|
||||
bytes[offset] = actorId.ModuleId;
|
||||
bytes[offset + 1] = actorId.NodeNum;
|
||||
bytes[offset + 2] = actorId.ActorTypeId;
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, short num)
|
||||
{
|
||||
bytes[offset] = (byte)(num & 0xff);
|
||||
bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, ushort num)
|
||||
{
|
||||
bytes[offset] = (byte)(num & 0xff);
|
||||
bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
|
||||
}
|
||||
|
||||
public static unsafe void WriteTo(this byte[] bytes, int offset, long num)
|
||||
{
|
||||
byte* bPoint = (byte*)#
|
||||
for (int i = 0; i < sizeof(long); ++i)
|
||||
{
|
||||
bytes[offset + i] = bPoint[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, int num)
|
||||
{
|
||||
//跟这个意思是一样的,把num解析为byte 拷贝到bytes中
|
||||
//BitConverter.GetBytes(num);
|
||||
|
||||
bytes[offset] = (byte)(num & 0xff);
|
||||
bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
|
||||
bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
|
||||
bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, uint num)
|
||||
{
|
||||
bytes[offset] = (byte)(num & 0xff);
|
||||
bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
|
||||
bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
|
||||
bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
|
||||
}
|
||||
|
||||
public static void WriteInt(this MemoryStream memoryStream, int num)
|
||||
{
|
||||
memoryStream.WriteByte((byte)(num & 0xff));
|
||||
memoryStream.WriteByte( (byte)((num & 0xff00) >> 8));
|
||||
memoryStream.WriteByte((byte)((num & 0xff0000) >> 16));
|
||||
memoryStream.WriteByte((byte)((num & 0xff000000) >> 24));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
71
ServerCore/Core/CodeTypes.cs
Normal file
71
ServerCore/Core/CodeTypes.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using MrWu.Debug;
|
||||
|
||||
namespace Server.Core
|
||||
{
|
||||
public class CodeTypes : Singleton<CodeTypes>
|
||||
{
|
||||
/// <summary>
|
||||
/// 所有类型
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, Type> allTypes = new Dictionary<string, Type>();
|
||||
|
||||
/// <summary>
|
||||
/// 所有标记 key 表示 BaseAttribute 子类型 values 类
|
||||
/// </summary>
|
||||
private readonly UnOrderMultiMapSet<Type, Type> types = new UnOrderMultiMapSet<Type, Type>();
|
||||
|
||||
public void Init(Assembly[] assemblies)
|
||||
{
|
||||
Dictionary<string, Type> addTypes = AssemblyHelper.GetAssemblyTypes(assemblies);
|
||||
foreach (var kv in addTypes)
|
||||
{
|
||||
string fullName = kv.Key;
|
||||
Type type = kv.Value;
|
||||
this.allTypes[fullName] = type;
|
||||
|
||||
if (type.IsAbstract)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 记录所有的有BaseAttribute标记的的类型
|
||||
object[] objects = type.GetCustomAttributes(typeof(BaseAttribute), true);
|
||||
|
||||
foreach (object o in objects)
|
||||
{
|
||||
this.types.Add(o.GetType(), type);
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Info($"AllTypeCount:{this.allTypes.Count}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="systemAttributeType"></param>
|
||||
/// <returns></returns>
|
||||
public HashSet<Type> GetTypes(Type systemAttributeType)
|
||||
{
|
||||
if (!this.types.ContainsKey(systemAttributeType))
|
||||
{
|
||||
return new HashSet<Type>();
|
||||
}
|
||||
|
||||
return this.types[systemAttributeType];
|
||||
}
|
||||
|
||||
public Dictionary<string, Type> GetTypes()
|
||||
{
|
||||
return allTypes;
|
||||
}
|
||||
|
||||
public Type GetType(string typeName)
|
||||
{
|
||||
return this.allTypes[typeName];
|
||||
}
|
||||
}
|
||||
}
|
||||
152
ServerCore/Core/DoubleMap.cs
Normal file
152
ServerCore/Core/DoubleMap.cs
Normal file
@ -0,0 +1,152 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Core
|
||||
{
|
||||
public class DoubleMap<K,V>
|
||||
{
|
||||
private readonly Dictionary<K, V> kv = new Dictionary<K, V>();
|
||||
private readonly Dictionary<V, K> vk = new Dictionary<V, K>();
|
||||
|
||||
public DoubleMap()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DoubleMap(int capacity)
|
||||
{
|
||||
kv = new Dictionary<K, V>(capacity);
|
||||
vk = new Dictionary<V, K>(capacity);
|
||||
}
|
||||
|
||||
public void ForEach(Action<K,V> action)
|
||||
{
|
||||
if (action == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Dictionary<K, V>.KeyCollection keys = kv.Keys;
|
||||
foreach (K key in keys)
|
||||
{
|
||||
action(key, kv[key]);
|
||||
}
|
||||
}
|
||||
|
||||
public List<K> Keys
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<K>(kv.Keys);
|
||||
}
|
||||
}
|
||||
|
||||
public List<V> Values
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<V>(kv.Values);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Add(K key, V value)
|
||||
{
|
||||
if (key == null || value == null || kv.ContainsKey(key) || vk.ContainsKey(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
kv.Add(key,value);
|
||||
vk.Add(value,key);
|
||||
return true;
|
||||
}
|
||||
|
||||
public V GetValueByKey(K key)
|
||||
{
|
||||
if (key != null && kv.ContainsKey(key))
|
||||
{
|
||||
return kv[key];
|
||||
}
|
||||
|
||||
return default(V);
|
||||
}
|
||||
|
||||
public K GetKeyByValue(V value)
|
||||
{
|
||||
if (value != null && vk.ContainsKey(value))
|
||||
{
|
||||
return vk[value];
|
||||
}
|
||||
return default(K);
|
||||
}
|
||||
|
||||
public void RemoveByKey(K key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
V value;
|
||||
if (!kv.TryGetValue(key, out value))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
kv.Remove(key);
|
||||
vk.Remove(value);
|
||||
}
|
||||
|
||||
public void RemoveByValue(V value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
K key;
|
||||
if (!vk.TryGetValue(value,out key))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
kv.Remove(key);
|
||||
vk.Remove(value);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
kv.Clear();
|
||||
vk.Clear();
|
||||
}
|
||||
|
||||
public bool ContainsKey(K key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return kv.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool ContainsValue(V value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return vk.ContainsKey(value);
|
||||
}
|
||||
|
||||
public bool Contains(K key, V value)
|
||||
{
|
||||
if (key == null || value == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return kv.ContainsKey(key) && vk.ContainsKey(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
47
ServerCore/Core/ListPool.cs
Normal file
47
ServerCore/Core/ListPool.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Core
|
||||
{
|
||||
public class ListPool<T>
|
||||
{
|
||||
private readonly Queue<List<T>> Queues = new Queue<List<T>>();
|
||||
|
||||
public List<T> Get()
|
||||
{
|
||||
if (Queues.Count > 0)
|
||||
{
|
||||
return Queues.Dequeue();
|
||||
}
|
||||
|
||||
return new List<T>();
|
||||
}
|
||||
|
||||
public void Recycle(List<T> list)
|
||||
{
|
||||
list.Clear();
|
||||
Queues.Enqueue(list);
|
||||
}
|
||||
}
|
||||
|
||||
public class ConcurrentListPool<T>
|
||||
{
|
||||
private readonly ConcurrentQueue<List<T>> Queues = new ConcurrentQueue<List<T>>();
|
||||
|
||||
public List<T> Get()
|
||||
{
|
||||
if (Queues.TryDequeue(out var list))
|
||||
{
|
||||
return list;
|
||||
}
|
||||
|
||||
return new List<T>();
|
||||
}
|
||||
|
||||
public void Recycle(List<T> list)
|
||||
{
|
||||
list.Clear();
|
||||
Queues.Enqueue(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
50
ServerCore/Core/ObjectRef.cs
Normal file
50
ServerCore/Core/ObjectRef.cs
Normal file
@ -0,0 +1,50 @@
|
||||
namespace Server.Core
|
||||
{
|
||||
public struct ObjectRef<T> where T : class,IReference
|
||||
{
|
||||
private readonly long ReferenceId;
|
||||
|
||||
private T self;
|
||||
|
||||
private ObjectRef(T t)
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
this.ReferenceId = 0;
|
||||
this.self = null;
|
||||
return;
|
||||
}
|
||||
|
||||
this.ReferenceId = t.ReferenceId;
|
||||
this.self = t;
|
||||
}
|
||||
|
||||
private T UnWarp
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.self == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.self.ReferenceId != this.ReferenceId)
|
||||
{
|
||||
this.self = null;
|
||||
}
|
||||
|
||||
return this.self;
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator ObjectRef<T>(T t)
|
||||
{
|
||||
return new ObjectRef<T>(t);
|
||||
}
|
||||
|
||||
public static implicit operator T(ObjectRef<T> t)
|
||||
{
|
||||
return t.UnWarp;
|
||||
}
|
||||
}
|
||||
}
|
||||
66
ServerCore/Core/ThreadSynchronizationContext.cs
Normal file
66
ServerCore/Core/ThreadSynchronizationContext.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using MrWu.Debug;
|
||||
|
||||
namespace Server.Core
|
||||
{
|
||||
public class ThreadSynchronizationContext : SynchronizationContext
|
||||
{
|
||||
private readonly ConcurrentQueue<Action> queue = new ConcurrentQueue<Action>();
|
||||
|
||||
private Action a;
|
||||
|
||||
private int actorTypeId;
|
||||
|
||||
public int MsgCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return queue.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public ThreadSynchronizationContext()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ThreadSynchronizationContext(int actorTypeId = -1)
|
||||
{
|
||||
this.actorTypeId = actorTypeId;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
int count = this.queue.Count;
|
||||
while (count -- > 0)
|
||||
{
|
||||
if (!this.queue.TryDequeue(out a))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
a();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.Error($"Post: {e.Message} ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Post(SendOrPostCallback callBack, object state)
|
||||
{
|
||||
this.Post(()=>callBack(state));
|
||||
}
|
||||
|
||||
public void Post(Action action)
|
||||
{
|
||||
//Debug.Info("添加Action!");
|
||||
this.queue.Enqueue(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
81
ServerCore/Core/UnOrderMultiMapSet.cs
Normal file
81
ServerCore/Core/UnOrderMultiMapSet.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Core
|
||||
{
|
||||
public class UnOrderMultiMapSet<T,K> : Dictionary<T, HashSet<K>>
|
||||
{
|
||||
// 重用HashSet
|
||||
public new HashSet<K> this[T t]
|
||||
{
|
||||
get
|
||||
{
|
||||
HashSet<K> set;
|
||||
if (!this.TryGetValue(t, out set))
|
||||
{
|
||||
set = new HashSet<K>();
|
||||
}
|
||||
return set;
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<T, HashSet<K>> GetDictionary()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Add(T t, K k)
|
||||
{
|
||||
HashSet<K> set;
|
||||
this.TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
set = new HashSet<K>();
|
||||
base[t] = set;
|
||||
}
|
||||
set.Add(k);
|
||||
}
|
||||
|
||||
public bool Remove(T t, K k)
|
||||
{
|
||||
HashSet<K> set;
|
||||
this.TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!set.Remove(k))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (set.Count == 0)
|
||||
{
|
||||
this.Remove(t);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Contains(T t, K k)
|
||||
{
|
||||
HashSet<K> set;
|
||||
this.TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return set.Contains(k);
|
||||
}
|
||||
|
||||
public new int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = 0;
|
||||
foreach (KeyValuePair<T,HashSet<K>> kv in this)
|
||||
{
|
||||
count += kv.Value.Count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user