Updated crate name for minidisc-rs

This commit is contained in:
G2-Games 2023-09-25 13:20:47 -05:00
parent 01e1c46d32
commit 4a7f95d081
7 changed files with 63 additions and 62 deletions

View file

@ -11,6 +11,6 @@ license = "AGPL-3.0"
[dependencies] [dependencies]
rusb = "0.9.3" rusb = "0.9.3"
[dependencies.minidisc] [dependencies.minidisc-rs]
path = "minidisc-rs" path = "minidisc-rs"
version = "0.0.1" version = "0.0.1"

View file

@ -1,5 +1,5 @@
[package] [package]
name = "minidisc" name = "minidisc-rs"
version = "0.0.1" version = "0.0.1"
homepage = "https://github.com/G2-Games/minidisc-cli/" homepage = "https://github.com/G2-Games/minidisc-cli/"
repository = "https://github.com/G2-Games/minidisc-cli/minidisc-rs/" repository = "https://github.com/G2-Games/minidisc-cli/minidisc-rs/"

View file

@ -5,4 +5,4 @@
/// This is a test /// This is a test
pub mod netmd; pub mod netmd;
use netmd::interface; use netmd::interface::NetMDInterface;

View file

@ -127,18 +127,18 @@ impl NetMD {
} }
/// Gets the device name from the struct /// Gets the device name from the struct
pub fn device_name(&self) -> Option<String> { pub fn device_name(&self) -> &Option<String> {
self.model.name.clone() &self.model.name
} }
/// Gets the vendor id from the struct /// Gets the vendor id from the struct
pub fn vendor_id(&self) -> u16 { pub fn vendor_id(&self) -> &u16 {
self.model.vendor_id.clone() &self.model.vendor_id
} }
/// Gets the product id from the struct /// Gets the product id from the struct
pub fn product_id(&self) -> u16 { pub fn product_id(&self) -> &u16 {
self.model.product_id.clone() &self.model.product_id
} }
/// Poll the device to get either the result /// Poll the device to get either the result

View file

