From bb87b72b917b96d2a81db28335ea9876cdb448bf Mon Sep 17 00:00:00 2001
From: Dmitry Isaenko <developer.su@gmail.com>
Date: Mon, 11 Feb 2019 04:26:51 +0300
Subject: [PATCH] Windows support implemented and tested.

---
 README.md                                     | 26 ++++++++++++++++---
 .../java/nsusbloader/UsbCommunications.java   |  5 ++--
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index 4c35c46..08f12d8 100644
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@ Source code spreads under the GNU General Public License v.3. You can find it in
 
 ## Requirements
 
-JRE 8 or higher.
+JRE 8 or higher. See below.
 
 ## Used libraries
 * OpenJFX https://wiki.openjdk.java.net/display/OpenJFX/Main
@@ -17,12 +17,29 @@ JRE 8 or higher.
 * Few icons taken from: http://materialdesignicons.com/
 
 ## Usage
-Linux:
+### Linux:
+
+Install JRE/JDK 8 or higher (openJDK is good. Oracle's one is also good). JavaFX not needed, if you're interested (it's embedded).
 
 `root # java -jar /path/to/NS-USBloader.jar`
 
+### Windows: 
+
+* Download Zadig: https://zadig.akeo.ie/
+* Open tinfoil. Set 'Title Managment' -> 'Usb install NSP'
+* Connect NS to pc
+* Open Zadig, select NS in dropdown, select 'libusbK (v3.0.7.0)' (version may vary), click 'Install WCID Driver'
+* Check that in device list of you system you have 'libusbK USB Devices' folder and your NS inside of it
+* Download and install Java JRE (8+)
+* Get this application (JAR file) double-click on on it (alternatively open 'cmd', go to place where jar located and execute via 'java -jar thisAppName.jar')
+* Remember to have fun!
+
+### macOS
+
+Coming...
+
 ## Tips&tricks
-### Add user to udev rules to use NS non-root:
+### Add user to udev rules to use NS non-root (Linux):
 `root # vim /etc/udev/rules.d/99-NS.rules`
 `SUBSYSTEM=="usb", ATTRS{idVendor}=="057e", ATTRS{idProduct}=="3000", GROUP="plugdev"`
 `root # udevadm control --reload-rules && udevadm trigger`
@@ -32,4 +49,5 @@ Linux:
 
 ## TODO:
 - [ ] macOS QA
-- [ ] Windows support
\ No newline at end of file
+- [x] Windows support
+- [ ] code refactoring
\ No newline at end of file
diff --git a/src/main/java/nsusbloader/UsbCommunications.java b/src/main/java/nsusbloader/UsbCommunications.java
index baee27f..8ba6f1c 100644
--- a/src/main/java/nsusbloader/UsbCommunications.java
+++ b/src/main/java/nsusbloader/UsbCommunications.java
@@ -336,7 +336,7 @@ class UsbCommunications extends Task<Void> {
      * After we sent commands to NS, this chain starts
      * */
     private void proceedCommands(){
-        printLog("Waiting for NS commands.", MsgType.INFO);
+        printLog("Awaiting for NS commands.", MsgType.INFO);
 
         /*  byte[] magic = new byte[4];
             ByteBuffer bb = StandardCharsets.UTF_8.encode("TUC0").rewind().get(magic);
@@ -565,7 +565,7 @@ class UsbCommunications extends Task<Void> {
         IntBuffer readBufTransferred = IntBuffer.allocate(1);
 
         int result;
-        result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 0);  // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
+        result = LibUsb.bulkTransfer(handlerNS, (byte) 0x81, readBuffer, readBufTransferred, 5000);  // last one is TIMEOUT. 0 stands for unlimited. Endpoint IN = 0x81
 
         if (result != LibUsb.SUCCESS){
             switch (result){
@@ -588,7 +588,6 @@ class UsbCommunications extends Task<Void> {
             return null;
         } else {
             // DEBUG START----------------------------------------------------------------------------------------------
-            readBuffer.rewind();
             int trans = readBufTransferred.get();
             byte[] receivedBytes = new byte[trans];
             readBuffer.get(receivedBytes);