From dd9c93b5b4c84bf818572b82f63445397ee3d1c9 Mon Sep 17 00:00:00 2001 From: raphaeIl Date: Tue, 4 Mar 2025 18:16:02 -0500 Subject: [PATCH] basic table loader --- Novaria.Common/Util/GameDataController.cs | 117 ++ .../Controllers/Api/ProtocolHandlers/Login.cs | 1048 +++++++++-------- Novaria.GameServer/GameServer.cs | 17 +- Novaria.GameServer/Services/TableService.cs | 112 ++ 4 files changed, 767 insertions(+), 527 deletions(-) create mode 100644 Novaria.Common/Util/GameDataController.cs create mode 100644 Novaria.GameServer/Services/TableService.cs diff --git a/Novaria.Common/Util/GameDataController.cs b/Novaria.Common/Util/GameDataController.cs new file mode 100644 index 0000000..29fa0ca --- /dev/null +++ b/Novaria.Common/Util/GameDataController.cs @@ -0,0 +1,117 @@ +using System.Text; + +namespace Novaria.Common.Util +{ + public class GameDataController : Singleton + { + private int _magicKey = 234324; + private string _binVersion = "0.1.0.2"; + + public void LoadCommonBinData(string filepath, Action primaryIntAction, Action primaryStringAction, Action primaryLongAction, Action noPrimaryAction) + { + byte[] array = File.ReadAllBytes(filepath); + + if (array == null) + { + return; + } + using (MemoryStream memoryStream = new MemoryStream(array)) + { + using (BinaryReader binaryReader = new BinaryReader(memoryStream)) + { + var curr_magicKey = binaryReader.ReadInt32(); + + if (curr_magicKey == this._magicKey) + { + int num = (int)binaryReader.ReadInt16(); + byte[] bytes = binaryReader.ReadBytes(num); + if (!(Encoding.UTF8.GetString(bytes) != this._binVersion)) + { + bool flag = binaryReader.ReadByte() == 1; + byte b = binaryReader.ReadByte(); + bool flag2 = b == 1; + bool flag3 = b == 2; + num = binaryReader.ReadInt32(); + if (flag) + { + if (flag2) + { + this.LoadPrimaryKeyIntTable(binaryReader, num, primaryIntAction); + } else if (flag3) + { + this.LoadPrimaryLongIntTable(binaryReader, num, primaryLongAction); + } else + { + this.LoadPrimaryKeyStringTable(binaryReader, num, primaryStringAction); + } + } else + { + this.LoadNoPrimaryKeyTable(binaryReader, num, noPrimaryAction); + } + } + } + } + } + } + + private void LoadPrimaryKeyIntTable(BinaryReader br, int length, Action primaryIntAction) + { + if (primaryIntAction == null) + { + return; + } + for (int i = 0; i < length; i++) + { + int arg = br.ReadInt32(); + int count = (int)br.ReadInt16(); + byte[] arg2 = br.ReadBytes(count); + primaryIntAction(arg, arg2); + } + } + + private void LoadPrimaryLongIntTable(BinaryReader br, int length, Action primaryLongAction) + { + if (primaryLongAction == null) + { + return; + } + for (int i = 0; i < length; i++) + { + long arg = br.ReadInt64(); + int count = (int)br.ReadInt16(); + byte[] arg2 = br.ReadBytes(count); + primaryLongAction(arg, arg2); + } + } + private void LoadPrimaryKeyStringTable(BinaryReader br, int length, Action primaryStringAction) + { + if (primaryStringAction == null) + { + return; + } + for (int i = 0; i < length; i++) + { + int count = (int)br.ReadInt16(); + byte[] array = br.ReadBytes(count); + string @string = Encoding.UTF8.GetString(array); + count = (int)br.ReadInt16(); + array = br.ReadBytes(count); + primaryStringAction(@string, array); + } + } + + private void LoadNoPrimaryKeyTable(BinaryReader br, int length, Action noPrimaryAction) + { + if (noPrimaryAction == null) + { + return; + } + for (int i = 0; i < length; i++) + { + int count = (int)br.ReadInt16(); + byte[] obj = br.ReadBytes(count); + noPrimaryAction(obj); + } + } + } +} diff --git a/Novaria.GameServer/Controllers/Api/ProtocolHandlers/Login.cs b/Novaria.GameServer/Controllers/Api/ProtocolHandlers/Login.cs index 4a1b12c..c5e1f04 100644 --- a/Novaria.GameServer/Controllers/Api/ProtocolHandlers/Login.cs +++ b/Novaria.GameServer/Controllers/Api/ProtocolHandlers/Login.cs @@ -5,14 +5,34 @@ using Serilog; using System.Text.Json; using Novaria.PcapParser; using Novaria.Common.Crypto; +using Novaria.GameServer.Services; +using Nova.Client; namespace Novaria.GameServer.Controllers.Api.ProtocolHandlers { public class Login : ProtocolHandlerBase { - public Login(IProtocolHandlerFactory protocolHandlerFactory) : base(protocolHandlerFactory) + public Login(IProtocolHandlerFactory protocolHandlerFactory, TableService tableService) : base(protocolHandlerFactory) { + table_Achievement achievements = (table_Achievement)tableService.GetTable(); + } + [ProtocolHandler(NetMsgId.gacha_information_req)] + public Packet PlayerLoginHandler(Nil req) + { + GachaInformationResp gachaInfoResp = new GachaInformationResp() + { + Information = + { + new GachaInfo() + { + Id = 1, + DaysCount = 31, + } + }, + }; + + return Packet.Create(NetMsgId.gacha_information_req, gachaInfoResp); } [ProtocolHandler(NetMsgId.player_login_req)] // req id goes here @@ -62,519 +82,519 @@ namespace Novaria.GameServer.Controllers.Api.ProtocolHandlers { Acc = accountInfo }; - playerInfoResponse.Chars.AddRange( - [ - new Proto.Char() - { - Tid = 111, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 11101, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[0].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 117, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 11701, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[1].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 108, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 10801, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[2].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 123, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 12301, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[3].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 127, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 12701, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[4].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 107, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 10701, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[5].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 132, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 13201, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[6].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 135, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 13501, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[7].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 126, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 12601, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[8].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 118, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 11801, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[9].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 142, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 14201, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[10].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 119, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 11901, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[11].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 103, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 10301, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[12].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 125, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 12501, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[13].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 120, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 12001, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[14].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 112, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 11201, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[15].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - new Proto.Char() - { - Tid = 113, - Exp = 0, - DatingLandmarkIds = [], - DatingEventIds = [], - DatingEventRewardIds = [], - EquipmentIds = [ - 0, - 0, - 0 - ], - Level = 90, - SkillLvs = [ - 10, - 10, - 10, - 10, - 1 - ], - Skin = 11301, - AffinityLevel = 20, - AffinityExp = 0, - Advance = 8, - Plots = [], - AffinityQuests = pcapPlayerInfo.Chars[16].AffinityQuests, - TalentNodes = ByteString.Empty, - CreateTime = 1736745112, - NextPackage = ByteString.Empty - }, - ]); + //playerInfoResponse.Chars.AddRange( + //[ + // new Proto.Char() + // { + // Tid = 111, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 11101, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[0].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 117, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 11701, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[1].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 108, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 10801, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[2].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 123, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 12301, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[3].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 127, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 12701, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[4].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 107, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 10701, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[5].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 132, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 13201, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[6].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 135, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 13501, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[7].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 126, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 12601, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[8].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 118, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 11801, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[9].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 142, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 14201, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[10].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 119, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 11901, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[11].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 103, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 10301, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[12].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 125, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 12501, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[13].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 120, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 12001, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[14].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 112, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 11201, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[15].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + // new Proto.Char() + // { + // Tid = 113, + // Exp = 0, + // DatingLandmarkIds = [], + // DatingEventIds = [], + // DatingEventRewardIds = [], + // EquipmentIds = [ + // 0, + // 0, + // 0 + // ], + // Level = 90, + // SkillLvs = [ + // 10, + // 10, + // 10, + // 10, + // 1 + // ], + // Skin = 11301, + // AffinityLevel = 20, + // AffinityExp = 0, + // Advance = 8, + // Plots = [], + // AffinityQuests = pcapPlayerInfo.Chars[16].AffinityQuests, + // TalentNodes = ByteString.Empty, + // CreateTime = 1736745112, + // NextPackage = ByteString.Empty + // }, + //]); playerInfoResponse.Res.AddRange(pcapPlayerInfo.Res); playerInfoResponse.Items.AddRange(pcapPlayerInfo.Items); playerInfoResponse.Formation = pcapPlayerInfo.Formation; diff --git a/Novaria.GameServer/GameServer.cs b/Novaria.GameServer/GameServer.cs index 1ab3583..74b1d41 100644 --- a/Novaria.GameServer/GameServer.cs +++ b/Novaria.GameServer/GameServer.cs @@ -4,6 +4,9 @@ using Serilog; using Novaria.GameServer.Controllers.Api.ProtocolHandlers; using Novaria.Common.Crypto; +using Novaria.Common.Util; +using Newtonsoft.Json; +using Novaria.GameServer.Services; namespace Novaria.GameServer { @@ -12,19 +15,6 @@ namespace Novaria.GameServer public static void Main(string[] args) { PcapParser.PcapParser.Instance.LoadAllPackets(); // turn this off after real handlers are finished - - // IDK how to dump - /*Log.Warning("Dumping Data....."); - var tables = Assembly.GetExecutingAssembly() - .GetTypes() - .Where(t => t.Name.StartsWith("table_")); - - foreach (var file in Directory.GetFiles("../Novaria.Common/dataBin/")) - { - Log.Information(file); - var data = new GameDataExtraction().LoadCommonBinData(file); - Log.Information(data.ToString()); - }*/ Log.Information("Starting SDK Server..."); try @@ -38,6 +28,7 @@ namespace Novaria.GameServer builder.Services.AddControllers(); builder.Services.AddProtocolHandlerFactory(); + builder.Services.AddExcelTableService(); builder.Services.AddControllers().AddApplicationPart(Assembly.GetAssembly(typeof(GameServer))); // Add all Handler Groups diff --git a/Novaria.GameServer/Services/TableService.cs b/Novaria.GameServer/Services/TableService.cs new file mode 100644 index 0000000..ecca62d --- /dev/null +++ b/Novaria.GameServer/Services/TableService.cs @@ -0,0 +1,112 @@ +using Google.Protobuf; +using Google.Protobuf.Collections; +using Ionic.Zip; +using Nova.Client; +using Novaria.Common.Util; +using Serilog; +using System.IO; +using System.Reflection; +using System.Runtime.Serialization.Formatters; + +namespace Novaria.GameServer.Services +{ + // note that all operations in this class MUST be thread-safe, idk if it is currently but if it's not, there will be race condition problems + public class TableService(ILogger _logger) + { + private readonly ILogger logger = _logger; + + // example: maps Achievement (type) -> table_Achievement (instance), table_Achievement has a list that stores table + private readonly Dictionary caches = []; + public static string ResourceDir = Path.Join(Path.GetDirectoryName(AppContext.BaseDirectory), "Resources"); + + private Type currentTableTypeCache; // this is the type that we're currently loading, ex. Achievement (type), reason for caching it in a field member? Nova uses hardcoded params Action so im too lazy to change + + /// + /// Please only use this to get table that have a respective file (i.e. CharacterExcelTable have characterexceltable.bytes) + /// + /// + /// + /// + public IMessage GetTable(bool bypassCache = false) where T : IMessage + { + var type = typeof(T); + currentTableTypeCache = type; + + if (!bypassCache && caches.TryGetValue(type, out var cache)) + return (T)cache; + + var tableDir = Path.Combine(ResourceDir, "Tables"); + var bytesFilePath = Path.Combine(tableDir, $"{typeof(T).Name}.bytes"); + + if (!File.Exists(bytesFilePath)) + { + throw new FileNotFoundException($"bytes files for {type.Name} not found"); + } + + //TableEncryptionService.XOR(type.Name, bytes); + + this.LoadCommonBin(bytesFilePath); // after this, loaded table will be in the cache + + logger.LogDebug("{Excel} loaded and cached", type.Name); + + return caches[currentTableTypeCache]; + } + + private void LoadCommonBin(string bytesFilePath) + { + currentTableTypeCache = typeof(T); // too lazy to change actions params, so do this + + // get the table_XXX type + Type table_Type = Assembly.GetAssembly(typeof(table_Achievement)).GetTypes().Where(t => t.Name == $"table_{typeof(T).Name}").FirstOrDefault(); + + if (table_Type == null) + { + Log.Error($"table_{typeof(T).Name} type was not found."); + return; + } + + var inst = (IMessage)Activator.CreateInstance(table_Type); + caches[currentTableTypeCache] = inst; + + GameDataController.Instance.LoadCommonBinData(bytesFilePath, new Action(this.AddCommonBin), + new Action(this.AddCommonBin), + new Action(this.AddCommonBin), + new Action(this.AddCommonBin)); + } + + private void AddCommonBin(int _, byte[] data) { AddCommonBin_internal(_, data); } + private void AddCommonBin(string _, byte[] data) { AddCommonBin_internal(_, data); } + private void AddCommonBin(long _, byte[] data) { AddCommonBin_internal(_, data); } + private void AddCommonBin(byte[] data) { AddCommonBin_internal(null, data); } + + private void AddCommonBin_internal(object _, byte[] data) + { + var parserProperty = currentTableTypeCache.GetProperties().Where(p => p.Name == "Parser").SingleOrDefault(); + var parserInstance = parserProperty.GetValue(null); + + var parsedData = typeof(MessageParser).GetMethods().Where(m => m.Name == "ParseFrom").FirstOrDefault().Invoke(parserInstance, new object[] { data }); ; + // add to target table, very inefficient rn + IMessage targetTable = caches[currentTableTypeCache]; + var __ = currentTableTypeCache.GetProperties(); + + + var listField = targetTable.GetType().GetProperties().Where(p => p.Name == "List").SingleOrDefault(); + + object repeatedFieldInstance = listField.GetValue(targetTable); + + var addMethod = repeatedFieldInstance.GetType().GetMethods().Where(m => m.Name == "Add").FirstOrDefault(); + + addMethod.Invoke(repeatedFieldInstance, new object[] { parsedData }); + + //Log.Information($"Added {_}"); + } + } + + internal static class ExcelTableServiceExtensions + { + public static void AddExcelTableService(this IServiceCollection services) + { + services.AddSingleton(); + } + } +}