using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using UnityEditor; public sealed class S3UploadCredentials { public string AccessKey; public string SecretKey; public string SessionToken; } public static class S3UploadSettings { const string MissingConfigurationMessage = "请先填写并保存 S3 上传配置(Bucket、Region、Access Key 和 Secret Key)。"; public const string Profile = "app-runtimes"; public static string Bucket => EditorPrefs.GetString("AppRuntimes.S3.Bucket", "ios-games"); public static string Region => EditorPrefs.GetString("AppRuntimes.S3.Region", "us-east-1"); public static string CredentialsPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".aws", "credentials"); public static S3UploadCredentials LoadCredentials() => ParseProfile(File.Exists(CredentialsPath) ? File.ReadAllText(CredentialsPath) : "", Profile); public static S3UploadCredentials ParseProfile(string text, string profile) { var credentials = new S3UploadCredentials(); bool selected = false; foreach (var raw in text.Split('\n')) { var line = raw.Trim(); if (line.StartsWith("[") && line.EndsWith("]")) { selected = line == "[" + profile + "]"; continue; } if (!selected || line.StartsWith("#") || line.StartsWith(";")) continue; int separator = line.IndexOf('='); if (separator < 0) continue; var key = line.Substring(0, separator).Trim(); var value = line.Substring(separator + 1).Trim(); if (key == "aws_access_key_id") credentials.AccessKey = value; if (key == "aws_secret_access_key") credentials.SecretKey = value; if (key == "aws_session_token") credentials.SessionToken = value; } return credentials; } public static string UpdateProfile(string text, string profile, S3UploadCredentials credentials) { if (!Regex.IsMatch(profile, @"\A[a-zA-Z0-9_-]+\z")) throw new ArgumentException("Invalid AWS profile name."); ValidateCredentials(credentials); var output = new StringBuilder(); bool selected = false; foreach (var raw in text.Split('\n')) { var line = raw.Trim(); if (line.StartsWith("[") && line.EndsWith("]")) selected = line == "[" + profile + "]"; if (!selected) output.AppendLine(raw.TrimEnd('\r')); } output.AppendLine("[" + profile + "]"); output.AppendLine("aws_access_key_id = " + credentials.AccessKey.Trim()); output.AppendLine("aws_secret_access_key = " + credentials.SecretKey.Trim()); if (!string.IsNullOrWhiteSpace(credentials.SessionToken)) output.AppendLine("aws_session_token = " + credentials.SessionToken.Trim()); return output.ToString(); } public static void ValidateCredentials(S3UploadCredentials credentials) { if (credentials == null || string.IsNullOrWhiteSpace(credentials.AccessKey) || string.IsNullOrWhiteSpace(credentials.SecretKey)) throw new InvalidOperationException(MissingConfigurationMessage); foreach (var value in new[] { credentials.AccessKey, credentials.SecretKey, credentials.SessionToken }) if (value != null && (value.Contains("\n") || value.Contains("\r"))) throw new ArgumentException("AWS credentials must not contain line breaks."); } public static void ValidateDestination(string bucket, string region) { if (string.IsNullOrWhiteSpace(bucket) || string.IsNullOrWhiteSpace(region)) throw new ArgumentException(MissingConfigurationMessage); if (bucket == null || !Regex.IsMatch(bucket, @"\A[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]\z") || bucket.Contains("..")) throw new ArgumentException("S3 Bucket 格式不正确。"); if (region == null || !Regex.IsMatch(region, @"\A[a-z]{2}(?:-[a-z]+)+-\d+\z")) throw new ArgumentException("S3 Region 格式不正确。"); } public static void Save(string bucket, string region, S3UploadCredentials credentials) { bucket = bucket.Trim(); region = region.Trim(); ValidateDestination(bucket, region); var content = UpdateProfile(File.Exists(CredentialsPath) ? File.ReadAllText(CredentialsPath) : "", Profile, credentials); Directory.CreateDirectory(Path.GetDirectoryName(CredentialsPath)); var temporary = CredentialsPath + "." + Guid.NewGuid().ToString("N") + ".tmp"; try { File.WriteAllText(temporary, content, new UTF8Encoding(false)); if (File.Exists(CredentialsPath)) File.Replace(temporary, CredentialsPath, null); else File.Move(temporary, CredentialsPath); } finally { if (File.Exists(temporary)) File.Delete(temporary); } EditorPrefs.SetString("AppRuntimes.S3.Bucket", bucket); EditorPrefs.SetString("AppRuntimes.S3.Region", region); } }