Repository navigation
Expand file tree
/
Copy pathSampleFactory.cs
More file actions
120 lines (99 loc) · 3.4 KB
/
Copy pathSampleFactory.cs
File metadata and controls
120 lines (99 loc) · 3.4 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
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Box2D.NET.Samples.Primitives;
using Serilog;
using static Box2D.NET.B2Types;
namespace Box2D.NET.Samples.Samples;
public class SampleFactory
{
private static readonly ILogger Logger = Log.ForContext<SampleFactory>();
public static readonly SampleFactory Shared = new SampleFactory();
private readonly List<SampleEntry> _sampleEntries;
public int SampleCount => _sampleEntries.Count;
private SampleFactory()
{
_sampleEntries = new List<SampleEntry>();
}
public int RegisterSample(string category, string name, Func<SampleContext, Sample> fcn)
{
int index = _sampleEntries.Count;
var entry = new SampleEntry(category, name, fcn, null);
_sampleEntries.Add(entry);
return index;
}
public int RegisterSampleWithCapacity(string category, string name, Func<SampleContext, Sample> fcn, Func<B2Capacity> capacityFcn)
{
int index = _sampleEntries.Count;
var entry = new SampleEntry(category, name, fcn, capacityFcn);
_sampleEntries.Add(entry);
return index;
}
public Sample Create(int index, SampleContext context)
{
var entry = GetInternal(index);
context.capacity = entry.CapacityFcn != null ? entry.CapacityFcn.Invoke() : b2DefaultWorldDef().capacity;
var sample = entry.CreateFcn.Invoke(context);
return sample;
}
public string GetCategory(int index)
{
var entry = GetInternal(index);
return entry.Category;
}
public string GetName(int index)
{
var entry = GetInternal(index);
return entry.Name;
}
private SampleEntry GetInternal(int index)
{
var sample = _sampleEntries[index];
return sample;
}
private static int CompareSamples(SampleEntry sa, SampleEntry sb)
{
int result = string.Compare(sa.Category, sb.Category, StringComparison.InvariantCultureIgnoreCase);
if (result == 0)
{
result = string.Compare(sa.Name, sb.Name, StringComparison.InvariantCultureIgnoreCase);
}
return result;
}
public void LoadSamples()
{
Assembly assembly = Assembly.GetExecutingAssembly();
var sampleTypes = assembly.GetTypes()
.Where(t => t.IsSubclassOf(typeof(Sample)) && !t.IsAbstract)
.ToList();
int index = 0;
foreach (var type in sampleTypes)
{
// Attributes = {MethodAttributes} Private | Static | HideBySig | SpecialName | RTSpecialName
// BindingFlags = {BindingFlags} Static | NonPublic
var method = type.GetConstructors(BindingFlags.Static | BindingFlags.NonPublic);
if (0 >= method.Length)
{
Logger.Information($"can not load sample - {type.Name}");
continue;
}
method[0].Invoke(null, null);
var title = GetTitle(index);
Logger.Information($"loaded sample - {title}");
++index;
}
}
public void SortSamples()
{
_sampleEntries.Sort(CompareSamples);
}
public string GetTitle(int index)
{
var entry = GetInternal(index);
return entry.Title;
}
}