2021-03-31 21:53:35 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using System.IO;
|
|
|
|
using GameServiceWarden.Core.Games;
|
2021-04-08 21:36:08 -05:00
|
|
|
using GameServiceWarden.API.Module;
|
2021-03-31 21:53:35 -05:00
|
|
|
|
|
|
|
namespace GameServiceWarden.Core.Persistence
|
|
|
|
{
|
2021-04-08 21:36:08 -05:00
|
|
|
public class ServiceDescriptorPersistence : IPersistent<ServiceDescriptor>
|
2021-03-31 21:53:35 -05:00
|
|
|
{
|
2021-04-08 21:36:08 -05:00
|
|
|
private readonly IReadOnlyPersistent<IReadOnlyDictionary<string, IServiceModule>> modules;
|
2021-03-31 21:53:35 -05:00
|
|
|
private readonly string mapDirectory;
|
|
|
|
private const string ASSEMBLY_NAME = "Assembly Name";
|
|
|
|
private const string MODULE_NAME = "Module Name";
|
|
|
|
private const string EXTENSION = ".sin";
|
|
|
|
|
|
|
|
|
2021-04-08 21:36:08 -05:00
|
|
|
public ServiceDescriptorPersistence(string mapDirectory, IReadOnlyPersistent<IReadOnlyDictionary<string, IServiceModule>> modules)
|
2021-03-31 21:53:35 -05:00
|
|
|
{
|
|
|
|
this.mapDirectory = mapDirectory;
|
|
|
|
this.modules = modules;
|
|
|
|
}
|
|
|
|
|
2021-04-08 21:36:08 -05:00
|
|
|
public ServiceDescriptor this[string key]
|
2021-03-31 21:53:35 -05:00
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
2021-04-08 21:36:08 -05:00
|
|
|
SaveService(key, value.GetAssemblyName(), value.GetModuleName());
|
2021-03-31 21:53:35 -05:00
|
|
|
}
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (!ContainsKey(key)) throw new KeyNotFoundException();
|
|
|
|
string assemblyName = GetServiceInfoValue(key, ASSEMBLY_NAME);
|
|
|
|
string moduleName = GetServiceInfoValue(key, MODULE_NAME);
|
2021-04-08 21:36:08 -05:00
|
|
|
IService service = modules[assemblyName][moduleName].InstantiateService(GetPathForKey(key), false);
|
|
|
|
return new ServiceDescriptor(service, key, moduleName, assemblyName);
|
2021-03-31 21:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string MapDirectory { get { return mapDirectory; } }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int Count
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Directory.GetFiles(mapDirectory).Length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 21:36:08 -05:00
|
|
|
public IEnumerable<ServiceDescriptor> Values {
|
2021-03-31 21:53:35 -05:00
|
|
|
get {
|
|
|
|
IEnumerable<string> keys = Keys;
|
2021-04-08 21:36:08 -05:00
|
|
|
List<ServiceDescriptor> res = new List<ServiceDescriptor>();
|
2021-03-31 21:53:35 -05:00
|
|
|
foreach (string key in keys)
|
|
|
|
{
|
|
|
|
res.Add(this[key]);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<string> Keys {
|
|
|
|
get {
|
|
|
|
IEnumerable<string> keys = Directory.EnumerateDirectories(mapDirectory);
|
|
|
|
List<string> res = new List<string>();
|
|
|
|
foreach (string key in keys)
|
|
|
|
{
|
|
|
|
res.Add(Path.GetDirectoryName(key));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 21:36:08 -05:00
|
|
|
public void AddToPersistence(string key, ServiceDescriptor value)
|
2021-03-31 21:53:35 -05:00
|
|
|
{
|
|
|
|
if (key == null) throw new ArgumentNullException();
|
|
|
|
if (ContainsKey(key)) throw new ArgumentException();
|
|
|
|
this[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
IEnumerable<string> directories = Keys;
|
|
|
|
foreach (string directory in directories)
|
|
|
|
{
|
|
|
|
Directory.Delete(directory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool ContainsKey(string key)
|
|
|
|
{
|
|
|
|
return Directory.Exists(GetPathForKey(key));
|
|
|
|
}
|
|
|
|
|
2021-04-08 21:36:08 -05:00
|
|
|
public IEnumerator<KeyValuePair<string, ServiceDescriptor>> GetEnumerator()
|
2021-03-31 21:53:35 -05:00
|
|
|
{
|
|
|
|
IEnumerable<string> keys = Keys;
|
2021-04-08 21:36:08 -05:00
|
|
|
List<KeyValuePair<string, ServiceDescriptor>> result = new List<KeyValuePair<string, ServiceDescriptor>>();
|
2021-03-31 21:53:35 -05:00
|
|
|
foreach (string key in keys)
|
|
|
|
{
|
2021-04-08 21:36:08 -05:00
|
|
|
result.Add(new KeyValuePair<string, ServiceDescriptor>(key, this[key]));
|
2021-03-31 21:53:35 -05:00
|
|
|
}
|
|
|
|
return result.GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetPathForKey(string key)
|
|
|
|
{
|
|
|
|
return Path.Combine(mapDirectory, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Delete(string key)
|
|
|
|
{
|
|
|
|
if (!ContainsKey(key)) return false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Directory.Delete(GetPathForKey(key));
|
|
|
|
}
|
|
|
|
catch (System.Exception)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-08 21:36:08 -05:00
|
|
|
public bool TryLoadValue(string key, [MaybeNullWhen(false)] out ServiceDescriptor value)
|
2021-03-31 21:53:35 -05:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
value = this[key];
|
|
|
|
} catch (KeyNotFoundException) {
|
|
|
|
value = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
|
|
|
return GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SaveService(string key, string assemblyName, string moduleName)
|
|
|
|
{
|
|
|
|
if (key == null) throw new ArgumentNullException("key");
|
|
|
|
if (assemblyName == null) throw new ArgumentNullException("assemblyName");
|
|
|
|
if (moduleName == null) throw new ArgumentNullException("moduleName");
|
|
|
|
string serviceInfoPath = GetPathForKey(key);
|
|
|
|
Directory.CreateDirectory(serviceInfoPath);
|
|
|
|
using (StreamWriter writer = File.CreateText(Path.Combine(serviceInfoPath, key + EXTENSION)))
|
|
|
|
{
|
|
|
|
writer.WriteLine($"{ASSEMBLY_NAME}: {assemblyName}");
|
|
|
|
writer.WriteLine($"{MODULE_NAME}: {moduleName}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetServiceInfoValue(string key, string value)
|
|
|
|
{
|
|
|
|
string path = GetPathForKey(key);
|
|
|
|
IEnumerable<string> lines = File.ReadAllLines(Path.Combine(path, key + EXTENSION));
|
|
|
|
foreach (string line in lines)
|
|
|
|
{
|
|
|
|
if (line.StartsWith($"{value}: "))
|
|
|
|
{
|
|
|
|
return line.Substring(value.Length + 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new FormatException($"\"{path}\" is corrupted. Could not find value for: {value}.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|