Skip to content

Commit

Permalink
add temporary nicknames
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzart committed Oct 10, 2023
1 parent 236d1b2 commit 2630bea
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 48 deletions.
7 changes: 7 additions & 0 deletions bff-gui/src-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ pub struct UnimplementedExporterError {
pub class_name: Name,
}

#[derive(Debug, Constructor, Display, Error)]
#[display(fmt = "failed to find nickname for {}", resource_name)]
pub struct InvalidNicknameError {
pub resource_name: Name,
}

#[derive(Debug, Display, Error, From)]
pub enum Error {
Bff(BffError),
InvalidResource(InvalidResourceError),
InvalidPreview(InvalidPreviewError),
UnimplementedExporter(UnimplementedExporterError),
InvalidNickname(InvalidNicknameError),
Io(std::io::Error),
SerdeJson(serde_json::Error),
AnsiToHtml(ansi_to_html::Error),
Expand Down
56 changes: 40 additions & 16 deletions bff-gui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bff::class::Class;
use bff::names::Name;
use bff::platforms::Platform;
use bff::traits::TryIntoVersionPlatform;
use error::{BffGuiResult, InvalidPreviewError, InvalidResourceError};
use error::{BffGuiResult, InvalidNicknameError, InvalidPreviewError, InvalidResourceError};
use serde::Serialize;
use serde_repr::Serialize_repr;
use traits::Export;
Expand Down Expand Up @@ -72,14 +72,17 @@ struct ResourceInfo {

pub struct InnerAppState {
pub bigfile: BigFile,
pub platform: Platform,
pub resource_previews: HashMap<Name, ResourcePreview>,
pub nicknames: HashMap<Name, String>,
}

impl InnerAppState {
pub fn add_preview(&mut self, preview: ResourcePreview) {
self.resource_previews.insert(preview.name, preview);
}
pub fn add_nickname(&mut self, name: Name, nickname: String) {
self.nicknames.insert(name, nickname);
}
}

pub struct AppState(pub Mutex<Option<InnerAppState>>);
Expand All @@ -94,6 +97,8 @@ fn main() {
export_one_json,
export_preview,
get_extensions,
add_nickname,
get_nickname,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down Expand Up @@ -122,8 +127,8 @@ fn extract_bigfile(path: &str, state: tauri::State<AppState>) -> Result<BigFileD
let mut state_guard = state.0.lock().unwrap();
*state_guard = Some(InnerAppState {
bigfile,
platform,
resource_previews: HashMap::new(),
nicknames: HashMap::new(),
});

Ok(BigFileData {
Expand All @@ -147,13 +152,13 @@ fn parse_resource(
if let Some(resource_preview) = state.resource_previews.get(&resource_name) {
return Ok(resource_preview.clone());
}
let bf = &state.bigfile;
let version = &bf.manifest.version;
let platform = state.platform;

let resource: &Resource = bf.objects.get(&resource_name).unwrap();
let resource: &Resource = state.bigfile.objects.get(&resource_name).unwrap();

let class = resource.try_into_version_platform(version.clone(), platform)?;
let class = resource.try_into_version_platform(
state.bigfile.manifest.version.clone(),
state.bigfile.manifest.platform,
)?;
let data = match class {
Class::Bitmap(ref bitmap) => Some(bitmap.export(resource_name)?),
Class::Sound(ref sound) => Some(sound.export(resource_name)?),
Expand Down Expand Up @@ -187,8 +192,10 @@ fn export_all_json(path: &Path, state: tauri::State<AppState>) -> BffGuiResult<(
let mut state_guard = state.0.lock().unwrap();
let state = state_guard.as_mut().unwrap();
for resource in state.bigfile.objects.values() {
let class_res: bff::BffResult<Class> = resource
.try_into_version_platform(state.bigfile.manifest.version.clone(), state.platform);
let class_res: bff::BffResult<Class> = resource.try_into_version_platform(
state.bigfile.manifest.version.clone(),
state.bigfile.manifest.platform,
);
match class_res {
Ok(class) => write_class(&path.join(format!("{}.json", resource.name)), &class)?,
Err(_) => println!("skipped {}", resource.name),
Expand All @@ -208,12 +215,11 @@ fn export_one_json(path: &Path, name: Name, state: tauri::State<AppState>) -> Bf
.ok_or(InvalidResourceError {
resource_name: name,
})?;
let class_res: bff::BffResult<Class> =
resource.try_into_version_platform(state.bigfile.manifest.version.clone(), state.platform);
match class_res {
Ok(class) => write_class(&path.join(format!("{}.json", resource.name)), &class)?,
Err(_) => println!("skipped {}", resource.name),
}
let class = resource.try_into_version_platform(
state.bigfile.manifest.version.clone(),
state.bigfile.manifest.platform,
)?;
write_class(&path.join(format!("{}.json", resource.name)), &class)?;
Ok(())
}

Expand Down Expand Up @@ -250,3 +256,21 @@ fn get_extensions() -> Vec<String> {
.map(|s| s.to_str().unwrap().into())
.collect()
}

#[tauri::command]
fn add_nickname(name: Name, nickname: String, state: tauri::State<AppState>) {
let mut state_guard = state.0.lock().unwrap();
let state = state_guard.as_mut().unwrap();
state.add_nickname(name, nickname);
}

#[tauri::command]
fn get_nickname(name: Name, state: tauri::State<AppState>) -> BffGuiResult<String> {
let mut state_guard = state.0.lock().unwrap();
let state = state_guard.as_mut().unwrap();
let nickname = state
.nicknames
.get(&name)
.ok_or(InvalidNicknameError::new(name))?;
Ok(nickname.clone())
}
10 changes: 9 additions & 1 deletion bff-gui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { View } from "./components/View";
import { Explorer } from "./components/Explorer";
import { Menubar } from "./components/Menubar";

import { BigFileData, ResourcePreview, ViewTab } from "./types/types";
import { BigFileData, Nickname, ResourcePreview, ViewTab } from "./types/types";

function App() {
const [bigfile, setBigfile] = useState<BigFileData>({
Expand All @@ -14,6 +14,8 @@ function App() {
const [resourcePreview, setResourcePreview] =
useState<ResourcePreview | null>(null);
const [openTab, setOpenTab] = useState<ViewTab>(ViewTab.Preview);
const [nicknames, setNicknames] = useState<Nickname[]>([]);
const [currentNickname, setCurrentNickname] = useState<string>("");

return (
<div className="container">
Expand All @@ -26,13 +28,19 @@ function App() {
<div className="main">
<Explorer
bigfile={bigfile}
nicknames={nicknames}
setResourcePreview={setResourcePreview}
setOpenTab={setOpenTab}
setCurrentNickname={setCurrentNickname}
/>
<View
resourcePreview={resourcePreview}
nicknames={nicknames}
currentNickname={currentNickname}
openTab={openTab}
setOpenTab={setOpenTab}
setNicknames={setNicknames}
setCurrentNickname={setCurrentNickname}
/>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion bff-gui/src/assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

:root {
--font: hsl(16, 100%, 97%);
--dark-font: hsl(1, 0%, 50%);
--background: hsl(320, 6%, 9%);
--light1-background: hsl(300, 3%, 12%);
--light2-background: hsl(300, 3%, 15%);
Expand All @@ -13,7 +14,7 @@
--shadow: hsla(0, 0%, 0%, 0.25);
--error: hsl(357, 97%, 75%);

font-family: WorkSans, Helvetica, Arial, sans-serif;
font-family: WorkSans, system-ui;
font-size: 16px;
line-height: 1rem;

Expand Down
18 changes: 13 additions & 5 deletions bff-gui/src/components/Explorer.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@
resize: horizontal;
}

.bffobject-list {
.resource-list {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: calc(100vh - 4.79em);
padding-top: 1.4em;
overflow-y: scroll;
}

.bffobject {
white-space: nowrap;
.resource {
width: 100%;
white-space: nowrap;
padding-left: 1em;
}

.bffobject:focus {
.resource:focus {
background-color: var(--hover);
}

.bffobject-unimpl {
.resource-unimpl {
color: var(--error);
}

.resource-realname {
margin-left: 1em;
color: var(--dark-font);
}

.explorer-sort {
width: calc(40% - 0.95rem);
}
Expand Down
Loading

0 comments on commit 2630bea

Please sign in to comment.