Beispiel #1
0
        /// <summary>
        /// Exports a file to either SQL, JSON or CSV
        /// <para>-export -f "*.dbc" -s ".mpq/wow dir" -b 11802 -o "*.sql|*.csv"</para>
        /// </summary>
        /// <param name="args"></param>
        public static void ExportArgCommand(string[] args)
        {
            var        pmap   = ConsoleManager.ParseCommand(args);
            string     output = ParamCheck <string>(pmap, "-o");
            OutputType oType  = GetOutputType(output);

            LoadCommand(args);

            var entry = Database.Entries[0];

            using (FileStream fs = new FileStream(output, FileMode.Create))
            {
                byte[] data = new byte[0];
                switch (oType)
                {
                case OutputType.CSV:
                    data = Encoding.UTF8.GetBytes(entry.ToCSV());
                    break;

                case OutputType.JSON:
                    data = Encoding.UTF8.GetBytes(entry.ToJSON());
                    break;

                case OutputType.SQL:
                    data = Encoding.UTF8.GetBytes(entry.ToSQL());
                    break;
                }

                fs.Write(data, 0, data.Length);

                Console.WriteLine($"Successfully exported to {output}.");
            }
        }
Beispiel #2
0
        public static void ExportConCommand(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     output   = ParamCheck <string>(pmap, "-o");
            OutputType oType    = GetOutputType(output);

            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 (FileStream fs = new FileStream(output, FileMode.Create))
            {
                byte[] data = new byte[0];
                switch (oType)
                {
                case OutputType.CSV:
                    data = Encoding.UTF8.GetBytes(entry.ToCSV());
                    break;

                case OutputType.JSON:
                    data = Encoding.UTF8.GetBytes(entry.ToJSON());
                    break;

                case OutputType.SQL:
                    data = Encoding.UTF8.GetBytes(entry.ToSQL());
                    break;
                }

                fs.Write(data, 0, data.Length);

                Console.WriteLine($"Successfully exported to {output}.");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Imports a csv into a DBC
        /// <para>-import -f "foo.dbc" -b 11802 -c "foo.csv" -h true -u replace -i FixIds</para>
        /// -f name of dbc file
        /// -b build number to use when loading dbc
        /// -c name of csv file
        /// -h sets whether csv has header row
        /// -u updateMode (0:Insert|1:Update|2:Replace)
        /// -i idImportMode (1:FixIds|2:TakeNewest)
        /// </summary>
        /// <param name="args"></param>
        public static void ImportArgCommand(string[] args)
        {
            LoadCommand(args);

            var pmap        = ConsoleManager.ParseCommand(args);
            var csvFileName = ParamCheck <string>(pmap, "-c");
            var hasHeader   = ParamCheck <bool>(pmap, "-h", false);
            var updateMode  = ParamCheck <UpdateMode>(pmap, "-u");
            var importMode  = ParamCheck <ImportFlags>(pmap, "-i");

            var entry = Database.Entries[0];

            if (!entry.ImportCSV(csvFileName, hasHeader, updateMode, out var importError, importMode))
            {
                var dbcFileName = ParamCheck <string>(pmap, "-f");
                throw new Exception($"   Error importing {csvFileName} into {dbcFileName}: {importError}");
            }

            new DBReader().Write(entry, entry.SavePath);
        }
Beispiel #4
0
        /// <summary>
        /// Exports a file directly into a SQL database
        /// <para>-sqldump -f "*.dbc" -s ".mpq/wow dir" -b 11802 -c "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"</para>
        /// </summary>
        /// <param name="args"></param>
        public static void SqlDumpArgCommand(string[] args)
        {
            var    pmap       = ConsoleManager.ParseCommand(args);
            string connection = ParamCheck <string>(pmap, "-c");

            LoadCommand(args);

            var entry = Database.Entries[0];

            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}.");
            }
        }
Beispiel #5
0
        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("");
            }
        }
Beispiel #6
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("");
        }
Beispiel #7
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("");
        }