@ -1,5 +1,5 @@
use crate::netmd::query_utils::{format_query, scan_query}; 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 crate::netmd::base;
use encoding_rs::*; use encoding_rs::*;
use std::collections::HashMap; use std::collections::HashMap;
@ -189,7 +189,7 @@ impl NetMDInterface {
fn construct_multibyte(&self, buffer: &Vec<u8>, n: u8, offset: &mut usize) -> u32 { fn construct_multibyte(&self, buffer: &Vec<u8>, n: u8, offset: &mut usize) -> u32 {
let mut output: u32 = 0; let mut output: u32 = 0;
for i in 0..n as usize { for _ in 0..n as usize {
output <<= 8; output <<= 8;
output |= buffer[*offset] as u32; output |= buffer[*offset] as u32;
*offset += 1; *offset += 1;
@ -736,12 +736,12 @@ impl NetMDInterface {
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead);
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead);
let mut done: u16 = 0; let mut done: i32 = 0;
let mut remaining: u16 = 0; let mut remaining: i32 = 0;
let mut total = 1; let mut total = 1;
let mut result: Vec<String> = Vec::new(); let mut result: Vec<String> = Vec::new();
let mut chunksize = 0; let mut chunksize;
let mut chunk = String::new(); let mut chunk;
while done < total { while done < total {
let wchar_value = match wchar { let wchar_value = match wchar {
@ -763,15 +763,17 @@ impl NetMDInterface {
let reply = self.send_query(&mut query, false, false)?; let reply = self.send_query(&mut query, false, false)?;
if remaining == 0 { if remaining == 0 {
let res = chunksize = u16::from_le_bytes([reply[13], reply[14]]); let res = scan_query(reply, "1806 02201801 00%? 3000 0a00 1000 %w0000 %?%?000a %w %*".to_string())?;
total = u16::from_le_bytes([reply[22], reply[23]]);
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; chunksize -= 6;
} else { } else {
chunksize = u16::from_le_bytes([reply[13], reply[14]]); let res = scan_query(reply, "1806 02201801 00%? 3000 0a00 1000 %w%?%? %*".to_string())?;
chunk = SHIFT_JIS.decode(&reply[18..]).0.into(); chunksize = res[0].to_i64().unwrap() as i32;
chunk = SHIFT_JIS.decode(&res[1].to_vec().unwrap()).0.into();
} }
result.push(chunk); result.push(chunk);
@ -779,12 +781,12 @@ impl NetMDInterface {
remaining = total - done; 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::DiscTitleTD, &DescriptorAction::Close);
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close);
Ok(final_result) Ok(res)
} }
pub fn disc_title(&self, wchar: bool) -> Result<String, Box<dyn Error>> { pub fn disc_title(&self, wchar: bool) -> Result<String, Box<dyn Error>> {
@ -844,7 +846,7 @@ impl NetMDInterface {
let group_name = &group[track_range.len() + 1..]; 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 let full_width_group_name = full_width_group_list
.find(|n| n.starts_with(&full_width_range)) .find(|n| n.starts_with(&full_width_range))
@ -936,4 +938,25 @@ impl NetMDInterface {
.0 .0
.into()) .into())
} }
pub fn set_disc_title(&self, title: String, wchar: bool) -> Result<(), Box<dyn Error>> {
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(())
}
} }

View file

@ -1,13 +1,14 @@
use std::collections::hash_map::HashMap; use std::collections::hash_map::HashMap;
use encoding_rs::SHIFT_JIS;
use std::error::Error; use std::error::Error;
pub fn bcd_to_int(bcd: i32) -> i32 { pub fn bcd_to_int(mut bcd: i32) -> i32 {
let mut original = bcd;
let mut value = 0; let mut value = 0;
let mut nibble = 0; let mut nibble = 0;
while original != 0 {
let nibble_value = original & 0xf; while bcd != 0 {
original = original >> 4; let nibble_value = bcd & 0xf;
bcd = bcd >> 4;
value += nibble_value * i32::pow(10, nibble); value += nibble_value * i32::pow(10, nibble);
nibble += 1; nibble += 1;
} }
@ -29,33 +30,6 @@ pub fn int_to_bcd(mut value: i32) -> i32 {
bcd bcd
} }
pub fn int_from_bcd(byte: u8) -> Result<u8, Box<dyn Error>> {
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<u8, Box<dyn Error>> {
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 { pub fn half_width_to_full_width_range(range: &String) -> String {
let mappings: HashMap<char, char> = HashMap::from([ let mappings: HashMap<char, char> = HashMap::from([
('0', ''), ('0', ''),
@ -93,3 +67,9 @@ pub fn get_bytes<const S: usize>(
Ok(bytes) Ok(bytes)
} }
pub fn length_after_encoding_to_jis(string: String) -> usize {
let new_string = SHIFT_JIS.encode(&string);
new_string.0.len()
}

View file

@ -1,4 +1,4 @@
use minidisc::netmd; use minidisc_rs::netmd::interface;
use rusb; use rusb;
fn main() { fn main() {
@ -19,19 +19,17 @@ fn main() {
new_device.read_product_string_ascii(&device_desc) 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, Ok(player) => player,
Err(_) => continue Err(_) => continue
}; };
println!( println!(
"Player Model: {}", "Player Model: {}",
player_controller.net_md_device.device_name().unwrap() player_controller.net_md_device.device_name().clone().unwrap()
); );
println!("Track Count: {:?}", player_controller.track_count().unwrap());
println!("Disc Flags? {:?}", player_controller.disc_flags()); println!("Disc Title: {} | {}", player_controller.disc_title(false).unwrap(), player_controller.disc_title(true).unwrap());
println!("Track Count: {:?}", player_controller.track_count());
println!("Disc Title: {:?}", player_controller.disc_title(false));
//println!("TEST CASE: {:?}", player_controller.disc_subunit_identifier()); //println!("TEST CASE: {:?}", player_controller.disc_subunit_identifier());