Created
January 10, 2013 19:41
-
-
Save DDuarte/4505135 to your computer and use it in GitHub Desktop.
#2.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| using MySql.Data.MySqlClient; | |
| using Wintellect.PowerCollections; | |
| using System.Linq; | |
| using System.Linq.Expressions; | |
| using MoreLinq; | |
| namespace CreatureAddonConverterTDB | |
| { | |
| public struct Addon | |
| { | |
| public uint PathId; | |
| public uint Mount; | |
| public uint Bytes1; | |
| public uint Bytes2; | |
| public uint Emote; | |
| public string Auras; | |
| public static bool Equals(Addon x, Addon y) | |
| { | |
| return x.PathId == y.PathId && | |
| x.Mount == y.Mount && | |
| x.Bytes1 == y.Bytes1 && | |
| x.Bytes2 == y.Bytes2 && | |
| x.Emote == y.Emote && | |
| x.Auras == y.Auras; | |
| } | |
| public bool Equals(Addon other) | |
| { | |
| return PathId == other.PathId && | |
| Mount == other.Mount && | |
| Bytes1 == other.Bytes1 && | |
| Bytes2 == other.Bytes2 && | |
| Emote == other.Emote && | |
| Auras == other.Auras; | |
| } | |
| public override int GetHashCode() | |
| { | |
| // ReSharper disable NonReadonlyFieldInGetHashCode | |
| return PathId.GetHashCode() + | |
| Mount.GetHashCode() + | |
| Bytes1.GetHashCode() + | |
| Bytes2.GetHashCode() + | |
| Emote.GetHashCode() + | |
| Auras.GetHashCode(); | |
| // ReSharper restore NonReadonlyFieldInGetHashCode | |
| } | |
| } | |
| public class Program | |
| { | |
| static void Main() | |
| { | |
| const string cs = @"server=localhost;userid=root;password=root;database=world"; | |
| MySqlConnection conn = null; | |
| MySqlDataReader rdr = null; | |
| var guidEntryMap = new Dictionary<uint, uint>(); | |
| var creatureAddons = new Dictionary<uint, Addon>(); | |
| var creatureTemplateAddons = new Dictionary<uint, Addon>(); | |
| var guidsToDelete = new List<uint>(); | |
| var creatureTemplateAddonToAdd = new Dictionary<uint, Addon>(); | |
| try | |
| { | |
| conn = new MySqlConnection(cs); | |
| conn.Open(); | |
| // guid, id | |
| const string stm1 = "SELECT `guid`, `id` FROM `creature`"; | |
| var cmd1 = new MySqlCommand(stm1, conn); | |
| rdr = cmd1.ExecuteReader(); | |
| while (rdr.Read()) | |
| guidEntryMap.Add(rdr.GetUInt32("guid"), rdr.GetUInt32("id")); | |
| // creature_addon | |
| const string stm2 = "SELECT `guid`, `path_id`, `mount`, `bytes1`, `bytes2`, `emote`, `auras` FROM `creature_addon`"; | |
| rdr.Close(); | |
| var cmd2 = new MySqlCommand(stm2, conn); | |
| rdr = cmd2.ExecuteReader(); | |
| while (rdr.Read()) | |
| { | |
| creatureAddons.Add(rdr.GetUInt32("guid"), new Addon | |
| { | |
| PathId = rdr.GetUInt32("path_id"), | |
| Mount = rdr.GetUInt32("mount"), | |
| Bytes1 = rdr.GetUInt32("bytes1"), | |
| Bytes2 = rdr.GetUInt32("bytes2"), | |
| Emote = rdr.GetUInt32("emote"), | |
| Auras = rdr.IsDBNull(rdr.GetOrdinal("auras")) ? string.Empty : rdr.GetString("auras") | |
| }); | |
| } | |
| // creature_template_addon | |
| const string stm3 = "SELECT `entry`, `path_id`, `mount`, `bytes1`, `bytes2`, `emote`, `auras` FROM `creature_template_addon`"; | |
| rdr.Close(); | |
| var cmd3 = new MySqlCommand(stm3, conn); | |
| rdr = cmd3.ExecuteReader(); | |
| while (rdr.Read()) | |
| { | |
| creatureTemplateAddons.Add(rdr.GetUInt32("entry"), new Addon | |
| { | |
| PathId = rdr.GetUInt32("path_id"), | |
| Mount = rdr.GetUInt32("mount"), | |
| Bytes1 = rdr.GetUInt32("bytes1"), | |
| Bytes2 = rdr.GetUInt32("bytes2"), | |
| Emote = rdr.GetUInt32("emote"), | |
| Auras = rdr.IsDBNull(rdr.GetOrdinal("auras")) ? string.Empty : rdr.GetString("auras") | |
| }); | |
| } | |
| } | |
| catch (MySqlException ex) | |
| { | |
| Console.WriteLine("Error: {0}", ex); | |
| return; | |
| } | |
| finally | |
| { | |
| if (rdr != null) | |
| rdr.Close(); | |
| if (conn != null) | |
| conn.Close(); | |
| } | |
| var entryToGuidsMap = new MultiDictionary<uint, uint>(false); | |
| foreach (var pair in guidEntryMap) | |
| entryToGuidsMap.Add(pair.Value, pair.Key); | |
| var addonByEntryMap = new MultiDictionary<uint, KeyValuePair<uint, Addon>>(false); | |
| foreach (KeyValuePair<uint, ICollection<uint>> pair in entryToGuidsMap) | |
| { | |
| foreach (uint u in pair.Value.Where(creatureAddons.ContainsKey)) | |
| { | |
| addonByEntryMap.Add(pair.Key, new KeyValuePair<uint, Addon>(u, creatureAddons[u])); | |
| } | |
| } | |
| foreach (KeyValuePair<uint, ICollection<KeyValuePair<uint, Addon>>> pair in addonByEntryMap) | |
| { | |
| var list = pair.Value.DistinctBy(x => x.Value); | |
| var keyValuePairs = list as IList<KeyValuePair<uint, Addon>> ?? list.ToList(); | |
| if (pair.Value.Count > 1 && keyValuePairs.Count() == 1) | |
| { | |
| if (!creatureTemplateAddons.ContainsKey(pair.Key)) | |
| { | |
| creatureTemplateAddonToAdd.Add(pair.Key, keyValuePairs.First().Value); | |
| guidsToDelete.AddRange(pair.Value.Select(a => a.Key)); | |
| } | |
| else | |
| { | |
| guidsToDelete.AddRange(pair.Value.Select(a => a.Key)); | |
| if (!creatureTemplateAddons[pair.Key].Equals(keyValuePairs.First().Value)) | |
| creatureTemplateAddonToAdd.Add(pair.Key, keyValuePairs.First().Value); | |
| } | |
| } | |
| } | |
| var sb = new StringBuilder("DELETE FROM `creature_addon` WHERE `guid` IN ("); | |
| foreach (var u in guidsToDelete) | |
| sb.AppendFormat("{0}, ", u); | |
| sb.Remove(sb.Length - 2, 2); | |
| sb.Append(");\n"); | |
| sb.Append("DELETE FROM `creature_template_addon` WHERE `entry` IN ("); | |
| foreach (var p in creatureTemplateAddonToAdd) | |
| sb.AppendFormat("{0}, ", p.Key); | |
| sb.Remove(sb.Length - 2, 2); | |
| sb.Append(");\n"); | |
| sb.AppendLine("INSERT INTO `creature_template_addon` (`entry`, `path_id`, `mount`, `bytes1`, `bytes2`, `emote`, `auras`) VALUES"); | |
| foreach (var p in creatureTemplateAddonToAdd) | |
| { | |
| sb.Append("("); | |
| sb.AppendFormat("{0}, {1}, {2}, {3}, {4}, {5}, '{6}'", | |
| p.Key, p.Value.PathId, | |
| p.Value.Mount, p.Value.Bytes1, | |
| p.Value.Bytes2, p.Value.Emote, p.Value.Auras); | |
| sb.Append("),\n"); | |
| } | |
| sb.Remove(sb.Length - 2, 2); | |
| sb.Append(";\n"); | |
| Console.WriteLine(sb); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment