summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--.travis-build-docker.sh2
-rwxr-xr-x.travis-build.sh2
-rw-r--r--CMakeLists.txt28
-rw-r--r--CMakeModules/DownloadExternals.cmake18
-rw-r--r--appveyor.yml11
-rw-r--r--dist/icons/checked.pngbin0 -> 451 bytes
-rw-r--r--dist/icons/failed.pngbin0 -> 428 bytes
-rw-r--r--dist/icons/icons.qrc6
-rw-r--r--externals/CMakeLists.txt19
-rw-r--r--src/citra/config.cpp2
-rw-r--r--src/citra/default_ini.h2
-rw-r--r--src/citra_qt/CMakeLists.txt5
-rw-r--r--src/citra_qt/configuration/config.cpp6
-rw-r--r--src/citra_qt/configuration/configure_web.cpp58
-rw-r--r--src/citra_qt/configuration/configure_web.h12
-rw-r--r--src/citra_qt/configuration/configure_web.ui75
-rw-r--r--src/core/CMakeLists.txt6
-rw-r--r--src/core/arm/dynarmic/arm_dynarmic.cpp4
-rw-r--r--src/core/core.cpp1
-rw-r--r--src/core/hle/kernel/memory.cpp30
-rw-r--r--src/core/hle/kernel/memory.h2
-rw-r--r--src/core/hle/kernel/thread.cpp12
-rw-r--r--src/core/hle/kernel/vm_manager.cpp13
-rw-r--r--src/core/hle/kernel/vm_manager.h6
-rw-r--r--src/core/hle/service/apt/apt.cpp286
-rw-r--r--src/core/hle/service/cfg/cfg.cpp2
-rw-r--r--src/core/hle/service/cfg/cfg.h2
-rw-r--r--src/core/hle/service/ns/ns.cpp16
-rw-r--r--src/core/hle/service/ns/ns.h16
-rw-r--r--src/core/hle/service/ns/ns_s.cpp34
-rw-r--r--src/core/hle/service/ns/ns_s.h (renamed from src/core/hle/service/ns_s.h)9
-rw-r--r--src/core/hle/service/ns_s.cpp33
-rw-r--r--src/core/hle/service/service.cpp5
-rw-r--r--src/core/loader/3dsx.cpp1
-rw-r--r--src/core/loader/elf.cpp1
-rw-r--r--src/core/loader/ncch.cpp1
-rw-r--r--src/core/memory.cpp153
-rw-r--r--src/core/memory.h62
-rw-r--r--src/core/memory_setup.h10
-rw-r--r--src/core/settings.h1
-rw-r--r--src/core/telemetry_session.cpp12
-rw-r--r--src/core/telemetry_session.h10
-rw-r--r--src/tests/core/arm/arm_test_common.cpp18
-rw-r--r--src/video_core/regs_rasterizer.h14
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp53
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h9
-rw-r--r--src/video_core/renderer_opengl/gl_shader_gen.cpp80
-rw-r--r--src/video_core/shader/shader.cpp3
-rw-r--r--src/video_core/swrasterizer/clipper.cpp31
-rw-r--r--src/web_service/CMakeLists.txt2
-rw-r--r--src/web_service/verify_login.cpp28
-rw-r--r--src/web_service/verify_login.h24
-rw-r--r--src/web_service/web_backend.cpp101
-rw-r--r--src/web_service/web_backend.h16
54 files changed, 932 insertions, 421 deletions
diff --git a/.travis-build-docker.sh b/.travis-build-docker.sh
index ca6fae42b..01249ace0 100644
--- a/.travis-build-docker.sh
+++ b/.travis-build-docker.sh
@@ -14,7 +14,7 @@ echo y | sh cmake-3.9.0-Linux-x86_64.sh --prefix=cmake
export PATH=/citra/cmake/cmake-3.9.0-Linux-x86_64/bin:$PATH
mkdir build && cd build
-cmake .. -DCMAKE_BUILD_TYPE=Release
+cmake .. -DUSE_SYSTEM_CURL=ON -DCMAKE_BUILD_TYPE=Release
make -j4
ctest -VV -C Release
diff --git a/.travis-build.sh b/.travis-build.sh
index 64f5aed94..bb4e6fc47 100755
--- a/.travis-build.sh
+++ b/.travis-build.sh
@@ -52,7 +52,7 @@ elif [ "$TRAVIS_OS_NAME" = "osx" ]; then
export Qt5_DIR=$(brew --prefix)/opt/qt5
mkdir build && cd build
- cmake .. -GXcode
+ cmake .. -DUSE_SYSTEM_CURL=ON -GXcode
xcodebuild -configuration Release
ctest -VV -C Release
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f8060270e..d9c2f78a2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,6 +2,7 @@
cmake_minimum_required(VERSION 3.6)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/externals/cmake-modules")
+include(DownloadExternals)
project(citra)
@@ -12,6 +13,15 @@ option(ENABLE_QT "Enable the Qt frontend" ON)
option(CITRA_USE_BUNDLED_QT "Download bundled Qt binaries" OFF)
option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON)
+option(CITRA_USE_BUNDLED_CURL "FOR MINGW ONLY: Download curl configured against winssl instead of openssl" OFF)
+if (ENABLE_WEB_SERVICE AND CITRA_USE_BUNDLED_CURL AND WINDOWS AND MSVC)
+ message("Turning off use bundled curl as msvc can compile curl on cpr")
+ SET(CITRA_USE_BUNDLED_CURL OFF CACHE BOOL "" FORCE)
+endif()
+if (ENABLE_WEB_SERVICE AND NOT CITRA_USE_BUNDLED_CURL AND MINGW)
+ message(AUTHOR_WARNING "Turning on CITRA_USE_BUNDLED_CURL. Override it only if you know what you are doing.")
+ SET(CITRA_USE_BUNDLED_CURL ON CACHE BOOL "" FORCE)
+endif()
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
message(STATUS "Copying pre-commit hook")
@@ -151,24 +161,6 @@ set_property(DIRECTORY APPEND PROPERTY
# System imported libraries
# ======================
-# This function downloads a binary library package from our external repo.
-# Params:
-# remote_path: path to the file to download, relative to the remote repository root
-# prefix_var: name of a variable which will be set with the path to the extracted contents
-function(download_bundled_external remote_path lib_name prefix_var)
- set(prefix "${CMAKE_BINARY_DIR}/externals/${lib_name}")
- if (NOT EXISTS "${prefix}")
- message(STATUS "Downloading binaries for ${lib_name}...")
- file(DOWNLOAD
- https://github.com/citra-emu/ext-windows-bin/raw/master/${remote_path}${lib_name}.7z
- "${CMAKE_BINARY_DIR}/externals/${lib_name}.7z" SHOW_PROGRESS)
- execute_process(COMMAND ${CMAKE_COMMAND} -E tar xf "${CMAKE_BINARY_DIR}/externals/${lib_name}.7z"
- WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/externals")
- endif()
- message(STATUS "Using bundled binaries at ${prefix}")
- set(${prefix_var} "${prefix}" PARENT_SCOPE)
-endfunction()
-
find_package(PNG QUIET)
if (NOT PNG_FOUND)
message(STATUS "libpng not found. Some debugging features have been disabled.")
diff --git a/CMakeModules/DownloadExternals.cmake b/CMakeModules/DownloadExternals.cmake
new file mode 100644
index 000000000..138a15d5a
--- /dev/null
+++ b/CMakeModules/DownloadExternals.cmake
@@ -0,0 +1,18 @@
+
+# This function downloads a binary library package from our external repo.
+# Params:
+# remote_path: path to the file to download, relative to the remote repository root
+# prefix_var: name of a variable which will be set with the path to the extracted contents
+function(download_bundled_external remote_path lib_name prefix_var)
+set(prefix "${CMAKE_BINARY_DIR}/externals/${lib_name}")
+if (NOT EXISTS "${prefix}")
+ message(STATUS "Downloading binaries for ${lib_name}...")
+ file(DOWNLOAD
+ https://github.com/citra-emu/ext-windows-bin/raw/master/${remote_path}${lib_name}.7z
+ "${CMAKE_BINARY_DIR}/externals/${lib_name}.7z" SHOW_PROGRESS)
+ execute_process(COMMAND ${CMAKE_COMMAND} -E tar xf "${CMAKE_BINARY_DIR}/externals/${lib_name}.7z"
+ WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/externals")
+endif()
+message(STATUS "Using bundled binaries at ${prefix}")
+set(${prefix_var} "${prefix}" PARENT_SCOPE)
+endfunction() \ No newline at end of file
diff --git a/appveyor.yml b/appveyor.yml
index ec9ca3747..5524eb576 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -44,7 +44,7 @@ before_build:
# redirect stderr and change the exit code to prevent powershell from cancelling the build if cmake prints a warning
cmd /C 'cmake -G "Visual Studio 15 2017 Win64" -DCITRA_USE_BUNDLED_QT=1 -DCITRA_USE_BUNDLED_SDL2=1 -DCMAKE_USE_OPENSSL=0 .. 2>&1 && exit 0'
} else {
- C:\msys64\usr\bin\bash.exe -lc "cmake -G 'MSYS Makefiles' -DUSE_SYSTEM_CURL=1 -DCMAKE_BUILD_TYPE=Release .. 2>&1"
+ C:\msys64\usr\bin\bash.exe -lc "cmake -G 'MSYS Makefiles' -DUSE_SYSTEM_CURL=1 -DCITRA_USE_BUNDLED_CURL=1 -DCMAKE_BUILD_TYPE=Release .. 2>&1"
}
- cd ..
@@ -112,9 +112,10 @@ after_build:
# copy the compiled binaries and other release files to the release folder
Get-ChildItem "$CMAKE_BINARY_DIR" -Recurse -Filter "citra*.exe" | Copy-Item -destination $RELEASE_DIST
+ # copy the libcurl dll
+ Get-ChildItem "$CMAKE_BINARY_DIR" -Recurse -Filter "libcurl.dll" | Copy-Item -destination $RELEASE_DIST
Copy-Item -path "$CMAKE_SOURCE_DIR/license.txt" -destination $RELEASE_DIST
Copy-Item -path "$CMAKE_SOURCE_DIR/README.md" -destination $RELEASE_DIST
-
# copy all the dll dependencies to the release folder
# hardcoded list because we don't build static and determining the list of dlls from the binary is a pain.
$MingwDLLs = "Qt5Core.dll","Qt5Widgets.dll","Qt5Gui.dll","Qt5OpenGL.dll",
@@ -123,11 +124,7 @@ after_build:
"libfreetype-*.dll","libglib-*.dll","libgobject-*.dll","libgraphite2.dll","libiconv-*.dll",
"libharfbuzz-*.dll","libintl-*.dll","libpcre-*.dll","libpcre16-*.dll","libpng16-*.dll",
# Runtime/Other dependencies
- "libgcc_s_seh-*.dll","libstdc++-*.dll","libwinpthread-*.dll","SDL2.dll","zlib1.dll",
- # curl dependencies
- "libcurl-*.dll","libnghttp2-*.dll","libeay32.dll","libgmp-*.dll","librtmp-*.dll",
- "libgnutls-*.dll","libhogweed-*.dll","libnettle-*.dll","libssh2-*.dll",
- "ssleay32.dll","libidn-*.dll","libp11-kit-*.dll","libtasn1-*.dll","libunistring-*.dll"
+ "libgcc_s_seh-*.dll","libstdc++-*.dll","libwinpthread-*.dll","SDL2.dll","zlib1.dll"
foreach ($file in $MingwDLLs) {
Copy-Item -path "C:/msys64/mingw64/bin/$file" -force -destination "$RELEASE_DIST"
}
diff --git a/dist/icons/checked.png b/dist/icons/checked.png
new file mode 100644
index 000000000..c277e6b40
--- /dev/null
+++ b/dist/icons/checked.png
Binary files differ
diff --git a/dist/icons/failed.png b/dist/icons/failed.png
new file mode 100644
index 000000000..ac10f174a
--- /dev/null
+++ b/dist/icons/failed.png
Binary files differ
diff --git a/dist/icons/icons.qrc b/dist/icons/icons.qrc
new file mode 100644
index 000000000..f0c44862f
--- /dev/null
+++ b/dist/icons/icons.qrc
@@ -0,0 +1,6 @@
+<RCC>
+ <qresource prefix="icons">
+ <file>checked.png</file>
+ <file>failed.png</file>
+ </qresource>
+</RCC>
diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt
index 8e4bcf21f..4a4ba1101 100644
--- a/externals/CMakeLists.txt
+++ b/externals/CMakeLists.txt
@@ -1,5 +1,8 @@
# Definitions for all external bundled libraries
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/CMakeModules)
+include(DownloadExternals)
+
# Catch
add_library(catch-single-include INTERFACE)
target_include_directories(catch-single-include INTERFACE catch/single_include)
@@ -54,9 +57,21 @@ add_subdirectory(enet)
target_include_directories(enet INTERFACE ./enet/include)
if (ENABLE_WEB_SERVICE)
+ # msys installed curl is configured to use openssl, but that isn't portable
+ # since it relies on having the bundled certs install in the home folder for SSL
+ # by default on mingw, download the precompiled curl thats linked against windows native ssl
+ if (MINGW AND CITRA_USE_BUNDLED_CURL)
+ download_bundled_external("curl/" "curl-7_55_1" CURL_PREFIX)
+ set(CURL_PREFIX "${CMAKE_BINARY_DIR}/externals/curl-7_55_1")
+ set(CURL_FOUND YES)
+ set(CURL_INCLUDE_DIR "${CURL_PREFIX}/include" CACHE PATH "Path to curl headers")
+ set(CURL_LIBRARY "${CURL_PREFIX}/lib/libcurldll.a" CACHE PATH "Path to curl library")
+ set(CURL_DLL_DIR "${CURL_PREFIX}/lib/" CACHE PATH "Path to curl.dll")
+ set(USE_SYSTEM_CURL ON CACHE BOOL "")
+ endif()
# CPR
- option(BUILD_TESTING OFF)
- option(BUILD_CPR_TESTS OFF)
+ set(BUILD_TESTING OFF CACHE BOOL "")
+ set(BUILD_CPR_TESTS OFF CACHE BOOL "")
add_subdirectory(cpr)
target_include_directories(cpr INTERFACE ./cpr/include)
diff --git a/src/citra/config.cpp b/src/citra/config.cpp
index a48ef08c7..45c28ad09 100644
--- a/src/citra/config.cpp
+++ b/src/citra/config.cpp
@@ -162,6 +162,8 @@ void Config::ReadValues() {
sdl2_config->GetBoolean("WebService", "enable_telemetry", true);
Settings::values.telemetry_endpoint_url = sdl2_config->Get(
"WebService", "telemetry_endpoint_url", "https://services.citra-emu.org/api/telemetry");
+ Settings::values.verify_endpoint_url = sdl2_config->Get(
+ "WebService", "verify_endpoint_url", "https://services.citra-emu.org/api/profile");
Settings::values.citra_username = sdl2_config->Get("WebService", "citra_username", "");
Settings::values.citra_token = sdl2_config->Get("WebService", "citra_token", "");
}
diff --git a/src/citra/default_ini.h b/src/citra/default_ini.h
index 4b13a2e1b..59faf773f 100644
--- a/src/citra/default_ini.h
+++ b/src/citra/default_ini.h
@@ -185,6 +185,8 @@ gdbstub_port=24689
enable_telemetry =
# Endpoint URL for submitting telemetry data
telemetry_endpoint_url = https://services.citra-emu.org/api/telemetry
+# Endpoint URL to verify the username and token
+verify_endpoint_url = https://services.citra-emu.org/api/profile
# Username and token for Citra Web Service
# See https://services.citra-emu.org/ for more info
citra_username =
diff --git a/src/citra_qt/CMakeLists.txt b/src/citra_qt/CMakeLists.txt
index e0a19fd9e..add7566c2 100644
--- a/src/citra_qt/CMakeLists.txt
+++ b/src/citra_qt/CMakeLists.txt
@@ -79,6 +79,7 @@ set(UIS
main.ui
)
+file(GLOB_RECURSE ICONS ${CMAKE_SOURCE_DIR}/dist/icons/*)
file(GLOB_RECURSE THEMES ${CMAKE_SOURCE_DIR}/dist/qt_themes/*)
create_directory_groups(${SRCS} ${HEADERS} ${UIS})
@@ -92,10 +93,10 @@ endif()
if (APPLE)
set(MACOSX_ICON "../../dist/citra.icns")
set_source_files_properties(${MACOSX_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
- add_executable(citra-qt MACOSX_BUNDLE ${SRCS} ${HEADERS} ${UI_HDRS} ${THEMES} ${MACOSX_ICON})
+ add_executable(citra-qt MACOSX_BUNDLE ${SRCS} ${HEADERS} ${UI_HDRS} ${ICONS} ${THEMES} ${MACOSX_ICON})
set_target_properties(citra-qt PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist)
else()
- add_executable(citra-qt ${SRCS} ${HEADERS} ${UI_HDRS} ${THEMES})
+ add_executable(citra-qt ${SRCS} ${HEADERS} ${UI_HDRS} ${ICONS} ${THEMES})
endif()
target_link_libraries(citra-qt PRIVATE audio_core common core input_common network video_core)
target_link_libraries(citra-qt PRIVATE Boost::boost glad nihstro-headers Qt5::OpenGL Qt5::Widgets)
diff --git a/src/citra_qt/configuration/config.cpp b/src/citra_qt/configuration/config.cpp
index ef114aad3..5261f4c4c 100644
--- a/src/citra_qt/configuration/config.cpp
+++ b/src/citra_qt/configuration/config.cpp
@@ -146,6 +146,10 @@ void Config::ReadValues() {
qt_config->value("telemetry_endpoint_url", "https://services.citra-emu.org/api/telemetry")
.toString()
.toStdString();
+ Settings::values.verify_endpoint_url =
+ qt_config->value("verify_endpoint_url", "https://services.citra-emu.org/api/profile")
+ .toString()
+ .toStdString();
Settings::values.citra_username = qt_config->value("citra_username").toString().toStdString();
Settings::values.citra_token = qt_config->value("citra_token").toString().toStdString();
qt_config->endGroup();
@@ -293,6 +297,8 @@ void Config::SaveValues() {
qt_config->setValue("enable_telemetry", Settings::values.enable_telemetry);
qt_config->setValue("telemetry_endpoint_url",
QString::fromStdString(Settings::values.telemetry_endpoint_url));
+ qt_config->setValue("verify_endpoint_url",
+ QString::fromStdString(Settings::values.verify_endpoint_url));
qt_config->setValue("citra_username", QString::fromStdString(Settings::values.citra_username));
qt_config->setValue("citra_token", QString::fromStdString(Settings::values.citra_token));
qt_config->endGroup();
diff --git a/src/citra_qt/configuration/configure_web.cpp b/src/citra_qt/configuration/configure_web.cpp
index 8715fb018..38ce19c0f 100644
--- a/src/citra_qt/configuration/configure_web.cpp
+++ b/src/citra_qt/configuration/configure_web.cpp
@@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <QMessageBox>
#include "citra_qt/configuration/configure_web.h"
#include "core/settings.h"
#include "core/telemetry_session.h"
@@ -11,7 +12,9 @@ ConfigureWeb::ConfigureWeb(QWidget* parent)
: QWidget(parent), ui(std::make_unique<Ui::ConfigureWeb>()) {
ui->setupUi(this);
connect(ui->button_regenerate_telemetry_id, &QPushButton::clicked, this,
- &ConfigureWeb::refreshTelemetryID);
+ &ConfigureWeb::RefreshTelemetryID);
+ connect(ui->button_verify_login, &QPushButton::clicked, this, &ConfigureWeb::VerifyLogin);
+ connect(this, &ConfigureWeb::LoginVerified, this, &ConfigureWeb::OnLoginVerified);
this->setConfiguration();
}
@@ -34,19 +37,66 @@ void ConfigureWeb::setConfiguration() {
ui->toggle_telemetry->setChecked(Settings::values.enable_telemetry);
ui->edit_username->setText(QString::fromStdString(Settings::values.citra_username));
ui->edit_token->setText(QString::fromStdString(Settings::values.citra_token));
+ // Connect after setting the values, to avoid calling OnLoginChanged now
+ connect(ui->edit_token, &QLineEdit::textChanged, this, &ConfigureWeb::OnLoginChanged);
+ connect(ui->edit_username, &QLineEdit::textChanged, this, &ConfigureWeb::OnLoginChanged);
ui->label_telemetry_id->setText("Telemetry ID: 0x" +
QString::number(Core::GetTelemetryId(), 16).toUpper());
+ user_verified = true;
}
void ConfigureWeb::applyConfiguration() {
Settings::values.enable_telemetry = ui->toggle_telemetry->isChecked();
- Settings::values.citra_username = ui->edit_username->text().toStdString();
- Settings::values.citra_token = ui->edit_token->text().toStdString();
+ if (user_verified) {
+ Settings::values.citra_username = ui->edit_username->text().toStdString();
+ Settings::values.citra_token = ui->edit_token->text().toStdString();
+ } else {
+ QMessageBox::warning(this, tr("Username and token not verfied"),
+ tr("Username and token were not verified. The changes to your "
+ "username and/or token have not been saved."));
+ }
Settings::Apply();
}
-void ConfigureWeb::refreshTelemetryID() {
+void ConfigureWeb::RefreshTelemetryID() {
const u64 new_telemetry_id{Core::RegenerateTelemetryId()};
ui->label_telemetry_id->setText("Telemetry ID: 0x" +
QString::number(new_telemetry_id, 16).toUpper());
}
+
+void ConfigureWeb::OnLoginChanged() {
+ if (ui->edit_username->text().isEmpty() && ui->edit_token->text().isEmpty()) {
+ user_verified = true;
+ ui->label_username_verified->setPixmap(QPixmap(":/icons/checked.png"));
+ ui->label_token_verified->setPixmap(QPixmap(":/icons/checked.png"));
+ } else {
+ user_verified = false;
+ ui->label_username_verified->setPixmap(QPixmap(":/icons/failed.png"));
+ ui->label_token_verified->setPixmap(QPixmap(":/icons/failed.png"));
+ }
+}
+
+void ConfigureWeb::VerifyLogin() {
+ verified =
+ Core::VerifyLogin(ui->edit_username->text().toStdString(),
+ ui->edit_token->text().toStdString(), [&]() { emit LoginVerified(); });
+ ui->button_verify_login->setDisabled(true);
+ ui->button_verify_login->setText(tr("Verifying"));
+}
+
+void ConfigureWeb::OnLoginVerified() {
+ ui->button_verify_login->setEnabled(true);
+ ui->button_verify_login->setText(tr("Verify"));
+ if (verified.get()) {
+ user_verified = true;
+ ui->label_username_verified->setPixmap(QPixmap(":/icons/checked.png"));
+ ui->label_token_verified->setPixmap(QPixmap(":/icons/checked.png"));
+ } else {
+ ui->label_username_verified->setPixmap(QPixmap(":/icons/failed.png"));
+ ui->label_token_verified->setPixmap(QPixmap(":/icons/failed.png"));
+ QMessageBox::critical(
+ this, tr("Verification failed"),
+ tr("Verification failed. Check that you have entered your username and token "
+ "correctly, and that your internet connection is working."));
+ }
+}
diff --git a/src/citra_qt/configuration/configure_web.h b/src/citra_qt/configuration/configure_web.h
index 20bc254b9..ad2d58f6e 100644
--- a/src/citra_qt/configuration/configure_web.h
+++ b/src/citra_qt/configuration/configure_web.h
@@ -4,6 +4,7 @@
#pragma once
+#include <future>
#include <memory>
#include <QWidget>
@@ -21,10 +22,19 @@ public:
void applyConfiguration();
public slots:
- void refreshTelemetryID();
+ void RefreshTelemetryID();
+ void OnLoginChanged();
+ void VerifyLogin();
+ void OnLoginVerified();
+
+signals:
+ void LoginVerified();
private:
void setConfiguration();
+ bool user_verified = true;
+ std::future<bool> verified;
+
std::unique_ptr<Ui::ConfigureWeb> ui;
};
diff --git a/src/citra_qt/configuration/configure_web.ui b/src/citra_qt/configuration/configure_web.ui
index d8d283fad..dd996ab62 100644
--- a/src/citra_qt/configuration/configure_web.ui
+++ b/src/citra_qt/configuration/configure_web.ui
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
- <width>400</width>
- <height>300</height>
+ <width>926</width>
+ <height>561</height>
</rect>
</property>
<property name="windowTitle">
@@ -31,14 +31,30 @@
</item>
<item>
<layout class="QGridLayout" name="gridLayoutCitraUsername">
- <item row="0" column="0">
- <widget class="QLabel" name="label_username">
+ <item row="2" column="3">
+ <widget class="QPushButton" name="button_verify_login">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="layoutDirection">
+ <enum>Qt::RightToLeft</enum>
+ </property>
<property name="text">
- <string>Username: </string>
+ <string>Verify</string>
</property>
</widget>
</item>
- <item row="0" column="1">
+ <item row="2" column="0">
+ <widget class="QLabel" name="web_signup_link">
+ <property name="text">
+ <string>Sign up</string>
+ </property>
+ </widget>
+ </item>
+ <item row="0" column="1" colspan="3">
<widget class="QLineEdit" name="edit_username">
<property name="maxLength">
<number>36</number>
@@ -52,7 +68,22 @@
</property>
</widget>
</item>
- <item row="1" column="1">
+ <item row="1" column="4">
+ <widget class="QLabel" name="label_token_verified">
+ </widget>
+ </item>
+ <item row="0" column="0">
+ <widget class="QLabel" name="label_username">
+ <property name="text">
+ <string>Username: </string>
+ </property>
+ </widget>
+ </item>
+ <item row="0" column="4">
+ <widget class="QLabel" name="label_username_verified">
+ </widget>
+ </item>
+ <item row="1" column="1" colspan="3">
<widget class="QLineEdit" name="edit_token">
<property name="maxLength">
<number>36</number>
@@ -62,13 +93,6 @@
</property>
</widget>
</item>
- <item row="2" column="0">
- <widget class="QLabel" name="web_signup_link">
- <property name="text">
- <string>Sign up</string>
- </property>
- </widget>
- </item>
<item row="2" column="1">
<widget class="QLabel" name="web_token_info_link">
<property name="text">
@@ -76,6 +100,19 @@
</property>
</widget>
</item>
+ <item row="2" column="2">
+ <spacer name="horizontalSpacer">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>40</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
</layout>
</item>
</layout>
@@ -105,17 +142,17 @@
<layout class="QGridLayout" name="gridLayoutTelemetryId">
<item row="0" column="0">
<widget class="QLabel" name="label_telemetry_id">
- <property name="text">
- <string>Telemetry ID:</string>
- </property>
+ <property name="text">
+ <string>Telemetry ID:</string>
+ </property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="button_regenerate_telemetry_id">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
- <horstretch>0</horstretch>
- <verstretch>0</verstretch>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 78dec8600..cd1a8de2d 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -135,7 +135,8 @@ set(SRCS
hle/service/nim/nim_aoc.cpp
hle/service/nim/nim_s.cpp
hle/service/nim/nim_u.cpp
- hle/service/ns_s.cpp
+ hle/service/ns/ns.cpp
+ hle/service/ns/ns_s.cpp
hle/service/nwm/nwm.cpp
hle/service/nwm/nwm_cec.cpp
hle/service/nwm/nwm_ext.cpp
@@ -335,7 +336,8 @@ set(HEADERS
hle/service/nim/nim_aoc.h
hle/service/nim/nim_s.h
hle/service/nim/nim_u.h
- hle/service/ns_s.h
+ hle/service/ns/ns.h
+ hle/service/ns/ns_s.h
hle/service/nwm/nwm.h
hle/service/nwm/nwm_cec.h
hle/service/nwm/nwm_ext.h
diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp
index 0a0b91590..34c5aa381 100644
--- a/src/core/arm/dynarmic/arm_dynarmic.cpp
+++ b/src/core/arm/dynarmic/arm_dynarmic.cpp
@@ -56,7 +56,9 @@ static Dynarmic::UserCallbacks GetUserCallbacks(
user_callbacks.memory.Write16 = &Memory::Write16;
user_callbacks.memory.Write32 = &Memory::Write32;
user_callbacks.memory.Write64 = &Memory::Write64;
- user_callbacks.page_table = Memory::GetCurrentPageTablePointers();
+ // TODO(Subv): Re-add the page table pointers once dynarmic supports switching page tables at
+ // runtime.
+ user_callbacks.page_table = nullptr;
user_callbacks.coprocessors[15] = std::make_shared<DynarmicCP15>(interpeter_state);
return user_callbacks;
}
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 5332318cf..59b8768e7 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -137,7 +137,6 @@ void System::Reschedule() {
}
System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
- Memory::InitMemoryMap();
LOG_DEBUG(HW_Memory, "initialized OK");
if (Settings::values.use_cpu_jit) {
diff --git a/src/core/hle/kernel/memory.cpp b/src/core/hle/kernel/memory.cpp
index 496d07cb5..7f27e9655 100644
--- a/src/core/hle/kernel/memory.cpp
+++ b/src/core/hle/kernel/memory.cpp
@@ -8,7 +8,6 @@
#include <memory>
#include <utility>
#include <vector>
-#include "audio_core/audio_core.h"
#include "common/assert.h"
#include "common/common_types.h"
#include "common/logging/log.h"
@@ -24,7 +23,7 @@
namespace Kernel {
-static MemoryRegionInfo memory_regions[3];
+MemoryRegionInfo memory_regions[3];
/// Size of the APPLICATION, SYSTEM and BASE memory regions (respectively) for each system
/// memory configuration type.
@@ -96,9 +95,6 @@ MemoryRegionInfo* GetMemoryRegion(MemoryRegion region) {
}
}
-std::array<u8, Memory::VRAM_SIZE> vram;
-std::array<u8, Memory::N3DS_EXTRA_RAM_SIZE> n3ds_extra_ram;
-
void HandleSpecialMapping(VMManager& address_space, const AddressMapping& mapping) {
using namespace Memory;
@@ -143,30 +139,14 @@ void HandleSpecialMapping(VMManager& address_space, const AddressMapping& mappin
return;
}
- // TODO(yuriks): Use GetPhysicalPointer when that becomes independent of the virtual
- // mappings.
- u8* target_pointer = nullptr;
- switch (area->paddr_base) {
- case VRAM_PADDR:
- target_pointer = vram.data();
- break;
- case DSP_RAM_PADDR:
- target_pointer = AudioCore::GetDspMemory().data();
- break;
- case N3DS_EXTRA_RAM_PADDR:
- target_pointer = n3ds_extra_ram.data();
- break;
- default:
- UNREACHABLE();
- }
+ u8* target_pointer = Memory::GetPhysicalPointer(area->paddr_base + offset_into_region);
// TODO(yuriks): This flag seems to have some other effect, but it's unknown what
MemoryState memory_state = mapping.unk_flag ? MemoryState::Static : MemoryState::IO;
- auto vma = address_space
- .MapBackingMemory(mapping.address, target_pointer + offset_into_region,
- mapping.size, memory_state)
- .Unwrap();
+ auto vma =
+ address_space.MapBackingMemory(mapping.address, target_pointer, mapping.size, memory_state)
+ .Unwrap();
address_space.Reprotect(vma,
mapping.read_only ? VMAPermission::Read : VMAPermission::ReadWrite);
}
diff --git a/src/core/hle/kernel/memory.h b/src/core/hle/kernel/memory.h
index 08c1a9989..da6bb3563 100644
--- a/src/core/hle/kernel/memory.h
+++ b/src/core/hle/kernel/memory.h
@@ -26,4 +26,6 @@ MemoryRegionInfo* GetMemoryRegion(MemoryRegion region);
void HandleSpecialMapping(VMManager& address_space, const AddressMapping& mapping);
void MapSharedPages(VMManager& address_space);
+
+extern MemoryRegionInfo memory_regions[3];
} // namespace Kernel
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index b957c45dd..324415a36 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -171,6 +171,8 @@ static void SwitchContext(Thread* new_thread) {
// Cancel any outstanding wakeup events for this thread
CoreTiming::UnscheduleEvent(ThreadWakeupEventType, new_thread->callback_handle);
+ auto previous_process = Kernel::g_current_process;
+
current_thread = new_thread;
ready_queue.remove(new_thread->current_priority, new_thread);
@@ -178,8 +180,18 @@ static void SwitchContext(Thread* new_thread) {
Core::CPU().LoadContext(new_thread->context);
Core::CPU().SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress());
+
+ if (previous_process != current_thread->owner_process) {
+ Kernel::g_current_process = current_thread->owner_process;
+ Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table;
+ // We have switched processes and thus, page tables, clear the instruction cache so we
+ // don't keep stale data from the previous process.
+ Core::CPU().ClearInstructionCache();
+ }
} else {
current_thread = nullptr;
+ // Note: We do not reset the current process and current page table when idling because
+ // technically we haven't changed processes, our threads are just paused.
}
}
diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp
index cef1f7fa8..7a007c065 100644
--- a/src/core/hle/kernel/vm_manager.cpp
+++ b/src/core/hle/kernel/vm_manager.cpp
@@ -56,6 +56,10 @@ void VMManager::Reset() {
initial_vma.size = MAX_ADDRESS;
vma_map.emplace(initial_vma.base, initial_vma);
+ page_table.pointers.fill(nullptr);
+ page_table.attributes.fill(Memory::PageType::Unmapped);
+ page_table.cached_res_count.fill(0);
+
UpdatePageTableForVMA(initial_vma);
}
@@ -328,16 +332,17 @@ VMManager::VMAIter VMManager::MergeAdjacent(VMAIter iter) {
void VMManager::UpdatePageTableForVMA(const VirtualMemoryArea& vma) {
switch (vma.type) {
case VMAType::Free:
- Memory::UnmapRegion(vma.base, vma.size);
+ Memory::UnmapRegion(page_table, vma.base, vma.size);
break;
case VMAType::AllocatedMemoryBlock:
- Memory::MapMemoryRegion(vma.base, vma.size, vma.backing_block->data() + vma.offset);
+ Memory::MapMemoryRegion(page_table, vma.base, vma.size,
+ vma.backing_block->data() + vma.offset);
break;
case VMAType::BackingMemory:
- Memory::MapMemoryRegion(vma.base, vma.size, vma.backing_memory);
+ Memory::MapMemoryRegion(page_table, vma.base, vma.size, vma.backing_memory);
break;
case VMAType::MMIO:
- Memory::MapIoRegion(vma.base, vma.size, vma.mmio_handler);
+ Memory::MapIoRegion(page_table, vma.base, vma.size, vma.mmio_handler);
break;
}
}
diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h
index 38e0d74d0..1302527bb 100644
--- a/src/core/hle/kernel/vm_manager.h
+++ b/src/core/hle/kernel/vm_manager.h
@@ -9,6 +9,7 @@
#include <vector>
#include "common/common_types.h"
#include "core/hle/result.h"
+#include "core/memory.h"
#include "core/mmio.h"
namespace Kernel {
@@ -102,7 +103,6 @@ struct VirtualMemoryArea {
* - http://duartes.org/gustavo/blog/post/page-cache-the-affair-between-memory-and-files/
*/
class VMManager final {
- // TODO(yuriks): Make page tables switchable to support multiple VMManagers
public:
/**
* The maximum amount of address space managed by the kernel. Addresses above this are never
@@ -184,6 +184,10 @@ public:
/// Dumps the address space layout to the log, for debugging
void LogLayout(Log::Level log_level) const;
+ /// Each VMManager has its own page table, which is set as the main one when the owning process
+ /// is scheduled.
+ Memory::PageTable page_table;
+
private:
using VMAIter = decltype(vma_map)::iterator;
diff --git a/src/core/hle/service/apt/apt.cpp b/src/core/hle/service/apt/apt.cpp
index 58d94768c..8c0ba73f2 100644
--- a/src/core/hle/service/apt/apt.cpp
+++ b/src/core/hle/service/apt/apt.cpp
@@ -19,6 +19,7 @@
#include "core/hle/service/apt/apt_s.h"
#include "core/hle/service/apt/apt_u.h"
#include "core/hle/service/apt/bcfnt/bcfnt.h"
+#include "core/hle/service/cfg/cfg.h"
#include "core/hle/service/fs/archive.h"
#include "core/hle/service/ptm/ptm.h"
#include "core/hle/service/service.h"
@@ -198,6 +199,143 @@ void Initialize(Service::Interface* self) {
Kernel::g_handle_table.Create(slot_data->parameter_event).Unwrap());
}
+static u32 DecompressLZ11(const u8* in, u8* out) {
+ u32_le decompressed_size;
+ memcpy(&decompressed_size, in, sizeof(u32));
+ in += 4;
+
+ u8 type = decompressed_size & 0xFF;
+ ASSERT(type == 0x11);
+ decompressed_size >>= 8;
+
+ u32 current_out_size = 0;
+ u8 flags = 0, mask = 1;
+ while (current_out_size < decompressed_size) {
+ if (mask == 1) {
+ flags = *(in++);
+ mask = 0x80;
+ } else {
+ mask >>= 1;
+ }
+
+ if (flags & mask) {
+ u8 byte1 = *(in++);
+ u32 length = byte1 >> 4;
+ u32 offset;
+ if (length == 0) {
+ u8 byte2 = *(in++);
+ u8 byte3 = *(in++);
+ length = (((byte1 & 0x0F) << 4) | (byte2 >> 4)) + 0x11;
+ offset = (((byte2 & 0x0F) << 8) | byte3) + 0x1;
+ } else if (length == 1) {
+ u8 byte2 = *(in++);
+ u8 byte3 = *(in++);
+ u8 byte4 = *(in++);
+ length = (((byte1 & 0x0F) << 12) | (byte2 << 4) | (byte3 >> 4)) + 0x111;
+ offset = (((byte3 & 0x0F) << 8) | byte4) + 0x1;
+ } else {
+ u8 byte2 = *(in++);
+ length = (byte1 >> 4) + 0x1;
+ offset = (((byte1 & 0x0F) << 8) | byte2) + 0x1;
+ }
+
+ for (u32 i = 0; i < length; i++) {
+ *out = *(out - offset);
+ ++out;
+ }
+
+ current_out_size += length;
+ } else {
+ *(out++) = *(in++);
+ current_out_size++;
+ }
+ }
+ return decompressed_size;
+}
+
+static bool LoadSharedFont() {
+ u8 font_region_code;
+ switch (CFG::GetRegionValue()) {
+ case 4: // CHN
+ font_region_code = 2;
+ break;
+ case 5: // KOR
+ font_region_code = 3;
+ break;
+ case 6: // TWN
+ font_region_code = 4;
+ break;
+ default: // JPN/EUR/USA
+ font_region_code = 1;
+ break;
+ }
+
+ const u64_le shared_font_archive_id_low = 0x0004009b00014002 | ((font_region_code - 1) << 8);
+ const u64_le shared_font_archive_id_high = 0x00000001ffffff00;
+ std::vector<u8> shared_font_archive_id(16);
+ std::memcpy(&shared_font_archive_id[0], &shared_font_archive_id_low, sizeof(u64));
+ std::memcpy(&shared_font_archive_id[8], &shared_font_archive_id_high, sizeof(u64));
+ FileSys::Path archive_path(shared_font_archive_id);
+ auto archive_result = Service::FS::OpenArchive(Service::FS::ArchiveIdCode::NCCH, archive_path);
+ if (archive_result.Failed())
+ return false;
+
+ std::vector<u8> romfs_path(20, 0); // 20-byte all zero path for opening RomFS
+ FileSys::Path file_path(romfs_path);
+ FileSys::Mode open_mode = {};
+ open_mode.read_flag.Assign(1);
+ auto file_result = Service::FS::OpenFileFromArchive(*archive_result, file_path, open_mode);
+ if (file_result.Failed())
+ return false;
+
+ auto romfs = std::move(file_result).Unwrap();
+ std::vector<u8> romfs_buffer(romfs->backend->GetSize());
+ romfs->backend->Read(0, romfs_buffer.size(), romfs_buffer.data());
+ romfs->backend->Close();
+
+ const char16_t* file_name[4] = {u"cbf_std.bcfnt.lz", u"cbf_zh-Hans-CN.bcfnt.lz",
+ u"cbf_ko-Hang-KR.bcfnt.lz", u"cbf_zh-Hant-TW.bcfnt.lz"};
+ const u8* font_file =
+ RomFS::GetFilePointer(romfs_buffer.data(), {file_name[font_region_code - 1]});
+ if (font_file == nullptr)
+ return false;
+
+ struct {
+ u32_le status;
+ u32_le region;
+ u32_le decompressed_size;
+ INSERT_PADDING_WORDS(0x1D);
+ } shared_font_header{};
+ static_assert(sizeof(shared_font_header) == 0x80, "shared_font_header has incorrect size");
+
+ shared_font_header.status = 2; // successfully loaded
+ shared_font_header.region = font_region_code;
+ shared_font_header.decompressed_size =
+ DecompressLZ11(font_file, shared_font_mem->GetPointer(0x80));
+ std::memcpy(shared_font_mem->GetPointer(), &shared_font_header, sizeof(shared_font_header));
+ *shared_font_mem->GetPointer(0x83) = 'U'; // Change the magic from "CFNT" to "CFNU"
+
+ return true;
+}
+
+static bool LoadLegacySharedFont() {
+ // This is the legacy method to load shared font.
+ // The expected format is a decrypted, uncompressed BCFNT file with the 0x80 byte header
+ // generated by the APT:U service. The best way to get is by dumping it from RAM. We've provided
+ // a homebrew app to do this: https://github.com/citra-emu/3dsutils. Put the resulting file
+ // "shared_font.bin" in the Citra "sysdata" directory.
+ std::string filepath = FileUtil::GetUserPath(D_SYSDATA_IDX) + SHARED_FONT;
+
+ FileUtil::CreateFullPath(filepath); // Create path if not already created
+ FileUtil::IOFile file(filepath, "rb");
+ if (file.IsOpen()) {
+ file.ReadBytes(shared_font_mem->GetPointer(), file.GetSize());
+ return true;
+ }
+
+ return false;
+}
+
void GetSharedFont(Service::Interface* self) {
IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x44, 0, 0); // 0x00440000
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
@@ -206,11 +344,20 @@ void GetSharedFont(Service::Interface* self) {
Core::Telemetry().AddField(Telemetry::FieldType::Session, "RequiresSharedFont", true);
if (!shared_font_loaded) {
- LOG_ERROR(Service_APT, "shared font file missing - go dump it from your 3ds");
- rb.Push<u32>(-1); // TODO: Find the right error code
- rb.Skip(1 + 2, true);
- Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSharedFont);
- return;
+ // On real 3DS, font loading happens on booting. However, we load it on demand to coordinate
+ // with CFG region auto configuration, which happens later than APT initialization.
+ if (LoadSharedFont()) {
+ shared_font_loaded = true;
+ } else if (LoadLegacySharedFont()) {
+ LOG_WARNING(Service_APT, "Loaded shared font by legacy method");
+ shared_font_loaded = true;
+ } else {
+ LOG_ERROR(Service_APT, "shared font file missing - go dump it from your 3ds");
+ rb.Push<u32>(-1); // TODO: Find the right error code
+ rb.Skip(1 + 2, true);
+ Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSharedFont);
+ return;
+ }
}
// The shared font has to be relocated to the new address before being passed to the
@@ -863,125 +1010,6 @@ void CheckNew3DS(Service::Interface* self) {
LOG_WARNING(Service_APT, "(STUBBED) called");
}
-static u32 DecompressLZ11(const u8* in, u8* out) {
- u32_le decompressed_size;
- memcpy(&decompressed_size, in, sizeof(u32));
- in += 4;
-
- u8 type = decompressed_size & 0xFF;
- ASSERT(type == 0x11);
- decompressed_size >>= 8;
-
- u32 current_out_size = 0;
- u8 flags = 0, mask = 1;
- while (current_out_size < decompressed_size) {
- if (mask == 1) {
- flags = *(in++);
- mask = 0x80;
- } else {
- mask >>= 1;
- }
-
- if (flags & mask) {
- u8 byte1 = *(in++);
- u32 length = byte1 >> 4;
- u32 offset;
- if (length == 0) {
- u8 byte2 = *(in++);
- u8 byte3 = *(in++);
- length = (((byte1 & 0x0F) << 4) | (byte2 >> 4)) + 0x11;
- offset = (((byte2 & 0x0F) << 8) | byte3) + 0x1;
- } else if (length == 1) {
- u8 byte2 = *(in++);
- u8 byte3 = *(in++);
- u8 byte4 = *(in++);
- length = (((byte1 & 0x0F) << 12) | (byte2 << 4) | (byte3 >> 4)) + 0x111;
- offset = (((byte3 & 0x0F) << 8) | byte4) + 0x1;
- } else {
- u8 byte2 = *(in++);
- length = (byte1 >> 4) + 0x1;
- offset = (((byte1 & 0x0F) << 8) | byte2) + 0x1;
- }
-
- for (u32 i = 0; i < length; i++) {
- *out = *(out - offset);
- ++out;
- }
-
- current_out_size += length;
- } else {
- *(out++) = *(in++);
- current_out_size++;
- }
- }
- return decompressed_size;
-}
-
-static bool LoadSharedFont() {
- // TODO (wwylele): load different font archive for region CHN/KOR/TWN
- const u64_le shared_font_archive_id_low = 0x0004009b00014002;
- const u64_le shared_font_archive_id_high = 0x00000001ffffff00;
- std::vector<u8> shared_font_archive_id(16);
- std::memcpy(&shared_font_archive_id[0], &shared_font_archive_id_low, sizeof(u64));
- std::memcpy(&shared_font_archive_id[8], &shared_font_archive_id_high, sizeof(u64));
- FileSys::Path archive_path(shared_font_archive_id);
- auto archive_result = Service::FS::OpenArchive(Service::FS::ArchiveIdCode::NCCH, archive_path);
- if (archive_result.Failed())
- return false;
-
- std::vector<u8> romfs_path(20, 0); // 20-byte all zero path for opening RomFS
- FileSys::Path file_path(romfs_path);
- FileSys::Mode open_mode = {};
- open_mode.read_flag.Assign(1);
- auto file_result = Service::FS::OpenFileFromArchive(*archive_result, file_path, open_mode);
- if (file_result.Failed())
- return false;
-
- auto romfs = std::move(file_result).Unwrap();
- std::vector<u8> romfs_buffer(romfs->backend->GetSize());
- romfs->backend->Read(0, romfs_buffer.size(), romfs_buffer.data());
- romfs->backend->Close();
-
- const u8* font_file = RomFS::GetFilePointer(romfs_buffer.data(), {u"cbf_std.bcfnt.lz"});
- if (font_file == nullptr)
- return false;
-
- struct {
- u32_le status;
- u32_le region;
- u32_le decompressed_size;
- INSERT_PADDING_WORDS(0x1D);
- } shared_font_header{};
- static_assert(sizeof(shared_font_header) == 0x80, "shared_font_header has incorrect size");
-
- shared_font_header.status = 2; // successfully loaded
- shared_font_header.region = 1; // region JPN/EUR/USA
- shared_font_header.decompressed_size =
- DecompressLZ11(font_file, shared_font_mem->GetPointer(0x80));
- std::memcpy(shared_font_mem->GetPointer(), &shared_font_header, sizeof(shared_font_header));
- *shared_font_mem->GetPointer(0x83) = 'U'; // Change the magic from "CFNT" to "CFNU"
-
- return true;
-}
-
-static bool LoadLegacySharedFont() {
- // This is the legacy method to load shared font.
- // The expected format is a decrypted, uncompressed BCFNT file with the 0x80 byte header
- // generated by the APT:U service. The best way to get is by dumping it from RAM. We've provided
- // a homebrew app to do this: https://github.com/citra-emu/3dsutils. Put the resulting file
- // "shared_font.bin" in the Citra "sysdata" directory.
- std::string filepath = FileUtil::GetUserPath(D_SYSDATA_IDX) + SHARED_FONT;
-
- FileUtil::CreateFullPath(filepath); // Create path if not already created
- FileUtil::IOFile file(filepath, "rb");
- if (file.IsOpen()) {
- file.ReadBytes(shared_font_mem->GetPointer(), file.GetSize());
- return true;
- }
-
- return false;
-}
-
void Init() {
AddService(new APT_A_Interface);
AddService(new APT_S_Interface);
@@ -995,16 +1023,6 @@ void Init() {
MemoryPermission::ReadWrite, MemoryPermission::Read, 0,
Kernel::MemoryRegion::SYSTEM, "APT:SharedFont");
- if (LoadSharedFont()) {
- shared_font_loaded = true;
- } else if (LoadLegacySharedFont()) {
- LOG_WARNING(Service_APT, "Loaded shared font by legacy method");
- shared_font_loaded = true;
- } else {
- LOG_WARNING(Service_APT, "Unable to load shared font");
- shared_font_loaded = false;
- }
-
lock = Kernel::Mutex::Create(false, "APT_U:Lock");
cpu_percent = 0;
diff --git a/src/core/hle/service/cfg/cfg.cpp b/src/core/hle/service/cfg/cfg.cpp
index 3dbeb27cc..f26a1f65f 100644
--- a/src/core/hle/service/cfg/cfg.cpp
+++ b/src/core/hle/service/cfg/cfg.cpp
@@ -168,7 +168,7 @@ void GetCountryCodeID(Service::Interface* self) {
cmd_buff[2] = country_code_id;
}
-static u32 GetRegionValue() {
+u32 GetRegionValue() {
if (Settings::values.region_value == Settings::REGION_VALUE_AUTO_SELECT)
return preferred_region_code;
diff --git a/src/core/hle/service/cfg/cfg.h b/src/core/hle/service/cfg/cfg.h
index 1659ebf32..282b6936b 100644
--- a/src/core/hle/service/cfg/cfg.h
+++ b/src/core/hle/service/cfg/cfg.h
@@ -101,6 +101,8 @@ void GetCountryCodeString(Service::Interface* self);
*/
void GetCountryCodeID(Service::Interface* self);
+u32 GetRegionValue();
+
/**
* CFG::SecureInfoGetRegion service function
* Inputs:
diff --git a/src/core/hle/service/ns/ns.cpp b/src/core/hle/service/ns/ns.cpp
new file mode 100644
index 000000000..9e19c38bf
--- /dev/null
+++ b/src/core/hle/service/ns/ns.cpp
@@ -0,0 +1,16 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include "core/hle/service/ns/ns.h"
+#include "core/hle/service/ns/ns_s.h"
+
+namespace Service {
+namespace NS {
+
+void InstallInterfaces(SM::ServiceManager& service_manager) {
+ std::make_shared<NS_S>()->InstallAsService(service_manager);
+}
+
+} // namespace NS
+} // namespace Service
diff --git a/src/core/hle/service/ns/ns.h b/src/core/hle/service/ns/ns.h
new file mode 100644
index 000000000..c3d67d98c
--- /dev/null
+++ b/src/core/hle/service/ns/ns.h
@@ -0,0 +1,16 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include "core/hle/service/service.h"
+
+namespace Service {
+namespace NS {
+
+/// Registers all NS services with the specified service manager.
+void InstallInterfaces(SM::ServiceManager& service_manager);
+
+} // namespace NS
+} // namespace Service
diff --git a/src/core/hle/service/ns/ns_s.cpp b/src/core/hle/service/ns/ns_s.cpp
new file mode 100644
index 000000000..d952888dc
--- /dev/null
+++ b/src/core/hle/service/ns/ns_s.cpp
@@ -0,0 +1,34 @@
+// Copyright 2015 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include "core/hle/service/ns/ns_s.h"
+
+namespace Service {
+namespace NS {
+
+NS_S::NS_S() : ServiceFramework("ns:s", 2) {
+ static const FunctionInfo functions[] = {
+ {0x000100C0, nullptr, "LaunchFIRM"},
+ {0x000200C0, nullptr, "LaunchTitle"},
+ {0x00030000, nullptr, "TerminateApplication"},
+ {0x00040040, nullptr, "TerminateProcess"},
+ {0x000500C0, nullptr, "LaunchApplicationFIRM"},
+ {0x00060042, nullptr, "SetFIRMParams4A0"},
+ {0x00070042, nullptr, "CardUpdateInitialize"},
+ {0x00080000, nullptr, "CardUpdateShutdown"},
+ {0x000D0140, nullptr, "SetTWLBannerHMAC"},
+ {0x000E0000, nullptr, "ShutdownAsync"},
+ {0x00100180, nullptr, "RebootSystem"},
+ {0x00110100, nullptr, "TerminateTitle"},
+ {0x001200C0, nullptr, "SetApplicationCpuTimeLimit"},
+ {0x00150140, nullptr, "LaunchApplication"},
+ {0x00160000, nullptr, "RebootSystemClean"},
+ };
+ RegisterHandlers(functions);
+}
+
+NS_S::~NS_S() = default;
+
+} // namespace NS
+} // namespace Service
diff --git a/src/core/hle/service/ns_s.h b/src/core/hle/service/ns/ns_s.h
index 90288a521..660ae453f 100644
--- a/src/core/hle/service/ns_s.h
+++ b/src/core/hle/service/ns/ns_s.h
@@ -4,18 +4,17 @@
#pragma once
+#include "core/hle/kernel/kernel.h"
#include "core/hle/service/service.h"
namespace Service {
namespace NS {
-class NS_S final : public Interface {
+/// Interface to "ns:s" service
+class NS_S final : public ServiceFramework<NS_S> {
public:
NS_S();
-
- std::string GetPortName() const override {
- return "ns:s";
- }
+ ~NS_S();
};
} // namespace NS
diff --git a/src/core/hle/service/ns_s.cpp b/src/core/hle/service/ns_s.cpp
deleted file mode 100644
index 215c9aacc..000000000
--- a/src/core/hle/service/ns_s.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copyright 2015 Citra Emulator Project
-// Licensed under GPLv2 or any later version
-// Refer to the license.txt file included.
-
-#include "core/hle/service/ns_s.h"
-
-namespace Service {
-namespace NS {
-
-const Interface::FunctionInfo FunctionTable[] = {
- {0x000100C0, nullptr, "LaunchFIRM"},
- {0x000200C0, nullptr, "LaunchTitle"},
- {0x00030000, nullptr, "TerminateApplication"},
- {0x00040040, nullptr, "TerminateProcess"},
- {0x000500C0, nullptr, "LaunchApplicationFIRM"},
- {0x00060042, nullptr, "SetFIRMParams4A0"},
- {0x00070042, nullptr, "CardUpdateInitialize"},
- {0x00080000, nullptr, "CardUpdateShutdown"},
- {0x000D0140, nullptr, "SetTWLBannerHMAC"},
- {0x000E0000, nullptr, "ShutdownAsync"},
- {0x00100180, nullptr, "RebootSystem"},
- {0x00110100, nullptr, "TerminateTitle"},
- {0x001200C0, nullptr, "SetApplicationCpuTimeLimit"},
- {0x00150140, nullptr, "LaunchApplication"},
- {0x00160000, nullptr, "RebootSystemClean"},
-};
-
-NS_S::NS_S() {
- Register(FunctionTable);
-}
-
-} // namespace NS
-} // namespace Service
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index aad950e50..f267aad74 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -38,7 +38,7 @@
#include "core/hle/service/news/news.h"
#include "core/hle/service/nfc/nfc.h"
#include "core/hle/service/nim/nim.h"
-#include "core/hle/service/ns_s.h"
+#include "core/hle/service/ns/ns.h"
#include "core/hle/service/nwm/nwm.h"
#include "core/hle/service/pm_app.h"
#include "core/hle/service/ptm/ptm.h"
@@ -215,6 +215,8 @@ void Init() {
SM::g_service_manager = std::make_shared<SM::ServiceManager>();
SM::ServiceManager::InstallInterfaces(SM::g_service_manager);
+ NS::InstallInterfaces(*SM::g_service_manager);
+
AddNamedPort(new ERR::ERR_F);
FS::ArchiveInit();
@@ -246,7 +248,6 @@ void Init() {
AddService(new HTTP::HTTP_C);
AddService(new LDR::LDR_RO);
AddService(new MIC::MIC_U);
- AddService(new NS::NS_S);
AddService(new PM::PM_APP);
AddService(new SOC::SOC_U);
AddService(new SSL::SSL_C);
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp
index 74e336487..69cdc0867 100644
--- a/src/core/loader/3dsx.cpp
+++ b/src/core/loader/3dsx.cpp
@@ -270,6 +270,7 @@ ResultStatus AppLoader_THREEDSX::Load() {
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
Kernel::g_current_process->svc_access_mask.set();
Kernel::g_current_process->address_mappings = default_address_mappings;
+ Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table;
// Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit =
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index cfcde9167..2f27606a1 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -397,6 +397,7 @@ ResultStatus AppLoader_ELF::Load() {
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
Kernel::g_current_process->svc_access_mask.set();
Kernel::g_current_process->address_mappings = default_address_mappings;
+ Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table;
// Attach the default resource limit (APPLICATION) to the process
Kernel::g_current_process->resource_limit =
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 7aff7f29b..79ea50147 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -172,6 +172,7 @@ ResultStatus AppLoader_NCCH::LoadExec() {
codeset->memory = std::make_shared<std::vector<u8>>(std::move(code));
Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
+ Memory::current_page_table = &Kernel::g_current_process->vm_manager.page_table;
// Attach a resource limit to the process based on the resource limit category
Kernel::g_current_process->resource_limit =
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index 097bc5b47..68a6b1ac2 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -4,83 +4,31 @@
#include <array>
#include <cstring>
+#include "audio_core/audio_core.h"
#include "common/assert.h"
#include "common/common_types.h"
#include "common/logging/log.h"
#include "common/swap.h"
+#include "core/hle/kernel/memory.h"
#include "core/hle/kernel/process.h"
#include "core/hle/lock.h"
#include "core/memory.h"
#include "core/memory_setup.h"
-#include "core/mmio.h"
#include "video_core/renderer_base.h"
#include "video_core/video_core.h"
namespace Memory {
-enum class PageType {
- /// Page is unmapped and should cause an access error.
- Unmapped,
- /// Page is mapped to regular memory. This is the only type you can get pointers to.
- Memory,
- /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and
- /// invalidation
- RasterizerCachedMemory,
- /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
- Special,
- /// Page is mapped to a I/O region, but also needs to check for rasterizer cache flushing and
- /// invalidation
- RasterizerCachedSpecial,
-};
-
-struct SpecialRegion {
- VAddr base;
- u32 size;
- MMIORegionPointer handler;
-};
+static std::array<u8, Memory::VRAM_SIZE> vram;
+static std::array<u8, Memory::N3DS_EXTRA_RAM_SIZE> n3ds_extra_ram;
-/**
- * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely
- * mimics the way a real CPU page table works, but instead is optimized for minimal decoding and
- * fetching requirements when accessing. In the usual case of an access to regular memory, it only
- * requires an indexed fetch and a check for NULL.
- */
-struct PageTable {
- /**
- * Array of memory pointers backing each page. An entry can only be non-null if the
- * corresponding entry in the `attributes` array is of type `Memory`.
- */
- std::array<u8*, PAGE_TABLE_NUM_ENTRIES> pointers;
-
- /**
- * Contains MMIO handlers that back memory regions whose entries in the `attribute` array is of
- * type `Special`.
- */
- std::vector<SpecialRegion> special_regions;
-
- /**
- * Array of fine grained page attributes. If it is set to any value other than `Memory`, then
- * the corresponding entry in `pointers` MUST be set to null.
- */
- std::array<PageType, PAGE_TABLE_NUM_ENTRIES> attributes;
-
- /**
- * Indicates the number of externally cached resources touching a page that should be
- * flushed before the memory is accessed
- */
- std::array<u8, PAGE_TABLE_NUM_ENTRIES> cached_res_count;
-};
-
-/// Singular page table used for the singleton process
-static PageTable main_page_table;
-/// Currently active page table
-static PageTable* current_page_table = &main_page_table;
+PageTable* current_page_table = nullptr;
std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers() {
return &current_page_table->pointers;
}
-static void MapPages(u32 base, u32 size, u8* memory, PageType type) {
+static void MapPages(PageTable& page_table, u32 base, u32 size, u8* memory, PageType type) {
LOG_DEBUG(HW_Memory, "Mapping %p onto %08X-%08X", memory, base * PAGE_SIZE,
(base + size) * PAGE_SIZE);
@@ -91,9 +39,9 @@ static void MapPages(u32 base, u32 size, u8* memory, PageType type) {
while (base != end) {
ASSERT_MSG(base < PAGE_TABLE_NUM_ENTRIES, "out of range mapping at %08X", base);
- current_page_table->attributes[base] = type;
- current_page_table->pointers[base] = memory;
- current_page_table->cached_res_count[base] = 0;
+ page_table.attributes[base] = type;
+ page_table.pointers[base] = memory;
+ page_table.cached_res_count[base] = 0;
base += 1;
if (memory != nullptr)
@@ -101,30 +49,24 @@ static void MapPages(u32 base, u32 size, u8* memory, PageType type) {
}
}
-void InitMemoryMap() {
- main_page_table.pointers.fill(nullptr);
- main_page_table.attributes.fill(PageType::Unmapped);
- main_page_table.cached_res_count.fill(0);
-}
-
-void MapMemoryRegion(VAddr base, u32 size, u8* target) {
+void MapMemoryRegion(PageTable& page_table, VAddr base, u32 size, u8* target) {
ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
- MapPages(base / PAGE_SIZE, size / PAGE_SIZE, target, PageType::Memory);
+ MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, target, PageType::Memory);
}
-void MapIoRegion(VAddr base, u32 size, MMIORegionPointer mmio_handler) {
+void MapIoRegion(PageTable& page_table, VAddr base, u32 size, MMIORegionPointer mmio_handler) {
ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
- MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Special);
+ MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Special);
- current_page_table->special_regions.emplace_back(SpecialRegion{base, size, mmio_handler});
+ page_table.special_regions.emplace_back(SpecialRegion{base, size, mmio_handler});
}
-void UnmapRegion(VAddr base, u32 size) {
+void UnmapRegion(PageTable& page_table, VAddr base, u32 size) {
ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
- MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Unmapped);
+ MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Unmapped);
}
/**
@@ -273,8 +215,7 @@ bool IsValidVirtualAddress(const VAddr vaddr) {
}
bool IsValidPhysicalAddress(const PAddr paddr) {
- boost::optional<VAddr> vaddr = PhysicalToVirtualAddress(paddr);
- return vaddr && IsValidVirtualAddress(*vaddr);
+ return GetPhysicalPointer(paddr) != nullptr;
}
u8* GetPointer(const VAddr vaddr) {
@@ -306,9 +247,63 @@ std::string ReadCString(VAddr vaddr, std::size_t max_length) {
}
u8* GetPhysicalPointer(PAddr address) {
- // TODO(Subv): This call should not go through the application's memory mapping.
- boost::optional<VAddr> vaddr = PhysicalToVirtualAddress(address);
- return vaddr ? GetPointer(*vaddr) : nullptr;
+ struct MemoryArea {
+ PAddr paddr_base;
+ u32 size;
+ };
+
+ static constexpr MemoryArea memory_areas[] = {
+ {VRAM_PADDR, VRAM_SIZE},
+ {IO_AREA_PADDR, IO_AREA_SIZE},
+ {DSP_RAM_PADDR, DSP_RAM_SIZE},
+ {FCRAM_PADDR, FCRAM_N3DS_SIZE},
+ {N3DS_EXTRA_RAM_PADDR, N3DS_EXTRA_RAM_SIZE},
+ };
+
+ const auto area =
+ std::find_if(std::begin(memory_areas), std::end(memory_areas), [&](const auto& area) {
+ return address >= area.paddr_base && address < area.paddr_base + area.size;
+ });
+
+ if (area == std::end(memory_areas)) {
+ LOG_ERROR(HW_Memory, "unknown GetPhysicalPointer @ 0x%08X", address);
+ return nullptr;
+ }
+
+ if (area->paddr_base == IO_AREA_PADDR) {
+ LOG_ERROR(HW_Memory, "MMIO mappings are not supported yet. phys_addr=0x%08X", address);
+ return nullptr;
+ }
+
+ u32 offset_into_region = address - area->paddr_base;
+
+ u8* target_pointer = nullptr;
+ switch (area->paddr_base) {
+ case VRAM_PADDR:
+ target_pointer = vram.data() + offset_into_region;
+ break;
+ case DSP_RAM_PADDR:
+ target_pointer = AudioCore::GetDspMemory().data() + offset_into_region;
+ break;
+ case FCRAM_PADDR:
+ for (const auto& region : Kernel::memory_regions) {
+ if (offset_into_region >= region.base &&
+ offset_into_region < region.base + region.size) {
+ target_pointer =
+ region.linear_heap_memory->data() + offset_into_region - region.base;
+ break;
+ }
+ }
+ ASSERT_MSG(target_pointer != nullptr, "Invalid FCRAM address");
+ break;
+ case N3DS_EXTRA_RAM_PADDR:
+ target_pointer = n3ds_extra_ram.data() + offset_into_region;
+ break;
+ default:
+ UNREACHABLE();
+ }
+
+ return target_pointer;
}
void RasterizerMarkRegionCached(PAddr start, u32 size, int count_delta) {
diff --git a/src/core/memory.h b/src/core/memory.h
index c8c56babd..b228a48c2 100644
--- a/src/core/memory.h
+++ b/src/core/memory.h
@@ -7,8 +7,10 @@
#include <array>
#include <cstddef>
#include <string>
+#include <vector>
#include <boost/optional.hpp>
#include "common/common_types.h"
+#include "core/mmio.h"
namespace Memory {
@@ -21,6 +23,59 @@ const u32 PAGE_MASK = PAGE_SIZE - 1;
const int PAGE_BITS = 12;
const size_t PAGE_TABLE_NUM_ENTRIES = 1 << (32 - PAGE_BITS);
+enum class PageType {
+ /// Page is unmapped and should cause an access error.
+ Unmapped,
+ /// Page is mapped to regular memory. This is the only type you can get pointers to.
+ Memory,
+ /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and
+ /// invalidation
+ RasterizerCachedMemory,
+ /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
+ Special,
+ /// Page is mapped to a I/O region, but also needs to check for rasterizer cache flushing and
+ /// invalidation
+ RasterizerCachedSpecial,
+};
+
+struct SpecialRegion {
+ VAddr base;
+ u32 size;
+ MMIORegionPointer handler;
+};
+
+/**
+ * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely
+ * mimics the way a real CPU page table works, but instead is optimized for minimal decoding and
+ * fetching requirements when accessing. In the usual case of an access to regular memory, it only
+ * requires an indexed fetch and a check for NULL.
+ */
+struct PageTable {
+ /**
+ * Array of memory pointers backing each page. An entry can only be non-null if the
+ * corresponding entry in the `attributes` array is of type `Memory`.
+ */
+ std::array<u8*, PAGE_TABLE_NUM_ENTRIES> pointers;
+
+ /**
+ * Contains MMIO handlers that back memory regions whose entries in the `attribute` array is of
+ * type `Special`.
+ */
+ std::vector<SpecialRegion> special_regions;
+
+ /**
+ * Array of fine grained page attributes. If it is set to any value other than `Memory`, then
+ * the corresponding entry in `pointers` MUST be set to null.
+ */
+ std::array<PageType, PAGE_TABLE_NUM_ENTRIES> attributes;
+
+ /**
+ * Indicates the number of externally cached resources touching a page that should be
+ * flushed before the memory is accessed
+ */
+ std::array<u8, PAGE_TABLE_NUM_ENTRIES> cached_res_count;
+};
+
/// Physical memory regions as seen from the ARM11
enum : PAddr {
/// IO register area
@@ -126,6 +181,9 @@ enum : VAddr {
NEW_LINEAR_HEAP_VADDR_END = NEW_LINEAR_HEAP_VADDR + NEW_LINEAR_HEAP_SIZE,
};
+/// Currently active page table
+extern PageTable* current_page_table;
+
bool IsValidVirtualAddress(const VAddr addr);
bool IsValidPhysicalAddress(const PAddr addr);
@@ -169,8 +227,6 @@ boost::optional<VAddr> PhysicalToVirtualAddress(PAddr addr);
/**
* Gets a pointer to the memory region beginning at the specified physical address.
- *
- * @note This is currently implemented using PhysicalToVirtualAddress().
*/
u8* GetPhysicalPointer(PAddr address);
@@ -209,4 +265,4 @@ void RasterizerFlushVirtualRegion(VAddr start, u32 size, FlushMode mode);
* retrieve the current page table for that purpose.
*/
std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers();
-}
+} // namespace Memory
diff --git a/src/core/memory_setup.h b/src/core/memory_setup.h
index 3fdf3a87d..c58baa50b 100644
--- a/src/core/memory_setup.h
+++ b/src/core/memory_setup.h
@@ -9,24 +9,24 @@
namespace Memory {
-void InitMemoryMap();
-
/**
* Maps an allocated buffer onto a region of the emulated process address space.
*
+ * @param page_table The page table of the emulated process.
* @param base The address to start mapping at. Must be page-aligned.
* @param size The amount of bytes to map. Must be page-aligned.
* @param target Buffer with the memory backing the mapping. Must be of length at least `size`.
*/
-void MapMemoryRegion(VAddr base, u32 size, u8* target);
+void MapMemoryRegion(PageTable& page_table, VAddr base, u32 size, u8* target);
/**
* Maps a region of the emulated process address space as a IO region.
+ * @param page_table The page table of the emulated process.
* @param base The address to start mapping at. Must be page-aligned.
* @param size The amount of bytes to map. Must be page-aligned.
* @param mmio_handler The handler that backs the mapping.
*/
-void MapIoRegion(VAddr base, u32 size, MMIORegionPointer mmio_handler);
+void MapIoRegion(PageTable& page_table, VAddr base, u32 size, MMIORegionPointer mmio_handler);
-void UnmapRegion(VAddr base, u32 size);
+void UnmapRegion(PageTable& page_table, VAddr base, u32 size);
}
diff --git a/src/core/settings.h b/src/core/settings.h
index 024f14666..8d78cb424 100644
--- a/src/core/settings.h
+++ b/src/core/settings.h
@@ -133,6 +133,7 @@ struct Values {
// WebService
bool enable_telemetry;
std::string telemetry_endpoint_url;
+ std::string verify_endpoint_url;
std::string citra_username;
std::string citra_token;
} extern values;
diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp
index 104a16cc9..ca517ff44 100644
--- a/src/core/telemetry_session.cpp
+++ b/src/core/telemetry_session.cpp
@@ -15,6 +15,7 @@
#ifdef ENABLE_WEB_SERVICE
#include "web_service/telemetry_json.h"
+#include "web_service/verify_login.h"
#endif
namespace Core {
@@ -75,6 +76,17 @@ u64 RegenerateTelemetryId() {
return new_telemetry_id;
}
+std::future<bool> VerifyLogin(std::string username, std::string token, std::function<void()> func) {
+#ifdef ENABLE_WEB_SERVICE
+ return WebService::VerifyLogin(username, token, Settings::values.verify_endpoint_url, func);
+#else
+ return std::async(std::launch::async, [func{std::move(func)}]() {
+ func();
+ return false;
+ });
+#endif
+}
+
TelemetrySession::TelemetrySession() {
#ifdef ENABLE_WEB_SERVICE
if (Settings::values.enable_telemetry) {
diff --git a/src/core/telemetry_session.h b/src/core/telemetry_session.h
index 65613daae..550c6ea2d 100644
--- a/src/core/telemetry_session.h
+++ b/src/core/telemetry_session.h
@@ -4,6 +4,7 @@
#pragma once
+#include <future>
#include <memory>
#include "common/telemetry.h"
@@ -47,4 +48,13 @@ u64 GetTelemetryId();
*/
u64 RegenerateTelemetryId();
+/**
+ * Verifies the username and token.
+ * @param username Citra username to use for authentication.
+ * @param token Citra token to use for authentication.
+ * @param func A function that gets exectued when the verification is finished
+ * @returns Future with bool indicating whether the verification succeeded
+ */
+std::future<bool> VerifyLogin(std::string username, std::string token, std::function<void()> func);
+
} // namespace Core
diff --git a/src/tests/core/arm/arm_test_common.cpp b/src/tests/core/arm/arm_test_common.cpp
index 1df6c5677..8384ce744 100644
--- a/src/tests/core/arm/arm_test_common.cpp
+++ b/src/tests/core/arm/arm_test_common.cpp
@@ -3,20 +3,30 @@
// Refer to the license.txt file included.
#include "core/core.h"
+#include "core/memory.h"
#include "core/memory_setup.h"
#include "tests/core/arm/arm_test_common.h"
namespace ArmTests {
+static Memory::PageTable page_table;
+
TestEnvironment::TestEnvironment(bool mutable_memory_)
: mutable_memory(mutable_memory_), test_memory(std::make_shared<TestMemory>(this)) {
- Memory::MapIoRegion(0x00000000, 0x80000000, test_memory);
- Memory::MapIoRegion(0x80000000, 0x80000000, test_memory);
+
+ page_table.pointers.fill(nullptr);
+ page_table.attributes.fill(Memory::PageType::Unmapped);
+ page_table.cached_res_count.fill(0);
+
+ Memory::MapIoRegion(page_table, 0x00000000, 0x80000000, test_memory);
+ Memory::MapIoRegion(page_table, 0x80000000, 0x80000000, test_memory);
+
+ Memory::current_page_table = &page_table;
}
TestEnvironment::~TestEnvironment() {
- Memory::UnmapRegion(0x80000000, 0x80000000);
- Memory::UnmapRegion(0x00000000, 0x80000000);
+ Memory::UnmapRegion(page_table, 0x80000000, 0x80000000);
+ Memory::UnmapRegion(page_table, 0x00000000, 0x80000000);
}
void TestEnvironment::SetMemory64(VAddr vaddr, u64 value) {
diff --git a/src/video_core/regs_rasterizer.h b/src/video_core/regs_rasterizer.h
index 2874fd127..4fef00d76 100644
--- a/src/video_core/regs_rasterizer.h
+++ b/src/video_core/regs_rasterizer.h
@@ -5,10 +5,10 @@
#pragma once
#include <array>
-
#include "common/bit_field.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
+#include "video_core/pica_types.h"
namespace Pica {
@@ -31,7 +31,17 @@ struct RasterizerRegs {
BitField<0, 24, u32> viewport_size_y;
- INSERT_PADDING_WORDS(0x9);
+ INSERT_PADDING_WORDS(0x3);
+
+ BitField<0, 1, u32> clip_enable;
+ BitField<0, 24, u32> clip_coef[4]; // float24
+
+ Math::Vec4<float24> GetClipCoef() const {
+ return {float24::FromRaw(clip_coef[0]), float24::FromRaw(clip_coef[1]),
+ float24::FromRaw(clip_coef[2]), float24::FromRaw(clip_coef[3])};
+ }
+
+ INSERT_PADDING_WORDS(0x1);
BitField<0, 24, u32> viewport_depth_range; // float24
BitField<0, 24, u32> viewport_depth_near_plane; // float24
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index aa95ef21d..7e09e4712 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -169,6 +169,8 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, proctex_diff_lut_buffer.handle);
// Sync fixed function OpenGL state
+ SyncClipEnabled();
+ SyncClipCoef();
SyncCullMode();
SyncBlendEnabled();
SyncBlendFuncs();
@@ -235,13 +237,24 @@ void RasterizerOpenGL::DrawTriangles() {
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
color_surface != nullptr ? color_surface->texture.handle : 0, 0);
- glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
- depth_surface != nullptr ? depth_surface->texture.handle : 0, 0);
- bool has_stencil =
- regs.framebuffer.framebuffer.depth_format == Pica::FramebufferRegs::DepthFormat::D24S8;
- glFramebufferTexture2D(
- GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
- (has_stencil && depth_surface != nullptr) ? depth_surface->texture.handle : 0, 0);
+ if (depth_surface != nullptr) {
+ if (regs.framebuffer.framebuffer.depth_format ==
+ Pica::FramebufferRegs::DepthFormat::D24S8) {
+ // attach both depth and stencil
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
+ depth_surface->texture.handle, 0);
+ } else {
+ // attach depth
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
+ depth_surface->texture.handle, 0);
+ // clear stencil attachment
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
+ }
+ } else {
+ // clear both depth and stencil attachment
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0,
+ 0);
+ }
// Sync the viewport
// These registers hold half-width and half-height, so must be multiplied by 2
@@ -401,6 +414,18 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
SyncCullMode();
break;
+ // Clipping plane
+ case PICA_REG_INDEX(rasterizer.clip_enable):
+ SyncClipEnabled();
+ break;
+
+ case PICA_REG_INDEX_WORKAROUND(rasterizer.clip_coef[0], 0x48):
+ case PICA_REG_INDEX_WORKAROUND(rasterizer.clip_coef[1], 0x49):
+ case PICA_REG_INDEX_WORKAROUND(rasterizer.clip_coef[2], 0x4a):
+ case PICA_REG_INDEX_WORKAROUND(rasterizer.clip_coef[3], 0x4b):
+ SyncClipCoef();
+ break;
+
// Depth modifiers
case PICA_REG_INDEX(rasterizer.viewport_depth_range):
SyncDepthScale();
@@ -1280,6 +1305,20 @@ void RasterizerOpenGL::SetShader() {
}
}
+void RasterizerOpenGL::SyncClipEnabled() {
+ state.clip_distance[1] = Pica::g_state.regs.rasterizer.clip_enable != 0;
+}
+
+void RasterizerOpenGL::SyncClipCoef() {
+ const auto raw_clip_coef = Pica::g_state.regs.rasterizer.GetClipCoef();
+ const GLvec4 new_clip_coef = {raw_clip_coef.x.ToFloat32(), raw_clip_coef.y.ToFloat32(),
+ raw_clip_coef.z.ToFloat32(), raw_clip_coef.w.ToFloat32()};
+ if (new_clip_coef != uniform_block_data.data.clip_coef) {
+ uniform_block_data.data.clip_coef = new_clip_coef;
+ uniform_block_data.dirty = true;
+ }
+}
+
void RasterizerOpenGL::SyncCullMode() {
const auto& regs = Pica::g_state.regs;
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index 78e218efe..46c62961c 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -151,14 +151,21 @@ private:
LightSrc light_src[8];
alignas(16) GLvec4 const_color[6]; // A vec4 color for each of the six tev stages
alignas(16) GLvec4 tev_combiner_buffer_color;
+ alignas(16) GLvec4 clip_coef;
};
static_assert(
- sizeof(UniformData) == 0x460,
+ sizeof(UniformData) == 0x470,
"The size of the UniformData structure has changed, update the structure in the shader");
static_assert(sizeof(UniformData) < 16384,
"UniformData structure must be less than 16kb as per the OpenGL spec");
+ /// Syncs the clip enabled status to match the PICA register
+ void SyncClipEnabled();
+
+ /// Syncs the clip coefficients to match the PICA register
+ void SyncClipCoef();
+
/// Sets the OpenGL shader in accordance with the current PICA register state
void SetShader();
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp
index c536e61e1..9fe183944 100644
--- a/src/video_core/renderer_opengl/gl_shader_gen.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp
@@ -25,6 +25,42 @@ using TevStageConfig = TexturingRegs::TevStageConfig;
namespace GLShader {
+static const std::string UniformBlockDef = R"(
+#define NUM_TEV_STAGES 6
+#define NUM_LIGHTS 8
+
+struct LightSrc {
+ vec3 specular_0;
+ vec3 specular_1;
+ vec3 diffuse;
+ vec3 ambient;
+ vec3 position;
+ vec3 spot_direction;
+ float dist_atten_bias;
+ float dist_atten_scale;
+};
+
+layout (std140) uniform shader_data {
+ vec2 framebuffer_scale;
+ int alphatest_ref;
+ float depth_scale;
+ float depth_offset;
+ int scissor_x1;
+ int scissor_y1;
+ int scissor_x2;
+ int scissor_y2;
+ vec3 fog_color;
+ vec2 proctex_noise_f;
+ vec2 proctex_noise_a;
+ vec2 proctex_noise_p;
+ vec3 lighting_global_ambient;
+ LightSrc light_src[NUM_LIGHTS];
+ vec4 const_color[NUM_TEV_STAGES];
+ vec4 tev_combiner_buffer_color;
+ vec4 clip_coef;
+};
+)";
+
PicaShaderConfig PicaShaderConfig::BuildFromRegs(const Pica::Regs& regs) {
PicaShaderConfig res;
@@ -1010,8 +1046,6 @@ std::string GenerateFragmentShader(const PicaShaderConfig& config) {
std::string out = R"(
#version 330 core
-#define NUM_TEV_STAGES 6
-#define NUM_LIGHTS 8
in vec4 primary_color;
in vec2 texcoord[3];
@@ -1023,36 +1057,6 @@ in vec4 gl_FragCoord;
out vec4 color;
-struct LightSrc {
- vec3 specular_0;
- vec3 specular_1;
- vec3 diffuse;
- vec3 ambient;
- vec3 position;
- vec3 spot_direction;
- float dist_atten_bias;
- float dist_atten_scale;
-};
-
-layout (std140) uniform shader_data {
- vec2 framebuffer_scale;
- int alphatest_ref;
- float depth_scale;
- float depth_offset;
- int scissor_x1;
- int scissor_y1;
- int scissor_x2;
- int scissor_y2;
- vec3 fog_color;
- vec2 proctex_noise_f;
- vec2 proctex_noise_a;
- vec2 proctex_noise_p;
- vec3 lighting_global_ambient;
- LightSrc light_src[NUM_LIGHTS];
- vec4 const_color[NUM_TEV_STAGES];
- vec4 tev_combiner_buffer_color;
-};
-
uniform sampler2D tex[3];
uniform samplerBuffer lighting_lut;
uniform samplerBuffer fog_lut;
@@ -1061,7 +1065,11 @@ uniform samplerBuffer proctex_color_map;
uniform samplerBuffer proctex_alpha_map;
uniform samplerBuffer proctex_lut;
uniform samplerBuffer proctex_diff_lut;
+)";
+
+ out += UniformBlockDef;
+ out += R"(
// Rotate the vector v by the quaternion q
vec3 quaternion_rotate(vec4 q, vec3 v) {
return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + q.w * v);
@@ -1197,6 +1205,12 @@ out float texcoord0_w;
out vec4 normquat;
out vec3 view;
+)";
+
+ out += UniformBlockDef;
+
+ out += R"(
+
void main() {
primary_color = vert_color;
texcoord[0] = vert_texcoord0;
@@ -1207,7 +1221,7 @@ void main() {
view = vert_view;
gl_Position = vert_position;
gl_ClipDistance[0] = -vert_position.z; // fixed PICA clipping plane z <= 0
- // TODO (wwylele): calculate gl_ClipDistance[1] from user-defined clipping plane
+ gl_ClipDistance[1] = dot(clip_coef, vert_position);
}
)";
diff --git a/src/video_core/shader/shader.cpp b/src/video_core/shader/shader.cpp
index e9063e616..2857d2829 100644
--- a/src/video_core/shader/shader.cpp
+++ b/src/video_core/shader/shader.cpp
@@ -52,7 +52,8 @@ OutputVertex OutputVertex::FromAttributeBuffer(const RasterizerRegs& regs,
// The hardware takes the absolute and saturates vertex colors like this, *before* doing
// interpolation
for (unsigned i = 0; i < 4; ++i) {
- ret.color[i] = float24::FromFloat32(std::fmin(std::fabs(ret.color[i].ToFloat32()), 1.0f));
+ float c = std::fabs(ret.color[i].ToFloat32());
+ ret.color[i] = float24::FromFloat32(c < 1.0f ? c : 1.0f);
}
LOG_TRACE(HW_GPU, "Output vertex: pos(%.2f, %.2f, %.2f, %.2f), quat(%.2f, %.2f, %.2f, %.2f), "
diff --git a/src/video_core/swrasterizer/clipper.cpp b/src/video_core/swrasterizer/clipper.cpp
index cdbc71502..a52129eb7 100644
--- a/src/video_core/swrasterizer/clipper.cpp
+++ b/src/video_core/swrasterizer/clipper.cpp
@@ -31,7 +31,7 @@ public:
: coeffs(coeffs), bias(bias) {}
bool IsInside(const Vertex& vertex) const {
- return Math::Dot(vertex.pos + bias, coeffs) <= float24::FromFloat32(0);
+ return Math::Dot(vertex.pos + bias, coeffs) >= float24::FromFloat32(0);
}
bool IsOutSide(const Vertex& vertex) const {
@@ -116,19 +116,18 @@ void ProcessTriangle(const OutputVertex& v0, const OutputVertex& v1, const Outpu
static const float24 f0 = float24::FromFloat32(0.0);
static const float24 f1 = float24::FromFloat32(1.0);
static const std::array<ClippingEdge, 7> clipping_edges = {{
- {Math::MakeVec(f1, f0, f0, -f1)}, // x = +w
- {Math::MakeVec(-f1, f0, f0, -f1)}, // x = -w
- {Math::MakeVec(f0, f1, f0, -f1)}, // y = +w
- {Math::MakeVec(f0, -f1, f0, -f1)}, // y = -w
- {Math::MakeVec(f0, f0, f1, f0)}, // z = 0
- {Math::MakeVec(f0, f0, -f1, -f1)}, // z = -w
- {Math::MakeVec(f0, f0, f0, -f1), Math::Vec4<float24>(f0, f0, f0, EPSILON)}, // w = EPSILON
+ {Math::MakeVec(-f1, f0, f0, f1)}, // x = +w
+ {Math::MakeVec(f1, f0, f0, f1)}, // x = -w
+ {Math::MakeVec(f0, -f1, f0, f1)}, // y = +w
+ {Math::MakeVec(f0, f1, f0, f1)}, // y = -w
+ {Math::MakeVec(f0, f0, -f1, f0)}, // z = 0
+ {Math::MakeVec(f0, f0, f1, f1)}, // z = -w
+ {Math::MakeVec(f0, f0, f0, f1), Math::Vec4<float24>(f0, f0, f0, EPSILON)}, // w = EPSILON
}};
// Simple implementation of the Sutherland-Hodgman clipping algorithm.
// TODO: Make this less inefficient (currently lots of useless buffering overhead happens here)
- for (auto edge : clipping_edges) {
-
+ auto Clip = [&](const ClippingEdge& edge) {
std::swap(input_list, output_list);
output_list->clear();
@@ -147,12 +146,24 @@ void ProcessTriangle(const OutputVertex& v0, const OutputVertex& v1, const Outpu
}
reference_vertex = &vertex;
}
+ };
+
+ for (auto edge : clipping_edges) {
+ Clip(edge);
// Need to have at least a full triangle to continue...
if (output_list->size() < 3)
return;
}
+ if (g_state.regs.rasterizer.clip_enable) {
+ ClippingEdge custom_edge{g_state.regs.rasterizer.GetClipCoef()};
+ Clip(custom_edge);
+
+ if (output_list->size() < 3)
+ return;
+ }
+
InitScreenCoordinates((*output_list)[0]);
InitScreenCoordinates((*output_list)[1]);
diff --git a/src/web_service/CMakeLists.txt b/src/web_service/CMakeLists.txt
index 334d82a8a..c93811892 100644
--- a/src/web_service/CMakeLists.txt
+++ b/src/web_service/CMakeLists.txt
@@ -1,10 +1,12 @@
set(SRCS
telemetry_json.cpp
+ verify_login.cpp
web_backend.cpp
)
set(HEADERS
telemetry_json.h
+ verify_login.h
web_backend.h
)
diff --git a/src/web_service/verify_login.cpp b/src/web_service/verify_login.cpp
new file mode 100644
index 000000000..1bc3b5afe
--- /dev/null
+++ b/src/web_service/verify_login.cpp
@@ -0,0 +1,28 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <json.hpp>
+#include "web_service/verify_login.h"
+#include "web_service/web_backend.h"
+
+namespace WebService {
+
+std::future<bool> VerifyLogin(std::string& username, std::string& token,
+ const std::string& endpoint_url, std::function<void()> func) {
+ auto get_func = [func, username](const std::string& reply) -> bool {
+ func();
+ if (reply.empty())
+ return false;
+ nlohmann::json json = nlohmann::json::parse(reply);
+ std::string result;
+ try {
+ result = json["username"];
+ } catch (const nlohmann::detail::out_of_range&) {
+ }
+ return result == username;
+ };
+ return GetJson<bool>(get_func, endpoint_url, false, username, token);
+}
+
+} // namespace WebService
diff --git a/src/web_service/verify_login.h b/src/web_service/verify_login.h
new file mode 100644
index 000000000..303f5dbbc
--- /dev/null
+++ b/src/web_service/verify_login.h
@@ -0,0 +1,24 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <functional>
+#include <future>
+#include <string>
+
+namespace WebService {
+
+/**
+ * Checks if username and token is valid
+ * @param username Citra username to use for authentication.
+ * @param token Citra token to use for authentication.
+ * @param endpoint_url URL of the services.citra-emu.org endpoint.
+ * @param func A function that gets exectued when the verification is finished
+ * @returns Future with bool indicating whether the verification succeeded
+ */
+std::future<bool> VerifyLogin(std::string& username, std::string& token,
+ const std::string& endpoint_url, std::function<void()> func);
+
+} // namespace WebService
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index d28a3f757..b17d82f9c 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -18,6 +18,19 @@ static constexpr char API_VERSION[]{"1"};
static std::unique_ptr<cpr::Session> g_session;
+void Win32WSAStartup() {
+#ifdef _WIN32
+ // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to
+ // initialize Winsock globally, which fixes this problem. Without this, only the first CPR
+ // session will properly be created, and subsequent ones will fail.
+ WSADATA wsa_data;
+ const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)};
+ if (wsa_result) {
+ LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result);
+ }
+#endif
+}
+
void PostJson(const std::string& url, const std::string& data, bool allow_anonymous,
const std::string& username, const std::string& token) {
if (url.empty()) {
@@ -31,16 +44,7 @@ void PostJson(const std::string& url, const std::string& data, bool allow_anonym
return;
}
-#ifdef _WIN32
- // On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to
- // initialize Winsock globally, which fixes this problem. Without this, only the first CPR
- // session will properly be created, and subsequent ones will fail.
- WSADATA wsa_data;
- const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)};
- if (wsa_result) {
- LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result);
- }
-#endif
+ Win32WSAStartup();
// Built request header
cpr::Header header;
@@ -56,8 +60,81 @@ void PostJson(const std::string& url, const std::string& data, bool allow_anonym
}
// Post JSON asynchronously
- static cpr::AsyncResponse future;
- future = cpr::PostAsync(cpr::Url{url.c_str()}, cpr::Body{data.c_str()}, header);
+ static std::future<void> future;
+ future = cpr::PostCallback(
+ [](cpr::Response r) {
+ if (r.error) {
+ LOG_ERROR(WebService, "POST returned cpr error: %u:%s",
+ static_cast<u32>(r.error.code), r.error.message.c_str());
+ return;
+ }
+ if (r.status_code >= 400) {
+ LOG_ERROR(WebService, "POST returned error status code: %u", r.status_code);
+ return;
+ }
+ if (r.header["content-type"].find("application/json") == std::string::npos) {
+ LOG_ERROR(WebService, "POST returned wrong content: %s",
+ r.header["content-type"].c_str());
+ return;
+ }
+ },
+ cpr::Url{url}, cpr::Body{data}, header);
+}
+
+template <typename T>
+std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url,
+ bool allow_anonymous, const std::string& username,
+ const std::string& token) {
+ if (url.empty()) {
+ LOG_ERROR(WebService, "URL is invalid");
+ return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); });
+ }
+
+ const bool are_credentials_provided{!token.empty() && !username.empty()};
+ if (!allow_anonymous && !are_credentials_provided) {
+ LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
+ return std::async(std::launch::async, [func{std::move(func)}]() { return func(""); });
+ }
+
+ Win32WSAStartup();
+
+ // Built request header
+ cpr::Header header;
+ if (are_credentials_provided) {
+ // Authenticated request if credentials are provided
+ header = {{"Content-Type", "application/json"},
+ {"x-username", username.c_str()},
+ {"x-token", token.c_str()},
+ {"api-version", API_VERSION}};
+ } else {
+ // Otherwise, anonymous request
+ header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}};
+ }
+
+ // Get JSON asynchronously
+ return cpr::GetCallback(
+ [func{std::move(func)}](cpr::Response r) {
+ if (r.error) {
+ LOG_ERROR(WebService, "GET returned cpr error: %u:%s",
+ static_cast<u32>(r.error.code), r.error.message.c_str());
+ return func("");
+ }
+ if (r.status_code >= 400) {
+ LOG_ERROR(WebService, "GET returned error code: %u", r.status_code);
+ return func("");
+ }
+ if (r.header["content-type"].find("application/json") == std::string::npos) {
+ LOG_ERROR(WebService, "GET returned wrong content: %s",
+ r.header["content-type"].c_str());
+ return func("");
+ }
+ return func(r.text);
+ },
+ cpr::Url{url}, header);
}
+template std::future<bool> GetJson(std::function<bool(const std::string&)> func,
+ const std::string& url, bool allow_anonymous,
+ const std::string& username, const std::string& token);
+
} // namespace WebService
diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h
index d17100398..a63c75d13 100644
--- a/src/web_service/web_backend.h
+++ b/src/web_service/web_backend.h
@@ -4,6 +4,8 @@
#pragma once
+#include <functional>
+#include <future>
#include <string>
#include "common/common_types.h"
@@ -20,4 +22,18 @@ namespace WebService {
void PostJson(const std::string& url, const std::string& data, bool allow_anonymous,
const std::string& username = {}, const std::string& token = {});
+/**
+ * Gets JSON from services.citra-emu.org.
+ * @param func A function that gets exectued when the json as a string is received
+ * @param url URL of the services.citra-emu.org endpoint to post data to.
+ * @param allow_anonymous If true, allow anonymous unauthenticated requests.
+ * @param username Citra username to use for authentication.
+ * @param token Citra token to use for authentication.
+ * @return future that holds the return value T of the func
+ */
+template <typename T>
+std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url,
+ bool allow_anonymous, const std::string& username = {},
+ const std::string& token = {});
+
} // namespace WebService