mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-12 15:14:35 +01:00
Merge pull request #251 from navidmafi/main
fix(web): standardize utils/game.ts for cross-platform (needs windows test)
This commit is contained in:
@@ -1,110 +1,34 @@
|
|||||||
import { invoke } from '@tauri-apps/api'
|
import { invoke } from '@tauri-apps/api'
|
||||||
import { getConfig } from './configuration'
|
import { basename, dirname, join } from '@tauri-apps/api/path'
|
||||||
|
import { getConfigOption } from './configuration'
|
||||||
|
|
||||||
|
export const getGrasscutterJar = () => getConfigOption('grasscutter_path')
|
||||||
|
|
||||||
export async function getGameExecutable() {
|
export async function getGameExecutable() {
|
||||||
const config = await getConfig()
|
const exe_path = await getConfigOption('game_install_path')
|
||||||
|
return await basename(exe_path)
|
||||||
if (!config.game_install_path) {
|
}
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const pathArr = config.game_install_path.replace(/\\/g, '/').split('/')
|
|
||||||
return pathArr[pathArr.length - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getGrasscutterJar() {
|
|
||||||
const config = await getConfig()
|
|
||||||
|
|
||||||
if (!config.grasscutter_path) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const pathArr = config.grasscutter_path.replace(/\\/g, '/').split('/')
|
|
||||||
return pathArr[pathArr.length - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getGameFolder() {
|
|
||||||
const config = await getConfig()
|
|
||||||
|
|
||||||
if (!config.game_install_path) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const pathArr = config.game_install_path.replace(/\\/g, '/').split('/')
|
|
||||||
pathArr.pop()
|
|
||||||
|
|
||||||
const path = pathArr.join('/')
|
|
||||||
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getGameDataFolder() {
|
|
||||||
const gameExec = await getGameExecutable()
|
|
||||||
|
|
||||||
if (!gameExec) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return (await getGameFolder()) + '\\' + gameExec.replace('.exe', '_Data')
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getGameVersion() {
|
export async function getGameVersion() {
|
||||||
const GameData = await getGameDataFolder()
|
const execPath = await getConfigOption('game_install_path')
|
||||||
const platform = await invoke('get_platform')
|
const rootPath = await dirname(execPath)
|
||||||
|
const baseName = (await basename(execPath, ".exe"))
|
||||||
if (!GameData) {
|
const datapath = await join(rootPath, `${baseName}_Data`)
|
||||||
return null
|
const asbPath = await join(datapath, 'StreamingAssets', 'asb_settings.json')
|
||||||
}
|
const hasAsb = await invoke<boolean>('dir_exists', { path: asbPath })
|
||||||
|
|
||||||
let hasAsb = await invoke('dir_exists', {
|
|
||||||
path: GameData + '\\StreamingAssets\\asb_settings.json',
|
|
||||||
})
|
|
||||||
|
|
||||||
if (platform != 'windows') {
|
|
||||||
hasAsb = await invoke('dir_exists', {
|
|
||||||
path: GameData + '/StreamingAssets/asb_settings.json',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!hasAsb) {
|
if (!hasAsb) {
|
||||||
// For games that cannot determine game version
|
const versionFile = await join(datapath, 'StreamingAssets', 'BinaryVersion.bytes')
|
||||||
let otherGameVer: string = await invoke('read_file', {
|
const rawVersion = await invoke<string>('read_file', { path: versionFile })
|
||||||
path: GameData + '\\StreamingAssets\\BinaryVersion.bytes',
|
if (!rawVersion) return null
|
||||||
})
|
|
||||||
|
|
||||||
if (platform != 'windows') {
|
const [major, minor] = rawVersion.split('.').map(Number)
|
||||||
otherGameVer = await invoke('read_file', {
|
return { major, minor, release: 0 }
|
||||||
path: GameData + '/StreamingAssets/BinaryVersion.bytes',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const versionRaw = otherGameVer.split('.')
|
|
||||||
const version = {
|
|
||||||
major: parseInt(versionRaw[0]),
|
|
||||||
minor: parseInt(versionRaw[1]),
|
|
||||||
// This will probably never matter, just use major/minor. If needed, full version values are near EOF
|
|
||||||
release: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
if (otherGameVer == null || otherGameVer.length < 1) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return version
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const settings = JSON.parse(
|
const settings = JSON.parse(await invoke<string>('read_file', { path: asbPath }))
|
||||||
await invoke('read_file', {
|
const [major, minorRelease] = settings.variance.split('.')
|
||||||
path: GameData + '\\StreamingAssets\\asb_settings.json',
|
const [minor, release] = minorRelease.split('_').map(Number)
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
const versionRaw = settings.variance.split('.')
|
return { major: parseInt(major), minor, release }
|
||||||
const version = {
|
|
||||||
major: parseInt(versionRaw[0]),
|
|
||||||
minor: parseInt(versionRaw[1].split('_')[0]),
|
|
||||||
release: parseInt(versionRaw[1].split('_')[1]),
|
|
||||||
}
|
|
||||||
|
|
||||||
return version
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user