コード例 #1
0
ファイル: ConsoleCommands.cs プロジェクト: karliky/WDBXEditor
        public static void SqlDumpConCommand(string[] args)
        {
            var    pmap       = ConsoleManager.ParseCommand(args);
            string file       = ParamCheck <string>(pmap, "-f");
            string filename   = Path.GetFileName(file);
            int    build      = ParamCheck <int>(pmap, "-b", false);
            string connection = ParamCheck <string>(pmap, "-c");

            if (build == 0 && !Database.Entries.Any(x => x.FileName.Equals(filename, IGNORECASE)))
            {
                throw new Exception("   File not loaded. Use the 'load' command first.");
            }
            else if (build != 0 && !Database.Entries.Any(x => x.FileName.Equals(filename, IGNORECASE) && x.Build == build))
            {
                throw new Exception("   File not loaded. Use the 'load' command first.");
            }

            var entry = Database.Entries.First(x => x.FileName.Equals(filename, IGNORECASE));

            if (build != 0 && entry.Build != build)
            {
                entry = Database.Entries.First(x => x.FileName.Equals(filename, IGNORECASE) && x.Build == build);
            }

            using (MySqlConnection conn = new MySqlConnection(connection))
            {
                try
                {
                    conn.Open();
                }
                catch { throw new Exception("   Incorrect MySQL login details."); }

                entry.ToSQLTable(connection);

                Console.WriteLine($"Successfully exported to {conn.Database}.");
                Console.WriteLine("");
            }
        }
コード例 #2
0
        /// <summary>
        /// Loads a file into the console
        /// <para>load -f "*.dbc" -s ".mpq/wow dir" -b 11802</para>
        /// </summary>
        /// <param name="args"></param>
        ///
        public static void LoadCommand(string[] args)
        {
            var        pmap      = ConsoleManager.ParseCommand(args);
            string     file      = ParamCheck <string>(pmap, "-f");
            string     filename  = Path.GetFileName(file);
            string     filenoext = Path.GetFileNameWithoutExtension(file);
            string     source    = ParamCheck <string>(pmap, "-s", false);
            int        build     = ParamCheck <int>(pmap, "-b");
            SourceType sType     = GetSourceType(source);

            //Check file exists if loaded from the filesystem
            if (!File.Exists(file) && sType == SourceType.File)
            {
                throw new Exception($"   File not found {file}.");
            }

            //Check the required definition exists
            var def = Database.Definitions.Tables.FirstOrDefault(x => x.Build == build && x.Name.Equals(filenoext, IGNORECASE));

            if (def == null)
            {
                throw new Exception($"   Could not find definition for {Path.GetFileName(file)} build {build}.");
            }

            Database.BuildNumber = build;
            var    dic   = new ConcurrentDictionary <string, MemoryStream>();
            string error = string.Empty;

            switch (sType)
            {
            case SourceType.MPQ:
                Console.WriteLine("Loading from MPQ archive...");
                using (MpqArchive archive = new MpqArchive(source, FileAccess.Read))
                {
                    string line = string.Empty;
                    bool   loop = true;
                    using (MpqFileStream listfile = archive.OpenFile("(listfile)"))
                        using (StreamReader sr = new StreamReader(listfile))
                        {
                            while ((line = sr.ReadLine()) != null && loop)
                            {
                                if (line.EndsWith(filename, IGNORECASE))
                                {
                                    loop = false;
                                    var ms = new MemoryStream();
                                    archive.OpenFile(line).CopyTo(ms);
                                    dic.TryAdd(filename, ms);

                                    error = Database.LoadFiles(dic).Result.FirstOrDefault();
                                }
                            }
                        }
                }
                break;

            case SourceType.CASC:
                Console.WriteLine("Loading from CASC directory...");
                using (var casc = new CASCHandler(source))
                {
                    string fullname = filename;
                    if (!fullname.StartsWith("DBFilesClient", IGNORECASE))
                    {
                        fullname = "DBFilesClient\\" + filename;     //Ensure we have the current file name structure
                    }
                    var stream = casc.ReadFile(fullname);
                    if (stream != null)
                    {
                        dic.TryAdd(filename, stream);

                        error = Database.LoadFiles(dic).Result.FirstOrDefault();
                    }
                }
                break;

            default:
                error = Database.LoadFiles(new string[] { file }).Result.FirstOrDefault();
                break;
            }

            dic.Clear();

            if (!string.IsNullOrWhiteSpace(error))
            {
                throw new Exception("   " + error);
            }

            if (Database.Entries.Count == 0)
            {
                throw new Exception("   File could not be loaded.");
            }

            Console.WriteLine($"{Path.GetFileName(file)} loaded.");
            Console.WriteLine("");
        }
コード例 #3
0
        public static void ExtractCommand(string[] args)
        {
            var        pmap   = ConsoleManager.ParseCommand(args);
            string     filter = ParamCheck <string>(pmap, "-f", false);
            string     source = ParamCheck <string>(pmap, "-s");
            string     output = ParamCheck <string>(pmap, "-o");
            SourceType sType  = GetSourceType(source);

            if (string.IsNullOrWhiteSpace(filter))
            {
                filter = "*";
            }

            string regexfilter            = "(" + Regex.Escape(filter).Replace(@"\*", @".*").Replace(@"\?", ".") + ")";
            Func <string, bool> TypeCheck = t => Path.GetExtension(t).ToLower() == ".dbc" || Path.GetExtension(t).ToLower() == ".db2";


            var dic = new ConcurrentDictionary <string, MemoryStream>();

            switch (sType)
            {
            case SourceType.MPQ:
                Console.WriteLine("Loading from MPQ archive...");
                using (MpqArchive archive = new MpqArchive(source, FileAccess.Read))
                {
                    string line = string.Empty;
                    using (MpqFileStream listfile = archive.OpenFile("(listfile)"))
                        using (StreamReader sr = new StreamReader(listfile))
                        {
                            while ((line = sr.ReadLine()) != null)
                            {
                                if (TypeCheck(line) && Regex.IsMatch(line, regexfilter, RegexOptions.Compiled | RegexOptions.IgnoreCase))
                                {
                                    var ms = new MemoryStream();
                                    archive.OpenFile(line).CopyTo(ms);
                                    dic.TryAdd(Path.GetFileName(line), ms);
                                }
                            }
                        }
                }
                break;

            case SourceType.CASC:
                Console.WriteLine("Loading from CASC directory...");
                using (var casc = new CASCHandler(source))
                {
                    var files = Constants.ClientDBFileNames.Where(x => Regex.IsMatch(Path.GetFileName(x), regexfilter, RegexOptions.Compiled | RegexOptions.IgnoreCase));
                    foreach (var file in files)
                    {
                        var stream = casc.ReadFile(file);
                        if (stream != null)
                        {
                            dic.TryAdd(Path.GetFileName(file), stream);
                        }
                    }
                }
                break;
            }

            if (dic.Count == 0)
            {
                throw new Exception("   No matching files found.");
            }

            if (!Directory.Exists(output))
            {
                Directory.CreateDirectory(output);
            }

            foreach (var d in dic)
            {
                using (var fs = new FileStream(Path.Combine(output, d.Key), FileMode.Create))
                {
                    fs.Write(d.Value.ToArray(), 0, (int)d.Value.Length);
                    fs.Close();
                }
            }

            dic.Clear();

            Console.WriteLine($"   Successfully extracted files.");
            Console.WriteLine("");
        }