Skip to content

Commit

Permalink
Add system_cwd dir, and the rest of the glkunix functions
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousdannii committed Mar 22, 2024
1 parent 758db5a commit 5530b15
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 19 deletions.
4 changes: 3 additions & 1 deletion remglk/src/glkapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod windows;
use std::cmp::min;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use std::str;
use std::time::SystemTime;

Expand Down Expand Up @@ -1034,7 +1035,7 @@ where S: Default + GlkSystem {
// Extensions

pub fn glkunix_fileref_create_by_name_uncleaned(&mut self, usage: u32, filename: String, rock: u32) -> GlkFileRef {
let path = self.dirs.storyfile.join(filename).to_str().unwrap().to_owned();
let path = self.dirs.system_cwd.join(filename).to_str().unwrap().to_owned();
self.create_fileref(path, rock, usage)
}

Expand Down Expand Up @@ -1619,6 +1620,7 @@ where S: Default + GlkSystem {
#[derive(Default)]
pub struct Directories {
pub storyfile: PathBuf,
pub system_cwd: PathBuf,
pub temp: PathBuf,
pub working: PathBuf,
}
Expand Down
4 changes: 1 addition & 3 deletions remglk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ https://github.com/curiousdannii/remglk-rs
pub mod blorb;
pub mod glkapi;

use std::path::PathBuf;

use glkapi::Directories;
use glkapi::protocol::{Event, Update};

Expand All @@ -32,5 +30,5 @@ pub trait GlkSystem {
fn get_glkote_event(&mut self) -> Option<Event>;

fn get_directories() -> Directories;
fn set_base_file(folders: &mut Directories, path: String);
fn set_base_file(dirs: &mut Directories, path: String);
}
3 changes: 3 additions & 0 deletions remglk_capi/src/glk/glkstart.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ extern strid_t glkunix_stream_open_pathname(char *pathname, glui32 textmode,
extern char *glkunix_fileref_get_filename(fileref_t *fref);
#endif /* GLKUNIX_FILEREF_GET_FILENAME */

#define GLKUNIX_FILEREF_CREATE_UNCLEANED (1)
extern frefid_t glkunix_fileref_create_by_name_uncleaned(glui32 usage, char *name, glui32 rock);

typedef struct glkunix_serialize_context_struct *glkunix_serialize_context_t;
typedef struct glkunix_unserialize_context_struct *glkunix_unserialize_context_t;
typedef int (*glkunix_serialize_object_f)(glkunix_serialize_context_t, void *);
Expand Down
23 changes: 18 additions & 5 deletions remglk_capi/src/glkstart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ unsafe fn glkunix_arguments() -> Vec<GlkUnixArgument> {
.collect()
}

#[no_mangle]
pub extern "C" fn glkunix_fileref_create_by_name_uncleaned(usage: u32, filename_ptr: *const i8, rock: u32) -> FileRefPtr {
let mut glkapi = glkapi().lock().unwrap();
let filename_cstr = unsafe {CStr::from_ptr(filename_ptr)};
let filename = filename_cstr.to_string_lossy().to_string();
let fileref = glkapi.glkunix_fileref_create_by_name_uncleaned(usage, filename, rock);
to_owned(fileref)
}

#[no_mangle]
pub extern "C" fn glkunix_set_base_file(filename_ptr: *const c_char) {
let path = unsafe {CStr::from_ptr(filename_ptr)}.to_str().unwrap().to_owned();
Expand All @@ -207,12 +216,16 @@ pub extern "C" fn glkunix_set_base_file(filename_ptr: *const c_char) {

#[no_mangle]
pub extern "C" fn glkunix_stream_open_pathname(filename_ptr: *const i8, textmode: u32, rock: u32) -> StreamPtr {
// Remglk banned this from being used except during glkunix_startup_code, but I don't think that's really necessary
glkunix_stream_open_pathname_gen(filename_ptr, 0, textmode, rock)
}

#[no_mangle]
pub extern "C" fn glkunix_stream_open_pathname_gen(filename_ptr: *const i8, writemode: u32, textmode: u32, rock: u32) -> StreamPtr {
// Remglk says this can only be called during glkunix_startup_code, but I don't think that's really necessary
let fileref = glkunix_fileref_create_by_name_uncleaned(fileusage_Data | if textmode > 0 {fileusage_TextMode} else {fileusage_BinaryMode}, filename_ptr, 0);
let fileref = reclaim(fileref);
let mut glkapi = glkapi().lock().unwrap();
let filename_cstr = unsafe {CStr::from_ptr(filename_ptr)};
let filename = filename_cstr.to_string_lossy().to_string();
let fileref = glkapi.glkunix_fileref_create_by_name_uncleaned(fileusage_Data | if textmode > 0 {fileusage_TextMode} else {fileusage_BinaryMode}, filename, rock);
let str = glkapi.glk_stream_open_file(&fileref, FileMode::Read, rock).unwrap().unwrap();
let str = glkapi.glk_stream_open_file(&fileref, if writemode > 0 {FileMode::Write} else {FileMode::Read}, rock).unwrap().unwrap();
glkapi.glk_fileref_destroy(fileref);
to_owned(str)
}
18 changes: 15 additions & 3 deletions remglk_capi/src/systems/emglken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl GlkSystem for EmglkenSystem {
let dirs: EmglkenDirectories = buffer_to_protocol_struct(buf);
Directories {
storyfile: PathBuf::from(dirs.storyfile),
system_cwd: PathBuf::from(dirs.system_cwd),
temp: PathBuf::from(dirs.temp),
working: PathBuf::from(dirs.working),
}
Expand All @@ -113,9 +114,13 @@ impl GlkSystem for EmglkenSystem {
let path = path.to_str().unwrap();
let mut buf: MaybeUninit<EmglkenBuffer> = MaybeUninit::uninit();
unsafe {emglken_set_storyfile_dir(path.as_ptr(), path.len(), buf.as_mut_ptr())};
let emglken_dirs: EmglkenDirectories = buffer_to_protocol_struct(buf);
dirs.storyfile = PathBuf::from(emglken_dirs.storyfile);
dirs.working = PathBuf::from(emglken_dirs.working);
let emglken_dirs: EmglkenSetBaseFileDirectories = buffer_to_protocol_struct(buf);
if let Some(path) = emglken_dirs.storyfile {
dirs.storyfile = PathBuf::from(path);
}
if let Some(path) = emglken_dirs.working {
dirs.working = PathBuf::from(path);
}
}
}

Expand All @@ -128,10 +133,17 @@ pub struct EmglkenBuffer {
#[derive(Deserialize)]
struct EmglkenDirectories {
pub storyfile: String,
pub system_cwd: String,
pub temp: String,
pub working: String,
}

#[derive(Deserialize)]
struct EmglkenSetBaseFileDirectories {
pub storyfile: Option<String>,
pub working: Option<String>,
}

fn buffer_to_boxed_slice(buffer: MaybeUninit<EmglkenBuffer>) -> Box<[u8]> {
let buffer = unsafe {buffer.assume_init()};
unsafe {Box::from_raw(slice::from_raw_parts_mut(buffer.ptr, buffer.len))}
Expand Down
3 changes: 1 addition & 2 deletions remglk_capi/src/systems/library_emglken.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ const EMGLKEN_JS = {

emglken_set_storyfile_dir(path_ptr, path_len, buffer) {
const path = UTF8ToString(path_ptr, path_len)
Dialog.set_storyfile_dir(path)
const dirs = Dialog.get_dirs()
const dirs = Dialog.set_storyfile_dir(path)
writeBufferJSON(buffer, dirs)
},

Expand Down
12 changes: 7 additions & 5 deletions remglk_capi/src/systems/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,19 @@ impl GlkSystem for StandardSystem {
}

fn get_directories() -> Directories {
let cwd = env::current_dir().unwrap();
Directories {
storyfile: env::current_dir().unwrap(),
storyfile: cwd.clone(),
system_cwd: cwd.clone(),
temp: env::temp_dir(),
working: env::current_dir().unwrap(),
working: cwd,
}
}

fn set_base_file(folders: &mut Directories, path: String) {
fn set_base_file(dirs: &mut Directories, path: String) {
let mut path = PathBuf::from(path);
path.pop();
folders.storyfile = path.clone();
folders.working = path;
dirs.storyfile = path.clone();
dirs.working = path;
}
}

0 comments on commit 5530b15

Please sign in to comment.