feat: initial chunithm support

This commit is contained in:
2025-03-19 17:39:12 +00:00
parent 1191cdd95c
commit 8ac45df3e1
31 changed files with 1368 additions and 884 deletions

View File

@ -1,15 +1,19 @@
use anyhow::{Result, anyhow};
use ongeki::OngekiProfile;
use serde::{Deserialize, Serialize};
use tauri::AppHandle;
use std::{collections::BTreeSet, path::{Path, PathBuf}};
use crate::{model::misc::Game, modules::package::prepare_packages, pkg::PkgKey, pkg_store::PackageStore, util};
use crate::{model::{misc::Game, profile::Aime}, modules::package::prepare_packages, pkg::PkgKey, pkg_store::PackageStore, util};
use tauri::Emitter;
use std::process::Stdio;
use crate::model::profile::BepInEx;
use crate::model::{profile::{Display, DisplayMode, Network, Segatools}, segatools_base::segatools_base};
use anyhow::{anyhow, Result};
use std::fs::File;
use tokio::process::Command;
use tokio::task::JoinSet;
pub mod ongeki;
#[derive(Deserialize, Serialize, Clone)]
pub enum AnyProfile {
OngekiProfile(OngekiProfile)
pub trait ProfilePaths {
fn config_dir(&self) -> PathBuf;
fn data_dir(&self) -> PathBuf;
}
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
@ -18,147 +22,311 @@ pub struct ProfileMeta {
pub name: String
}
pub trait Profile: Sized {
fn new(name: String) -> Result<Self>;
fn load(name: String) -> Result<Self>;
fn save(&self) -> Result<()>;
async fn start(&self, app: AppHandle) -> Result<()>;
impl ProfilePaths for ProfileMeta {
fn config_dir(&self) -> PathBuf {
util::profile_config_dir(self.game, &self.name)
}
fn data_dir(&self) -> PathBuf {
util::data_dir().join(format!("profile-{}-{}", &self.game, &self.name))
}
}
pub trait ProfilePaths {
fn config_dir(&self) -> PathBuf;
fn data_dir(&self) -> PathBuf;
#[derive(Deserialize, Serialize, Clone)]
pub struct Profile {
pub meta: ProfileMeta,
pub data: ProfileData,
}
impl AnyProfile {
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct ProfileData {
pub mods: BTreeSet<PkgKey>,
pub sgt: Segatools,
pub display: Option<Display>,
pub network: Network,
#[serde(skip_serializing_if = "Option::is_none")]
pub bepinex: Option<BepInEx>,
#[cfg(not(target_os = "windows"))]
pub wine: crate::model::profile::Wine,
}
impl Profile {
pub fn new(mut meta: ProfileMeta) -> Result<Self> {
meta.name = fixed_name(&meta, true);
log::debug!("created profile-{:?}", &meta);
let p = Profile {
data: ProfileData {
mods: BTreeSet::new(),
sgt: Segatools::default_for(meta.game),
#[cfg(target_os = "windows")]
display: if meta.game == Game::Ongeki { Some(Display::default_for(meta.game)) } else { None },
#[cfg(not(target_os = "windows"))]
display: None,
network: Network::default(),
bepinex: if meta.game == Game::Ongeki { Some(BepInEx::default()) } else { None },
#[cfg(not(target_os = "windows"))]
wine: crate::model::profile::Wine::default(),
},
meta: meta.clone()
};
p.save()?;
std::fs::create_dir_all(p.config_dir())?;
std::fs::create_dir_all(p.data_dir())?;
std::fs::write(p.config_dir().join("segatools-base.ini"), segatools_base(meta.game))?;
Ok(p)
}
pub fn load(game: Game, name: String) -> Result<Self> {
Ok(match game {
Game::Ongeki => AnyProfile::OngekiProfile(OngekiProfile::load(name)?),
Game::Chunithm => panic!("Not implemented")
})
let path = util::profile_config_dir(game, &name).join("profile.json");
if let Ok(s) = std::fs::read_to_string(&path) {
let data = serde_json::from_str::<ProfileData>(&s)
.map_err(|e| anyhow!("Unable to parse {:?}: {:?}", path, e))?;
log::debug!("{:?}", data);
Ok(Profile {
meta: ProfileMeta {
game, name
},
data
})
} else {
Err(anyhow!("Unable to open {:?}", path))
}
}
pub fn save(&self) -> Result<()> {
match self {
Self::OngekiProfile(p) => p.save()
}
}
pub fn meta(&self) -> ProfileMeta {
match self {
Self::OngekiProfile(p) => {
ProfileMeta {
game: Game::Ongeki,
name: p.name.as_ref().unwrap().clone()
}
}
let path = self.config_dir().join("profile.json");
let s = serde_json::to_string_pretty(&self.data)?;
if !self.config_dir().exists() {
std::fs::create_dir(self.config_dir())
.map_err(|e| anyhow!("error when creating profile directory: {}", e))?;
}
std::fs::write(&path, s)
.map_err(|e| anyhow!("error when writing to {:?}: {}", path, e))?;
log::info!("Written to {:?}", path);
Ok(())
}
pub fn rename(&mut self, name: String) {
match self {
Self::OngekiProfile(p) => {
p.name = Some(fixed_name(&ProfileMeta { name, game: Game::Ongeki }, false));
}
}
self.meta.name = fixed_name(&ProfileMeta { game: self.meta.game, name}, false);
}
pub fn mod_pkgs(&self) -> &BTreeSet<PkgKey> {
match self {
Self::OngekiProfile(p) => &p.mods
}
&self.data.mods
}
pub fn mod_pkgs_mut(&mut self) -> &mut BTreeSet<PkgKey> {
match self {
Self::OngekiProfile(p) => &mut p.mods
}
&mut self.data.mods
}
pub fn special_pkgs(&self) -> Vec<PkgKey> {
let mut res = Vec::new();
match self {
Self::OngekiProfile(p) => {
if let Some(hook) = &p.sgt.hook {
res.push(hook.clone());
}
if let Some(io) = &p.sgt.io {
res.push(io.clone());
}
}
if let Some(hook) = &self.data.sgt.hook {
res.push(hook.clone());
}
if let Some(io) = &self.data.sgt.io {
res.push(io.clone());
}
if let Aime::AMNet(aime) = &self.data.sgt.aime {
res.push(aime.clone());
} else if let Aime::Other(aime) = &self.data.sgt.aime {
res.push(aime.clone());
}
res
}
pub fn fix(&mut self, store: &PackageStore) {
match self {
Self::OngekiProfile(p) => p.sgt.fix(store)
}
self.data.sgt.fix(store);
}
pub fn sync(&mut self, source: AnyProfile) {
match self {
Self::OngekiProfile(p) => {
#[allow(irrefutable_let_patterns)]
if let AnyProfile::OngekiProfile(source) = source {
p.bepinex = source.bepinex;
p.display = source.display;
p.network = source.network;
p.sgt = source.sgt;
} else {
log::error!("sync: invalid profile type {:?}", source);
}
}
pub fn sync(&mut self, source: ProfileData) {
if self.data.bepinex.is_some() {
self.data.bepinex = source.bepinex;
}
if self.data.display.is_some() {
self.data.display = source.display;
}
// if self.data.network.is_some() {
self.data.network = source.network;
// }
// if self.data.sgt.is_some() {
self.data.sgt = source.sgt;
// }
}
pub async fn line_up(&self, pkg_hash: String, _app: AppHandle) -> Result<()> {
match self {
Self::OngekiProfile(_p) => {
#[cfg(target_os = "windows")]
let info = _p.display.line_up()?;
let info = match &self.data.display {
None => None,
Some(display) => display.line_up()?
};
let res = self.line_up_the_rest(pkg_hash).await;
let res = self.line_up_the_rest(pkg_hash).await;
#[cfg(target_os = "windows")]
if let Some(info) = info {
use crate::model::profile::Display;
if res.is_ok() {
Display::wait_for_exit(_app, info);
} else {
Display::clean_up(&info)?;
}
}
res
#[cfg(target_os = "windows")]
if let Some(info) = info {
use crate::model::profile::Display;
if res.is_ok() {
Display::wait_for_exit(_app, info);
} else {
Display::clean_up(&info)?;
}
}
res
}
async fn line_up_the_rest(&self, pkg_hash: String) -> Result<()> {
match self {
Self::OngekiProfile(p) => {
if !p.data_dir().exists() {
tokio::fs::create_dir(p.data_dir()).await?;
}
let hash_path = p.data_dir().join(".sl-state");
let meta = self.meta();
util::clean_up_opts(p.data_dir().join("option"))?;
if Self::hash_check(&hash_path, &pkg_hash).await? == true {
prepare_packages(&meta, &p.mods).await
.map_err(|e| anyhow!("package configuration failed:\n{:?}", e))?;
}
let mut ini = p.sgt.line_up(&meta).await
.map_err(|e| anyhow!("segatools configuration failed:\n{:?}", e))?;
p.network.line_up(&mut ini)?;
ini.write_to_file(p.data_dir().join("segatools.ini"))
.map_err(|e| anyhow!("Error writing segatools.ini: {}", e))?;
p.bepinex.line_up(&meta)?;
Ok(())
}
if !self.data_dir().exists() {
tokio::fs::create_dir(self.data_dir()).await?;
}
let hash_path = self.data_dir().join(".sl-state");
util::clean_up_opts(self.data_dir().join("option"))?;
let hash_check = Self::hash_check(&hash_path, &pkg_hash).await?;
prepare_packages(&self.meta, &self.data.mods, hash_check).await
.map_err(|e| anyhow!("package configuration failed:\n{:?}", e))?;
let mut ini = self.data.sgt.line_up(&self.meta, self.meta.game).await
.map_err(|e| anyhow!("segatools configuration failed:\n{:?}", e))?;
self.data.network.line_up(&mut ini)?;
ini.write_to_file(self.data_dir().join("segatools.ini"))
.map_err(|e| anyhow!("Error writing segatools.ini: {}", e))?;
if let Some(bepinex) = &self.data.bepinex {
bepinex.line_up(&self.meta)?;
}
Ok(())
}
pub async fn start(&self, app: AppHandle) -> Result<()> {
match self {
Self::OngekiProfile(p) => p.start(app).await
let ini_path = self.data_dir().join("segatools.ini");
log::debug!("With path {:?}", ini_path);
let mut game_builder;
let mut amd_builder;
let target_path = PathBuf::from(&self.data.sgt.target);
let exe_dir = target_path.parent().ok_or_else(|| anyhow!("Invalid target path"))?;
let sgt_dir = self.data.sgt.hook_dir()?;
#[cfg(target_os = "windows")]
{
game_builder = Command::new(sgt_dir.join(self.meta.game.inject_exe()));
amd_builder = Command::new("cmd.exe");
}
#[cfg(target_os = "linux")]
{
game_builder = Command::new(&self.wine.runtime);
amd_builder = Command::new(&self.wine.runtime);
game_builder.arg(sgt_dir.join(self.meta.game.inject_exe()));
amd_builder.arg("cmd.exe");
}
amd_builder.env(
"SEGATOOLS_CONFIG_PATH",
&ini_path,
)
.current_dir(&exe_dir)
.arg("/C")
.arg(&sgt_dir.join(self.meta.game.inject_amd()))
.args(["-d", "-k"])
.arg(sgt_dir.join(self.meta.game.hook_amd()))
.arg("amdaemon.exe")
.args(self.meta.game.amd_args());
game_builder
.env(
"SEGATOOLS_CONFIG_PATH",
ini_path,
)
.env(
"INOHARA_CONFIG_PATH",
self.config_dir().join("inohara.cfg"),
)
.current_dir(&exe_dir)
.args(["-d", "-k"])
.arg(sgt_dir.join(self.meta.game.hook_exe()))
.arg(self.meta.game.exe());
if let Some(display) = &self.data.display {
game_builder.args([
"-monitor 1",
"-screen-width", &display.rez.0.to_string(),
"-screen-height", &display.rez.1.to_string(),
"-screen-fullscreen", if display.mode == DisplayMode::Fullscreen { "1" } else { "0" }
]);
if display.mode == DisplayMode::Borderless {
game_builder.arg("-popupwindow");
}
}
#[cfg(target_os = "linux")]
{
amd_builder.env("WINEPREFIX", &self.wine.prefix);
game_builder.env("WINEPREFIX", &self.wine.prefix);
}
let amd_log = File::create(self.data_dir().join("amdaemon.exe.log"))?;
let game_log = File::create(self.data_dir().join(format!("{}.log", self.meta.game.exe())))?;
amd_builder
.stdout(Stdio::from(amd_log));
// do they use stderr?
game_builder
.stdout(Stdio::from(game_log));
#[cfg(target_os = "windows")]
{
amd_builder.creation_flags(util::CREATE_NO_WINDOW);
game_builder.creation_flags(util::CREATE_NO_WINDOW);
}
if self.data.sgt.intel == true {
amd_builder.env("OPENSSL_ia32cap", ":~0x20000000");
}
util::pkill("amdaemon.exe").await;
log::info!("Launching amdaemon: {:?}", amd_builder);
log::info!("Launching {}: {:?}", self.meta.game, game_builder);
let mut amd = amd_builder.spawn()?;
let mut game = game_builder.spawn()?;
let mut set = JoinSet::new();
set.spawn(async move {
(amd.wait().await.expect("amdaemon failed to run"), "amdaemon")
});
set.spawn(async move {
(game.wait().await.expect("game failed to run"), "game")
});
if let Err(e) = app.emit("launch-start", "") {
log::warn!("Unable to emit launch-start: {}", e);
}
let (rc, process) = set.join_next().await.expect("No spawn").expect("No result");
log::info!("{} died with return code {}", process, rc);
if process == "amdaemon" {
util::pkill(self.meta.game.exe()).await;
} else {
util::pkill("amdaemon.exe").await;
}
set.join_next().await.expect("No spawn").expect("No result");
log::debug!("Fin");
if let Err(e) = app.emit("launch-end", "") {
log::warn!("Unable to emit launch-end: {}", e);
}
Ok(())
}
async fn hash_check(prev_hash_path: &impl AsRef<Path>, new_hash: &str) -> Result<bool> {
@ -174,11 +342,19 @@ impl AnyProfile {
}
}
impl std::fmt::Debug for AnyProfile {
impl ProfilePaths for Profile {
fn config_dir(&self) -> PathBuf {
self.meta.config_dir()
}
fn data_dir(&self) -> PathBuf {
self.meta.data_dir()
}
}
impl std::fmt::Debug for Profile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::OngekiProfile(p) => f.debug_tuple("ongeki").field(&p.name).finish(),
}
f.debug_tuple(&self.meta.game.to_string()).field(&self.meta.name).finish()
}
}
@ -227,7 +403,7 @@ pub fn fixed_name(meta: &ProfileMeta, prepend_new: bool) -> String {
.replace(" ", "-")
.replace("..", "").replace("/", "").replace("\\", "");
while prepend_new && util::profile_config_dir(&meta.game, &name).exists() {
while prepend_new && util::profile_config_dir(meta.game, &name).exists() {
name = format!("new-{}", name);
}