示例#1
0
        public Dictionary <string, List <string> > RetrieveAllLogs()
        {
            var gameLogLines = new Dictionary <string, List <string> >();

            if (!Directory.Exists(logDirectory))
            {
                return(gameLogLines);
            }

            foreach (
                var file in
                Directory.GetFiles(logDirectory)
                .Where(
                    f =>
                    f != null && Path.GetFileName(f).StartsWith("Journal.") &&
                    Path.GetFileName(f).EndsWith(".log")))
            {
                var fileContents = ReadLinesWithoutLock(file);
                if (fileContents.Item1 == DEFAULT_COMMANDER_NAME)
                {
                    continue;
                }

                if (gameLogLines.ContainsKey(fileContents.Item1))
                {
                    gameLogLines[fileContents.Item1].AddRange(fileContents.Item2);
                }
                else
                {
                    gameLogLines[fileContents.Item1] = fileContents.Item2;
                }
            }

            ManualChangesDirectory = IOUtils.GetManualChangesDirectory();

            var commandersInGame = gameLogLines.Keys.ToHashSet();

            foreach (var file in Directory.GetFiles(ManualChangesDirectory).Where(f => f != null && Path.GetFileName(f).StartsWith("manualChanges.") && f.EndsWith(".json")).ToList())
            {
                var fileName = Path.GetFileName(file);
                var manualChangesCommander = fileName.Substring("manualChanges.".Length);

                string commanderName;

                if (manualChangesCommander == "json") // manualChanges.json
                {
                    commanderName = gameLogLines.Keys.FirstOrDefault(k => k != DEFAULT_COMMANDER_NAME) ?? DEFAULT_COMMANDER_NAME;
                }
                else // manualChanges.Hg.Json
                {
                    commanderName = manualChangesCommander
                                    .Substring(0, manualChangesCommander.Length - ".json".Length)
                                    .Desanitize();
                }

                // if there's commanders found in the logs and none of them corresponds to the current commander's manualChange, then skip it
                if (commandersInGame.Any() && commandersInGame.All(x => x != commanderName))
                {
                    continue;
                }

                var content = File.ReadAllLines(file).ToList();

                if (!gameLogLines.ContainsKey(commanderName))
                {
                    gameLogLines[commanderName] = content;
                }
                else
                {
                    gameLogLines[commanderName].AddRange(content);
                }

                // migrate old manualChanges.json files to new one:
                if (manualChangesCommander == "json")
                {
                    File.Move(file, Path.Combine(ManualChangesDirectory, $"manualChanges.{commanderName.Sanitize()}.json"));
                    File.Delete(file);
                }
            }

            return(gameLogLines);
        }