-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFancyTextRuntime.cs
More file actions
1123 lines (972 loc) · 38.6 KB
/
Copy pathFancyTextRuntime.cs
File metadata and controls
1123 lines (972 loc) · 38.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Reflection.Emit;
using System.Windows.Forms;
using System.Xml;
using LiveSplit.Model;
using LiveSplit.UI;
namespace LiveSplit.UI.Components
{
internal sealed class FancyTextTargetInfo
{
public string Key { get; set; }
public string ComponentName { get; set; }
public string DisplayName { get; set; }
public string Path { get; set; }
public int LayoutIndex { get; set; }
public IComponent Component { get; set; }
public override string ToString()
{
return DisplayName;
}
}
public sealed class FancyTextResolvedEffects
{
public bool OverrideOutline { get; internal set; }
public Color OutlineColor { get; internal set; }
public float OutlineSize { get; internal set; }
public bool OverrideShadow { get; internal set; }
public bool ShadowEnabled { get; internal set; }
public bool ShadowNormalEnabled { get; internal set; }
public bool ShadowOutsideEnabled { get; internal set; }
public Color ShadowColor { get; internal set; }
public float ShadowSize { get; internal set; }
public int ShadowSizePercent { get; internal set; }
public float ShadowBlur { get; internal set; }
public int ShadowMultiply { get; internal set; }
public bool ShadowClipToRow { get; internal set; }
public bool HasGradient { get; internal set; }
public bool UseExistingColorMiddle { get; internal set; }
public Color GradientColor1 { get; internal set; }
public Color GradientColor2 { get; internal set; }
public Color GradientColor3 { get; internal set; }
public FancyTextGradientDirection GradientDirection { get; internal set; }
}
internal sealed class FancyTextActiveInstance
{
public FancyTextScopeMode ScopeMode { get; set; }
public HashSet<string> TargetKeys { get; private set; }
public HashSet<string> LegacyTargetNames { get; private set; }
public HashSet<IComponent> TargetComponents { get; private set; }
public FancyTextResolvedEffects Effects { get; set; }
public FancyTextActiveInstance()
{
TargetKeys = new HashSet<string>(StringComparer.Ordinal);
LegacyTargetNames = new HashSet<string>(StringComparer.Ordinal);
TargetComponents = new HashSet<IComponent>(ReferenceComponentComparer.Instance);
}
public bool AppliesTo(FancyTextTargetInfo target)
{
if (target == null)
{
return false;
}
if (ScopeMode == FancyTextScopeMode.AllComponents)
{
return true;
}
if (ScopeMode != FancyTextScopeMode.SelectedComponents)
{
return false;
}
return TargetComponents.Contains(target.Component)
|| TargetKeys.Contains(target.Key)
|| LegacyTargetNames.Contains(target.ComponentName);
}
public bool AppliesToComponent(IComponent component)
{
if (component == null)
{
return false;
}
if (ScopeMode == FancyTextScopeMode.AllComponents)
{
return true;
}
if (ScopeMode != FancyTextScopeMode.SelectedComponents)
{
return false;
}
return TargetComponents.Contains(component);
}
}
internal sealed class ReferenceComponentComparer : IEqualityComparer<IComponent>
{
public static readonly ReferenceComponentComparer Instance = new ReferenceComponentComparer();
public bool Equals(IComponent x, IComponent y)
{
return ReferenceEquals(x, y);
}
public int GetHashCode(IComponent obj)
{
return obj == null ? 0 : System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
}
}
public static class FancyTextRuntime
{
private static readonly object Sync = new object();
private static readonly Dictionary<FancyTextComponent, FancyTextActiveInstance> ActiveInstances =
new Dictionary<FancyTextComponent, FancyTextActiveInstance>();
[ThreadStatic]
private static Stack<FancyTextDrawContext> DrawStack;
private static readonly HashSet<FancyTextComponent> PendingReorders = new HashSet<FancyTextComponent>();
internal static Stack<FancyTextDrawContext> DrawStackForPop { get { return DrawStack; } }
public static void Publish(FancyTextComponent owner, LiveSplitState state, FancyTextSettings settings)
{
if (owner == null || settings == null)
{
return;
}
lock (Sync)
{
PruneInactiveInstances(state);
if (settings.ScopeMode == FancyTextScopeMode.ThisComponentOnly
|| (!settings.OverrideTextColors && !settings.OverrideOutline && !settings.OverrideShadow))
{
ActiveInstances.Remove(owner);
return;
}
ActiveInstances[owner] = CreateActiveInstance(state, settings);
}
}
public static void Unpublish(FancyTextComponent owner)
{
if (owner == null)
{
return;
}
lock (Sync)
{
ActiveInstances.Remove(owner);
PendingReorders.Remove(owner);
}
}
public static FancyTextResolvedEffects GetEffectsForComponent(LiveSplitState state, IComponent component)
{
component = Unwrap(component);
if (component == null)
{
return null;
}
lock (Sync)
{
PruneInactiveInstances(state);
FancyTextResolvedEffects merged = null;
foreach (FancyTextActiveInstance instance in ActiveInstances.Values)
{
if (!instance.AppliesToComponent(component))
{
continue;
}
if (merged == null)
{
merged = new FancyTextResolvedEffects();
}
MergeInto(merged, instance.Effects);
}
return merged;
}
}
public static FancyTextResolvedEffects GetCurrentEffects()
{
if (DrawStack == null || DrawStack.Count == 0)
{
return GetGlobalEffectsFallback();
}
FancyTextDrawContext context = DrawStack.Peek();
return GetEffectsForComponent(context.State, context.Component);
}
internal static FancyTextResolvedEffects GetCurrentComponentEffects()
{
if (DrawStack == null || DrawStack.Count == 0)
{
return null;
}
FancyTextDrawContext context = DrawStack.Peek();
return GetEffectsForComponent(context.State, context.Component);
}
internal static LiveSplitState GetCurrentDrawState()
{
if (DrawStack == null || DrawStack.Count == 0)
{
return null;
}
return DrawStack.Peek().State;
}
public static IDisposable BeginComponentDraw(LiveSplitState state, IComponent component)
{
if (DrawStack == null)
{
DrawStack = new Stack<FancyTextDrawContext>();
}
DrawStack.Push(new FancyTextDrawContext(state, Unwrap(component)));
return new DrawContextPopper();
}
internal static void InstallHooks(LiveSplitState state)
{
FancyTextSimpleLabelHook.Install();
FancyTextExternalTextHook.Install();
PruneInactiveInstances(state);
ILayout layout = state != null ? state.Layout : null;
if (layout == null || layout.LayoutComponents == null)
{
return;
}
foreach (ILayoutComponent layoutComponent in layout.LayoutComponents)
{
if (layoutComponent == null || layoutComponent.Component == null)
{
continue;
}
if (layoutComponent.Component is FancyTextComponent
|| layoutComponent.Component is FancyTextComponentProxy)
{
continue;
}
layoutComponent.Component = FancyTextComponentProxy.Create(layoutComponent.Component, state);
}
}
internal static FancyTextComponent RecreateController(LiveSplitState state, FancyTextComponent owner, FancyTextSettings settings)
{
if (state == null || owner == null || settings == null
|| state.Layout == null || state.Layout.LayoutComponents == null)
{
return owner;
}
IList<ILayoutComponent> components = state.Layout.LayoutComponents;
int ownerIndex = IndexOfComponent(components, owner);
if (ownerIndex < 0)
{
InstallHooks(state);
Publish(owner, state, settings);
Invalidate(state);
return owner;
}
ILayoutComponent layoutComponent = components[ownerIndex];
var refreshedOwner = new FancyTextComponent(state, settings);
layoutComponent.Component = refreshedOwner;
components.RemoveAt(ownerIndex);
components.Add(layoutComponent);
Unpublish(owner);
Publish(refreshedOwner, state, settings);
InstallHooks(state);
state.Layout.HasChanged = true;
Invalidate(state);
return refreshedOwner;
}
internal static void ScheduleControllerFirst(LiveSplitState state, FancyTextComponent owner)
{
if (state == null || owner == null || state.Layout == null || state.Layout.LayoutComponents == null)
{
return;
}
IList<ILayoutComponent> components = state.Layout.LayoutComponents;
int ownerIndex = IndexOfComponent(components, owner);
if (ownerIndex <= 0)
{
return;
}
lock (Sync)
{
if (!PendingReorders.Add(owner))
{
return;
}
}
Form form = state.Form;
if (form == null || form.IsDisposed)
{
try
{
ReorderControllerFirst(state, owner);
}
finally
{
lock (Sync)
{
PendingReorders.Remove(owner);
}
}
return;
}
try
{
form.BeginInvoke((Action)(() =>
{
try
{
ReorderControllerFirst(state, owner);
form.Invalidate();
}
finally
{
lock (Sync)
{
PendingReorders.Remove(owner);
}
}
}));
}
catch
{
lock (Sync)
{
PendingReorders.Remove(owner);
}
}
}
internal static IList<FancyTextTargetInfo> GetTargets(LiveSplitState state, IComponent owner)
{
var targets = new List<FancyTextTargetInfo>();
ILayout layout = state != null ? state.Layout : null;
if (layout == null || layout.LayoutComponents == null)
{
return targets;
}
var occurrenceByBaseKey = new Dictionary<string, int>(StringComparer.Ordinal);
for (int index = 0; index < layout.LayoutComponents.Count; index++)
{
ILayoutComponent layoutComponent = layout.LayoutComponents[index];
IComponent component = layoutComponent != null ? Unwrap(layoutComponent.Component) : null;
if (component == null || ReferenceEquals(component, owner) || component is FancyTextComponent)
{
continue;
}
string path = layoutComponent.Path ?? string.Empty;
string componentName = SafeComponentName(component);
string baseKey = !string.IsNullOrEmpty(path)
? path
: component.GetType().FullName;
if (string.IsNullOrEmpty(baseKey))
{
baseKey = componentName;
}
int occurrence;
occurrenceByBaseKey.TryGetValue(baseKey, out occurrence);
occurrenceByBaseKey[baseKey] = occurrence + 1;
string key = baseKey + "#" + occurrence.ToString(System.Globalization.CultureInfo.InvariantCulture);
targets.Add(new FancyTextTargetInfo
{
Key = key,
ComponentName = componentName,
DisplayName = (index + 1).ToString(System.Globalization.CultureInfo.InvariantCulture) + ". " + componentName,
Path = path,
LayoutIndex = index,
Component = component
});
}
return targets;
}
private static FancyTextTargetInfo FindTarget(LiveSplitState state, IComponent component)
{
component = Unwrap(component);
if (component == null)
{
return null;
}
foreach (FancyTextTargetInfo target in GetTargets(state, null))
{
if (ReferenceEquals(target.Component, component))
{
return target;
}
}
return null;
}
private static FancyTextActiveInstance CreateActiveInstance(LiveSplitState state, FancyTextSettings settings)
{
var active = new FancyTextActiveInstance
{
ScopeMode = settings.ScopeMode,
Effects = new FancyTextResolvedEffects
{
OverrideOutline = settings.OverrideOutline,
OutlineColor = settings.OutlineColor,
OutlineSize = settings.OutlineSize,
OverrideShadow = settings.OverrideShadow,
ShadowEnabled = settings.ShadowEnabled,
ShadowNormalEnabled = settings.ShadowNormalEnabled,
ShadowOutsideEnabled = settings.ShadowOutsideEnabled,
ShadowColor = settings.ShadowColor,
ShadowSize = settings.ShadowSize,
ShadowSizePercent = settings.ShadowSizePercent,
ShadowBlur = settings.ShadowBlur,
ShadowMultiply = settings.ShadowMultiply,
ShadowClipToRow = settings.ShadowClipToRow,
HasGradient = settings.OverrideTextColors,
UseExistingColorMiddle = settings.GradientMode == FancyTextGradientMode.ExistingColors,
GradientColor1 = settings.TextColor1,
GradientColor2 = settings.TextColor2,
GradientColor3 = settings.TextColor3,
GradientDirection = settings.GradientDirection
}
};
foreach (string target in settings.TargetComponents)
{
if (string.IsNullOrWhiteSpace(target))
{
continue;
}
if (target.IndexOf("#", StringComparison.Ordinal) >= 0
|| target.IndexOf("\\", StringComparison.Ordinal) >= 0
|| target.IndexOf("/", StringComparison.Ordinal) >= 0)
{
active.TargetKeys.Add(target);
}
else
{
active.LegacyTargetNames.Add(target);
}
}
if (active.ScopeMode == FancyTextScopeMode.SelectedComponents)
{
foreach (FancyTextTargetInfo target in GetTargets(state, null))
{
if (target == null || target.Component == null)
{
continue;
}
if (active.TargetKeys.Contains(target.Key)
|| active.LegacyTargetNames.Contains(target.ComponentName))
{
active.TargetComponents.Add(target.Component);
}
}
}
return active;
}
private static void MergeInto(FancyTextResolvedEffects merged, FancyTextResolvedEffects next)
{
if (next == null)
{
return;
}
if (next.OverrideOutline)
{
merged.OverrideOutline = true;
merged.OutlineColor = next.OutlineColor;
merged.OutlineSize = next.OutlineSize;
}
if (next.OverrideShadow)
{
merged.OverrideShadow = true;
merged.ShadowEnabled = next.ShadowEnabled;
merged.ShadowNormalEnabled = next.ShadowNormalEnabled;
merged.ShadowOutsideEnabled = next.ShadowOutsideEnabled;
merged.ShadowColor = next.ShadowColor;
merged.ShadowSize = next.ShadowSize;
merged.ShadowSizePercent = next.ShadowSizePercent;
merged.ShadowBlur = next.ShadowBlur;
merged.ShadowMultiply = next.ShadowMultiply;
merged.ShadowClipToRow = next.ShadowClipToRow;
}
if (next.HasGradient)
{
merged.HasGradient = true;
merged.UseExistingColorMiddle = next.UseExistingColorMiddle;
merged.GradientColor1 = next.GradientColor1;
merged.GradientColor2 = next.GradientColor2;
merged.GradientColor3 = next.GradientColor3;
merged.GradientDirection = next.GradientDirection;
}
}
private static FancyTextResolvedEffects GetGlobalEffectsFallback()
{
lock (Sync)
{
PruneInactiveInstances(null);
FancyTextResolvedEffects merged = null;
foreach (FancyTextActiveInstance instance in ActiveInstances.Values)
{
if (instance.ScopeMode != FancyTextScopeMode.AllComponents)
{
continue;
}
if (merged == null)
{
merged = new FancyTextResolvedEffects();
}
MergeInto(merged, instance.Effects);
}
return merged;
}
}
private static void PruneInactiveInstances(LiveSplitState state)
{
if (state == null || state.Layout == null || state.Layout.LayoutComponents == null)
{
return;
}
lock (Sync)
{
if (ActiveInstances.Count == 0)
{
return;
}
var liveOwners = new HashSet<FancyTextComponent>();
foreach (ILayoutComponent layoutComponent in state.Layout.LayoutComponents)
{
IComponent component = layoutComponent != null ? Unwrap(layoutComponent.Component) : null;
var fancyText = component as FancyTextComponent;
if (fancyText != null)
{
liveOwners.Add(fancyText);
}
}
var staleOwners = new List<FancyTextComponent>();
foreach (FancyTextComponent owner in ActiveInstances.Keys)
{
if (!liveOwners.Contains(owner))
{
staleOwners.Add(owner);
}
}
foreach (FancyTextComponent owner in staleOwners)
{
ActiveInstances.Remove(owner);
PendingReorders.Remove(owner);
}
}
}
private static string SafeComponentName(IComponent component)
{
try
{
string name = component.ComponentName;
return string.IsNullOrWhiteSpace(name) ? component.GetType().Name : name;
}
catch
{
return component.GetType().Name;
}
}
internal static IComponent Unwrap(IComponent component)
{
int depth = 0;
while (component != null && depth < 16)
{
depth++;
var fancyTextProxy = component as FancyTextComponentProxy;
if (fancyTextProxy != null)
{
component = fancyTextProxy.Inner;
continue;
}
IComponent knownProxyInner;
if (TryGetKnownProxyInner(component, out knownProxyInner))
{
component = knownProxyInner;
continue;
}
break;
}
return component;
}
private static bool TryGetKnownProxyInner(IComponent component, out IComponent inner)
{
inner = null;
if (!IsTypeName(component, "LiveSplit.UI.Components.FancyBackgroundComponentProxy"))
{
return false;
}
PropertyInfo property = component.GetType().GetProperty("Inner",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
try
{
inner = property != null ? property.GetValue(component, null) as IComponent : null;
}
catch
{
inner = null;
}
return inner != null && !ReferenceEquals(inner, component);
}
private static bool IsTypeName(IComponent component, string fullName)
{
return component != null
&& string.Equals(component.GetType().FullName, fullName, StringComparison.Ordinal);
}
private static void ReorderControllerFirst(LiveSplitState state, FancyTextComponent owner)
{
if (state == null || state.Layout == null || state.Layout.LayoutComponents == null)
{
return;
}
IList<ILayoutComponent> components = state.Layout.LayoutComponents;
int ownerIndex = IndexOfComponent(components, owner);
if (ownerIndex <= 0)
{
return;
}
ILayoutComponent item = components[ownerIndex];
components.RemoveAt(ownerIndex);
components.Insert(0, item);
state.Layout.HasChanged = true;
}
private static int IndexOfComponent(IList<ILayoutComponent> components, IComponent component)
{
for (int i = 0; i < components.Count; i++)
{
IComponent current = components[i] != null ? Unwrap(components[i].Component) : null;
if (ReferenceEquals(current, component))
{
return i;
}
}
return -1;
}
private static void Invalidate(LiveSplitState state)
{
Form form = state != null ? state.Form : null;
if (form == null || form.IsDisposed)
{
return;
}
try
{
form.Invalidate();
}
catch
{
}
}
}
internal sealed class FancyTextDrawContext
{
public LiveSplitState State { get; private set; }
public IComponent Component { get; private set; }
public FancyTextDrawContext(LiveSplitState state, IComponent component)
{
State = state;
Component = component;
}
}
internal sealed class DrawContextPopper : IDisposable
{
public void Dispose()
{
if (FancyTextRuntime.DrawStackForPop != null && FancyTextRuntime.DrawStackForPop.Count > 0)
{
FancyTextRuntime.DrawStackForPop.Pop();
}
}
}
public class FancyTextComponentProxy : IDeactivatableComponent
{
private const string TextComponentTypeName = "LiveSplit.UI.Components.TextComponent";
private const string GlobalFontAttributeTypeName = "LiveSplit.UI.Components.GlobalFontConsumerAttribute";
private const string GlobalFontTypeName = "LiveSplit.UI.Components.GlobalFont";
private static readonly object ProxyTypeSync = new object();
private static readonly Dictionary<int, Type> ProxyTypes = new Dictionary<int, Type>();
private static ModuleBuilder _proxyModule;
private readonly LiveSplitState _state;
public IComponent Inner { get; private set; }
public FancyTextComponentProxy(IComponent inner)
: this(inner, null)
{
}
public FancyTextComponentProxy(IComponent inner, LiveSplitState state)
{
if (inner == null)
throw new ArgumentNullException("inner");
Inner = inner;
_state = state;
RepairLegacyTextFonts();
}
internal static FancyTextComponentProxy Create(IComponent inner, LiveSplitState state)
{
Type proxyType = GetProxyType(inner);
if (proxyType != null && proxyType != typeof(FancyTextComponentProxy))
{
try
{
return (FancyTextComponentProxy)Activator.CreateInstance(proxyType, inner, state);
}
catch
{
// Fall back to the compatible base proxy on runtimes that disallow emitted types.
}
}
return new FancyTextComponentProxy(inner, state);
}
private static Type GetProxyType(IComponent inner)
{
int usedGlobalFonts = GetUsedGlobalFonts(inner);
if (usedGlobalFonts == 0)
return typeof(FancyTextComponentProxy);
lock (ProxyTypeSync)
{
Type proxyType;
if (ProxyTypes.TryGetValue(usedGlobalFonts, out proxyType))
return proxyType;
proxyType = BuildProxyType(usedGlobalFonts) ?? typeof(FancyTextComponentProxy);
ProxyTypes[usedGlobalFonts] = proxyType;
return proxyType;
}
}
private static int GetUsedGlobalFonts(IComponent inner)
{
if (inner == null)
return 0;
Type coreAssemblyType = typeof(IComponent);
Type attributeType = coreAssemblyType.Assembly.GetType(GlobalFontAttributeTypeName, false);
Type globalFontType = coreAssemblyType.Assembly.GetType(GlobalFontTypeName, false);
if (attributeType == null || globalFontType == null)
return 0;
IComponent unwrapped = FancyTextRuntime.Unwrap(inner);
Type componentType = unwrapped != null ? unwrapped.GetType() : inner.GetType();
try
{
object[] attributes = componentType.GetCustomAttributes(attributeType, true);
if (attributes.Length > 0)
{
PropertyInfo property = attributeType.GetProperty("UsedGlobalFonts",
BindingFlags.Instance | BindingFlags.Public);
object value = property != null ? property.GetValue(attributes[0], null) : null;
if (value != null)
return Convert.ToInt32(value);
}
}
catch
{
// A legacy component can reference metadata unavailable to its current host.
}
// Legacy Text owns its two font controls. Opting it into the host's
// global-font UI changes its settings contract and can hide those controls.
return 0;
}
private static Type BuildProxyType(int usedGlobalFonts)
{
try
{
Assembly coreAssembly = typeof(IComponent).Assembly;
Type attributeType = coreAssembly.GetType(GlobalFontAttributeTypeName, false);
Type globalFontType = coreAssembly.GetType(GlobalFontTypeName, false);
ConstructorInfo attributeConstructor = attributeType != null && globalFontType != null
? attributeType.GetConstructor(new[] { globalFontType })
: null;
ConstructorInfo baseConstructor = typeof(FancyTextComponentProxy).GetConstructor(
new[] { typeof(IComponent), typeof(LiveSplitState) });
if (attributeConstructor == null || baseConstructor == null)
return null;
if (_proxyModule == null)
{
var assemblyName = new AssemblyName("LiveSplit.FancyText.DynamicProxies");
AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.Run);
_proxyModule = assembly.DefineDynamicModule(assemblyName.Name);
}
TypeBuilder type = _proxyModule.DefineType(
"LiveSplit.UI.Components.FancyTextComponentProxy_Fonts_" + usedGlobalFonts,
TypeAttributes.Public | TypeAttributes.Sealed,
typeof(FancyTextComponentProxy));
ConstructorBuilder constructor = type.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
new[] { typeof(IComponent), typeof(LiveSplitState) });
ILGenerator il = constructor.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Call, baseConstructor);
il.Emit(OpCodes.Ret);
object globalFont = Enum.ToObject(globalFontType, usedGlobalFonts);
type.SetCustomAttribute(new CustomAttributeBuilder(
attributeConstructor, new[] { globalFont }));
return type.CreateType();
}
catch
{
return null;
}
}
private void RepairLegacyTextFonts()
{
IComponent textComponent = FancyTextRuntime.Unwrap(Inner);
if (textComponent == null
|| !string.Equals(textComponent.GetType().FullName, TextComponentTypeName, StringComparison.Ordinal))
{
return;
}
object settings = null;
try
{
PropertyInfo settingsProperty = textComponent.GetType().GetProperty("Settings",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
settings = settingsProperty != null ? settingsProperty.GetValue(textComponent, null) : null;
}
catch
{
return;
}
if (settings == null)
return;
Font fallback = GetFallbackTextFont();
RepairFontProperty(settings, "Font1", fallback);
RepairFontProperty(settings, "Font2", fallback);
}
private Font GetFallbackTextFont()
{
try
{
object layoutSettings = _state != null ? _state.LayoutSettings : null;
PropertyInfo textFontProperty = layoutSettings != null
? layoutSettings.GetType().GetProperty("TextFont", BindingFlags.Instance | BindingFlags.Public)
: null;
Font textFont = textFontProperty != null
? textFontProperty.GetValue(layoutSettings, null) as Font
: null;
if (textFont != null)
return textFont;
}
catch
{
// SystemFonts.DefaultFont is a safe last resort for malformed legacy settings.
}
return SystemFonts.DefaultFont;
}
private static void RepairFontProperty(object settings, string propertyName, Font fallback)
{
try
{
PropertyInfo property = settings.GetType().GetProperty(propertyName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (property == null || !property.CanRead || !property.CanWrite
|| property.PropertyType != typeof(Font)
|| property.GetValue(settings, null) != null)
{
return;
}
property.SetValue(settings, fallback != null ? fallback.Clone() : null, null);
}
catch
{
// Non-standard Text implementations remain untouched.
}
}
public string ComponentName { get { return Inner.ComponentName; } }
public float HorizontalWidth { get { return Inner.HorizontalWidth; } }
public float MinimumHeight { get { return Inner.MinimumHeight; } }
public float VerticalHeight { get { return Inner.VerticalHeight; } }
public float MinimumWidth { get { return Inner.MinimumWidth; } }
public float PaddingTop { get { return Inner.PaddingTop; } }
public float PaddingBottom { get { return Inner.PaddingBottom; } }
public float PaddingLeft { get { return Inner.PaddingLeft; } }
public float PaddingRight { get { return Inner.PaddingRight; } }
public IDictionary<string, Action> ContextMenuControls { get { return Inner.ContextMenuControls; } }
public bool Activated
{
get
{
var deactivatable = Inner as IDeactivatableComponent;