Skip to content

Commit

Permalink
Fix elf exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
1whatleytay committed Feb 22, 2024
1 parent c3cbcbf commit 99e3bdd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src-tauri/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs;
use std::path::Path;
use base64::Engine;
use crate::access_manager::AccessManager;
use crate::access_manager::{AccessFilter, AccessManager};
use crate::build::HexRegion;

fn write_region_contents(destination: &Path, contents: &str) {
Expand All @@ -10,6 +10,15 @@ fn write_region_contents(destination: &Path, contents: &str) {
fs::write(destination, value).ok();
}

#[tauri::command]
pub async fn export_binary_contents(data: Vec<u8>, filters: Vec<AccessFilter>, state: tauri::State<'_, AccessManager>) -> Result<String, ()> {
let destination = state.select_save("Save File", &filters, false).await.ok_or(())?;

fs::write(&destination, data).map_err(|_| ())?;

Ok(destination.to_string_lossy().to_string())
}

#[tauri::command]
pub async fn export_hex_contents(data: &str, state: tauri::State<'_, AccessManager>) -> Result<String, ()> {
let destination = state.select_save("Save Region File", &[], false).await.ok_or(())?;
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::debug::{read_bytes, set_register, swap_breakpoints, write_bytes};
use crate::menu::platform_shortcuts;
use crate::midi::{midi_install, midi_protocol, MidiProviderContainer};
use crate::state::commands::DebuggerBody;
use crate::export::{export_hex_regions, export_hex_contents};
use crate::export::{export_hex_regions, export_hex_contents, export_binary_contents};

use crate::state::commands::{resume, rewind, pause, stop, last_pc, post_key, post_input, wake_sync};
use crate::testing::{all_tests, run_tests};
Expand Down Expand Up @@ -127,6 +127,7 @@ fn main() {
detailed_disassemble,
export_hex_regions,
export_hex_contents,
export_binary_contents,
])
.register_uri_scheme_protocol("midi", midi_protocol)
.register_uri_scheme_protocol("display", display_protocol)
Expand Down
4 changes: 3 additions & 1 deletion src/utils/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
} from '../state/console-data'
import {
AssemblerResult,
assembleText, assembleWithBinary, disassemblyDetails,
assembleText,
assembleWithBinary,
disassemblyDetails,
ExecutionModeType,
ExecutionResult,
ExecutionState
Expand Down
14 changes: 4 additions & 10 deletions src/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { appWindow } from '@tauri-apps/api/window'
import { watch } from 'vue'
import { MidiNote, playNote } from './midi'
import { splitLines } from './split-lines'
import { writeBinaryFile } from '@tauri-apps/api/fs'
import { exportBinaryContents } from './query/serialize-files'

export enum PromptType {
NeverPrompt,
Expand Down Expand Up @@ -203,16 +203,10 @@ export async function setupEvents() {
binary = result.binary
}

let destination: AccessFile<undefined> | null = null
let destination: string | null = null

if (binary !== null) {
destination = await selectSaveDestination('Save File', elfFilter)

if (!destination) {
return
}

await writeBinaryFile(destination.path, binary)
destination = await exportBinaryContents(binary.buffer, elfFilter)
}

consoleData.showConsole = true
Expand All @@ -222,7 +216,7 @@ export async function setupEvents() {
}

if (destination !== null) {
pushConsole(`ELF file written to ${destination.path}`, ConsoleType.Info)
pushConsole(`ELF file written to ${destination}`, ConsoleType.Info)
}
})

Expand Down
12 changes: 12 additions & 0 deletions src/utils/query/serialize-files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { HexRegion } from '../mips'

import { invoke } from '@tauri-apps/api'
import { AccessFilter } from './access-manager'

export async function exportBinaryContents(data: ArrayBuffer, filters: AccessFilter[]): Promise<string | null> {
try {
return await invoke('export_binary_contents', {
data: Array.from(new Uint8Array(data)),
filters
})
} catch {
return null
}
}

export async function exportHexContents(data: string): Promise<string | null> {
try {
Expand Down

0 comments on commit 99e3bdd

Please sign in to comment.