Beispiel #1
0
        private static int MainInner(string[] args)
        {
            var argsFile = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? "", "Arguments.txt");

            if (File.Exists(argsFile))
            {
                args = File.ReadAllLines(argsFile).Concat(args).ToArray();
            }

            var options = new Options();

            if (!Parser.Default.ParseArguments(args, options) || options.Help)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                var form = new Form {
                    Width           = 640,
                    Height          = 480,
                    FormBorderStyle = FormBorderStyle.FixedToolWindow,
                    StartPosition   = FormStartPosition.CenterScreen
                };

                var message = new TextBox {
                    Multiline   = true,
                    ReadOnly    = true,
                    BackColor   = Control.DefaultBackColor,
                    BorderStyle = BorderStyle.None,
                    Text        = options.GetUsage(),
                    Location    = new Point(20, 8),
                    Size        = new Size(form.ClientSize.Width - 40, form.ClientSize.Height - 16),
                    Padding     = new Padding(20),
                    Font        = new Font("Consolas", 9f),
                    TabStop     = false
                };

                var button = new Button {
                    Text     = "OK",
                    Location = new Point(form.ClientSize.Width / 2 - 80, form.ClientSize.Height - 40),
                    Size     = new Size(160, 32)
                };

                button.Click += (sender, eventArgs) => form.Close();

                form.Controls.Add(button);
                form.Controls.Add(message);
                form.ShowDialog();

                return(1);
            }

            var filename = Assembly.GetEntryAssembly().Location;

            if (options.Verbose || filename?.IndexOf("log", StringComparison.OrdinalIgnoreCase) != -1 ||
                filename.IndexOf("debug", StringComparison.OrdinalIgnoreCase) != -1)
            {
                var log = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? "", "Log.txt");
                try {
                    File.WriteAllBytes(log, new byte[0]);
                    RenderLogging.Initialize(log);
                } catch (Exception e) {
                    MessageBox.Show("Can’t setup logging: " + e, @"Oops!", MessageBoxButtons.OK);
                }
            }

            var inputItems = options.Items;

#if DEBUG
            inputItems             = inputItems.Any() ? inputItems : new[] { DebugHelper.GetCarKn5(), DebugHelper.GetShowroomKn5() };
            options.MagickOverride = true;
