mirror of
https://github.com/Xevion/rust-sdl2-emscripten.git
synced 2025-12-11 04:08:28 -06:00
Add native volume data saving support
This commit is contained in:
@@ -105,7 +105,7 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let storage = store::Store {};
|
let mut storage = store::Store::new();
|
||||||
|
|
||||||
let mut volume = storage.volume().map_or_else(
|
let mut volume = storage.volume().map_or_else(
|
||||||
|| {
|
|| {
|
||||||
|
|||||||
73
src/store.rs
73
src/store.rs
@@ -1,13 +1,48 @@
|
|||||||
|
use std::{io::Seek, path::PathBuf};
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "emscripten"))]
|
||||||
|
pub struct Store {
|
||||||
|
file: std::fs::File,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "emscripten")]
|
||||||
pub struct Store;
|
pub struct Store;
|
||||||
|
|
||||||
#[cfg(not(target_os = "emscripten"))]
|
#[cfg(not(target_os = "emscripten"))]
|
||||||
impl Store {
|
impl Store {
|
||||||
pub fn volume(&self) -> Option<u32> {
|
pub fn new () -> Self {
|
||||||
None
|
let path = Store::get_path();
|
||||||
|
let file = std::fs::OpenOptions::new()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.create(true)
|
||||||
|
.open(&path)
|
||||||
|
.unwrap();
|
||||||
|
Store { file }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_volume(&self, volume: u32) {
|
fn get_path() -> PathBuf {
|
||||||
// Do nothing
|
use std::env;
|
||||||
|
let mut path = env::current_exe().unwrap();
|
||||||
|
path.pop();
|
||||||
|
path.push("volume.txt");
|
||||||
|
path
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn volume(&mut self) -> Option<u32> {
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
let mut buffer = String::new();
|
||||||
|
self.file.read_to_string(&mut buffer).unwrap();
|
||||||
|
buffer.trim().parse::<u32>().ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_volume(&mut self, volume: u32) {
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
self.file.set_len(0).unwrap();
|
||||||
|
self.file.seek(std::io::SeekFrom::Start(0)).unwrap();
|
||||||
|
write!(self.file, "{}", volume).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,6 +54,23 @@ extern "C" {
|
|||||||
|
|
||||||
#[cfg(target_os = "emscripten")]
|
#[cfg(target_os = "emscripten")]
|
||||||
impl Store {
|
impl Store {
|
||||||
|
pub fn new () -> Self {
|
||||||
|
Store
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn volume(&self) -> Option<u32> {
|
||||||
|
// Use local storage to try and read volume
|
||||||
|
let script = "localStorage.getItem('volume')";
|
||||||
|
let response = Store::run_script_string(&script);
|
||||||
|
response.parse::<u32>().ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_volume(&self, volume: u32) {
|
||||||
|
// Use local storage to set volume
|
||||||
|
let script = format!("localStorage.setItem('volume', '{}')", volume);
|
||||||
|
Store::run_script_string(&script);
|
||||||
|
}
|
||||||
|
|
||||||
fn run_script(script: &str) {
|
fn run_script(script: &str) {
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
|
||||||
@@ -38,17 +90,4 @@ impl Store {
|
|||||||
String::from(c_str.to_str().unwrap())
|
String::from(c_str.to_str().unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn volume(&self) -> Option<u32> {
|
|
||||||
// Use local storage to try and read volume
|
|
||||||
let script = "localStorage.getItem('volume')";
|
|
||||||
let response = Store::run_script_string(&script);
|
|
||||||
response.parse::<u32>().ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_volume(&self, volume: u32) {
|
|
||||||
// Use local storage to set volume
|
|
||||||
let script = format!("localStorage.setItem('volume', '{}')", volume);
|
|
||||||
Store::run_script_string(&script);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user