Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.IO;
  7. using System.Text;
  8. using CoCosLog;
  9. using System.Threading;
  10.  
  11. namespace ConfigClass
  12. {
  13.     public class Config
  14.     {
  15.         public JObject config;
  16.         private Log _log = new Log("Config");
  17.  
  18.         public Config(string configLocation = null)
  19.         {
  20.             loadConfig(configLocation);
  21.         }
  22.         public void loadConfig(string configLocation)
  23.         {
  24.             string configPath = "";
  25.             if (configLocation == null || configLocation.Length == 0)
  26.             {
  27.                 if (IsLinux)
  28.                 {
  29.                     configPath = @"/etc/cocos/gateway.conf";
  30.                 }
  31.                 else
  32.                 {
  33.                     configPath = @"c:\cocos\gateway.conf";
  34.                 }
  35.             }
  36.             else
  37.             {
  38.                 if (File.Exists(configLocation))
  39.                 {
  40.                     configPath = configLocation;
  41.                 }
  42.                 else
  43.                 {
  44.                     if (Directory.Exists(configLocation))
  45.                     {
  46.                         if(File.Exists(configLocation + "/gateway.conf"))
  47.                         {
  48.                             configPath = configLocation + "/gateway.conf";
  49.                         }
  50.                         else if(File.Exists(configLocation + "gateway.conf"))
  51.                         {
  52.                             configPath = configLocation + "gateway.conf";
  53.                         }
  54.                     }
  55.                 }
  56.             }
  57.             try
  58.             {  
  59.                 if (!File.Exists(configPath))
  60.                 {
  61.                     configPath = System.IO.Directory.GetCurrentDirectory() + "/gateway.conf";
  62.                 }
  63.  
  64.                 _log.logInfo("using config file:" + configPath);
  65.                 JObject result = JObject.Parse(File.ReadAllText(configPath));
  66.                 config = result;
  67.             }
  68.             catch(Exception ex)
  69.             {
  70.                 _log.logError("Cannot read from config file in directory:" + configPath);
  71.                 Thread.Sleep(2000);
  72.                 Environment.Exit(Environment.ExitCode);
  73.             }
  74.         }
  75.         //will get the value from the config JOBject
  76.         public string getConfigValue(string key)
  77.         {
  78.             try
  79.             {
  80.                 string value = config["config"][0][key].ToString();
  81.                 return value;
  82.             }
  83.             catch(Exception ex)
  84.             {
  85.                 return "";
  86.             }          
  87.         }
  88.         //This function will do as it says, is it running on Linux?
  89.         private bool IsLinux
  90.         {
  91.             get
  92.             {
  93.                 int p = (int)Environment.OSVersion.Platform;
  94.                 return (p == 4) || (p == 6) || (p == 128);
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement