using System; namespace Sog { /* 64-61 4b inst id (1-15) 60-57 4b server type index (1-15) 56-47 10b worldid (1-1023) 46-15 32b time 14-1 14b seq = 16384 */ public class ServerUniqueIDMgr { public readonly uint UID_START_TIME = 1655827200; //2022/6/22 private ulong baseUUid; private ulong timeGap; private ulong uuid; private const int InstIdBitNum = 4; private const int servertypeindexBitNum = 4; private const int worldIdBitNum = 10; private static ServerType[] UsedServerTypes = { ServerType.Game, ServerType.Arena, ServerType.Account}; public ServerUniqueIDMgr(uint serverId) { int serverType = ServerIDUtils.GetServerType(serverId); uint instId = ServerIDUtils.GetInstanceID(serverId); uint worldId = ServerIDUtils.GetWorldID(serverId); int serverTypeIndex = -1; for (int i = 0; i < UsedServerTypes.Length; i++) { if(serverType == (int)UsedServerTypes[i]) { serverTypeIndex = i; break; } } if(serverTypeIndex == -1) { throw new Exception("servertype need add to UsedServerTypes"); } if (instId >= (1 << InstIdBitNum)) { throw new Exception("instId overflow"); } if (serverTypeIndex >= (1 << servertypeindexBitNum)) { throw new Exception("serverTypeIndex overflow"); } if (worldId >= (1 << worldIdBitNum)) { throw new Exception("worldId overflow"); } timeGap = (ulong)(AppTime.GetNowSysSecond() - UID_START_TIME); baseUUid = ((ulong)instId << 60) | ((ulong)serverTypeIndex << 56) | ((ulong)worldId << 46) | (timeGap << 14); uuid = baseUUid; } public ulong GenUniqueId() { uuid++; return uuid; } } }