Skip to content

Commit

Permalink
Merge pull request #7 from Berrysoft/dev/new-config
Browse files Browse the repository at this point in the history
Use the new config structure.
  • Loading branch information
Berrysoft authored Sep 17, 2022
2 parents 84174a9 + 1020020 commit 190240a
Show file tree
Hide file tree
Showing 31 changed files with 597 additions and 459 deletions.
3 changes: 3 additions & 0 deletions bins/ayaka-check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ async fn main() -> Result<()> {
OpenStatus::LoadPlugin(name, i, len) => {
println!("Loading plugin {} ({}/{})", name, i + 1, len)
}
OpenStatus::GamePlugin => println!("Preprocessing game..."),
OpenStatus::LoadResource => println!("Loading resources..."),
OpenStatus::LoadParagraph => println!("Loading paragraphs..."),
}
}
let mut ctx = context.await?;
Expand Down
39 changes: 21 additions & 18 deletions bins/ayaka-gui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ struct FullSettings {
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "t", content = "data")]
enum OpenGameStatus {
LoadSettings,
LoadProfile(String),
CreateRuntime,
LoadPlugin(String, usize, usize),
GamePlugin,
LoadResource,
LoadParagraph,
LoadSettings,
LoadGlobalRecords,
LoadRecords,
Loaded,
Expand All @@ -78,15 +81,18 @@ async fn open_game(handle: AppHandle, storage: State<'_, Storage>) -> CommandRes
OpenStatus::LoadPlugin(name, i, len) => {
emit_open_status(&handle, OpenGameStatus::LoadPlugin(name, i, len))?
}
OpenStatus::GamePlugin => emit_open_status(&handle, OpenGameStatus::GamePlugin)?,
OpenStatus::LoadResource => emit_open_status(&handle, OpenGameStatus::LoadResource)?,
OpenStatus::LoadParagraph => emit_open_status(&handle, OpenGameStatus::LoadParagraph)?,
}
}
let mut ctx = context.await?;

let window = handle.get_window("main").unwrap();
window.set_title(&ctx.game.title)?;
window.set_title(&ctx.game.config.title)?;
let settings = {
emit_open_status(&handle, OpenGameStatus::LoadSettings)?;
load_settings(&storage.ident).await.unwrap_or_else(|e| {
load_settings(&storage.ident).unwrap_or_else(|e| {
warn!("Load settings failed: {}", e);
Settings::new()
})
Expand All @@ -95,17 +101,14 @@ async fn open_game(handle: AppHandle, storage: State<'_, Storage>) -> CommandRes

emit_open_status(&handle, OpenGameStatus::LoadGlobalRecords)?;
ctx.set_global_record(
load_global_record(&storage.ident, &ctx.game.title)
.await
.unwrap_or_else(|e| {
warn!("Load global records failed: {}", e);
Default::default()
}),
load_global_record(&storage.ident, &ctx.game.config.title).unwrap_or_else(|e| {
warn!("Load global records failed: {}", e);
Default::default()
}),
);

emit_open_status(&handle, OpenGameStatus::LoadRecords)?;
*storage.records.lock().await = load_records(&storage.ident, &ctx.game.title)
.await
*storage.records.lock().await = load_records(&storage.ident, &ctx.game.config.title)
.unwrap_or_else(|e| {
warn!("Load records failed: {}", e);
Default::default()
Expand Down Expand Up @@ -162,10 +165,10 @@ async fn save_record_to(index: usize, storage: State<'_, Storage>) -> CommandRes
#[command]
async fn save_all(storage: State<'_, Storage>) -> CommandResult<()> {
if let Some(context) = storage.context.lock().await.as_ref() {
let game = &context.game.title;
save_settings(&storage.ident, context.settings()).await?;
save_global_record(&storage.ident, game, context.global_record()).await?;
save_records(&storage.ident, game, &storage.records.lock().await).await?;
let game = &context.game.config.title;
save_settings(&storage.ident, context.settings())?;
save_global_record(&storage.ident, game, context.global_record())?;
save_records(&storage.ident, game, &storage.records.lock().await)?;
}
Ok(())
}
Expand Down Expand Up @@ -206,9 +209,9 @@ struct GameInfo {
impl GameInfo {
pub fn new(game: &Game) -> Self {
Self {
title: game.title.clone(),
author: game.author.clone(),
props: game.props.clone(),
title: game.config.title.clone(),
author: game.config.author.clone(),
props: game.config.props.clone(),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions bins/ayaka-gui/src/interop/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export enum OpenGameStatusType {
LoadProfile,
CreateRuntime,
LoadPlugin,
GamePlugin,
LoadResource,
LoadParagraph,
LoadSettings,
LoadGlobalRecords,
LoadRecords,
Expand Down
8 changes: 7 additions & 1 deletion bins/ayaka-gui/src/views/StartView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default {
}
},
status_to_text(s: OpenGameStatus): [string, number] {
const step = 100 / 7
const step = 100 / 10
const t = OpenGameStatusType[s.t]
switch (t) {
case OpenGameStatusType.LoadProfile:
Expand All @@ -71,6 +71,12 @@ export default {
case OpenGameStatusType.LoadPlugin:
const data = s.data as unknown as [string, number, number]
return [`Loading plugin ${data[0]}...`, step * (t + 1) + data[1] / data[2] * step]
case OpenGameStatusType.GamePlugin:
return ["Preprocessing game...", step * (t + 1)]
case OpenGameStatusType.LoadResource:
return ["Loading resources...", step * (t + 1)]
case OpenGameStatusType.LoadParagraph:
return ["Loading paragraphs...", step * (t + 1)]
case OpenGameStatusType.LoadSettings:
return ["Loading settings...", step * (t + 1)]
case OpenGameStatusType.LoadGlobalRecords:
Expand Down
4 changes: 2 additions & 2 deletions bins/ayaka-latex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ async fn main() -> Result<()> {
output.command("usepackage", ["lua-ul"]).await?;
output.command("usepackage", ["luatexja-ruby"]).await?;
output.command("usepackage", ["verbatim"]).await?;
output.command("title", [&ctx.game.title]).await?;
output.command("author", [&ctx.game.author]).await?;
output.command("title", [&ctx.game.config.title]).await?;
output.command("author", [&ctx.game.config.author]).await?;
output
.environment("document", |output| async move {
output.command0("maketitle").await?;
Expand Down
32 changes: 17 additions & 15 deletions book/src/config/resources.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
# Resources
The resources are indexed by locale:
``` yaml
res:
en:
foo: Foo value
bar: Bar value
``` ignore
config.yaml
└─res
├─en.yaml
├─ja.yaml
└─zh.yaml
```
You can specify other locales, too.
The keys not specified in other locales will fallback to `base_lang` ones.

`en.yaml`
``` yaml
foo: Foo
bar: Bar
```
`zh.yaml`
``` yaml
base_lang: en
res:
en:
foo: Foo
bar: Bar
zh:
foo: 天
bar: 地
foo: 天
bar: 地
```

## Reference resources
You can reference resources in texts with `\res{}` command.
``` yaml
- 'The foo value: \res{foo}'
- 'The bar value: \res{bar}'
- The foo value is \res{foo}
- The bar value is \res{bar}
```
94 changes: 74 additions & 20 deletions book/src/config/structure.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
# File structure

## Directory structure
``` ignore
config.yaml
├─paras
│ ├─ja
│ │ ├─start.yaml
│ │ └─end.yaml
│ └─zh-Hans
│ ├─start.yaml
│ └─end.yaml
└─res
├─ja.yaml
└─zh-Hans.yaml
```

## Properties
The total config file is a `Game` object.
The total config file is a `GameConfig` object.
Here shows the properties:

| Property | Description |
| ----------- | ------------------------------------------- |
| `title` | The title of the game. |
| `base_lang` | The base language. |
| `paras` | The `Paragraph` objects, indexed by locale. |
| `author` | Optional. The author of the game. |
| `plugins` | Optional. The `PluginConfig` object. |
| `res` | Optional. The resources, indexed by locale. |
| `props` | Optional. The custom properties. |
| Property | Description |
| ----------- | ------------------------------------ |
| `title` | The title of the game. |
| `base_lang` | The base language. |
| `paras` | The paragraph path. |
| `start` | The start paragraph. |
| `author` | Optional. The author of the game. |
| `plugins` | Optional. The `PluginConfig` object. |
| `res` | Optional. The resource path. |
| `props` | Optional. The custom properties. |

The `PluginConfig` object contains the base directory and the plugin names:

Expand All @@ -30,21 +46,59 @@ A `Paragraph` object is a collection of texts:
| `title` | Optional. The title of the paragraph. |
| `next` | Optional. The next paragraph. |

## The visibility of paragraphs
Only the paragraph whose tag is the same as the file name(without extension) is public to all paragraphs.
The rest paragraphs in this file could only be referenced by the paragraphs in the same file.

For example, for `start.yaml`
``` ignore
- tag: start
next: foo
- tag: foo
next: bar
- tag: bar
next: end
```
and `end.yaml`
``` ignore
- tag: end
next: foo
- tag: foo
next: bar
- tag: bar
```

The `foo` and `bar` referenced are the ones in the same file, while `start` and `end` could be referenced from other files.

## Basic example
This is a config example, with 2 paragraphs.
``` ignore
config.yaml
└─paras
└─en
├─para1.yaml
└─para2.yaml
```
`config.yaml`
``` yaml
title: Title
base_lang: en
paras:
en:
- tag: para1
texts:
- This is the first line.
- This is the second line.
next: para2
- tag: para2
texts:
- The first line of the second paragraph.
paras: paras
start: para1
```
`para1.yaml`
``` yaml
- tag: para1
texts:
- This is the first line.
- This is the second line.
next: para2
```
`para2.yaml`
``` yaml
- tag: para2
texts:
- The first line of the second paragraph.
```
The output will be
``` ignore
Expand Down
13 changes: 2 additions & 11 deletions examples/Fibonacci/config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
title: Fibonacci
author: Berrysoft
base_lang: en
paras:
en:
- tag: init
texts:
- 1
- \exec{$n = 50; $a = 1; $b = 1; $i = 1; $b}
next: loop
- tag: loop
texts:
- \exec{c = $b; $b += $a; $a = c; $i += 1; $b}
next: \exec{if($i < $n, "loop")}
paras: paras
start: init
9 changes: 9 additions & 0 deletions examples/Fibonacci/paras/en/init.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- tag: init
texts:
- 1
- \exec{$n = 50; $a = 1; $b = 1; $i = 1; $b}
next: loop
- tag: loop
texts:
- \exec{c = $b; $b += $a; $a = c; $i += 1; $b}
next: \exec{if($i < $n, "loop")}
12 changes: 2 additions & 10 deletions examples/Fibonacci2/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,5 @@ plugins:
- random
- format
base_lang: en
paras:
en:
- tag: init
texts:
- \exec{$n = random.rnd(50); $a = 1; $b = 1; $i = 1; format.fmt("Starting to calculate Fibonacci {}", $n)}
next: loop
- tag: loop
texts:
- \exec{a = $a; b = $b; c = $b; $b += $a; $a = c; $i += 1; format.fmt("{:<11} + {:<11} = {:<11}", a, b, $b)}
next: \exec{if($i < $n, "loop")}
paras: paras
start: init
4 changes: 4 additions & 0 deletions examples/Fibonacci2/paras/en/init.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- tag: init
texts:
- \exec{$n = random.rnd(50); $a = 1; $b = 1; $i = 1; format.fmt("Starting to calculate Fibonacci {}", $n)}
next: loop
4 changes: 4 additions & 0 deletions examples/Fibonacci2/paras/en/loop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- tag: loop
texts:
- \exec{a = $a; b = $b; c = $b; $b += $a; $a = c; $i += 1; format.fmt("{:<11} + {:<11} = {:<11}", a, b, $b)}
next: \exec{if($i < $n, "loop")}
Loading

0 comments on commit 190240a

Please sign in to comment.