<commit_before>﻿using System;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Xml;
using System.Xml.Linq;

namespace Data
{
    public class WindowData
    {
        private readonly string _windowFile;
        private readonly XDocument _xml;
        private readonly FileStream _filestream;

        public WindowData(BasicTeraData basicData)
        {
            DefaultValue();
            // Load XML File
            _windowFile = Path.Combine(basicData.ResourceDirectory, "config/window.xml");

            try
            {
                FileAttributes attrs = File.GetAttributes(_windowFile);
                File.SetAttributes(_windowFile, attrs & ~FileAttributes.ReadOnly);
            }
            catch {
                //ignore
            }

            try
            {
                _filestream = new FileStream(_windowFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                _xml = XDocument.Load(_filestream);
            }
            catch (Exception ex) when (ex is XmlException || ex is InvalidOperationException)
            {
                Save();
                _filestream = new FileStream(_windowFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                return;
            }
            catch
            {
                return;
            }

            ParseLocation();
            ParseLanguage();
            ParseMainWindowOpacity();
            ParseSkillWindowOpacity();
            ParseAutoUpdate();
            ParseRememberPosition();
            ParseWinpcap();
            ParseInvisibleUi();
            ParseAllowTrasparency();
            ParseTopmost();
            ParseTeraDps();
            ParseDebug();
            ParseExcel();
            ParseAlwaysVisible();
            ParseScale();
            ParsePartyOnly();
            ParseShowHealCrit();
            ParseExcelSaveDirectory();
            ParseLFDelay();
        }


        public Point Location { get; set; }

        public string ExcelSaveDirectory { get; set; }

        public double Scale { get; set; }
        public bool PartyOnly { get; set; }
        public double MainWindowOpacity { get; private set; }
        public double SkillWindowOpacity { get; private set; }

        public bool RememberPosition { get; private set; }
        public string Language { get; private set; }

        public bool AutoUpdate { get; private set; }

        public bool Winpcap { get; private set; }

        public bool InvisibleUI { get; set; }

        public bool AllowTransparency { get; set; }

        public string TeraDpsUser { get; private set; }
        public string TeraDpsToken { get; private set; }

        public bool AlwaysVisible { get;  set; }

        public bool Topmost { get; set; }

        public bool Debug { get; set; }

        public bool Excel { get; set; }

        public bool SiteExport { get; set; }
        public bool ShowHealCrit { get; set; }
        public int LFDelay { get; set; }

        private void DefaultValue()
        {
            Location = new Point(0, 0);
            Language = "Auto";
            MainWindowOpacity = 0.5;
            SkillWindowOpacity = 0.7;
            AutoUpdate = true;
            RememberPosition = true;
            InvisibleUI = false;
            Winpcap = true;
            Topmost = true;
            AllowTransparency = true;
            Debug = true;
            TeraDpsToken = "";
            TeraDpsUser = "";
            Excel = false;
            AlwaysVisible = false;
            Scale = 1;
            PartyOnly = false;
            SiteExport = true;
            ShowHealCrit = true;
            ExcelSaveDirectory = "";
            LFDelay = 150;
        }

        private void ParseLFDelay()
        {
            var root = _xml.Root;
            var xml = root?.Element("lf_delay");
            if (xml == null) return;
            int value;
            var parseSuccess = int.TryParse(xml.Value, out value);
            if (parseSuccess)
            {
                LFDelay = value;
            }
        }

        private void ParseExcelSaveDirectory()
        {
            var root = _xml.Root;
            var excelxml = root?.Element("excel_save_directory");
            if (excelxml == null) return;
            ExcelSaveDirectory = excelxml.Value;
            
        }

        private void ParseShowHealCrit()
        {
            var root = _xml.Root;
            var xml = root?.Element("showhealcrit");
            if (xml == null) return;
            bool val;
            var parseSuccess = bool.TryParse(xml.Value, out val);
            if (parseSuccess)
            {
                ShowHealCrit = val;
            }
        }
        private void ParsePartyOnly()
        {
            var root = _xml.Root;
            var xml = root?.Element("partyonly");
            if (xml == null) return;
            bool party;
            var parseSuccess = bool.TryParse(xml.Value, out party);
            if (parseSuccess)
            {
                PartyOnly = party;
            }
        }

        private void ParseExcel()
        {
            var root = _xml.Root;
            var excelxml = root?.Element("excel");
            if (excelxml == null) return;
            bool excel;
            var parseSuccess = bool.TryParse(excelxml.Value, out excel);
            if (parseSuccess)
            {
                Excel = excel;
            }
        }
        private void ParseScale()
        {
            var root = _xml.Root;
            var scalexml = root?.Element("scale");
            if (scalexml == null) return;
            double scale;
            var parseSuccess = double.TryParse(scalexml.Value, out scale);
            if (parseSuccess)
            {
                Scale = scale;
            }
        }
        private void ParseAlwaysVisible()
        {
            var root = _xml.Root;
            var alwaysvisible = root?.Element("always_visible");
            if (alwaysvisible == null) return;
            bool visible;
            var parseSuccess = bool.TryParse(alwaysvisible.Value, out visible);<commit_msg>Fix scale save issue. Now using InvariantCulture.<commit_after>﻿using System;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Xml;
using System.Xml.Linq;

namespace Data
{
    public class WindowData
    {
        private readonly string _windowFile;
        private readonly XDocument _xml;
        private readonly FileStream _filestream;

        public WindowData(BasicTeraData basicData)
        {
            DefaultValue();
            // Load XML File
            _windowFile = Path.Combine(basicData.ResourceDirectory, "config/window.xml");

            try
            {
                FileAttributes attrs = File.GetAttributes(_windowFile);
                File.SetAttributes(_windowFile, attrs & ~FileAttributes.ReadOnly);
            }
            catch {
                //ignore
            }

            try
            {
                _filestream = new FileStream(_windowFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                _xml = XDocument.Load(_filestream);
            }
            catch (Exception ex) when (ex is XmlException || ex is InvalidOperationException)
            {
                Save();
                _filestream = new FileStream(_windowFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                return;
            }
            catch
            {
                return;
            }

            ParseLocation();
            ParseLanguage();
            ParseMainWindowOpacity();
            ParseSkillWindowOpacity();
            ParseAutoUpdate();
            ParseRememberPosition();
            ParseWinpcap();
            ParseInvisibleUi();
            ParseAllowTrasparency();
            ParseTopmost();
            ParseTeraDps();
            ParseDebug();
            ParseExcel();
            ParseAlwaysVisible();
            ParseScale();
            ParsePartyOnly();
            ParseShowHealCrit();
            ParseExcelSaveDirectory();
            ParseLFDelay();
        }


        public Point Location { get; set; }

        public string ExcelSaveDirectory { get; set; }

        public double Scale { get; set; }
        public bool PartyOnly { get; set; }
        public double MainWindowOpacity { get; private set; }
        public double SkillWindowOpacity { get; private set; }

        public bool RememberPosition { get; private set; }
        public string Language { get; private set; }

        public bool AutoUpdate { get; private set; }

        public bool Winpcap { get; private set; }

        public bool InvisibleUI { get; set; }

        public bool AllowTransparency { get; set; }

        public string TeraDpsUser { get; private set; }
        public string TeraDpsToken { get; private set; }

        public bool AlwaysVisible { get;  set; }

        public bool Topmost { get; set; }

        public bool Debug { get; set; }

        public bool Excel { get; set; }

        public bool SiteExport { get; set; }
        public bool ShowHealCrit { get; set; }
        public int LFDelay { get; set; }

        private void DefaultValue()
        {
            Location = new Point(0, 0);
            Language = "Auto";
            MainWindowOpacity = 0.5;
            SkillWindowOpacity = 0.7;
            AutoUpdate = true;
            RememberPosition = true;
            InvisibleUI = false;
            Winpcap = true;
            Topmost = true;
            AllowTransparency = true;
            Debug = true;
            TeraDpsToken = "";
            TeraDpsUser = "";
            Excel = false;
            AlwaysVisible = false;
            Scale = 1;
            PartyOnly = false;
            SiteExport = true;
            ShowHealCrit = true;
            ExcelSaveDirectory = "";
            LFDelay = 150;
        }

        private void ParseLFDelay()
        {
            var root = _xml.Root;
            var xml = root?.Element("lf_delay");
            if (xml == null) return;
            int value;
            var parseSuccess = int.TryParse(xml.Value, out value);
            if (parseSuccess)
            {
                LFDelay = value;
            }
        }

        private void ParseExcelSaveDirectory()
        {
            var root = _xml.Root;
            var excelxml = root?.Element("excel_save_directory");
            if (excelxml == null) return;
            ExcelSaveDirectory = excelxml.Value;
            
        }

        private void ParseShowHealCrit()
        {
            var root = _xml.Root;
            var xml = root?.Element("showhealcrit");
            if (xml == null) return;
            bool val;
            var parseSuccess = bool.TryParse(xml.Value, out val);
            if (parseSuccess)
            {
                ShowHealCrit = val;
            }
        }
        private void ParsePartyOnly()
        {
            var root = _xml.Root;
            var xml = root?.Element("partyonly");
            if (xml == null) return;
            bool party;
            var parseSuccess = bool.TryParse(xml.Value, out party);
            if (parseSuccess)
            {
                PartyOnly = party;
            }
        }

        private void ParseExcel()
        {
            var root = _xml.Root;
            var excelxml = root?.Element("excel");
            if (excelxml == null) return;
            bool excel;
            var parseSuccess = bool.TryParse(excelxml.Value, out excel);
            if (parseSuccess)
            {
                Excel = excel;
            }
        }
        private void ParseScale()
        {
            var root = _xml.Root;
            var scalexml = root?.Element("scale");
            if (scalexml == null) return;
            double scale;
            var parseSuccess = double.TryParse(scalexml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out scale);
            if (parseSuccess)
            {
                Scale = scale;
            }
        }
        private void ParseAlwaysVisible()
        {
            var root = _xml.Root;
            var alwaysvisible = root?.Element("always_visible");
            if (alwaysvisible == null) return;
            bool visible;
            var parseSuccess = bool.TryParse(alwaysvisible.Value, out visible);
{'fraction_non_alphanumeric': 0.08437801350048216, 'fraction_numerical': 0.0016072002571520412, 'mean_word_length': 1.4349442379182156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 56}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '610779', 'n_tokens_mistral': 3184, 'n_tokens_neox': 3048, 'n_words': 823}