private static string GetDigitFeminineStatus(IConfigConverter converter, int digit, int groupLevel) { if (groupLevel == 0) { if (converter.IsFeminine) { return(arabicFeminineOnes[digit]);// use feminine field } else { return(arabicOnes[digit]); } } else { return(arabicOnes[digit]); } }
public T Get <T>(string name, T defaultValue, IConfigConverter converter, string category = null, string description = null) { string key = $"{(category != null ? category.Replace(SPACE, string.Empty) + "." : string.Empty)}{name.Replace(SPACE, string.Empty)}"; if (configs.ContainsKey(key)) { try { return(converter != null ? (T)converter.ConvertToValue(configs[key]) : (T)Convert.ChangeType(configs[key], typeof(T))); } catch (Exception e) { UnityEngine.Debug.LogException(e); SRML.Console.LogError($"Value of '{name}' {(category != null ? $"from '{category}'" : string.Empty)} can't be converted. Default value will be returned"); return(defaultValue); } } else { Set(name, defaultValue, converter, category, description); return(defaultValue); } }
public void Set <T>(string name, T value, IConfigConverter converter, string category = null, string description = null) { string key = $"{(category != null ? category.Replace(SPACE, string.Empty) + "." : string.Empty)}{name.Replace(SPACE, string.Empty)}"; if (converter == null) { if (configs.ContainsKey(key)) { configs[key] = value.ToString(); } else { configs.Add(key, value.ToString()); } } else { if (configs.ContainsKey(key)) { configs[key] = converter.ConvertFromValue(value); } else { configs.Add(key, converter.ConvertFromValue(value)); } } if (infos.ContainsKey(key)) { infos.Add(key, new ConfigInfo(name, description, category)); } else { infos[key] = new ConfigInfo(name, description, category); } }
public static ConversionResult Convert(string OrigFile, ConfigurationVersion ToVerison) { ConversionResult Result = new ConversionResult(); ConfigurationVersion Ver = Parser.GetVersion(OrigFile); try { string OldFile = OrigFile; for (int i = (int)Ver + 1; i <= (int)Launcher.CurrentVersion; ++i) { Result.NewConfigFile = OldFile.Insert(OldFile.Length - ".cfg".Length, "." + ((ConfigurationVersion)i).ToString()); IConfigConverter Converter = ConverterFactory.CreateConverter((ConfigurationVersion)i); if (Converter == null) { AppLogger.Log("Unknown config format"); return(Result); } Result.Success = Converter.Convert(OldFile, Result.NewConfigFile); if (!Result.Success) { throw new Exception(string.Format("Couldn't convert\n%s\n\to\n%s", OldFile, Result.NewConfigFile)); } OldFile = Result.NewConfigFile; } } catch (Exception ex) { AppLogger.Log("Conversion error: " + ex.Message); } return(Result); }
public ConfigWrapper(string key, BaseUnityPlugin plugin, IConfigConverter <T> converter, T @default = default(T)) : this(key, converter.ConvertFromString, converter.ConvertToString, @default) { Section = MetadataHelper.GetMetadata(plugin).GUID; }
public ConfigWrapper(string key, IConfigConverter <T> converter, T @default = default(T)) : this(key, converter.ConvertFromString, converter.ConvertToString, @default) { }
/// <summary> /// Defines the converter used to get the value from a ConfigValueAttribute /// </summary> /// <param name="converter">The converter to use</param> public ConfigConverterAttribute(IConfigConverter converter) { this.converter = converter; }
// POPULATES EVERY CLASS WITH THE CONFIG VALUES. THESE VALUES CAN STILL BE OBTAINED IN A CONVENTIONAL WAY THROUGH FILES internal static void Populate() { foreach (Assembly modDll in cachedModDlls) { foreach (Type type in modDll.GetTypes()) { foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { object[] attributes = field.GetCustomAttributes(typeof(ConfigValueAttribute), false); ConfigValueAttribute configValue = attributes.Length > 0 ? attributes[0] as ConfigValueAttribute : null; attributes = field.GetCustomAttributes(typeof(ConfigDescriptionAttribute), false); ConfigDescriptionAttribute description = attributes.Length > 0 ? attributes[0] as ConfigDescriptionAttribute : null; attributes = field.GetCustomAttributes(typeof(ConfigCategoryAttribute), false); ConfigCategoryAttribute category = attributes.Length > 0 ? attributes[0] as ConfigCategoryAttribute : null; attributes = field.GetCustomAttributes(typeof(ConfigConverterAttribute), false); ConfigConverterAttribute converter = attributes.Length > 0 ? attributes[0] as ConfigConverterAttribute : null; attributes = field.GetCustomAttributes(typeof(ConfigUIAttribute), false); ConfigUIAttribute uiDesign = attributes.Length > 0 ? attributes[0] as ConfigUIAttribute : null; if (configValue == null) { continue; } ConfigFile file; FileID id = new FileID(modDll, configValue.file == null ? "main.config" : configValue.file + ".config"); if (!files.ContainsKey(id)) { file = new ConfigFile(modDll, configValue.file == null ? "main.config" : configValue.file + ".config"); files.Add(id, file); } else { file = files[id]; } IConfigConverter convert = converter?.converter; if (uiDesign != null) { file.AddDesign(configValue.name, uiDesign, category?.category); } string value = file.Get(configValue.name, convert != null ? convert.ConvertFromValue(configValue.defaultValue) : configValue.defaultValue.ToString(), null, category?.category, description?.description); try { field.SetValue(null, Convert.ChangeType(value, field.FieldType)); } catch (Exception e) { SRML.Console.LogError($"Trying to set value for field '{field.Name}' from config, but config value can't be converted. Default value will be used, however this might mean a field is asking for a diferent type of data then it can contain"); UnityEngine.Debug.LogException(e); field.SetValue(null, configValue.defaultValue); continue; } } } } }
public static string ToArabic(this Decimal number, IConfigConverter converter, string ArabicPrefixText, string ArabicSuffixText) { if (converter == null) { converter = new GradeConverter(); } string minus = ""; if (number < 0) { minus = "ناقص "; number *= -1; } if (number == 0) { return("صفر"); } if (number == 1) { if (converter.IsFeminine) { return(minus + converter.Arabic1Name + " واحدة"); } else { return(minus + converter.Arabic1Name + " واحد"); } } if (number == 2) { if (converter.IsFeminine) { return(minus + converter.Arabic2Name + " اثنتان"); } else { return(minus + converter.Arabic2Name + " اثنان"); } } Decimal tempNumber = number; string retVal = String.Empty; Byte group = 0; while (tempNumber >= 1) { // seperate number into groups int numberToProcess = (int)(tempNumber % 1000); tempNumber = tempNumber / 1000; // convert group into its text string groupDescription = ProcessArabicGroup(converter, number, numberToProcess, group, tempNumber); if (groupDescription != String.Empty) { // here we add the new converted group to the previous concatenated text if (group > 0) { if (retVal != String.Empty) { retVal = String.Format("{0} {1}", "و", retVal); } if (numberToProcess != 2) { if (numberToProcess % 100 != 1) { if (numberToProcess >= 3 && numberToProcess <= 10) // for numbers between 3 and 9 we use plural name { retVal = String.Format("{0} {1}", arabicPluralGroups[group], retVal); } else { if (retVal != String.Empty) // use appending case { retVal = String.Format("{0} {1}", arabicAppendedGroup[group], retVal); } else { retVal = String.Format("{0} {1}", arabicGroup[group], retVal); // use normal case } } } else { retVal = String.Format("{0} {1}", arabicGroup[group], retVal); // use normal case } } } retVal = String.Format("{0} {1}", groupDescription, retVal); } group++; } String formattedNumber = String.Empty; formattedNumber += (ArabicPrefixText != String.Empty) ? String.Format("{0} ", ArabicPrefixText) : String.Empty; formattedNumber += (retVal != String.Empty) ? retVal : String.Empty; if (number != 0) { // here we add currency name depending on _intergerValue : 1 ,2 , 3--->10 , 11--->99 int remaining100 = (int)(number % 100); if (remaining100 == 0) { formattedNumber += converter.Arabic1Name; } else if (remaining100 == 1) { formattedNumber += converter.Arabic1Name; } else if (remaining100 == 2) { if (number == 2) { formattedNumber += converter.Arabic2Name; } else { formattedNumber += converter.Arabic1Name; } } else if (remaining100 >= 3 && remaining100 <= 10) { formattedNumber += converter.Arabic310Name; } else if (remaining100 >= 11 && remaining100 <= 99) { formattedNumber += converter.Arabic1199Name; } } formattedNumber += (ArabicSuffixText != String.Empty) ? String.Format(" {0}", ArabicSuffixText) : String.Empty; return(minus + formattedNumber); }
public static string ToArabic(this Decimal number, IConfigConverter converter = null) { return(ToArabic(number, converter, "", "")); }
private static string ProcessArabicGroup(IConfigConverter converter, Decimal number, int groupNumber, int groupLevel, Decimal remainingNumber) { int tens = groupNumber % 100; int hundreds = groupNumber / 100; string retVal = String.Empty; if (hundreds > 0) { retVal = String.Format("{0}", arabicHundreds[hundreds]); } if (tens > 0) { if (tens < 20) { // if we are processing under 20 numbers if (tens == 2 && hundreds == 0 && groupLevel > 0) { // This is special case for number 2 when it comes alone in the group retVal = String.Format("{0}", arabicTwos[groupLevel]); // في حالة الافراد } else { // General case if (retVal != String.Empty) { retVal += " و "; } if (tens == 1 && groupLevel > 0 && hundreds == 0) { retVal += " "; } else if ((tens == 1 || tens == 2) && (groupLevel == 0 || groupLevel == -1) && hundreds == 0 && remainingNumber == 0) { retVal += String.Empty; // Special case for 1 and 2 numbers like: ليرة سورية و ليرتان سوريتان } else { retVal += GetDigitFeminineStatus(converter, tens, groupLevel); // Get Feminine status for this digit } } } else { int ones = tens % 10; tens = (tens / 10) - 2; // 20's offset if (ones > 0) { if (retVal != String.Empty) { retVal += " و "; } // Get Feminine status for this digit retVal += GetDigitFeminineStatus(converter, ones, groupLevel); } if (retVal != String.Empty) { retVal += " و "; } // Get Tens text retVal += arabicTens[tens]; } } return(retVal); }