Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
nomnomab committed Sep 12, 2022
0 parents commit 5aba23e
Show file tree
Hide file tree
Showing 100 changed files with 3,485 additions and 0 deletions.
71 changes: 71 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*
8 changes: 8 additions & 0 deletions Editor.meta

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

96 changes: 96 additions & 0 deletions Editor/ComponentField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Leopotam.EcsLite;
using Nomnom.EcsLiteDebugger.Editor.Fields;
using UnityEditor;
using UnityEngine.UIElements;

namespace Nomnom.EcsLiteDebugger.Editor {
public class ComponentField: VisualElement {
private static Dictionary<Type, Type> _lookup;

private readonly IEcsPool _pool;
private readonly int _entity;
private readonly FieldInfo _fieldInfo;

private VisualElement _field;
private IFieldGui _guiInstance;

static ComponentField() {
_lookup = new Dictionary<Type, Type>();

TypeCache.TypeCollection types = TypeCache.GetTypesDerivedFrom(typeof(FieldGui<>));

foreach (Type type in types) {
Type[] args = type.BaseType.GenericTypeArguments;

_lookup[args[0]] = type;
}
}

public ComponentField(IEcsPool pool, int entity, FieldInfo fieldInfo) {
_pool = pool;
_entity = entity;
_fieldInfo = fieldInfo;
}

public void Refresh() {
if (_field == null) {
_field = GatherValue(GetReference(), _fieldInfo);
Add(_field);
return;
}

UpdateValue();
}

private object GetReference() {
return _pool.GetRaw(_entity);
}

private VisualElement GatherValue(object item, FieldInfo fieldInfo) {
Type type = fieldInfo.FieldType;
VisualElement element;
bool hasLookup = _lookup.TryGetValue(type, out Type guiType);

if (hasLookup || TryFindBaseType(type, out guiType)) {
_guiInstance = (IFieldGui)Activator.CreateInstance(guiType, this);
element = _guiInstance.Create(item, fieldInfo);
} else {
element = new Label("Unsupported type");
}

return element;
}

private bool TryFindBaseType(Type type, out Type gui) {
foreach (Type lookupKey in _lookup.Keys) {
if (!lookupKey.IsAssignableFrom(type)) {
continue;
}

gui = _lookup[lookupKey];
return true;
}

gui = default;
return false;
}

public void UpdateReference(FieldInfo info, object value) {
object reference = GetReference();
info.SetValue(reference, value);
_pool.SetRaw(_entity, reference);
}

private void UpdateValue() {
if (_guiInstance == null || _guiInstance.IsEditing) {
return;
}

object value = _fieldInfo.GetValue(GetReference());
_guiInstance.UpdateValue(value);
}
}
}
3 changes: 3 additions & 0 deletions Editor/ComponentField.cs.meta

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

53 changes: 53 additions & 0 deletions Editor/ComponentGUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Reflection;
using Leopotam.EcsLite;
using UnityEngine.UIElements;

namespace Nomnom.EcsLiteDebugger.Editor {
internal class ComponentGUI: VisualElement {
private Type _type;
private FieldInfo[] _fields;
private EcsWorld _world;
private int _entity;
private bool _empty;

public ComponentGUI(Type type, int entity, EcsWorld world) {
_type = type;
_fields = _type.GetFields();
_world = world;
_entity = entity;
_empty = _fields.Length == 0;

if (_empty) {
Add(new Label("No fields to show"));
}
}

public void Refresh() {
if (_empty) {
return;
}

if (_fields.Length != childCount) {
Clear();

IEcsPool pool = _world.GetPoolByType(_type);

foreach (FieldInfo fieldInfo in _fields) {
ComponentField field = new ComponentField(pool, _entity, fieldInfo);
field.Refresh();
Add(field);
}
}

if (parent is Toggle {value: false}) {
return;
}

foreach (VisualElement visualElement in Children()) {
ComponentField field = visualElement as ComponentField;
field.Refresh();
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/ComponentGUI.cs.meta

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

3 changes: 3 additions & 0 deletions Editor/DuoVia.FuzzyStrings.meta

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

70 changes: 70 additions & 0 deletions Editor/DuoVia.FuzzyStrings/DiceCoefficientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Derived from http://www.codeguru.com/vb/gen/vb_misc/algorithms/article.php/c13137__1/Fuzzy-Matching-Demo-in-Access.htm
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

using System.Linq;

namespace DuoVia.FuzzyStrings
{
public static class DiceCoefficientExtensions
{
/// <summary>
/// Dice Coefficient based on bigrams. <br />
/// A good value would be 0.33 or above, a value under 0.2 is not a good match, from 0.2 to 0.33 is iffy.
/// </summary>
/// <param name="input"></param>
/// <param name="comparedTo"></param>
/// <returns></returns>
public static double DiceCoefficient(this string input, string comparedTo)
{
var ngrams = input.ToBiGrams();
var compareToNgrams = comparedTo.ToBiGrams();
return ngrams.DiceCoefficient(compareToNgrams);
}

/// <summary>
/// Dice Coefficient used to compare nGrams arrays produced in advance.
/// </summary>
/// <param name="nGrams"></param>
/// <param name="compareToNGrams"></param>
/// <returns></returns>
public static double DiceCoefficient(this string[] nGrams, string[] compareToNGrams)
{
int matches = nGrams.Intersect(compareToNGrams).Count();
if (matches == 0) return 0.0d;
double totalBigrams = nGrams.Length + compareToNGrams.Length;
return (2 * matches) / totalBigrams;
}

public static string[] ToBiGrams(this string input)
{
// nLength == 2
// from Jackson, return %j ja ac ck ks so on n#
// from Main, return #m ma ai in n#
input = SinglePercent + input + SinglePound;
return ToNGrams(input, 2);
}

public static string[] ToTriGrams(this string input)
{
// nLength == 3
// from Jackson, return %%j %ja jac ack cks kso son on# n##
// from Main, return ##m #ma mai ain in# n##
input = DoublePercent + input + DoublePount;
return ToNGrams(input, 3);
}

private static string[] ToNGrams(string input, int nLength)
{
int itemsCount = input.Length - 1;
string[] ngrams = new string[input.Length - 1];
for (int i = 0; i < itemsCount; i++) ngrams[i] = input.Substring(i, nLength);
return ngrams;
}

private const string SinglePercent = "%";
private const string SinglePound = "#";
private const string DoublePercent = "&&";
private const string DoublePount = "##";
}
}
3 changes: 3 additions & 0 deletions Editor/DuoVia.FuzzyStrings/DiceCoefficientExtensions.cs.meta

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

Loading

0 comments on commit 5aba23e

Please sign in to comment.