..:: PCSX2 Forums ::..

Full Version: DS4Windows -- yet another DualShock 4 driver! (No longer under active development)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(05-17-2014, 03:49 PM)jusTiC3 Wrote: [ -> ]#zspwmw41-30362

here is the information you have requested.

see above, hopefully fixed.
thx Smile

but the error is still there.
(05-17-2014, 04:00 PM)jusTiC3 Wrote: [ -> ]thx Smile

but the error is still there.

hmm, im open to thoughts on this one lol, since it appears that its system files throwing the exception the trace info is not that helpful, coupled with the fact I cannot seem to reproduce it... at what point does the error occur, during the download or after the DL?
(05-17-2014, 04:00 PM)jusTiC3 Wrote: [ -> ]thx Smile

but the error is still there.

Try this one

Note: it will only work the first time as this now also automatically replaces the updater with with any new one it finds so until j2k adopts these changes (if he so chooses) it will replace with one where the changes have not been made.

j2k; changes to the MainWindow.xaml.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;

namespace Updater2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        WebClient wc = new WebClient();
        protected string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\DS4Tool";

        public MainWindow()
        {
            InitializeComponent();
            //double newversion;
            DLUpdate();

        }
        public class Global
        {
            public static string appdatapath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\DS4Tool";
            public static string executiblepath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName;
        }
        private void CheckIfRunning()
        {
            Process[] processes = Process.GetProcessesByName("DS4Tool");
            if (processes.Length > 0)
            {
                if (MessageBox.Show("It must be closed to update.", "DS4Tool is still running", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) == MessageBoxResult.OK)
                    for (int i = processes.Length - 1; i >= 0; i--)
                    {
                        processes[i].Kill();
                        processes[i].WaitForExit();
                    }
                else
                    this.Close();
            }
        }
        
        private void DLUpdate()
        {
            try
            {
                if (GetNewestVerNum()!=null)
                {

                    Uri url = new Uri("https://dl.dropboxusercontent.com/u/16364552/DS4Tool/DS4Tool%20-%20J2K%20%28v" + GetNewestVerNum().ToString() + "%29.zip");//Sorry other devs, gonna have to find your own server
                    sw.Start();
                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                    try { wc.DownloadFileAsync(url, path + "\\Update.zip"); }
                    catch (Exception e) { label1.Content = e.Message; }
                    wc.DownloadFileCompleted += wc_DownloadFileCompleted;
                    wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                }
            }
            catch
            {
                label1.Content = "version.txt not found, please re-run DS4Tool";
            }
        }
        private void DLVerDoc()
        {
            Uri url = new Uri("https://dl.dropboxusercontent.com/u/16364552/DS4Tool/newest%20version.txt"); //Sorry other devs, gonna have to find your own server
            Directory.CreateDirectory(Global.appdatapath);
            wc.DownloadFile(url, Global.appdatapath + "\\version.txt");
            //wc.DownloadFileCompleted += Check_Version; Not needed in this app
        }
        private double? GetNewestVerNum()
        {
            double returnVer;
            if (!File.Exists(Global.appdatapath + "\\version.txt"))
            {

                DLVerDoc();
            }
            
            if (double.TryParse(File.ReadAllText(path + "\\version.txt"), out returnVer))
            {
                return returnVer;
            }
            else
            {
                return null;
            }
        }
        Stopwatch sw = new Stopwatch();
        private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            label2.Opacity = 1;
            double speed = e.BytesReceived / sw.Elapsed.TotalSeconds;
            double timeleft = (e.TotalBytesToReceive - e.BytesReceived) / speed;
            if (timeleft > 3660)
                label2.Content = (int)timeleft / 3600 + "h left";
            else if (timeleft > 90)
                label2.Content = (int)timeleft / 60 + "m left";
            else
                label2.Content = (int)timeleft + "s left";
            UpdaterBar.Value = e.ProgressPercentage;
            TaskbarItemInfo.ProgressValue = UpdaterBar.Value / 106d;
            string convertedrev, convertedtotal;
            if (e.BytesReceived > 1024 * 1024 * 5) convertedrev = (int)(e.BytesReceived / 1024d / 1024d) + "MB";
            else convertedrev = (int)(e.BytesReceived / 1024d) + "kB";
            if (e.TotalBytesToReceive > 1024 * 1024 * 5) convertedtotal = (int)(e.TotalBytesToReceive / 1024d / 1024d) + "MB";
            else convertedtotal = (int)(e.TotalBytesToReceive / 1024d) + "kB";
            label1.Content = "Downloading update: " + convertedrev + " / " + convertedtotal;

        }

        private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            CheckIfRunning();

            sw.Reset();
            label2.Opacity = 0;
            label1.Content = "Deleting old files";
            UpdaterBar.Value = 102;
            TaskbarItemInfo.ProgressValue = UpdaterBar.Value / 106d;
            //File.Delete("DS4Tool.exe");
            //File.Delete("DS4Control.dll");
            //File.Delete("DS4Library.dll");
            //File.Delete("HidLibrary.dll");
            label1.Content = "Installing new files";
            UpdaterBar.Value = 104;
            TaskbarItemInfo.ProgressValue = UpdaterBar.Value / 106d;

            string SourcePath = path + "\\Update.zip";
            string DestinationPath = Global.executiblepath;

            using (ZipArchive archive = ZipFile.OpenRead(SourcePath))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {

                    try { entry.ExtractToFile(System.IO.Path.Combine(DestinationPath, entry.FullName),true);}
                    catch (IOException) { }
                }
            }

            //try { ZipFile.ExtractToDirectory(path + "\\Update.zip", Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName); }
            //catch (IOException) { } //Since profiles may be in the zip ignore them if already exists
            try {
            File.Delete(path + "\\version.txt");
            File.Delete(path + "\\Update.zip");
            }
            catch (IOException) { }


                label1.Content = "Update complete";
            UpdaterBar.Value = 106;
            TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
            btnOpenDS4.IsEnabled = true;












        }


        private void btnChangelog_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("https://docs.google.com/document/d/1l4xcgVQkGUskc5CQ0p069yW22Cd5WAH_yE3Fz2hXo0E/edit?usp=sharing");
        }

        private void btnOpenDS4_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("DS4Tool.exe");
            this.Close();
        }
    }
}

