diff --git a/OpenFK/ConfigForm.Designer.cs b/OpenFK/ConfigForm.Designer.cs index 22dffe8..67b5aed 100644 --- a/OpenFK/ConfigForm.Designer.cs +++ b/OpenFK/ConfigForm.Designer.cs @@ -35,6 +35,7 @@ this.ExitButton = new System.Windows.Forms.Button(); this.header = new System.Windows.Forms.Label(); this.rdfToggle = new System.Windows.Forms.CheckBox(); + this.USBToggle = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // QualityCB @@ -44,7 +45,7 @@ "0 - High", "1 - Medium", "2 - Low"}); - this.QualityCB.Location = new System.Drawing.Point(12, 104); + this.QualityCB.Location = new System.Drawing.Point(12, 129); this.QualityCB.Name = "QualityCB"; this.QualityCB.Size = new System.Drawing.Size(121, 21); this.QualityCB.TabIndex = 0; @@ -80,7 +81,7 @@ "1 - Crop 4:3", "2 - Stretch 4:3", "3 - Dot by Dot"}); - this.ScaleCB.Location = new System.Drawing.Point(12, 131); + this.ScaleCB.Location = new System.Drawing.Point(12, 156); this.ScaleCB.Name = "ScaleCB"; this.ScaleCB.Size = new System.Drawing.Size(121, 21); this.ScaleCB.TabIndex = 3; @@ -88,7 +89,7 @@ // // ExitButton // - this.ExitButton.Location = new System.Drawing.Point(12, 158); + this.ExitButton.Location = new System.Drawing.Point(12, 183); this.ExitButton.Name = "ExitButton"; this.ExitButton.Size = new System.Drawing.Size(75, 23); this.ExitButton.TabIndex = 4; @@ -116,11 +117,23 @@ this.rdfToggle.UseVisualStyleBackColor = true; this.rdfToggle.CheckedChanged += new System.EventHandler(this.rdfToggle_CheckedChanged); // + // USBToggle + // + this.USBToggle.AutoSize = true; + this.USBToggle.Location = new System.Drawing.Point(15, 104); + this.USBToggle.Name = "USBToggle"; + this.USBToggle.Size = new System.Drawing.Size(88, 17); + this.USBToggle.TabIndex = 8; + this.USBToggle.Text = "USB Support"; + this.USBToggle.UseVisualStyleBackColor = true; + this.USBToggle.CheckedChanged += new System.EventHandler(this.USBToggle_CheckedChanged); + // // ConfigForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(173, 193); + this.ClientSize = new System.Drawing.Size(173, 218); + this.Controls.Add(this.USBToggle); this.Controls.Add(this.rdfToggle); this.Controls.Add(this.header); this.Controls.Add(this.ExitButton); @@ -146,5 +159,6 @@ private System.Windows.Forms.Button ExitButton; private System.Windows.Forms.Label header; private System.Windows.Forms.CheckBox rdfToggle; + private System.Windows.Forms.CheckBox USBToggle; } } \ No newline at end of file diff --git a/OpenFK/ConfigForm.cs b/OpenFK/ConfigForm.cs index 013db00..9bf556e 100644 --- a/OpenFK/ConfigForm.cs +++ b/OpenFK/ConfigForm.cs @@ -20,6 +20,7 @@ namespace OpenFK rdfToggle.Checked = Properties.Settings.Default.RDF; QualityCB.SelectedIndex = Properties.Settings.Default.Quality; ScaleCB.SelectedIndex = Properties.Settings.Default.ScaleMode; + USBToggle.Checked = Properties.Settings.Default.USBSupport; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) @@ -71,5 +72,16 @@ namespace OpenFK Properties.Settings.Default.RDF = false; Properties.Settings.Default.Save(); } + + private void USBToggle_CheckedChanged(object sender, EventArgs e) + { + if (USBToggle.Checked) + { + Properties.Settings.Default.USBSupport = true; + } + else + Properties.Settings.Default.USBSupport = false; + Properties.Settings.Default.Save(); + } } } diff --git a/OpenFK/Form1.cs b/OpenFK/Form1.cs index 1e74f54..17725f9 100644 --- a/OpenFK/Form1.cs +++ b/OpenFK/Form1.cs @@ -8,9 +8,11 @@ using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; +using System.IO.Pipes; using System.Linq; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -50,6 +52,19 @@ namespace OpenFK public bool DebugMB; public bool DebugOnline; + //MegaByte Data + private System.Windows.Forms.Timer bittyTimer; //Timer to check connected bitty. + + [DllImport("kernel32.dll")] + public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); + + [DllImport("kernel32.dll")] + public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead); + const int PROCESS_WM_READ = 0x0010; + + public string bittyID; //Current bitty connected. + + //Items public XmlDocument bittyData; public DiscordRpcClient client; @@ -124,26 +139,75 @@ namespace OpenFK //USB Initialization - //MegaByte (Not functional) - - DebugMB = false; - - if (DebugMB == true) + if (Settings.Default.USBSupport == true) { Process MBRun = new Process(); ProcessStartInfo MBData = new ProcessStartInfo(); MBData.FileName = Directory.GetParent(Directory.GetCurrentDirectory()) + @"\MegaByte\" + "MegaByte.exe"; MBData.Arguments = "-MBRun -MBDebug"; - MBData.RedirectStandardOutput = true; - MBData.RedirectStandardInput = true; MBData.UseShellExecute = false; MBRun.StartInfo = MBData; MBRun.Start(); + InitTimer(); } //End of USB Initialization } + // + //USB READING + // + public void InitTimer() + { + bittyTimer = new System.Windows.Forms.Timer(); + bittyTimer.Tick += new EventHandler(bittyT_Tick); + bittyTimer.Interval = 1000; + bittyTimer.Start(); + } + + private void bittyT_Tick(object sender, EventArgs e) + { + try + { + Process process = Process.GetProcessesByName("MegaByte")[0]; //Get the process + int bytesRead = 0; + IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id); + byte[] buffer = new byte[4]; //BittyID is 4 bytes + + //This reads the memory to fetch the current bittyID + ReadProcessMemory((int)processHandle, 0x0049F020, buffer, buffer.Length, ref bytesRead); + Int32 baseValue = BitConverter.ToInt32(buffer, 0); + + Int32 firstAddress = baseValue + 0xF88; + ReadProcessMemory((int)processHandle, firstAddress, buffer, buffer.Length, ref bytesRead); + Int32 firstValue = BitConverter.ToInt32(buffer, 0); + + ReadProcessMemory((int)processHandle, firstValue, buffer, buffer.Length, ref bytesRead); + + //Converts bytes to BittyID + int bittyIDInt = BitConverter.ToInt32(buffer, 0); + string s = bittyIDInt.ToString("X").PadLeft(8, '0'); + if (s == "00000000") //If no bitty is connected. + { + s = "FFFFFFF0"; + } + if (s != bittyID) //If it's still the same, it won't repeat the actions + { + Debug.WriteLine("BittyID - " + s); + bittyID = s; + setBitty(s); + } + } + catch + { + + } + } + // + //END OF USB READING + // + + private void flashPlayerAS3_FlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e) { Debug.WriteLine("NEW AS3 CALL!" + " - " + e.request); @@ -331,6 +395,11 @@ namespace OpenFK { client.Dispose(); //Disposes RP } + if(Settings.Default.USBSupport == true) + { + Process process = Process.GetProcessesByName("MegaByte")[0]; + process.Kill(); + } Application.Exit(); //Closes OpenFK Debug.WriteLine("radicaclose called, goodbye!"); //Debug output } @@ -438,6 +507,11 @@ namespace OpenFK { client.Dispose(); //Disposes RP } + if (Settings.Default.USBSupport == true) + { + Process process = Process.GetProcessesByName("MegaByte")[0]; + process.Kill(); + } Application.Exit(); //Closes OpenFK } } @@ -524,12 +598,22 @@ namespace OpenFK { setVar(@""); currentBitty = bittyID.ToLower(); - XmlNodeList nodes = bittyData.SelectNodes("//funkey[@id='" + bittyID + "']"); - foreach (XmlNode xn in nodes) + if (Settings.Default.RPC == true) { - currentBittyName = xn.Attributes["name"].Value; + try + { + XmlNodeList nodes = bittyData.SelectNodes("//funkey[@id='" + bittyID + "']"); + foreach (XmlNode xn in nodes) + { + currentBittyName = xn.Attributes["name"].Value; + } + setRP(currentWorld, currentActivity, currentBitty, currentBittyName); + } + catch + { + + } } - setRP(currentWorld, currentActivity, currentBitty, currentBittyName); } // diff --git a/OpenFK/Properties/Settings.Designer.cs b/OpenFK/Properties/Settings.Designer.cs index c4a9e3f..a4a6ae5 100644 --- a/OpenFK/Properties/Settings.Designer.cs +++ b/OpenFK/Properties/Settings.Designer.cs @@ -37,7 +37,7 @@ namespace OpenFK.Properties { [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("True")] + [global::System.Configuration.DefaultSettingValueAttribute("False")] public bool RPC { get { return ((bool)(this["RPC"])); @@ -82,5 +82,17 @@ namespace OpenFK.Properties { this["RDF"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool USBSupport { + get { + return ((bool)(this["USBSupport"])); + } + set { + this["USBSupport"] = value; + } + } } } diff --git a/OpenFK/Properties/Settings.settings b/OpenFK/Properties/Settings.settings index 2af0a9c..d658842 100644 --- a/OpenFK/Properties/Settings.settings +++ b/OpenFK/Properties/Settings.settings @@ -6,7 +6,7 @@ True - True + False 0 @@ -17,5 +17,8 @@ True + + False + \ No newline at end of file diff --git a/OpenFK/app.config b/OpenFK/app.config index ee4548a..0985fec 100644 --- a/OpenFK/app.config +++ b/OpenFK/app.config @@ -1,17 +1,17 @@ - + - +
- + True - True + False 0 @@ -22,6 +22,17 @@ True + + False + + + + + + + + +