Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Text;
- using CoCosLog;
- using System.Threading;
- namespace ConfigClass
- {
- public class Config
- {
- public JObject config;
- private Log _log = new Log("Config");
- public Config(string configLocation = null)
- {
- loadConfig(configLocation);
- }
- public void loadConfig(string configLocation)
- {
- string configPath = "";
- if (configLocation == null || configLocation.Length == 0)
- {
- if (IsLinux)
- {
- configPath = @"/etc/cocos/gateway.conf";
- }
- else
- {
- configPath = @"c:\cocos\gateway.conf";
- }
- }
- else
- {
- if (File.Exists(configLocation))
- {
- configPath = configLocation;
- }
- else
- {
- if (Directory.Exists(configLocation))
- {
- if(File.Exists(configLocation + "/gateway.conf"))
- {
- configPath = configLocation + "/gateway.conf";
- }
- else if(File.Exists(configLocation + "gateway.conf"))
- {
- configPath = configLocation + "gateway.conf";
- }
- }
- }
- }
- try
- {
- if (!File.Exists(configPath))
- {
- configPath = System.IO.Directory.GetCurrentDirectory() + "/gateway.conf";
- }
- _log.logInfo("using config file:" + configPath);
- JObject result = JObject.Parse(File.ReadAllText(configPath));
- config = result;
- }
- catch(Exception ex)
- {
- _log.logError("Cannot read from config file in directory:" + configPath);
- Thread.Sleep(2000);
- Environment.Exit(Environment.ExitCode);
- }
- }
- //will get the value from the config JOBject
- public string getConfigValue(string key)
- {
- try
- {
- string value = config["config"][0][key].ToString();
- return value;
- }
- catch(Exception ex)
- {
- return "";
- }
- }
- //This function will do as it says, is it running on Linux?
- private bool IsLinux
- {
- get
- {
- int p = (int)Environment.OSVersion.Platform;
- return (p == 4) || (p == 6) || (p == 128);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement