1234567891011121314151617181920212223242526272829303132333435363738 |
- using System.Text.Json;
- using Microsoft.Extensions.Configuration;
- using System.IO;
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace gameapi.Library
- {
- public class BlockLoader : IBlockLoader
- {
- private string path;
- public BlockLoader()
- {
- path = GlobalConfiguration.Get().GetSection("Slackbot").GetValue<string>("BlocksPath");
- }
- public dynamic Read(string filename, Dictionary<string, string> replaceKeys = null)
- {
- if (replaceKeys == null)
- {
- replaceKeys = new Dictionary<string, string>();
- }
- var filePath = path + "/" + filename;
- var text = File.ReadAllText(filePath);
- foreach (var replacement in replaceKeys)
- {
- text = text.Replace("{" + replacement.Key + "}", replacement.Value, StringComparison.InvariantCultureIgnoreCase);
- }
- var blocks = JsonSerializer.Deserialize<JsonElement>(text);
- return blocks;
- }
- }
- public interface IBlockLoader
- {
- dynamic Read(string filename, Dictionary<string, string> replaceKeys = null);
- }
- }
|