From 29bb82cb8e5c34eba174964099b2cdc04103de7b Mon Sep 17 00:00:00 2001
From: freiro <freiro@users.noreply.github.com>
Date: Thu, 17 Nov 2016 01:33:16 +0100
Subject: [PATCH 1/5] Win32 move default user folder location to AppData

---
 src/common/file_util.cpp | 23 +++++++++++++++++++++++
 src/common/file_util.h   |  1 +
 2 files changed, 24 insertions(+)

diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 407ed047af..8a8ff3092b 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -26,6 +26,9 @@
 #define stat _stat64
 #define fstat _fstat64
 #define fileno _fileno
+// Windows version, at least Vista is required to obtain AppData Path
+#define WINVER 0x0600
+#define _WIN32_WINNT 0x0600
 #else
 #ifdef __APPLE__
 #include <sys/param.h>
@@ -594,6 +597,21 @@ std::string& GetExeDirectory() {
     }
     return exe_path;
 }
+
+std::string& AppDataLocalDirectory() {
+    // Windows Vista or later only
+    static std::string local_path;
+    if (local_path.empty()) {
+        PWSTR pw_local_path = 0;
+        wchar_t* wchar_local_path;
+        SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &pw_local_path);
+        wchar_local_path = pw_local_path;
+        local_path = Common::UTF16ToUTF8(wchar_local_path);
+        // Freeing memory
+        CoTaskMemFree(static_cast<void*>(pw_local_path));
+    }
+    return local_path;
+}
 #else
 /**
  * @return The user’s home directory on POSIX systems
@@ -671,6 +689,11 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string& new
     if (paths[D_USER_IDX].empty()) {
 #ifdef _WIN32
         paths[D_USER_IDX] = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
+        if (!FileUtil::IsDirectory(paths[D_USER_IDX])) {
+            paths[D_USER_IDX] =
+                AppDataLocalDirectory() + DIR_SEP + EMU_DATA_DIR DIR_SEP USERDATA_DIR DIR_SEP;
+        }
+
         paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP;
         paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP;
 #else
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 204b06f145..03cb222fef 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -154,6 +154,7 @@ std::string GetBundleDirectory();
 
 #ifdef _WIN32
 std::string& GetExeDirectory();
+std::string& AppDataLocalDirectory();
 #endif
 
 size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename);

From 3d75e3cd07a8c577d74aa1c7017c7ee5019e632d Mon Sep 17 00:00:00 2001
From: freiro <f73b2894@opayq.com>
Date: Thu, 17 Nov 2016 12:29:57 +0100
Subject: [PATCH 2/5] Return by value and other fixes

---
 src/common/file_util.cpp | 20 +++++++-------------
 src/common/file_util.h   |  2 +-
 2 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 8a8ff3092b..a1c12cbce3 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -598,18 +598,12 @@ std::string& GetExeDirectory() {
     return exe_path;
 }
 
-std::string& AppDataLocalDirectory() {
-    // Windows Vista or later only
-    static std::string local_path;
-    if (local_path.empty()) {
-        PWSTR pw_local_path = 0;
-        wchar_t* wchar_local_path;
-        SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &pw_local_path);
-        wchar_local_path = pw_local_path;
-        local_path = Common::UTF16ToUTF8(wchar_local_path);
-        // Freeing memory
-        CoTaskMemFree(static_cast<void*>(pw_local_path));
-    }
+std::string AppDataLocalDirectory() {
+    PWSTR pw_local_path = nullptr;
+    // Only supported by Windows Vista or later
+    SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &pw_local_path);
+    std::string local_path = Common::UTF16ToUTF8(pw_local_path);
+    CoTaskMemFree(pw_local_path);
     return local_path;
 }
 #else
@@ -691,7 +685,7 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string& new
         paths[D_USER_IDX] = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
         if (!FileUtil::IsDirectory(paths[D_USER_IDX])) {
             paths[D_USER_IDX] =
-                AppDataLocalDirectory() + DIR_SEP + EMU_DATA_DIR DIR_SEP USERDATA_DIR DIR_SEP;
+                AppDataLocalDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP USERDATA_DIR DIR_SEP;
         }
 
         paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP;
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 03cb222fef..4c5ab676e1 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -154,7 +154,7 @@ std::string GetBundleDirectory();
 
 #ifdef _WIN32
 std::string& GetExeDirectory();
-std::string& AppDataLocalDirectory();
+std::string AppDataLocalDirectory();
 #endif
 
 size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename);

From 79317b63d9c509bae2457c7c52ea3b1e71c07ea0 Mon Sep 17 00:00:00 2001
From: freiro <f73b2894@opayq.com>
Date: Thu, 24 Nov 2016 16:42:31 +0100
Subject: [PATCH 3/5] Switch to AppData/Roaming

---
 src/common/file_util.cpp | 6 +++---
 src/common/file_util.h   | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index a1c12cbce3..6661fe96a0 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -598,10 +598,10 @@ std::string& GetExeDirectory() {
     return exe_path;
 }
 
-std::string AppDataLocalDirectory() {
+std::string AppDataRoamingDirectory() {
     PWSTR pw_local_path = nullptr;
     // Only supported by Windows Vista or later
-    SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &pw_local_path);
+    SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &pw_local_path);
     std::string local_path = Common::UTF16ToUTF8(pw_local_path);
     CoTaskMemFree(pw_local_path);
     return local_path;
@@ -685,7 +685,7 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string& new
         paths[D_USER_IDX] = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
         if (!FileUtil::IsDirectory(paths[D_USER_IDX])) {
             paths[D_USER_IDX] =
-                AppDataLocalDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP USERDATA_DIR DIR_SEP;
+                AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP USERDATA_DIR DIR_SEP;
         }
 
         paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP;
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 4c5ab676e1..ac58607c59 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -154,7 +154,7 @@ std::string GetBundleDirectory();
 
 #ifdef _WIN32
 std::string& GetExeDirectory();
-std::string AppDataLocalDirectory();
+std::string AppDataRoamingDirectory();
 #endif
 
 size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename);

From 972b289c787c6f6cc4f097d838b697a767b1642f Mon Sep 17 00:00:00 2001
From: freiro <f73b2894@opayq.com>
Date: Sat, 26 Nov 2016 01:00:52 +0100
Subject: [PATCH 4/5] Removed /user/ from path

---
 src/common/file_util.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 6661fe96a0..413a8e7e59 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -684,8 +684,7 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string& new
 #ifdef _WIN32
         paths[D_USER_IDX] = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
         if (!FileUtil::IsDirectory(paths[D_USER_IDX])) {
-            paths[D_USER_IDX] =
-                AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP USERDATA_DIR DIR_SEP;
+            paths[D_USER_IDX] = AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP;
         }
 
         paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP;

From 593ec7fa74cf01819b0e3e29ddd3b1d3ba339090 Mon Sep 17 00:00:00 2001
From: freiro <f73b2894@opayq.com>
Date: Sat, 26 Nov 2016 22:57:36 +0100
Subject: [PATCH 5/5] Move to AppData/Roaming/Citra/

---
 src/common/common_paths.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/common/common_paths.h b/src/common/common_paths.h
index a5342a6109..37304d2363 100644
--- a/src/common/common_paths.h
+++ b/src/common/common_paths.h
@@ -19,7 +19,7 @@
 #define EMU_DATA_DIR USER_DIR
 #else
 #ifdef _WIN32
-#define EMU_DATA_DIR "Citra Emulator"
+#define EMU_DATA_DIR "Citra"
 #else
 #define EMU_DATA_DIR "citra-emu"
 #endif