Example #1
0
        internal static int RegisterStatic(E entry, string name, int preferredID = -1, bool overrideExisting = true)
        {
            if (!registeringVanilla && modRegistering < 0)
            {
                throw new InvalidOperationException("Data registration may only be performed by the Initialize method of a Gadget!");
            }
            if (!entry.ReadyToRegister())
            {
                throw new InvalidOperationException("This registry entry is not yet ready to be registered, or has already been registered!");
            }
            string modNamePrefix = Gadgets.GetGadgetInfo(modRegistering).Attribute.Name + ":";
            bool   hasPrefix     = false;

            if (name != null)
            {
                hasPrefix = name.StartsWith(modNamePrefix);
                if (!(hasPrefix ? name.Substring(modNamePrefix.Length) : name).All(x => char.IsLetterOrDigit(x) || x == ' '))
                {
                    throw new InvalidOperationException("Registry name must be alphanumeric!");
                }
            }
            if (name == null)
            {
                name = overrideExisting && Singleton.HasEntry(preferredID) ? Singleton[preferredID].RegistryName.Substring(Singleton[preferredID].RegistryName.IndexOf(':') + 1) : Singleton.GenerateDefaultRegistryName(preferredID);
            }
            if (name == null)
            {
                throw new InvalidOperationException("Must provide a registry name!");
            }
            string registryName = hasPrefix ? name : modNamePrefix + name;
            int    reservedID   = Singleton.GetReservedID(registryName);

            if (reservedID >= 0 && preferredID < 0)
            {
                preferredID = reservedID;
            }
            T   typeEnum = entry.GetEntryType();
            int id       = lastUsedIDs.ContainsKey(typeEnum) ? lastUsedIDs[typeEnum] : -1;

            if (preferredID < 0 || (!overrideExisting && Singleton.HasEntry(preferredID)) || !entry.IsValidIDForType(preferredID))
            {
                if (!overrideExisting && preferredID >= 0)
                {
                    return(-1);
                }
                do
                {
                    id = entry.GetNextIDForType(id);
                } while (id >= 0 && Singleton.HasEntry(id) || Singleton.IsIDReserved(id) != null);
                if (id < 0)
                {
                    return(id);
                }
            }
            else
            {
                id = preferredID;
            }
            entry.ModID = modRegistering;
            entry.ID    = id;
            if (name == preferredID.ToString())
            {
                name = id.ToString();
            }
            entry.RegistryName                         = (registeringVanilla ? "Roguelands" : Gadgets.GetGadgetInfo(modRegistering).Attribute.Name) + ":" + name;
            Singleton.IDRegistry[entry.ID]             = entry;
            Singleton.NameRegistry[entry.RegistryName] = entry;
            if (!registeringVanilla)
            {
                Singleton.reservedIDs[entry.RegistryName] = id;
            }
            lastUsedIDs[typeEnum] = id;
            Singleton.PostRegistration(entry);
            entry.PostRegister();
            return(id);
        }
Example #2
0
 /// <summary>
 /// Gets the mod that registered this Registry Entry. This will return null until the Registry Entry has been registered.
 /// </summary>
 public Gadget GetMod()
 {
     return(ModID >= 0 ? Gadgets.GetGadget(ModID) : null);
 }