Beispiel #1
0
        public DefFile(string title, string path, bool getTitleFromDtx = true) : base(title, path)
        {
            if (!ProperlyInitialized)
            {
                return;
            }

            foreach (string file in Directory.GetFiles(Path.GetDirectoryName(path), "*.dtx", SearchOption.AllDirectories))
            {
                DTXFile dtxFile = new DTXFile(file, Path.GetDirectoryName(FilePath));
                if (dtxFile.ProperlyInitialized)
                {
                    _dtxFiles.Add(dtxFile);
                }
            }

            if (_dtxFiles.Count == 0)
            {
                Logger.Instance.LogError("No DTX files were found in '" + Path.GetDirectoryName(FilePath) + "'.");
                ProperlyInitialized = false;
                return;
            }

            _dtxFiles.Sort();

            if (getTitleFromDtx)
            {
                if (TryChangeValueForProperty(PROPERTY_TITLE, _dtxFiles[0].Title))
                {
                    Title = _dtxFiles[0].Title;
                    if (!SaveFile())
                    {
                        Logger.Instance.LogError($"Couldn't save file '{FilePath}' after setting title to '{Title}'");
                    }
                }
            }

            SortDTXFileListByLevel();
            string newInfo    = "\r\n";
            Uri    defFileUri = new Uri(FilePath);

            for (int i = 0; i < DTX_LABELS.Length; i++)
            {
                if (_dtxFiles[i] != null)
                {
                    Uri dtxFileUri = new Uri(_dtxFiles[i].FilePath);
                    newInfo += string.Format(PROPERTY_LABEL_PRE + DTX_LABELS[i] + "\r\n", i + 1);
                    newInfo += string.Format(
                        PROPERTY_FILE_PRE + defFileUri.MakeRelativeUri(dtxFileUri).ToString().Replace('/', Path.DirectorySeparatorChar) +
                        "\r\n\r\n", i + 1);
                }
            }

            rawValue += newInfo;
            File.AppendAllText(FilePath, newInfo, Encoding.GetEncoding("shift_jis"));
            _dtxFiles.RemoveAll(file => file == null);

            Logger.Instance.LogInfo("Created new SET.DEF file for '" + Title + "' in '" + Path.GetDirectoryName(path) + "'.");
        }
Beispiel #2
0
        private void InitializeDtxFiles()
        {
            Regex           regex   = new Regex(@"#L\dFILE\s*:?\s*(?<file>[^.]*\.dtx)\s*\n?");
            MatchCollection matches = regex.Matches(rawValue);

            foreach (Match match in matches)
            {
                string file = match.Groups["file"].Value;

                DTXFile dtxFile = new DTXFile(Path.Combine(new [] { Path.GetDirectoryName(FilePath), file }), Path.GetDirectoryName(FilePath));
                if (dtxFile.ProperlyInitialized)
                {
                    _dtxFiles.Add(dtxFile);
                }
            }

            _dtxFiles.Sort();
        }
Beispiel #3
0
        public override void FindProblems(bool autoFix)
        {
            Regex           regex   = new Regex(@"(?<prop>#L(?<num>\d)FILE\s*:?)\s*(?<file>[^.]*\.dtx)");
            MatchCollection matches = regex.Matches(rawValue);

            using (UserPrompt userPrompt = new UserPrompt()) {
                foreach (Match match in matches)
                {
                    string file     = match.Groups["file"].Value;
                    string filePath = Path.Combine(new[] { Path.GetDirectoryName(FilePath), file });

                    if (!File.Exists(filePath))
                    {
                        if (!autoFix)
                        {
                            Logger.Instance.LogWarning($"Couldn't find file '{Path.GetFileName(filePath)}' in '{filePath}'.");
                        }
                        else
                        {
                            // Get all DTX Files in song directory (including subdirectories)
                            List <string> dtxFiles = Directory.GetFiles(Path.GetDirectoryName(FilePath), "*.dtx",
                                                                        SearchOption.AllDirectories).ToList();
                            // Remove all DTX Files which are already initialized (since they were found)
                            dtxFiles.RemoveAll(dtxFilePath => _dtxFiles.Exists(file1 => file1.FilePath == dtxFilePath));

                            if (dtxFiles.Count != 0)
                            {
                                // Make paths relative to Def file.
                                string[] unbindedDtxFiles = dtxFiles.Select(fullPath =>
                                                                            fullPath.Replace(Path.GetDirectoryName(FilePath) + "\\", "")).ToArray();

                                string propertyName = match.Groups["prop"].Value;
                                string prompt       = $"\n\nPlease select appropiate file for '{propertyName}' property in song '{Title}':";

                                int fileIndex = userPrompt.PromptUserForChoice(prompt, unbindedDtxFiles, file, true, false);

                                // If user chose to leave the value as it already was
                                if (fileIndex == unbindedDtxFiles.Length)
                                {
                                    continue;
                                }

                                if (TryChangeValueForProperty(propertyName, unbindedDtxFiles[fileIndex]))
                                {
                                    DTXFile dtxFile = new DTXFile(Path.Combine(Path.GetDirectoryName(FilePath),
                                                                               unbindedDtxFiles[fileIndex]), Path.GetDirectoryName(FilePath));

                                    if (dtxFile.ProperlyInitialized)
                                    {
                                        _dtxFiles.Add(dtxFile);
                                        SaveFile();
                                        Logger.Instance.LogInfo(
                                            $"Changed property '{propertyName}' from '{file}' to '{unbindedDtxFiles[fileIndex]}' in file '{FilePath}'");
                                    }
                                }
                                else
                                {
                                    Logger.Instance.LogError(
                                        $"Couldn't change property '{propertyName}' from '{file}' to '{unbindedDtxFiles[fileIndex]}' in file {Title}.");
                                }
                            }
                            else
                            {
                                int numToDelete = int.Parse(match.Groups["num"].Value);
                                DeleteDtxFromDefinition(numToDelete);
                            }
                        }
                    }
                }
            }

            foreach (DTXFile dtxFile in _dtxFiles)
            {
                dtxFile.FindProblems(autoFix);
            }
        }