changes to App.xaml.cs that allow for self replacement of updater;
Code:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Reflection;


namespace Updater2
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public class Global
        {
            public static string appdatapath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\DS4Tool";
            public static string executiblepath = System.IO.Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName;
        }
        public App()
        {
            //If the code for the Updator ever gets adopted, uncomment these lines and it should create a automatic updator replacer.

                this.Exit += (s, e) =>
                {
                    if (System.IO.File.Exists(System.IO.Path.Combine(Global.executiblepath, "Updater NEW.exe")))
                    {
                        var myTempBatFilePath = System.IO.Path.Combine(Global.executiblepath, "UpdateReplacer.bat");
                        System.IO.StreamWriter w = new System.IO.StreamWriter(myTempBatFilePath);
                        w.WriteLine("@echo off"); // Turn off echo
                        w.WriteLine("@echo Attempting to replace updater, please wait...");
                        w.WriteLine("@ping -n 4 127.0.0.1 > nul"); //Its silly but its the most compatible way to call for a timeout in a batch file, used to give the main updater time to cleanup and exit.
                        w.WriteLine("@del \"" + (System.IO.Path.Combine(Global.executiblepath, "Updater.exe")) + "\""); // Out with the old
                        w.WriteLine("@ren \"" + (System.IO.Path.Combine(Global.executiblepath, "Updater NEW.exe")) + "\" \"Updater.exe\""); // In with the new
                        w.WriteLine("@DEL \"%~f0\""); // Attempt to delete myself without opening a time paradox.

                        w.Close();

                        System.Diagnostics.Process.Start(System.IO.Path.Combine(Global.executiblepath, "UpdateReplacer.bat"));
                    }
               };
            
        }


    }
}
(05-17-2014, 05:37 PM)jhebbel Wrote: [ -> ]Try this one

Note: it will only work the first time as this now also automatically replaces the updater with with any new one it finds so until j2k adopts these changes (if he so chooses) it will replace with one where the changes have not been made.

j2k; changes to the MainWindow.xaml.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;

