|
| 1 | +using KSP.Localization; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace PartInfoInPAW |
| 10 | +{ |
| 11 | + public delegate void PartPatchesHistoryCallback(List<MMPatchInfo> patchesList); |
| 12 | + |
| 13 | + public static class MMLogParser |
| 14 | + { |
| 15 | + public enum ParserStatus |
| 16 | + { |
| 17 | + NotInitialized, |
| 18 | + LogsNotFound, |
| 19 | + LoadingLogFile, |
| 20 | + ReadyToParse, |
| 21 | + ParsingLog, |
| 22 | + LogParsed |
| 23 | + } |
| 24 | + |
| 25 | + private static ParserStatus status = ParserStatus.NotInitialized; |
| 26 | + private static string[] LogFileLines; |
| 27 | + |
| 28 | + private static Dictionary<string, List<MMPatchInfo>> PatchesHistoryDict = new Dictionary<string, List<MMPatchInfo>>(); |
| 29 | + |
| 30 | + public static ParserStatus GetStatus() |
| 31 | + { |
| 32 | + return status; |
| 33 | + } |
| 34 | + |
| 35 | + public static string GetStatusMsg(string partName) |
| 36 | + { |
| 37 | + switch (status) |
| 38 | + { |
| 39 | + case ParserStatus.LogParsed: |
| 40 | + if (PatchesHistoryDict.ContainsKey(partName)) |
| 41 | + { |
| 42 | + return Localizer.Format("#LOC_PartInfoInPAW_PartMMPatchesHistory_PartPatchesMsg", partName, PatchesHistoryDict[partName].Count.ToString()); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + return Localizer.Format("#LOC_PartInfoInPAW_PartMMPatchesHistory_StatusLogParsed"); |
| 47 | + } |
| 48 | + default: |
| 49 | + return ""; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static async Task Initialize() |
| 54 | + { |
| 55 | + if (status != ParserStatus.NotInitialized) return; |
| 56 | + string LogFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs", "ModuleManager", "MMPatch.log"); |
| 57 | + if (!File.Exists(LogFilePath)) |
| 58 | + { |
| 59 | + LogFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs", "ModuleManager", "ModuleManager.log"); |
| 60 | + if (!File.Exists(LogFilePath)) |
| 61 | + { |
| 62 | + status = ParserStatus.LogsNotFound; |
| 63 | + Utils.LogWarning("No Module Manager log file found"); |
| 64 | + return; |
| 65 | + } |
| 66 | + } |
| 67 | + status = ParserStatus.LoadingLogFile; |
| 68 | + Utils.Log($"Reading Module Manager log file from {LogFilePath.Replace('\\', '/')}"); |
| 69 | + await ReadLogFileLinesAsync(LogFilePath); |
| 70 | + } |
| 71 | + |
| 72 | + private static async Task ReadLogFileLinesAsync(string filePath) |
| 73 | + { |
| 74 | + await Task.Run(() => { |
| 75 | + try |
| 76 | + { |
| 77 | + // memory intensive, but fast |
| 78 | + LogFileLines = File.ReadAllLines(filePath, Encoding.UTF8); |
| 79 | + Utils.Log($"Finished reading Module Manager log file from {filePath.Replace('\\', '/')}"); |
| 80 | + status = ParserStatus.ReadyToParse; |
| 81 | + } |
| 82 | + catch (Exception e) |
| 83 | + { |
| 84 | + Utils.LogError($"Could not read file {filePath.Replace('\\', '/')} : " + e.Message); |
| 85 | + Utils.OnScreenMsg(Localizer.Format("#LOC_PartInfoInPAW_CantReadFile_FailureMsg", filePath)); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + public static async Task<List<MMPatchInfo>> ParseLogFile(string partName) |
| 91 | + { |
| 92 | + await Initialize(); |
| 93 | + List<MMPatchInfo> patches = new List<MMPatchInfo>(); |
| 94 | + if ((status != ParserStatus.LogParsed) && (status != ParserStatus.ReadyToParse)) |
| 95 | + { |
| 96 | + return patches; |
| 97 | + } |
| 98 | + if (PatchesHistoryDict.ContainsKey(partName)) |
| 99 | + { |
| 100 | + Utils.LogDebugMsg($"Patches history for part {partName} found in cache"); |
| 101 | + patches = PatchesHistoryDict[partName]; |
| 102 | + status = ParserStatus.LogParsed; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + status = ParserStatus.ParsingLog; |
| 107 | + Utils.LogDebugMsg($"Parsing MM log file for part {partName}"); |
| 108 | + int lineNum = 0; |
| 109 | + int totalLinesCount = LogFileLines.Length; |
| 110 | + if (totalLinesCount > 0) |
| 111 | + { |
| 112 | + string searchStr = "/PART[" + @partName + "]"; |
| 113 | + foreach (string line in LogFileLines) |
| 114 | + { |
| 115 | + if (line.IndexOf(searchStr) != -1) |
| 116 | + { |
| 117 | + Match m = Regex.Match(line.Trim(), RegexPattern(partName)); |
| 118 | + if (m.Success) |
| 119 | + { |
| 120 | + patches.Add(new MMPatchInfo(MMPatchInfo.AddExtension(m.Groups[1].ToString()), m.Groups[2].ToString())); |
| 121 | + Utils.LogDebugMsg($"Found patch for part {partName} in MM log file"); |
| 122 | + } |
| 123 | + } |
| 124 | + lineNum++; |
| 125 | + } |
| 126 | + } |
| 127 | + PatchesHistoryDict.Add(partName, patches); |
| 128 | + Utils.LogDebugMsg($"Finished parsing MM log file for part {partName}"); |
| 129 | + status = ParserStatus.LogParsed; |
| 130 | + } |
| 131 | + return patches; |
| 132 | + } |
| 133 | + |
| 134 | + public static string GetPatchesHistoryAsStr(string partName) |
| 135 | + { |
| 136 | + if (PatchesHistoryDict.ContainsKey(partName)) |
| 137 | + { |
| 138 | + List<MMPatchInfo> patches = PatchesHistoryDict[partName]; |
| 139 | + string result = $"Patches for part {partName}: {patches.Count}" + Environment.NewLine + Environment.NewLine; |
| 140 | + foreach (MMPatchInfo m in patches) |
| 141 | + { |
| 142 | + result += m; |
| 143 | + } |
| 144 | + return result; |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + return $"Patches count for part {partName}: 0" + Environment.NewLine; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private static string RegexPattern(string partName) |
| 153 | + { |
| 154 | + return @"^\[LOG \d{2}:\d{2}:\d{2}\.\d{3}\] Applying update (.+)/(@PART\[.+) to .+/PART\[" + Regex.Escape(partName) + @"\]$"; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public class MMPatchInfo |
| 159 | + { |
| 160 | + public string PatchFilePath { get; protected set; } |
| 161 | + public string Patch { get; protected set; } |
| 162 | + |
| 163 | + public MMPatchInfo(string patchFilePath, string patch) |
| 164 | + { |
| 165 | + PatchFilePath = patchFilePath; |
| 166 | + Patch = patch; |
| 167 | + } |
| 168 | + |
| 169 | + public static string AddExtension(string patch) |
| 170 | + { |
| 171 | + string result = patch; |
| 172 | + if (result[0] == '/') |
| 173 | + { |
| 174 | + result = result.Substring(1); |
| 175 | + } |
| 176 | + result += "." + UrlDir.configExtension; |
| 177 | + return result; |
| 178 | + } |
| 179 | + |
| 180 | + public override string ToString() |
| 181 | + { |
| 182 | + return PatchFilePath + Environment.NewLine + "\t" + Patch + Environment.NewLine; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments