using System.Globalization; using System.Xml.Linq; using Microsoft.Data.SqlClient; namespace XmlTool.Importers { public sealed class BuffAssetImportSummary { public int RecordCount { get; init; } public int DeletedCount { get; init; } public int InsertedCount { get; init; } } public static class BuffAssetImporter { public static BuffAssetImportSummary ValidateXml(string xmlPath) { List records = LoadRecords(xmlPath); return new BuffAssetImportSummary { RecordCount = records.Count }; } public static BuffAssetImportSummary ImportReplace(string connectionString, string xmlPath) { List records = LoadRecords(xmlPath); if (records.Count == 0) { throw new InvalidOperationException("Buff.xml does not contain any Buff records."); } using SqlConnection connection = new(connectionString); connection.Open(); using SqlTransaction transaction = connection.BeginTransaction(); try { int deletedCount = ExecuteScalarInt(connection, transaction, "SELECT COUNT(*) FROM [Asset].[Buff];"); ExecuteNonQuery(connection, transaction, "DELETE FROM [Asset].[Buff];"); ExecuteNonQuery(connection, transaction, "SET IDENTITY_INSERT [Asset].[Buff] ON;"); using SqlCommand insertCommand = new(@" INSERT INTO [Asset].[Buff] ([Id], [BuffId], [Name], [DigimonSkillCode], [SkillCode], [MinLevel], [ConditionLevel], [Class], [Type], [LifeType], [TimeType]) VALUES (@Id, @BuffId, @Name, @DigimonSkillCode, @SkillCode, @MinLevel, @ConditionLevel, @Class, @Type, @LifeType, @TimeType);", connection, transaction); AddParameter(insertCommand, "@Id"); AddParameter(insertCommand, "@BuffId"); AddParameter(insertCommand, "@Name"); AddParameter(insertCommand, "@DigimonSkillCode"); AddParameter(insertCommand, "@SkillCode"); AddParameter(insertCommand, "@MinLevel"); AddParameter(insertCommand, "@ConditionLevel"); AddParameter(insertCommand, "@Class"); AddParameter(insertCommand, "@Type"); AddParameter(insertCommand, "@LifeType"); AddParameter(insertCommand, "@TimeType"); int identity = 1; foreach (BuffAssetRecord record in records) { insertCommand.Parameters["@Id"].Value = identity++; insertCommand.Parameters["@BuffId"].Value = record.BuffId; insertCommand.Parameters["@Name"].Value = record.Name; insertCommand.Parameters["@DigimonSkillCode"].Value = record.DigimonSkillCode; insertCommand.Parameters["@SkillCode"].Value = record.SkillCode; insertCommand.Parameters["@MinLevel"].Value = record.MinLevel; insertCommand.Parameters["@ConditionLevel"].Value = record.ConditionLevel; insertCommand.Parameters["@Class"].Value = record.Class; insertCommand.Parameters["@Type"].Value = record.Type; insertCommand.Parameters["@LifeType"].Value = record.LifeType; insertCommand.Parameters["@TimeType"].Value = record.TimeType; insertCommand.ExecuteNonQuery(); } ExecuteNonQuery(connection, transaction, "SET IDENTITY_INSERT [Asset].[Buff] OFF;"); ExecuteNonQuery(connection, transaction, $"DBCC CHECKIDENT(N'[Asset].[Buff]', RESEED, {records.Count});"); transaction.Commit(); return new BuffAssetImportSummary { RecordCount = records.Count, DeletedCount = deletedCount, InsertedCount = records.Count }; } catch { TryDisableIdentityInsert(connection, transaction); transaction.Rollback(); throw; } } private static void TryDisableIdentityInsert(SqlConnection connection, SqlTransaction transaction) { try { ExecuteNonQuery(connection, transaction, "SET IDENTITY_INSERT [Asset].[Buff] OFF;"); } catch { // Best-effort cleanup before rollback. } } private static int ExecuteScalarInt(SqlConnection connection, SqlTransaction transaction, string sql) { using SqlCommand command = new(sql, connection, transaction); object? result = command.ExecuteScalar(); return Convert.ToInt32(result, CultureInfo.InvariantCulture); } private static void ExecuteNonQuery(SqlConnection connection, SqlTransaction transaction, string sql) { using SqlCommand command = new(sql, connection, transaction); command.ExecuteNonQuery(); } private static void AddParameter(SqlCommand command, string name) { command.Parameters.Add(new SqlParameter(name, DBNull.Value)); } private static List LoadRecords(string xmlPath) { if (!File.Exists(xmlPath)) { throw new FileNotFoundException("Buff.xml not found.", xmlPath); } XDocument document = XDocument.Load(xmlPath); XElement? root = document.Root; if (root == null || root.Name.LocalName != "BuffTable") { throw new InvalidOperationException("Buff.xml root node must be ."); } List records = new(); int rowIndex = 0; foreach (XElement buffElement in root.Elements("Buff")) { rowIndex++; string recordContext = BuildRecordContext(buffElement, rowIndex); records.Add(new BuffAssetRecord { BuffId = ReadRequiredInt(buffElement, "s_dwID", recordContext), Name = ReadOptionalString(buffElement, "s_szName", 200), DigimonSkillCode = ReadRequiredInt(buffElement, "s_dwDigimonSkillCode", recordContext), SkillCode = ReadRequiredInt(buffElement, "s_dwSkillCode", recordContext), MinLevel = ReadRequiredInt(buffElement, "s_nMinLv", recordContext), ConditionLevel = ReadRequiredInt(buffElement, "s_nConditionLv", recordContext), Class = ReadRequiredShort(buffElement, "s_nBuffClass", recordContext), Type = ReadRequiredInt(buffElement, "s_nBuffType", recordContext), LifeType = ReadRequiredInt(buffElement, "s_nBuffLifeType", recordContext), TimeType = ReadRequiredInt(buffElement, "s_nBuffTimeType", recordContext) }); } return records; } private static string BuildRecordContext(XElement buffElement, int rowIndex) { string buffId = buffElement.Element("s_dwID")?.Value?.Trim() ?? "?"; string name = buffElement.Element("s_szName")?.Value?.Trim() ?? string.Empty; if (string.IsNullOrWhiteSpace(name)) { return $"Buff row {rowIndex} (BuffId={buffId})"; } return $"Buff row {rowIndex} (BuffId={buffId}, Name={name})"; } private static int ReadRequiredInt(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!int.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) { throw new InvalidOperationException($"{recordContext} has invalid integer value in '{elementName}': '{element.Value}'."); } return value; } private static short ReadRequiredShort(XElement parent, string elementName, string recordContext) { int value = ReadRequiredInt(parent, elementName, recordContext); if (value < short.MinValue || value > short.MaxValue) { throw new InvalidOperationException($"{recordContext} has out-of-range short value in '{elementName}': '{value}'."); } return (short)value; } private static string ReadOptionalString(XElement parent, string elementName, int maxLength) { string value = parent.Element(elementName)?.Value?.Trim() ?? string.Empty; if (value.Length <= maxLength) { return value; } return value.Substring(0, maxLength); } private sealed class BuffAssetRecord { public int BuffId { get; init; } public string Name { get; init; } = string.Empty; public int DigimonSkillCode { get; init; } public int SkillCode { get; init; } public int MinLevel { get; init; } public int ConditionLevel { get; init; } public short Class { get; init; } public int Type { get; init; } public int LifeType { get; init; } public int TimeType { get; init; } } } }