You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.0 KiB

using Apewer;
using Apewer.Internals;
using System;
using System.Collections.Generic;
using System.Text;
namespace Apewer
{
/// <summary></summary>
public class Configuration
{
private static Json _json = null;
private static bool _loaded = false;
private static Json JsonFile
{
get
{
if (!_loaded) Load();
return _json ?? Json.NewObject();
}
}
private static string GetPath()
{
if (StorageUtility.FileExists("config.json"))
{
return "config.json";
}
if (StorageUtility.FileExists("configuration.json"))
{
return "configuration.json";
}
try
{
var type = typeof(Configuration);
var path = System.IO.Path.GetDirectoryName(type.Assembly.CodeBase);
if (path.StartsWith("file:\\") && path.Length > 6) path = path.Substring(6);
var ad_config = path + "\\..\\app_data\\config.json";
if (StorageUtility.FileExists(ad_config)) return ad_config;
var ad_configuration = path + "\\..\\app_data\\configuration.json";
if (StorageUtility.FileExists(ad_configuration)) return ad_configuration;
var wr_config = path + "\\..\\config.json";
if (StorageUtility.FileExists(ad_config)) return wr_config;
var wr_configuration = path + "\\..\\configuration.json";
if (StorageUtility.FileExists(ad_configuration)) return ad_configuration;
}
catch { }
return null;
}
/// <summary>从默认路径加载配置文件,默认文件名为 config.json 和 configuration.json。</summary>
public static bool Load()
{
var path = GetPath();
if (path != null && StorageUtility.FileExists(path))
{
var text = TextUtility.FromBytes(StorageUtility.ReadFile(path, true));
var loaded = Load(text);
if (loaded) return true;
}
return false;
}
/// <summary>加载配置文件。</summary>
public static bool Load(string json)
{
var text = json;
_json = Json.From(text);
return _json != null;
}
/// <summary>获取配置文件中的属性。</summary>
public static Json GetProperties(Type type)
{
if (type == null) return null;
var result = GetProperties((string)type.FullName);
return result;
}
/// <summary>获取配置文件中的属性。</summary>
public static Json GetProperties(string type)
{
if (TextUtility.IsBlank(type)) return null;
var config = JsonFile;
if (config == null) return null;
return config.GetProperty(type);
}
}
}