public void Serialize(BinWriter data) { foreach (SerializeValue value in GetSerializeValues(this.GetType())) { value.Serialize(this, data); } }
static bool OnCreatureQuery(LoginClient client, CMSG msgID, BinReader data) { uint id = data.ReadUInt32(); DBCreature creature = (DBCreature)DataServer.Database.FindObjectByKey(typeof(DBCreature), id); if (creature == null) { client.Close("OnCreatureQuery(): id didn't exists."); return(true); } BinWriter w = LoginClient.NewPacket(SMSG.CREATURE_QUERY_RESPONSE); w.Write(creature.ObjectId); w.Write(creature.Name); w.Write(creature.Name1); w.Write(creature.Name2); w.Write(creature.Name3); w.Write(creature.Title); w.Write(creature.Flags); w.Write(creature.CreatureType); w.Write(creature.CreatureFamily); w.Write(0); // unknown client.Send(w); return(true); }
public void Serialize_deserialize_blockheader() { var keychain = Keychain.From_random_seed(); var b = New_block(new Transaction[] { }, keychain); // Console.WriteLine(JsonConvert.SerializeObject(b.header, Formatting.Indented)); using (var vec = new MemoryStream()) { var bw = new BinWriter(vec); var bh1 = b.Header; b.Header.Write(bw); vec.Position = 0; var br = new BinReader(vec); var bh2 = BlockHeader.Readnew(br); Assert.Equal(bh1.Version, bh2.Version); Assert.Equal(bh1.Height, bh2.Height); Assert.Equal(bh1.Previous.Value, bh2.Previous.Value); Assert.Equal(bh1.Timestamp.PrecisionSeconds(), bh2.Timestamp); Assert.Equal(bh1.UtxoRoot.Value, bh2.UtxoRoot.Value); Assert.Equal(bh1.RangeProofRoot.Value, bh2.RangeProofRoot.Value); Assert.Equal(bh1.KernelRoot.Value, bh2.KernelRoot.Value); Assert.Equal(bh1.Nonce, bh2.Nonce); Assert.Equal(bh1.Difficulty.Num, bh2.Difficulty.Num); Assert.Equal(bh1.TotalDifficulty.Num, bh2.TotalDifficulty.Num); } }
public void WriteUnihanData(BinWriter bw) { // Start position of simplified hash array: we'll return here bw.WriteInt(0); // Number of characters bw.WriteInt((int)infos.Count); // File pointers for each char: we'll return here int pos = bw.Position; for (int i = 0; i != infos.Count; ++i) { bw.WriteShort(0); bw.WriteInt(0); } // Make character info for each character; serialize it; remember file position foreach (var x in infos) { x.Value.FilePos = bw.Position; UniHanziInfo uhi = getInfo(x.Key, x.Value); uhi.Serialize(bw); } // Remember end of file int endPos = bw.Position; // Go back to start of file, write file positions for each character. bw.Position = pos; foreach (var x in infos) { bw.WriteShort((short)x.Key); bw.WriteInt(x.Value.FilePos); } // Return to end of file bw.Position = endPos; }
static bool GossipHello(LoginClient client, CMSG msgID, BinReader data) { ulong vendorGUID = data.ReadUInt64(); BinWriter pkg = LoginClient.NewPacket(SMSG.GOSSIP_MESSAGE); string message = "Welcome to World of WoWCraft!"; string message2 = "Known Bugs: Read this!"; pkg.Write(vendorGUID); // Vendor GUID pkg.Write((int)1); // Message ID pkg.Write((int)2); // Counter pkg.Write((int)1); // Counter Number pkg.Write((int)7); // Message Type pkg.Write(message); // Message Text pkg.Write((int)2); // Counter Number pkg.Write((int)2); // Message Type pkg.Write(message2); // Message Text pkg.Write(0); // ? client.Send(pkg); return(true); }
public bool Invite(WorldClient client, string name) { try { WorldClient t_client = (WorldClient)(WorldServer.GetClientByName(name)); if (t_client != null) { if (!Users.Contains(t_client)) { BinWriter w = new BinWriter(); w.Write((byte)CHANNEL.INVITE); w.Write((string)name); w.Write(client.Player.GUID); t_client.Send(SMSG.CHANNEL_NOTIFY, w); Join(t_client); } else { BinWriter w = new BinWriter(); w.Write((byte)CHANNEL.ALREADYON); w.Write((string)name); w.Write(t_client.Player.GUID); client.Send(SMSG.CHANNEL_NOTIFY, w); } return(true); } return(false); } catch (Exception e) { Console.WriteLine("Channel Exception (Invite): " + e.Message); return(false); } }
static bool GossipSelectOption(LoginClient client, CMSG msgID, BinReader data) { ulong vendorGUID = data.ReadUInt64(); uint msgid = data.ReadUInt32(); BinWriter pkg = LoginClient.NewPacket(SMSG.GOSSIP_MESSAGE); string message = "Welcome to World of WoWCraft! Please report bugs on our Home-Page: http://www.worldofwowcraft.com"; string message2 = "Visit our Page for the list of known bugs: http://www.worldofwowcraft.com"; pkg.Write(vendorGUID); // Vendor GUID pkg.Write(msgid); // Message ID pkg.Write((int)1); // Counter pkg.Write((int)1); // Counter Number pkg.Write((int)7); // Message Type if (msgid == 1) { pkg.Write(message); // Message Text 1 } if (msgid == 2) { pkg.Write(message2); // Message Text 2 } pkg.Write(0); // ? client.Send(pkg); return(true); }
static bool PetitionList(LoginClient client, CMSG msgID, BinReader data) { ulong vendorGUID = data.ReadUInt64(); BinWriter pkg = LoginClient.NewPacket(SMSG.PETITION_SHOWLIST); string msg = "This is a test msg for petition"; pkg.Write(vendorGUID); pkg.Write((ulong)client.Character.ObjectId); pkg.Write("TestString"); pkg.Write((byte)1); pkg.Write((byte)1); pkg.Write((byte)1); pkg.Write((byte)1); pkg.Write((int)1); pkg.Write((int)1); pkg.Write((string)"TestGuild"); pkg.Write((int)1); pkg.Write((int)1); pkg.Write((int)1); pkg.Write(msg); client.Send(pkg); Chat.System(client, "Petition Query Working :" + vendorGUID); return(true); }
static bool HandleCharDelete(LoginClient client, CMSG msgID, BinReader data) { uint id = data.ReadUInt32(); if (client.Account.Characters == null) { client.Close(client.Account.Name + " tried to delete a character when there was none on the account."); return(true); } foreach (DBCharacter c in client.Account.Characters) { if (id == c.ObjectId) { try { DataServer.Database.DeleteObject(c); } catch (Exception e) { Console.WriteLine("Deleting character " + c.ObjectId + " failed! " + e.Message); } client.Account.Characters = null; DataServer.Database.FillObjectRelations(client.Account); BinWriter w = LoginClient.NewPacket(SMSG.CHAR_DELETE); w.Write((byte)0x28); client.Send(w); return(true); } } client.Close(client.Account.Name + " tried to delete a character that didn't belong to him."); return(true); }
static void OnLootRelease(WorldClient client, CMSG msgID, BinReader data) { ulong targetguid = (ulong)data.ReadInt64(); BinWriter writer = new BinWriter(); writer.Write(targetguid); writer.Write((byte)0x01); client.Send(SMSG.LOOT_RELEASE_RESPONSE, writer); /* UnitBase target = (UnitBase)ObjectManager.GetWorldObject(OBJECTTYPE.UNIT, targetguid); * if (target == null) * { * Chat.System(client, "Invalid corpsetarget."); * return; * } * * // If no loot left.. Make corpse nonlootable. * if (target.Money == 0) * { * target.DynamicFlags = 0; * target.UpdateData(); * } */ return; }
public virtual void UpdateData(BinWriter w, bool clear, bool isClient) { w.Write((char)0xFF); // UpdateData w.Write(GUID); //w.Write((byte)0); ObjectUpdateManager.WriteDataUpdate(w, m_updateValues, this, clear, isClient); }
internal void CreatePlayerObject(bool isClient) { try { // at enter world DebugLogger.Logger.Log("I have a feeling... that this is whats causing it to not work."); BinWriter w = new BinWriter(); w.Write((byte)1); w.Write((uint)0); if (isClient == false) { w.Write((byte)2); Console.WriteLine("DEBUG: 2"); } else { w.Write((byte)3); Console.WriteLine("DEBUG: 3"); } m_player.AddCreateObject(w, true, true); BinWriter pkg = new BinWriter(); pkg.Write((int)w.BaseStream.Length); pkg.Write(ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length)); Send(SMSG.COMPRESSED_UPDATE_OBJECT, pkg); m_player.updateTime(); } catch (Exception exp) { DebugLogger.Logger.Log("", exp); } }
static bool DelFriend(LoginClient client, CMSG msgID, BinReader data) { // BinWriter FriendStatus=LoginClient.NewPacket(SMSG.FRIEND_STATUS); uint delfriend = data.ReadUInt32(); Console.WriteLine(delfriend); foreach (DBFriendList Friend in client.Character.Friends) { if (Friend.Friend_ID == delfriend) { BinWriter w = LoginClient.NewPacket(SMSG.FRIEND_STATUS); try { DataServer.Database.DeleteObject(Friend); } catch (Exception e) { Console.WriteLine("Deleting Friend failed! " + e.Message); w.Write((char)0x05); w.Write((ulong)delfriend); client.Send(w); } client.Character.Friends = null; DataServer.Database.FillObjectRelations(client.Character); // BinWriter w = LoginClient.NewPacket(SMSG.FRIEND_STATUS)); w.Write((char)0x05); w.Write((ulong)delfriend); client.Send(w); return(true); } } return(true); }
private void write_updatemovement(BinWriter w, bool isClient) { // Separate the code: One for self, one for Other if (!isClient) { w.Write((char)0x70); w.Write((char)0x00); } else { w.Write((byte)0x71); // 0x70 if not for self w.Write((uint)0x2000); // 0x00 if not for self } w.Write((uint)0xB74D85D1); // w.Write(MovementFlags); // w.Write(0); w.WriteVector(Position); w.Write(Facing); w.Write((uint)0); if (!isClient) { // this code is only if creating for self w.Write((float)0); w.Write((float)1.0); w.Write((float)0); w.Write((float)0); } w.Write(WalkSpeed); w.Write(RunningSpeed); w.Write(RunBackSpeed); w.Write(SwimSpeed); w.Write(SwimBackSpeed); w.Write(TurnRate); }
static void OnLoot(WorldClient client, CMSG msgID, BinReader data) { ulong targetguid = (ulong)data.ReadInt64(); UnitBase target = (UnitBase)ObjectManager.GetWorldObject(OBJECTTYPE.UNIT, targetguid); if (target == null) { Chat.System(client, "Invalid corpsetarget."); return; } BinWriter writer = new BinWriter(); writer.Write(targetguid); if (target.LootOwner != client.Player.GUID) { writer.Write((byte)0); } else { writer.Write((byte)0x01); writer.Write((uint)target.Money); writer.Write((byte)0); } client.Send(SMSG.LOOT_RESPONSE, writer); // TODO: Get items. return; }
public LoginClient(Socket sock) : base(sock, 2) { BinWriter w = NewPacket(SMSG.AUTH_CHALLENGE); w.Write(0); Send(w); }
internal void OnEnterWorld() { BinWriter w = LoginClient.NewPacket(SMSG.TUTORIAL_FLAGS); for (int i = 0; i < 8; i++) { w.Write(-1); } Send(w); SendConfigCRC(); // Send initial spells initialSpells(); w = LoginClient.NewPacket(SMSG.ACTION_BUTTONS); w.Write(new byte[0x1E0]); Send(w); Chat.System(this, "Connected to " + LoginServer.ServerName); Chat.System(this, "This server is running WoWDaemon 1.3"); Chat.System(this, "Users Online: " + LoginServer.CurrentUsers + " Total Users Online This Session: " + LoginServer.TopUsers); if (Character.Guild != null) { Chat.GuildSay(0, this, "MOTD: " + Character.Guild.MOTD, CHATMESSAGETYPE.GUILD); } m_worldServer.OnEnterWorld(m_character, m_account.AccessLvl); }
static bool Quest(LoginClient client, CMSG msgID, BinReader data) { BinWriter pkg = LoginClient.NewPacket(SMSG.QUESTGIVER_QUEST_LIST); ulong quester = data.ReadUInt64(); string greets = ("Hy, " + client.Character.Name + ", the " + client.Character.Class + "!"); int lenght = DataServer.Database.GetObjectCount(typeof(DBQuest)); pkg.Write(quester); pkg.Write(greets); pkg.Write((int)5); // Quester Emote Delay (5 is default) pkg.Write((int)3); // Quester Emote pkg.Write((byte)lenght); // Quest Counter int i = 1; while (i <= lenght) { DBQuest quest = null; quest = (DBQuest)DataServer.Database.FindObjectByKey(typeof(DBQuest), i); pkg.Write(i); // Quest ID pkg.Write(quest.RequiredLevel); // Quest Level pkg.Write((int)5); // ?? pkg.Write(quest.Title); // Quest Description i++; } client.Send(pkg); return(true); }
/// <summary> /// Serializes index into binary stream. /// </summary> public void Serialize(BinWriter bw) { WordHolder.Serialize(bw); int senseIndexKeyCount = SenseIndex.Count; bw.WriteInt(senseIndexKeyCount); foreach (var x in SenseIndex) { bw.WriteInt(x.Key); x.Value.Serialize(bw); } int ideoIndexKeyCount = IdeoIndex.Count; bw.WriteInt(ideoIndexKeyCount); foreach (var x in IdeoIndex) { bw.WriteChar(x.Key); x.Value.Serialize(bw); } int pinyinIndexKeyCount = PinyinIndex.Keys.Count; bw.WriteInt(pinyinIndexKeyCount); foreach (var x in PinyinIndex) { bw.WriteString(x.Key); x.Value.Serialize(bw); } }
private ServerPacket MakeCreatePacket(WorldObject obj) { BinWriter w = new BinWriter(); w.Write(0); w.Write((uint)0); w.Write(2); obj.AddCreateObject(w, false, true); int count = 1; if (obj.ObjectType == OBJECTTYPE.PLAYER) { PlayerObject plr = obj as PlayerObject; count += plr.Inventory.AddCreateInventory(w, false); } w.Set(0, count); ServerPacket pkg = new ServerPacket(SMSG.COMPRESSED_UPDATE_OBJECT); byte[] compressed = ZLib.Compress(w.GetBuffer(), 0, (int)w.BaseStream.Length); pkg.Write((int)w.BaseStream.Length); pkg.Write(compressed); pkg.Finish(); return(pkg); }
static bool Zoneupdate(LoginClient client, CMSG msgID, BinReader data) { uint newZone = (uint)data.ReadUInt32(); client.Character.Zone = newZone; client.Character.Dirty = true; DataServer.Database.SaveObject(client.Character); ScriptPacket WorldZoneUpdate = new ScriptPacket(SCRMSG.ZONEUPDATE); WorldZoneUpdate.Write(client.Character.ObjectId); WorldZoneUpdate.Write(newZone); client.WorldConnection.Send(WorldZoneUpdate); if (client.Character.OnFriends != null) { foreach (DBFriendList Friend in client.Character.OnFriends) { LoginClient FriendOnline = LoginServer.GetLoginClientByCharacterID(Friend.Owner_ID); if (FriendOnline != null) { BinWriter flist = LoginClient.NewPacket(SMSG.FRIEND_STATUS); Chat.System(FriendOnline, client.Character.Name + "'s zone updated"); flist.Write((char)0x02); flist.Write((ulong)client.Character.ObjectId); flist.Write((int)newZone); flist.Write((int)client.Character.Level); flist.Write((int)client.Character.Class); client.Send(flist); } } } return(true); }
internal static void RemoveCharacter(LoginClient client) { if (client.Character != null) { m_loginCharacters.Remove(client.Character.ObjectId); if (client.Character.OnFriends != null) { BinWriter flist = null; LoginClient FriendOnline = null; foreach (DBFriendList Friend in client.Character.OnFriends) { flist = LoginClient.NewPacket(SMSG.FRIEND_STATUS); FriendOnline = LoginServer.GetLoginClientByCharacterID(Friend.Owner_ID); if (FriendOnline != null) { // Chat.System(FriendOnline, client.Character.Name+" has Gone Offline"); flist.Write((char)0x03); flist.Write((ulong)client.Character.ObjectId); FriendOnline.Send(flist); } FriendOnline = null; flist = null; } } client.Character = null; } }
public static void FriendIsOnline(LoginClient client) { LoginClient FriendOnline = null; BinWriter flist = null; if (client.Character.OnFriends != null) { foreach (DBFriendList Friend in client.Character.OnFriends) { FriendOnline = LoginServer.GetLoginClientByCharacterID(Friend.Owner_ID); if (FriendOnline != null) { flist = LoginClient.NewPacket(SMSG.FRIEND_STATUS); // Chat.System(FriendOnline, client.Character.Name+" is Online"); flist.Write((char)0x02); flist.Write((ulong)client.Character.ObjectId); flist.Write((int)client.Character.Zone); flist.Write((int)client.Character.Level); flist.Write((int)client.Character.Class); FriendOnline.Send(flist); } FriendOnline = null; flist = null; } } }
static bool TradeGold(LoginClient client, CMSG msgID, BinReader data) { uint gold = data.ReadUInt32(); uint inviteeGUID = client.Character.LastTradeID; BinWriter pkg = LoginClient.NewPacket(SMSG.TRADE_STATUS_EXTENDED); pkg.Write((byte)0); // 0 for giving, 1 for recieving pkg.Write((int)1); // Message Count pkg.Write(gold); // Money Amount pkg.Write((int)0); // ? pkg.Write((int)0); // ? client.Send(pkg); client.Character.TradeMoney = gold; LoginClient invitee = LoginServer.GetLoginClientByCharacterID((uint)inviteeGUID); BinWriter pkg2 = LoginClient.NewPacket(SMSG.TRADE_STATUS_EXTENDED); pkg2.Write((byte)1); // 0 for giving, 1 for recieving pkg2.Write((int)1); // Message Count pkg2.Write(gold); // Money Amount pkg2.Write((int)0); // ? pkg2.Write((int)0); // ? invitee.Send(pkg2); invitee.Character.TradeCompleted = false; return(true); }
public static void SendWhoList(LoginClient whoClient) { BinWriter pkg = LoginClient.NewPacket(SMSG.WHO); pkg.Write((int)Instance.ClientCount); pkg.Write((int)Instance.ClientCount); IEnumerator e = Instance.Clients.GetEnumerator(); // int Group = 0; // 0 = No, 1 = Yes while (e.MoveNext()) { try { pkg.Write((string)((LoginClient)e.Current).Character.Name); pkg.Write((string)((LoginClient)e.Current).Character.GuildName); pkg.Write((int)((LoginClient)e.Current).Character.Level); pkg.Write((int)((LoginClient)e.Current).Character.Class); pkg.Write((int)((LoginClient)e.Current).Character.Race); pkg.Write((int)((LoginClient)e.Current).Character.Zone); pkg.Write((int)((LoginClient)e.Current).Character.GroupLook); } catch (Exception) { } } whoClient.Send(pkg); }
static bool TradeInvite(LoginClient client, CMSG msgID, BinReader data) { ulong inviteeGUID = data.ReadUInt64(); uint inviterGUID = client.Character.ObjectId; string inviterNAME = client.Character.Name; LoginClient invitee = LoginServer.GetLoginClientByCharacterID((uint)inviteeGUID); BinWriter pkga1 = LoginClient.NewPacket(SMSG.TRADE_STATUS); pkga1.Write((int)1); pkga1.Write((ulong)inviteeGUID); client.Send(pkga1); BinWriter pkgb1 = LoginClient.NewPacket(SMSG.TRADE_STATUS); pkgb1.Write((int)1); pkgb1.Write((ulong)inviterGUID); invitee.Send(pkgb1); BinWriter pkga2 = LoginClient.NewPacket(SMSG.TRADE_STATUS); pkga2.Write((int)2); client.Send(pkga2); invitee.Send(pkga2); client.Character.LastTradeID = invitee.Character.ObjectId; invitee.Character.LastTradeID = client.Character.ObjectId; return(true); }
void initialSpells() { if ((Character.Spells == null) || (Character.Spells.Length == 0)) { return; } int slotNum = 1; BinWriter w = LoginClient.NewPacket(SMSG.INITIAL_SPELLS); // Unknow byte w.Write((byte)0); // Number of abilites/spells w.Write((ushort)this.Character.Spells.Length); foreach (DBKnownSpell knownSpell in this.Character.Spells) { try { // Spell ID : Defensive Stance w.Write((ushort)knownSpell.Spell_Id); // Slot Number w.Write((short)knownSpell.Slot); slotNum++; } catch (Exception) {} //{Chat.System(client, e.InnerException.Message);} } w.Write((byte)0x00); w.Write((byte)0x00); Send(w); }
/// <summary> /// Serialize to binary stream. /// </summary> public void Serialize(BinWriter bw) { bw.WriteInt(EntryId); bw.WriteInt(SenseIx); int equivTokenCount = EquivTokens.Count; bw.WriteInt(equivTokenCount); for (int i = 0; i != equivTokenCount; ++i) { EquivToken eqt = EquivTokens[i]; if (eqt.RunIx < byte.MinValue || eqt.RunIx > byte.MaxValue) { throw new Exception("RangeIx value out of byte range: " + eqt.StartInRun.ToString()); } if (eqt.StartInRun < short.MinValue || eqt.StartInRun > short.MaxValue) { throw new Exception("StartInSense value out of short range: " + eqt.StartInRun.ToString()); } if (eqt.LengthInRun < short.MinValue || eqt.LengthInRun > short.MaxValue) { throw new Exception("LengthInSense value out of short range: " + eqt.LengthInRun.ToString()); } byte rangeIx = (byte)eqt.RunIx; short startInSense = (short)eqt.StartInRun; short lengthInSense = (short)eqt.LengthInRun; bw.WriteInt(eqt.TokenId); bw.WriteByte(rangeIx); bw.WriteShort(startInSense); bw.WriteShort(lengthInSense); } }
static bool OnGuildQuery(LoginClient client, CMSG msgID, BinReader data) { uint guildId = data.ReadUInt32(); BinWriter pkg = LoginClient.NewPacket(SMSG.GUILD_QUERY_RESPONSE); Chat.System(client, "Guild Query"); DBGuild guild = (DBGuild)DataServer.Database.FindObjectByKey(typeof(DBGuild), guildId); if (guild == null) { return(true); } else { pkg.Write(guild.ObjectId); pkg.Write(guild.Name); for (uint i = 0; i < 10; i++) { pkg.Write(guild.getRankName(i)); } pkg.Write((uint)guild.Icon); pkg.Write((uint)guild.IconColor); pkg.Write((uint)guild.Border); pkg.Write((uint)guild.BorderColor); pkg.Write((uint)guild.Color); } client.Send(pkg); return(true); } //OnGuildQuery
static bool OnGuildInvite(LoginClient client, CMSG msgID, BinReader data) { DBGuild guild = client.Character.Guild; if (guild == null || client.Character.GuildID == 0) { SendResult(client, 2, " ", (int)GUILDRESULT.NOT_IN_GUILD); return(true); } if ((guild.getRankFlags(client.Character.GuildRank) & (uint)GUILDFLAGS.INVITE) != (uint)GUILDFLAGS.INVITE) { SendResult(client, 1, " ", (int)GUILDRESULT.PERMISSIONS); ; return(true); } string name = data.ReadString(); DBCharacter character = (DBCharacter)DataServer.Database.FindObjectByKey(typeof(DBCharacter), name.ToLower()); if (character == null) { SendResult(client, 1, name, (int)GUILDRESULT.NOT_FOUND); } else { LoginClient targetClient = LoginServer.GetLoginClientByCharacterID(character.ObjectId); if (targetClient != null) { // targetClient.Character.LastGuildID=client.Character.GuildID; if (targetClient.Character.LastGuildInviterID != 0) { SendResult(client, 1, name, (int)GUILDRESULT.ALREADY_INVITED_TO_GUILD_S); return(true); } if (targetClient.Character.GuildID != 0) { SendResult(client, 1, targetClient.Character.Name, (int)GUILDRESULT.ALREADY_IN_GUILD_S); return(true); } targetClient.Character.LastGuildInviterID = client.Character.ObjectId; BinWriter gpkg = LoginClient.NewPacket(SMSG.GUILD_INVITE); gpkg.Write(client.Character.Name); gpkg.Write(guild.Name); targetClient.Send(gpkg); SendResult(client, 1, name, (int)GUILDRESULT.SUCCESS); } else { Chat.System(client, name + " is not currently online"); } } return(true); } //OnGuildInvite
/// <summary> /// Serializes data into binary stream. /// </summary> public void Serialize(BinWriter bw) { bw.WriteArray(EntriesNT, (i, bwr) => bwr.WriteInt(i)); bw.WriteArray(Entries0, (i, bwr) => bwr.WriteInt(i)); bw.WriteArray(Entries1, (i, bwr) => bwr.WriteInt(i)); bw.WriteArray(Entries2, (i, bwr) => bwr.WriteInt(i)); bw.WriteArray(Entries3, (i, bwr) => bwr.WriteInt(i)); bw.WriteArray(Entries4, (i, bwr) => bwr.WriteInt(i)); }
/// <summary> /// Serialize to binary stream. /// </summary> public void Serialize(BinWriter bw) { bw.WriteInt(tokenToIdMap.Count); foreach (var x in tokenToIdMap) { bw.WriteString(x.Key); bw.WriteInt(x.Value); } }
/// <summary> /// Writes parsed and indexed dictionary to compiled binary file. /// </summary> public void WriteResults(DateTime date, string dictFileName, string statsFolder) { // Cannot do this twice: we'll have replaced entry IDs with file positions in index if (resultsWritten) throw new InvalidOperationException("WriteResults already called."); resultsWritten = true; // First, statistics stats.WriteStats(statsFolder); // ID to file position Dictionary<int, int> entryIdToPos = new Dictionary<int, int>(); Dictionary<int, int> senseIdToPos = new Dictionary<int, int>(); using (BinWriter bw = new BinWriter(dictFileName)) { // Write date and entry count bw.WriteLong(date.Ticks); bw.WriteInt(entries.Count); int returnPos = bw.Position; // Placeholder: will return here to save start position of index at end bw.WriteInt(-1); // Serialize all entries; fill entry ID -> file pos map for (int i = 0; i != entries.Count; ++i) { entryIdToPos[i] = bw.Position; entries[i].Serialize(bw); } // Replace entry IDs with file positions in all tokenized senses for (int i = 0; i != tsenses.Count; ++i) { tsenses[i].EntryId = entryIdToPos[tsenses[i].EntryId]; } // Serialize all tokenized senses; fill sense ID -> file pos map for (int i = 0; i != tsenses.Count; ++i) { senseIdToPos[i] = bw.Position; tsenses[i].Serialize(bw); } // Fill in index start position int idxPos = bw.Position; bw.Position = returnPos; bw.WriteInt(idxPos); bw.Position = idxPos; // Replace IDs with file positions across index foreach (var x in index.IdeoIndex) { replaceIdsWithPositions(x.Value.EntriesHeadwordSimp, entryIdToPos); replaceIdsWithPositions(x.Value.EntriesHeadwordTrad, entryIdToPos); replaceIdsWithPositions(x.Value.EntriesSense, entryIdToPos); } foreach (var x in index.PinyinIndex) { replaceIdsWithPositions(x.Value.EntriesNT, entryIdToPos); replaceIdsWithPositions(x.Value.Entries0, entryIdToPos); replaceIdsWithPositions(x.Value.Entries1, entryIdToPos); replaceIdsWithPositions(x.Value.Entries2, entryIdToPos); replaceIdsWithPositions(x.Value.Entries3, entryIdToPos); replaceIdsWithPositions(x.Value.Entries4, entryIdToPos); } foreach (var x in index.SenseIndex) { List<SenseInfo> instances = x.Value.Instances; for (int i = 0; i != instances.Count; ++i) { SenseInfo senseInfo = instances[i]; senseInfo.TokenizedSenseId = senseIdToPos[senseInfo.TokenizedSenseId]; instances[i] = senseInfo; } } // Serialize index index.Serialize(bw); } }
/// <summary> /// Serializes object into a binary stream. /// </summary> public void Serialize(BinWriter bw) { bw.WriteInt(Instances.Count); foreach (SenseInfo si in Instances) { bw.WriteInt(si.TokenizedSenseId); bw.WriteByte(si.TokensInSense); } }
/// <summary> /// Serialize to binary stream. /// </summary> public void Serialize(BinWriter bw) { bw.WriteInt(EntryId); bw.WriteInt(SenseIx); int equivTokenCount = EquivTokens.Count; bw.WriteInt(equivTokenCount); for (int i = 0; i != equivTokenCount; ++i) { EquivToken eqt = EquivTokens[i]; if (eqt.RunIx < byte.MinValue || eqt.RunIx > byte.MaxValue) throw new Exception("RangeIx value out of byte range: " + eqt.StartInRun.ToString()); if (eqt.StartInRun < short.MinValue || eqt.StartInRun > short.MaxValue) throw new Exception("StartInSense value out of short range: " + eqt.StartInRun.ToString()); if (eqt.LengthInRun < short.MinValue || eqt.LengthInRun > short.MaxValue) throw new Exception("LengthInSense value out of short range: " + eqt.LengthInRun.ToString()); byte rangeIx = (byte)eqt.RunIx; short startInSense = (short)eqt.StartInRun; short lengthInSense = (short)eqt.LengthInRun; bw.WriteInt(eqt.TokenId); bw.WriteByte(rangeIx); bw.WriteShort(startInSense); bw.WriteShort(lengthInSense); } }
/// <summary> /// Serialize to binary stream. /// </summary> public void Serialize(BinWriter bw) { bw.WriteArray(pinyin, (ps, bwr) => ps.Serialize(bwr)); bw.WriteString(ChSimpl); bw.WriteString(ChTrad); bw.WriteArray(senses); bw.WriteArray(hanziPinyinMap, (x, bwr) => bwr.WriteShort(x)); }
/// <summary> /// Serialize into binary stream. /// </summary> public void Serialize(BinWriter bw) { Domain.Serialize(bw); Equiv.Serialize(bw); Note.Serialize(bw); }