From 4a7f95d081571a755fa56a1532f6cce27f21f1c5 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Mon, 25 Sep 2023 13:20:47 -0500 Subject: [PATCH] Updated crate name for `minidisc-rs` --- Cargo.toml | 2 +- minidisc-rs/Cargo.toml | 2 +- minidisc-rs/src/lib.rs | 2 +- minidisc-rs/src/netmd/base.rs | 12 +++---- minidisc-rs/src/netmd/interface.rs | 51 ++++++++++++++++++++++-------- minidisc-rs/src/netmd/utils.rs | 44 +++++++------------------- src/main.rs | 12 +++---- 7 files changed, 63 insertions(+), 62 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f2eaaa..e77504f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,6 @@ license = "AGPL-3.0" [dependencies] rusb = "0.9.3" -[dependencies.minidisc] +[dependencies.minidisc-rs] path = "minidisc-rs" version = "0.0.1" diff --git a/minidisc-rs/Cargo.toml b/minidisc-rs/Cargo.toml index 74a99cf..6272fa4 100644 --- a/minidisc-rs/Cargo.toml +++ b/minidisc-rs/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "minidisc" +name = "minidisc-rs" version = "0.0.1" homepage = "https://github.com/G2-Games/minidisc-cli/" repository = "https://github.com/G2-Games/minidisc-cli/minidisc-rs/" diff --git a/minidisc-rs/src/lib.rs b/minidisc-rs/src/lib.rs index 022b5c7..2a4ca9f 100644 --- a/minidisc-rs/src/lib.rs +++ b/minidisc-rs/src/lib.rs @@ -5,4 +5,4 @@ /// This is a test pub mod netmd; -use netmd::interface; +use netmd::interface::NetMDInterface; diff --git a/minidisc-rs/src/netmd/base.rs b/minidisc-rs/src/netmd/base.rs index 521df21..399b27b 100644 --- a/minidisc-rs/src/netmd/base.rs +++ b/minidisc-rs/src/netmd/base.rs @@ -127,18 +127,18 @@ impl NetMD { } /// Gets the device name from the struct - pub fn device_name(&self) -> Option { - self.model.name.clone() + pub fn device_name(&self) -> &Option { + &self.model.name } /// Gets the vendor id from the struct - pub fn vendor_id(&self) -> u16 { - self.model.vendor_id.clone() + pub fn vendor_id(&self) -> &u16 { + &self.model.vendor_id } /// Gets the product id from the struct - pub fn product_id(&self) -> u16 { - self.model.product_id.clone() + pub fn product_id(&self) -> &u16 { + &self.model.product_id } /// Poll the device to get either the result diff --git a/minidisc-rs/src/netmd/interface.rs b/minidisc-rs/src/netmd/interface.rs index dc3b3d5..2a42934 100644 --- a/minidisc-rs/src/netmd/interface.rs +++ b/minidisc-rs/src/netmd/interface.rs @@ -1,5 +1,5 @@ use crate::netmd::query_utils::{format_query, scan_query}; -use crate::netmd::utils; +use crate::netmd::utils::{length_after_encoding_to_jis, half_width_to_full_width_range}; use crate::netmd::base; use encoding_rs::*; use std::collections::HashMap; @@ -189,7 +189,7 @@ impl NetMDInterface { fn construct_multibyte(&self, buffer: &Vec, n: u8, offset: &mut usize) -> u32 { let mut output: u32 = 0; - for i in 0..n as usize { + for _ in 0..n as usize { output <<= 8; output |= buffer[*offset] as u32; *offset += 1; @@ -736,12 +736,12 @@ impl NetMDInterface { self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead); - let mut done: u16 = 0; - let mut remaining: u16 = 0; + let mut done: i32 = 0; + let mut remaining: i32 = 0; let mut total = 1; let mut result: Vec = Vec::new(); - let mut chunksize = 0; - let mut chunk = String::new(); + let mut chunksize; + let mut chunk; while done < total { let wchar_value = match wchar { @@ -763,15 +763,17 @@ impl NetMDInterface { let reply = self.send_query(&mut query, false, false)?; if remaining == 0 { - let res = chunksize = u16::from_le_bytes([reply[13], reply[14]]); - total = u16::from_le_bytes([reply[22], reply[23]]); + let res = scan_query(reply, "1806 02201801 00%? 3000 0a00 1000 %w0000 %?%?000a %w %*".to_string())?; - chunk = SHIFT_JIS.decode(&reply[25..]).0.into(); + chunksize = res[0].to_i64().unwrap() as i32; + total = res[1].to_i64().unwrap() as i32; + chunk = SHIFT_JIS.decode(&res[2].to_vec().unwrap()).0.into(); chunksize -= 6; } else { - chunksize = u16::from_le_bytes([reply[13], reply[14]]); - chunk = SHIFT_JIS.decode(&reply[18..]).0.into(); + let res = scan_query(reply, "1806 02201801 00%? 3000 0a00 1000 %w%?%? %*".to_string())?; + chunksize = res[0].to_i64().unwrap() as i32; + chunk = SHIFT_JIS.decode(&res[1].to_vec().unwrap()).0.into(); } result.push(chunk); @@ -779,12 +781,12 @@ impl NetMDInterface { remaining = total - done; } - let final_result = result.join(""); + let res = result.join(""); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close); - Ok(final_result) + Ok(res) } pub fn disc_title(&self, wchar: bool) -> Result> { @@ -844,7 +846,7 @@ impl NetMDInterface { let group_name = &group[track_range.len() + 1..]; - let full_width_range = utils::half_width_to_full_width_range(&track_range); + let full_width_range = half_width_to_full_width_range(&track_range); let full_width_group_name = full_width_group_list .find(|n| n.starts_with(&full_width_range)) @@ -936,4 +938,25 @@ impl NetMDInterface { .0 .into()) } + + pub fn set_disc_title(&self, title: String, wchar: bool) -> Result<(), Box> { + let current_title = self._disc_title(wchar)?; + if current_title == title { + return Ok(()) + } + + let old_len = length_after_encoding_to_jis(current_title); + let new_len = length_after_encoding_to_jis(title); + + let wchar_value = match wchar { + true => { + + }, + false => { + + } + }; + + Ok(()) + } } diff --git a/minidisc-rs/src/netmd/utils.rs b/minidisc-rs/src/netmd/utils.rs index f8b4c3c..f087a33 100644 --- a/minidisc-rs/src/netmd/utils.rs +++ b/minidisc-rs/src/netmd/utils.rs @@ -1,13 +1,14 @@ use std::collections::hash_map::HashMap; +use encoding_rs::SHIFT_JIS; use std::error::Error; -pub fn bcd_to_int(bcd: i32) -> i32 { - let mut original = bcd; +pub fn bcd_to_int(mut bcd: i32) -> i32 { let mut value = 0; let mut nibble = 0; - while original != 0 { - let nibble_value = original & 0xf; - original = original >> 4; + + while bcd != 0 { + let nibble_value = bcd & 0xf; + bcd = bcd >> 4; value += nibble_value * i32::pow(10, nibble); nibble += 1; } @@ -29,33 +30,6 @@ pub fn int_to_bcd(mut value: i32) -> i32 { bcd } -pub fn int_from_bcd(byte: u8) -> Result> { - let upper = (byte & 0xF0) >> 4; - let lower = byte & 0x0F; - - if upper >= 10 { - return Err("Upper nybble out of range [0..9]".into()); - } - - if lower >= 10 { - return Err("Lower nybble out of range [0..9]".into()); - } - - Ok(upper * 10 + lower) -} - -pub fn bcd_from_int(byte: u8) -> Result> { - let mut new_byte: u8 = 0; - - let upper = (byte / 10) << 4; - let lower = byte % 10; - - new_byte |= upper; - new_byte |= lower; - - Ok(new_byte) -} - pub fn half_width_to_full_width_range(range: &String) -> String { let mappings: HashMap = HashMap::from([ ('0', '0'), @@ -93,3 +67,9 @@ pub fn get_bytes( Ok(bytes) } + +pub fn length_after_encoding_to_jis(string: String) -> usize { + let new_string = SHIFT_JIS.encode(&string); + + new_string.0.len() +} diff --git a/src/main.rs b/src/main.rs index ed58997..3f44712 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use minidisc::netmd; +use minidisc_rs::netmd::interface; use rusb; fn main() { @@ -19,19 +19,17 @@ fn main() { new_device.read_product_string_ascii(&device_desc) ); - let player_controller = match netmd::interface::NetMDInterface::new(new_device, device_desc) { + let player_controller = match interface::NetMDInterface::new(new_device, device_desc) { Ok(player) => player, Err(_) => continue }; println!( "Player Model: {}", - player_controller.net_md_device.device_name().unwrap() + player_controller.net_md_device.device_name().clone().unwrap() ); - - println!("Disc Flags? {:?}", player_controller.disc_flags()); - println!("Track Count: {:?}", player_controller.track_count()); - println!("Disc Title: {:?}", player_controller.disc_title(false)); + println!("Track Count: {:?}", player_controller.track_count().unwrap()); + println!("Disc Title: {} | {}", player_controller.disc_title(false).unwrap(), player_controller.disc_title(true).unwrap()); //println!("TEST CASE: {:?}", player_controller.disc_subunit_identifier());