Exemplo n.º 1
0
        private void FixList(List <string> list)
        {
            BackgroundWorker bw = new BackgroundWorker();

            bw.WorkerReportsProgress = true;

            if (cb_wod.Checked)
            {
                list.Add("wod");
            }

            bw.DoWork += new DoWorkEventHandler((sender, e) =>
            {
                List <string> items = e.Argument as List <string>;
                List <ConvertionErrorInfo> errors = new List <ConvertionErrorInfo>();
                int progress = 0;

                bool wod = (items[items.Count - 1] == "wod");
                if (wod)
                {
                    items.RemoveAt(items.Count - 1);
                }

                BackgroundWorker worker = sender as BackgroundWorker;

                foreach (string s in items)
                {
                    IConverter converter = null;

                    if (s.EndsWith("m2"))
                    {
                        converter = new M2Converter(s, helm_fix_cb.Checked);
                    }
                    else if (s.EndsWith("adt"))
                    {
                        converter = new AdtConverter(s, adt_water.Checked, adt_models.Checked);
                    }
                    else if (s.EndsWith("wdt"))
                    {
                        converter = new WDTConverter(s);
                    }
                    else if (Regex.IsMatch(s, @".*_[0-9]{3}(_(lod[0-9]))?\.(wmo)"))
                    {
                        converter = new WMOGroupConverter(s, wod);
                    }
                    else if (s.EndsWith("wmo") && !wod)
                    {
                        converter = new WMORootConverter(s);
                    }
                    else if (s.EndsWith("anim"))
                    {
                        converter = new AnimConverter(s);
                    }

                    // ? -> in case a file with a wrong extension/pattern was in the list
                    try
                    {
                        if (converter?.Fix() ?? false)
                        {
                            converter.Save();
                        }
                    }
                    catch (Exception exception)
                    {
                        errors.Add(new ConvertionErrorInfo(exception, s));
                    }

                    if (++progress == PROGRESS)
                    {
                        worker.ReportProgress(1);
                        progress = 0;
                    }
                }

                e.Result = errors;
            });

            bw.ProgressChanged += new ProgressChangedEventHandler((sender, e) =>
            {
                progress.PerformStep();
                converted++;
            });

            List <ConvertionErrorInfo> error_to_log = new List <ConvertionErrorInfo>();

            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler((sender, e) =>
            {
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.ToString());
                }

                List <ConvertionErrorInfo> errors = e.Result as List <ConvertionErrorInfo>;
                error_to_log.AddRange(errors);

                if ((--threadRemaining) == 0)
                {
                    progress.Value = progress.Maximum;
                    Enabled        = true;
                    Clear();

                    if (error_to_log.Count > 0)
                    {
                        MessageBox.Show(error_to_log.Count + " error(s) while converting files, please send the error.log file to the developper so this issue can be fixed");

                        using (StreamWriter sw = new StreamWriter("error.log", true))
                        {
                            foreach (ConvertionErrorInfo error in error_to_log)
                            {
                                sw.WriteLine("Error fixing: " + error.filename);
                                sw.WriteLine(error.exception.Message);
                                sw.WriteLine(error.exception.StackTrace);
                            }
                        }
                    }
                }
            });

            bw.RunWorkerAsync(list);
        }
Exemplo n.º 2
0
        public void Run()
        {
            if (files_to_convert.Count == 0)
            {
                Console.WriteLine("No files to convert.");
                return;
            }

            Console.WriteLine("Converting {0} files...", files_to_convert.Count);

            int errors = 0;

            using (StreamWriter sw = new StreamWriter("error.log", false))
            {
                foreach (string s in files_to_convert)
                {
                    IConverter converter = null;

                    if (s.EndsWith("m2"))
                    {
                        converter = new M2Converter(s, FixHelm);
                    }
                    else if (s.EndsWith("adt"))
                    {
                        converter = new AdtConverter(s, AdtWater, AdtModels);
                    }
                    else if (s.EndsWith("wdt"))
                    {
                        converter = new WDTConverter(s);
                    }
                    else if (Regex.IsMatch(s, @".*_[0-9]{3}(_(lod[0-9]))?\.(wmo)"))
                    {
                        converter = new WMOGroupConverter(s, false);
                    }
                    else if (s.EndsWith("wmo"))
                    {
                        converter = new WMORootConverter(s);
                    }
                    else if (s.EndsWith("anim"))
                    {
                        converter = new AnimConverter(s);
                    }

                    try
                    {
                        // ? -> in case a file with a wrong extension/pattern was in the list
                        if (converter?.Fix() ?? false)
                        {
                            converter.Save();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error fixing: " + s);
                        sw.WriteLine("Error fixing: " + s);
                        sw.WriteLine(e.Message);
                        sw.WriteLine(e.StackTrace);
                        errors++;
                    }
                }
            }

            files_to_convert.Clear();

            if (errors > 0)
            {
                Console.WriteLine("{0} error(s) while converting files, please send the error.log file to the developper so this issue can be fixed", errors);
            }

            Console.WriteLine("Done.");
        }