Skip to content

Commit

Permalink
Added CVarContainer attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
weeeBox committed Jul 20, 2015
1 parent ad6f79f commit 48de3fa
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
6 changes: 6 additions & 0 deletions Project/Assets/Plugins/Lunar/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public static bool ExecCommand(string commandLine, bool manual = false)
throw new ArgumentNullException("commandLine");
}

if (s_sharedInstance == null)
{
Log.e("Can't execute command: app is not initialized");
return false;
}

return Imp.ExecCommand(commandLine, manual);
}

Expand Down
4 changes: 0 additions & 4 deletions Project/Assets/Plugins/Lunar/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ namespace LunarPluginInternal
{
static class Config
{
public static readonly bool isFullFeatured;

public static readonly bool isUnityFree;
public static readonly bool isUnityPro;

Expand All @@ -40,15 +38,13 @@ static Config()
{
try
{
isFullFeatured = Application.isEditor;
isDebugBuild = Debug.isDebugBuild;
isUnityPro = Application.HasProLicense();
isUnityFree = !isUnityPro;
isUnityBuild = true;
}
catch (Exception)
{
isFullFeatured = true;
isUnityFree = isUnityPro = false;
isDebugBuild = true;
isUnityBuild = false;
Expand Down
32 changes: 14 additions & 18 deletions Project/Assets/Plugins/Lunar/Console/CVar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,10 @@ private CVar(string name, CVarType type, CFlags flags)

#region Registry

[System.Diagnostics.Conditional("UNITY_EDITOR")]
private static void Register(CVar cvar)
{
if (Config.isFullFeatured)
{
CRegistery.Register(cvar);
}
CRegistery.Register(cvar);
}

#endregion
Expand All @@ -245,24 +243,22 @@ private static void Register(CVar cvar)

#region Delegates

[System.Diagnostics.Conditional("UNITY_EDITOR")]
public void AddDelegate(CVarChangedDelegate del)
{
if (Config.isFullFeatured)
if (del == null)
{
if (del == null)
{
throw new ArgumentNullException("del");
}
throw new ArgumentNullException("del");
}

if (m_delegateList == null)
{
m_delegateList = new CVarChangedDelegateList(1);
m_delegateList.Add(del);
}
else if (!m_delegateList.Contains(del))
{
m_delegateList.Add(del);
}
if (m_delegateList == null)
{
m_delegateList = new CVarChangedDelegateList(1);
m_delegateList.Add(del);
}
else if (!m_delegateList.Contains(del))
{
m_delegateList.Add(del);
}
}

Expand Down
12 changes: 12 additions & 0 deletions Project/Assets/Plugins/Lunar/Console/CVarContainerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

using LunarPluginInternal;

namespace LunarPlugin
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class CVarContainerAttribute : Attribute
{
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 28 additions & 4 deletions Project/Assets/Plugins/Lunar/Console/RuntimeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// limitations under the License.
//

using System;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -51,10 +51,9 @@ public static List<CCommand> ResolveCommands()
{
foreach (Type type in assembly.GetTypes())
{
object[] attrs = type.GetCustomAttributes(typeof(CCommandAttribute), true);
if (attrs != null && attrs.Length == 1)
CCommandAttribute cmdAttr = GetCustomAttribute<CCommandAttribute>(type);
if (cmdAttr != null)
{
CCommandAttribute cmdAttr = (CCommandAttribute)attrs[0];
string commandName = cmdAttr.Name;
if (!IsCorrectPlatform(cmdAttr.Flags))
{
Expand All @@ -80,6 +79,25 @@ public static List<CCommand> ResolveCommands()
Log.e("Unable to register command: name={0} type={1}", commandName, type);
}
}
else
{
CVarContainerAttribute cvarAttr = GetCustomAttribute<CVarContainerAttribute>(type);
if (cvarAttr != null)
{
try
{
FieldInfo[] fields = type.GetFields(BindingFlags.Static|BindingFlags.Public);
if (fields != null && fields.Length > 0)
{
fields[0].GetValue(null);
}
}
catch (Exception e)
{
Log.error(e, "Unable to initialize cvar container: {0}", type);
}
}
}
}
}
}
Expand All @@ -102,6 +120,12 @@ public static List<CCommand> ResolveCommands()
return list;
}

private static T GetCustomAttribute<T>(Type type) where T : Attribute
{
object[] attributes = type.GetCustomAttributes(typeof(T), false);
return attributes != null && attributes.Length == 1 ? attributes[0] as T : null;
}

public static void ResolveOptions(CCommand command)
{
ResolveOptions(command, command.GetType());
Expand Down

0 comments on commit 48de3fa

Please sign in to comment.