using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Net; using Newtonsoft.Json.Linq; namespace ArchBreaker { public partial class IntroForm : Form { private ArchSession _session; public IntroForm() { InitializeComponent(); _session = new ArchSession(); _session.OnLog += (o, e) => { Log(e.Message); }; configDataGrid.DataSource = _session.Config; configDataGrid.Columns[0].Width = 140; configDataGrid.Columns[1].Width = 300; } private void Log(string text) { logBox.AppendText("\r\n" + text); } private async void logInButton_Click(object sender, EventArgs e) { // Step 1: Log in with our account var devAccount = _session.GetParam("NPFDeviceAccount").Value; var devPassword = _session.GetParam("NPFDevicePassword").Value; if (devAccount.Length == 0 || devPassword.Length == 0) { MessageBox.Show("No device account! You must create one first"); return; } var idToken = await _session.NPFLoginAsync(devAccount, devPassword); Log(string.Format("Received ID Token: {0}[...]", idToken.Substring(0, 30))); var sessionId = await _session.AuthToMiitomoAsync(idToken); Log("Login complete! Session ID obtained: " + sessionId); _session.GetParam("MiitomoSessionID").Value = sessionId; } private async void sandboxSend_Click(object sender, EventArgs e) { string method = "GET"; if (postRadioButton.Checked) method = "POST"; else if (putRadioButton.Checked) method = "PUT"; else if (deleteRadioButton.Checked) method = "DELETE"; if (_session.GetParam("MiitomoSessionID").Value.Length == 0) { sandboxOutput.Text = "No cookie!"; return; } JObject payload = null; if ((method == "POST" || method == "PUT") && sandboxInput.Text.Length > 0) { try { payload = JObject.Parse(sandboxInput.Text); } catch (Exception ex) { sandboxOutput.Text = "Could not parse JSON:\r\n" + ex.ToString(); return; } } var result = await _session.CallMiitomoAPIAsync(sandboxURL.Text, method, payload); if (result != null) sandboxOutput.Text = result.ToString(); } private ArchSession.NewAccountResult _newAccountState = null; private async void newAccountButton_Click(object sender, EventArgs e) { _newAccountState = await _session.CreateNewAccountAsync(); Log("Open the following URL: " + _newAccountState.OAuthURL); Log("\r\nWhen you see \"The above account will be linked, and Miitomo will begin. Select OK to proceed.\", right click on the OK button, copy the link it leads to, and paste it into the box above."); authURLInput.Text = ""; newAccountPanel.Visible = true; } private async void newAccountFinishButton_Click(object sender, EventArgs e) { var url = authURLInput.Text; if (!url.StartsWith(Constants.RedirectURI)) { MessageBox.Show("That doesn't look right; the address should begin with " + Constants.RedirectURI); return; } newAccountPanel.Visible = false; var authURLResult = await _session.HandleAuthURLAsync(url, _newAccountState); if (authURLResult == null) return; var accountToken = await _session.GetAccountTokenAsync(authURLResult.SessionToken); Log(string.Format("Obtained target account info - id {0}", accountToken.UserID)); await _session.CallMiitomoAPIAsync( string.Format("/v1/platform_accounts/{0}/authorization_code", accountToken.UserID), "POST", new JObject { { "authorization_code", authURLResult.Code } }, _newAccountState.MiitomoSessionID); Log("Miitomo platform account authorised successfully, doing federation now"); var newIdToken = await _session.DoFederationAsync( accountToken.IDToken, _newAccountState.NPFSessionID, _newAccountState.OriginalUserID, _newAccountState.DeviceAccount, _newAccountState.DevicePassword); Log("Federation completed, new ID token obtained, getting new Miitomo session ID"); var newMiiSession = await _session.AuthToMiitomoAsync(newIdToken); Log("All done! Remember to save your settings so you don't lose them."); _session.GetParam("NPFDeviceAccount").Value = _newAccountState.DeviceAccount; _session.GetParam("NPFDevicePassword").Value = _newAccountState.DevicePassword; _session.GetParam("MiitomoSessionID").Value = newMiiSession; configDataGrid.Refresh(); } private void saveSettingsButton_Click(object sender, EventArgs e) { if (saveSettingsDialog.ShowDialog() != DialogResult.OK) return; var jsonRoot = new JObject(); foreach (var param in _session.Config) jsonRoot[param.Key] = param.Value; File.WriteAllText(saveSettingsDialog.FileName, jsonRoot.ToString()); } private void loadSettingsButton_Click(object sender, EventArgs e) { if (loadSettingsDialog.ShowDialog() != DialogResult.OK) return; var jsonRoot = JObject.Parse(File.ReadAllText(loadSettingsDialog.FileName)); foreach (var param in _session.Config) { JToken value; if (jsonRoot.TryGetValue(param.Key, out value)) param.Value = value.ToObject(); } configDataGrid.Refresh(); } private JObject _stockItem; private async void downloadInventoryButton_Click(object sender, EventArgs e) { uploadInventoryButton.Enabled = false; _stockItem = await _session.CallMiitomoAPIAsync( "/v1/player/stock_item", "GET", null); if (_stockItem == null) { MessageBox.Show("Error! You might need to log in again."); return; } var si = _stockItem["player"]["stock_item"]; sweetCounter.Value = si["snack"].ToObject(); ticketCounter.Value = si["game_ticket"].ToObject(); inventoryStatsLabel.Text = string.Format( "JSON Version: {0}\r\n" + "Levels: Popularity {1} / Style {2}\r\n" + "{3} clothing item(s), {4} template(s), {5} background(s)", si["json_version"].ToObject(), si["level_progress_popularity"].ToObject(), si["level_progress_fashionability"].ToObject(), (si["wears"] as JArray).Count, (si["templates"] as JArray).Count, (si["backgrounds"] as JArray).Count ); uploadInventoryButton.Enabled = true; } private async void uploadInventoryButton_Click(object sender, EventArgs e) { var root = _stockItem["player"] as JObject; var si = root["stock_item"]; si["snack"] = (int)sweetCounter.Value; si["game_ticket"] = (int)ticketCounter.Value; uploadInventoryButton.Enabled = false; var result = await _session.CallMiitomoAPIAsync( "/v1/player/stock_item", "PUT", root); if (result == null) MessageBox.Show("Error!"); uploadInventoryButton.Enabled = true; } } }