diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/common_funcs.h | 4 | ||||
| -rw-r--r-- | src/common/common_paths.h | 1 | ||||
| -rw-r--r-- | src/common/file_util.cpp | 14 | ||||
| -rw-r--r-- | src/common/file_util.h | 3 | ||||
| -rw-r--r-- | src/common/logging/backend.cpp | 16 | ||||
| -rw-r--r-- | src/common/logging/log.h | 16 | ||||
| -rw-r--r-- | src/common/logging/text_formatter.h | 1 | ||||
| -rw-r--r-- | src/common/math_util.h | 10 | ||||
| -rw-r--r-- | src/common/string_util.cpp | 8 | ||||
| -rw-r--r-- | src/common/swap.h | 2 | ||||
| -rw-r--r-- | src/common/threadsafe_queue.h | 32 | ||||
| -rw-r--r-- | src/common/timer.cpp | 95 | ||||
| -rw-r--r-- | src/common/timer.h | 17 |
13 files changed, 124 insertions, 95 deletions
diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 93f1c0044..8b0d34da6 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h | |||
| @@ -6,7 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <string> | 7 | #include <string> |
| 8 | 8 | ||
| 9 | #if !defined(ARCHITECTURE_x86_64) && !defined(ARCHITECTURE_ARM) | 9 | #if !defined(ARCHITECTURE_x86_64) |
| 10 | #include <cstdlib> // for exit | 10 | #include <cstdlib> // for exit |
| 11 | #endif | 11 | #endif |
| 12 | #include "common/common_types.h" | 12 | #include "common/common_types.h" |
| @@ -32,8 +32,6 @@ | |||
| 32 | 32 | ||
| 33 | #ifdef ARCHITECTURE_x86_64 | 33 | #ifdef ARCHITECTURE_x86_64 |
| 34 | #define Crash() __asm__ __volatile__("int $3") | 34 | #define Crash() __asm__ __volatile__("int $3") |
| 35 | #elif defined(ARCHITECTURE_ARM) | ||
| 36 | #define Crash() __asm__ __volatile__("trap") | ||
| 37 | #else | 35 | #else |
| 38 | #define Crash() exit(1) | 36 | #define Crash() exit(1) |
| 39 | #endif | 37 | #endif |
diff --git a/src/common/common_paths.h b/src/common/common_paths.h index 6799a357a..df2ce80b1 100644 --- a/src/common/common_paths.h +++ b/src/common/common_paths.h | |||
| @@ -32,6 +32,7 @@ | |||
| 32 | #define SDMC_DIR "sdmc" | 32 | #define SDMC_DIR "sdmc" |
| 33 | #define NAND_DIR "nand" | 33 | #define NAND_DIR "nand" |
| 34 | #define SYSDATA_DIR "sysdata" | 34 | #define SYSDATA_DIR "sysdata" |
| 35 | #define KEYS_DIR "keys" | ||
| 35 | #define LOG_DIR "log" | 36 | #define LOG_DIR "log" |
| 36 | 37 | ||
| 37 | // Filenames | 38 | // Filenames |
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index b8dd92b65..7aeda737f 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -706,6 +706,7 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { | |||
| 706 | paths.emplace(UserPath::SDMCDir, user_path + SDMC_DIR DIR_SEP); | 706 | paths.emplace(UserPath::SDMCDir, user_path + SDMC_DIR DIR_SEP); |
| 707 | paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP); | 707 | paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP); |
| 708 | paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP); | 708 | paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP); |
| 709 | paths.emplace(UserPath::KeysDir, user_path + KEYS_DIR DIR_SEP); | ||
| 709 | // TODO: Put the logs in a better location for each OS | 710 | // TODO: Put the logs in a better location for each OS |
| 710 | paths.emplace(UserPath::LogDir, user_path + LOG_DIR DIR_SEP); | 711 | paths.emplace(UserPath::LogDir, user_path + LOG_DIR DIR_SEP); |
| 711 | } | 712 | } |
| @@ -736,6 +737,19 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) { | |||
| 736 | return paths[path]; | 737 | return paths[path]; |
| 737 | } | 738 | } |
| 738 | 739 | ||
| 740 | std::string GetHactoolConfigurationPath() { | ||
| 741 | #ifdef _WIN32 | ||
| 742 | PWSTR pw_local_path = nullptr; | ||
| 743 | if (SHGetKnownFolderPath(FOLDERID_Profile, 0, nullptr, &pw_local_path) != S_OK) | ||
| 744 | return ""; | ||
| 745 | std::string local_path = Common::UTF16ToUTF8(pw_local_path); | ||
| 746 | CoTaskMemFree(pw_local_path); | ||
| 747 | return local_path + "\\.switch"; | ||
| 748 | #else | ||
| 749 | return GetHomeDirectory() + "/.switch"; | ||
| 750 | #endif | ||
| 751 | } | ||
| 752 | |||
| 739 | size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) { | 753 | size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) { |
| 740 | return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size()); | 754 | return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size()); |
| 741 | } | 755 | } |
diff --git a/src/common/file_util.h b/src/common/file_util.h index bc9272d89..28697d527 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -23,6 +23,7 @@ namespace FileUtil { | |||
| 23 | enum class UserPath { | 23 | enum class UserPath { |
| 24 | CacheDir, | 24 | CacheDir, |
| 25 | ConfigDir, | 25 | ConfigDir, |
| 26 | KeysDir, | ||
| 26 | LogDir, | 27 | LogDir, |
| 27 | NANDDir, | 28 | NANDDir, |
| 28 | RootDir, | 29 | RootDir, |
| @@ -125,6 +126,8 @@ bool SetCurrentDir(const std::string& directory); | |||
| 125 | // directory. To be used in "multi-user" mode (that is, installed). | 126 | // directory. To be used in "multi-user" mode (that is, installed). |
| 126 | const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); | 127 | const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); |
| 127 | 128 | ||
| 129 | std::string GetHactoolConfigurationPath(); | ||
| 130 | |||
| 128 | // Returns the path to where the sys file are | 131 | // Returns the path to where the sys file are |
| 129 | std::string GetSysDirectory(); | 132 | std::string GetSysDirectory(); |
| 130 | 133 | ||
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index ad9edbcdf..355abd682 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp | |||
| @@ -168,26 +168,41 @@ void FileBackend::Write(const Entry& entry) { | |||
| 168 | SUB(Service, AM) \ | 168 | SUB(Service, AM) \ |
| 169 | SUB(Service, AOC) \ | 169 | SUB(Service, AOC) \ |
| 170 | SUB(Service, APM) \ | 170 | SUB(Service, APM) \ |
| 171 | SUB(Service, ARP) \ | ||
| 171 | SUB(Service, BCAT) \ | 172 | SUB(Service, BCAT) \ |
| 173 | SUB(Service, BPC) \ | ||
| 174 | SUB(Service, BTM) \ | ||
| 175 | SUB(Service, Capture) \ | ||
| 172 | SUB(Service, Fatal) \ | 176 | SUB(Service, Fatal) \ |
| 177 | SUB(Service, FGM) \ | ||
| 173 | SUB(Service, Friend) \ | 178 | SUB(Service, Friend) \ |
| 174 | SUB(Service, FS) \ | 179 | SUB(Service, FS) \ |
| 175 | SUB(Service, HID) \ | 180 | SUB(Service, HID) \ |
| 181 | SUB(Service, LBL) \ | ||
| 176 | SUB(Service, LDN) \ | 182 | SUB(Service, LDN) \ |
| 177 | SUB(Service, LM) \ | 183 | SUB(Service, LM) \ |
| 184 | SUB(Service, Migration) \ | ||
| 185 | SUB(Service, Mii) \ | ||
| 178 | SUB(Service, MM) \ | 186 | SUB(Service, MM) \ |
| 187 | SUB(Service, NCM) \ | ||
| 188 | SUB(Service, NFC) \ | ||
| 179 | SUB(Service, NFP) \ | 189 | SUB(Service, NFP) \ |
| 180 | SUB(Service, NIFM) \ | 190 | SUB(Service, NIFM) \ |
| 181 | SUB(Service, NS) \ | 191 | SUB(Service, NS) \ |
| 182 | SUB(Service, NVDRV) \ | 192 | SUB(Service, NVDRV) \ |
| 193 | SUB(Service, PCIE) \ | ||
| 183 | SUB(Service, PCTL) \ | 194 | SUB(Service, PCTL) \ |
| 195 | SUB(Service, PCV) \ | ||
| 184 | SUB(Service, PREPO) \ | 196 | SUB(Service, PREPO) \ |
| 197 | SUB(Service, PSC) \ | ||
| 185 | SUB(Service, SET) \ | 198 | SUB(Service, SET) \ |
| 186 | SUB(Service, SM) \ | 199 | SUB(Service, SM) \ |
| 187 | SUB(Service, SPL) \ | 200 | SUB(Service, SPL) \ |
| 188 | SUB(Service, SSL) \ | 201 | SUB(Service, SSL) \ |
| 189 | SUB(Service, Time) \ | 202 | SUB(Service, Time) \ |
| 203 | SUB(Service, USB) \ | ||
| 190 | SUB(Service, VI) \ | 204 | SUB(Service, VI) \ |
| 205 | SUB(Service, WLAN) \ | ||
| 191 | CLS(HW) \ | 206 | CLS(HW) \ |
| 192 | SUB(HW, Memory) \ | 207 | SUB(HW, Memory) \ |
| 193 | SUB(HW, LCD) \ | 208 | SUB(HW, LCD) \ |
| @@ -204,6 +219,7 @@ void FileBackend::Write(const Entry& entry) { | |||
| 204 | CLS(Input) \ | 219 | CLS(Input) \ |
| 205 | CLS(Network) \ | 220 | CLS(Network) \ |
| 206 | CLS(Loader) \ | 221 | CLS(Loader) \ |
| 222 | CLS(Crypto) \ | ||
| 207 | CLS(WebService) | 223 | CLS(WebService) |
| 208 | 224 | ||
| 209 | // GetClassName is a macro defined by Windows.h, grrr... | 225 | // GetClassName is a macro defined by Windows.h, grrr... |
diff --git a/src/common/logging/log.h b/src/common/logging/log.h index ad3cbf5d1..a889ebefa 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h | |||
| @@ -54,27 +54,42 @@ enum class Class : ClassType { | |||
| 54 | Service_AM, ///< The AM (Applet manager) service | 54 | Service_AM, ///< The AM (Applet manager) service |
| 55 | Service_AOC, ///< The AOC (AddOn Content) service | 55 | Service_AOC, ///< The AOC (AddOn Content) service |
| 56 | Service_APM, ///< The APM (Performance) service | 56 | Service_APM, ///< The APM (Performance) service |
| 57 | Service_ARP, ///< The ARP service | ||
| 57 | Service_Audio, ///< The Audio (Audio control) service | 58 | Service_Audio, ///< The Audio (Audio control) service |
| 58 | Service_BCAT, ///< The BCAT service | 59 | Service_BCAT, ///< The BCAT service |
| 60 | Service_BPC, ///< The BPC service | ||
| 61 | Service_BTM, ///< The BTM service | ||
| 62 | Service_Capture, ///< The capture service | ||
| 59 | Service_Fatal, ///< The Fatal service | 63 | Service_Fatal, ///< The Fatal service |
| 64 | Service_FGM, ///< The FGM service | ||
| 60 | Service_Friend, ///< The friend service | 65 | Service_Friend, ///< The friend service |
| 61 | Service_FS, ///< The FS (Filesystem) service | 66 | Service_FS, ///< The FS (Filesystem) service |
| 62 | Service_HID, ///< The HID (Human interface device) service | 67 | Service_HID, ///< The HID (Human interface device) service |
| 68 | Service_LBL, ///< The LBL (LCD backlight) service | ||
| 63 | Service_LDN, ///< The LDN (Local domain network) service | 69 | Service_LDN, ///< The LDN (Local domain network) service |
| 64 | Service_LM, ///< The LM (Logger) service | 70 | Service_LM, ///< The LM (Logger) service |
| 71 | Service_Migration, ///< The migration service | ||
| 72 | Service_Mii, ///< The Mii service | ||
| 65 | Service_MM, ///< The MM (Multimedia) service | 73 | Service_MM, ///< The MM (Multimedia) service |
| 74 | Service_NCM, ///< The NCM service | ||
| 75 | Service_NFC, ///< The NFC (Near-field communication) service | ||
| 66 | Service_NFP, ///< The NFP service | 76 | Service_NFP, ///< The NFP service |
| 67 | Service_NIFM, ///< The NIFM (Network interface) service | 77 | Service_NIFM, ///< The NIFM (Network interface) service |
| 68 | Service_NS, ///< The NS services | 78 | Service_NS, ///< The NS services |
| 69 | Service_NVDRV, ///< The NVDRV (Nvidia driver) service | 79 | Service_NVDRV, ///< The NVDRV (Nvidia driver) service |
| 80 | Service_PCIE, ///< The PCIe service | ||
| 70 | Service_PCTL, ///< The PCTL (Parental control) service | 81 | Service_PCTL, ///< The PCTL (Parental control) service |
| 82 | Service_PCV, ///< The PCV service | ||
| 71 | Service_PREPO, ///< The PREPO (Play report) service | 83 | Service_PREPO, ///< The PREPO (Play report) service |
| 84 | Service_PSC, ///< The PSC service | ||
| 72 | Service_SET, ///< The SET (Settings) service | 85 | Service_SET, ///< The SET (Settings) service |
| 73 | Service_SM, ///< The SM (Service manager) service | 86 | Service_SM, ///< The SM (Service manager) service |
| 74 | Service_SPL, ///< The SPL service | 87 | Service_SPL, ///< The SPL service |
| 75 | Service_SSL, ///< The SSL service | 88 | Service_SSL, ///< The SSL service |
| 76 | Service_Time, ///< The time service | 89 | Service_Time, ///< The time service |
| 90 | Service_USB, ///< The USB (Universal Serial Bus) service | ||
| 77 | Service_VI, ///< The VI (Video interface) service | 91 | Service_VI, ///< The VI (Video interface) service |
| 92 | Service_WLAN, ///< The WLAN (Wireless local area network) service | ||
| 78 | HW, ///< Low-level hardware emulation | 93 | HW, ///< Low-level hardware emulation |
| 79 | HW_Memory, ///< Memory-map and address translation | 94 | HW_Memory, ///< Memory-map and address translation |
| 80 | HW_LCD, ///< LCD register emulation | 95 | HW_LCD, ///< LCD register emulation |
| @@ -89,6 +104,7 @@ enum class Class : ClassType { | |||
| 89 | Audio_DSP, ///< The HLE implementation of the DSP | 104 | Audio_DSP, ///< The HLE implementation of the DSP |
| 90 | Audio_Sink, ///< Emulator audio output backend | 105 | Audio_Sink, ///< Emulator audio output backend |
| 91 | Loader, ///< ROM loader | 106 | Loader, ///< ROM loader |
| 107 | Crypto, ///< Cryptographic engine/functions | ||
| 92 | Input, ///< Input emulation | 108 | Input, ///< Input emulation |
| 93 | Network, ///< Network emulation | 109 | Network, ///< Network emulation |
| 94 | WebService, ///< Interface to yuzu Web Services | 110 | WebService, ///< Interface to yuzu Web Services |
diff --git a/src/common/logging/text_formatter.h b/src/common/logging/text_formatter.h index c587faefb..9609cec7c 100644 --- a/src/common/logging/text_formatter.h +++ b/src/common/logging/text_formatter.h | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <cstddef> | 7 | #include <cstddef> |
| 8 | #include <string> | ||
| 8 | 9 | ||
| 9 | namespace Log { | 10 | namespace Log { |
| 10 | 11 | ||
diff --git a/src/common/math_util.h b/src/common/math_util.h index c6a83c953..343cdd902 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h | |||
| @@ -19,12 +19,12 @@ inline bool IntervalsIntersect(unsigned start0, unsigned length0, unsigned start | |||
| 19 | 19 | ||
| 20 | template <class T> | 20 | template <class T> |
| 21 | struct Rectangle { | 21 | struct Rectangle { |
| 22 | T left; | 22 | T left{}; |
| 23 | T top; | 23 | T top{}; |
| 24 | T right; | 24 | T right{}; |
| 25 | T bottom; | 25 | T bottom{}; |
| 26 | 26 | ||
| 27 | Rectangle() {} | 27 | Rectangle() = default; |
| 28 | 28 | ||
| 29 | Rectangle(T left, T top, T right, T bottom) | 29 | Rectangle(T left, T top, T right, T bottom) |
| 30 | : left(left), top(top), right(right), bottom(bottom) {} | 30 | : left(left), top(top), right(right), bottom(bottom) {} |
diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 1f0456aee..0ca663032 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp | |||
| @@ -2,12 +2,12 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 5 | #include <cctype> | 6 | #include <cctype> |
| 6 | #include <cerrno> | 7 | #include <cerrno> |
| 7 | #include <cstdio> | 8 | #include <cstdio> |
| 8 | #include <cstdlib> | 9 | #include <cstdlib> |
| 9 | #include <cstring> | 10 | #include <cstring> |
| 10 | #include <boost/range/algorithm/transform.hpp> | ||
| 11 | #include "common/common_paths.h" | 11 | #include "common/common_paths.h" |
| 12 | #include "common/logging/log.h" | 12 | #include "common/logging/log.h" |
| 13 | #include "common/string_util.h" | 13 | #include "common/string_util.h" |
| @@ -24,13 +24,15 @@ namespace Common { | |||
| 24 | 24 | ||
| 25 | /// Make a string lowercase | 25 | /// Make a string lowercase |
| 26 | std::string ToLower(std::string str) { | 26 | std::string ToLower(std::string str) { |
| 27 | boost::transform(str, str.begin(), ::tolower); | 27 | std::transform(str.begin(), str.end(), str.begin(), |
| 28 | [](unsigned char c) { return std::tolower(c); }); | ||
| 28 | return str; | 29 | return str; |
| 29 | } | 30 | } |
| 30 | 31 | ||
| 31 | /// Make a string uppercase | 32 | /// Make a string uppercase |
| 32 | std::string ToUpper(std::string str) { | 33 | std::string ToUpper(std::string str) { |
| 33 | boost::transform(str, str.begin(), ::toupper); | 34 | std::transform(str.begin(), str.end(), str.begin(), |
| 35 | [](unsigned char c) { return std::toupper(c); }); | ||
| 34 | return str; | 36 | return str; |
| 35 | } | 37 | } |
| 36 | 38 | ||
diff --git a/src/common/swap.h b/src/common/swap.h index fc7af4280..32af0b6ac 100644 --- a/src/common/swap.h +++ b/src/common/swap.h | |||
| @@ -69,7 +69,7 @@ inline u32 swap32(u32 _data) { | |||
| 69 | inline u64 swap64(u64 _data) { | 69 | inline u64 swap64(u64 _data) { |
| 70 | return _byteswap_uint64(_data); | 70 | return _byteswap_uint64(_data); |
| 71 | } | 71 | } |
| 72 | #elif ARCHITECTURE_ARM | 72 | #elif defined(ARCHITECTURE_ARM) && (__ARM_ARCH >= 6) |
| 73 | inline u16 swap16(u16 _data) { | 73 | inline u16 swap16(u16 _data) { |
| 74 | u32 data = _data; | 74 | u32 data = _data; |
| 75 | __asm__("rev16 %0, %1\n" : "=l"(data) : "l"(data)); | 75 | __asm__("rev16 %0, %1\n" : "=l"(data) : "l"(data)); |
diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index a0c731e8c..edf13bc49 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h | |||
| @@ -33,9 +33,11 @@ public: | |||
| 33 | bool Empty() const { | 33 | bool Empty() const { |
| 34 | return !read_ptr->next.load(); | 34 | return !read_ptr->next.load(); |
| 35 | } | 35 | } |
| 36 | |||
| 36 | T& Front() const { | 37 | T& Front() const { |
| 37 | return read_ptr->current; | 38 | return read_ptr->current; |
| 38 | } | 39 | } |
| 40 | |||
| 39 | template <typename Arg> | 41 | template <typename Arg> |
| 40 | void Push(Arg&& t) { | 42 | void Push(Arg&& t) { |
| 41 | // create the element, add it to the queue | 43 | // create the element, add it to the queue |
| @@ -108,15 +110,41 @@ private: | |||
| 108 | // single reader, multiple writer queue | 110 | // single reader, multiple writer queue |
| 109 | 111 | ||
| 110 | template <typename T, bool NeedSize = true> | 112 | template <typename T, bool NeedSize = true> |
| 111 | class MPSCQueue : public SPSCQueue<T, NeedSize> { | 113 | class MPSCQueue { |
| 112 | public: | 114 | public: |
| 115 | u32 Size() const { | ||
| 116 | return spsc_queue.Size(); | ||
| 117 | } | ||
| 118 | |||
| 119 | bool Empty() const { | ||
| 120 | return spsc_queue.Empty(); | ||
| 121 | } | ||
| 122 | |||
| 123 | T& Front() const { | ||
| 124 | return spsc_queue.Front(); | ||
| 125 | } | ||
| 126 | |||
| 113 | template <typename Arg> | 127 | template <typename Arg> |
| 114 | void Push(Arg&& t) { | 128 | void Push(Arg&& t) { |
| 115 | std::lock_guard<std::mutex> lock(write_lock); | 129 | std::lock_guard<std::mutex> lock(write_lock); |
| 116 | SPSCQueue<T, NeedSize>::Push(t); | 130 | spsc_queue.Push(t); |
| 131 | } | ||
| 132 | |||
| 133 | void Pop() { | ||
| 134 | return spsc_queue.Pop(); | ||
| 135 | } | ||
| 136 | |||
| 137 | bool Pop(T& t) { | ||
| 138 | return spsc_queue.Pop(t); | ||
| 139 | } | ||
| 140 | |||
| 141 | // not thread-safe | ||
| 142 | void Clear() { | ||
| 143 | spsc_queue.Clear(); | ||
| 117 | } | 144 | } |
| 118 | 145 | ||
| 119 | private: | 146 | private: |
| 147 | SPSCQueue<T, NeedSize> spsc_queue; | ||
| 120 | std::mutex write_lock; | 148 | std::mutex write_lock; |
| 121 | }; | 149 | }; |
| 122 | } // namespace Common | 150 | } // namespace Common |
diff --git a/src/common/timer.cpp b/src/common/timer.cpp index f0c5b1a43..2dc15e434 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp | |||
| @@ -3,31 +3,16 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <ctime> | 5 | #include <ctime> |
| 6 | |||
| 7 | #include <fmt/format.h> | 6 | #include <fmt/format.h> |
| 8 | |||
| 9 | #ifdef _WIN32 | ||
| 10 | #include <windows.h> | ||
| 11 | // windows.h needs to be included before other windows headers | ||
| 12 | #include <mmsystem.h> | ||
| 13 | #include <sys/timeb.h> | ||
| 14 | #else | ||
| 15 | #include <sys/time.h> | ||
| 16 | #endif | ||
| 17 | #include "common/common_types.h" | 7 | #include "common/common_types.h" |
| 18 | #include "common/string_util.h" | 8 | #include "common/string_util.h" |
| 19 | #include "common/timer.h" | 9 | #include "common/timer.h" |
| 20 | 10 | ||
| 21 | namespace Common { | 11 | namespace Common { |
| 22 | 12 | ||
| 23 | u32 Timer::GetTimeMs() { | 13 | std::chrono::milliseconds Timer::GetTimeMs() { |
| 24 | #ifdef _WIN32 | 14 | return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 25 | return timeGetTime(); | 15 | std::chrono::system_clock::now().time_since_epoch()); |
| 26 | #else | ||
| 27 | struct timeval t; | ||
| 28 | (void)gettimeofday(&t, nullptr); | ||
| 29 | return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000)); | ||
| 30 | #endif | ||
| 31 | } | 16 | } |
| 32 | 17 | ||
| 33 | // -------------------------------------------- | 18 | // -------------------------------------------- |
| @@ -63,7 +48,7 @@ void Timer::Update() { | |||
| 63 | // ------------------------------------- | 48 | // ------------------------------------- |
| 64 | 49 | ||
| 65 | // Get the number of milliseconds since the last Update() | 50 | // Get the number of milliseconds since the last Update() |
| 66 | u64 Timer::GetTimeDifference() { | 51 | std::chrono::milliseconds Timer::GetTimeDifference() { |
| 67 | return GetTimeMs() - m_LastTime; | 52 | return GetTimeMs() - m_LastTime; |
| 68 | } | 53 | } |
| 69 | 54 | ||
| @@ -74,11 +59,11 @@ void Timer::AddTimeDifference() { | |||
| 74 | } | 59 | } |
| 75 | 60 | ||
| 76 | // Get the time elapsed since the Start() | 61 | // Get the time elapsed since the Start() |
| 77 | u64 Timer::GetTimeElapsed() { | 62 | std::chrono::milliseconds Timer::GetTimeElapsed() { |
| 78 | // If we have not started yet, return 1 (because then I don't | 63 | // If we have not started yet, return 1 (because then I don't |
| 79 | // have to change the FPS calculation in CoreRerecording.cpp . | 64 | // have to change the FPS calculation in CoreRerecording.cpp . |
| 80 | if (m_StartTime == 0) | 65 | if (m_StartTime.count() == 0) |
| 81 | return 1; | 66 | return std::chrono::milliseconds(1); |
| 82 | 67 | ||
| 83 | // Return the final timer time if the timer is stopped | 68 | // Return the final timer time if the timer is stopped |
| 84 | if (!m_Running) | 69 | if (!m_Running) |
| @@ -90,49 +75,34 @@ u64 Timer::GetTimeElapsed() { | |||
| 90 | // Get the formatted time elapsed since the Start() | 75 | // Get the formatted time elapsed since the Start() |
| 91 | std::string Timer::GetTimeElapsedFormatted() const { | 76 | std::string Timer::GetTimeElapsedFormatted() const { |
| 92 | // If we have not started yet, return zero | 77 | // If we have not started yet, return zero |
| 93 | if (m_StartTime == 0) | 78 | if (m_StartTime.count() == 0) |
| 94 | return "00:00:00:000"; | 79 | return "00:00:00:000"; |
| 95 | 80 | ||
| 96 | // The number of milliseconds since the start. | 81 | // The number of milliseconds since the start. |
| 97 | // Use a different value if the timer is stopped. | 82 | // Use a different value if the timer is stopped. |
| 98 | u64 Milliseconds; | 83 | std::chrono::milliseconds Milliseconds; |
| 99 | if (m_Running) | 84 | if (m_Running) |
| 100 | Milliseconds = GetTimeMs() - m_StartTime; | 85 | Milliseconds = GetTimeMs() - m_StartTime; |
| 101 | else | 86 | else |
| 102 | Milliseconds = m_LastTime - m_StartTime; | 87 | Milliseconds = m_LastTime - m_StartTime; |
| 103 | // Seconds | 88 | // Seconds |
| 104 | u32 Seconds = (u32)(Milliseconds / 1000); | 89 | std::chrono::seconds Seconds = std::chrono::duration_cast<std::chrono::seconds>(Milliseconds); |
| 105 | // Minutes | 90 | // Minutes |
| 106 | u32 Minutes = Seconds / 60; | 91 | std::chrono::minutes Minutes = std::chrono::duration_cast<std::chrono::minutes>(Milliseconds); |
| 107 | // Hours | 92 | // Hours |
| 108 | u32 Hours = Minutes / 60; | 93 | std::chrono::hours Hours = std::chrono::duration_cast<std::chrono::hours>(Milliseconds); |
| 109 | 94 | ||
| 110 | std::string TmpStr = fmt::format("{:02}:{:02}:{:02}:{:03}", Hours, Minutes % 60, Seconds % 60, | 95 | std::string TmpStr = fmt::format("{:02}:{:02}:{:02}:{:03}", Hours.count(), Minutes.count() % 60, |
| 111 | Milliseconds % 1000); | 96 | Seconds.count() % 60, Milliseconds.count() % 1000); |
| 112 | return TmpStr; | 97 | return TmpStr; |
| 113 | } | 98 | } |
| 114 | 99 | ||
| 115 | // Get current time | ||
| 116 | void Timer::IncreaseResolution() { | ||
| 117 | #ifdef _WIN32 | ||
| 118 | timeBeginPeriod(1); | ||
| 119 | #endif | ||
| 120 | } | ||
| 121 | |||
| 122 | void Timer::RestoreResolution() { | ||
| 123 | #ifdef _WIN32 | ||
| 124 | timeEndPeriod(1); | ||
| 125 | #endif | ||
| 126 | } | ||
| 127 | |||
| 128 | // Get the number of seconds since January 1 1970 | 100 | // Get the number of seconds since January 1 1970 |
| 129 | u64 Timer::GetTimeSinceJan1970() { | 101 | std::chrono::seconds Timer::GetTimeSinceJan1970() { |
| 130 | time_t ltime; | 102 | return std::chrono::duration_cast<std::chrono::seconds>(GetTimeMs()); |
| 131 | time(<ime); | ||
| 132 | return ((u64)ltime); | ||
| 133 | } | 103 | } |
| 134 | 104 | ||
| 135 | u64 Timer::GetLocalTimeSinceJan1970() { | 105 | std::chrono::seconds Timer::GetLocalTimeSinceJan1970() { |
| 136 | time_t sysTime, tzDiff, tzDST; | 106 | time_t sysTime, tzDiff, tzDST; |
| 137 | struct tm* gmTime; | 107 | struct tm* gmTime; |
| 138 | 108 | ||
| @@ -149,7 +119,7 @@ u64 Timer::GetLocalTimeSinceJan1970() { | |||
| 149 | gmTime = gmtime(&sysTime); | 119 | gmTime = gmtime(&sysTime); |
| 150 | tzDiff = sysTime - mktime(gmTime); | 120 | tzDiff = sysTime - mktime(gmTime); |
| 151 | 121 | ||
| 152 | return (u64)(sysTime + tzDiff + tzDST); | 122 | return std::chrono::seconds(sysTime + tzDiff + tzDST); |
| 153 | } | 123 | } |
| 154 | 124 | ||
| 155 | // Return the current time formatted as Minutes:Seconds:Milliseconds | 125 | // Return the current time formatted as Minutes:Seconds:Milliseconds |
| @@ -164,30 +134,16 @@ std::string Timer::GetTimeFormatted() { | |||
| 164 | 134 | ||
| 165 | strftime(tmp, 6, "%M:%S", gmTime); | 135 | strftime(tmp, 6, "%M:%S", gmTime); |
| 166 | 136 | ||
| 167 | // Now tack on the milliseconds | 137 | u64 milliseconds = static_cast<u64>(GetTimeMs().count()) % 1000; |
| 168 | #ifdef _WIN32 | 138 | return fmt::format("{}:{:03}", tmp, milliseconds); |
| 169 | struct timeb tp; | ||
| 170 | (void)::ftime(&tp); | ||
| 171 | return fmt::format("{}:{:03}", tmp, tp.millitm); | ||
| 172 | #else | ||
| 173 | struct timeval t; | ||
| 174 | (void)gettimeofday(&t, nullptr); | ||
| 175 | return fmt::format("{}:{:03}", tmp, static_cast<int>(t.tv_usec / 1000)); | ||
| 176 | #endif | ||
| 177 | } | 139 | } |
| 178 | 140 | ||
| 179 | // Returns a timestamp with decimals for precise time comparisons | 141 | // Returns a timestamp with decimals for precise time comparisons |
| 180 | // ---------------- | 142 | // ---------------- |
| 181 | double Timer::GetDoubleTime() { | 143 | double Timer::GetDoubleTime() { |
| 182 | #ifdef _WIN32 | ||
| 183 | struct timeb tp; | ||
| 184 | (void)::ftime(&tp); | ||
| 185 | #else | ||
| 186 | struct timeval t; | ||
| 187 | (void)gettimeofday(&t, nullptr); | ||
| 188 | #endif | ||
| 189 | // Get continuous timestamp | 144 | // Get continuous timestamp |
| 190 | u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970(); | 145 | u64 TmpSeconds = static_cast<u64>(Common::Timer::GetTimeSinceJan1970().count()); |
| 146 | double ms = static_cast<u64>(GetTimeMs().count()) % 1000; | ||
| 191 | 147 | ||
| 192 | // Remove a few years. We only really want enough seconds to make | 148 | // Remove a few years. We only really want enough seconds to make |
| 193 | // sure that we are detecting actual actions, perhaps 60 seconds is | 149 | // sure that we are detecting actual actions, perhaps 60 seconds is |
| @@ -196,12 +152,7 @@ double Timer::GetDoubleTime() { | |||
| 196 | TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60); | 152 | TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60); |
| 197 | 153 | ||
| 198 | // Make a smaller integer that fits in the double | 154 | // Make a smaller integer that fits in the double |
| 199 | u32 Seconds = (u32)TmpSeconds; | 155 | u32 Seconds = static_cast<u32>(TmpSeconds); |
| 200 | #ifdef _WIN32 | ||
| 201 | double ms = tp.millitm / 1000.0 / 1000.0; | ||
| 202 | #else | ||
| 203 | double ms = t.tv_usec / 1000000.0; | ||
| 204 | #endif | ||
| 205 | double TmpTime = Seconds + ms; | 156 | double TmpTime = Seconds + ms; |
| 206 | 157 | ||
| 207 | return TmpTime; | 158 | return TmpTime; |
diff --git a/src/common/timer.h b/src/common/timer.h index 78d37426b..27b521baa 100644 --- a/src/common/timer.h +++ b/src/common/timer.h | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <chrono> | ||
| 7 | #include <string> | 8 | #include <string> |
| 8 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 9 | 10 | ||
| @@ -18,24 +19,22 @@ public: | |||
| 18 | 19 | ||
| 19 | // The time difference is always returned in milliseconds, regardless of alternative internal | 20 | // The time difference is always returned in milliseconds, regardless of alternative internal |
| 20 | // representation | 21 | // representation |
| 21 | u64 GetTimeDifference(); | 22 | std::chrono::milliseconds GetTimeDifference(); |
| 22 | void AddTimeDifference(); | 23 | void AddTimeDifference(); |
| 23 | 24 | ||
| 24 | static void IncreaseResolution(); | 25 | static std::chrono::seconds GetTimeSinceJan1970(); |
| 25 | static void RestoreResolution(); | 26 | static std::chrono::seconds GetLocalTimeSinceJan1970(); |
| 26 | static u64 GetTimeSinceJan1970(); | ||
| 27 | static u64 GetLocalTimeSinceJan1970(); | ||
| 28 | static double GetDoubleTime(); | 27 | static double GetDoubleTime(); |
| 29 | 28 | ||
| 30 | static std::string GetTimeFormatted(); | 29 | static std::string GetTimeFormatted(); |
| 31 | std::string GetTimeElapsedFormatted() const; | 30 | std::string GetTimeElapsedFormatted() const; |
| 32 | u64 GetTimeElapsed(); | 31 | std::chrono::milliseconds GetTimeElapsed(); |
| 33 | 32 | ||
| 34 | static u32 GetTimeMs(); | 33 | static std::chrono::milliseconds GetTimeMs(); |
| 35 | 34 | ||
| 36 | private: | 35 | private: |
| 37 | u64 m_LastTime; | 36 | std::chrono::milliseconds m_LastTime; |
| 38 | u64 m_StartTime; | 37 | std::chrono::milliseconds m_StartTime; |
| 39 | bool m_Running; | 38 | bool m_Running; |
| 40 | }; | 39 | }; |
| 41 | 40 | ||