From baab12a6a9109bfa50d24e24f77307ea9fb0dfc2 Mon Sep 17 00:00:00 2001 From: dcb <38927017+dotcomboom@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:46:05 -0600 Subject: [PATCH] Use config.json file to store application settings --- Mario Kart Wii Rich Presence Redux/App.config | 31 ----- .../My Project/Application.Designer.vb | 2 +- .../My Project/Application.myapp | 2 +- .../My Project/Settings.Designer.vb | 96 -------------- .../My Project/Settings.settings | 29 +---- Mario Kart Wii Rich Presence Redux/frmMain.vb | 123 +++++++++++++----- 6 files changed, 94 insertions(+), 189 deletions(-) diff --git a/Mario Kart Wii Rich Presence Redux/App.config b/Mario Kart Wii Rich Presence Redux/App.config index 6d690af..9c23ce4 100644 --- a/Mario Kart Wii Rich Presence Redux/App.config +++ b/Mario Kart Wii Rich Presence Redux/App.config @@ -1,39 +1,8 @@  - -
- - - - - https://wiimmfi.de/stats/mkw/room/p1 - - - True - - - 0000-0000-0000 - - - False - - - - - - False - - - False - - - False - - - \ No newline at end of file diff --git a/Mario Kart Wii Rich Presence Redux/My Project/Application.Designer.vb b/Mario Kart Wii Rich Presence Redux/My Project/Application.Designer.vb index a364c12..ad88102 100644 --- a/Mario Kart Wii Rich Presence Redux/My Project/Application.Designer.vb +++ b/Mario Kart Wii Rich Presence Redux/My Project/Application.Designer.vb @@ -24,7 +24,7 @@ Namespace My _ Public Sub New() MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows) - Me.IsSingleInstance = true + Me.IsSingleInstance = false Me.EnableVisualStyles = true Me.SaveMySettingsOnExit = true Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses diff --git a/Mario Kart Wii Rich Presence Redux/My Project/Application.myapp b/Mario Kart Wii Rich Presence Redux/My Project/Application.myapp index 74f2fe3..1eb0a95 100644 --- a/Mario Kart Wii Rich Presence Redux/My Project/Application.myapp +++ b/Mario Kart Wii Rich Presence Redux/My Project/Application.myapp @@ -2,7 +2,7 @@ true frmMain - true + false 0 true 0 diff --git a/Mario Kart Wii Rich Presence Redux/My Project/Settings.Designer.vb b/Mario Kart Wii Rich Presence Redux/My Project/Settings.Designer.vb index 3ce979f..8759394 100644 --- a/Mario Kart Wii Rich Presence Redux/My Project/Settings.Designer.vb +++ b/Mario Kart Wii Rich Presence Redux/My Project/Settings.Designer.vb @@ -53,102 +53,6 @@ Namespace My Return defaultInstance End Get End Property - - _ - Public Property startUrl() As Global.System.Uri - Get - Return CType(Me("startUrl"),Global.System.Uri) - End Get - Set - Me("startUrl") = value - End Set - End Property - - _ - Public Property shareBtn() As Boolean - Get - Return CType(Me("shareBtn"),Boolean) - End Get - Set - Me("shareBtn") = value - End Set - End Property - - _ - Public Property addText() As String - Get - Return CType(Me("addText"),String) - End Get - Set - Me("addText") = value - End Set - End Property - - _ - Public Property useOwnApp() As Boolean - Get - Return CType(Me("useOwnApp"),Boolean) - End Get - Set - Me("useOwnApp") = value - End Set - End Property - - _ - Public Property userAppId() As String - Get - Return CType(Me("userAppId"),String) - End Get - Set - Me("userAppId") = value - End Set - End Property - - _ - Public Property useTrackState() As Boolean - Get - Return CType(Me("useTrackState"),Boolean) - End Get - Set - Me("useTrackState") = value - End Set - End Property - - _ - Public Property useTextDetails() As Boolean - Get - Return CType(Me("useTextDetails"),Boolean) - End Get - Set - Me("useTextDetails") = value - End Set - End Property - - _ - Public Property useCustomCourseImages() As Boolean - Get - Return CType(Me("useCustomCourseImages"),Boolean) - End Get - Set - Me("useCustomCourseImages") = value - End Set - End Property End Class End Namespace diff --git a/Mario Kart Wii Rich Presence Redux/My Project/Settings.settings b/Mario Kart Wii Rich Presence Redux/My Project/Settings.settings index f1bfcc0..f7f176a 100644 --- a/Mario Kart Wii Rich Presence Redux/My Project/Settings.settings +++ b/Mario Kart Wii Rich Presence Redux/My Project/Settings.settings @@ -1,30 +1,5 @@  - + - - - https://wiimmfi.de/stats/mkw/room/p1 - - - True - - - 0000-0000-0000 - - - False - - - - - - False - - - False - - - False - - + \ No newline at end of file diff --git a/Mario Kart Wii Rich Presence Redux/frmMain.vb b/Mario Kart Wii Rich Presence Redux/frmMain.vb index 815e607..e093699 100644 --- a/Mario Kart Wii Rich Presence Redux/frmMain.vb +++ b/Mario Kart Wii Rich Presence Redux/frmMain.vb @@ -1,6 +1,7 @@ Imports System.Text.RegularExpressions Imports DiscordRPC Imports DiscordRPC.Logging +Imports Newtonsoft.Json Public Class frmMain Private details As String = "" @@ -10,10 +11,59 @@ Public Class frmMain Private small_image As String = "" Private party_size As Integer = 0 + Private conf_startUrl As String = "https://wiimmfi.de/stats/mkw/room/p1" + Private conf_shareBtn As Boolean = True + Private conf_addText As String = "0000-0000-0000" + Private conf_useOwnApp As Boolean = False + Private conf_userAppId As String = "" + Private conf_useTrackState As Boolean = False + Private conf_useTextDetails As Boolean = False + Private conf_useCustomCourseImages As Boolean = False + Public default_app_id = "662481965840072717" Private client As DiscordRpcClient + Private Sub conf_load() + If My.Computer.FileSystem.FileExists("config.json") Then + Dim conf = My.Computer.FileSystem.ReadAllText("config.json") + Try + Dim j = Linq.JObject.Parse(conf) + If j.ContainsKey("startUrl") Then conf_startUrl = j.GetValue("startUrl") + If j.ContainsKey("shareBtn") Then conf_shareBtn = j.GetValue("shareBtn") + If j.ContainsKey("addText") Then conf_addText = j.GetValue("addText") + If j.ContainsKey("userAppId") Then conf_userAppId = j.GetValue("userAppId") + Try + If j.ContainsKey("useOwnApp") Then conf_useOwnApp = j.GetValue("useOwnApp") + If j.ContainsKey("useTrackState") Then conf_useTrackState = j.GetValue("useTrackState") + If j.ContainsKey("useTextDetails") Then conf_useTextDetails = j.GetValue("useTextDetails") + If j.ContainsKey("useCustomCourseImages") Then conf_useCustomCourseImages = j.GetValue("useCustomCourseImages") + Catch ex As Exception + ' booleans in config aren't booleans + End Try + Catch e As JsonReaderException + Exit Sub + End Try + End If + End Sub + + Private Sub conf_save() + Dim ob As New Dictionary(Of String, Object) + + ob.Add("startUrl", conf_startUrl) + ob.Add("shareBtn", conf_shareBtn) + ob.Add("addText", conf_addText) + ob.Add("userAppId", conf_userAppId) + ob.Add("useOwnApp", conf_useOwnApp) + ob.Add("useTrackState", conf_useTrackState) + ob.Add("useTextDetails", conf_useTextDetails) + ob.Add("useCustomCourseImages", conf_useCustomCourseImages) + + Dim json As String = JsonConvert.SerializeObject(ob, Formatting.indented) + + My.Computer.FileSystem.WriteAllText("config.json", json, False) + End Sub + Private Async Sub UpdateRPC() Handles webView.NavigationCompleted TextBox3.Text = webView.Source.AbsoluteUri @@ -24,7 +74,7 @@ Public Class frmMain If matches.Count > 0 Then TextBox2.Text = webView.Source.AbsoluteUri - My.Settings.startUrl = webView.Source + conf_startUrl = webView.Source.AbsoluteUri.ToString 'scraping :) ' N64 Sherbet Land (Nintendo) @@ -37,7 +87,7 @@ Public Class frmMain End Try - ' "The Html comes back with unicode character codes, other escaped characters, and + ' "The Html comes back with unicode character codes, other escaped characters, And ' wrapped in double quotes, so I'm using this code to clean it up for what I'm doing." html = Regex.Unescape(html) html = html.Remove(0, 1) @@ -86,7 +136,7 @@ Public Class frmMain End If track = track.Split("(").First - If My.Settings.useCustomCourseImages Or Not large_image = "custom" Then + If conf_useCustomCourseImages Or Not large_image = "custom" Then large_image = track.Replace(" ", "").Replace("'", "").ToLower() large_image = Regex.Replace(large_image, "[^a-z0-9\-/]", "_") 'large_image = track.Replace(" ", "").Replace("'", "").ToLower().Replace(".", "_").Replace(",", "_") @@ -95,7 +145,7 @@ Public Class frmMain If details = "Not in a room" Then large_image = "placeholder" - state = My.Settings.addText + state = conf_addText End If If large_image.Length > 32 Then @@ -153,38 +203,44 @@ Public Class frmMain .LargeImageText = track, .SmallImageText = smallimagetext}} - If My.Settings.shareBtn Then - Dim btn As New DiscordRPC.Button - btn.Url = txtUserURL.Text - btn.Label = "View details" - pres.Buttons = {btn} + If conf_shareBtn Then + Try + Dim btn As New DiscordRPC.Button + btn.Url = txtUserURL.Text + btn.Label = "View details" + pres.Buttons = {btn} + Catch ex As ArgumentException + ' url invalid + End Try End If client.SetPresence(pres) - + conf_save() End If End Sub Private Sub Form1_Load() Handles MyBase.Load + conf_load() Me.Size = New Size(Me.MinimumSize.Width, Me.MinimumSize.Height) - If My.Settings.startUrl.AbsolutePath = "about:blank" Then - My.Settings.startUrl = New Uri("https://wiimmfi.de/stats/mkw/room/p") - End If - chkShare.Checked = My.Settings.shareBtn - txtUserURL.Text = My.Settings.startUrl.AbsoluteUri - webView.Source = My.Settings.startUrl - txtAddText.Text = My.Settings.addText - - useOwnApp.Checked = My.Settings.useOwnApp - useTrackState.Checked = My.Settings.useTrackState - useTextDetails.Checked = My.Settings.useTextDetails + chkShare.Checked = conf_shareBtn + txtUserURL.Text = conf_startUrl + Try + webView.Source = New Uri(conf_startUrl) + Catch ex As UriFormatException + webView.Source = New Uri("https://wiimmfi.de/stats/mkw/room/p1") + End Try + txtAddText.Text = conf_addText + + useOwnApp.Checked = conf_useOwnApp + useTrackState.Checked = conf_useTrackState + useTextDetails.Checked = conf_useTextDetails init_client() End Sub Private Sub init_client() - If My.Settings.useOwnApp Then + If conf_useOwnApp Then Try - client = New DiscordRpcClient(My.Settings.userAppId) + client = New DiscordRpcClient(conf_userAppId) Catch ex As Exception MsgBox(ex.Message & vbNewLine & "Using own application ID has been disabled.", MsgBoxStyle.Exclamation) client = New DiscordRpcClient(default_app_id) @@ -213,8 +269,9 @@ Public Class frmMain End Sub Private Sub btnSaveUpdate_Click(sender As Object, e As EventArgs) Handles btnSaveUpdate.Click - My.Settings.addText = txtAddText.Text + conf_addText = txtAddText.Text webView.CoreWebView2.Navigate(txtUserURL.Text) + UpdateRPC() End Sub Private Sub lnkList_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lnkList.LinkClicked @@ -245,19 +302,19 @@ Public Class frmMain Private Sub useOwnApp_CheckedChanged(sender As Object, e As EventArgs) Handles useOwnApp.Click If useOwnApp.Checked Then - frmOwnRPC.ClientId.Text = My.Settings.userAppId - frmOwnRPC.useCustomImages.Checked = My.Settings.useCustomCourseImages + frmOwnRPC.ClientId.Text = conf_userAppId + frmOwnRPC.useCustomImages.Checked = conf_useCustomCourseImages If frmOwnRPC.ShowDialog = DialogResult.OK Then - My.Settings.userAppId = frmOwnRPC.ClientId.Text - My.Settings.useCustomCourseImages = frmOwnRPC.useCustomImages.Checked + conf_userAppId = frmOwnRPC.ClientId.Text + conf_useCustomCourseImages = frmOwnRPC.useCustomImages.Checked Else useOwnApp.Checked = False End If End If - If My.Settings.userAppId = "" Then + If conf_userAppId = "" Then useOwnApp.Checked = False End If - My.Settings.useOwnApp = useOwnApp.Checked + conf_useOwnApp = useOwnApp.Checked client.ClearPresence() client.Dispose() init_client() @@ -265,17 +322,17 @@ Public Class frmMain End Sub Private Sub chkShare_CheckedChanged(sender As Object, e As EventArgs) Handles chkShare.CheckedChanged - My.Settings.shareBtn = chkShare.Checked + conf_shareBtn = chkShare.Checked UpdateRPC() End Sub Private Sub useTrackState_CheckedChanged(sender As Object, e As EventArgs) Handles useTrackState.CheckedChanged - My.Settings.useTrackState = useTrackState.Checked + conf_useTrackState = useTrackState.Checked UpdateRPC() End Sub Private Sub useTextDetails_CheckedChanged(sender As Object, e As EventArgs) Handles useTextDetails.CheckedChanged - My.Settings.useTextDetails = useTextDetails.Checked + conf_useTextDetails = useTextDetails.Checked UpdateRPC() End Sub End Class