using Comal.Classes; using InABox.Core; using InABox.Scripting; namespace PRSDimensionUtils; public static class DimensionUtils { private static Dictionary? _dimensionscriptcache; public static void ResetDimensionScriptCache() => _dimensionscriptcache = null; public static void ReloadDimensionScriptCache(Guid[]? ids, Func?,Columns,CoreTable> reload) { _dimensionscriptcache ??= new Dictionary(); var scripts = reload( ids != null ? new Filter(x => x.ID).InList(ids) : null, Columns.None() .Add(x => x.ID) .Add(x => x.Conversion) ).ToDictionary(x => x.ID, x => x.Conversion); foreach (var id in scripts.Keys) { var doc = !String.IsNullOrWhiteSpace(scripts[id]) ? new ScriptDocument(scripts[id]) : null; if (doc?.Compile() == true) _dimensionscriptcache[id] = doc; else _dimensionscriptcache.Remove(id); } } public static void ConvertDimensions(IDimensions dimensions, ref double quantity, Func?,Columns,CoreTable> reload) { if (quantity.IsEffectivelyEqual(0.0)) return; if (_dimensionscriptcache == null) ReloadDimensionScriptCache(null, reload); if (_dimensionscriptcache?.TryGetValue(dimensions.Unit.ID, out ScriptDocument? script) == true) { script.SetValue("Quantity", quantity); script.SetValue("Dimensions", dimensions); script.Execute("Module", DimensionUnit.ConvertDimensionsMethodName()); quantity = Convert.ToDouble(script.GetValue("Quantity")); } } }