This commit is contained in:
2026-07-09 21:33:33 +02:00
commit 84a2a365f3
2364 changed files with 950134 additions and 0 deletions
@@ -0,0 +1,17 @@
using System;
using UnityEngine;
namespace VOID.Generic.DynamicValue
{
[Serializable]
public class DynamicFormula : DynamicText
{
public new float Get()
{
ExpressionEvaluator.Evaluate<float>(this, out var result);
return result;
}
public static implicit operator float(DynamicFormula dynamicFormula) => dynamicFormula.Get();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 64a6716b881e4d07bf851d05f640fe67
timeCreated: 1723902288
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Sirenix.OdinInspector;
using UnityEngine;
using Object = UnityEngine.Object;
namespace VOID.Generic.DynamicValue
{
[Serializable]
public class DynamicText
{
// STATIC - Cached markers
private static List<IMarker> _markers =
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => typeof(IMarker).IsAssignableFrom(type) && type.IsClass)
.Select(Activator.CreateInstance)
.Cast<IMarker>()
.ToList();
private static List<IMarkerGroup> _groupMarkers =
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => typeof(IMarkerGroup).IsAssignableFrom(type) && type.IsClass)
.Select(Activator.CreateInstance)
.Cast<IMarkerGroup>()
.ToList();
#if UNITY_EDITOR
[ShowInInspector, Searchable, ListDrawerSettings(IsReadOnly = true)]
private static List<MarkerDummyString> _allMarkers = new List<string>()
.Union(_markers.Select(m => m.Marker()))
.Union(_groupMarkers.SelectMany(mg => mg.Markers().Select(m => $"{mg.Group()}.{m}")))
.Select(str => new MarkerDummyString(str))
.ToList();
#endif
// User defined formula or text with markers
[SerializeField, TextArea(1,20)] private string _dynamicValue;
public string dynamicValue => _dynamicValue;
// TODO: Sources are needed to work, but so far will be given only via script
private List<Object> _sources = new();
// Parsed
private bool _isParsed;
private string _stringFormat;
private List<Func<string>> _valueGetters;
public void AddSource(Object obj) => _sources.Add(obj);
private void Parse()
{
_isParsed = true;
_valueGetters = Regex.Matches(_dynamicValue, "{(.*?)}")
.Select(match => match.Value)
.Select(stringMarker =>
{
stringMarker = stringMarker.Trim('{', '}');
var dotIndex = stringMarker.LastIndexOf('.');
var groupName = string.Join(string.Empty, stringMarker.Take(dotIndex));
var markerName = string.Join(string.Empty, stringMarker.Skip(dotIndex+1));
return TryParseMarker(markerName) ?? TryParseMarkerGroup(groupName, markerName) ?? (() => $"{{{stringMarker}}}");
})
.ToList();
var matchIndex = 0;
_stringFormat = Regex.Replace(_dynamicValue, "{(.*?)}", _ => $"{{{matchIndex++}}}");
}
private Func<string> TryParseMarker(string markerName)
{
var marker = _markers.FirstOrDefault(m => m.Marker().Equals(markerName));
if (marker == null) return null;
var source = _sources.FirstOrDefault(source => marker.SourceType().IsInstanceOfType(source));
if (source == null) return null;
return marker.Getter(source);
}
private Func<string> TryParseMarkerGroup(string groupName, string markerName)
{
var markerGroup = _groupMarkers.FirstOrDefault(mg => mg.Group().Equals(groupName) && mg.Markers().Contains(markerName));
if (markerGroup == null) return null;
var source = _sources.FirstOrDefault(source => markerGroup.SourceType().IsInstanceOfType(source));
if (source == null) return null;
return markerGroup.Getter(source, markerName);
}
public string Get()
{
#if UNITY_EDITOR
_isParsed = false;
#endif
if (!_isParsed) Parse();
return string.Format(_stringFormat, _valueGetters.Select(getter => getter.Invoke()).ToArray<object>());
}
public static implicit operator string(DynamicText dynamicText) => dynamicText.Get();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3db9e944765244689e919b03fb2497ac
timeCreated: 1723749241
@@ -0,0 +1,12 @@
using System;
namespace VOID.Generic.DynamicValue
{
public interface IMarker
{
public string Marker();
public Type SourceType();
public Type ResultType();
public Func<string> Getter(object source);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 746396e224004f6089ad5bfad97448f7
timeCreated: 1723749975
@@ -0,0 +1,13 @@
using System;
namespace VOID.Generic.DynamicValue
{
public interface IMarkerGroup
{
public string Group();
public string[] Markers();
public Type SourceType();
public Type ResultType();
public Func<string> Getter(object source, string marker);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 61682dac33b247bd9831c62f9f7add58
timeCreated: 1723754442
@@ -0,0 +1,12 @@
using Sirenix.OdinInspector;
namespace VOID.Generic.DynamicValue
{
[HideLabel]
[HideReferenceObjectPicker]
public class MarkerDummyString
{
[ShowInInspector, ReadOnly, HideLabel] private readonly string _str;
public MarkerDummyString(string str) => _str = str;
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 08fc82a76d044adca722d05297adf9bb
timeCreated: 1723900808
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ecc673319e824096bb51f469672cba6a
timeCreated: 1723753635
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Abstract;
namespace VOID.Generic.DynamicValue.Markers
{
public class BasicResourceBaseMaxMarker : IMarkerGroup
{
public string Group() => "BasicResource.BaseMax";
public string[] Markers() =>
Enum.GetValues(typeof(BasicResourceType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(AbstractObject);
public Type ResultType() => typeof(float);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<BasicResourceType>(marker, true);
return () => ((AbstractObject)source).basicData.baseMaxResources.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4942698e6fd04edf90b69fff542fa9db
timeCreated: 1723807295
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Abstract;
namespace VOID.Generic.DynamicValue.Markers
{
public class BasicResourceCurrentMarker : IMarkerGroup
{
public string Group() => "BasicResource.Current";
public string[] Markers() =>
Enum.GetValues(typeof(BasicResourceType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(AbstractObject);
public Type ResultType() => typeof(float);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<BasicResourceType>(marker, true);
return () => ((AbstractObject)source).basicData.currentResources.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 76479c858a8e4cf994c9866a76a135d5
timeCreated: 1723753669
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Abstract;
namespace VOID.Generic.DynamicValue.Markers
{
public class BasicResourceMaxMarker : IMarkerGroup
{
public string Group() => "BasicResource.Max";
public string[] Markers() =>
Enum.GetValues(typeof(BasicResourceType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(AbstractObject);
public Type ResultType() => typeof(float);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<BasicResourceType>(marker, true);
return () => ((AbstractObject)source).basicData.maxResources.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4c2fb06317774fc08a985354eae2fead
timeCreated: 1723758688
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Unit;
namespace VOID.Generic.DynamicValue.Markers
{
public class MainAttributeBaseMarkerGroup : IMarkerGroup
{
public string Group() => "MainAttribute.Base";
public string[] Markers() =>
Enum.GetValues(typeof(MainAttributeType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(UnitObject);
public Type ResultType() => typeof(int);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<MainAttributeType>(marker, true);
return () => ((UnitObject)source).unitData.baseMainAttributes.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7e6e5f60bff449cb99425846d4c41d4c
timeCreated: 1723807495
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Unit;
namespace VOID.Generic.DynamicValue.Markers
{
public class MainAttributeCurrentMarkerGroup : IMarkerGroup
{
public string Group() => "MainAttribute.Current";
public string[] Markers() =>
Enum.GetValues(typeof(MainAttributeType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(UnitObject);
public Type ResultType() => typeof(int);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<MainAttributeType>(marker, true);
return () => ((UnitObject)source).unitData.mainAttributes.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8ddcb87940f5471da872893714bd03ce
timeCreated: 1723753669
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.DynamicValue;
namespace VOID.Generic.Objects.Unit.DataCalculator
{
public class SubAttributeBaseMarkerGroup : IMarkerGroup
{
public string Group() => "SubAttribute.Base";
public string[] Markers() =>
Enum.GetValues(typeof(SubAttributeType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(UnitObject);
public Type ResultType() => typeof(int);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<SubAttributeType>(marker, true);
return () => ((UnitObject)source).unitData.baseSubAttributes.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 75058bf984b2442cbc65dc519da68e68
timeCreated: 1723807746
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.DynamicValue;
namespace VOID.Generic.Objects.Unit.DataCalculator
{
public class SubAttributeCurrentMarkerGroup : IMarkerGroup
{
public string Group() => "SubAttribute.Current";
public string[] Markers() =>
Enum.GetValues(typeof(SubAttributeType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(UnitObject);
public Type ResultType() => typeof(int);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<SubAttributeType>(marker, true);
return () => ((UnitObject)source).unitData.subAttributes.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2bba575b15d6482d9ea8391dc02d85fa
timeCreated: 1723753669
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Unit;
namespace VOID.Generic.DynamicValue.Markers
{
public class UnitResourceBaseMaxMarkerGroup : IMarkerGroup
{
public string Group() => "UnitResource.BaseMax";
public string[] Markers() =>
Enum.GetValues(typeof(UnitResourceType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(UnitObject);
public Type ResultType() => typeof(float);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<UnitResourceType>(marker, true);
return () => ((UnitObject)source).unitData.baseMaxResources.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9cfb5059ccdc44529a34b7e69db2a4a4
timeCreated: 1723808019
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Unit;
namespace VOID.Generic.DynamicValue.Markers
{
public class UnitResourceCurrentMarkerGroup : IMarkerGroup
{
public string Group() => "UnitResource.Current";
public string[] Markers() =>
Enum.GetValues(typeof(UnitResourceType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(UnitObject);
public Type ResultType() => typeof(float);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<UnitResourceType>(marker, true);
return () => ((UnitObject)source).unitData.currentResources.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 510651cddd9d46faa1ed0347575a882c
timeCreated: 1723753669
@@ -0,0 +1,29 @@
using System;
using System.Globalization;
using System.Linq;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Unit;
namespace VOID.Generic.DynamicValue.Markers
{
public class UnitResourceMaxMarkerGroup : IMarkerGroup
{
public string Group() => "UnitResource.Max";
public string[] Markers() =>
Enum.GetValues(typeof(UnitResourceType))
.OfType<Enum>()
.Select(@enum => @enum.ToString())
.ToArray();
public Type SourceType() => typeof(UnitObject);
public Type ResultType() => typeof(float);
public Func<string> Getter(object source, string marker)
{
var @enum = Enum.Parse<UnitResourceType>(marker, true);
return () => ((UnitObject)source).unitData.maxResources.Get(@enum).ToString(CultureInfo.InvariantCulture);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 789b65f3c3d14a26bb5393fe4b2f101e
timeCreated: 1723759932