Skip to content

Commit e2488df

Browse files
committed
fix: 修复数字解析区域依赖和 tick 缩放溢出问题
UgcParser/SusParser: TryParse 统一使用 CultureInfo.InvariantCulture UgcGenerator/SusGenerator: ScaleUp 乘法提升为 long 防溢出
1 parent 54e5c27 commit e2488df

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

generator/chu/SusGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static SusChart ConvertToSus(IChuChart chart, List<Alert> alerts)
5252

5353
private static ChuNote ScaleUp(ChuNote n)
5454
{
55-
int s(int v) => v * SusTpb / (C2sRsl / 4);
55+
int s(int v) => (int)((long)v * SusTpb / (C2sRsl / 4));
5656
return new ChuNote
5757
{
5858
Type = n.Type, Measure = n.Measure, Offset = s(n.Offset),

generator/chu/UgcGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static UgcChart ConvertToUgc(IChuChart chart, List<Alert> alerts)
5151

5252
private static ChuNote ScaleUpNote(ChuNote n)
5353
{
54-
int s(int v) => v * UgcTicksPerBeat / (C2sResolution / 4);
54+
int s(int v) => (int)((long)v * UgcTicksPerBeat / (C2sResolution / 4));
5555
return new ChuNote
5656
{
5757
Type = n.Type, Measure = n.Measure, Offset = s(n.Offset),
@@ -64,7 +64,7 @@ private static ChuNote ScaleUpNote(ChuNote n)
6464
};
6565
}
6666

67-
private static int ScaleUp(int v) => v * UgcTicksPerBeat / (C2sResolution / 4);
67+
private static int ScaleUp(int v) => (int)((long)v * UgcTicksPerBeat / (C2sResolution / 4));
6868

6969
private static bool IsAir(string t) => t is "AIR" or "AUR" or "AUL" or "AHD" or "ADW" or "ADR" or "ADL";
7070

parser/chu/SusParser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using MuConvert.chart;
23
using MuConvert.parser;
34
using MuConvert.utils;
@@ -82,15 +83,15 @@ private static void ParseHeaderLine(string content, SusChart chart, List<Alert>
8283
else if (content.StartsWith("BPM_DEF "))
8384
{
8485
var bpmStr = content[8..].Trim().Trim('"');
85-
if (double.TryParse(bpmStr, out var bpm))
86+
if (double.TryParse(bpmStr, NumberStyles.Float, CultureInfo.InvariantCulture, out var bpm))
8687
chart.Bpm = bpm;
8788
else
8889
alerts.Add(new Alert(Warning, $"BPM_DEF 格式错误: {content}") { Line = lineNum });
8990
}
9091
else if (content.StartsWith("REQUEST "))
9192
{
9293
var reqStr = content[8..].Trim().Trim('"');
93-
if (int.TryParse(reqStr, out var ticks))
94+
if (int.TryParse(reqStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out var ticks))
9495
chart.TicksPerBeat = ticks;
9596
else
9697
alerts.Add(new Alert(Warning, $"REQUEST 格式错误: {content}") { Line = lineNum });

parser/chu/UgcParser.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using MuConvert.chart;
23
using MuConvert.parser;
34
using MuConvert.utils;
@@ -90,7 +91,7 @@ private static void ParseHeaderLine(string line, UgcChart chart, List<Alert> ale
9091
break;
9192

9293
case "@DIFF":
93-
if (int.TryParse(value, out var diff))
94+
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var diff))
9495
{
9596
chart.Difficulty = diff switch
9697
{
@@ -109,14 +110,14 @@ private static void ParseHeaderLine(string line, UgcChart chart, List<Alert> ale
109110
break;
110111

111112
case "@LEVEL":
112-
if (int.TryParse(value, out var level))
113+
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var level))
113114
chart.Level = level;
114115
else
115116
alerts.Add(new Alert(Warning, $"@LEVEL 格式错误: {line}") { Line = lineNum });
116117
break;
117118

118119
case "@CONST":
119-
if (double.TryParse(value, out var constant))
120+
if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var constant))
120121
chart.Constant = constant;
121122
else
122123
alerts.Add(new Alert(Warning, $"@CONST 格式错误: {line}") { Line = lineNum });
@@ -127,7 +128,7 @@ private static void ParseHeaderLine(string line, UgcChart chart, List<Alert> ale
127128
break;
128129

129130
case "@TICKS":
130-
if (int.TryParse(value, out var ticks))
131+
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var ticks))
131132
chart.TicksPerBeat = ticks;
132133
else
133134
alerts.Add(new Alert(Warning, $"@TICKS 格式错误: {line}") { Line = lineNum });
@@ -136,9 +137,9 @@ private static void ParseHeaderLine(string line, UgcChart chart, List<Alert> ale
136137
case "@BEAT":
137138
var beatParts = value.Split(' ');
138139
if (beatParts.Length >= 3
139-
&& int.TryParse(beatParts[0], out var beatMeasure)
140-
&& int.TryParse(beatParts[1], out var beatNum)
141-
&& int.TryParse(beatParts[2], out var beatDen))
140+
&& int.TryParse(beatParts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out var beatMeasure)
141+
&& int.TryParse(beatParts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var beatNum)
142+
&& int.TryParse(beatParts[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out var beatDen))
142143
{
143144
chart.BeatEvents.Add((beatMeasure, beatNum, beatDen));
144145
}
@@ -157,9 +158,9 @@ private static void ParseHeaderLine(string line, UgcChart chart, List<Alert> ale
157158
var bpmValueStr = bpmPart[(bpmSpaceIdx + 1)..];
158159
var apostropheIdx = measureOffset.IndexOf('\'');
159160
if (apostropheIdx > 0
160-
&& int.TryParse(measureOffset[..apostropheIdx], out var bpmMeasure)
161-
&& int.TryParse(measureOffset[(apostropheIdx + 1)..], out var bpmOffset)
162-
&& double.TryParse(bpmValueStr, out var bpmValue))
161+
&& int.TryParse(measureOffset[..apostropheIdx], NumberStyles.Integer, CultureInfo.InvariantCulture, out var bpmMeasure)
162+
&& int.TryParse(measureOffset[(apostropheIdx + 1)..], NumberStyles.Integer, CultureInfo.InvariantCulture, out var bpmOffset)
163+
&& double.TryParse(bpmValueStr, NumberStyles.Float, CultureInfo.InvariantCulture, out var bpmValue))
163164
{
164165
chart.BpmEvents.Add((bpmMeasure, bpmOffset, bpmValue));
165166
}
@@ -202,12 +203,12 @@ private static int ParseNoteLine(string[] lines, int idx, UgcChart chart, List<A
202203
return idx;
203204
}
204205

205-
if (!int.TryParse(prefix[(hashIdx + 1)..apostropheIdx], out var measure))
206+
if (!int.TryParse(prefix[(hashIdx + 1)..apostropheIdx], NumberStyles.Integer, CultureInfo.InvariantCulture, out var measure))
206207
{
207208
alerts.Add(new Alert(Warning, $"无法解析 measure: {line}") { Line = lineNum });
208209
return idx;
209210
}
210-
if (!int.TryParse(prefix[(apostropheIdx + 1)..], out var tick))
211+
if (!int.TryParse(prefix[(apostropheIdx + 1)..], NumberStyles.Integer, CultureInfo.InvariantCulture, out var tick))
211212
{
212213
alerts.Add(new Alert(Warning, $"无法解析 tick: {line}") { Line = lineNum });
213214
return idx;
@@ -322,7 +323,7 @@ private static bool TryParseFollowerLine(string line, out int duration, out int
322323
if (gtSIdx < 1) return false;
323324

324325
var durationStr = line[1..gtSIdx];
325-
if (!int.TryParse(durationStr, out duration)) return false;
326+
if (!int.TryParse(durationStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out duration)) return false;
326327

327328
var afterMarker = line[(gtSIdx + 2)..];
328329
if (afterMarker.Length >= 2)
@@ -386,7 +387,7 @@ private static void ParseAirNote(string code, ChuNote note, List<Alert> alerts,
386387
if (underscoreIdx >= 0 && note.Type == "AHD")
387388
{
388389
var durStr = remaining[(underscoreIdx + 1)..];
389-
if (int.TryParse(durStr, out var ahdDuration))
390+
if (int.TryParse(durStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out var ahdDuration))
390391
note.AirHoldDuration = ahdDuration;
391392
}
392393
}
@@ -428,3 +429,4 @@ private static string FormatNoteRef(ChuNote note)
428429
return $"#{note.Measure}'{note.Offset}:{note.Type}";
429430
}
430431
}
432+

0 commit comments

Comments
 (0)