using Fantasy.Helper; namespace Fantasy.Tools.ProtocalExporter; internal class OpCodeCache { private readonly List _opCodes = new List(); private readonly Dictionary _opcodeCache; private readonly Dictionary _saveOpCodeCache = new(); private readonly string _opcodeCachePath = $"{ExporterSettingsHelper.NetworkProtocolDirectory}OpCode.Cache"; /// /// 构造函数,用于初始化网络协议操作码缓存 /// public OpCodeCache(bool regenerate) { if (File.Exists(_opcodeCachePath) && !regenerate) { var readAllText = File.ReadAllText(_opcodeCachePath); _opcodeCache = readAllText.Deserialize>(); _opCodes.AddRange(_opcodeCache.Values); } else { _opcodeCache = new Dictionary(); } } /// /// 保存网络协议操作码 /// public void Save() { File.WriteAllText(_opcodeCachePath, _saveOpCodeCache.ToJson()); } /// /// 根据className获得OpCode、如果是新增的会产生一个新的OpCode /// /// 协议名 /// 操作码 /// public uint GetOpcodeCache(string className, ref uint opcode) { if (!_opcodeCache.TryGetValue(className, out var opCode)) { while (_opCodes.Contains(++opcode)) { } opCode = opcode; _opCodes.Add(opCode); } _saveOpCodeCache.Add(className, opCode); return opCode; } }