Skip to content

Commit

Permalink
Added define symbols that remove console functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Orange-Panda committed Jun 25, 2023
1 parent 6f24aba commit fb9b6b5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 62 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
- Only one Object can be the target of a method. The first object found will be used.
- This works for methods, fields, and properties.
- Added `LogStyling` parameter to the `Console.Log` method which will apply common styling such as warning or error formatting.
- Added `VESPA_DISABLE` define symbol which when present will remove core functionality of the console. Not present by default
- Added `VESPA_DISABLE_NATIVE_COMMANDS` define symbol which when present will remove the native commands that come with the console. Not present by default.

### Removed

Expand Down
8 changes: 7 additions & 1 deletion Runtime/HLAPI/DevConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ public static class DevConsole
/// <summary>
/// Whether the console is currently enabled and open.
/// </summary>
#if !VESPA_DISABLE
public static bool ConsoleActive { get; set; }

#else
// ReSharper disable once ValueParameterNotUsed
public static bool ConsoleActive { get => false; set { } }
#endif
public static readonly NativeConsole console = new NativeConsole();

static DevConsole()
Expand All @@ -17,6 +21,7 @@ static DevConsole()
console.AliasSet = Aliases.aliasSet;
}

#if !VESPA_DISABLE
[RuntimeInitializeOnLoadMethod]
private static void CreateConsole()
{
Expand All @@ -41,6 +46,7 @@ private static void CreateConsole()
}
}
}
#endif

///<inheritdoc cref="Console.Log"/>
public static void Log(string text, Console.LogStyling logStyling = Console.LogStyling.Plain)
Expand Down
6 changes: 4 additions & 2 deletions Runtime/HLAPI/NativeCommands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -12,6 +11,7 @@ namespace LMirman.VespaIO
/// </summary>
public static class NativeCommands
{
#if !VESPA_DISABLE && !VEPSA_DISABLE_NATIVE_COMMANDS
private const int HelpPageLength = 10;

[VespaCommand("quit", Name = "Quit Application", Description = "Closes the application", ManualPriority = 70)]
Expand Down Expand Up @@ -67,7 +67,7 @@ private static AutofillValue GetSceneAutofillValue(AutofillBuilder autofillBuild
SceneNameList.Add(Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i)));
}
}

if (autofillBuilder.RelevantParameterIndex != 0)
{
return null;
Expand Down Expand Up @@ -331,5 +331,7 @@ public static void ListAlias(int pageNum = 0)
DevConsole.Log("--- Use \"alias_list {page #}\" for more ---");
}
#endregion

#endif
}
}
120 changes: 63 additions & 57 deletions Runtime/LLAPI/Data Types/Invocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public Invocation(string input, CommandSet commandSet)

public InvokeResult RunInvocation(Console console, out Exception exception)
{
#if !VESPA_DISABLE
try
{
exception = null;
Expand All @@ -183,12 +184,12 @@ public InvokeResult RunInvocation(Console console, out Exception exception)
}
else if (invocationType == Command.InvocationType.Property)
{
InvokeProperty(console);
InvokeProperty();
return InvokeResult.Success;
}
else if (invocationType == Command.InvocationType.Field)
{
InvokeField(console);
InvokeField();
return InvokeResult.Success;
}
else
Expand All @@ -201,72 +202,77 @@ public InvokeResult RunInvocation(Console console, out Exception exception)
exception = e.InnerException ?? e;
return InvokeResult.ErrorException;
}
}

