mirror of
https://github.com/G2-Games/minidisc-cli.git
synced 2025-04-20 04:02:53 -05:00
Created function for wasm/native sleep, used clippy suggestions
This commit is contained in:
parent
630a8c95eb
commit
0d9646c5a0
4 changed files with 27 additions and 49 deletions
|
@ -4,16 +4,12 @@ use once_cell::sync::Lazy;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
|
||||||
use gloo::{
|
|
||||||
timers::future::TimeoutFuture,
|
|
||||||
console::log,
|
|
||||||
};
|
|
||||||
|
|
||||||
// USB stuff
|
// USB stuff
|
||||||
//use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer};
|
//use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer};
|
||||||
use cross_usb::{UsbDevice, UsbInterface};
|
use cross_usb::{UsbDevice, UsbInterface};
|
||||||
use cross_usb::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
|
use cross_usb::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
|
||||||
|
|
||||||
|
use super::utils::cross_sleep;
|
||||||
//use nusb::{Device, DeviceInfo, Interface};
|
//use nusb::{Device, DeviceInfo, Interface};
|
||||||
|
|
||||||
const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0);
|
const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0);
|
||||||
|
@ -257,14 +253,10 @@ impl NetMD {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Back off while trying again
|
// Back off while trying again
|
||||||
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64
|
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL
|
||||||
* (u64::pow(2, attempt as u32 / 10) - 1);
|
* (u32::pow(2, attempt / 10) - 1);
|
||||||
|
|
||||||
#[cfg(not(target_family = "wasm"))]
|
cross_sleep(sleep_time).await;
|
||||||
std::thread::sleep(std::time::Duration::from_millis(sleep_time));
|
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
|
||||||
TimeoutFuture::new(sleep_time as u32).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(value) = override_length {
|
if let Some(value) = override_length {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
#![cfg_attr(debug_assertions, allow(dead_code))]
|
#![cfg_attr(debug_assertions, allow(dead_code))]
|
||||||
use std::{error::Error, thread::sleep, time::Duration};
|
use std::error::Error;
|
||||||
use num_derive::FromPrimitive;
|
use num_derive::FromPrimitive;
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
|
|
||||||
use super::interface::{NetMDInterface, MDTrack};
|
use super::interface::{NetMDInterface, MDTrack};
|
||||||
|
use super::utils::cross_sleep;
|
||||||
|
|
||||||
#[derive(FromPrimitive)]
|
#[derive(FromPrimitive)]
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
|
@ -59,7 +60,7 @@ pub async fn device_status(interface: &mut NetMDInterface) -> Result<DeviceStatu
|
||||||
|
|
||||||
pub async fn prepare_download(interface: &mut NetMDInterface) -> Result<(), Box<dyn Error>>{
|
pub async fn prepare_download(interface: &mut NetMDInterface) -> Result<(), Box<dyn Error>>{
|
||||||
while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(&device_status(interface).await?.state.unwrap_or(OperatingStatus::NoDisc)) {
|
while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(&device_status(interface).await?.state.unwrap_or(OperatingStatus::NoDisc)) {
|
||||||
sleep(Duration::from_millis(200));
|
cross_sleep(200).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = interface.session_key_forget().await;
|
let _ = interface.session_key_forget().await;
|
||||||
|
|
|
@ -14,18 +14,7 @@ use std::error::Error;
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
use super::utils::cross_sleep;
|
||||||
use gloo::{
|
|
||||||
timers::future::TimeoutFuture,
|
|
||||||
console::log,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Blocking stuff - can't use on WASM
|
|
||||||
#[cfg(not(arget_family = "wasm"))]
|
|
||||||
use std::{
|
|
||||||
thread::sleep,
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
enum Action {
|
enum Action {
|
||||||
|
@ -398,15 +387,10 @@ impl NetMDInterface {
|
||||||
NetmdStatus::NotImplemented => return Err("Not implemented".into()),
|
NetmdStatus::NotImplemented => return Err("Not implemented".into()),
|
||||||
NetmdStatus::Rejected => return Err("Rejected".into()),
|
NetmdStatus::Rejected => return Err("Rejected".into()),
|
||||||
NetmdStatus::Interim if !accept_interim => {
|
NetmdStatus::Interim if !accept_interim => {
|
||||||
let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL as u64
|
let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL
|
||||||
* (u64::pow(2, current_attempt as u32) - 1);
|
* (u32::pow(2, current_attempt as u32) - 1);
|
||||||
|
|
||||||
#[cfg(not(target_family = "wasm"))]
|
|
||||||
sleep(Duration::from_millis(sleep_time));
|
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
|
||||||
TimeoutFuture::new(sleep_time as u32).await;
|
|
||||||
|
|
||||||
|
cross_sleep(sleep_time).await;
|
||||||
|
|
||||||
current_attempt += 1;
|
current_attempt += 1;
|
||||||
continue; // Retry!
|
continue; // Retry!
|
||||||
|
@ -1004,7 +988,7 @@ impl NetMDInterface {
|
||||||
|
|
||||||
let wchar_value = match wchar {
|
let wchar_value = match wchar {
|
||||||
true => {
|
true => {
|
||||||
new_title = sanitize_full_width_title(&title, false);
|
new_title = sanitize_full_width_title(title, false);
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
|
@ -1055,7 +1039,7 @@ impl NetMDInterface {
|
||||||
let new_title: Vec<u8>;
|
let new_title: Vec<u8>;
|
||||||
let (wchar_value, descriptor) = match wchar {
|
let (wchar_value, descriptor) = match wchar {
|
||||||
true => {
|
true => {
|
||||||
new_title = sanitize_full_width_title(&title, false);
|
new_title = sanitize_full_width_title(title, false);
|
||||||
(3, Descriptor::AudioUTOC4TD)
|
(3, Descriptor::AudioUTOC4TD)
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
|
@ -1520,11 +1504,7 @@ impl NetMDInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sharps are slow
|
// Sharps are slow
|
||||||
#[cfg(not(target_family = "wasm"))]
|
cross_sleep(200).await;
|
||||||
sleep(Duration::from_millis(200));
|
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
|
||||||
TimeoutFuture::new(200).await;
|
|
||||||
|
|
||||||
let total_bytes = pkt_size + 24; //framesizedict[wireformat] * frames + pktcount * 24;
|
let total_bytes = pkt_size + 24; //framesizedict[wireformat] * frames + pktcount * 24;
|
||||||
|
|
||||||
|
@ -1544,11 +1524,7 @@ impl NetMDInterface {
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Sharps are slow
|
// Sharps are slow
|
||||||
#[cfg(not(target_family = "wasm"))]
|
cross_sleep(200).await;
|
||||||
sleep(Duration::from_millis(200));
|
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
|
||||||
TimeoutFuture::new(200).await;
|
|
||||||
|
|
||||||
let mut _written_bytes = 0;
|
let mut _written_bytes = 0;
|
||||||
for (packet_count, (key, iv, data)) in packets.into_iter().enumerate() {
|
for (packet_count, (key, iv, data)) in packets.into_iter().enumerate() {
|
||||||
|
|
|
@ -8,6 +8,15 @@ use unicode_normalization::UnicodeNormalization;
|
||||||
extern crate kana;
|
extern crate kana;
|
||||||
use kana::*;
|
use kana::*;
|
||||||
|
|
||||||
|
/// Sleep for a specified number of milliseconds on any platform
|
||||||
|
pub async fn cross_sleep(millis: u32) {
|
||||||
|
#[cfg(not(target_family = "wasm"))]
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(millis as u64));
|
||||||
|
|
||||||
|
#[cfg(target_family = "wasm")]
|
||||||
|
gloo::timers::future::TimeoutFuture::new(millis).await;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn bcd_to_int(mut bcd: i32) -> i32 {
|
pub fn bcd_to_int(mut bcd: i32) -> i32 {
|
||||||
let mut value = 0;
|
let mut value = 0;
|
||||||
let mut nibble = 0;
|
let mut nibble = 0;
|
||||||
|
@ -90,7 +99,7 @@ fn check(string: String) -> Option<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sanitize_half_width_title(title: &str) -> Vec<u8> {
|
pub fn sanitize_half_width_title(title: &str) -> Vec<u8> {
|
||||||
let mut string_title = wide2ascii(&title);
|
let mut string_title = wide2ascii(title);
|
||||||
string_title = nowidespace(&string_title);
|
string_title = nowidespace(&string_title);
|
||||||
string_title = hira2kata(&string_title);
|
string_title = hira2kata(&string_title);
|
||||||
string_title = combine(&string_title);
|
string_title = combine(&string_title);
|
||||||
|
@ -107,7 +116,7 @@ pub fn sanitize_half_width_title(title: &str) -> Vec<u8> {
|
||||||
let sjis_string = SHIFT_JIS.encode(&new_title).0;
|
let sjis_string = SHIFT_JIS.encode(&new_title).0;
|
||||||
|
|
||||||
if validate_shift_jis(sjis_string.clone().into()) {
|
if validate_shift_jis(sjis_string.clone().into()) {
|
||||||
return agressive_sanitize_title(&title).into();
|
return agressive_sanitize_title(title).into();
|
||||||
}
|
}
|
||||||
|
|
||||||
sjis_string.into()
|
sjis_string.into()
|
||||||
|
|
Loading…
Reference in a new issue