Skip to content

Release 0.3.0 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions .github/workflows/openai.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: Code Review

permissions:
contents: read
pull-requests: write

on:
pull_request:
pull_request_review_comment:
types: [created]

concurrency:
group:
${{ github.repository }}-${{ github.event.number || github.head_ref ||
github.sha }}-${{ github.workflow }}-${{ github.event_name ==
'pull_request_review_comment' && 'pr_comment' || 'pr' }}
cancel-in-progress: ${{ github.event_name != 'pull_request_review_comment' }}

jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: coderabbitai/openai-pr-reviewer@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
with:
debug: false
review_simple_changes: false
review_comment_lgtm: false
openai_heavy_model: gpt-3.5-turbo-16k
#openai_heavy_model: gpt-4
#openai_heavy_model: gpt-4-32k
path_filters: |
!**/*.pb.go
!**/*.lock
!**/*.cfg
!**/*.toml
!**/*.ini
!**/*.mod
!**/*.sum
!**/*.work
!**/*.json
!**/*.mmd
!**/*.svg
!**/*.png
!**/*.dot
!**/*.md5sum
!**/*.wasm
!**/gen/**
!**/_gen/**
!**/generated/**
!**/vendor/**
!**/*.png
!**/*.jpeg
!**/*.jpg
!**/*.tga
!**/*.fbx
!**/*.anim
!**/*.obj
!**/*.asset
!**/*.prefab
!**/*.mat
!**/*.meta
!**/*.asset
!**/*.unity
!**/*.unitypackage
!**/*.dll
!**/*.qtn
system_message: |
You are `@GLaDOS`, a language model trained by OpenAI with the voice
of Valve's Portal game character GLaDOS.
Your purpose is to act as a highly experienced software engineer and
unity developer working on a mobile game in C#.
Provide a thorough review of the code hunks and suggest code snippets to improve key areas such as:
- Logic: Provide specific examples where the logic can be simplified or optimized.
- Security: Highlight common security vulnerabilities to watch out for.
- Performance: Identify areas where performance improvements can be made.
- Data races: Point out potential data race conditions and suggest solutions.
- Consistency: Ensure consistent coding style and naming conventions.
- Error handling: Review error handling mechanisms and suggest improvements.
- Maintainability: Identify code smells and propose refactoring suggestions.
- Modularity: Evaluate the modularity of the codebase and suggest enhancements.
- Complexity: Identify complex code sections and propose simplifications.
- Optimization: Suggest optimizations for resource-intensive operations.
- Testing: Purpose potential automation test cases that can run in Unity EditMode and RuntimeMode.
Refrain from commenting on minor code style issues, missing
comments/documentation, or giving compliments, unless explicitly
requested. Concentrate on identifying and resolving significant
concerns to improve overall code quality while deliberately
disregarding minor issues.
Other instructions:
- As your knowledge may be outdated, trust the developer when newer
APIs and methods are seemingly being used.
- Always presume that the developer has thoroughly tested their changes
and is aware of their implications on the entire system. Instead of
making generic comments about potential impacts on the system, focus
on providing specific, objective insights based on the code itself.
Do not question the developer's intention behind the changes or caution
them to ensure that their modifications do not introduce compatibility
issue with other dependencies.

59 changes: 32 additions & 27 deletions Editor/EnumSelectorPropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,28 @@ namespace GameLoversEditor
/// {
/// }
/// </summary>
public abstract class EnumSelectorPropertyDrawer<T> : PropertyDrawer
public abstract class EnumSelectorPropertyDrawer<T> : PropertyDrawer
where T : Enum
{
private static readonly Dictionary<Type, string[]> _sortedEnums = new Dictionary<Type, string[]>();
private static readonly Dictionary<Type, GUIContent[]> _sortedEnums = new Dictionary<Type, GUIContent[]>();

private bool _errorFound;

/// <inheritdoc />
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);

var enumType = typeof(T);
var enumValues = GetSortedEnumConstants(enumType);
var selectionWidth = Mathf.Clamp(EditorGUIUtility.labelWidth, EditorGUIUtility.labelWidth, position.width * 0.33f);
var selectionRect = new Rect(position.width - selectionWidth, position.y, selectionWidth, position.height);
var selectionProperty = property.FindPropertyRelative("_selection");
var currentString = selectionProperty.stringValue;
var currentIndex = Array.IndexOf(enumValues, currentString);

EditorGUI.LabelField(position, label);

var currentIndex = string.IsNullOrWhiteSpace(currentString) ? 0 : Array.FindIndex(enumValues, s => s.text == currentString);

if (currentIndex != -1)
{
selectionProperty.stringValue = enumValues[EditorGUI.Popup(selectionRect, currentIndex, enumValues)];
selectionProperty.stringValue = enumValues[EditorGUI.Popup(position, label, currentIndex, enumValues)].text;

_errorFound = false;
}
else
Expand All @@ -52,37 +48,46 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
if (!_errorFound)
{
var targetObject = selectionProperty.serializedObject.targetObject;

Debug.LogError($"Invalid enum constant: {enumType.Name}.{currentString} in object {targetObject.name} of type: {targetObject.GetType().Name}");

_errorFound = true;
}

var color = GUI.contentColor;
var finalArray = new[] { "Invalid: " + currentString }.Concat(enumValues).ToArray();
var finalArray = new[] { new GUIContent("Invalid: " + currentString) }.Concat(enumValues).ToArray();

GUI.contentColor = Color.red;
var newSelection = EditorGUI.Popup(selectionRect, 0, finalArray);
var newSelection = EditorGUI.Popup(position, label, 0, finalArray);
GUI.contentColor = color;

if (newSelection > 0)
{
selectionProperty.stringValue = finalArray[newSelection];
selectionProperty.stringValue = finalArray[newSelection].text;
}
}

EditorGUI.EndProperty();
}
private string[] GetSortedEnumConstants(Type enumType)

private GUIContent[] GetSortedEnumConstants(Type enumType)
{
if (!_sortedEnums.TryGetValue(enumType, out var values))
if (!_sortedEnums.TryGetValue(enumType, out var content))
{
values = Enum.GetNames(enumType);
var values = Enum.GetNames(enumType);

content = new GUIContent[values.Length];

Array.Sort(values);
_sortedEnums.Add(enumType, values);

for (var i = 0; i < values.Length; i++)
{
content[i] = new GUIContent(values[i]);
}

_sortedEnums.Add(enumType, content);
}
return values;
return content;
}
}
}
33 changes: 33 additions & 0 deletions Editor/ReadOnlyPropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEditor;
using UnityEngine;
using GameLovers;

// ReSharper disable once CheckNamespace

namespace GameLoversEditor
{
/// <summary>
/// This class contain custom drawer for ReadOnly attribute.
/// </summary>
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyPropertyDrawer : PropertyDrawer
{
/// <summary>
/// Unity method for drawing GUI in Editor
/// </summary>
/// <param name="position">Position.</param>
/// <param name="property">Property.</param>
/// <param name="label">Label.</param>
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Saving previous GUI enabled value
var previousGUIState = GUI.enabled;
// Disabling edit for property
GUI.enabled = false;
// Drawing Property
EditorGUI.PropertyField(position, property, label);
// Setting old GUI enabled value
GUI.enabled = previousGUIState;
}
}
}
3 changes: 3 additions & 0 deletions Editor/ReadOnlyPropertyDrawer.cs.meta

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

35 changes: 19 additions & 16 deletions Runtime/EnumSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,30 @@ public interface IEnumSelector
/// </summary>
string GetSelectionString();
}

/// <summary>
/// The EnumSelector <typeparamref name="T"/> serves as a dropdown selection field that offers all enum values of <typeparamref name="T"/>.
/// It stores the enum name instead of the enum value, to prevent pointing to the wrong enum when new values are added or removed
/// </summary>
[Serializable]
public abstract class EnumSelector<T> : IEnumSelector where T : Enum
public class EnumSelector<T> : IEnumSelector where T : Enum
{
[SerializeField, HideInInspector] private string _selection = "";

public static readonly string[] EnumNames = Enum.GetNames(typeof(T));
public static readonly T[] EnumValues = (T[])Enum.GetValues(typeof(T));
public static readonly Dictionary<string, T> EnumDictionary = new Dictionary<string, T>();

private EnumSelector() {}

private EnumSelector()
{
SetSelection(EnumValues[0]);
}

protected EnumSelector(T data)
{
SetSelection(data);
}

/// <inheritdoc />
public int GetSelectedIndex()
{
Expand All @@ -58,24 +61,24 @@ public int GetSelectedIndex()
return i;
}
}

Debug.LogError($"Could not load enum for string: {_selection}");

return -1;
}

/// <inheritdoc />
public bool HasValidSelection()
{
return GetSelectedIndex() != -1;
}

/// <inheritdoc />
public string GetSelectionString()
{
return _selection;
}

/// <summary>
/// Requests the enum selected value
/// </summary>
Expand All @@ -85,21 +88,21 @@ public T GetSelection()
{
return enumConstant;
}

enumConstant = EnumValues[GetSelectedIndex()];
EnumDictionary.Add(_selection, enumConstant);

return enumConstant;
}

/// <summary>
/// Sets the enum value to <paramref name="data"/>
/// </summary>
public void SetSelection(T data)
{
_selection = EnumNames[(int) (object) data];
_selection = EnumNames[(int)(object)data];
}

public static implicit operator T(EnumSelector<T> d)
{
return d.GetSelection();
Expand Down
Loading