BlockLoader.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Text.Json;
  2. using Microsoft.Extensions.Configuration;
  3. using System.IO;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text.RegularExpressions;
  7. namespace gameapi.Library
  8. {
  9. public class BlockLoader : IBlockLoader
  10. {
  11. private string path;
  12. public BlockLoader()
  13. {
  14. path = GlobalConfiguration.Get().GetSection("Slackbot").GetValue<string>("BlocksPath");
  15. }
  16. public dynamic Read(string filename, Dictionary<string, string> replaceKeys = null)
  17. {
  18. if (replaceKeys == null)
  19. {
  20. replaceKeys = new Dictionary<string, string>();
  21. }
  22. var filePath = path + "/" + filename;
  23. var text = File.ReadAllText(filePath);
  24. foreach (var replacement in replaceKeys)
  25. {
  26. text = text.Replace("{" + replacement.Key + "}", replacement.Value, StringComparison.InvariantCultureIgnoreCase);
  27. }
  28. var blocks = JsonSerializer.Deserialize<JsonElement>(text);
  29. return blocks;
  30. }
  31. }
  32. public interface IBlockLoader
  33. {
  34. dynamic Read(string filename, Dictionary<string, string> replaceKeys = null);
  35. }
  36. }