Created
March 1, 2018 14:49
-
-
Save copygirl/78dec49ff05679e5b83a78572064acf5 to your computer and use it in GitHub Desktop.
Vintage Story mod attribute example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Text; | |
| namespace Vintagestory.API.Common | |
| { | |
| [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] | |
| public class ModDependencyAttribute : Attribute | |
| { | |
| /// <summary> The mod ID of this dependency. </summary> | |
| public string ModID { get; } | |
| /// <summary> | |
| /// The minimum version requirement of this dependency. | |
| /// May be null if the no specific version is required. | |
| /// </summary> | |
| public string Version { get; } | |
| public ModDependencyAttribute(string modID, string version = null) | |
| { | |
| if (modID == null) throw new ArgumentNullException(nameof(modID)); | |
| if (!Utility.IsValidModID(modID)) throw new ArgumentException( | |
| $"'{ modID }' is not a valid mod ID", nameof(modID)); | |
| ModID = modID; | |
| Version = version; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace Vintagestory.API.Common | |
| { | |
| [AttributeUsage(AttributeTargets.Class)] | |
| public class ModInfoAttribute : Attribute | |
| { | |
| // Not needed. DLL mods are implicitly Code mods. | |
| // public EnumModType Type { get; set; } = EnumModType.Code; | |
| /// <summary> The name of this mod. For example "My Example Mod". </summary> | |
| public string Name { get; } | |
| /// <summary> The ID of this mod. For example "myexamplemod". </summary> | |
| public string ModID { get; } | |
| /// <summary> The version of this mod. For example "2.10.4". </summary> | |
| public string Version { get; set; } | |
| /// <summary> A short description of what this mod does. </summary> | |
| public string Description { get; set; } | |
| /// <summary> Location of the website or project site of this mod. </summary> | |
| public string URL { get; set; } | |
| /// <summary> Names of people working on this mod. </summary> | |
| public string[] Authors { get; set; } | |
| /// <summary> Names of people contributing to this mod. </summary> | |
| public string[] Contributors { get; set; } | |
| public ModInfoAttribute(string name, string modID) | |
| { | |
| if (name == null) throw new ArgumentNullException(nameof(name)); | |
| if (modID == null) throw new ArgumentNullException(nameof(modID)); | |
| if (name.Length == 0) throw new ArgumentException( | |
| "name can't be empty", nameof(name)); | |
| if (!Utility.IsValidModID(modID)) throw new ArgumentException( | |
| $"'{ modID }' is not a valid mod ID", nameof(modID)); | |
| Name = name; | |
| ModID = modID; | |
| } | |
| public ModInfoAttribute(string name) | |
| : this(name, Utility.ToModID(name)) { } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Vintagestory.API.Common; | |
| namespace TestMod | |
| { | |
| [ModInfo("TestMod", | |
| Description = "Example mod for testing things", | |
| URL = "https://github.com/copygirl/TestMod", | |
| Authors = new []{ "copygirl" })] | |
| [ModDependency("api", "1.5.2")] | |
| [ModDependency("game", "1.5.2")] | |
| public class TestMod : ModBase | |
| { | |
| public override void Start(ICoreAPI api) | |
| { | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| namespace Vintagestory.API.Common | |
| { | |
| public static class Utility | |
| { | |
| /// <summary> | |
| /// Attempts to convert the specified mod name to a mod ID, stripping any | |
| /// non-alphanumerical (including spaces and dashes) and lowercasing letters. | |
| /// </summary> | |
| public static string ToModID(string name) | |
| { | |
| if (name == null) return null; | |
| var sb = new StringBuilder(name.Length); | |
| for (var i = 0; i < name.Length; i++) { | |
| var chr = name[i]; | |
| if ((chr > 'a') && (chr < 'z')) | |
| sb.Append(chr); | |
| else if ((chr > 'A') && (chr < 'Z')) | |
| sb.Append(char.ToLower(chr)); | |
| else if ((chr > '0') && (chr < '9')) { | |
| if (i == 0) throw new ArgumentException( | |
| $"Can't convert '{ name }' to a mod ID automatically, because " + | |
| "it starts with a number, which is illegal", nameof(name)); | |
| sb.Append(chr); | |
| } // Otherwise, drop the character. | |
| } | |
| return sb.ToString(); | |
| } | |
| /// <summary> | |
| /// Returns whether the specified mod ID is valid. | |
| /// | |
| /// Tests if the string is non-null, has a length of at least 1, starts with | |
| /// a basic lowercase letter and contains only lowercase letters and numbers. | |
| /// </summary> | |
| public static bool IsValidModID(string str) | |
| { | |
| if ((str == null) || (str.Length == 0)) return false; | |
| for (var i = 0; i < str.Length; i++) { | |
| var chr = str[i]; | |
| if (((chr < 'a') || (chr > 'z')) | |
| && ((i == 0) || (chr < '0') || (chr > '9'))) | |
| return false; | |
| } | |
| return true; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment