Skip to content

Commit 66ce43e

Browse files
committed
[R] 给Maidata类的常用信息属性加上setter
1 parent 7cd7503 commit 66ce43e

2 files changed

Lines changed: 51 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ return maidataText; // maidataText即为转谱结果
109109
- bool `isUtage` (默认为false): 仅影响生成的MA2的文件头区域的`FES_MODE`的值是1还是0,一般来说是不重要的。
110110
111111
#### 更多示例(异常处理)
112-
注意:当解析/生成步骤失败时,会抛出ConversionException异常,其中含有Alerts属性,是转谱过程中遇到的错误和警告等信息。(类比于C语言编译器会打印出Error和Warning信息)
112+
注意:当解析/生成步骤失败时,会抛出`ConversionException`异常,其中含有`Alerts`属性,是转谱过程中遇到的错误和警告等信息。(类比于C语言编译器会打印出Error和Warning信息)
113113
因此,建议您采用try-catch的写法,捕获可能出现的异常,并无论转谱成功失败、总是打印出Alert信息:(下面例子以Simai → MA2为例,如果反过来转则直接更换Parser和Generator即可)
114114
```csharp
115115
using System.Text;
@@ -135,7 +135,7 @@ catch (ConversionException e)
135135
throw;
136136
}
137137
finally
138-
{
138+
{ // 无论转换成功还是失败,都打印出Alert信息
139139
foreach (Alert a in alerts) Console.Error.WriteLine(a);
140140
}
141141
```

maidata/Maidata.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using System.Text;
23
using MuConvert.utils;
34

@@ -85,11 +86,40 @@ private void _putKey(string? key, StringBuilder content)
8586
return (levels, infos);
8687
}
8788

88-
public string Title => this.GetValueOrDefault("title", "");
89-
public string Artist => this.GetValueOrDefault("artist", "");
90-
public float? WholeBpm => float.TryParse(this.GetValueOrDefault("wholebpm", ""), out var wholebpm) ? wholebpm : null;
91-
public float First => float.TryParse(this.GetValueOrDefault("first", ""), out var first) ? first : 0f;
92-
public int ClockCount => int.TryParse(this.GetValueOrDefault("clock_count", ""), out var clockCount) ? clockCount : 4;
89+
public string Title
90+
{
91+
get => this.GetValueOrDefault("title", "");
92+
set => this["title"] = value ?? "";
93+
}
94+
95+
public string Artist
96+
{
97+
get => this.GetValueOrDefault("artist", "");
98+
set => this["artist"] = value ?? "";
99+
}
100+
101+
public float? WholeBpm
102+
{
103+
get => float.TryParse(this.GetValueOrDefault("wholebpm", ""), out var wholebpm) ? wholebpm : null;
104+
set
105+
{
106+
if (value is null) Remove("wholebpm");
107+
else this["wholebpm"] = value.Value.ToString(CultureInfo.InvariantCulture);
108+
}
109+
}
110+
111+
public float First
112+
{
113+
get => float.TryParse(this.GetValueOrDefault("first", ""), out var first) ? first : 0f;
114+
set => this["first"] = $"{value:0.####}";
115+
}
116+
117+
public int ClockCount
118+
{
119+
get => int.TryParse(this.GetValueOrDefault("clock_count", ""), out var clockCount) ? clockCount : 4;
120+
set => this["clock_count"] = value.ToString();
121+
}
122+
93123
public (float, float?)? Demo
94124
{
95125
get
@@ -98,6 +128,20 @@ private void _putKey(string? key, StringBuilder content)
98128
float? demoLen = float.TryParse(this.GetValueOrDefault("demo_len", ""), out var v) ? v : null;
99129
return (demoStart, demoLen);
100130
}
131+
set
132+
{
133+
if (value is null)
134+
{
135+
Remove("demo_seek");
136+
Remove("demo_len");
137+
return;
138+
}
139+
140+
var (start, len) = value.Value;
141+
this["demo_seek"] = $"{start:0.####}";
142+
if (len is null) Remove("demo_len");
143+
else this["demo_len"] = $"{len:0.####}";
144+
}
101145
}
102146

103147
public override string ToString()

0 commit comments

Comments
 (0)