Skip to content

Commit

Permalink
fix: incomplete path data + broken "reveal in file explorer" (#77)
Browse files Browse the repository at this point in the history
* update PathData type

* validate recent saves

* write recent saves if file needed to be fixed
  • Loading branch information
andrewbenington authored Dec 15, 2024
1 parent cb6ebdf commit 6f84739
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/backend/backendInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default interface BackendInterface {
rollbackTransaction: () => Promise<Errorable<null>>

/* application */
pickFile: () => Promise<Errorable<string | undefined>>
pickFile: () => Promise<Errorable<PathData | undefined>>
pickFolder: () => Promise<Errorable<string | undefined>>
getResourcesPath: () => Promise<string>
openDirectory: (directory: string) => Promise<Errorable<null>>
Expand Down
49 changes: 44 additions & 5 deletions src/backend/tauri/tauriBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import { Errorable, JSONObject, LoadSaveResponse, LookupMap, SaveRef } from 'src
import { Settings } from '../../state/appInfo'
import { TauriInvoker } from './tauriInvoker'

async function pathDataFromRaw(raw: string): Promise<PathData> {
const filename = await path.basename(raw)
const dir = await path.dirname(raw)
const ext = await path.extname(raw)

const pathData: PathData = {
raw,
name: filename,
separator: path.sep(),
dir,
ext,
}

return pathData
}

export const TauriBackend: BackendInterface = {
/* past gen identifier lookups */
loadGen12Lookup: function (): Promise<Errorable<LookupMap>> {
Expand Down Expand Up @@ -100,9 +116,31 @@ export const TauriBackend: BackendInterface = {

// /* game save management */
getRecentSaves: async (): Promise<Errorable<Record<string, SaveRef>>> => {
return TauriInvoker.getStorageFileJSON('recent_saves.json') as Promise<
const result = await (TauriInvoker.getStorageFileJSON('recent_saves.json') as Promise<
Errorable<Record<string, SaveRef>>
>
>)

if (E.isLeft(result)) {
return result
}

const validatedSaves: Record<string, SaveRef> = {}

let modified = false

for (let [rawPath, saveRef] of Object.entries(result.right)) {
if (!saveRef.filePath.dir) {
saveRef.filePath = await pathDataFromRaw(rawPath)
modified = true
}

if (modified) {
TauriInvoker.writeStorageFileJSON('recent_saves.json', validatedSaves)
}
validatedSaves[rawPath] = saveRef
}

return E.right(validatedSaves)
},
addRecentSave: async (saveRef: SaveRef): Promise<Errorable<null>> => {
const recentSavesResult = await (TauriInvoker.getStorageFileJSON(
Expand Down Expand Up @@ -185,10 +223,11 @@ export const TauriBackend: BackendInterface = {
},

/* application */
pickFile: async (): Promise<Errorable<string | undefined>> => {
const path = await fileDialog({ directory: false, title: 'Select File' })
pickFile: async (): Promise<Errorable<PathData | undefined>> => {
const filePath = await fileDialog({ directory: false, title: 'Select File' })

return E.right(path ?? undefined)
if (!filePath) return E.right(undefined)
return E.right(await pathDataFromRaw(filePath))
},
pickFolder: async (): Promise<Errorable<string | undefined>> => {
const path = await fileDialog({ directory: true, title: 'Select Folder' })
Expand Down
3 changes: 2 additions & 1 deletion src/saves/SavesModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const SavesModal = (props: SavesModalProps) => {
console.error(pickedFile.left)
return
}
filePath = { raw: pickedFile.right } as PathData
if (!pickedFile.right) return
filePath = pickedFile.right
}
backend.loadSaveFile(filePath).then(
E.match(
Expand Down
5 changes: 2 additions & 3 deletions src/types/SAVTypes/G3SAV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'src/util/byteLogic'
import { gen3StringToUTF } from 'src/util/Strings/StringConverter'
import { OHPKM } from '../pkm/OHPKM'
import { emptyPathData, PathData, splitPath } from './path'
import { emptyPathData, PathData } from './path'
import { Box, BoxCoordinates, SAV } from './SAV'
import { LOOKUP_TYPE } from './util'

Expand Down Expand Up @@ -255,8 +255,7 @@ export class G3SAV implements SAV<PK3> {
if (trainerMon) {
this.origin = trainerMon?.gameOfOrigin
} else {
const filePathElements = splitPath(this.filePath)
let fileName = filePathElements[filePathElements.length - 1]
let fileName = this.filePath.name

fileName = fileName.replace(/\s+/g, '')
if (fileName.includes('Ruby')) {
Expand Down
6 changes: 0 additions & 6 deletions src/types/SAVTypes/__test__/G3RRSAV.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ describe('G3RRSAV - Radical Red Save File Read Test', () => {

const parsedPath: PathData = {
raw: './SAVFILES/radicalred.sav',
base: '.SAVFILES/radical red.sav',
name: 'radical red',
dir: './SAVFILES',
ext: '.sav',
root: '/',
separator: '/',
}

Expand Down Expand Up @@ -125,11 +123,9 @@ describe('G3RRSAV - Radical Red Save File Write Test', () => {

const parsedPath: PathData = {
raw: './SAVFILES/radicalred.sav',
base: '.SAVFILES/radicalred.sav',
name: 'radical red',
dir: './SAVFILES',
ext: '.sav',
root: '/',
separator: '/',
}

Expand Down Expand Up @@ -168,11 +164,9 @@ describe('G3RRSAV - Radical Red Save File Write Test', () => {
test('should check if modifications were made', () => {
const parsedPath: PathData = {
raw: './SAVFILES/radicalred_modified.sav',
base: '.SAVFILES/radicalred_modified.sav',
name: 'radical red',
dir: './SAVFILES',
ext: '.sav',
root: '/',
separator: '/',
}

Expand Down
2 changes: 0 additions & 2 deletions src/types/SAVTypes/__test__/G7SAV.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ describe('gen 7 save files', () => {

const parsedPath: PathData = {
raw: './SAVFILES/ultrasun',
base: '.SAVFILES/ultrasun',
name: 'ultrasun',
dir: './SAVFILES',
ext: '',
root: '/',
separator: '/',
}

Expand Down
12 changes: 2 additions & 10 deletions src/types/SAVTypes/path.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
export type PathData = {
raw: string
base: string
name: string
dir: string
ext: string
root: string
separator: '\\' | '/'
separator: string
}

export const emptyPathData: PathData = {
raw: '',
base: '',
dir: '',
name: '',
separator: '/',
ext: '',
root: '',
}

export function joinPath(path: PathData) {
return [path.dir, path.name].join(path.separator) + path.ext
}

export function splitPath(path: PathData) {
return [path.root, ...path.dir.split(path.separator).filter((seg) => seg !== ''), path.name]
return [...path.dir.split(path.separator).filter((seg) => seg !== ''), path.name]
}

export type PossibleSaves = {
Expand Down

0 comments on commit 6f84739

Please sign in to comment.