From a4c5b119c5abc57b5d89446f9561990454408c41 Mon Sep 17 00:00:00 2001 From: SzymonKaminski Date: Sat, 9 Nov 2024 12:48:21 +0100 Subject: [PATCH] Cache property info in initial sdb load (#53) --- .../StaticDB/Loaders/StaticDBLoader.cs | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/UdpHosts/GameServer/StaticDB/Loaders/StaticDBLoader.cs b/UdpHosts/GameServer/StaticDB/Loaders/StaticDBLoader.cs index 8aad194..6700a9f 100644 --- a/UdpHosts/GameServer/StaticDB/Loaders/StaticDBLoader.cs +++ b/UdpHosts/GameServer/StaticDB/Loaders/StaticDBLoader.cs @@ -1315,36 +1315,44 @@ private static T[] LoadStaticDB(string tableName) Table table = sdb.GetTableByName(tableName); var list = new List(); + var properties = typeof(T).GetProperties() + .Select(propInfo => + { + var convertedName = Policy.ConvertName(propInfo.Name); + var index = table.GetColumnIndexByName(convertedName); + + if (index == -1 && ManualNameConversions.TryGetValue(propInfo.Name, out var value)) + { + index = table.GetColumnIndexByName(value); + } + + if (index == -1) + { + index = table.GetColumnIndexByName(propInfo.Name); + } + + return new { PropInfo = propInfo, ConvertedName = convertedName, Index = index, }; + }).ToList(); + foreach(Row row in table.Rows) { T entry = new T(); - foreach (var propInfo in entry.GetType().GetProperties()) + foreach (var prop in properties) { - string convertedName = Policy.ConvertName(propInfo.Name); - int index = table.GetColumnIndexByName(convertedName); - int backupIndex = table.GetColumnIndexByName(propInfo.Name); try { - if (index != -1) - { - propInfo.SetValue(entry, row[index], null); - } - else if (backupIndex != -1) - { - propInfo.SetValue(entry, row[backupIndex], null); - } - else if (ManualNameConversions.TryGetValue(propInfo.Name, out var value)) + if (prop.Index != -1) { - propInfo.SetValue(entry, row[table.GetColumnIndexByName(value)], null); + prop.PropInfo.SetValue(entry, row[prop.Index], null); } else { - warningsSet.Add($"Could not find column for {propInfo.Name} (converted to {convertedName}) in {tableName}"); + warningsSet.Add($"Could not find column for {prop.PropInfo.Name} (converted to {prop.ConvertedName}) in {tableName}"); } } catch (Exception ex) { - Console.WriteLine($"Exception when loading {tableName}, {propInfo.Name}: {ex.Message}"); + Console.WriteLine($"Exception when loading {tableName}, {prop.PropInfo.Name}: {ex.Message}"); } }