Skip to content

Commit 0f2bf9a

Browse files
committed
Added sentry to coreconsole
1 parent b504235 commit 0f2bf9a

File tree

8 files changed

+108
-24
lines changed

8 files changed

+108
-24
lines changed

coreconsole/Program.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.CommandLine;
44
using System.Text.Json;
55
using coreconsole.handlers;
6+
using coreconsole.utils;
67
using PKHeX.Core;
8+
using Sentry;
79
using Version = coreconsole.Models.Version;
810

911
namespace coreconsole;
@@ -12,7 +14,14 @@ public static class MainClass
1214
{
1315
public static void Main(string[] args)
1416
{
15-
var pokemonArg = new Argument<string>(
17+
if(!Helpers.LoadEnv()) return;
18+
19+
using (SentrySdk.Init(o =>
20+
{
21+
o.Dsn = Environment.GetEnvironmentVariable("SENTRY_DSN");
22+
}))
23+
{
24+
var pokemonArg = new Argument<string>(
1625
"pokemon",
1726
"The pokemon file (in base64 format)."
1827
);
@@ -66,6 +75,7 @@ public static void Main(string[] args)
6675
cmd3,
6776
cmd4
6877
};
69-
cli.Invoke(args);
78+
cli.Invoke(args);
79+
}
7080
}
7181
}

coreconsole/coreconsole.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="PKHeX.Core" Version="23.6.3" />
12+
<PackageReference Include="Sentry" Version="3.33.0" />
1213
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
1314
</ItemGroup>
1415

coreconsole/handlers/Legality.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ public static LegalityAnalysis CheckLegality(PKM pokemon)
6565
pkmn.SetTrainerData(info);
6666

6767
// Check if still legal
68-
if (!CheckLegality(pkmn).Valid) return null;
69-
70-
return pkmn;
68+
return !CheckLegality(pkmn).Valid ? null : pkmn;
7169
}
7270

7371
private static SimpleTrainerInfo _GetTrainerInfo(PKM pokemon, GameVersion? version)

coreconsole/models/Legality.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text.Json.Serialization;
22
using PKHeX.Core;
3+
using Sentry;
34

45
namespace coreconsole.Models;
56

@@ -26,9 +27,16 @@ public AutoLegalizationResult(LegalityAnalysis la, PKM? pokemon)
2627
Legal = la.Valid;
2728
Report = la.Report().Split("\n");
2829

29-
if (pokemon != null)
30+
if (pokemon == null) return;
31+
try
3032
{
31-
PokemonBase64 = Convert.ToBase64String(pokemon.SIZE_PARTY > pokemon.SIZE_STORED ? pokemon.DecryptedPartyData : pokemon.DecryptedBoxData);
33+
PokemonBase64 = Convert.ToBase64String(pokemon.SIZE_PARTY > pokemon.SIZE_STORED
34+
? pokemon.DecryptedPartyData
35+
: pokemon.DecryptedBoxData);
36+
}
37+
catch (Exception e)
38+
{
39+
SentrySdk.CaptureException(e);
3240
}
3341
}
3442
}

coreconsole/models/Move.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text.Json.Serialization;
22
using PKHeX.Core;
3+
using Sentry;
34

45
namespace coreconsole.Models;
56

@@ -13,10 +14,17 @@ public struct Move
1314
public Move(ushort id, string name, EntityContext context, int? pp, int? ppUps)
1415
{
1516
Name = id == 0 ? "None" : name;
16-
Enum.TryParse(MoveInfo.GetType(id, context).ToString(), out MoveType type);
17-
18-
Type = id == 0 ? "Normal" : type.ToString();
19-
Pp = pp ?? MoveInfo.GetPP(context, id);
20-
PpUps = ppUps ?? 0;
17+
try
18+
{
19+
Enum.TryParse(MoveInfo.GetType(id, context).ToString(), out MoveType type);
20+
Type = id == 0 ? "Normal" : type.ToString();
21+
Pp = pp ?? MoveInfo.GetPP(context, id);
22+
PpUps = ppUps ?? 0;
23+
}
24+
catch (Exception e)
25+
{
26+
SentrySdk.CaptureException(e);
27+
Type = "Normal";
28+
}
2129
}
2230
}

coreconsole/models/Pokemon.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json.Serialization;
22
using coreconsole.handlers;
33
using PKHeX.Core;
4+
using Sentry;
45

56
namespace coreconsole.Models;
67

