diff --git a/Discord WMP/Form1.Designer.cs b/Discord WMP/Form1.Designer.cs index 8e279f6..3e985e9 100644 --- a/Discord WMP/Form1.Designer.cs +++ b/Discord WMP/Form1.Designer.cs @@ -46,6 +46,7 @@ private void InitializeComponent() { this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.label7 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); + this.linkLabel2 = new System.Windows.Forms.LinkLabel(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown_retryattempts)).BeginInit(); this.SuspendLayout(); // @@ -102,7 +103,7 @@ private void InitializeComponent() { // checkBox_showconsole // this.checkBox_showconsole.AutoSize = true; - this.checkBox_showconsole.Location = new System.Drawing.Point(0, 268); + this.checkBox_showconsole.Location = new System.Drawing.Point(0, 280); this.checkBox_showconsole.Name = "checkBox_showconsole"; this.checkBox_showconsole.Size = new System.Drawing.Size(91, 17); this.checkBox_showconsole.TabIndex = 9; @@ -113,7 +114,7 @@ private void InitializeComponent() { // checkBox_userpc // this.checkBox_userpc.AutoSize = true; - this.checkBox_userpc.Location = new System.Drawing.Point(0, 294); + this.checkBox_userpc.Location = new System.Drawing.Point(0, 306); this.checkBox_userpc.Name = "checkBox_userpc"; this.checkBox_userpc.Size = new System.Drawing.Size(109, 17); this.checkBox_userpc.TabIndex = 10; @@ -124,7 +125,7 @@ private void InitializeComponent() { // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(5, 314); + this.label1.Location = new System.Drawing.Point(5, 326); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(70, 13); this.label1.TabIndex = 13; @@ -132,7 +133,7 @@ private void InitializeComponent() { // // numericUpDown_retryattempts // - this.numericUpDown_retryattempts.Location = new System.Drawing.Point(81, 311); + this.numericUpDown_retryattempts.Location = new System.Drawing.Point(81, 323); this.numericUpDown_retryattempts.Name = "numericUpDown_retryattempts"; this.numericUpDown_retryattempts.Size = new System.Drawing.Size(66, 20); this.numericUpDown_retryattempts.TabIndex = 14; @@ -194,7 +195,7 @@ private void InitializeComponent() { // checkBox_dontautohide // this.checkBox_dontautohide.AutoSize = true; - this.checkBox_dontautohide.Location = new System.Drawing.Point(0, 342); + this.checkBox_dontautohide.Location = new System.Drawing.Point(0, 354); this.checkBox_dontautohide.Name = "checkBox_dontautohide"; this.checkBox_dontautohide.Size = new System.Drawing.Size(70, 17); this.checkBox_dontautohide.TabIndex = 21; @@ -213,7 +214,7 @@ private void InitializeComponent() { // linkLabel1 // this.linkLabel1.AutoSize = true; - this.linkLabel1.Location = new System.Drawing.Point(5, 367); + this.linkLabel1.Location = new System.Drawing.Point(5, 379); this.linkLabel1.Name = "linkLabel1"; this.linkLabel1.Size = new System.Drawing.Size(89, 13); this.linkLabel1.TabIndex = 23; @@ -242,12 +243,23 @@ private void InitializeComponent() { this.label8.TabIndex = 25; this.label8.Text = "label8"; // + // linkLabel2 + // + this.linkLabel2.AutoSize = true; + this.linkLabel2.Location = new System.Drawing.Point(16, 264); + this.linkLabel2.Name = "linkLabel2"; + this.linkLabel2.Size = new System.Drawing.Size(55, 13); + this.linkLabel2.TabIndex = 26; + this.linkLabel2.TabStop = true; + this.linkLabel2.Text = "linkLabel2"; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.White; - this.ClientSize = new System.Drawing.Size(192, 121); + this.ClientSize = new System.Drawing.Size(856, 801); + this.Controls.Add(this.linkLabel2); this.Controls.Add(this.label8); this.Controls.Add(this.label7); this.Controls.Add(this.linkLabel1); @@ -301,6 +313,7 @@ private void InitializeComponent() { private System.Windows.Forms.ToolTip toolTip1; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label8; + private System.Windows.Forms.LinkLabel linkLabel2; } } diff --git a/Discord WMP/Form1.cs b/Discord WMP/Form1.cs index babedfe..78ad1ae 100644 --- a/Discord WMP/Form1.cs +++ b/Discord WMP/Form1.cs @@ -42,7 +42,6 @@ public partial class Form1 : Form { private bool use_rpc; public DiscordRpcClient client; public static int random_port; - public static bool isAvailable = true; public static bool albummanageropen = false; [DllImport("kernel32.dll")] @@ -89,25 +88,29 @@ public Form1() { ShowWindow(handle, SW_SHOW); Random random = new Random(); - woomy:; - random_port = random.Next(49152, 65535); - //check if port is being used and if it is, generate new one - IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); - TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); - - foreach(TcpConnectionInformation tcpi in tcpConnInfoArray) { - if(tcpi.LocalEndPoint.Port == random_port) { - isAvailable = false; - break; + bool isAvailable = false; + while(isAvailable == false) { + random_port = random.Next(49152, 65535); + //check if port is being used and if it is, generate new one + IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); + TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); + + foreach(TcpConnectionInformation tcpi in tcpConnInfoArray) { + if(tcpi.LocalEndPoint.Port == random_port) { + isAvailable = false; + continue; + } + else { + isAvailable = true; + } } } - if(isAvailable == false) { - isAvailable = true; - goto woomy; - } Console.SetWindowSize(50, 15); InitializeComponent(); + linkLabel2.Text = $"http://localhost:{random_port}/"; + linkLabel2.Links.Add(0, linkLabel2.Text.Length, $"http://localhost:{random_port}/"); + rm.Dock = DockStyle.Fill; panel1.Controls.Add(rm); @@ -134,7 +137,119 @@ public Form1() { Console.WriteLine("Loading app, please wait:)"); this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingText_Paint); } - public struct playback_data { + private void Form1_Deactivate(object sender, EventArgs e) { + //check if console is active + var handle = GetConsoleWindow(); + var foregroundHandle = GetForegroundWindow(); + bool consoleopen = (foregroundHandle == handle); + if(!albummanageropen && !consoleopen && !checkBox_dontautohide.Checked) { + Hide(); + + // Hide + ShowWindow(handle, SW_HIDE); + notifyIcon1.Visible = true; + notifyIcon1.BalloonTipText = "Veemo"; + notifyIcon1.Text = "Windows Media Player Discord RPC"; + notifyIcon1.ShowBalloonTip(1000); + } + } + private void RestoreForm() { + if(show_console) { + var handle = GetConsoleWindow(); + ShowWindow(handle, SW_SHOW); + } + Show(); + WindowState = FormWindowState.Normal; + notifyIcon1.Visible = false; + } + protected override void OnFormClosing(FormClosingEventArgs e) { + notifyIcon1.Dispose(); + base.OnFormClosing(e); + systemMediaControls._run_server = false; + } + private void Form1_Load(object sender, EventArgs e) { + Console.WriteLine("veemo"); + albummanager.LoadListFromCsv(); + if(!show_console) { + var handle = GetConsoleWindow(); + // Show console during boot + ShowWindow(handle, SW_HIDE); + } + } + + private void client_id_TextChanged(object sender, EventArgs e) { + //set rpc_id in settings1 to client_id.Text + Settings1.Default.RPC_ID = client_id.Text; + } + private static bool loadingsettings = false; + private void settingsload() { + loadingsettings = true; + show_album = Settings1.Default.show_album; + show_albumart = Settings1.Default.show_albumart; + show_author = Settings1.Default.show_author; + show_progressbar = Settings1.Default.show_progressbar; + client_id.Text = Settings1.Default.RPC_ID; + show_console = Settings1.Default.show_console; + checkBox_showconsole.Checked = show_console; + send_media_info = Settings1.Default.send_media_info; + checkBox_sendMediaInfo.Checked = send_media_info; + use_rpc = Settings1.Default.show_discord; + checkBox_userpc.Checked = use_rpc; + + Console.WriteLine("loaded settings"); + loadingsettings = false; + } + private void Form1_Closing(object sender, FormClosingEventArgs e) { + Console.WriteLine("saved settings"); + //save settings + Settings1.Default.Save(); + //close the console window + //get handlr + var handle = GetConsoleWindow(); + Application.Exit(); + } + + private void checkBox_changed(object sender, EventArgs e) { + //do nothing if settings are loading + if(loadingsettings) return; + send_media_info = checkBox_sendMediaInfo.Checked; + Settings1.Default.send_media_info = send_media_info; + show_console = checkBox_showconsole.Checked; + Settings1.Default.show_console = show_console; + use_rpc = checkBox_userpc.Checked; + Settings1.Default.show_discord = use_rpc; + + var handle = GetConsoleWindow(); + if(show_console) ShowWindow(handle, SW_SHOW); + else ShowWindow(handle, SW_HIDE); + + Console.WriteLine("saved settings"); + Settings1.Default.Save(); + } + private void button1_Click(object sender, EventArgs e) { + //open albumartadder form + albummanageropen = true; + AlbumArtAdder form = new AlbumArtAdder(); + form.Show(); + } + + private void button_resizeBack_Click(object sender, EventArgs e) { + this.Height = 160; + this.Width = 208; + } + + private void button_settings_Click(object sender, EventArgs e) { + if(this.Height > 160) { + this.Height = 160; + this.Width = 208; + } + else { + this.Width = 400; + this.Height = 428; + } + } + + public struct playback_data { public string title; public string album; public string artist; @@ -214,36 +329,6 @@ public playback_data Data() { return data; } - private void Form1_Deactivate(object sender, EventArgs e) { - //check if console is active - var handle = GetConsoleWindow(); - var foregroundHandle = GetForegroundWindow(); - bool consoleopen = (foregroundHandle == handle); - if(!albummanageropen && !consoleopen && !checkBox_dontautohide.Checked) { - Hide(); - - // Hide - ShowWindow(handle, SW_HIDE); - notifyIcon1.Visible = true; - notifyIcon1.BalloonTipText = "Veemo"; - notifyIcon1.Text = "Windows Media Player Discord RPC"; - notifyIcon1.ShowBalloonTip(1000); - } - } - private void RestoreForm() { - if(show_console) { - var handle = GetConsoleWindow(); - ShowWindow(handle, SW_SHOW); - } - Show(); - WindowState = FormWindowState.Normal; - notifyIcon1.Visible = false; - } - protected override void OnFormClosing(FormClosingEventArgs e) { - notifyIcon1.Dispose(); - base.OnFormClosing(e); - systemMediaControls._run_server = false; - } private void debug(playback_data data) { label3.Text = "initialized " + initialized.ToString(); label4.Text = "send_data_lasttime " + send_data_lasttime.ToString(); @@ -413,66 +498,6 @@ void Deinitialize() { }; client.Dispose(); } - - private void Form1_Load(object sender, EventArgs e) { - Console.WriteLine("veemo"); - albummanager.LoadListFromCsv(); - if(!show_console) { - var handle = GetConsoleWindow(); - // Show console during boot - ShowWindow(handle, SW_HIDE); - } - } - - private void client_id_TextChanged(object sender, EventArgs e) { - //set rpc_id in settings1 to client_id.Text - Settings1.Default.RPC_ID = client_id.Text; - } - private static bool loadingsettings = false; - private void settingsload() { - loadingsettings = true; - show_album = Settings1.Default.show_album; - show_albumart = Settings1.Default.show_albumart; - show_author = Settings1.Default.show_author; - show_progressbar = Settings1.Default.show_progressbar; - client_id.Text = Settings1.Default.RPC_ID; - show_console = Settings1.Default.show_console; - checkBox_showconsole.Checked = show_console; - send_media_info = Settings1.Default.send_media_info; - checkBox_sendMediaInfo.Checked = send_media_info; - use_rpc = Settings1.Default.show_discord; - checkBox_userpc.Checked = use_rpc; - - Console.WriteLine("loaded settings"); - loadingsettings = false; - } - private void Form1_Closing(object sender, FormClosingEventArgs e) { - Console.WriteLine("saved settings"); - //save settings - Settings1.Default.Save(); - //close the console window - //get handlr - var handle = GetConsoleWindow(); - Application.Exit(); - } - - private void checkBox_changed(object sender, EventArgs e) { - //do nothing if settings are loading - if(loadingsettings) return; - send_media_info = checkBox_sendMediaInfo.Checked; - Settings1.Default.send_media_info = send_media_info; - show_console = checkBox_showconsole.Checked; - Settings1.Default.show_console = show_console; - use_rpc = checkBox_userpc.Checked; - Settings1.Default.show_discord = use_rpc; - - var handle = GetConsoleWindow(); - if(show_console) ShowWindow(handle, SW_SHOW); - else ShowWindow(handle, SW_HIDE); - - Console.WriteLine("saved settings"); - Settings1.Default.Save(); - } private string progressbar(double value, int lenght) { string bar = ""; int progress = (int)(value * lenght); @@ -507,29 +532,6 @@ private void checknottoolong(string jedna, string dva, string tri, string ctyri, } } - - private void button1_Click(object sender, EventArgs e) { - //open albumartadder form - albummanageropen = true; - AlbumArtAdder form = new AlbumArtAdder(); - form.Show(); - } - - private void button_resizeBack_Click(object sender, EventArgs e) { - this.Height = 160; - this.Width = 208; - } - - private void button_settings_Click(object sender, EventArgs e) { - if(this.Height > 160) { - this.Height = 160; - this.Width = 208; - } - else { - this.Width = 400; - this.Height = 428; - } - } } //create new class for extension method public static class ExtensionMethods { diff --git a/Discord WMP/systemMediaControls.cs b/Discord WMP/systemMediaControls.cs index 1bb9757..27cb0f4 100644 --- a/Discord WMP/systemMediaControls.cs +++ b/Discord WMP/systemMediaControls.cs @@ -169,23 +169,29 @@ public static void GetAlbumArt() { static HttpListener listener = new HttpListener(); static void StartServer() { string prefix = $"http://localhost:{Form1.random_port}/"; + listener.Prefixes.Add(prefix); // Add your localhost address listener.Start(); Console.WriteLine("Listening on " + prefix); } static void ListenerCallback(IAsyncResult result) { - HttpListener listener = (HttpListener)result.AsyncState; + //HttpListener listener = (HttpListener)result.AsyncState; // Call EndGetContext to complete the asynchronous operation. - HttpListenerContext context = listener.EndGetContext(result); - /*if(context.Request.RemoteEndPoint.Address.ToString() != "localhost") { - Console.WriteLine("not localhost"); - context.Response.StatusCode = (int)HttpStatusCode.Forbidden; - context.Response.Close(); - return; - }*/ - ProcessRequest(context); - // Start listening for next request - listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); + HttpListenerContext context; + try { + context = listener.EndGetContext(result); + if(!context.Request.IsLocal) { + Console.WriteLine("request from not localhost"); + context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; + context.Response.Close(); + return; + } + ProcessRequest(context); + } + finally { + // Start listening for next request + listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); + } } static void checkRequests() { if(listener.IsListening) { @@ -194,20 +200,24 @@ static void checkRequests() { } static void ProcessRequest(HttpListenerContext context) { HttpListenerRequest request = context.Request; - HttpListenerResponse response = context.Response; + using(HttpListenerResponse response = context.Response) { + if(File.Exists(thumbnail_path)) { + response.ContentType = "image/jpeg"; - if(File.Exists(thumbnail_path)) { - byte[] buffer = File.ReadAllBytes(thumbnail_path); - response.ContentLength64 = buffer.Length; - using(Stream st = response.OutputStream) { - st.Write(buffer, 0, buffer.Length); + using(FileStream fileStream = File.OpenRead(thumbnail_path)) { + response.ContentLength64 = fileStream.Length; + + byte[] buffer = new byte[4096]; + int bytesRead; + while((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { + response.OutputStream.Write(buffer, 0, bytesRead); + } + } + } + else { + response.StatusCode = (int)HttpStatusCode.NotFound; } } - else { - response.StatusCode = (int)HttpStatusCode.NotFound; - } - - response.Close(); } } }