Skip to content

Commit

Permalink
Add to source control
Browse files Browse the repository at this point in the history
  • Loading branch information
earalov committed Jul 9, 2017
1 parent 7eb5c11 commit 5203a2a
Show file tree
Hide file tree
Showing 15 changed files with 716 additions and 7 deletions.
60 changes: 60 additions & 0 deletions Attibutes/AbstractOptionsAttribute.cs
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; }


}
}
14 changes: 14 additions & 0 deletions Attibutes/ButtonAttribute.cs
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)
{

}
}
}
14 changes: 14 additions & 0 deletions Attibutes/CheckboxAttribute.cs
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)
{
}
}
}
15 changes: 15 additions & 0 deletions Attibutes/DontTranslateDescriptionAttribute.cs
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)
{

}
}
}
32 changes: 32 additions & 0 deletions Attibutes/DropDownAttribute.cs
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; }
}
}
10 changes: 10 additions & 0 deletions Attibutes/HideConditionAttribute.cs
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();
}
}
13 changes: 13 additions & 0 deletions Attibutes/LabelAttribute.cs
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)
{
}
}
}
20 changes: 20 additions & 0 deletions Attibutes/OptionsAttribute.cs
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; }
}
}
21 changes: 21 additions & 0 deletions Attibutes/SliderAttribute.cs
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; }
}
}
13 changes: 13 additions & 0 deletions Attibutes/TextFieldAttribute.cs
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)
{
}
}
}
31 changes: 31 additions & 0 deletions Extensions/CommonExtensions.cs
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];
}
}
}
Loading

0 comments on commit 5203a2a

Please sign in to comment.