Exemplo n.º 1
0
 public override void ResolveReferences()
 {
     base.ResolveReferences();
     try {
         if (!modIdentifier.NullOrEmpty() && !PersistentDataManager.IsValidElementName(modIdentifier))
         {
             throw new Exception($"{nameof(modIdentifier)} value has invalid format. " +
                                 "Must start with a letter and contain any of [A-z, 0-9, -, _, :].");
         }
         if (defName == null)
         {
             defName = modIdentifier + assemblyVersion;
         }
         if (modNameReadable == null)
         {
             throw new Exception($"{nameof(modNameReadable)} value must be set.");
         }
         try {
             if (string.IsNullOrEmpty(assemblyVersion))
             {
                 throw new Exception("No value specified.");
             }
             Version = new Version(assemblyVersion);
         } catch (Exception e) {
             Version = new Version();
             throw new Exception($"{nameof(assemblyVersion)} value is not valid.", e);
         }
         if (content == null)
         {
             throw new Exception($"{nameof(content)} value must be set.");
         }
     } catch (Exception e) {
         throw new Exception($"{nameof(UpdateFeatureDef)} contains invalid data.", e);
     }
 }
Exemplo n.º 2
0
        protected ModBase()
        {
            var modId = ModIdentifier;

            if (!PersistentDataManager.IsValidElementName(modId))
            {
                throw new FormatException("Invalid mod identifier: " + modId);
            }
            Logger   = new ModLogger(modId);
            Settings = HugsLibController.Instance.Settings.GetModSettings(modId);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Retrieves an existing SettingHandle from the pack, or creates a new one.
        /// Loaded settings will only display in the Mod Settings dialog after they have been claimed using this method.
        /// </summary>
        /// <typeparam name="T">The type of setting value you are creating.</typeparam>
        /// <param name="settingName">Unique identifier for the setting. Must be unique for this specific pack only.</param>
        /// <param name="title">A display name for the setting that will show up next to it in the Mod Settings dialog. Recommended to keep this short.</param>
        /// <param name="description">A description for the setting that will appear in a tooltip when the player hovers over the setting in the Mod Settings dialog.</param>
        /// <param name="defaultValue">The value the setting will assume when newly created and when the player resets the setting to its default.</param>
        /// <param name="validator">An optional delegate that will be called when a new value is about to be assigned to the handle. Receives a string argument and must return a bool to indicate if the passed value is valid for the setting.</param>
        /// <param name="enumPrefix">Used only for Enum settings. Enum values are displayed in a readable format by the following method: Translate(prefix+EnumValueName)</param>
        public SettingHandle <T> GetHandle <T>(string settingName, string title, string description, T defaultValue = default(T), SettingHandle.ValueIsValid validator = null, string enumPrefix = null)
        {
            if (!PersistentDataManager.IsValidElementName(settingName))
            {
                throw new Exception("Invalid name for mod setting: " + settingName);
            }
            SettingHandle <T> handle = null;

            for (int i = 0; i < handles.Count; i++)
            {
                if (handles[i].Name != settingName)
                {
                    continue;
                }
                if (!(handles[i] is SettingHandle <T>))
                {
                    continue;
                }
                handle = (SettingHandle <T>)handles[i];
                break;
            }
            if (handle == null)
            {
                handle = new SettingHandle <T>(settingName)
                {
                    Value = defaultValue
                };
                handle.ParentPack = this;
                handles.Add(handle);
            }
            handle.DefaultValue     = defaultValue;
            handle.Title            = title;
            handle.Description      = description;
            handle.Validator        = validator;
            handle.EnumStringPrefix = enumPrefix;
            if (typeof(T).IsEnum && (enumPrefix == null || !(enumPrefix + Enum.GetName(typeof(T), defaultValue)).CanTranslate()))
            {
                HugsLibController.Logger.Warning("Missing enum setting labels for enum " + typeof(T));
            }
            if (loadedValues.ContainsKey(settingName))
            {
                var loadedValue = loadedValues[settingName];
                loadedValues.Remove(settingName);
                handle.StringValue = loadedValue;
                if (handle.Validator != null && !handle.Validator(loadedValue))
                {
                    handle.ResetToDefault();
                }
            }
            handle.HasUnsavedChanges = false;
            return(handle);
        }
Exemplo n.º 4
0
        protected ModBase()
        {
            modContentPackInt = CurrentlyProcessedContentPack;
            Logger            = new ModLogger(LogIdentifierSafe);
            var settingsPackId = SettingsIdentifier;

            if (!string.IsNullOrEmpty(settingsPackId))
            {
                if (PersistentDataManager.IsValidElementName(settingsPackId))
                {
                    Settings = HugsLibController.Instance.Settings.GetModSettings(settingsPackId, modContentPackInt?.Name);
                }
                else
                {
                    Logger.Error($"string \"{settingsPackId}\" cannot be used as a settings identifier. " +
                                 $"Override {nameof(ModBase)}.{nameof(SettingsIdentifier)} to manually specify one. " +
                                 $"See {nameof(SettingsIdentifier)} autocomplete documentation for expected format.");
                }
            }
        }