public void Import(string path, bool silent = false) { DbgFileStamp = File.GetLastWriteTime(path); string[] fileRows = File.ReadAllLines(path); string basePath = Path.GetDirectoryName(path); DbgPath = basePath; foreach (string row in fileRows) { try { if (LoadLines(row) || LoadSpans(row) || LoadSymbols(row) || LoadCSymbols(row) || LoadScopes(row) || LoadFiles(row, basePath) || LoadSegments(row)) { continue; } } catch { _errorCount++; } } LoadFileData(basePath); BuildCdlData(); foreach (LineInfo line in _lines.Values) { foreach (int spanID in line.SpanIDs) { SpanInfo span; if (_spans.TryGetValue(spanID, out span)) { SegmentInfo segment; if (_segments.TryGetValue(span.SegmentID, out segment) && !segment.IsRam) { for (int i = 0; i < span.Size; i++) { int prgAddress = segment.FileOffset - _headerSize + span.Offset + i; LineInfo existingLine; if (_linesByPrgAddress.TryGetValue(prgAddress, out existingLine) && existingLine.Type == LineType.External) { //Give priority to lines that come from C files continue; } _linesByPrgAddress[prgAddress] = line; if (i == 0 && spanID == line.SpanIDs[0]) { //Mark the first byte of the first span representing this line as the PRG address for this line of code FileInfo file = _files[line.FileID]; _prgAddressByLine[file.ID.ToString() + "_" + line.LineNumber.ToString()] = prgAddress; } } } } } } LoadLabels(); int labelCount = 0; DbgIntegrationConfig config = ConfigManager.Config.Debug.DbgIntegration; if (config.ImportCpuComments || config.ImportSpcComments) { LoadComments(); } List <CodeLabel> labels = new List <CodeLabel>(_romLabels.Count + _ramLabels.Count + _workRamLabels.Count + _saveRamLabels.Count); if (config.ImportCpuPrgRomLabels) { labels.AddRange(_romLabels.Values); labelCount += _romLabels.Count; } if (config.ImportCpuWorkRamLabels) { labels.AddRange(_workRamLabels.Values); labelCount += _workRamLabels.Count; } if (config.ImportCpuSaveRamLabels) { labels.AddRange(_saveRamLabels.Values); labelCount += _saveRamLabels.Count; } if (config.ImportSpcRamLabels) { labels.AddRange(_spcRamLabels.Values); labelCount += _spcRamLabels.Count; } if (ConfigManager.Config.Debug.DbgIntegration.ResetLabelsOnImport) { DebugWorkspaceManager.ResetLabels(); } LabelManager.SetLabels(labels, true); if (!silent) { if (_errorCount > 0) { _errorCount -= _filesNotFound.Count; string message = $"Import completed with {labelCount} labels imported"; if (_errorCount > 0) { message += $"and {_errorCount} errors - please file a bug report and attach the DBG file you tried to import."; } if (_filesNotFound.Count > 0) { message += Environment.NewLine + Environment.NewLine + "The following files could not be found:"; foreach (string file in _filesNotFound) { message += Environment.NewLine + file; } } MessageBox.Show(message, "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show($"Import completed with {labelCount} labels imported.", "Mesen", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
private void LoadComments() { DbgIntegrationConfig config = ConfigManager.Config.Debug.DbgIntegration; foreach (KeyValuePair <int, LineInfo> kvp in _lines) { try { LineInfo line = kvp.Value; if (line.SpanIDs.Count == 0) { continue; } SpanInfo span = _spans[line.SpanIDs[0]]; SegmentInfo segment = _segments[span.SegmentID]; if (_files[line.FileID].Data == null) { //File was not found. if (_filesNotFound.Add(_files[line.FileID].Name)) { _errorCount++; } continue; } bool isAsm = _files[line.FileID].IsAssembly; string comment = ""; for (int i = line.LineNumber; i >= 0; i--) { string sourceCodeLine = _files[line.FileID].Data[i]; if (sourceCodeLine.Trim().Length == 0) { //Ignore empty lines continue; } Regex regex; if (i == line.LineNumber) { regex = isAsm ? _asmFirstLineRegex : _cFirstLineRegex; } else { regex = isAsm ? _asmPreviousLinesRegex : _cPreviousLinesRegex; } Match match = regex.Match(sourceCodeLine); if (match.Success) { string matchedComment = match.Groups[1].Value.Replace("\t", " "); if (string.IsNullOrWhiteSpace(comment)) { comment = matchedComment; } else { comment = matchedComment + Environment.NewLine + comment; } } else if (i != line.LineNumber) { break; } } if (comment.Length > 0) { int address = -1; SnesMemoryType?memoryType; if (segment.IsRam) { if (segment.IsSpc) { address = span.Offset + segment.Start; memoryType = SnesMemoryType.SpcRam; } else { GetRamLabelAddressAndType(span.Offset + segment.Start, out address, out memoryType); } } else { address = GetPrgAddress(span); memoryType = SnesMemoryType.PrgRom; } if (memoryType == SnesMemoryType.SpcRam && !config.ImportSpcComments) { continue; } else if (memoryType != SnesMemoryType.SpcRam && !config.ImportCpuComments) { continue; } if (address >= 0 && memoryType != null) { CodeLabel label = this.CreateLabel(address, memoryType.Value, 1); if (label != null) { label.Comment = comment; } } } } catch { _errorCount++; } } }