/// <summary> /// Creates a ConfigFileAttribute using an object from another mod. /// </summary> /// <param name="attr">The attribute from the other mod.</param> /// <returns>A ConfigFileAttribute object with the values from that object, where /// possible to retrieve; or null if none could be obtained.</returns> internal static ConfigFileAttribute CreateFrom(object attr) { ConfigFileAttribute cfa = null; if (attr.GetType().Name == typeof(ConfigFileAttribute).Name) { var trAttr = Traverse.Create(attr); string file = null; bool indent = false; // Log any errors from obtaining these values try { file = trAttr.GetProperty <string>(nameof(ConfigFileName)); indent = trAttr.GetProperty <bool>(nameof(IndentOutput)); } catch (Exception e) { PUtil.LogExcWarn(e); } // Remove invalid file names if (!PUtil.IsValidFileName(file)) { file = null; } cfa = new ConfigFileAttribute(file, indent); } return(cfa); }
internal OptionsDialog(Type optionsType, KMod.Mod modSpec) { dialog = null; modImage = null; this.modSpec = modSpec ?? throw new ArgumentNullException("modSpec"); this.optionsType = optionsType ?? throw new ArgumentNullException("optionsType"); optionCategories = OptionsEntry.BuildOptions(optionsType); options = null; // Determine config location infoAttr = POptions.GetModInfoAttribute(optionsType); typeAttr = POptions.GetConfigFileAttribute(optionsType); var src = modSpec.file_source; if (src == null) { path = null; } else { path = Path.Combine(src.GetRoot(), typeAttr?.ConfigFileName ?? POptions. CONFIG_FILE_NAME); } // Find mod home page string url = infoAttr?.URL; var label = modSpec.label; if (string.IsNullOrEmpty(url) && label.distribution_platform == KMod.Label. DistributionPlatform.Steam) { // Steam mods use their workshop ID as the label url = "https://steamcommunity.com/sharedfiles/filedetails/?id=" + label.id; } modURL = url; }
/// <summary> /// Retrieves the configuration file path used by PLib Options for a specified type. /// </summary> /// <param name="attr">The config file attribute for that type.</param> /// <param name="modAssembly">The assembly to use for determining the path.</param> /// <returns>The path to the configuration file that will be used by PLib for that /// mod's config.</returns> private static string GetConfigPath(ConfigFileAttribute attr, Assembly modAssembly) { string path, name = modAssembly.GetNameSafe(); path = (name != null && (attr?.UseSharedConfigLocation == true)) ? Path.Combine(KMod.Manager.GetDirectory(), SHARED_CONFIG_FOLDER, name) : PUtil.GetModPath(modAssembly); return(Path.Combine(path, attr?.ConfigFileName ?? CONFIG_FILE_NAME)); }
internal OptionsDialog(Type optionsType, IOptionsHandler handler) { string root = handler.ConfigPath; dialog = null; modImage = null; this.handler = handler ?? throw new ArgumentNullException("handler"); this.optionsType = optionsType ?? throw new ArgumentNullException("optionsType"); optionCategories = OptionsEntry.BuildOptions(optionsType); options = null; // Determine config location infoAttr = POptions.GetModInfoAttribute(optionsType); typeAttr = POptions.GetConfigFileAttribute(optionsType); path = (root == null) ? null : Path.Combine(root, typeAttr?.ConfigFileName ?? POptions.CONFIG_FILE_NAME); }
/// <summary> /// Retrieves the configuration file attribute for a mod config. /// </summary> /// <param name="optionsType">The type potentially containing the config file attribute.</param> /// <returns>The ConfigFileAttribute (in this mod's assembly) applied to that type, /// or null if none is present.</returns> public static ConfigFileAttribute GetConfigFileAttribute(Type optionsType) { if (optionsType == null) { throw new ArgumentNullException("optionsType"); } ConfigFileAttribute newAttr = null; foreach (var attr in optionsType.GetCustomAttributes(true)) { // Cross mod types need reflection if ((newAttr = ConfigFileAttribute.CreateFrom(attr)) != null) { break; } } return(newAttr); }
internal OptionsDialog(Type optionsType, KMod.Mod modSpec) { dialog = null; this.modSpec = modSpec ?? throw new ArgumentNullException("modSpec"); this.optionsType = optionsType ?? throw new ArgumentNullException("optionsType"); optionCategories = BuildOptions(optionsType); options = null; // Determine config location typeAttr = POptions.GetConfigFileAttribute(optionsType); var src = modSpec.file_source; if (src == null) { path = null; } else { path = Path.Combine(src.GetRoot(), typeAttr?.ConfigFileName ?? POptions. CONFIG_FILE_NAME); } }
internal OptionsDialog(Type optionsType) { OnClose = null; dialog = null; modImage = null; this.optionsType = optionsType ?? throw new ArgumentNullException(nameof( optionsType)); optionCategories = OptionsEntry.BuildOptions(optionsType); options = null; // Determine config location var infoAttr = optionsType.GetCustomAttribute <ModInfoAttribute>(); if (infoAttr != null) { collapseCategories = infoAttr.ForceCollapseCategories; } else { collapseCategories = false; } configAttr = optionsType.GetCustomAttribute <ConfigFileAttribute>(); displayInfo = new ModDialogInfo(optionsType, infoAttr?.URL, infoAttr?.Image); }
/// <summary> /// Retrieves the configuration file attribute for a mod config. /// </summary> /// <param name="optionsType">The type potentially containing the config file attribute.</param> /// <returns>The ConfigFileAttribute (in this mod's assembly) applied to that type, /// or null if none is present.</returns> public static ConfigFileAttribute GetConfigFileAttribute(Type optionsType) { if (optionsType == null) { throw new ArgumentNullException("optionsType"); } ConfigFileAttribute newAttr = null; foreach (var attr in optionsType.GetCustomAttributes(true)) { // Cross mod types need reflection if (attr.GetType().Name == typeof(ConfigFileAttribute).Name) { var trAttr = Traverse.Create(attr); string file = null; bool indent = false; // Log any errors from obtaining these values try { file = trAttr.GetProperty <string>(nameof(ConfigFileAttribute. ConfigFileName)); indent = trAttr.GetProperty <bool>(nameof(ConfigFileAttribute. IndentOutput)); } catch (Exception e) { PUtil.LogExcWarn(e); } // Remove invalid file names if (!PUtil.IsValidFileName(file)) { file = null; } newAttr = new ConfigFileAttribute(file, indent); break; } } return(newAttr); }
/// <summary> /// Retrieves the configuration file path used by PLib Options for a specified type. /// </summary> /// <param name="attr">The config file attribute for that type.</param> /// <param name="modAssembly">The assembly to use for determining the path.</param> /// <returns>The path to the configuration file that will be used by PLib for that /// mod's config.</returns> private static string GetConfigPath(ConfigFileAttribute attr, Assembly modAssembly) { return(Path.Combine(GetModDir(modAssembly), attr?.ConfigFileName ?? CONFIG_FILE_NAME)); }