Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/shadps4-emu/shadPS4 into Bu…
Browse files Browse the repository at this point in the history
…ild_Pre-release
  • Loading branch information
DanielSvoboda committed Sep 20, 2024
2 parents 0405974 + 0c63267 commit 901c118
Show file tree
Hide file tree
Showing 68 changed files with 6,555 additions and 2,599 deletions.
17 changes: 14 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,18 @@ set(SYSTEM_LIBS src/core/libraries/system/commondialog.cpp
src/core/libraries/system/msgdialog_ui.cpp
src/core/libraries/system/posix.cpp
src/core/libraries/system/posix.h
src/core/libraries/save_data/error_codes.h
src/core/libraries/save_data/save_backup.cpp
src/core/libraries/save_data/save_backup.h
src/core/libraries/save_data/save_instance.cpp
src/core/libraries/save_data/save_instance.h
src/core/libraries/save_data/save_memory.cpp
src/core/libraries/save_data/save_memory.h
src/core/libraries/save_data/savedata.cpp
src/core/libraries/save_data/savedata.h
src/core/libraries/system/savedatadialog.cpp
src/core/libraries/system/savedatadialog.h
src/core/libraries/save_data/dialog/savedatadialog.cpp
src/core/libraries/save_data/dialog/savedatadialog.h
src/core/libraries/save_data/dialog/savedatadialog_ui.cpp
src/core/libraries/save_data/dialog/savedatadialog_ui.h
src/core/libraries/system/sysmodule.cpp
src/core/libraries/system/sysmodule.h
src/core/libraries/system/systemservice.cpp
Expand Down Expand Up @@ -349,6 +356,7 @@ set(COMMON src/common/logging/backend.cpp
src/common/concepts.h
src/common/config.cpp
src/common/config.h
src/common/cstring.h
src/common/debug.h
src/common/disassembler.cpp
src/common/disassembler.h
Expand Down Expand Up @@ -607,6 +615,7 @@ set(VIDEO_CORE src/video_core/amdgpu/liverpool.cpp
set(IMGUI src/imgui/imgui_config.h
src/imgui/imgui_layer.h
src/imgui/imgui_std.h
src/imgui/imgui_texture.h
src/imgui/layer/video_info.cpp
src/imgui/layer/video_info.h
src/imgui/renderer/imgui_core.cpp
Expand All @@ -615,6 +624,8 @@ set(IMGUI src/imgui/imgui_config.h
src/imgui/renderer/imgui_impl_sdl3.h
src/imgui/renderer/imgui_impl_vulkan.cpp
src/imgui/renderer/imgui_impl_vulkan.h
src/imgui/renderer/texture_manager.cpp
src/imgui/renderer/texture_manager.h
)

set(INPUT src/input/controller.cpp
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
</h1>

<h1 align="center">
<a href="https://discord.gg/MyZRaBngxA">
<a href="https://discord.gg/bFJxfftGW6">
<img src="https://img.shields.io/discord/1080089157554155590?color=5865F2&label=shadPS4 Discord&logo=Discord&logoColor=white" width="240">
<a href="https://github.com/shadps4-emu/shadPS4/releases/latest">
<img src="https://img.shields.io/github/downloads/shadps4-emu/shadPS4/total.svg" width="140">
Expand Down
52 changes: 52 additions & 0 deletions scripts/file_formats/sfo.hexpat
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

import std.io;
import std.sys;

struct Header {
u32 magic;
u32 version;
u32 key_table_offset;
u32 data_table_offset;
u32 index_table_entries;
};

struct KeyEntry {
char name[];
} [[inline]];

struct DataEntry<auto fmt, auto size> {
if (fmt == 0x0404) {
u32 int_value;
} else if(fmt == 0x0004) {
char bin_value[size];
} else if(fmt == 0x0204) {
char str_value[size];
} else {
std::warning("unknown fmt type");
}
} [[inline]];

struct IndexEntry {
u16 key_offset;
u16 param_fmt;
u32 param_len;
u32 param_max_len;
u32 data_offset;
};

struct Entry<auto KeyTableOffset, auto DataTableOffset> {
u64 begin = $;
IndexEntry index;
KeyEntry key @ KeyTableOffset + index.key_offset;
DataEntry<index.param_fmt, index.param_len> data @ DataTableOffset + index.data_offset;
u8 data_empty[index.param_max_len - index.param_len] @ DataTableOffset + index.data_offset + index.param_len;
$ = begin + sizeof(IndexEntry);
};

Header header @ 0;
std::assert(header.magic == 0x46535000, "Miss match magic");
std::assert(header.version == 0x00000101, "Miss match version");

Entry<header.key_table_offset, header.data_table_offset> list[header.index_table_entries] @ 0x14;
133 changes: 133 additions & 0 deletions src/common/cstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <string_view>

#include "assert.h"

namespace Common {

/**
* @brief A null-terminated string with a fixed maximum length
* This class is not meant to be used as a general-purpose string class
* It is meant to be used as `char[N]` where memory layout is fixed
* @tparam N Maximum length of the string
* @tparam T Type of character
*/
template <size_t N, typename T = char>
class CString {
T data[N]{};

public:
class Iterator;

CString() = default;

template <size_t M>
explicit CString(const CString<M>& other)
requires(M <= N)
{
std::ranges::copy(other.begin(), other.end(), data);
}

void FromString(const std::basic_string_view<T>& str) {
size_t p = str.copy(data, N - 1);
data[p] = '\0';
}

void Zero() {
std::ranges::fill(data, 0);
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-undefined-compare"
explicit(false) operator std::basic_string_view<T>() const {
if (this == nullptr) {
return {};
}
return std::basic_string_view<T>{data};
}

explicit operator std::basic_string<T>() const {
if (this == nullptr) {
return {};
}
return std::basic_string<T>{data};
}

std::basic_string<T> to_string() const {
if (this == nullptr) {
return {};
}
return std::basic_string<T>{data};
}

std::basic_string_view<T> to_view() const {
if (this == nullptr) {
return {};
}
return std::basic_string_view<T>{data};
}
#pragma clang diagnostic pop

char* begin() {
return data;
}

const char* begin() const {
return data;
}

char* end() {
return data + N;
}

const char* end() const {
return data + N;
}

T& operator[](size_t idx) {
return data[idx];
}

const T& operator[](size_t idx) const {
return data[idx];
}

class Iterator {
T* ptr;
T* end;

public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
using iterator_category = std::random_access_iterator_tag;

Iterator() = default;
explicit Iterator(T* ptr) : ptr(ptr), end(ptr + N) {}

Iterator& operator++() {
++ptr;
return *this;
}

Iterator operator++(int) {
Iterator tmp = *this;
++ptr;
return tmp;
}

operator T*() {
ASSERT_MSG(ptr >= end, "CString iterator out of bounds");
return ptr;
}
};
};
static_assert(sizeof(CString<13>) == sizeof(char[13])); // Ensure size still matches a simple array
static_assert(std::weakly_incrementable<CString<13>::Iterator>);

} // namespace Common
14 changes: 14 additions & 0 deletions src/common/io_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,18 @@ s64 IOFile::Tell() const {
return ftello(file);
}

u64 GetDirectorySize(const std::filesystem::path& path) {
if (!fs::exists(path)) {
return 0;
}

u64 total = 0;
for (const auto& entry : fs::recursive_directory_iterator(path)) {
if (fs::is_regular_file(entry.path())) {
total += fs::file_size(entry.path());
}
}
return total;
}

} // namespace Common::FS
2 changes: 2 additions & 0 deletions src/common/io_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,6 @@ class IOFile final {
uintptr_t file_mapping = 0;
};

u64 GetDirectorySize(const std::filesystem::path& path);

} // namespace Common::FS
8 changes: 8 additions & 0 deletions src/common/logging/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ class Impl {
initialization_in_progress_suppress_logging = false;
}

static bool IsActive() {
return instance != nullptr;
}

static void Start() {
instance->StartBackendThread();
}
Expand Down Expand Up @@ -275,6 +279,10 @@ void Initialize(std::string_view log_file) {
Impl::Initialize(log_file.empty() ? LOG_FILE : log_file);
}

bool IsActive() {
return Impl::IsActive();
}

void Start() {
Impl::Start();
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/logging/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Filter;
/// Initializes the logging system. This should be the first thing called in main.
void Initialize(std::string_view log_file = "");

bool IsActive();

/// Starts the logging threads.
void Start();

Expand Down
11 changes: 8 additions & 3 deletions src/common/string_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

namespace Common {

std::string ToLower(std::string str) {
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
std::string ToLower(std::string_view input) {
std::string str;
str.resize(input.size());
std::ranges::transform(input, str.begin(), tolower);
return str;
}

void ToLowerInPlace(std::string& str) {
std::ranges::transform(str, str.begin(), tolower);
}

std::vector<std::string> SplitString(const std::string& str, char delimiter) {
std::istringstream iss(str);
std::vector<std::string> output(1);
Expand Down
4 changes: 3 additions & 1 deletion src/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace Common {

/// Make a string lowercase
[[nodiscard]] std::string ToLower(std::string str);
[[nodiscard]] std::string ToLower(std::string_view str);

void ToLowerInPlace(std::string& str);

std::vector<std::string> SplitString(const std::string& str, char delimiter);

Expand Down
43 changes: 43 additions & 0 deletions src/common/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "common/thread.h"
#ifdef __APPLE__
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <pthread.h>
#elif defined(_WIN32)
#include <windows.h>
Expand All @@ -31,6 +32,48 @@

namespace Common {

#ifdef __APPLE__

void SetCurrentThreadRealtime(const std::chrono::nanoseconds period_ns) {
// CPU time to grant.
const std::chrono::nanoseconds computation_ns = period_ns / 2;

// Determine the timebase for converting time to ticks.
struct mach_timebase_info timebase {};
mach_timebase_info(&timebase);
const auto ticks_per_ns =
static_cast<double>(timebase.denom) / static_cast<double>(timebase.numer);

const auto period_ticks =
static_cast<u32>(static_cast<double>(period_ns.count()) * ticks_per_ns);
const auto computation_ticks =
static_cast<u32>(static_cast<double>(computation_ns.count()) * ticks_per_ns);

thread_time_constraint_policy policy = {
.period = period_ticks,
.computation = computation_ticks,
// Should not matter since preemptible is false, but needs to be >= computation regardless.
.constraint = computation_ticks,
.preemptible = false,
};

int ret = thread_policy_set(
pthread_mach_thread_np(pthread_self()), THREAD_TIME_CONSTRAINT_POLICY,
reinterpret_cast<thread_policy_t>(&policy), THREAD_TIME_CONSTRAINT_POLICY_COUNT);
if (ret != KERN_SUCCESS) {
LOG_ERROR(Common, "Could not set thread to real-time with period {} ns: {}",
period_ns.count(), ret);
}
}

#else

void SetCurrentThreadRealtime(const std::chrono::nanoseconds period_ns) {
// Not implemented
}

#endif

#ifdef _WIN32

void SetCurrentThreadPriority(ThreadPriority new_priority) {
Expand Down
Loading

0 comments on commit 901c118

Please sign in to comment.