Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TAS studio split and join sections of the script #110

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
split markers assumed to be ready if save exists
  • Loading branch information
Eddio0141 committed Nov 27, 2024
commit eb1121d9691f1cd31c0f299283da0a0ce1596f57
52 changes: 47 additions & 5 deletions src/modules/tas_studio/editor/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;
use std::ffi::CStr;
use std::path::Path;
use std::{collections::HashSet, fmt};

use bincode::Options;
use color_eyre::eyre::{self, ensure, eyre};
Expand All @@ -8,6 +9,9 @@ use itertools::{Itertools, MultiPeek};
use rusqlite::{params, Connection, OpenFlags, OptionalExtension};
use serde::{Deserialize, Serialize};

use crate::hooks::engine;
use crate::utils::MainThreadMarker;

use super::operation::Operation;

#[derive(Debug)]
Expand Down Expand Up @@ -46,6 +50,7 @@ pub struct SplitInfo {
pub bulk_idx: usize,
// a split could have no name
// this could also be duplicate names, which becomes `None`
// TODO: could probably get away with &str
pub name: Option<String>,
pub split_type: SplitType,
// ready as in, there's a save created, and lines before and including start_idx is still unchanged
Expand Down Expand Up @@ -93,6 +98,8 @@ impl SplitInfo {
}
}

let mut used_save_names = HashSet::new();

while let Some(line) = lines.next() {
// this is correct, if FrameBulk is at index 0, we are searching from index 1
line_idx += 1;
Expand All @@ -116,7 +123,7 @@ impl SplitInfo {
}
line_idx += 1;

name = Some(save_name.to_owned());
name = Some(save_name.as_str());
start_idx = line_idx;
split_type = SplitType::Save;
}
Expand Down Expand Up @@ -167,7 +174,7 @@ impl SplitInfo {
if comment.is_empty() {
name = None;
} else {
name = Some(comment.to_owned());
name = Some(comment);
}
}
Line::FrameBulk(_) => {
Expand All @@ -177,6 +184,17 @@ impl SplitInfo {
_ => continue,
}

let name = if let Some(name) = name {
if used_save_names.contains(name) {
None
} else {
used_save_names.insert(name.to_owned());
Some(name.to_owned())
}
} else {
None
};

splits.push(SplitInfo {
start_idx,
name,
Expand All @@ -197,6 +215,22 @@ impl SplitInfo {
}
true
}

pub fn validate_all_by_saves(splits: &mut Vec<SplitInfo>, marker: MainThreadMarker) {
for split in splits {
let Some(name) = &split.name else {
return;
};

let game_dir = Path::new(
unsafe { CStr::from_ptr(engine::com_gamedir.get(marker).cast()) }
.to_str()
.unwrap(),
);
let save_path = game_dir.join("SAVE").join(name);
split.ready = save_path.is_file();
}
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -354,7 +388,11 @@ impl Db {
let script = HLTAS::from_str(&buffer)
.map_err(|err| eyre!("invalid script value, cannot parse: {err:?}"))?;

let splits = SplitInfo::split_lines(script.lines.iter());
let mut splits = SplitInfo::split_lines(script.lines.iter());
// TODO: is this fine? not sure how else to get MainThreadMarker for game directory
unsafe {
SplitInfo::validate_all_by_saves(&mut splits, MainThreadMarker::new());
}
Comment on lines +485 to +488
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fine?


Ok(Branch {
branch_id,
Expand Down Expand Up @@ -387,7 +425,11 @@ impl Db {
let script = HLTAS::from_str(&buffer)
.map_err(|err| eyre!("invalid script value, cannot parse: {err:?}"))?;

let splits = SplitInfo::split_lines(script.lines.iter());
let mut splits = SplitInfo::split_lines(script.lines.iter());
// TODO: is this fine? not sure how else to get MainThreadMarker for game directory
unsafe {
SplitInfo::validate_all_by_saves(&mut splits, MainThreadMarker::new());
}

branches.push(Branch {
branch_id,
Expand Down
1 change: 1 addition & 0 deletions src/modules/tas_studio/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3533,6 +3533,7 @@ impl Editor {
return Ok(());
}

// don't validate by checking save file, its a rewrite
branch.splits = SplitInfo::split_lines(new_script.lines.iter());

let mut buffer = Vec::new();
Expand Down