public void SaveAvatar(Avatar avatar) { var path = Path.Combine(CoCServerPaths.Avatars, avatar.Token); if (!Directory.Exists(path)) Directory.CreateDirectory(path); var village = avatar.Home.ToJson(true); File.WriteAllText(Path.Combine(path, "village.json"), village); var builder = new StringBuilder(); var type = avatar.GetType(); var properties = type.GetProperties(); for (int i = 0; i < properties.Length; i++) { var property = properties[i]; if (property.PropertyType == typeof(Village)) continue; var name = property.Name; var value = property.GetValue(avatar); if (value == null) value = "null"; builder.AppendFormat("{0}={1}\n", name, value); } var savePath = Path.Combine(path, avatar.Name + ".txt"); File.WriteAllText(savePath, builder.ToString()); }
public CoCRemoteClient(CoCServer server, Socket connection, NetworkManagerAsyncSettings settings) { _server = server; Connection = connection; Avatar = new Avatar(); NetworkManager = new NetworkManagerAsync(connection, settings); NetworkManager.MessageReceived += OnMessageReceived; }
public CoCRemoteClient(CoCServer server, Socket connection) { Connection = connection; Server = server; NetworkManager = new NetworkManagerAsync(connection, HandlePacketReceived, HandleReceicedPacketFailed); PacketHandlers = new Dictionary<ushort, PacketHandler>(); Avatar = new Avatar(); Home = new Village(); }
public Avatar CreateNewAvatar(string token, long id) { var vilPath = Path.Combine(CoCServerPaths.Content, "default_village.json"); var avatar = new Avatar(); avatar.ShieldEndTime = DateTime.UtcNow.AddDays(3); avatar.Token = token; avatar.ID = id; avatar.Level = 10; // bypass tut avatar.Home = Village.FromJson(File.ReadAllText(vilPath)); avatar.Name = "Patrik"; // :] LoadedAvatar.Add(avatar.Token, avatar); return avatar; }
public CoCClient() { Fingerprint = new Fingerprint(); Home = new Village(); Avatar = new Avatar(); Connection = new Socket(SocketType.Stream, ProtocolType.Tcp); PacketHandlers = new Dictionary<ushort, PacketHandler>(); KeepAliveManager = new KeepAliveManager(this); PacketLogger = new PacketLogger() { LogConsole = false }; LoginPacketHandlers.RegisterLoginPacketHandlers(this); InGamePacketHandlers.RegisterInGamePacketHandler(this); }
public CoCClient() { Fingerprint = new Fingerprint(); Home = new Village(); Avatar = new Avatar(); Connection = new Socket(SocketType.Stream, ProtocolType.Tcp); DefaultPacketHandlers = new Dictionary<ushort, PacketHandler>(); PacketHandlers = new Dictionary<ushort, PacketHandler>(); KeepAliveManager = new KeepAliveManager(this); PacketLog = new PacketLog("packets.log") { AutoSave = true }; PluginManager = new PluginManager(this); LoginPacketHandlers.RegisterLoginPacketHandlers(this); InGamePacketHandlers.RegisterInGamePacketHandler(this); PluginManager.LoadPlugins(); PluginManager.EnablePlugins(); }
/// <summary> /// Initializes a new instance of the <see cref="AvatarData"/> class from /// the specified <see cref="Avatar"/>. /// </summary> /// <param name="avatar"><see cref="Avatar"/> from which the data will be set.</param> public AvatarData(Avatar avatar) { OwnVillageData = new VillageData() { UserID = avatar.ID, ShieldDuration = avatar.ShieldDuration, Unknown2 = 1200, Unknown3 = 60, Compressed = true, Home = avatar.Home, }; UserID = avatar.ID; UserID2 = avatar.ID; //TODO: Might be something different. if (avatar.Alliance != null) { OwnClanData = new ClanData() { ID = avatar.Alliance.ID, Name = avatar.Alliance.Name, Badge = avatar.Alliance.Badge, Role = avatar.Alliance.Role, Level = avatar.Alliance.Level }; } League = avatar.League; Name = avatar.Name; Unknown12 = -1; Experience = avatar.Experience; Level = avatar.Level; Gems = avatar.Gems; FreeGems = avatar.FreeGems; Unknown13 = 1200; Unknown14 = 60; Trophies = avatar.Trophies; Unknown22 = 1; Unknown23 = 946720861000; IsNamed = avatar.IsNamed; Unknown27 = 1; ResourcesCapacity = new ResourceCapacitySlot[0]; ResourcesAmount = new ResourceAmountSlot[0]; Units = new UnitSlot[0]; Spells = new SpellSlot[0]; UnitUpgrades = new UnitUpgradeSlot[0]; SpellUpgrades = new SpellUpgradeSlot[0]; HeroUpgrades = new HeroUpgradeSlot[0]; HeroHealths = new HeroHealthSlot[0]; HeroStates = new HeroStateSlot[0]; TutorialProgess = new TutorialProgressSlot[0]; Acheivements = new AchievementSlot[0]; AcheivementProgress = new AchievementProgessSlot[0]; NpcStars = new NpcStarSlot[0]; NpcGold = new NpcGoldSlot[0]; NpcElixir = new NpcElixirSlot[0]; }
public void ReadPacket(PacketReader reader) { var offset = 0x2A; LastVisit = TimeSpan.FromSeconds(reader.ReadInt32()); Unknown1 = reader.ReadInt32(); Timestamp = DateTimeConverter.FromUnixTimestamp(reader.ReadInt32()); Unknown2 = reader.ReadInt32(); UserID = reader.ReadInt64(); ShieldDuration = TimeSpan.FromSeconds(reader.ReadInt32()); Unknown3 = reader.ReadInt32(); Unknown4 = reader.ReadInt32(); Compressed = reader.ReadBoolean(); Home = new Village(); Home.Read(reader); Avatar = new Avatar(); // Seems like a whole object Unknown6 = reader.ReadInt32(); UserID1 = reader.ReadInt64(); UserID2 = reader.ReadInt64(); Avatar.ID = UserID1; switch (reader.ReadByte()) { case 0: break; case 1: Avatar.Clan = new Clan(); Avatar.Clan.ID = reader.ReadInt64(); Avatar.Clan.Name = reader.ReadString(); Avatar.Clan.Badge = reader.ReadInt32(); reader.ReadInt32(); Avatar.Clan.Level = reader.ReadInt32(); offset += 1; break; case 2: // clanless but clan castle built? var lel = reader.ReadInt64(); break; } if (Unknown7 = reader.ReadBoolean()) Unknown8 = reader.ReadInt64(); if (Unknown9 = reader.ReadBoolean()) Unknown10 = reader.ReadInt64(); reader.Seek(offset, SeekOrigin.Current); Unknown11 = reader.ReadInt32(); AllianceCastleLevel = reader.ReadInt32(); // -1 if not constructed AllianceCastleUnitCapacity = reader.ReadInt32(); AllianceCastleUnitCount = reader.ReadInt32(); Avatar.TownHallLevel = reader.ReadInt32(); Avatar.Username = reader.ReadString(); FacebookID = reader.ReadString(); Avatar.Level = reader.ReadInt32(); Avatar.Experience = reader.ReadInt32(); Avatar.Gems = reader.ReadInt32(); Gems1 = reader.ReadInt32(); Unknown14 = reader.ReadInt32(); Unknown15 = reader.ReadInt32(); Avatar.Trophies = reader.ReadInt32(); Avatar.AttacksWon = reader.ReadInt32(); Avatar.AttacksLost = reader.ReadInt32(); Avatar.DefencesWon = reader.ReadInt32(); Avatar.DefencesLost = reader.ReadInt32(); Unknown16 = reader.ReadInt32(); Unknown17 = reader.ReadInt32(); Unknown18 = reader.ReadInt32(); if (Unknown19 = reader.ReadBoolean()) Unknown20 = reader.ReadInt64(); Unknown21 = reader.ReadByte(); Unknown22 = reader.ReadInt32(); Unknown23 = reader.ReadInt32(); Unknown24 = reader.ReadInt32(); Unknown25 = reader.ReadInt32(); var count1 = reader.ReadInt32(); for (int i = 0; i < count1; i++) { var id = reader.ReadInt32(); // resource id from resources.csv var capacity = reader.ReadInt32(); } var count2 = reader.ReadInt32(); for (int i = 0; i < count2; i++) { var id = reader.ReadInt32(); // resource id from resources.csv var amount = reader.ReadInt32(); } var count3 = reader.ReadInt32(); for (int i = 0; i < count3; i++) { var id = reader.ReadInt32(); // unit id from characters.csv var amount = reader.ReadInt32(); } var count4 = reader.ReadInt32(); for (int i = 0; i < count4; i++) { var id = reader.ReadInt32(); // spell id from spells.csv var amount = reader.ReadInt32(); } var count5 = reader.ReadInt32(); for (int i = 0; i < count5; i++) { var id = reader.ReadInt32(); // unit id from characters.csv var level = reader.ReadInt32(); } var count6 = reader.ReadInt32(); for (int i = 0; i < count6; i++) { var id = reader.ReadInt32(); // spell id from spells.csv var level = reader.ReadInt32(); } var count7 = reader.ReadInt32(); for (int i = 0; i < count7; i++) { var id = reader.ReadInt32(); // hero id from heros.csv var level = reader.ReadInt32(); } var count8 = reader.ReadInt32(); for (int i = 0; i < count8; i++) { var id = reader.ReadInt32(); // hero id from heros.csv var health = reader.ReadInt32(); } var count9 = reader.ReadInt32(); for (int i = 0; i < count9; i++) { var id = reader.ReadInt32(); // hero id from heros.csv var state = reader.ReadInt32(); } var count10 = reader.ReadInt32(); for (int i = 0; i < count10; i++) { var id = reader.ReadInt32(); // unit id from characters.csv var amount = reader.ReadInt32(); var level = reader.ReadInt32(); } var count11 = reader.ReadInt32(); for (int i = 0; i < count11; i++) { var id = reader.ReadInt32(); // mission id from missions.csv } var count12 = reader.ReadInt32(); for (int i = 0; i < count12; i++) { var id = reader.ReadInt32(); // achievement id from achievements.csv } var count13 = reader.ReadInt32(); for (int i = 0; i < count13; i++) { var id = reader.ReadInt32(); // achievement id from achievements.csv var progress = reader.ReadInt32(); } var count14 = reader.ReadInt32(); for (int i = 0; i < count14; i++) { var id = reader.ReadInt32(); // npc id from npcs.csv var stars = reader.ReadInt32(); } var count15 = reader.ReadInt32(); for (int i = 0; i < count15; i++) { var id = reader.ReadInt32(); // npc id from npcs.csv var gold = reader.ReadInt32(); } var count16 = reader.ReadInt32(); for (int i = 0; i < count16; i++) { var id = reader.ReadInt32(); // npc id from npcs.csv var elixir = reader.ReadInt32(); } Unknown26 = reader.ReadInt32(); Unknown27 = reader.ReadInt32(); Unknown28 = reader.ReadInt32(); }
public AvatarSave(Avatar avatar) { Avatar = avatar; }
public void LoadAllAvatars() { if (!Directory.Exists(CoCServerPaths.Avatars)) { Directory.CreateDirectory(CoCServerPaths.Avatars); return; // exit early cause the file didnt exist } var avatarDirectories = Directory.GetDirectories("Avatars"); for (int i = 0; i < avatarDirectories.Length; i++) { var directory = avatarDirectories[i]; var files = Directory.GetFiles(directory); var homePath = (string)null; var dataPath = (string)null; if (files.Length > 2) Console.WriteLine("WARNING: Found avatar save directory with more than 2 files. {0}", directory); for (int j = 0; j < files.Length; j++) { var file = files[j]; if (Path.GetFileName(file) == "village.json") homePath = file; else if (Path.GetExtension(file) == ".txt") dataPath = file; } if (homePath == null || dataPath == null) { Console.WriteLine("WARNING: Couldn't find a save file. Skipping {0}", directory); continue; } var home = Village.FromJson(File.ReadAllText(homePath)); var avatar = new Avatar(); avatar.Home = home; var type = avatar.GetType(); var saveProperties = File.ReadAllLines(dataPath); for (int j = 0; j < saveProperties.Length; j++) { var saveValues = ParseSaveProperty(saveProperties[j]); if (saveValues[0] == "ShieldDuration") continue; var property = type.GetProperty(saveValues[0]); var value = (object)saveValues[1]; if (saveValues[1] == "null") { value = null; } else if (property.PropertyType == typeof(TimeSpan)) { value = TimeSpan.Parse(saveValues[1]); } else if (property.PropertyType == typeof(DateTime)) { value = DateTime.Parse(saveValues[1]); } else if (property.PropertyType == typeof(int)) { value = int.Parse(saveValues[1]); } else if (property.PropertyType == typeof(long)) { value = long.Parse(saveValues[1]); } else if (property.PropertyType == typeof(bool)) { value = bool.Parse(saveValues[1]); } property.SetValue(avatar, value); } LoadedAvatar.Add(avatar.Token, avatar); } }