Example #1
0
        private OperationResult SaveConfiguration()
        {
            var result = new OperationResult();

            var converterPathMinusFileName    = directoryHelper.GetConverterWorkingDirectory(options.CurrentConverter);
            var absoluteConfigurationFilePath = folderProxy.Combine(converterPathMinusFileName, configurationFileName);

            try
            {
                fileProxy.WriteAllText(absoluteConfigurationFilePath,
                                       outputConfigurationHelper.BuiltOutputString(options.CurrentConverter, new DirectoryHelper()));
                //TODO: Consider encoding problems

                result.LogEntries.Add(new LogEntry("Configuration file saved successfully as ", LogEntrySeverity.Info,
                                                   LogEntrySource.UI, absoluteConfigurationFilePath));
            }
            catch (Exception e)
            {
                result.State = OperationResultState.Error;
                // No permitted to write to folder, ask user for elevated permission
                result.LogEntries.Add(new LogEntry("Failed to save configuration file", LogEntrySeverity.Error,
                                                   LogEntrySource.UI));
                result.LogEntries.Add(new LogEntry(
                                          "It may help running this application with administrator permissions", LogEntrySeverity.Error,
                                          LogEntrySource.UI));
                result.LogEntries.Add(new LogEntry("Internal save error was: " + e.Message, LogEntrySeverity.Error,
                                                   LogEntrySource.UI));
            }

            return(result);
        }
Example #2
0
        public OperationResult Process()
        {
            var result = new OperationResult();
            var task   = Task.FromResult(result);

            // Reading process output syncronously. The async part is already handled by the command
            using (var process = new Process())
            {
                var argument = "\"" + options.CurrentConverter.AbsoluteSourceSaveGame.SelectedValue + "\"";

                process.StartInfo = new ProcessStartInfo
                {
                    FileName               = options.CurrentConverter.AbsoluteConverter.SelectedValue,
                    Arguments              = argument,
                    CreateNoWindow         = true,
                    RedirectStandardError  = true,
                    RedirectStandardInput  = true,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    WorkingDirectory       = directoryHelper.GetConverterWorkingDirectory(options.CurrentConverter)
                };

                var stopwatch = new Stopwatch();
                stopwatch.Start();

                process.Start();

                while (!process.StandardOutput.EndOfStream)
                {
                    result.LogEntries.Add(new LogEntry(process.StandardOutput.ReadLine(), LogEntrySeverity.Info, LogEntrySource.Converter, null));
                }

                process.WaitForExit();

                stopwatch.Stop();

                if (process.ExitCode == 0)
                {
                    // TODO:REMOVE
                    options.WasConversionSuccessful = true;
                    result.LogEntries.Add(new LogEntry("Conversion complete after " + this.BuildTimeSpanString(stopwatch.Elapsed), LogEntrySeverity.Info, LogEntrySource.UI, null));
                }
                else
                {
                    // TODO:REMOVE
                    options.WasConversionSuccessful = false;
                    result.State = OperationResultState.Error;
                    result.LogEntries.Add(new LogEntry("Conversion failed after " + this.BuildTimeSpanString(stopwatch.Elapsed), LogEntrySeverity.Error, LogEntrySource.UI, null));

                    if (this.options.CurrentConverter.Faq != null && this.options.CurrentConverter.Faq.AlternativePaths.Any(p => p.Exists))
                    {
                        result.LogEntries.Add(new LogEntry("Please see the FAQ for suggestions: ", LogEntrySeverity.Error, LogEntrySource.UI, this.options.CurrentConverter.Faq.SelectedValue));

                        if (this.options.CurrentConverter.Log != null)
                        {
                            result.LogEntries.Add(new LogEntry(
                                                      "If that fails, please take a look at the log generated during the conversion process:",
                                                      LogEntrySeverity.Error,
                                                      LogEntrySource.UI,
                                                      this.options.CurrentConverter.Log.SelectedValue
                                                      ));
                        }
                    }
                }
            }

            return(task.Result);
        }
