Skip to content

Commit

Permalink
Clean up duplicate pragma directives
Browse files Browse the repository at this point in the history
  • Loading branch information
bdawg1989 committed Jun 11, 2024
1 parent 88acc0a commit 232e2ab
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 320 deletions.
2 changes: 1 addition & 1 deletion SysBot.Pokemon.Discord/Commands/Bots/BotAvatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task SetAvatarAsync()
using var httpClient = new HttpClient();
var imageBytes = await httpClient.GetByteArrayAsync(attachment.Url);

using var ms = new MemoryStream(imageBytes);
await using var ms = new MemoryStream(imageBytes);
var image = new Image(ms);
await Context.Client.CurrentUser.ModifyAsync(user => user.Avatar = image);

Expand Down
16 changes: 3 additions & 13 deletions SysBot.Pokemon.Discord/Commands/Bots/Pokepaste.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ public class Pokepaste : ModuleBase<SocketCommandContext>
{
private static System.Drawing.Image CombineImages(List<System.Drawing.Image> images)
{
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
int width = images.Sum(img => img.Width);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
int height = images.Max(img => img.Height);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility

#pragma warning disable CA1416 // Validate platform compatibility
Bitmap combinedImage = new Bitmap(width, height);
Expand Down Expand Up @@ -87,7 +83,7 @@ await Task.Run(async () =>
var pokemonImages = new List<System.Drawing.Image>();
#pragma warning restore CA1416 // Validate platform compatibility

using var memoryStream = new MemoryStream();
await using var memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (var set in showdownSets)
Expand Down Expand Up @@ -121,17 +117,13 @@ await Task.Run(async () =>
var speciesName = GameInfo.GetStrings("en").Species[set.Species];
var fileName = namer.GetName(pk); // Use GengarNamer to generate the file name
var entry = archive.CreateEntry($"{fileName}.{pk.Extension}");
using var entryStream = entry.Open();
await using var entryStream = entry.Open();
await entryStream.WriteAsync(pk.Data.AsMemory(0, pk.Data.Length)).ConfigureAwait(false);

string speciesImageUrl = AbstractTrade<PK9>.PokeImg(pk, false, false);
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
var speciesImage = await Task.Run(() => System.Drawing.Image.FromStream(new HttpClient().GetStreamAsync(speciesImageUrl).Result)).ConfigureAwait(false);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
pokemonImages.Add(speciesImage);
#pragma warning restore CA1416 // Validate platform compatibility
Expand All @@ -155,12 +147,10 @@ await Task.Run(async () =>
#pragma warning disable CA1416 // Validate platform compatibility
combinedImage.Save($"{title}.png");
#pragma warning restore CA1416 // Validate platform compatibility
using (var imageStream = new MemoryStream())
await using (var imageStream = new MemoryStream())
{
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
combinedImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
imageStream.Position = 0;

Expand Down
4 changes: 2 additions & 2 deletions SysBot.Pokemon.Discord/Commands/Bots/RemoteControlModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private async Task ClickAsyncImpl(SwitchButton button, BotSource<PokeBotState> b
await ReplyAsync($"{b.Connection.Name} has performed: {button}").ConfigureAwait(false);
}

private string GetRunningBotIP()
private static string GetRunningBotIP()
{
var r = SysCord<T>.Runner;
var runningBot = r.Bots.Find(x => x.IsRunning);
Expand All @@ -130,7 +130,7 @@ private string GetRunningBotIP()

private async Task SetScreen(bool on)
{
string ip = GetRunningBotIP();
string ip = RemoteControlModule<T>.GetRunningBotIP();
var bot = GetBot(ip);
if (bot == null)
{
Expand Down
6 changes: 3 additions & 3 deletions SysBot.Pokemon.Discord/Commands/Bots/TradeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ public async Task BatchTradeZipAsync()
}

var zipBytes = await new HttpClient().GetByteArrayAsync(attachment.Url);
using var zipStream = new MemoryStream(zipBytes);
await using var zipStream = new MemoryStream(zipBytes);
using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read);
var entries = archive.Entries.ToList();

Expand All @@ -791,7 +791,7 @@ public async Task BatchTradeZipAsync()

foreach (var entry in entries)
{
using var entryStream = entry.Open();
await using var entryStream = entry.Open();
var pkBytes = await TradeModule<T>.ReadAllBytesAsync(entryStream).ConfigureAwait(false);
var pk = EntityFormat.GetFromBytes(pkBytes);

Expand All @@ -809,7 +809,7 @@ public async Task BatchTradeZipAsync()

private static async Task<byte[]> ReadAllBytesAsync(Stream stream)
{
using var memoryStream = new MemoryStream();
await using var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
return memoryStream.ToArray();
}
Expand Down
20 changes: 7 additions & 13 deletions SysBot.Pokemon.Discord/Commands/Bots/VGCPastes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace SysBot.Pokemon.Discord
{
// Uses VGCPastes Repository Spreadsheet in which they keep track of all current teams
// https://twitter.com/VGCPastes
private async Task<string> DownloadSpreadsheetAsCsv()
private static async Task<string> DownloadSpreadsheetAsCsv()
{
var GID = SysCord<T>.Runner.Config.Trade.VGCPastesConfiguration.GID;
var csvUrl = $"https://docs.google.com/spreadsheets/d/1axlwmzPA49rYkqXh7zHvAtSP-TKbM0ijGYBPRflLSWw/export?format=csv&gid={GID}";
Expand All @@ -32,7 +32,7 @@ private async Task<string> DownloadSpreadsheetAsCsv()

private async Task<List<List<string>>> FetchSpreadsheetData()
{
var csvData = await DownloadSpreadsheetAsCsv();
var csvData = await VGCPastes<T>.DownloadSpreadsheetAsCsv();
var rows = csvData.Split('\n');
var data = rows.Select(row => row.Split(',').Select(cell => cell.Trim('"')).ToList()).ToList();
return data;
Expand Down Expand Up @@ -77,7 +77,7 @@ private async Task<List<List<string>>> FetchSpreadsheetData()
return pokePasteData;
}

private (string PokePasteUrl, List<string> RowData) SelectRandomPokePasteUrl(List<List<string>> data, string? pokemonName = null)
private static (string PokePasteUrl, List<string> RowData) SelectRandomPokePasteUrl(List<List<string>> data, string? pokemonName = null)
{
var filteredData = data.Where(row => row.Count > 40 && Uri.IsWellFormedUriString(row[24]?.Trim('"'), UriKind.Absolute));

Expand Down Expand Up @@ -122,7 +122,7 @@ public async Task GenerateSpreadsheetTeamAsync(string? pokemonName = null)
var spreadsheetData = await FetchSpreadsheetData();

// Use the adjusted method to select a random PokePaste URL (and row data) based on the Pokémon name
var (PokePasteUrl, selectedRow) = SelectRandomPokePasteUrl(spreadsheetData, pokemonName);
var (PokePasteUrl, selectedRow) = VGCPastes<T>.SelectRandomPokePasteUrl(spreadsheetData, pokemonName);
if (PokePasteUrl == null)
{
await ReplyAsync("Failed to find a valid PokePaste URL with the specified Pokémon.");
Expand Down Expand Up @@ -155,7 +155,7 @@ public async Task GenerateSpreadsheetTeamAsync(string? pokemonName = null)
#pragma warning disable CS8604 // Possible null reference argument.
var sanitizedTeamDescription = SanitizeFileName(teamDescription);
#pragma warning restore CS8604 // Possible null reference argument.
using var memoryStream = new MemoryStream();
await using var memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (var set in showdownSets)
Expand All @@ -179,7 +179,7 @@ public async Task GenerateSpreadsheetTeamAsync(string? pokemonName = null)
var speciesName = GameInfo.GetStrings("en").Species[set.Species];
var fileName = namer.GetName(pk);
var entry = archive.CreateEntry($"{fileName}.{pk.Extension}");
using var entryStream = entry.Open();
await using var entryStream = entry.Open();
await entryStream.WriteAsync(pk.Data.AsMemory(0, pk.Data.Length));

string speciesImageUrl = AbstractTrade<PK9>.PokeImg(pk, false, false);
Expand Down Expand Up @@ -210,12 +210,10 @@ public async Task GenerateSpreadsheetTeamAsync(string? pokemonName = null)
#pragma warning disable CA1416 // Validate platform compatibility
combinedImage.Save("spreadsheetteam.png");
#pragma warning restore CA1416 // Validate platform compatibility
using (var imageStream = new MemoryStream())
await using (var imageStream = new MemoryStream())
{
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
combinedImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
imageStream.Position = 0;

Expand Down Expand Up @@ -287,16 +285,12 @@ private static List<ShowdownSet> ParseShowdownSets(string pokePasteHtml)

private static System.Drawing.Image CombineImages(List<System.Drawing.Image> images)
{
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
int width = images.Sum(img => img.Width);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
int height = images.Max(img => img.Height);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility

#pragma warning disable CA1416 // Validate platform compatibility
Bitmap combinedImage = new Bitmap(width, height);
Expand Down
6 changes: 0 additions & 6 deletions SysBot.Pokemon.Discord/Commands/Management/BotModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@ private static string GetBotIPFromJsonConfig()
var config = JObject.Parse(jsonData);

// Access the IP address from the first bot in the Bots array
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
var ip = config["Bots"][0]["Connection"]["IP"].ToString();
#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
return ip;
}
Expand Down
18 changes: 3 additions & 15 deletions SysBot.Pokemon.Discord/Commands/Management/OwnerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public async Task RePeek()
return;
}

using MemoryStream ms = new(bytes);
await using MemoryStream ms = new(bytes);
var img = "cap.jpg";
var embed = new EmbedBuilder { ImageUrl = $"attachment://{img}", Color = (DiscordColor?)Color.Purple }
.WithFooter(new EmbedFooterBuilder { Text = "Here's your screenshot." });
Expand Down Expand Up @@ -320,20 +320,14 @@ public async Task RePeekGIF()
return;
}

using (var ms = new MemoryStream(bytes))
await using (var ms = new MemoryStream(bytes))
{
#pragma warning disable CA1416 // Validate platform compatibility
using var bitmap = new Bitmap(ms);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
var frame = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning disable CA1416 // Validate platform compatibility
gifFrames.Add(frame);
#pragma warning restore CA1416 // Validate platform compatibility
Expand All @@ -342,7 +336,7 @@ public async Task RePeekGIF()
await Task.Delay(screenshotInterval).ConfigureAwait(false);
}

using (var ms = new MemoryStream())
await using (var ms = new MemoryStream())
{
using (var gif = new AnimatedGifCreator(ms, 200))
{
Expand Down Expand Up @@ -387,14 +381,8 @@ private static string GetBotIPFromJsonConfig()
var jsonData = File.ReadAllText(TradeBot.ConfigPath);
var config = JObject.Parse(jsonData);

#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
var ip = config["Bots"][0]["Connection"]["IP"].ToString();
#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
return ip;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private static async Task<Bitmap> LoadImageAsync(string imagePath)
{
using var httpClient = new HttpClient();
using var response = await httpClient.GetAsync(imagePath);
using var stream = await response.Content.ReadAsStreamAsync();
await using var stream = await response.Content.ReadAsStreamAsync();
#pragma warning disable CA1416 // Validate platform compatibility
return new Bitmap(stream);
#pragma warning restore CA1416 // Validate platform compatibility
Expand Down
Loading

0 comments on commit 232e2ab

Please sign in to comment.