private void InvokeProperty(Console console)
{
object prevValue = propertyInfo.CanRead ? propertyInfo.GetValue(targetObject) : string.Empty;
if (!propertyInfo.CanWrite)
{
console.Log($"{GetLogValue("Property", propertyInfo.Name)} has readonly value \"{prevValue}\"");
return;
}
else if (arguments.Length == 0 && !propertyInfo.CanRead)
{
console.Log($"Property \"{propertyInfo.Name}\" can only have its value set.", Console.LogStyling.Error);
return;
}
else if (arguments.Length == 0)
void InvokeProperty()
{
console.Log($"{GetLogValue("Property", propertyInfo.Name)} has value \"{prevValue}\"");
return;
}
object prevValue = propertyInfo.CanRead ? propertyInfo.GetValue(targetObject) : string.Empty;
if (!propertyInfo.CanWrite)
{
console.Log($"{GetLogValue("Property", propertyInfo.Name)} has readonly value \"{prevValue}\"");
return;
}
else if (arguments.Length == 0 && !propertyInfo.CanRead)
{
console.Log($"Property \"{propertyInfo.Name}\" can only have its value set.", Console.LogStyling.Error);
return;
}
else if (arguments.Length == 0)
{
console.Log($"{GetLogValue("Property", propertyInfo.Name)} has value \"{prevValue}\"");
return;
}

string inputValue = arguments[0].text;
TypeConverter typeConverter = TypeDescriptor.GetConverter(propertyInfo.PropertyType);
if (typeConverter.IsValid(inputValue))
{
object newValue = typeConverter.ConvertFrom(inputValue);
propertyInfo.SetValue(targetObject, newValue);
console.Log(propertyInfo.CanRead
? $"Set {GetLogValue("property", propertyInfo.Name)} from \"{prevValue}\" to \"{newValue}\""
: $"Set {GetLogValue("property", propertyInfo.Name)} to \"{newValue}\"");
}
else
{
console.Log($"Cannot set value of property \"{propertyInfo.Name}\" ({propertyInfo.PropertyType}) to \"{inputValue}\"", Console.LogStyling.Error);
string inputValue = arguments[0].text;
TypeConverter typeConverter = TypeDescriptor.GetConverter(propertyInfo.PropertyType);
if (typeConverter.IsValid(inputValue))
{
object newValue = typeConverter.ConvertFrom(inputValue);
propertyInfo.SetValue(targetObject, newValue);
console.Log(propertyInfo.CanRead
? $"Set {GetLogValue("property", propertyInfo.Name)} from \"{prevValue}\" to \"{newValue}\""
: $"Set {GetLogValue("property", propertyInfo.Name)} to \"{newValue}\"");
}
else
{
console.Log($"Cannot set value of property \"{propertyInfo.Name}\" ({propertyInfo.PropertyType}) to \"{inputValue}\"", Console.LogStyling.Error);
}
}
}

private void InvokeField(Console console)
{
object prevValue = fieldInfo.GetValue(targetObject);
if (fieldInfo.IsInitOnly)
{
console.Log($"{GetLogValue("Field", fieldInfo.Name)} has readonly value \"{prevValue}\"");
return;
}
else if (arguments.Length == 0)
void InvokeField()
{
console.Log($"{GetLogValue("Field", fieldInfo.Name)} has value \"{prevValue}\"");
return;
}
object prevValue = fieldInfo.GetValue(targetObject);
if (fieldInfo.IsInitOnly)
{
console.Log($"{GetLogValue("Field", fieldInfo.Name)} has readonly value \"{prevValue}\"");
return;
}
else if (arguments.Length == 0)
{
console.Log($"{GetLogValue("Field", fieldInfo.Name)} has value \"{prevValue}\"");
return;
}

string inputValue = arguments[0].text;
TypeConverter typeConverter = TypeDescriptor.GetConverter(fieldInfo.FieldType);
if (typeConverter.IsValid(inputValue))
{
object newValue = typeConverter.ConvertFrom(inputValue);
fieldInfo.SetValue(targetObject, newValue);
console.Log($"Set {GetLogValue("field", fieldInfo.Name)} from \"{prevValue}\" to \"{newValue}\"");
}
else
{
console.Log($"Cannot set value of field \"{fieldInfo.Name}\" ({fieldInfo.FieldType}) to \"{inputValue}\"", Console.LogStyling.Error);
string inputValue = arguments[0].text;
TypeConverter typeConverter = TypeDescriptor.GetConverter(fieldInfo.FieldType);
if (typeConverter.IsValid(inputValue))
{
object newValue = typeConverter.ConvertFrom(inputValue);
fieldInfo.SetValue(targetObject, newValue);
console.Log($"Set {GetLogValue("field", fieldInfo.Name)} from \"{prevValue}\" to \"{newValue}\"");
}
else
{
console.Log($"Cannot set value of field \"{fieldInfo.Name}\" ({fieldInfo.FieldType}) to \"{inputValue}\"", Console.LogStyling.Error);
}
}
#else
exception = null;
return InvokeResult.ErrorConsoleInactive;
#endif
}

private static readonly StringBuilder LogValueBuilder = new StringBuilder();

private string GetLogValue(string invokeType, string variableName)
{
LogValueBuilder.Clear();
Expand Down
2 changes: 0 additions & 2 deletions Runtime/LLAPI/VespaFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Object = UnityEngine.Object;

namespace LMirman.VespaIO
{
Expand Down

0 comments on commit fb9b6b5

Please sign in to comment.