cellMusicExport: first implementation

This commit is contained in:
Megamouse 2023-03-31 00:14:28 +02:00
parent 4289ffef1c
commit 3e556c710b

View File

@ -1,9 +1,10 @@
#include "stdafx.h"
#include "Emu/Cell/PPUModule.h"
#include "Emu/IdManager.h"
#include "Emu/VFS.h"
#include "Utilities/StrUtil.h"
#include "cellSysutil.h"
LOG_CHANNEL(cellMusicExport);
// Return Codes
@ -44,6 +45,15 @@ void fmt_class_string<CellMusicExportError>::format(std::string& out, u64 arg)
});
}
enum
{
CELL_MUSIC_EXPORT_UTIL_VERSION_CURRENT = 0,
CELL_MUSIC_EXPORT_UTIL_HDD_PATH_MAX = 1055,
CELL_MUSIC_EXPORT_UTIL_MUSIC_TITLE_MAX_LENGTH = 64,
CELL_MUSIC_EXPORT_UTIL_GAME_TITLE_MAX_LENGTH = 64,
CELL_MUSIC_EXPORT_UTIL_GAME_COMMENT_MAX_SIZE = 1024,
};
struct CellMusicExportSetParam
{
vm::bptr<char> title;
@ -51,15 +61,100 @@ struct CellMusicExportSetParam
vm::bptr<char> artist;
vm::bptr<char> genre;
vm::bptr<char> game_comment;
vm::bptr<void> reserved1;
vm::bptr<char> reserved1;
vm::bptr<void> reserved2;
};
using CellMusicExportUtilFinishCallback = void(s32 result, vm::ptr<void> userdata);
struct music_export
{
atomic_t<s32> progress = 0; // 0x0-0xFFFF for 0-100%
};
bool check_music_path(const std::string& file_path)
{
if (file_path.size() >= CELL_MUSIC_EXPORT_UTIL_HDD_PATH_MAX)
{
return false;
}
for (char c : file_path)
{
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '_' ||
c == '/' || c == '.'))
{
return false;
}
}
if (!file_path.starts_with("/dev_hdd0"sv) &&
!file_path.starts_with("/dev_bdvd"sv) &&
!file_path.starts_with("/dev_hdd1"sv))
{
return false;
}
if (file_path.find(".."sv) != umax)
{
return false;
}
return true;
}
std::string get_available_music_path(const std::string& filename)
{
const std::string music_dir = "/dev_hdd0/music/";
std::string dst_path = vfs::get(music_dir + filename);
// Do not overwrite existing files. Add a suffix instead.
for (u32 i = 0; fs::exists(dst_path); i++)
{
const std::string suffix = fmt::format("_%d", i);
std::string new_filename = filename;
if (const usz pos = new_filename.find_last_of('.'); pos != std::string::npos)
{
new_filename.insert(pos, suffix);
}
else
{
new_filename.append(suffix);
}
dst_path = vfs::get(music_dir + new_filename);
}
return dst_path;
}
error_code cellMusicExportInitialize(u32 version, u32 container, vm::ptr<CellMusicExportUtilFinishCallback> funcFinish, vm::ptr<void> userdata)
{
cellMusicExport.todo("cellMusicExportInitialize(version=0x%x, container=0x%x, funcFinish=*0x%x, userdata=*0x%x)", version, container, funcFinish, userdata);
cellMusicExport.warning("cellMusicExportInitialize(version=0x%x, container=0x%x, funcFinish=*0x%x, userdata=*0x%x)", version, container, funcFinish, userdata);
if (version != CELL_MUSIC_EXPORT_UTIL_VERSION_CURRENT)
{
return CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM;
}
if (container != 0xfffffffe)
{
if (false) // TODO: Check if memory container size >= 0x300000
{
return CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM;
}
}
if (!funcFinish)
{
return CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM;
}
sysutil_register_cb([=](ppu_thread& ppu) -> s32
{
@ -72,7 +167,17 @@ error_code cellMusicExportInitialize(u32 version, u32 container, vm::ptr<CellMus
error_code cellMusicExportInitialize2(u32 version, vm::ptr<CellMusicExportUtilFinishCallback> funcFinish, vm::ptr<void> userdata)
{
cellMusicExport.todo("cellMusicExportInitialize2(version=0x%x, funcFinish=*0x%x, userdata=*0x%x)", version, funcFinish, userdata);
cellMusicExport.notice("cellMusicExportInitialize2(version=0x%x, funcFinish=*0x%x, userdata=*0x%x)", version, funcFinish, userdata);
if (version != CELL_MUSIC_EXPORT_UTIL_VERSION_CURRENT)
{
return CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM;
}
if (!funcFinish)
{
return CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM;
}
sysutil_register_cb([=](ppu_thread& ppu) -> s32
{
@ -85,7 +190,12 @@ error_code cellMusicExportInitialize2(u32 version, vm::ptr<CellMusicExportUtilFi
error_code cellMusicExportFinalize(vm::ptr<CellMusicExportUtilFinishCallback> funcFinish, vm::ptr<void> userdata)
{
cellMusicExport.todo("cellMusicExportFinalize(funcFinish=*0x%x, userdata=*0x%x)", funcFinish, userdata);
cellMusicExport.notice("cellMusicExportFinalize(funcFinish=*0x%x, userdata=*0x%x)", funcFinish, userdata);
if (!funcFinish)
{
return CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM;
}
sysutil_register_cb([=](ppu_thread& ppu) -> s32
{
@ -100,8 +210,69 @@ error_code cellMusicExportFromFile(vm::cptr<char> srcHddDir, vm::cptr<char> srcH
{
cellMusicExport.todo("cellMusicExportFromFile(srcHddDir=%s, srcHddFile=%s, param=*0x%x, funcFinish=*0x%x, userdata=*0x%x)", srcHddDir, srcHddFile, param, funcFinish, userdata);
if (!param || !funcFinish)
{
return CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM;
}
// TODO: check param members ?
cellMusicExport.notice("cellMusicExportFromFile: param: title=%s, game_title=%s, artist=%s, genre=%s, game_comment=%s", param->title, param->game_title, param->artist, param->genre, param->game_comment);
const std::string file_path = fmt::format("%s/%s", srcHddDir.get_ptr(), srcHddFile.get_ptr());
if (!check_music_path(file_path))
{
return { CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM, file_path };
}
std::string filename = param->title.get_ptr();
if (filename.empty())
{
return { CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM, "title empty" };
}
if (const std::string extension = get_file_extension(file_path); !extension.empty())
{
fmt::append(filename, ".%s", extension);
}
sysutil_register_cb([=](ppu_thread& ppu) -> s32
{
auto& mexp = g_fxo->get<music_export>();
mexp.progress = 0; // 0%
const std::string src_path = vfs::get(file_path);
const std::string dst_path = get_available_music_path(filename);
cellMusicExport.notice("Copying file from '%s' to '%s'", file_path, dst_path);
// TODO: We ignore metadata for now
if (!fs::create_path(fs::get_parent_dir(dst_path)) || !fs::copy_file(src_path, dst_path, false))
{
// TODO: find out which error is used
cellMusicExport.error("Failed to copy file from '%s' to '%s' (%s)", src_path, dst_path, fs::g_tls_error);
funcFinish(ppu, CELL_MUSIC_EXPORT_UTIL_ERROR_MOVE, userdata);
return CELL_OK;
}
if (!file_path.starts_with("/dev_bdvd"sv))
{
cellMusicExport.notice("Removing file '%s'", src_path);
if (!fs::remove_file(src_path))
{
// TODO: find out if an error is used here
cellMusicExport.error("Failed to remove file '%s' (%s)", src_path, fs::g_tls_error);
}
}
// TODO: track progress during file copy
mexp.progress = 0xFFFF; // 100%
funcFinish(ppu, CELL_OK, userdata);
return CELL_OK;
});
@ -113,9 +284,18 @@ error_code cellMusicExportProgress(vm::ptr<CellMusicExportUtilFinishCallback> fu
{
cellMusicExport.todo("cellMusicExportProgress(funcFinish=*0x%x, userdata=*0x%x)", funcFinish, userdata);
if (!funcFinish)
{
return CELL_MUSIC_EXPORT_UTIL_ERROR_PARAM;
}
sysutil_register_cb([=](ppu_thread& ppu) -> s32
{
funcFinish(ppu, 0xFFFF, userdata);
// Set the status as 0x0-0xFFFF (0-100%) depending on the copy status.
// Only the copy or move of the movie and metadata files is considered for the progress.
const auto& mexp = g_fxo->get<music_export>();
funcFinish(ppu, mexp.progress, userdata);
return CELL_OK;
});