<commit_before>﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;

namespace PrefsGUI.Kvs
{
    /// <summary>
    /// Custom File Path for PrefsKvs
    /// Relative to Application.dataPath
    /// you can use magic path
    /// - %dataPath% -> Application.dataPath
    /// - %companyName% -> Application.companyName
    /// - %productName% -> Application.productName
    /// - other %[word]% -> System.Environment.GetEnvironmentVariable([word])
    /// </summary>
    public class PrefsKvsPathCustom : MonoBehaviour, IPrefsKvsPath
    {
        #region Type Define
        
        /// <summary>
        /// Platform flags
        /// RuntimePlatform is too much, define it myself
        /// https://docs.unity3d.com/ScriptReference/RuntimePlatform.html
        /// </summary>
        [Flags]
        public enum Platform
        {
            OSXEditor      = (1 << 0),
            OSXPlayer      = (1 << 1),
            WindowsPlayer  = (1 << 2),
            WindowsEditor  = (1 << 3),
            IPhonePlayer   = (1 << 4),
            Android        = (1 << 5),
            LinuxPlayer    = (1 << 6),
            LinuxEditor    = (1 << 7),
            WebGLPlayer    = (1 << 8),
            WSAPlayerX86   = (1 << 9),
            WSAPlayerX64   = (1 << 10),
            WSAPlayerARM   = (1 << 11),
            PS4            = (1 << 12),
            XboxOne        = (1 << 13),
            tvOS           = (1 << 14),
            Switch         = (1 << 15),
            Stadia         = (1 << 16),
            CloudRendering = (1 << 17),
            PS5            = (1 << 18),
            LinuxServer    = (1 << 19),
            WindowsServer  = (1 << 20),
            OSXServer      = (1 << 21),
        }
        
        #endregion
        
        
        #region Static

        static bool IsPlatformActive(Platform platform)
        {
            var name = Enum.GetName(typeof(RuntimePlatform), Application.platform);
            Platform current;
            try
            {
                current = Enum.Parse<Platform>(name);
            }
            catch
            {
                return false;
            }

            return platform.HasFlag(current);
        }

        public string ReplaceMagicWord(string rawString)
        {
            var ret = rawString
                .Replace("%dataPath%", Application.dataPath)
                .Replace("%companyName%", Application.companyName)
                .Replace("%productName%", Application.productName);

            var matches = Regex.Matches(ret, @"%\w+?%").Cast<Match>();

            return matches.Aggregate(ret, (current, m) => current.Replace(m.Value, Environment.GetEnvironmentVariable(m.Value.Trim('%'))));
        }

        #endregion

        [SerializeField]
        public Platform platform = (Platform.WindowsEditor | Platform.WindowsPlayer);        

        [SerializeField]
        protected string _path = "%dataPath%/../../%productName%Prefs";

        protected virtual string rawPath => _path;


        public string path =>
            IsPlatformActive(platform)
                ? ReplaceMagicWord(rawPath)
                : null;
    }
}<commit_msg>feat: PrefsKvsPathCustom new magic path %currentDir%<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;

namespace PrefsGUI.Kvs
{
    /// <summary>
    /// Custom File Path for PrefsKvs
    /// Relative to Application.dataPath
    /// you can use magic path
    /// - %dataPath% Application.dataPath
    /// - %companyName% Application.companyName
    /// - %productName% Application.productName
    /// - %currentDir% Path.GetFileName(Directory.GetCurrentDirectory()))
    /// - other %[word]% System.Environment.GetEnvironmentVariable([word]))]
    /// </summary>
    public class PrefsKvsPathCustom : MonoBehaviour, IPrefsKvsPath
    {
        #region Type Define
        
        /// <summary>
        /// Platform flags
        /// RuntimePlatform is too much, define it myself
        /// https://docs.unity3d.com/ScriptReference/RuntimePlatform.html
        /// </summary>
        [Flags]
        public enum Platform
        {
            OSXEditor      = (1 << 0),
            OSXPlayer      = (1 << 1),
            WindowsPlayer  = (1 << 2),
            WindowsEditor  = (1 << 3),
            IPhonePlayer   = (1 << 4),
            Android        = (1 << 5),
            LinuxPlayer    = (1 << 6),
            LinuxEditor    = (1 << 7),
            WebGLPlayer    = (1 << 8),
            WSAPlayerX86   = (1 << 9),
            WSAPlayerX64   = (1 << 10),
            WSAPlayerARM   = (1 << 11),
            PS4            = (1 << 12),
            XboxOne        = (1 << 13),
            tvOS           = (1 << 14),
            Switch         = (1 << 15),
            Stadia         = (1 << 16),
            CloudRendering = (1 << 17),
            PS5            = (1 << 18),
            LinuxServer    = (1 << 19),
            WindowsServer  = (1 << 20),
            OSXServer      = (1 << 21),
        }
        
        #endregion
        
        #region Static

        static bool IsPlatformActive(Platform platform)
        {
            var name = Enum.GetName(typeof(RuntimePlatform), Application.platform);
            Platform current;
            try
            {
                current = Enum.Parse<Platform>(name);
            }
            catch
            {
                return false;
            }

            return platform.HasFlag(current);
        }

        public string ReplaceMagicWord(string rawString)
        {
            var ret = rawString
                .Replace("%dataPath%", Application.dataPath)
                .Replace("%companyName%", Application.companyName)
                .Replace("%productName%", Application.productName)
                .Replace("%currentDir%", Path.GetFileName(Directory.GetCurrentDirectory()));

            var matches = Regex.Matches(ret, @"%\w+?%").Cast<Match>();

            return matches.Aggregate(ret, (current, m) => current.Replace(m.Value, Environment.GetEnvironmentVariable(m.Value.Trim('%'))));
        }

        #endregion

        [SerializeField]
        public Platform platform = (Platform.WindowsEditor | Platform.WindowsPlayer);        

        [SerializeField]
        [Tooltip(@"Custom File Path for PrefsKvs
Relative to Application.dataPath
you can use magic path
- %dataPath% Application.dataPath
- %companyName% Application.companyName
- %productName% Application.productName
- %currentDir% Path.GetFileName(Directory.GetCurrentDirectory()))
- other %[word]% System.Environment.GetEnvironmentVariable([word]))]
")]
        protected string _path = "%dataPath%/../../%productName%Prefs";

        protected virtual string rawPath => _path;


        public string path =>
            IsPlatformActive(platform)
                ? ReplaceMagicWord(rawPath)
                : null;
    }
}
{'fraction_non_alphanumeric': 0.12365359758724688, 'fraction_numerical': 0.01809564842740198, 'mean_word_length': 1.5697416974169742, 'pattern_counts': {'":': 0, '<': 103, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4661507', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1810, 'n_words': 417}