@@ -93,16 +94,41 @@ public Pokemon(PKM pkmn, EntitySummary summary)
9394
Ribbons = tmpRibbons;
9495
MetData = new MetData(summary.MetLoc, summary.MetLevel, summary.Met_Year, summary.Met_Month, summary.Met_Day);
9596
EggData = new EggData(summary.EggLoc, summary.Egg_Year, summary.Egg_Month, summary.Egg_Day);
96-
OtGender = GameInfo.GenderSymbolASCII[pkmn.OT_Gender];
97+
try
98+
{
99+
OtGender = GameInfo.GenderSymbolASCII[pkmn.OT_Gender];
100+
}
101+
catch (Exception e)
102+
{
103+
SentrySdk.CaptureException(e);
104+
OtGender = "";
105+
}
97106
DexNumber = pkmn.Species;
98107
StoredSize = pkmn.SIZE_STORED;
99108
PartySize = pkmn.SIZE_PARTY;
100109
ItemNum = pkmn.HeldItem;
101110
Generation = pkmn.Generation;
102111
VersionNum = pkmn.Version;
103-
Base64 = Convert.ToBase64String(PartySize > StoredSize ? pkmn.DecryptedPartyData : pkmn.DecryptedBoxData);
104-
105-
if (Version == "") Version = Enum.GetName(typeof(GameVersion), VersionNum) ?? "???";
112+
try
113+
{
114+
Base64 = Convert.ToBase64String(PartySize > StoredSize ? pkmn.DecryptedPartyData : pkmn.DecryptedBoxData);
115+
if (Version == "") Version = Enum.GetName(typeof(GameVersion), VersionNum) ?? "???";
116+
}
117+
catch (Exception e)
118+
{
119+
SentrySdk.CaptureException(e);
120+
Base64 = "";
121+
}
122+
123+
try
124+
{
125+
if (Version == "") Version = Enum.GetName(typeof(GameVersion), VersionNum) ?? "???";
126+
}
127+
catch (Exception e)
128+
{
129+
SentrySdk.CaptureException(e);
130+
Version = "";
131+
}
106132

107133
var legality = Legality.CheckLegality(pkmn);
108134

coreconsole/models/Sprites.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22
using System.Text.Json.Serialization;
33
using PKHeX.Core;
4+
using Sentry;
45

56
namespace coreconsole.Models;
67

@@ -65,7 +66,7 @@ public static void Init()
6566
}
6667
catch (Exception e)
6768
{
68-
Console.WriteLine("Failed to load sprites!");
69+
SentrySdk.CaptureException(e);
6970
_spritesLoaded = false;
7071
}
7172
}
@@ -117,11 +118,17 @@ private string ConstructSpeciesSprite(EntitySummary summary, PKM pkmn)
117118
{
118119
if (species == "alcremie" && pkmn is IFormArgument ifo)
119120
{
120-
if (!Enum.TryParse(ifo.FormArgument.ToString(), out AlcremieDecoration dec)) return "";
121-
122-
var name = Enum.GetName(dec)!.ToLower();
123-
if (!binding.TryGetProperty("file", out var file)) return "";
124-
path += $"{file.GetString()!.Replace(".png", $"-{name}.png")}";
121+
try
122+
{
123+
if (!Enum.TryParse(ifo.FormArgument.ToString(), out AlcremieDecoration dec)) return "";
124+
var name = Enum.GetName(dec)!.ToLower();
125+
if (!binding.TryGetProperty("file", out var file)) return "";
126+
path += $"{file.GetString()!.Replace(".png", $"-{name}.png")}";
127+
}
128+
catch (Exception e)
129+
{
130+
SentrySdk.CaptureException(e);
131+
}
125132
}
126133
else
127134
{

coreconsole/utils/Helpers.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22
using coreconsole.Models;
33
using PKHeX.Core;
4+
using Sentry;
45

56
namespace coreconsole.utils;
67

@@ -38,16 +39,41 @@ public static void Init()
3839

3940
if (pkmn is null)
4041
{
41-
Console.Error.WriteLine("base64 is not a pokemon");
42+
Console.Error.WriteLine("{\"error\": \"base64 is not a pokemon\"}");
43+
SentrySdk.CaptureMessage(level: SentryLevel.Error, message: "base64 provided is not a pokemon");
4244
return null;
4345
}
4446

4547
return pkmn;
4648
}
4749
catch (Exception e) when (e is FormatException or ArgumentNullException)
4850
{
49-
Console.Error.WriteLine("invalid base64 string provided");
51+
Console.Error.WriteLine("{\"error\": \"invalid base64 string provided\"}");
52+
SentrySdk.CaptureException(e);
53+
return null;
54+
} catch (Exception e) when (e is not FormatException and not ArgumentNullException)
55+
{
56+
SentrySdk.CaptureException(e);
5057
return null;
5158
}
5259
}
60+
61+
public static bool LoadEnv()
62+
{
63+
if (!File.Exists(".env"))
64+
{
65+
Console.Error.WriteLine(".env is missing");
66+
return false;
67+
}
68+
69+
foreach (var line in File.ReadAllLines(".env"))
70+
{
71+
var parts = line.Split("=");
72+
if (parts.Length != 2) continue;
73+
74+
Environment.SetEnvironmentVariable(parts[0], parts[1]);
75+
}
76+
77+
return true;
78+
}
5379
}

0 commit comments

Comments
 (0)