using System.Globalization; using System.Xml.Linq; using Microsoft.Data.SqlClient; namespace XmlTool.Importers { public sealed class DMBaseStatusApplyImportSummary { public int RecordCount { get; init; } public int TamerBaseStatusRecordCount { get; init; } public int TamerLevelStatusRecordCount { get; init; } public int DigimonLevelStatusRecordCount { get; init; } public int DeletedCount { get; init; } public int InsertedCount { get; init; } public int DeletedTamerBaseStatusCount { get; init; } public int DeletedTamerLevelStatusCount { get; init; } public int DeletedDigimonLevelStatusCount { get; init; } public int InsertedTamerBaseStatusCount { get; init; } public int InsertedTamerLevelStatusCount { get; init; } public int InsertedDigimonLevelStatusCount { get; init; } } public static class DMBaseStatusApplyImporter { public static DMBaseStatusApplyImportSummary ValidateXml(string xmlPath) { List records = LoadRecords(xmlPath); List tamerRecords = LoadTamerLevelStatusRecords(ResolveSiblingXmlPath(xmlPath, "TamerBaseInfo.xml")); List digimonRecords = LoadDigimonLevelStatusRecords(ResolveSiblingXmlPath(xmlPath, "DigimonBaseInfo.xml")); return new DMBaseStatusApplyImportSummary { RecordCount = records.Count, TamerBaseStatusRecordCount = tamerRecords.Select(x => x.Type).Distinct().Count(), TamerLevelStatusRecordCount = tamerRecords.Count, DigimonLevelStatusRecordCount = digimonRecords.Count }; } public static DMBaseStatusApplyImportSummary ImportReplace(string connectionString, string xmlPath) { List records = LoadRecords(xmlPath); List tamerRecords = LoadTamerLevelStatusRecords(ResolveSiblingXmlPath(xmlPath, "TamerBaseInfo.xml")); List digimonRecords = LoadDigimonLevelStatusRecords(ResolveSiblingXmlPath(xmlPath, "DigimonBaseInfo.xml")); List tamerBaseRecords = BuildTamerBaseStatusRecords(tamerRecords); if (records.Count == 0) { throw new InvalidOperationException("EvolutionBaseApply.xml does not contain any EvolutionBaseApply records."); } using SqlConnection connection = new(connectionString); connection.Open(); using SqlTransaction transaction = connection.BeginTransaction(); try { int deletedCount = ExecuteScalarInt(connection, transaction, "SELECT COUNT(*) FROM [Asset].[StatusApply];"); int deletedTamerLevelStatusCount = DeleteRowsForTypes(connection, transaction, "[Asset].[CharacterLevelStatus]", tamerRecords.Select(x => x.Type)); int deletedTamerBaseStatusCount = DeleteRowsForTypes(connection, transaction, "[Asset].[CharacterBaseStatus]", tamerBaseRecords.Select(x => x.Type)); int deletedDigimonLevelStatusCount = DeleteRowsForTypes(connection, transaction, "[Asset].[DigimonLevelStatus]", digimonRecords.Select(x => x.Type)); ExecuteNonQuery(connection, transaction, "DELETE FROM [Asset].[StatusApply];"); ExecuteNonQuery(connection, transaction, "SET IDENTITY_INSERT [Asset].[StatusApply] ON;"); using SqlCommand insertCommand = new(@" INSERT INTO [Asset].[StatusApply] ([Id], [StageValue], [ApplyValue]) VALUES (@Id, @StageValue, @ApplyValue);", connection, transaction); AddParameter(insertCommand, "@Id"); AddParameter(insertCommand, "@StageValue"); AddParameter(insertCommand, "@ApplyValue"); int identity = 1; foreach (StatusApplyRecord record in records) { insertCommand.Parameters["@Id"].Value = identity++; insertCommand.Parameters["@StageValue"].Value = record.StageValue; insertCommand.Parameters["@ApplyValue"].Value = record.ApplyValue; insertCommand.ExecuteNonQuery(); } ExecuteNonQuery(connection, transaction, "SET IDENTITY_INSERT [Asset].[StatusApply] OFF;"); ExecuteNonQuery(connection, transaction, $"DBCC CHECKIDENT(N'[Asset].[StatusApply]', RESEED, {records.Count});"); InsertTamerBaseStatus(connection, transaction, tamerBaseRecords); InsertTamerLevelStatus(connection, transaction, tamerRecords); InsertDigimonLevelStatus(connection, transaction, digimonRecords); transaction.Commit(); return new DMBaseStatusApplyImportSummary { RecordCount = records.Count, TamerBaseStatusRecordCount = tamerBaseRecords.Count, TamerLevelStatusRecordCount = tamerRecords.Count, DigimonLevelStatusRecordCount = digimonRecords.Count, DeletedCount = deletedCount, InsertedCount = records.Count, DeletedTamerBaseStatusCount = deletedTamerBaseStatusCount, DeletedTamerLevelStatusCount = deletedTamerLevelStatusCount, DeletedDigimonLevelStatusCount = deletedDigimonLevelStatusCount, InsertedTamerBaseStatusCount = tamerBaseRecords.Count, InsertedTamerLevelStatusCount = tamerRecords.Count, InsertedDigimonLevelStatusCount = digimonRecords.Count }; } catch { TryDisableIdentityInsert(connection, transaction); transaction.Rollback(); throw; } } private static string ResolveSiblingXmlPath(string xmlPath, string fileName) { string? directory = Path.GetDirectoryName(xmlPath); return Path.Combine(directory ?? string.Empty, fileName); } private static List LoadRecords(string xmlPath) { if (!File.Exists(xmlPath)) { throw new FileNotFoundException("EvolutionBaseApply.xml not found.", xmlPath); } XDocument document = XDocument.Load(xmlPath); XElement? root = document.Root; if (root == null || root.Name.LocalName != "EvolutionBaseApplies") { throw new InvalidOperationException("EvolutionBaseApply.xml root node must be ."); } List records = new(); int rowIndex = 0; foreach (XElement applyElement in root.Elements("EvolutionBaseApply")) { rowIndex++; string recordContext = BuildRecordContext(applyElement, rowIndex); records.Add(new StatusApplyRecord { StageValue = ReadRequiredInt(applyElement, "EvolutionType", recordContext), ApplyValue = ReadRequiredInt(applyElement, "EvolutionApplyValue", recordContext) }); } return records; } private static string BuildRecordContext(XElement applyElement, int rowIndex) { string stageValue = applyElement.Element("EvolutionType")?.Value?.Trim() ?? "?"; string name = applyElement.Element("EvolutionName")?.Value?.Trim() ?? string.Empty; if (string.IsNullOrWhiteSpace(name)) { return $"EvolutionBaseApply row {rowIndex} (EvolutionType={stageValue})"; } return $"EvolutionBaseApply row {rowIndex} (EvolutionType={stageValue}, EvolutionName={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 long ReadRequiredLong(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!long.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out long value)) { throw new InvalidOperationException($"{recordContext} has invalid integer value in '{elementName}': '{element.Value}'."); } return value; } private static List LoadTamerLevelStatusRecords(string xmlPath) { if (!File.Exists(xmlPath)) { throw new FileNotFoundException("TamerBaseInfo.xml not found.", xmlPath); } XDocument document = XDocument.Load(xmlPath); XElement? root = document.Root; if (root == null || root.Name.LocalName != "CharacterList") { throw new InvalidOperationException("TamerBaseInfo.xml root node must be ."); } List records = new(); int rowIndex = 0; foreach (XElement element in root.Elements("DMBaseInfo")) { rowIndex++; string recordContext = $"TamerBaseInfo row {rowIndex}"; int sourceId = ReadRequiredInt(element, "Id", recordContext); int level = ReadRequiredInt(element, "Level", recordContext); records.Add(new TamerLevelStatusRecord { Type = 80000 + (sourceId / 1000), Level = level, ExpValue = ReadRequiredLong(element, "Exp", recordContext) / 100, HPValue = ReadRequiredInt(element, "Hp", recordContext), DSValue = ReadRequiredInt(element, "Ds", recordContext), MSValue = 0, DEValue = ReadRequiredInt(element, "De", recordContext), EVValue = ReadRequiredInt(element, "Ev", recordContext), CTValue = ReadRequiredInt(element, "Ct", recordContext), ATValue = ReadRequiredInt(element, "At", recordContext), HTValue = ReadRequiredInt(element, "Ht", recordContext) }); } return records; } private static List LoadDigimonLevelStatusRecords(string xmlPath) { if (!File.Exists(xmlPath)) { throw new FileNotFoundException("DigimonBaseInfo.xml not found.", xmlPath); } XDocument document = XDocument.Load(xmlPath); XElement? root = document.Root; if (root == null || root.Name.LocalName != "DigimonList") { throw new InvalidOperationException("DigimonBaseInfo.xml root node must be ."); } List records = new(); int rowIndex = 0; foreach (XElement element in root.Elements("DigiBaseInfo")) { rowIndex++; string recordContext = $"DigimonBaseInfo row {rowIndex}"; int sourceId = ReadRequiredInt(element, "Id", recordContext); int scaleType = sourceId / 1000; records.Add(new DigimonLevelStatusRecord { Type = 31000 + scaleType, Level = ReadRequiredInt(element, "Level", recordContext), ExpValue = ReadRequiredLong(element, "Exp", recordContext) / 100, HPValue = ReadRequiredInt(element, "Hp", recordContext), DSValue = ReadRequiredInt(element, "Ds", recordContext), MSValue = 0, DEValue = ReadRequiredInt(element, "De", recordContext), EVValue = ReadRequiredInt(element, "Ev", recordContext), CTValue = ReadRequiredInt(element, "Ct", recordContext), ATValue = ReadRequiredInt(element, "At", recordContext), HTValue = ReadRequiredInt(element, "Ht", recordContext), StatusId = sourceId, ScaleType = scaleType }); } return records; } private static List BuildTamerBaseStatusRecords(List tamerRecords) { return tamerRecords .GroupBy(x => x.Type) .Select(group => { TamerLevelStatusRecord levelOne = group.OrderBy(x => x.Level).First(); return new TamerBaseStatusRecord { Type = group.Key, HPValue = levelOne.HPValue, DSValue = levelOne.DSValue, MSValue = ReadBaseMovementSpeed(group.Key, levelOne), DEValue = levelOne.DEValue, ATValue = levelOne.ATValue }; }) .ToList(); } private static int ReadBaseMovementSpeed(int type, TamerLevelStatusRecord levelOne) { return type >= 80014 ? 580 : 550; } private static int DeleteRowsForTypes(SqlConnection connection, SqlTransaction transaction, string tableName, IEnumerable types) { string typeList = string.Join(",", types.Distinct().OrderBy(x => x)); if (string.IsNullOrWhiteSpace(typeList)) { return 0; } using SqlCommand command = new($"DELETE FROM {tableName} WHERE [Type] IN ({typeList});", connection, transaction); return command.ExecuteNonQuery(); } private static void InsertTamerBaseStatus(SqlConnection connection, SqlTransaction transaction, List records) { using SqlCommand command = new(@" INSERT INTO [Asset].[CharacterBaseStatus] ([Type], [ASValue], [ARValue], [ATValue], [BLValue], [CTValue], [DEValue], [DSValue], [EVValue], [HPValue], [HTValue], [MSValue], [WSValue]) VALUES (@Type, 0, 0, @ATValue, 0, 0, @DEValue, @DSValue, 0, @HPValue, 0, @MSValue, 0);", connection, transaction); AddParameter(command, "@Type"); AddParameter(command, "@ATValue"); AddParameter(command, "@DEValue"); AddParameter(command, "@DSValue"); AddParameter(command, "@HPValue"); AddParameter(command, "@MSValue"); foreach (TamerBaseStatusRecord record in records) { command.Parameters["@Type"].Value = record.Type; command.Parameters["@ATValue"].Value = record.ATValue; command.Parameters["@DEValue"].Value = record.DEValue; command.Parameters["@DSValue"].Value = record.DSValue; command.Parameters["@HPValue"].Value = record.HPValue; command.Parameters["@MSValue"].Value = record.MSValue; command.ExecuteNonQuery(); } } private static void InsertTamerLevelStatus(SqlConnection connection, SqlTransaction transaction, List records) { using SqlCommand command = new(@" INSERT INTO [Asset].[CharacterLevelStatus] ([Type], [Level], [ExpValue], [ASValue], [ARValue], [ATValue], [BLValue], [CTValue], [DEValue], [DSValue], [EVValue], [HPValue], [HTValue], [MSValue], [WSValue]) VALUES (@Type, @Level, @ExpValue, 0, 0, @ATValue, 0, @CTValue, @DEValue, @DSValue, @EVValue, @HPValue, @HTValue, @MSValue, 0);", connection, transaction); AddStatusParameters(command); foreach (TamerLevelStatusRecord record in records) { BindStatusParameters(command, record); command.ExecuteNonQuery(); } } private static void InsertDigimonLevelStatus(SqlConnection connection, SqlTransaction transaction, List records) { using SqlCommand command = new(@" INSERT INTO [Asset].[DigimonLevelStatus] ([Type], [Level], [ExpValue], [ASValue], [ARValue], [ATValue], [BLValue], [CTValue], [DEValue], [DSValue], [EVValue], [HPValue], [HTValue], [MSValue], [WSValue], [StatusId], [ScaleType]) VALUES (@Type, @Level, @ExpValue, 0, 0, @ATValue, 0, @CTValue, @DEValue, @DSValue, @EVValue, @HPValue, @HTValue, @MSValue, 0, @StatusId, @ScaleType);", connection, transaction); AddStatusParameters(command); AddParameter(command, "@StatusId"); AddParameter(command, "@ScaleType"); foreach (DigimonLevelStatusRecord record in records) { BindStatusParameters(command, record); command.Parameters["@StatusId"].Value = record.StatusId; command.Parameters["@ScaleType"].Value = record.ScaleType; command.ExecuteNonQuery(); } } private static void AddStatusParameters(SqlCommand command) { AddParameter(command, "@Type"); AddParameter(command, "@Level"); AddParameter(command, "@ExpValue"); AddParameter(command, "@ATValue"); AddParameter(command, "@CTValue"); AddParameter(command, "@DEValue"); AddParameter(command, "@DSValue"); AddParameter(command, "@EVValue"); AddParameter(command, "@HPValue"); AddParameter(command, "@HTValue"); AddParameter(command, "@MSValue"); } private static void BindStatusParameters(SqlCommand command, LevelStatusRecord record) { command.Parameters["@Type"].Value = record.Type; command.Parameters["@Level"].Value = record.Level; command.Parameters["@ExpValue"].Value = record.ExpValue; command.Parameters["@ATValue"].Value = record.ATValue; command.Parameters["@CTValue"].Value = record.CTValue; command.Parameters["@DEValue"].Value = record.DEValue; command.Parameters["@DSValue"].Value = record.DSValue; command.Parameters["@EVValue"].Value = record.EVValue; command.Parameters["@HPValue"].Value = record.HPValue; command.Parameters["@HTValue"].Value = record.HTValue; command.Parameters["@MSValue"].Value = record.MSValue; } 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 void TryDisableIdentityInsert(SqlConnection connection, SqlTransaction transaction) { try { ExecuteNonQuery(connection, transaction, "SET IDENTITY_INSERT [Asset].[StatusApply] OFF;"); } catch { // Best-effort cleanup before rollback. } } private sealed class StatusApplyRecord { public int StageValue { get; init; } public int ApplyValue { get; init; } } private abstract class LevelStatusRecord { public int Type { get; init; } public int Level { get; init; } public long ExpValue { get; init; } public int ATValue { get; init; } public int CTValue { get; init; } public int DEValue { get; init; } public int DSValue { get; init; } public int EVValue { get; init; } public int HPValue { get; init; } public int HTValue { get; init; } public int MSValue { get; init; } } private sealed class TamerLevelStatusRecord : LevelStatusRecord { } private sealed class DigimonLevelStatusRecord : LevelStatusRecord { public int StatusId { get; init; } public int ScaleType { get; init; } } private sealed class TamerBaseStatusRecord { public int Type { get; init; } public int ATValue { get; init; } public int DEValue { get; init; } public int DSValue { get; init; } public int HPValue { get; init; } public int MSValue { get; init; } } } }