Skip to content

Commit

Permalink
Merge pull request #11 from GGGuenni/legacy_message_parsing
Browse files Browse the repository at this point in the history
Added parsing for legacy messages
  • Loading branch information
p3t3rix authored Sep 7, 2022
2 parents 7351e4a + ff8dd65 commit 8783ff8
Showing 1 changed file with 89 additions and 4 deletions.
93 changes: 89 additions & 4 deletions src/Map/ProspectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ public ProspectInfo(int x, int z, string message, List<OreOccurence> values)
Z = z;
Values = values;
Message = message;
if (values == null)
if (message != null) // ProspectInfo is legacy style message
{
Values = new List<OreOccurence>();
ParseLegacyMessage();
}
else
{
foreach (var val in values)
_foundOres.Add(val.Name);
}
}

public ProspectInfo(int x, int z, string message)
Expand All @@ -93,9 +98,8 @@ public ProspectInfo(int x, int z, string message)
}
catch (System.Exception)
{
Values.Clear();
// TODO instead of just saving the message we could check every language to parse this message.
// Sadly, applying the cleanup regex makes this message unparsable in the future.
Values = null;
// We just save the message as it will be parsed correctly on the next load anyway.
Message = _cleanupRegex.Replace(message, string.Empty);
}
}
Expand Down Expand Up @@ -165,6 +169,87 @@ private void ParseMessage(string message)
_foundOres.Add(val.Name);
}

/// <summary>
/// Parses the legacy style messages (From VsProspectorInfo versions < 4.0.0).
/// Should only be called on load since it is significantly slower than <see cref="ParseMessage(string)"/>.
/// Checks every language to parse <see cref="Message"/> and sets the field to null afterwards.
/// If no language can be matched to the Message, nothing happens.
/// </summary>
private void ParseLegacyMessage()
{
string[] splits = null;
string langCode = null;

foreach (var language in Lang.AvailableLanguages.Keys)
{
var langRegex = new Regex(Lang.GetL(language, "propick-reading-title", ".*?"));
var langMatch = langRegex.Match(Message);
if (langMatch.Success)
{
splits = Message.Replace(langMatch.Value, string.Empty).Split('\n');
langCode = language;
break;
}
}

if (langCode == null)
return;

var ores = new Dictionary<string, string>();
foreach (var ore in _allOres)
ores[Lang.GetL(langCode, ore.Value)] = ore.Value;

Dictionary<string, RelativeDensity> _relativeDensities = new Dictionary<string, RelativeDensity>{
{ Lang.GetL(langCode, "propick-density-verypoor"), RelativeDensity.VeryPoor },
{ Lang.GetL(langCode, "propick-density-poor"), RelativeDensity.Poor },
{ Lang.GetL(langCode, "propick-density-decent"), RelativeDensity.Decent },
{ Lang.GetL(langCode, "propick-density-high"), RelativeDensity.High },
{ Lang.GetL(langCode, "propick-density-veryhigh"), RelativeDensity.VeryHigh },
{ Lang.GetL(langCode, "propick-density-ultrahigh"), RelativeDensity.UltraHigh }
};

foreach (var reading in splits)
{
RelativeDensity relativeDensity = RelativeDensity.Zero;

foreach (var density in _relativeDensities)
{
if (reading.Contains(density.Key))
{
relativeDensity = density.Value;
break;
}
}

if (relativeDensity != RelativeDensity.Zero)
{
Regex absoluteDensityRegex = new Regex("([0-9]+,?[0-9]*)");
double absoluteDensity = double.Parse(absoluteDensityRegex.Match(reading).Value);

string oreName = null;
foreach (var ore in ores)
if (reading.Contains(ore.Key))
{
oreName = ore.Value;
break;
}

Values.Add(new OreOccurence(oreName, null, relativeDensity, absoluteDensity));
}
else // if it is RelativeDensity.Zero we are checking miniscule amounts
{
foreach (var ore in ores)
if (reading.Contains(ore.Key))
Values.Add(new OreOccurence(ore.Value, null, RelativeDensity.Miniscule, 0));
}
}

foreach (var val in Values)
_foundOres.Add(val.Name);

Message = null;
}

public string GetMessage()
{
if (_messageCache != null)
Expand Down

0 comments on commit 8783ff8

Please sign in to comment.