forked from jbevain/PmipMyCallStack
-
Notifications
You must be signed in to change notification settings - Fork 10
/
PmipReader.cs
168 lines (147 loc) · 5.1 KB
/
PmipReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using UnityMixedCallstack;
namespace UnityMixedCallStack
{
public class PmipReader
{
private static List<Range> _rangesSortedByIp = new List<Range>();
private static List<Range> _legacyRanges = new List<Range>();
private static FuzzyRangeComparer _comparer = new FuzzyRangeComparer();
private struct PmipStreams
{
public FileStream fileStream;
public StreamReader fileStreamReader;
public void Dispose()
{
fileStreamReader.Dispose();
fileStreamReader = null;
fileStream.Dispose();
fileStream = null;
}
}
private static Dictionary<string, PmipStreams> _currentFiles = new Dictionary<string, PmipStreams>();
public static void Sort()
{
_legacyRanges.Sort((r1, r2) => r1.Start.CompareTo(r2.Start));
_rangesSortedByIp.Sort((r1, r2) => r1.Start.CompareTo(r2.Start));
}
public static void DisposeStreams()
{
foreach (PmipStreams streams in _currentFiles.Values)
streams.Dispose();
_currentFiles.Clear();
_rangesSortedByIp.Clear();
_legacyRanges.Clear();
}
public static bool ReadPmipFile(string filePath)
{
var _debugPane = UnityMixedCallstackFilter._debugPane;
#if DEBUG
_debugPane?.OutputString("MIXEDCALLSTACK :: Reading pmip file: " + filePath + "\n");
#endif
//DisposeStreams();
if (!_currentFiles.TryGetValue(filePath, out PmipStreams pmipStreams))
{
pmipStreams = new PmipStreams();
try
{
pmipStreams.fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
pmipStreams.fileStreamReader = new StreamReader(pmipStreams.fileStream);
var versionStr = pmipStreams.fileStreamReader.ReadLine();
const char delimiter = ':';
var tokens = versionStr.Split(delimiter);
if (tokens.Length != 2)
throw new Exception("Failed reading input file " + filePath + ": Incorrect format");
if (!double.TryParse(tokens[1], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var version))
throw new Exception("Failed reading input file " + filePath + ": Incorrect version format");
if (version > 2.0)
throw new Exception("Failed reading input file " + filePath + ": A newer version of UnityMixedCallstacks plugin is required to read this file");
}
catch (Exception ex)
{
_debugPane?.OutputString("MIXEDCALLSTACK :: Unable to read dumped pmip file: " + ex.Message + "\n");
DisposeStreams();
return false;
}
_currentFiles.Add(filePath, pmipStreams);
}
try
{
string line;
int count = 0;
int legacyCount = 0;
while ((line = pmipStreams.fileStreamReader.ReadLine()) != null)
{
const char delemiter = ';';
var tokens = line.Split(delemiter);
//should never happen, but lets be safe and not get array out of bounds if it does
if (tokens.Length == 3 || tokens.Length == 4)
{
string startip = tokens[0];
string endip = tokens[1];
string description = tokens[2];
string file = "";
if (tokens.Length == 4)
file = tokens[3];
if (startip.StartsWith("---"))
{
startip = startip.Remove(0, 3);
}
var startiplong = ulong.Parse(startip, NumberStyles.HexNumber);
var endipint = ulong.Parse(endip, NumberStyles.HexNumber);
if (tokens[0].StartsWith("---"))
{
// legacy stored in new pmip file
_legacyRanges.Add(new Range() { Name = description, File = file, Start = startiplong, End = endipint });
legacyCount++;
}
else
{
Range range = new Range() { Name = description, File = file, Start = startiplong, End = endipint };
#if DEBUG
_debugPane?.OutputString($"MIXEDCALLSTACK :: adding range: {range}\n");
#endif
_rangesSortedByIp.Add(range);
count++;
}
}
}
#if DEBUG
if (count > 0 || legacyCount > 0)
_debugPane?.OutputString($"MIXEDCALLSTACK :: added {count} to map for a total of {_rangesSortedByIp.Count} entries! Added {legacyCount} to legacy map for a total of {_legacyRanges.Count} \n");
#endif
}
catch (Exception ex)
{
_debugPane?.OutputString("MIXEDCALLSTACK :: Unable to read dumped pmip file: " + ex.Message + "\n");
DisposeStreams();
return false;
}
return true;
}
public static bool TryGetDescriptionForIp(ulong ip, out string name)
{
name = string.Empty;
//_debugPane?.OutputString("MIXEDCALLSTACK :: Looking for ip: " + String.Format("{0:X}", ip) + "\n");
var rangeToFindIp = new Range() { Start = ip };
var index = _rangesSortedByIp.BinarySearch(rangeToFindIp, _comparer);
if (index >= 0)
{
//_debugPane?.OutputString("MIXEDCALLSTACK :: SUCCESS!!\n");
name = _rangesSortedByIp[index].Name;
return true;
}
index = _legacyRanges.BinarySearch(rangeToFindIp, _comparer);
if (index >= 0)
{
//_debugPane?.OutputString("MIXEDCALLSTACK :: LEGACY SUCCESS!! "+ String.Format("{0:X}", _legacyRanges[index].Start) +" -- "+ String.Format("{0:X}", _legacyRanges[index].End) + "\n");
name = _legacyRanges[index].Name;
return true;
}
return false;
}
}
}