StringMotion.cs
Runtime/Adapters/Strings/StringMotion.cs
1 using System;
2 using Unity.Collections;
3 using MeatMotion.Adapters;
4
5 namespace MeatMotion
6 {
7 public enum ScrambleMode : byte { None, Uppercase, Lowercase, Numerals, All, Custom }
8
9 [Serializable]
10 public struct StringOptions : IMotionOptions, IEquatable<StringOptions>
11 {
12 public ScrambleMode ScrambleMode;
13 public bool RichTextEnabled;
14 public FixedString64Bytes CustomScrambleChars;
15 public uint RandomSeed;
16
17 public readonly bool Equals(StringOptions other) =>
18 ScrambleMode == other.ScrambleMode && RichTextEnabled == other.RichTextEnabled &&
19 CustomScrambleChars.Equals(other.CustomScrambleChars) && RandomSeed == other.RandomSeed;
20 public override readonly bool Equals(object obj) => obj is StringOptions other && Equals(other);
21 public override readonly int GetHashCode() => HashCode.Combine(ScrambleMode, RichTextEnabled, CustomScrambleChars, RandomSeed);
22 }
23
24 public static partial class MMotion
25 {
26 public static class String
27 {
28 public static MotionBuilder<FixedString32Bytes, StringOptions, FixedString32BytesMotionAdapter> Create32Bytes(
29 in FixedString32Bytes from, in FixedString32Bytes to, float duration)
30 => Create<FixedString32Bytes, StringOptions, FixedString32BytesMotionAdapter>(from, to, duration);
31
32 public static MotionBuilder<FixedString64Bytes, StringOptions, FixedString64BytesMotionAdapter> Create64Bytes(
33 in FixedString64Bytes from, in FixedString64Bytes to, float duration)
34 => Create<FixedString64Bytes, StringOptions, FixedString64BytesMotionAdapter>(from, to, duration);
35
36 public static MotionBuilder<FixedString128Bytes, StringOptions, FixedString128BytesMotionAdapter> Create128Bytes(
37 in FixedString128Bytes from, in FixedString128Bytes to, float duration)
38 => Create<FixedString128Bytes, StringOptions, FixedString128BytesMotionAdapter>(from, to, duration);
39
40 public static MotionBuilder<FixedString512Bytes, StringOptions, FixedString512BytesMotionAdapter> Create512Bytes(
41 in FixedString512Bytes from, in FixedString512Bytes to, float duration)
42 => Create<FixedString512Bytes, StringOptions, FixedString512BytesMotionAdapter>(from, to, duration);
43
44 public static MotionBuilder<FixedString4096Bytes, StringOptions, FixedString4096BytesMotionAdapter> Create4096Bytes(
45 in FixedString4096Bytes from, in FixedString4096Bytes to, float duration)
46 => Create<FixedString4096Bytes, StringOptions, FixedString4096BytesMotionAdapter>(from, to, duration);
47 }
48 }
49
50 public static partial class MotionBuilderExtensions
51 {
52 public static MotionBuilder<TValue, StringOptions, TAdapter> WithRichText<TValue, TAdapter>(
53 this MotionBuilder<TValue, StringOptions, TAdapter> builder, bool richTextEnabled = true)
54 where TValue : unmanaged where TAdapter : unmanaged, IMotionAdapter<TValue, StringOptions>
55 {
56 var options = builder.Options;
57 options.RichTextEnabled = richTextEnabled;
58 return builder.WithOptions(options);
59 }
60
61 public static MotionBuilder<TValue, StringOptions, TAdapter> WithScrambleChars<TValue, TAdapter>(
62 this MotionBuilder<TValue, StringOptions, TAdapter> builder, ScrambleMode scrambleMode)
63 where TValue : unmanaged where TAdapter : unmanaged, IMotionAdapter<TValue, StringOptions>
64 {
65 var options = builder.Options;
66 if (scrambleMode == ScrambleMode.Custom) throw new ArgumentException("Supply a custom alphabet.", nameof(scrambleMode));
67 options.ScrambleMode = scrambleMode;
68 return builder.WithOptions(options);
69 }
70
71 public static MotionBuilder<TValue, StringOptions, TAdapter> WithScrambleChars<TValue, TAdapter>(
72 this MotionBuilder<TValue, StringOptions, TAdapter> builder, FixedString64Bytes customScrambleChars)
73 where TValue : unmanaged where TAdapter : unmanaged, IMotionAdapter<TValue, StringOptions>
74 {
75 var options = builder.Options;
76 options.ScrambleMode = ScrambleMode.Custom;
77 options.CustomScrambleChars = customScrambleChars;
78 return builder.WithOptions(options);
79 }
80
81 public static MotionBuilder<TValue, StringOptions, TAdapter> WithRandomSeed<TValue, TAdapter>(
82 this MotionBuilder<TValue, StringOptions, TAdapter> builder, uint seed)
83 where TValue : unmanaged where TAdapter : unmanaged, IMotionAdapter<TValue, StringOptions>
84 {
85 var options = builder.Options;
86 options.RandomSeed = seed;
87 return builder.WithOptions(options);
88 }
89 }
90 }
91
92 namespace MeatMotion.Adapters
93 {
94 internal interface IFixedStringAdapter { }
95 public readonly struct FixedString32BytesMotionAdapter : IMotionAdapter<FixedString32Bytes, StringOptions>, IFixedStringAdapter
96 {
97 public FixedString32Bytes Evaluate(ref FixedString32Bytes startValue, ref FixedString32Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
98 => StringInterpolation.Evaluate(ref startValue, ref endValue, ref options, in context);
99 }
100
101 public readonly struct FixedString64BytesMotionAdapter : IMotionAdapter<FixedString64Bytes, StringOptions>, IFixedStringAdapter
102 {
103 public FixedString64Bytes Evaluate(ref FixedString64Bytes startValue, ref FixedString64Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
104 => StringInterpolation.Evaluate(ref startValue, ref endValue, ref options, in context);
105 }
106
107 public readonly struct FixedString128BytesMotionAdapter : IMotionAdapter<FixedString128Bytes, StringOptions>, IFixedStringAdapter
108 {
109 public FixedString128Bytes Evaluate(ref FixedString128Bytes startValue, ref FixedString128Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
110 => StringInterpolation.Evaluate(ref startValue, ref endValue, ref options, in context);
111 }
112
113 public readonly struct FixedString512BytesMotionAdapter : IMotionAdapter<FixedString512Bytes, StringOptions>, IFixedStringAdapter
114 {
115 public FixedString512Bytes Evaluate(ref FixedString512Bytes startValue, ref FixedString512Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
116 => StringInterpolation.Evaluate(ref startValue, ref endValue, ref options, in context);
117 }
118
119 public readonly struct FixedString4096BytesMotionAdapter : IMotionAdapter<FixedString4096Bytes, StringOptions>, IFixedStringAdapter
120 {
121 public FixedString4096Bytes Evaluate(ref FixedString4096Bytes startValue, ref FixedString4096Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
122 => StringInterpolation.Evaluate(ref startValue, ref endValue, ref options, in context);
123 }
124 }