physis/
common.rs

1// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4use std::fs;
5use std::path::Path;
6
7use binrw::binrw;
8
9#[binrw]
10#[brw(repr(u8))]
11#[repr(u8)]
12#[derive(Copy, Clone)]
13/// The language the game data is written for. Some of these languages are supported in the Global region.
14pub enum Language {
15    /// Used for data that is language-agnostic, such as item data.
16    None,
17    /// Japanese language.
18    Japanese,
19    /// English language.
20    English,
21    /// German language.
22    German,
23    /// French language.
24    French,
25    /// Chinese (Simplified) language.
26    ChineseSimplified,
27    /// Chinese (Traditional) language.
28    ChineseTraditional,
29    /// Korean language.
30    Korean,
31}
32
33/// Returns the shorthand language code for `language`. For example, English becomes "en".
34pub fn get_language_code(lang: &Language) -> &'static str {
35    match &lang {
36        Language::None => "",
37        Language::Japanese => "ja",
38        Language::English => "en",
39        Language::German => "de",
40        Language::French => "fr",
41        Language::ChineseSimplified => "chs",
42        Language::ChineseTraditional => "cht",
43        Language::Korean => "ko",
44    }
45}
46
47/// The region of the game. Used to denote the region a patch is meant for.
48#[binrw]
49#[brw(repr = i16)]
50#[derive(Debug, PartialEq, Eq)]
51pub enum Region {
52    /// The global region, used for any region not specified.
53    Global = -1,
54    /// Korea and China clients.
55    KoreaChina = 1,
56}
57
58/// Reads a version file.
59pub fn read_version(p: &Path) -> Option<String> {
60    fs::read_to_string(p).ok()
61}
62
63#[binrw]
64#[brw(repr = u8)]
65#[derive(Clone, Debug, PartialEq, Eq)]
66pub enum Platform {
67    /// Windows and macOS
68    Win32 = 0x0,
69    /// Playstation 3
70    PS3 = 0x1,
71    /// Playstation 4
72    PS4 = 0x2,
73    /// Playstation 5
74    PS5 = 0x3,
75    /// Xbox
76    Xbox = 0x4,
77}
78
79pub fn get_platform_string(id: &Platform) -> &'static str {
80    match &id {
81        Platform::Win32 => "win32",
82        Platform::PS3 => "ps3",
83        Platform::PS4 => "ps4",
84        Platform::PS5 => "ps5",
85        Platform::Xbox => "lys",
86    }
87}