Example #1
0
        private void SaveTrackingFile()
        {
            Logger.DebugLogDebug($"{nameof(SaveTrackingFile)} fired");
            if (!SavePending)
            {
                return;
            }
            if (TrackLastLoadedSceneFile == null)
            {
                throw new NullReferenceException($"{nameof(TrackLastLoadedSceneFile)} should not be null");
            }

            var prefix  = Path.Combine(Paths.CachePath, Path.GetFileName(TrackLastLoadedSceneFile));
            var newFile = string.Concat(prefix, Path.GetRandomFileName());
            var oldFile = string.Concat(prefix, Path.GetRandomFileName());

            var relativeScenes = DictionaryPool <string, string> .Get();

            try
            {
                foreach (var entry in LastLoadedScenes)
                {
                    relativeScenes[SceneRelativePathCache.Get(entry.Key)] =
                        Path.GetFileName(entry.Value);
                }

                lock (SavePendingLock)
                {
                    if (!SavePending)
                    {
                        return;
                    }

                    try
                    {
                        using (var fileStream = new FileStream(newFile, FileMode.Create))
                            using (var streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
                            {
                                streamWriter.Write(GUID);
                                streamWriter.Write(TrackingFileEntrySplit[0]);
                                streamWriter.Write(Version);
                                streamWriter.Write(TrackingFileEntrySplit[0]);
                                streamWriter.Write(relativeScenes.Count);
                                streamWriter.Write(TrackingFileEntrySplit[0]);
                                streamWriter.Write('\n');
                                foreach (var entry in relativeScenes)
                                {
                                    streamWriter.Write(entry.Key);
                                    streamWriter.Write(TrackingFileEntrySplit[0]);
                                    streamWriter.Write(entry.Value);
                                    streamWriter.Write('\n');
                                }
                            }

                        File.Move(TrackLastLoadedSceneFile, oldFile);
                        File.Move(newFile, TrackLastLoadedSceneFile);
                        File.Delete(oldFile);
                        SavePending = false;
                        Logger.DebugLogDebug($"Updated {TrackLastLoadedSceneFile}");
                    }
                    catch (Exception err)
                    {
                        if (!File.Exists(oldFile))
                        {
                            throw;
                        }
                        Logger.LogException(err, this,
                                            $"{nameof(SaveTrackingFile)}: Error encountered, restoring {TrackLastLoadedSceneFile}");

                        File.Copy(oldFile, TrackLastLoadedSceneFile);

                        throw;
                    }
                    finally
                    {
                        if (File.Exists(oldFile))
                        {
                            File.Delete(oldFile);
                        }
                        if (File.Exists(newFile))
                        {
                            File.Delete(newFile);
                        }
                    }
                }
            }
            finally
            {
                DictionaryPool <string, string> .Release(relativeScenes);
            }
        }
Example #2
0
        private void ExecuteDump()
        {
            if (StudioInitObject == null)
            {
                return;
            }
            var scenes = GetListPath();

            if (scenes.Count < 1)
            {
                return;
            }

            var dirName = Path.GetDirectoryName(scenes[0]);

            if (string.IsNullOrEmpty(dirName))
            {
                return;
            }
            Logger.LogInfo("Start dump.");

            var outputFile = Path.GetFullPath(Path.Combine(dirName, "SceneCharaInfo.csv"));

            var append = false;

            _processedScenes.Clear();
            if (File.Exists(outputFile))
            {
                append = true;
                using (var reader = new StreamReader(outputFile, Encoding.UTF8))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        var fPath = line.Split(',').FirstOrDefault()?.Trim();
                        if (string.IsNullOrEmpty(fPath))
                        {
                            continue;
                        }
                        if (fPath.StartsWith($"{DQ}", StringComparison.InvariantCulture) &&
                            fPath.EndsWith($"{DQ}", StringComparison.InvariantCulture))
                        {
                            fPath = fPath.Substring(1, fPath.Length - 2);
                        }

                        _processedScenes.Add(fPath);
                    }
                }
            }

            Logger.LogDebug($"ProcessedScenes: \n\t{string.Join("\n\t", _processedScenes.ToArray())}");

            Logger.LogInfoMessage($"Dumping {scenes.Count} scenes to {outputFile}");

            using (var writer = new StreamWriter(outputFile, append, Encoding.UTF8))
            {
                var line = new List <string>();
                var i    = 0;
                foreach (var pth in scenes)
                {
                    ResetHspe();
                    i++;
                    var displayPath = PrepPath(pth);
                    //writer.Write($"{q}{displayPath}{q}");
                    if (_processedScenes.Contains(displayPath))
                    {
                        continue;
                    }

                    line.Clear();
                    line.Add(displayPath);
                    try
                    {
                        var names = ProcessScene(pth);
                        line.AddRange(names);

                        /*
                         *  foreach (string name in names.Distinct().OrderBy(a => a))
                         *  {
                         *      writer.Write($",{q}{name}{q}");
                         *  }
                         */
                        Logger.LogDebug($"finished {displayPath} ({i}/{scenes.Count})");
                    }
                    catch (Exception err)
                    {
                        //writer.Write($",{q}ERROR PROCESSING FILE{q}");
                        line.Add("ERROR PROCESSING FILE");
                        line.Add($"{err}".Replace(DQ, '\''));
                        Logger.LogException(err, $"${nameof(ExecuteDump)}: error processing {displayPath}");
                    }

                    writer.Write(DQ);
                    try
                    {
                        writer.Write(string.Join($"{DQ},{DQ}", line.ToArray()));
                    }
                    finally
                    {
                        writer.WriteLine(DQ);
                    }

                    _processedScenes.Add(displayPath);
                }
            }

            Logger.LogInfo($"Completed dumping {scenes.Count} scenes to {outputFile}");
            if (_resetHspeWrapper != null)
            {
                Logger.LogWarningMessage("Dump complete. Reset or load new scene before proceeding");
            }

            GeBoAPI.Instance.PlayNotificationSound(NotificationSound.Success);
        }