Example #3
0
        public OperationResult MoveModFileAndFolder()
        {
            var operationResult = new OperationResult();

            // NOTE: This method may be V2 specific. I'll have to talk to Idhrendur about the rules for how this is/will be handled in other converters.
            // I'll figure out some generic way of handling any problems related to that when and if it occurs.

            var targetGameModPathItem =
                options.CurrentConverter.RequiredItems.First(i => i.InternalTagName.Equals("targetGameModPath"));
            var desiredFileName =
                folderProxy.GetFileNameWithoutExtension(options.CurrentConverter.AbsoluteSourceSaveGame.SelectedValue) +
                options.CurrentConverter.TargetGame.SaveGameExtension;

            if (options.CurrentConverter.Categories != null)
            {
                var activeConfiguration = options.CurrentConverter.Categories.First(c => c.FriendlyName == "Configuration");
                if (activeConfiguration != null)
                {
                    var outputName = activeConfiguration.Preferences.FirstOrDefault(d => d.Name == "output_name");
                    if (outputName != null)
                    {
                        var grabbedName = outputName.ToString();
                        int index1      = grabbedName.IndexOf("\"");
                        int index2      = grabbedName.LastIndexOf("\"");
                        if (index2 - index1 > 1)
                        {
                            grabbedName     = grabbedName.Substring(index1 + 1, index2 - index1 - 1);
                            desiredFileName = grabbedName;
                        }

                        operationResult.LogEntries.Add(new LogEntry("Output name: " + desiredFileName,
                                                                    LogEntrySeverity.Info, LogEntrySource.UI));
                    }
                }
            }


            // This is savegame normalization. It strips accents in a fashion compatible with the converter.

            var normalizedString = desiredFileName.Normalize(NormalizationForm.FormD);
            var stringBuilder    = new StringBuilder();

            foreach (var c in normalizedString)
            {
                var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
                if (unicodeCategory != UnicodeCategory.NonSpacingMark)
                {
                    stringBuilder.Append(c);
                }
            }
            desiredFileName = stringBuilder.ToString().Normalize(NormalizationForm.FormC);

            // Here we transliterate the name in case Normalize failed to do it's job (cyrillic letters won't normalize at all in frontend, unlike converter)
            // This produces questionmarks for non-normalizable characters, which we replace with zeroes, same as the converter does.

            byte[] unicodeBytes = Encoding.Unicode.GetBytes(desiredFileName);
            byte[] asciiBytes   = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, unicodeBytes);
            desiredFileName = Encoding.ASCII.GetString(asciiBytes);
            desiredFileName = desiredFileName.Replace('?', '0');
            operationResult.LogEntries.Add(new LogEntry("Mod to be copied: ", LogEntrySeverity.Info, LogEntrySource.UI, desiredFileName));

            var translatedSaveGameName = nameTranslator.TranslateName(desiredFileName);

            // Copy the newly created output mod to the target game mod directory.
            // The mod consists of two things: One file and one folder named after the save. Ex: The folder "France144_11_11" and the file "France144_11_11.mod".
            var converterWorkingDirectory = directoryHelper.GetConverterWorkingDirectory(options.CurrentConverter);
            var outputModFolderSourcePath = folderProxy.Combine(converterWorkingDirectory, "output",
                                                                translatedSaveGameName);
            var outputModFileSourcePath = folderProxy.Combine(converterWorkingDirectory, "output",
                                                              (translatedSaveGameName + ".mod"));

            var expectedAbsoluteOutputModFolderTargetPath = folderProxy.Combine(targetGameModPathItem.SelectedValue,
                                                                                translatedSaveGameName);
            var expectedAbsoluteOutputModFileTargetPath = expectedAbsoluteOutputModFolderTargetPath + ".mod";

            var modFileExists   = fileProxy.Exists(expectedAbsoluteOutputModFileTargetPath);
            var modFolderExists = folderProxy.Exists(expectedAbsoluteOutputModFolderTargetPath);

            if (modFileExists || modFolderExists)
            {
                var confirmationMessage =
                    string.Format("One or more parts of the output mod exists in {0} already. Overwrite?",
                                  targetGameModPathItem.SelectedValue);
                var result = messageBoxProxy.Show(confirmationMessage, "Confirmation Required", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.No)
                {
                    operationResult.State = OperationResultState.Warning;
                    return(operationResult);
                }
            }

            try
            {
                fileProxy.Copy(outputModFileSourcePath, expectedAbsoluteOutputModFileTargetPath, true);
                directoryCopyHelper.DirectoryCopy(outputModFolderSourcePath, expectedAbsoluteOutputModFolderTargetPath,
                                                  true, true);

                operationResult.LogEntries.Add(new LogEntry("Mod copied to: ", LogEntrySeverity.Info, LogEntrySource.UI,
                                                            expectedAbsoluteOutputModFolderTargetPath));
            }
            catch (Exception e)
            {
                operationResult.LogEntries.Add(new LogEntry(e.Message, LogEntrySeverity.Error, LogEntrySource.UI));
                operationResult.State = OperationResultState.Error;
            }

            return(operationResult);
        }
