Skip to content

Commit

Permalink
Merge pull request #3 from SARDONYX-sard/feature/support-1st_person-m…
Browse files Browse the repository at this point in the history
…apping

Feature/support 1st person mapping
  • Loading branch information
SARDONYX-sard committed Oct 1, 2023
2 parents 8eaf561 + d98a942 commit 8016be3
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 21 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,26 @@ DAR to OAR Converter CLI
Usage: dar2oar.exe [OPTIONS] --src <SRC>

Options:
--src <SRC> DAR source dir path
--dist <DIST> OAR destination dir path(If not, it is inferred from src)
--name <NAME> mod name in config.json & folder name(If not, it is inferred from src)
--author <AUTHOR> mod author in config.json
--mapping-file <MAPPING_FILE> path to section name table
--log-level <LOG_LEVEL> log_level trace | debug | info | warn | error [default: error]
--log-path <LOG_PATH> [default: ./convert.log]
-h, --help Print help
-V, --version Print version
--src <SRC>
DAR source dir path
--dist <DIST>
OAR destination dir path(If not, it is inferred from src)
--name <NAME>
mod name in config.json & folder name(If not, it is inferred from src)
--author <AUTHOR>
mod author in config.json
--mapping-file <MAPPING_FILE>
path to section name table
--mapping-1person-file <MAPPING_1PERSON_FILE>
path to section name table(For _1st_person)
--log-level <LOG_LEVEL>
log_level trace | debug | info | warn | error [default: error]
--log-path <LOG_PATH>
[default: ./convert.log]
-h, --help
Print help
-V, --version
Print version
```

## What is the mapping file?
Expand All @@ -64,6 +75,7 @@ Sample
8000000 Combat
8000001
8000002
// This is a line comment. // to line breaks are ignored.
8000005
8000005 Female
8001000
Expand Down
12 changes: 12 additions & 0 deletions dar2oar_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub struct Args {
/// path to section name table
#[arg(long)]
mapping_file: Option<String>,
/// path to section name table(For _1st_person)
#[arg(long)]
mapping_1person_file: Option<String>,
/// log_level trace | debug | info | warn | error
#[arg(long, default_value = "error")]
log_level: String,
Expand Down Expand Up @@ -49,12 +52,21 @@ pub fn run_cli(args: Args) -> anyhow::Result<()> {
None => None,
};

let table_1person = match args.mapping_1person_file {
Some(table_path) => {
let mapping = read_mapping_table(table_path)?;
Some(mapping)
}
None => None,
};

convert_dar_to_oar(
args.src,
dist,
args.name.as_deref(),
args.author.as_deref(),
table,
table_1person
)?;
Ok(())
}
5 changes: 5 additions & 0 deletions dar2oar_core/src/fs/mapping_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ fn parse_mapping_table(table: &str) -> HashMap<String, String> {
let mut current_section_name = String::new();
let mut idx = 0;
for line in table.lines() {
if line.starts_with("//") {
continue;
};

let mapping: Vec<&str> = line.split_whitespace().collect();
let section_name = match mapping.get(1) {
Some(val) => {
Expand Down Expand Up @@ -55,6 +59,7 @@ mod tests {
8000000 Combat
8000001
8000001 Base
// This is a line comment
8000002
8000005
8000005 Female
Expand Down
13 changes: 9 additions & 4 deletions dar2oar_core/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub fn convert_dar_to_oar<P>(
mod_name: Option<&str>,
author: Option<&str>,
section_table: Option<HashMap<String, String>>,
section_1person_table: Option<HashMap<String, String>>,
) -> Result<()>
where
P: AsRef<Path>,
Expand Down Expand Up @@ -115,10 +116,13 @@ where
// For this reason, an empty string should be put in the name space folder.
let priority = &priority.unwrap_or_default();

let section_name = section_table
.as_ref()
.and_then(|table| table.get(priority))
.unwrap_or(priority);
let section_name = match is_1st_person {
true => section_1person_table
.as_ref()
.and_then(|table| table.get(priority)),
false => section_table.as_ref().and_then(|table| table.get(priority)),
}
.unwrap_or(priority);

let section_root = oar_name_space_path.join(section_name);
log::trace!("section root: {:?}", section_root);
Expand Down Expand Up @@ -189,6 +193,7 @@ mod test {
None,
None,
Some(mapping),
None,
)
}
}
55 changes: 48 additions & 7 deletions frontend/src/components/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type FormProps = {
modName: string;
modAuthor: string;
mappingPath: string;
mapping1personPath: string;
logLevel: LogLevel;
loading: boolean;
};
Expand Down Expand Up @@ -43,6 +44,7 @@ export function ConvertForm() {
modName: localStorage.getItem("modName") ?? "",
modAuthor: localStorage.getItem("modAuthor") ?? "",
mappingPath: localStorage.getItem("mappingPath") ?? "",
mapping1personPath: localStorage.getItem("mapping1personPath") ?? "",
logLevel: tryGetLogLevel(),
loading: false as boolean,
} satisfies FormProps,
Expand All @@ -65,15 +67,21 @@ export function ConvertForm() {
modName,
modAuthor,
mappingPath,
mapping1personPath,
}) => {
setLoading(true);

await convertDar2oar({ src, dist, modName, modAuthor, mappingPath }).catch(
(e) => {
toast.error(`${e}`);
setLoading(false);
}
);
await convertDar2oar({
src,
dist,
modName,
modAuthor,
mappingPath,
mapping1personPath,
}).catch((e) => {
toast.error(`${e}`);
setLoading(false);
});
setLoading(false);
};

Expand Down Expand Up @@ -153,7 +161,7 @@ export function ConvertForm() {
<TextField
sx={{ minWidth: "100%" }}
label="Mapping Table Path"
placeholder="../mapping_table.txt"
placeholder="./mapping_table.txt"
value={value}
variant="outlined"
margin="dense"
Expand All @@ -172,6 +180,39 @@ export function ConvertForm() {
)}
/>

<Controller
name="mapping1personPath"
control={control}
render={({
field: { onChange, onBlur, value },
fieldState: { error },
}) => (
<>
<TextField
sx={{ minWidth: "100%" }}
label="Mapping Table Path(For _1st_person)"
placeholder="./mapping_table_for_1st_person.txt"
value={value}
variant="outlined"
margin="dense"
onChange={(e) => {
localStorage.setItem("mapping1personPath", e.target.value);
onChange(e);
}}
onBlur={onBlur}
error={Boolean(error)}
helperText={
"Correspondence path that can change the priority number to your own section name instead of the dir name"
}
/>
<PathSelector
path={value}
setValue={setStorage("mapping1personPath")}
/>
</>
)}
/>

<Grid>
<Controller
name="modName"
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/tauri_cmd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ConverterArgs = {
modName?: string;
modAuthor?: string;
mappingPath?: string;
mapping1personPath?: string;
logLevel?: LogLevel;
};

Expand All @@ -29,6 +30,7 @@ export async function convertDar2oar(props: ConverterArgs): Promise<void> {
modName,
modAuthor,
mappingPath: props.mappingPath,
mapping1personPath: props.mapping1personPath,
logLevel: props.logLevel,
});
} catch (e) {
Expand Down
13 changes: 13 additions & 0 deletions src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn convert_dar2oar(
mod_name: Option<&str>,
mod_author: Option<&str>,
mapping_path: Option<String>,
mapping_1person_path: Option<String>,
log_level: Option<String>,
) -> Result<(), String> {
let dist = oar_mod_folder.and_then(|dist| match dist.is_empty() {
Expand All @@ -24,6 +25,16 @@ pub fn convert_dar2oar(
}
None => None,
};
let table_1person = match mapping_1person_path {
Some(ref table_path) => {
let mapping = match read_mapping_table(table_path) {
Ok(table) => table,
Err(err) => return Err(err.to_string()),
};
Some(mapping)
}
None => None,
};

let log_level = match log_level {
Some(level) => match level.as_str() {
Expand All @@ -38,6 +49,7 @@ pub fn convert_dar2oar(
log::debug!("mod_name: {:?}", mod_name);
log::debug!("mod_author: {:?}", mod_author);
log::debug!("table path: {:?}", mapping_path.as_ref());
log::debug!("1st person table path: {:?}", mapping_1person_path.as_ref());
log::debug!("log level: {:?}", log_level.as_str());

match convert_dar_to_oar(
Expand All @@ -46,6 +58,7 @@ pub fn convert_dar2oar(
mod_name.as_deref(),
mod_author.as_deref(),
table,
table_1person,
) {
Ok(_) => Ok(()),
Err(err) => return Err(err.to_string()),
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"windows": [
{
"fullscreen": false,
"height": 750,
"height": 850,
"resizable": true,
"theme": "Dark",
"title": "DAR to OAR Converter",
Expand Down

0 comments on commit 8016be3

Please sign in to comment.