From b906491d2fae4e66f275ac29bef075e78afe8a6c Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 13 Mar 2015 19:59:51 -0700 Subject: [PATCH] Refactoring, QR operations -> method --- Misc/PKX.cs | 29 ++++++++ Misc/QR.cs | 39 ++++------ Misc/Util.cs | 54 ++++++++++++++ PKX/f1-Main.Designer.cs | 6 -- PKX/f1-Main.cs | 131 +++++++++++---------------------- SAV/SAV_Wondercard.Designer.cs | 1 - SAV/SAV_Wondercard.cs | 58 ++------------- 7 files changed, 146 insertions(+), 172 deletions(-) diff --git a/Misc/PKX.cs b/Misc/PKX.cs index d8c9dfd51..0cf6c1dea 100644 --- a/Misc/PKX.cs +++ b/Misc/PKX.cs @@ -1073,6 +1073,35 @@ public SaveGame(string GameID) } #endregion + internal static string[] getPKXSummary(PKX data) + { + string[] response = new string[3]; + // Summarize + string filename = data.Nickname; + if (filename != data.Species) + filename += " (" + data.Species + ")"; + response[0] = String.Format("{0} [{4}] lv{3} @ {1} -- {2}", filename, data.HeldItem, data.Nature, data.Level, data.Ability); + response[1] = String.Format("{0} / {1} / {2} / {3}", data.Move1, data.Move2, data.Move3, data.Move4); + response[2] = String.Format( + "IVs:{0}{1}{2}{3}{4}{5}" + + Environment.NewLine + Environment.NewLine + + "EVs:{6}{7}{8}{9}{10}{11}", + Environment.NewLine + data.HP_IV.ToString("00"), + Environment.NewLine + data.ATK_IV.ToString("00"), + Environment.NewLine + data.DEF_IV.ToString("00"), + Environment.NewLine + data.SPA_IV.ToString("00"), + Environment.NewLine + data.SPD_IV.ToString("00"), + Environment.NewLine + data.SPE_IV.ToString("00"), + Environment.NewLine + data.HP_EV.ToString("00"), + Environment.NewLine + data.ATK_EV.ToString("00"), + Environment.NewLine + data.DEF_EV.ToString("00"), + Environment.NewLine + data.SPA_EV.ToString("00"), + Environment.NewLine + data.SPD_EV.ToString("00"), + Environment.NewLine + data.SPE_EV.ToString("00")); + + return response; + } + // Save File Related internal static int detectSAVIndex(byte[] data, ref int savindex) { diff --git a/Misc/QR.cs b/Misc/QR.cs index d16c16e98..c5cafcb8c 100644 --- a/Misc/QR.cs +++ b/Misc/QR.cs @@ -16,36 +16,23 @@ public QR(Image qr, Image pkm, string top, string bottom, string left, string ri gfx.FillRectangle(new SolidBrush(Color.White), 0, 0, preview.Width, preview.Height); gfx.DrawImage(pkm, preview.Width / 2 - pkm.Width / 2, preview.Height / 2 - pkm.Height / 2); } - // Layer on PokéSprite - Image pic = Util.LayerImage(qr, preview, qr.Width / 2 - preview.Width / 2, qr.Height / 2 - preview.Height / 2, 1); // Middle + // Layer on Preview Image + Image pic = Util.LayerImage(qr, preview, qr.Width / 2 - preview.Width / 2, qr.Height / 2 - preview.Height / 2, 1); // Layer on Text - if (ModifierKeys == Keys.Control) - { - Graphics g = Graphics.FromImage(pic); - g.DrawString(top, font, Brushes.Black, new PointF(18, 5)); - g.DrawString(bottom, font, Brushes.Black, new PointF(18, pic.Width - 15)); - g.DrawString(left.Replace("s",""), font, Brushes.Black, new PointF(0, 18)); // trim off *V-s-: - g.DrawString(right, font, Brushes.Black, new PointF(pic.Width - 15, 18), new StringFormat(StringFormatFlags.DirectionVertical)); + const int stretch = 50; + Height += stretch; + Image newpic = new Bitmap(PB_QR.Width, PB_QR.Height); + Graphics g = Graphics.FromImage(newpic); + g.FillRectangle(new SolidBrush(Color.White), 0, 0, newpic.Width, newpic.Height); + g.DrawImage(pic, 0, 0); - PB_QR.BackgroundImage = pic; - } - else - { - const int stretch = 50; - Height += stretch; - Image newpic = new Bitmap(PB_QR.Width, PB_QR.Height); - Graphics g = Graphics.FromImage(newpic); - g.FillRectangle(new SolidBrush(Color.White), 0, 0, newpic.Width, newpic.Height); - g.DrawImage(pic, 0, 0); + g.DrawString(top, font, Brushes.Black, new PointF(18, qr.Height - 5)); + g.DrawString(bottom, font, Brushes.Black, new PointF(18, qr.Height + 8)); + g.DrawString(left.Replace(Environment.NewLine, "/").Replace("//", " ").Replace(":/", ": "), font, Brushes.Black, new PointF(18, qr.Height + 20)); + g.DrawString(right, font, Brushes.Black, new PointF(18, qr.Height + 32)); - g.DrawString(top, font, Brushes.Black, new PointF(18, qr.Height - 5)); - g.DrawString(bottom, font, Brushes.Black, new PointF(18, qr.Height + 8)); - g.DrawString(left.Replace(Environment.NewLine, "/").Replace("//", " ").Replace(":/", ": "), font, Brushes.Black, new PointF(18, qr.Height + 20)); - g.DrawString(right, font, Brushes.Black, new PointF(18, qr.Height + 32)); - - PB_QR.BackgroundImage = newpic; - } + PB_QR.BackgroundImage = newpic; } private void PB_QR_Click(object sender, EventArgs e) { diff --git a/Misc/Util.cs b/Misc/Util.cs index ce2c6abf7..93e10d05c 100644 --- a/Misc/Util.cs +++ b/Misc/Util.cs @@ -553,5 +553,59 @@ internal static List getUnsortedCBList(string textfile) } return cbList; } + + // QR Utility + internal static byte[] getQRData() + { + // Fetch data from QR code... + string address; + try { address = Clipboard.GetText(); } + catch { Alert("No text (url) in clipboard."); return null; } + try { if (address.Length < 4 || address.Substring(0, 3) != "htt") { Alert("Clipboard text is not a valid URL:", address); return null; } } + catch { Alert("Clipboard text is not a valid URL:", address); return null; } + string webURL = "http://api.qrserver.com/v1/read-qr-code/?fileurl=" + System.Web.HttpUtility.UrlEncode(address); + try + { + System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(webURL); + System.Net.HttpWebResponse httpWebReponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); + var reader = new StreamReader(httpWebReponse.GetResponseStream()); + string data = reader.ReadToEnd(); + if (data.Contains("could not find")) { Alert("Reader could not find QR data in the image."); return null; } + if (data.Contains("filetype not supported")) { Alert("Input URL is not valid. Double check that it is an image (jpg/png).", address); return null; } + // Quickly convert the json response to a data string + string pkstr = data.Substring(data.IndexOf("#", StringComparison.Ordinal) + 1); // Trim intro + pkstr = pkstr.Substring(0, pkstr.IndexOf("\",\"error\":null}]}]", StringComparison.Ordinal)); // Trim outro + if (pkstr.Contains("nQR-Code:")) pkstr = pkstr.Substring(0, pkstr.IndexOf("nQR-Code:", StringComparison.Ordinal)); // Remove multiple QR codes in same image + pkstr = pkstr.Replace("\\", ""); // Rectify response + + try { return Convert.FromBase64String(pkstr); } + catch { Alert("QR string to Data failed.", pkstr); return null; } + } + catch { Alert("Unable to connect to the internet to decode QR code."); return null;} + } + internal static Image getQRImage(byte[] data, string server) + { + string qrdata = Convert.ToBase64String(data); + string message = server + qrdata; + string webURL = "http://chart.apis.google.com/chart?chs=365x365&cht=qr&chl=" + System.Web.HttpUtility.UrlEncode(message); + + try + { + System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(webURL); + System.Net.HttpWebResponse httpWebReponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); + Stream stream = httpWebReponse.GetResponseStream(); + if (stream != null) return Image.FromStream(stream); + } + catch + { + if (Prompt(MessageBoxButtons.YesNo, + "Unable to connect to the internet to receive QR code.", + "Copy QR URL to Clipboard?") + != DialogResult.Yes) return null; + try { Clipboard.SetText(webURL); } + catch { Alert("Failed to set text to Clipboard"); } + } + return null; + } } } \ No newline at end of file diff --git a/PKX/f1-Main.Designer.cs b/PKX/f1-Main.Designer.cs index 42019ce21..9a97cef06 100644 --- a/PKX/f1-Main.Designer.cs +++ b/PKX/f1-Main.Designer.cs @@ -704,12 +704,6 @@ public void InitializeComponent() // this.CB_PKRSDays.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.CB_PKRSDays.FormattingEnabled = true; - this.CB_PKRSDays.Items.AddRange(new object[] { - "0", - "1", - "2", - "3", - "4"}); this.CB_PKRSDays.Location = new System.Drawing.Point(197, 213); this.CB_PKRSDays.Name = "CB_PKRSDays"; this.CB_PKRSDays.Size = new System.Drawing.Size(30, 21); diff --git a/PKX/f1-Main.cs b/PKX/f1-Main.cs index 961f00602..0653ebec1 100644 --- a/PKX/f1-Main.cs +++ b/PKX/f1-Main.cs @@ -306,7 +306,7 @@ private void mainMenuSave(object sender, EventArgs e) byte[] backupfile = File.ReadAllBytes(path); File.WriteAllBytes(path + ".bak", backupfile); } - byte[] pkx = preparepkx(buff); + byte[] pkx = preparepkx(); if ((ext == ".ekx") || (ext == ".bin") || (ext == ".pkx") || (ext == ".ek6") || (ext == ".pk6")) { @@ -349,7 +349,7 @@ private void mainMenuCodeGen(object sender, EventArgs e) { // Open Code Generator byte[] formdata = null; - if (verifiedPKX()) formdata = preparepkx(buff); + if (verifiedPKX()) formdata = preparepkx(); CodeGenerator CodeGen = new CodeGenerator(this, formdata); CodeGen.ShowDialog(); byte[] data = CodeGen.returnArray; @@ -722,7 +722,7 @@ private void openSave(bool oras) // Language Translation private void changeMainLanguage(object sender, EventArgs e) { - if (init) buff = preparepkx(buff); // get data currently in form + if (init) buff = preparepkx(); // get data currently in form Menu_Options.DropDown.Close(); InitializeStrings(); @@ -916,8 +916,12 @@ private void InitializeLanguage() } } } - private void populateFields(byte[] buff) + private void populateFields(byte[] data) { + // Store all loaded data in a persistent buffer for easy access. + Array.Resize(ref buff, data.Length); + Array.Copy(data, buff, data.Length); + init = false; CAL_EggDate.Value = new DateTime(2000, 01, 01); Tab_Main.Focus(); @@ -1578,92 +1582,34 @@ private void clickQR(object sender, EventArgs e) if (ModifierKeys == Keys.Alt) { // Fetch data from QR code... - string address; - try { address = Clipboard.GetText(); } - catch { Util.Alert("No text (url) in clipboard."); return; } - try { if (address.Length < 4 || address.Substring(0, 3) != "htt") { Util.Alert("Clipboard text is not a valid URL:", address); return; } } - catch { Util.Alert("Clipboard text is not a valid URL:", address); return; } - string webURL = "http://api.qrserver.com/v1/read-qr-code/?fileurl=" + System.Web.HttpUtility.UrlEncode(address); - try - { - System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(webURL); - System.Net.HttpWebResponse httpWebReponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); - var reader = new StreamReader(httpWebReponse.GetResponseStream()); - string data = reader.ReadToEnd(); - if (data.Contains("could not find")) { Util.Alert("Reader could not find QR data in the image."); return; } - // Quickly convert the json response to a data string - string pkstr = data.Substring(data.IndexOf("#", StringComparison.Ordinal) + 1); // Trim intro - pkstr = pkstr.Substring(0, pkstr.IndexOf("\",\"error\":null}]}]", StringComparison.Ordinal)); // Trim outro - if (pkstr.Contains("nQR-Code:")) pkstr = pkstr.Substring(0, pkstr.IndexOf("nQR-Code:", StringComparison.Ordinal)); // Remove multiple QR codes in same image - pkstr = pkstr.Replace("\\", ""); // Rectify response + byte[] ekx = Util.getQRData(); - byte[] ekx; - try { ekx = Convert.FromBase64String(pkstr); } - catch { Util.Alert("QR string to Data failed.", pkstr); return; } + if (ekx == null) return; - if (ekx.Length != 232) { Util.Alert("Decoded data not 232 bytes.", String.Format("QR Data Size: {0}", ekx.Length)); } - else try { + if (ekx.Length != 232) { Util.Alert("Decoded data not 232 bytes.", String.Format("QR Data Size: {0}", ekx.Length)); } + else try + { byte[] pkx = PKX.decryptArray(ekx); if (PKX.verifychk(pkx)) { Array.Copy(pkx, buff, 0xE8); populateFields(buff); } else Util.Alert("Invalid checksum in QR data."); - } catch { Util.Alert("Error loading decrypted data."); } - } - catch { Util.Alert("Unable to connect to the internet to decode QR code."); } + } + catch { Util.Alert("Error loading decrypted data."); } } else { if (!verifiedPKX()) return; - byte[] pkx = preparepkx(buff); + byte[] pkx = preparepkx(); byte[] ekx = PKX.encryptArray(pkx); Array.Resize(ref ekx, 232); - const string server = "http://loadcode.projectpokemon.org/b1s1.html#"; // Rehosted with permission from LC/MS -- massive thanks! - string qrdata = Convert.ToBase64String(ekx); - string message = server + qrdata; - string webURL = "http://chart.apis.google.com/chart?chs=365x365&cht=qr&chl=" + System.Web.HttpUtility.UrlEncode(message); + Image qr = Util.getQRImage(ekx, server); + + if (qr == null) return; - Image qr = null; - try - { - System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(webURL); - System.Net.HttpWebResponse httpWebReponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); - Stream stream = httpWebReponse.GetResponseStream(); - if (stream != null) qr = Image.FromStream(stream); - } - catch - { - if (DialogResult.Yes == Util.Prompt(MessageBoxButtons.YesNo, "Unable to connect to the internet to receive QR code.", "Copy QR URL to Clipboard?")) - { - try { Clipboard.SetText(webURL); } - catch { Util.Alert("Failed to set text to Clipboard"); } - return; - } - } PKX data = new PKX(pkx, "Tabs"); - string filename = data.Nickname; - if (filename != data.Species) - filename += " (" + data.Species + ")"; - string s1 = String.Format("{0} [{4}] lv{3} @ {1} -- {2}", filename, data.HeldItem, data.Nature, data.Level, data.Ability); - string s2 = String.Format("{0} / {1} / {2} / {3}", data.Move1, data.Move2, data.Move3, data.Move4); - string IVs = String.Format( - "IVs:{0}{1}{2}{3}{4}{5}" - + Environment.NewLine + Environment.NewLine + - "EVs:{6}{7}{8}{9}{10}{11}", - Environment.NewLine + data.HP_IV.ToString("00"), - Environment.NewLine + data.ATK_IV.ToString("00"), - Environment.NewLine + data.DEF_IV.ToString("00"), - Environment.NewLine + data.SPA_IV.ToString("00"), - Environment.NewLine + data.SPD_IV.ToString("00"), - Environment.NewLine + data.SPE_IV.ToString("00"), - Environment.NewLine + data.HP_EV.ToString("00"), - Environment.NewLine + data.ATK_EV.ToString("00"), - Environment.NewLine + data.DEF_EV.ToString("00"), - Environment.NewLine + data.SPA_EV.ToString("00"), - Environment.NewLine + data.SPD_EV.ToString("00"), - Environment.NewLine + data.SPE_EV.ToString("00")); - - new QR(qr, dragout.Image, s1, s2, IVs, "PKHeX @ ProjectPokemon.org").ShowDialog(); + string[] r = PKX.getPKXSummary(data); + new QR(qr, dragout.Image, r[0], r[1], r[2], "PKHeX @ ProjectPokemon.org").ShowDialog(); } } private void clickFriendship(object sender, EventArgs e) @@ -1673,7 +1619,6 @@ private void clickFriendship(object sender, EventArgs e) else TB_Friendship.Text = TB_Friendship.Text == "255" ? PKX.getBaseFriendship(Util.getIndex(CB_Species)).ToString() : "255"; } - private void clickGender(object sender, EventArgs e) { // Get Gender Threshold @@ -2006,7 +1951,17 @@ private void updatePP(object sender, EventArgs e) } private void updatePKRSstrain(object sender, EventArgs e) { + // Change the PKRS Days to the legal bounds. + int currentDuration = CB_PKRSDays.SelectedIndex; + CB_PKRSDays.Items.Clear(); + int[] days = Enumerable.Range(0, CB_PKRSStrain.SelectedIndex % 4 + 1).Select(i => i).ToArray(); + foreach (int day in days) CB_PKRSDays.Items.Add(day); + + // Set the days back if they're legal, else set it to 1. (0 always passes). + CB_PKRSDays.SelectedIndex = (currentDuration < CB_PKRSDays.Items.Count) ? currentDuration : 1; + if (CB_PKRSStrain.SelectedIndex != 0) return; + // Never Infected CB_PKRSDays.SelectedValue = 0; CHK_Cured.Checked = false; @@ -2025,7 +1980,6 @@ private void updatePKRSdays(object sender, EventArgs e) } else CHK_Cured.Checked = true; } - private void updatePKRSCured(object sender, EventArgs e) { if (!init) return; @@ -2459,16 +2413,16 @@ private bool verifiedPKX() // Further logic checking if (Convert.ToUInt32(TB_EVTotal.Text) > 510 && !CHK_HackedStats.Checked) { tabMain.SelectedIndex = 2; goto invalid; } - if (Util.getIndex(CB_Species) == 0) // Not gonna write 0 species. - { tabMain.SelectedIndex = 0; goto invalid; } - // If no errors detected... - return true; - // else... + if (Util.getIndex(CB_Species) != 0) return true; + // Else + tabMain.SelectedIndex = 0; + + // else... invalid: { System.Media.SystemSounds.Exclamation.Play(); return false; } } - private byte[] preparepkx(byte[] buff, bool click = true) + private byte[] preparepkx(bool click = true) { if (click) tabMain.Select(); // hack to make sure comboboxes are set (users scrolling through and immediately setting causes this) @@ -2747,7 +2701,7 @@ private void dragout_MouseDown(object sender, MouseEventArgs e) Cursor.Current = Cursors.Hand; // Make a new file name - byte[] dragdata = preparepkx(buff); + byte[] dragdata = preparepkx(); PKX pkx = new PKX(dragdata, "Tabs"); string filename = pkx.Nickname; if (filename != pkx.Species) @@ -2755,7 +2709,7 @@ private void dragout_MouseDown(object sender, MouseEventArgs e) filename += " - " + pkx.PID; filename += (e.Button == MouseButtons.Right) ? ".ek6" : ".pk6"; - dragdata = (e.Button == MouseButtons.Right) ? PKX.encryptArray(preparepkx(buff)) : preparepkx(buff); + dragdata = (e.Button == MouseButtons.Right) ? PKX.encryptArray(preparepkx()) : preparepkx(); // Strip out party stats (if they are there) Array.Resize(ref dragdata, 232); // Make file @@ -3497,7 +3451,7 @@ private void clickSet(object sender, EventArgs e) if (slot == 30 && (CB_Species.SelectedIndex == 0 || CHK_IsEgg.Checked)) { Util.Alert("Can't have empty/egg first slot."); return; } int offset = getPKXOffset(slot); - byte[] pkxdata = preparepkx(buff); + byte[] pkxdata = preparepkx(); byte[] ekxdata = PKX.encryptArray(pkxdata); if (!savegame_oras) @@ -3564,7 +3518,7 @@ private void clickClone(object sender, EventArgs e) { if (Util.Prompt(MessageBoxButtons.YesNo, String.Format("Clone Pokemon from Editing Tabs to all slots in Box {0}?", box)) == DialogResult.Yes) { - pkxdata = preparepkx(buff); + pkxdata = preparepkx(); setPokedex(pkxdata); } else if (Util.Prompt(MessageBoxButtons.YesNo, String.Format("Delete Pokemon from all slots in Box {0}?", box)) == DialogResult.Yes) @@ -3846,7 +3800,7 @@ private PictureBox getPictureBox(int slot) private void getQuickFiller(PictureBox pb, byte[] dslotdata = null) { if (!init) return; - dslotdata = dslotdata ?? preparepkx(buff, false); // don't perform control loss click + dslotdata = dslotdata ?? preparepkx(false); // don't perform control loss click int species = BitConverter.ToInt16(dslotdata, 0x08); // Get Species uint isegg = (BitConverter.ToUInt32(dslotdata, 0x74) >> 30) & 1; @@ -4654,5 +4608,6 @@ private void pbBoxSlot_DragEnter(object sender, DragEventArgs e) private int pkm_from_offset; private int pkm_from_slot = -1; #endregion + } } diff --git a/SAV/SAV_Wondercard.Designer.cs b/SAV/SAV_Wondercard.Designer.cs index ae42ebd4c..706c32c3f 100644 --- a/SAV/SAV_Wondercard.Designer.cs +++ b/SAV/SAV_Wondercard.Designer.cs @@ -192,7 +192,6 @@ private void InitializeComponent() // // L_QR // - this.L_QR.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.L_QR.AutoSize = true; this.L_QR.Location = new System.Drawing.Point(258, 7); this.L_QR.Name = "L_QR"; diff --git a/SAV/SAV_Wondercard.cs b/SAV/SAV_Wondercard.cs index a38a2fe31..7d0586532 100644 --- a/SAV/SAV_Wondercard.cs +++ b/SAV/SAV_Wondercard.cs @@ -320,44 +320,19 @@ private Image getWCPreviewImage(byte[] data) } return img; } - private void L_QR_Click(object sender, EventArgs e) { if (ModifierKeys == Keys.Alt) { - // Fetch data from QR code... - string address; - try { address = Clipboard.GetText(); } - catch { Util.Alert("No text (url) in clipboard."); return; } - try { if (address.Length < 4 || address.Substring(0, 3) != "htt") { Util.Alert("Clipboard text is not a valid URL:", address); return; } } - catch { Util.Alert("Clipboard text is not a valid URL:", address); return; } - string webURL = "http://api.qrserver.com/v1/read-qr-code/?fileurl=" + System.Web.HttpUtility.UrlEncode(address); - try - { - System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(webURL); - System.Net.HttpWebResponse httpWebReponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); - var reader = new StreamReader(httpWebReponse.GetResponseStream()); - string data = reader.ReadToEnd(); - if (data.Contains("could not find")) { Util.Alert("Reader could not find QR data in the image."); return; } - // Quickly convert the json response to a data string - string pkstr = data.Substring(data.IndexOf("#", StringComparison.Ordinal) + 1); // Trim intro - pkstr = pkstr.Substring(0, pkstr.IndexOf("\",\"error\":null}]}]", StringComparison.Ordinal)); // Trim outro - if (pkstr.Contains("nQR-Code:")) pkstr = pkstr.Substring(0, pkstr.IndexOf("nQR-Code:", StringComparison.Ordinal)); // Remove multiple QR codes in same image - pkstr = pkstr.Replace("\\", ""); // Rectify response + byte[] wc = Util.getQRData(); - byte[] wc; - try { wc = Convert.FromBase64String(pkstr); } - catch { Util.Alert("QR string to Data failed.", pkstr); return; } - - if (wc.Length != 0x108) { Util.Alert("Decoded data not 0x108 bytes.", String.Format("QR Data Size: 0x{0}", wc.Length.ToString("X"))); } - else try + if (wc.Length != 0x108) { Util.Alert("Decoded data not 0x108 bytes.", String.Format("QR Data Size: 0x{0}", wc.Length.ToString("X"))); } + else try { Array.Copy(wc, wondercard_data, wc.Length); - loadwcdata(); + loadwcdata(); } catch { Util.Alert("Error loading wondercard data."); } - } - catch { Util.Alert("Unable to connect to the internet to decode QR code."); } } else { @@ -369,31 +344,12 @@ private void L_QR_Click(object sender, EventArgs e) Array.Resize(ref wcdata, 0x108); // Setup QR const string server = "http://lunarcookies.github.io/wc.html#"; - string qrdata = Convert.ToBase64String(wcdata); - string message = server + qrdata; - string webURL = "http://chart.apis.google.com/chart?chs=365x365&cht=qr&chl=" + System.Web.HttpUtility.UrlEncode(message); + Image qr = Util.getQRImage(wcdata, server); + if (qr == null) return; - Image qr = null; - try - { - System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(webURL); - System.Net.HttpWebResponse httpWebReponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); - Stream stream = httpWebReponse.GetResponseStream(); - if (stream != null) qr = Image.FromStream(stream); - } - catch - { - if (DialogResult.Yes == Util.Prompt(MessageBoxButtons.YesNo, "Unable to connect to the internet to receive QR code.", "Copy QR URL to Clipboard?")) - { - try { Clipboard.SetText(webURL); } - catch { Util.Alert("Failed to set text to Clipboard"); } - return; - } - } string desc = getWCDescriptionString(wondercard_data); - Image img = PB_Preview.Image; - new QR(qr, img, desc, "", "", "PKHeX Wondercard @ ProjectPokemon.org").ShowDialog(); + new QR(qr, PB_Preview.Image, desc, "", "", "PKHeX Wondercard @ ProjectPokemon.org").ShowDialog(); } } }