using System.Collections.Generic; using System.IO; using Godot; using FileAccess = Godot.FileAccess; namespace ColdMint.scripts; /// /// SloganProvider /// 标语提供器 /// public static class SloganProvider { private static string _csvPath = "res://locals/slogan.csv"; private static string[]? _sloganKeys; /// /// Loading CSV file /// 加载CSV文件 /// public static void LoadSloganCsv() { var exists = FileAccess.FileExists(_csvPath); if (!exists) { return; } using var file = FileAccess.Open(_csvPath, FileAccess.ModeFlags.Read); var content = file.GetAsText(); var lineStrings = content.Split('\n'); var keys = new List(); foreach (var lineString in lineStrings) { var index = lineString.IndexOf(','); if (index > -1) { keys.Add(lineString[..index]); } } if (keys.Count > 0) { keys.RemoveAt(0); } _sloganKeys = keys.ToArray(); } /// /// Swipe the machine to get a slogan /// 刷机获取一个标语 /// /// public static string? GetSlogan() { if (_sloganKeys == null || _sloganKeys.Length == 0) { return null; } return TranslationServer.Translate(_sloganKeys[GD.Randi() % _sloganKeys.Length]); } }