public static FileContent GetFileContent(string filename) { try { if (FileContentCache.TryGetValue(filename, out var filecontent)) { return(filecontent); } if (!File.Exists(filename)) { return(null); } filecontent = new FileContent() { Lines = File.ReadAllLines(filename), Text = File.ReadAllText(filename), Watcher = new FileSystemWatcher(Path.GetDirectoryName(filename), Path.GetFileName(filename)) }; filecontent.Watcher.Renamed += (S, A) => { filecontent.Watcher.EnableRaisingEvents = false; FileContentCache.TryRemove(filename, out var _); }; filecontent.Watcher.Changed += (S, A) => { filecontent.Watcher.EnableRaisingEvents = false; FileContentCache.TryRemove(filename, out var _); }; filecontent.Watcher.Deleted += (S, A) => { filecontent.Watcher.EnableRaisingEvents = false; FileContentCache.TryRemove(filename, out var _); }; filecontent.Watcher.Created += (S, A) => { filecontent.Watcher.EnableRaisingEvents = false; FileContentCache.TryRemove(filename, out var _); }; filecontent.Watcher.EnableRaisingEvents = true; FileContentCache.AddOrUpdate(filename, filecontent, (S, O) => filecontent); return(filecontent); } catch (Exception error) { EmpyrionScripting.Log($"Filename: {filename} => {error}", EmpyrionNetAPIDefinitions.LogLevel.Message); return(null); } }
protected T SafeGet <T>(string func, object check, Func <T> getFunction) { try { return(check == null ? default : getFunction()); } catch (Exception error) { EmpyrionScripting.Log($"ReadFailed({func}): {error}", EmpyrionNetAPIDefinitions.LogLevel.Debug); return(default);
private static bool ExtractBlockToContainer(IEntityData E, Dictionary <int, double> ressources, int blockId) { EmpyrionScripting.ConfigEcfAccess.FlatConfigBlockById.TryGetValue(blockId, out var blockData); if (!EmpyrionScripting.ConfigEcfAccess.ResourcesForBlockById.TryGetValue(blockId, out var recipe)) { if (blockData?.Values != null && blockData.Values.ContainsKey("Name")) { string parentBlockName = null; if (EmpyrionScripting.ConfigEcfAccess.ParentBlockName.TryGetValue(PlaceAtType(E.EntityType) + blockData.Values["Name"].ToString(), out var parentBlockName1)) { parentBlockName = parentBlockName1; } if (EmpyrionScripting.ConfigEcfAccess.ParentBlockName.TryGetValue(blockData.Values["Name"].ToString(), out var parentBlockName2)) { parentBlockName = parentBlockName2; } if (parentBlockName != null && EmpyrionScripting.ConfigEcfAccess.ResourcesForBlockById.TryGetValue(EmpyrionScripting.ConfigEcfAccess.BlockIdMapping[parentBlockName], out var parentRecipe)) { recipe = parentRecipe; } } if (recipe == null) { EmpyrionScripting.Log($"No recipe for {blockId}:{(EmpyrionScripting.ConfigEcfAccess.FlatConfigBlockById.TryGetValue(blockId, out var noRecipeBlock) ? noRecipeBlock.Values["Name"] : "")}", LogLevel.Message); return(false); } } EmpyrionScripting.Log($"Recipe for [{blockId}] {blockData?.Values["Name"]}: {recipe.Aggregate("", (r, i) => $"{r}\n{i.Key}:{i.Value}")}", LogLevel.Debug); recipe.ForEach(R => { if (ressources.TryGetValue(R.Key, out var count)) { ressources[R.Key] = count + R.Value; } else { ressources.Add(R.Key, R.Value); } }); return(false); }