forked from bloodypenguin/OptionsFramework
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
716 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public abstract class AbstractOptionsAttribute : Attribute | ||
{ | ||
protected AbstractOptionsAttribute(string description, string group, string actionClass, string actionMethod) | ||
{ | ||
Description = description; | ||
Group = group; | ||
ActionClass = actionClass; | ||
ActionMethod = actionMethod; | ||
} | ||
|
||
public string Description { get; } | ||
public string Group { get; } | ||
|
||
public Action<T> Action<T>() | ||
{ | ||
if (ActionClass == null || ActionMethod == null) | ||
{ | ||
return s => { }; | ||
} | ||
var method = Util.FindType(ActionClass).GetMethod(ActionMethod, BindingFlags.Public | BindingFlags.Static); | ||
if (method == null) | ||
{ | ||
return s => { }; | ||
} | ||
return s => | ||
{ | ||
method.Invoke(null, new object[] { s }); | ||
}; | ||
} | ||
|
||
public Action Action() | ||
{ | ||
if (ActionClass == null || ActionMethod == null) | ||
{ | ||
return () => { }; | ||
} | ||
var method = Util.FindType(ActionClass).GetMethod(ActionMethod, BindingFlags.Public | BindingFlags.Static); | ||
if (method == null) | ||
{ | ||
return () => { }; | ||
} | ||
return () => | ||
{ | ||
method.Invoke(null, new object[] { }); | ||
}; | ||
} | ||
|
||
private string ActionClass { get; } | ||
|
||
private string ActionMethod { get; } | ||
|
||
|
||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class ButtonAttribute : AbstractOptionsAttribute | ||
{ | ||
public ButtonAttribute(string description, string group, string actionClass = null, string actionMethod = null) : | ||
base(description, group, actionClass, actionMethod) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class CheckboxAttribute : AbstractOptionsAttribute | ||
{ | ||
|
||
public CheckboxAttribute(string description, string group = null, string actionClass = null, string actionMethod = null) : | ||
base(description, group, actionClass, actionMethod) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.ComponentModel; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.All)] | ||
public class DontTranslateDescriptionAttribute : DescriptionAttribute | ||
{ | ||
public DontTranslateDescriptionAttribute(string description) : | ||
base(description) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class DropDownAttribute : AbstractOptionsAttribute | ||
{ | ||
public DropDownAttribute(string description, string itemsClass, string group = null, string actionClass = null, | ||
string actionMethod = null) : base(description, group, actionClass, actionMethod) | ||
{ | ||
ItemsClass = itemsClass; | ||
} | ||
|
||
public IList<KeyValuePair<string, int>> GetItems(Func<string, string> translator = null) | ||
{ | ||
var type = Util.FindType(ItemsClass); | ||
var enumValues = Enum.GetValues(type); | ||
return (from object enumValue in enumValues | ||
let code = (int) enumValue | ||
let memInfo = type.GetMember(Enum.GetName(type, enumValue)) | ||
let attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false) | ||
let description = ((DescriptionAttribute) attributes[0]).Description | ||
let translatedDesctiption = translator == null ? description : translator.Invoke(description) | ||
select new KeyValuePair<string, int>(translatedDesctiption, code)).ToList(); | ||
} | ||
|
||
private string ItemsClass { get; } | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public abstract class HideConditionAttribute : Attribute | ||
{ | ||
public abstract bool IsHidden(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class LabelAttribute : AbstractOptionsAttribute | ||
{ | ||
public LabelAttribute(string description, string group) : | ||
base(description, group, null, null) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] | ||
public class OptionsAttribute : Attribute | ||
{ | ||
public OptionsAttribute(string fileName, string legacyFileName = "") | ||
{ | ||
FileName = fileName; | ||
LegacyFileName = legacyFileName; | ||
} | ||
|
||
//file name in local app data | ||
public string FileName { get; } | ||
|
||
//file name in Cities: Skylines folder | ||
public string LegacyFileName { get; } | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class SliderAttribute : AbstractOptionsAttribute | ||
{ | ||
public SliderAttribute(string description, float min, float max, float step, string group = null, string actionClass = null, string actionMethod = null) : base(description, group, actionClass, actionMethod) | ||
{ | ||
Min = min; | ||
Max = max; | ||
Step = step; | ||
} | ||
|
||
public float Min { get; private set; } | ||
|
||
public float Max { get; private set; } | ||
|
||
public float Step { get; private set; } | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace OptionsFramework.Attibutes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class TextfieldAttribute : AbstractOptionsAttribute | ||
{ | ||
public TextfieldAttribute(string description, string group = null, string actionClass = null, | ||
string actionMethod = null) : base(description, group, actionClass, actionMethod) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using OptionsFramework.Attibutes; | ||
|
||
namespace OptionsFramework.Extensions | ||
{ | ||
public static class CommonExtensions | ||
{ | ||
public static string GetPropertyDescription<T>(this T value, string propertyName) | ||
{ | ||
var fi = value.GetType().GetProperty(propertyName); | ||
var attributes = | ||
(AbstractOptionsAttribute[]) fi.GetCustomAttributes(typeof(AbstractOptionsAttribute), false); | ||
return attributes.Length > 0 ? attributes[0].Description : throw new Exception($"Property {propertyName} wasn't annotated with AbstractOptionsAttribute"); | ||
} | ||
|
||
public static string GetPropertyGroup<T>(this T value, string propertyName) | ||
{ | ||
var fi = value.GetType().GetProperty(propertyName); | ||
var attributes = | ||
(AbstractOptionsAttribute[]) fi.GetCustomAttributes(typeof(AbstractOptionsAttribute), false); | ||
return attributes.Length > 0 ? attributes[0].Group : throw new Exception($"Property {propertyName} wasn't annotated with AbstractOptionsAttribute"); | ||
} | ||
|
||
public static TR GetAttribute<T, TR>(this T value, string propertyName)where TR : Attribute | ||
{ | ||
var fi = value.GetType().GetProperty(propertyName); | ||
var attributes = (TR[])fi.GetCustomAttributes(typeof(TR), false); | ||
return attributes.Length != 1 ? null : attributes[0]; | ||
} | ||
} | ||
} |
Oops, something went wrong.