#endif

            if (inputItems.Count == 0)
            {
                var dialog = new OpenFileDialog {
                    Title  = @"Select KN5",
                    Filter = @"KN5 Files (*.kn5)|*.kn5"
                };
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return(2);
                }

                inputItems = new[] { dialog.FileName };
            }

            var kn5File = inputItems.ElementAtOrDefault(0);
            if (kn5File == null || !File.Exists(kn5File))
            {
                MessageBox.Show(@"File is missing", @"Custom Showroom", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(3);
            }

            if (options.Mode == Mode.UpdateAmbientShadows)
            {
                MessageBox.Show("Started");
                var sw = Stopwatch.StartNew();
                UpdateAmbientShadows(kn5File);
                MessageBox.Show($@"Time taken: {sw.Elapsed.TotalSeconds:F2}s");
                return(0);
            }

            if (options.Mode == Mode.ExtractUv)
            {
                if (string.IsNullOrWhiteSpace(options.ExtractUvTexture))
                {
                    MessageBox.Show(@"Texture to extract is not specified", @"Custom Showroom", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return(4);
                }
                ExtractUv(kn5File, options.ExtractUvTexture);
                return(0);
            }

            var showroomKn5File = inputItems.ElementAtOrDefault(1);
            if (showroomKn5File == null && options.ShowroomId != null)
            {
                showroomKn5File = Path.Combine(
                    Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(kn5File))) ?? "",
                    "showroom", options.ShowroomId, options.ShowroomId + ".kn5");
            }

            if (!File.Exists(showroomKn5File))
            {
                showroomKn5File = null;
            }

            if (options.Mode == Mode.Lite)
            {
                using (var renderer = new ToolsKn5ObjectRenderer(new CarDescription(kn5File))) {
                    renderer.UseMsaa        = options.UseMsaa;
                    renderer.UseFxaa        = options.UseFxaa;
                    renderer.UseSsaa        = options.UseSsaa;
                    renderer.MagickOverride = options.MagickOverride;
                    new LiteShowroomFormWrapper(renderer).Run();
                }
            }
            else if (options.Mode == Mode.Dark)
            {
                using (var renderer = new DarkKn5ObjectRenderer(new CarDescription(kn5File), showroomKn5File)) {
                    // UI
                    renderer.UseSprite = true;
                    renderer.VisibleUi = true;

                    // renderer.FlatMirror = true;
                    renderer.UseMsaa = options.UseMsaa;
                    renderer.UseFxaa = options.UseFxaa;
                    renderer.UseSsaa = options.UseSsaa;

#if DEBUG
                    renderer.AoOpacity = 0.5f;

                    /*renderer.BackgroundColor = Color.Black;
                     * renderer.LightBrightness = 0.2f;
                     * renderer.AmbientBrightness = 0.2f;
                     * /*renderer.BackgroundBrightness = 0.02f;
                     * renderer.FlatMirror = true;*/
                    renderer.FlatMirrorReflectedLight     = true;
                    renderer.TryToGuessCarLightsIfMissing = true;

                    renderer.FlatMirrorBlurred = true;
                    renderer.FlatMirror        = true;

                    //renderer.AddCar(new CarDescription(@"D:\Games\Assetto Corsa\content\cars\ferrari_f40\ferrari_f40.kn5"));
#endif

                    renderer.MagickOverride = options.MagickOverride;
                    new LiteShowroomFormWrapper(renderer)
                    {
                        ReplaceableShowroom = true
                    }.Run(() => {
                        // ReSharper disable once AccessToDisposedClosure
                        var r = renderer;

                        if (r.CarNode != null)
                        {
                            // r.CarNode.AlignWheelsByData = true;
                        }
                    });
                }
            }
            else if (options.Mode == Mode.TrackMap)
            {
                using (var renderer = new TrackMapPreparationRenderer(kn5File)) {
                    renderer.UseFxaa = options.UseFxaa;
                    renderer.SetFilter(new TrackMapRendererFilter());
                    new BaseKn5FormWrapper(renderer, "Track", 800, 800).Run();
                }
            }

            GCHelper.CleanUp();
            return(0);
        }
        public static async Task StartAsync(string kn5, string skinId = null, string presetFilename = null)
        {
            if (_starting)
            {
                return;
            }
            _starting = true;

            await FormWrapperBase.PrepareAsync();

            ForwardKn5ObjectRenderer renderer = null;

            Logging.Write("Custom Showroom: Magick.NET IsSupported=" + ImageUtils.IsMagickSupported);
            RenderLogging.Initialize(Logging.Filename, true);

            try {
                var carDirectory = Path.GetDirectoryName(kn5);
                if (Path.GetFileName(Path.GetDirectoryName(carDirectory)) == @"..")
                {
                    carDirectory = Path.GetDirectoryName(Path.GetDirectoryName(carDirectory));
                }

                var carObject   = CarsManager.Instance.GetById(Path.GetFileName(carDirectory) ?? "");
                var toolboxMode = IsSameDirectories(carObject?.Location, carDirectory);

                LiteShowroomFormWrapper formWrapper;
                using (var waiting = new WaitingDialog()) {
                    waiting.Report(ControlsStrings.CustomShowroom_Loading);

                    var description = new CarDescription(kn5, carDirectory, carObject?.AcdData);
                    if (toolboxMode)
                    {
                        ExtraModelProvider.Initialize();

                        var toolsRenderer = await Task.Run(() => SettingsHolder.CustomShowroom.UseOldLiteShowroom?
                                                           new ToolsKn5ObjectRenderer(description) {
                            UseMsaa      = SettingsHolder.CustomShowroom.LiteUseMsaa,
                            UseFxaa      = SettingsHolder.CustomShowroom.LiteUseFxaa,
                            UseBloom     = SettingsHolder.CustomShowroom.LiteUseBloom,
                            SoundFactory = new AcCarSoundFactory()
                        } :
                                                           new DarkKn5ObjectRenderer(description)
                        {
                            SoundFactory = new AcCarSoundFactory()
                        });

                        formWrapper = new LiteShowroomFormWrapperWithTools(toolsRenderer, carObject, skinId, presetFilename);
                        renderer    = toolsRenderer;
                    }
                    else
                    {
                        Logging.Warning($"Can’t find CarObject for “{carDirectory}”");
                        Logging.Warning($"Found location: “{carObject?.Location ?? @"NULL"}”");

                        renderer = await Task.Run(() => SettingsHolder.CustomShowroom.UseOldLiteShowroom?
                                                  new ForwardKn5ObjectRenderer(description) {
                            UseMsaa  = SettingsHolder.CustomShowroom.LiteUseMsaa,
                            UseFxaa  = SettingsHolder.CustomShowroom.LiteUseFxaa,
                            UseBloom = SettingsHolder.CustomShowroom.LiteUseBloom
                        } :
                                                  new DarkKn5ObjectRenderer(description)
                        {
                            FlatMirror = true,
                            VisibleUi  = true,
                            UseSprite  = true
                        });

                        formWrapper = new LiteShowroomFormWrapper(renderer);
                        if (skinId != null)
                        {
                            renderer.SelectSkin(skinId);
                        }
                    }

                    SetProperties(formWrapper, renderer);
                    formWrapper.Form.Icon = AppIconService.GetAppIcon();
                }

                formWrapper.Run(() => _starting = false);
            } catch (Exception e) {
                NonfatalError.Notify(ControlsStrings.CustomShowroom_CannotStart, e);
            } finally {
                try {
                    renderer?.Dispose();
                } catch (Exception e) {
                    NonfatalError.Notify("Can’t close Custom Showroom", e);
                } finally {
                    _starting = false;
                }
            }
        }