Example #4
0
        public OperationResult MoveModFileAndFolder()
        {
            var operationResult = new OperationResult();

            // NOTE: This method may be V2 specific. I'll have to talk to Idhrendur about the rules for how this is/will be handled in other converters.
            // I'll figure out some generic way of handling any problems related to that when and if it occurs.

            var targetGameModPathItem =
                options.CurrentConverter.RequiredItems.First(i => i.InternalTagName.Equals("targetGameModPath"));
            var desiredFileName =
                folderProxy.GetFileNameWithoutExtension(options.CurrentConverter.AbsoluteSourceSaveGame.SelectedValue) +
                options.CurrentConverter.TargetGame.SaveGameExtension;

            var translatedSaveGameName = nameTranslator.TranslateName(desiredFileName);

            // Copy the newly created output mod to the target game mod directory.
            // The mod consists of two things: One file and one folder named after the save. Ex: The folder "France144_11_11" and the file "France144_11_11.mod".
            var converterWorkingDirectory = directoryHelper.GetConverterWorkingDirectory(options.CurrentConverter);
            var outputModFolderSourcePath = folderProxy.Combine(converterWorkingDirectory, "output",
                                                                translatedSaveGameName);
            var outputModFileSourcePath = folderProxy.Combine(converterWorkingDirectory, "output",
                                                              (translatedSaveGameName + ".mod"));

            var expectedAbsoluteOutputModFolderTargetPath = folderProxy.Combine(targetGameModPathItem.SelectedValue,
                                                                                translatedSaveGameName);
            var expectedAbsoluteOutputModFileTargetPath = expectedAbsoluteOutputModFolderTargetPath + ".mod";

            var modFileExists   = fileProxy.Exists(expectedAbsoluteOutputModFileTargetPath);
            var modFolderExists = folderProxy.Exists(expectedAbsoluteOutputModFolderTargetPath);

            if (modFileExists || modFolderExists)
            {
                var confirmationMessage =
                    string.Format("One or more parts of the output mod exists in {0} already. Overwrite?",
                                  targetGameModPathItem.SelectedValue);
                var result = messageBoxProxy.Show(confirmationMessage, "Confirmation Required", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.No)
                {
                    operationResult.State = OperationResultState.Warning;
                    return(operationResult);
                }
            }

            try
            {
                fileProxy.Copy(outputModFileSourcePath, expectedAbsoluteOutputModFileTargetPath, true);
                directoryCopyHelper.DirectoryCopy(outputModFolderSourcePath, expectedAbsoluteOutputModFolderTargetPath,
                                                  true, true);

                operationResult.LogEntries.Add(new LogEntry("Mod copied to: ", LogEntrySeverity.Info, LogEntrySource.UI,
                                                            expectedAbsoluteOutputModFolderTargetPath));
            }
            catch (Exception e)
            {
                operationResult.LogEntries.Add(new LogEntry(e.Message, LogEntrySeverity.Error, LogEntrySource.UI));
                operationResult.State = OperationResultState.Error;
            }

            return(operationResult);
        }