Exemplo n.º 1
0
        public static void ListenOnGameUnloading(this TorchPluginBase self, Action f)
        {
            var sessionManager = self.Torch.Managers.GetManager <TorchSessionManager>();

            sessionManager.SessionStateChanged += (session, state) =>
            {
                if (state == TorchSessionState.Unloading)
                {
                    f();
                }
            };
        }
Exemplo n.º 2
0
 /// <summary>
 /// Метод для сохранения файла конфига
 /// </summary>
 /// <typeparam name="T">Класс обьекта конфигурации</typeparam>
 /// <param name="plugin">Ссылка на текущий плагин</param>
 /// <param name="data">Обьект конфигурации</param>
 /// <param name="fileName"></param>
 /// <returns></returns>
 public static bool Save <T>(TorchPluginBase plugin, T data, string fileName) where T : new()
 {
     try
     {
         ///пишем конфиг в файл
         using (StreamWriter streamWriter = new StreamWriter(Path.Combine(plugin.StoragePath, fileName)))
             new XmlSerializer(typeof(T)).Serialize((TextWriter)streamWriter, (object)data);
         return(true);
     }
     catch (Exception ex)
     {
     }
     return(false);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Saves the specified data instance to a file.
 /// </summary>
 /// <typeparam name="T">The type of the data to save.</typeparam>
 /// <param name="plugin">Instance reference to your plugin</param>
 /// <param name="data">The actual data instance</param>
 /// <param name="fileName">Name of the file to store to</param>
 /// <returns></returns>
 public static bool Save <T>(TorchPluginBase plugin, T data, string fileName) where T : new()
 {
     try
     {
         string filePath = Path.Combine(plugin.StoragePath, fileName);
         using (StreamWriter streamWriter = new StreamWriter(filePath))
         {
             var xmlSerializer = new XmlSerializer(typeof(T));
             xmlSerializer.Serialize(streamWriter, data);
         }
         return(true);
     }
     catch (System.Exception) { }
     return(false);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Загрузка конфига из файла
        /// </summary>
        /// <typeparam name="T">Класс обьекта конфигурации</typeparam>
        /// <param name="plugin">Ссылка на текущий плагин</param>
        /// <param name="fileName">Имя файла конфигурации</param>
        /// <returns></returns>
        public static T Load <T>(TorchPluginBase plugin, string fileName) where T : new()
        {
            ///получаем путь к файлу конфига
            string path = Path.Combine(plugin.StoragePath, fileName);
            T      data = new T();

            if (File.Exists(path))
            {
                ///если файл конфига существует - читаем с конвертацией в T
                using (StreamReader streamReader = new StreamReader(path))
                    data = (T) new XmlSerializer(typeof(T)).Deserialize((TextReader)streamReader);
            }
            else
            {
                ///если файла нет - создаем(первый запуск торча с плагином)
                ConfigUtils.Save <T>(plugin, data, fileName);
            }
            return(data);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Generic Configuration/Data Load function to retreive data from specified file in the specified type.
        /// </summary>
        /// <typeparam name="T">The type of your configuration or data object</typeparam>
        /// <param name="plugin">Instance to the plugin</param>
        /// <param name="fileName">Name of the file to use</param>
        /// <returns></returns>
        public static T Load <T>(TorchPluginBase plugin, string fileName) where T : new()
        {
            string filePath = Path.Combine(plugin.StoragePath, fileName);
            T      config   = new T();

            if (File.Exists(filePath))
            {
                using (StreamReader streamReader = new StreamReader(filePath))
                {
                    var xmlSerializer = new XmlSerializer(typeof(T));
                    config = (T)xmlSerializer.Deserialize(streamReader);
                }
            }
            else
            {
                Save <T>(plugin, config, fileName);
            }
            return(config);
        }
Exemplo n.º 6
0
        public static T Load <T>(TorchPluginBase plugin, string fileName) where T : new()
        {
            string path = Path.Combine(plugin.StoragePath, "Lotto", fileName);
            T      t    = Activator.CreateInstance <T>();
            bool   flag = File.Exists(path);

            if (flag)
            {
                using (StreamReader streamReader = new StreamReader(path))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                    t = (T)((object)xmlSerializer.Deserialize(streamReader));
                }
            }
            else
            {
                ConfigUtils.Save <T>(plugin, t, fileName);
            }
            return(t);
        }
Exemplo n.º 7
0
        public static bool Save <T>(TorchPluginBase plugin, T data, string fileName) where T : new()
        {
            try
            {
                bool dossier = Directory.Exists(Path.Combine(plugin.StoragePath, "Lotto"));
                if (!dossier)
                {
                    Directory.CreateDirectory(Path.Combine(plugin.StoragePath, "Lotto"));
                }

                string path = Path.Combine(plugin.StoragePath, "Lotto", fileName);
                using (StreamWriter streamWriter = new StreamWriter(path))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                    xmlSerializer.Serialize(streamWriter, data);
                }
                return(true);
            }
            catch (Exception)
            {
            }
            return(false);
        }
Exemplo n.º 8
0
 public static string MakeFilePath(this TorchPluginBase self, string relativeFilePath)
 {
     return(Path.Combine(self.StoragePath, relativeFilePath));
 }
Exemplo n.º 9
0
 public static string MakeConfigFilePath(this TorchPluginBase self)
 {
     return(self.MakeFilePath($"{self.GetType().Name}.cfg"));
 }
Exemplo n.º 10
0
        public static void CreateConfigFile <T>(this TorchPluginBase self, string fileName, T content)
        {
            var filePath = Path.Combine(self.StoragePath, fileName);

            XmlUtils.SaveOrCreateXmlFile(filePath, content);
        }
Exemplo n.º 11
0
        public static bool TryFindConfigFile <T>(this TorchPluginBase self, string fileName, out T foundConfig) where T : class
        {
            var filePath = Path.Combine(self.StoragePath, fileName);

            return(XmlUtils.TryLoadXmlFile(filePath, out foundConfig));
        }
Exemplo n.º 12
0
 public static string MakeConfigFilePath(this TorchPluginBase self)
 {
     return(Path.Combine(self.StoragePath, $"{self.GetType().Name}.cfg"));
 }