namespace Updater2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        WebClient wc = new WebClient();
        protected string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\DS4Tool";

        public MainWindow()
        {
            InitializeComponent();
            //double newversion;
            DLUpdate();

        }
        public class Global
        {
            public static string appdatapath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\DS4Tool";
            public static string executiblepath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName;
        }
        private void CheckIfRunning()
        {
            Process[] processes = Process.GetProcessesByName("DS4Tool");
            if (processes.Length > 0)
            {
                if (MessageBox.Show("It must be closed to update.", "DS4Tool is still running", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) == MessageBoxResult.OK)
                    for (int i = processes.Length - 1; i >= 0; i--)
                    {
                        processes[i].Kill();
                        processes[i].WaitForExit();
                    }
                else
                    this.Close();
            }
        }
        
        private void DLUpdate()
        {
            try
            {
                if (GetNewestVerNum()!=null)
                {

                    Uri url = new Uri("https://dl.dropboxusercontent.com/u/16364552/DS4Tool/DS4Tool%20-%20J2K%20%28v" + GetNewestVerNum().ToString() + "%29.zip");//Sorry other devs, gonna have to find your own server
                    sw.Start();
                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                    try { wc.DownloadFileAsync(url, path + "\\Update.zip"); }
                    catch (Exception e) { label1.Content = e.Message; }
                    wc.DownloadFileCompleted += wc_DownloadFileCompleted;
                    wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                }
            }
            catch
            {
                label1.Content = "version.txt not found, please re-run DS4Tool";
            }
        }
        private void DLVerDoc()
        {
            Uri url = new Uri("https://dl.dropboxusercontent.com/u/16364552/DS4Tool/newest%20version.txt"); //Sorry other devs, gonna have to find your own server
            Directory.CreateDirectory(Global.appdatapath);
            wc.DownloadFile(url, Global.appdatapath + "\\version.txt");
            //wc.DownloadFileCompleted += Check_Version; Not needed in this app
        }
        private double? GetNewestVerNum()
        {
            double returnVer;
            if (!File.Exists(Global.appdatapath + "\\version.txt"))
            {

                DLVerDoc();
            }
            
            if (double.TryParse(File.ReadAllText(path + "\\version.txt"), out returnVer))
            {
                return returnVer;
            }
            else
            {
                return null;
            }
        }
        Stopwatch sw = new Stopwatch();
        private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            label2.Opacity = 1;
            double speed = e.BytesReceived / sw.Elapsed.TotalSeconds;
            double timeleft = (e.TotalBytesToReceive - e.BytesReceived) / speed;
            if (timeleft > 3660)
                label2.Content = (int)timeleft / 3600 + "h left";
            else if (timeleft > 90)
                label2.Content = (int)timeleft / 60 + "m left";
            else
                label2.Content = (int)timeleft + "s left";
            UpdaterBar.Value = e.ProgressPercentage;
            TaskbarItemInfo.ProgressValue = UpdaterBar.Value / 106d;
            string convertedrev, convertedtotal;
            if (e.BytesReceived > 1024 * 1024 * 5) convertedrev = (int)(e.BytesReceived / 1024d / 1024d) + "MB";
            else convertedrev = (int)(e.BytesReceived / 1024d) + "kB";
            if (e.TotalBytesToReceive > 1024 * 1024 * 5) convertedtotal = (int)(e.TotalBytesToReceive / 1024d / 1024d) + "MB";
            else convertedtotal = (int)(e.TotalBytesToReceive / 1024d) + "kB";
            label1.Content = "Downloading update: " + convertedrev + " / " + convertedtotal;

        }

        private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            CheckIfRunning();

            sw.Reset();
            label2.Opacity = 0;
            label1.Content = "Deleting old files";
            UpdaterBar.Value = 102;
            TaskbarItemInfo.ProgressValue = UpdaterBar.Value / 106d;
            //File.Delete("DS4Tool.exe");
            //File.Delete("DS4Control.dll");
            //File.Delete("DS4Library.dll");
            //File.Delete("HidLibrary.dll");
            label1.Content = "Installing new files";
            UpdaterBar.Value = 104;
            TaskbarItemInfo.ProgressValue = UpdaterBar.Value / 106d;

            string SourcePath = path + "\\Update.zip";
            string DestinationPath = Global.executiblepath;

            using (ZipArchive archive = ZipFile.OpenRead(SourcePath))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {

                    try { entry.ExtractToFile(System.IO.Path.Combine(DestinationPath, entry.FullName),true);}
                    catch (IOException) { }
                }
            }

            //try { ZipFile.ExtractToDirectory(path + "\\Update.zip", Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName); }
            //catch (IOException) { } //Since profiles may be in the zip ignore them if already exists
            try {
            File.Delete(path + "\\version.txt");
            File.Delete(path + "\\Update.zip");
            }
            catch (IOException) { }


                label1.Content = "Update complete";
            UpdaterBar.Value = 106;
            TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
            btnOpenDS4.IsEnabled = true;












        }


        private void btnChangelog_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("https://docs.google.com/document/d/1l4xcgVQkGUskc5CQ0p069yW22Cd5WAH_yE3Fz2hXo0E/edit?usp=sharing");
        }

        private void btnOpenDS4_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("DS4Tool.exe");
            this.Close();
        }
    }
}

changes to App.xaml.cs that allow for self replacement of updater;
Code:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Reflection;


namespace Updater2
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public class Global
        {
            public static string appdatapath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\DS4Tool";
            public static string executiblepath = System.IO.Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName;
        }
        public App()
        {
            //If the code for the Updator ever gets adopted, uncomment these lines and it should create a automatic updator replacer.

                this.Exit += (s, e) =>
                {
                    if (System.IO.File.Exists(System.IO.Path.Combine(Global.executiblepath, "Updater NEW.exe")))
                    {
                        var myTempBatFilePath = System.IO.Path.Combine(Global.executiblepath, "UpdateReplacer.bat");
                        System.IO.StreamWriter w = new System.IO.StreamWriter(myTempBatFilePath);
                        w.WriteLine("@echo off"); // Turn off echo
                        w.WriteLine("@echo Attempting to replace updater, please wait...");
                        w.WriteLine("@ping -n 4 127.0.0.1 > nul"); //Its silly but its the most compatible way to call for a timeout in a batch file, used to give the main updater time to cleanup and exit.
                        w.WriteLine("@del \"" + (System.IO.Path.Combine(Global.executiblepath, "Updater.exe")) + "\""); // Out with the old
                        w.WriteLine("@ren \"" + (System.IO.Path.Combine(Global.executiblepath, "Updater NEW.exe")) + "\" \"Updater.exe\""); // In with the new
                        w.WriteLine("@DEL \"%~f0\""); // Attempt to delete myself without opening a time paradox.

                        w.Close();

                        System.Diagnostics.Process.Start(System.IO.Path.Combine(Global.executiblepath, "UpdateReplacer.bat"));
                    }
               };
            
        }


    }
}

While I do like the idea of a way to make sure the updater gets an update as well, The idea of the updater was never meant for it to able to run by itself. It is that way so my server isn't harassed by people constantly running the updater. I should probably change the text from version.txt no found to "You are up to date" or actually I have another way of incorporating the version into the updater as well.

I didn't want to have to make another update to the updater again but that would be great to have it update itself, but I still rather not have the updater work on it's own.
(05-17-2014, 07:34 PM)Jays2Kings Wrote: [ -> ]While I do like the idea of a way to make sure the updater gets an update as well, The idea of the updater was never meant for it to able to run by itself. It is that way so my server isn't harassed by people constantly running the updater. I should probably change the text from version.txt no found to "You are up to date" or actually I have another way of incorporating the version into the updater as well.

I didn't want to have to make another update to the updater again but that would be great to have it update itself, but I still rather not have the updater work on it's own.

Its absolutely doable without the additional requests, the only thing disallowing it is that ur using a hardcoded version variable in the ds4tool which as no way to access outside the code instead of updating the version in the manifest which gets added to the exe's meta info which can be accessed by any app. Making that change would allow for the if availableVer > currentVer logic inside the checker itself.

-Edit-
Sorry, ment the AssembilyInfo, not the manifest, manifest version is useless, its the AssembilyInfo.cs stuff that gets slapped into the exe's meta using FileVersionInfo.GetVersionInfo(pathToExe)
(05-17-2014, 08:16 PM)jhebbel Wrote: [ -> ]Its absolutely doable without the additional requests, the only thing disallowing it is that ur using a hardcoded version variable in the ds4tool which as no way to access outside the code instead of updating the version in the manifest which gets added to the exe's meta info which can be accessed by any app. Making that change would allow for the if availableVer > currentVer logic inside the checker itself.

Well I got it fixed up now. I put the version number in the settings (Profiles.xml in appdata). That way both ds4tool and the updater can access it. The updater can now run on it's own, but it still won't download an update unless the ds4tool is old. I also took your code for the .bat file to update, it's very good thanks, so once you manually update the updater, it will update itself from here on out. I still rather use my hard coded version number since 1) that version number contains multiple dots so I can't just convert it to a double/int/long/etc. 2) I'm lazy Tongue. But it's not in a file so both programs can access it.

This version of the DS4tool adds the version number to it's settings, aside from that, there's nothing new.
(05-17-2014, 09:47 PM)Jays2Kings Wrote: [ -> ]Well I got it fixed up now. I put the version number in the settings (Profiles.xml in appdata). That way both ds4tool and the updater can access it. The updater can now run on it's own, but it still won't download an update unless the ds4tool is old. I also took your code for the .bat file to update, it's very good thanks, so once you manually update the updater, it will update itself from here on out. I still rather use my hard coded version number since 1) that version number contains multiple dots so I can't just convert it to a double/int/long/etc. 2) I'm lazy Tongue. But it's not in a file so both programs can access it.

This version of the DS4tool adds the version number to it's settings, aside from that, there's nothing new.

Do you mind If i fork your version to fix some bugs and add some features?
(05-17-2014, 09:53 PM)jhebbel Wrote: [ -> ]Do you mind If i fork your version to fix some bugs and add some features?

Not at all, feel free to.
(05-17-2014, 09:54 PM)Jays2Kings Wrote: [ -> ]Not at all, feel free to.

Cool, spent the day trying to figure out why SVN doesn't like google code for some reason, gonna try GIT if i can't get it working soon. In the meantime here is my project page http://dsdcs.com/index.php/portfolio/sof...ds4windows and my current version is below.