Skip to content

Commit a194af7

Browse files
committed
Refactor: Improve API usability and enforce code style
This commit introduces two main improvements: 1. **API Refactoring (`ref` to `in`)**: Updated creation APIs (e.g., `b2CreateBody`, `b2CreateShape`, `b2CreateJoint`) to use `in` parameters instead of `ref` for definition structs. This enhances API usability by allowing for more flexible argument passing (e.g., temporary values) and clearly marks the parameters as read-only, preventing unintended modifications. 2. **Enforce Warning CS9191 as Error**: Modified all `.csproj` files to treat warning CS9191 (unnecessary `ref` modifier) as a compilation error. This enforces a cleaner code style by removing redundant `ref` keywords from method calls, which became obsolete after the `ref`-to-`in` refactoring. Combined, these changes lead to a more robust and modern API surface and a stricter, more consistent codebase.
1 parent e2def40 commit a194af7

130 files changed

Lines changed: 602 additions & 599 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Box2D.NET.Samples/Box2D.NET.Samples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<IsPackable>false</IsPackable>
77
<ServerGarbageCollection>true</ServerGarbageCollection>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
<WarningsAsErrors>$(WarningsAsErrors);CS9191</WarningsAsErrors>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkBarrel.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public BenchmarkBarrel(SampleContext context) : base(context)
7070
for (int i = 0; i < 81; ++i)
7171
{
7272
B2Polygon box = b2MakeOffsetBox(0.5f * gridSize, 0.5f * gridSize, new B2Vec2(x, y), b2Rot_identity);
73-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
73+
b2CreatePolygonShape(groundId, shapeDef, box);
7474
x += gridSize;
7575
}
7676

@@ -79,7 +79,7 @@ public BenchmarkBarrel(SampleContext context) : base(context)
7979
for (int i = 0; i < 100; ++i)
8080
{
8181
B2Polygon box = b2MakeOffsetBox(0.5f * gridSize, 0.5f * gridSize, new B2Vec2(x, y), b2Rot_identity);
82-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
82+
b2CreatePolygonShape(groundId, shapeDef, box);
8383
y += gridSize;
8484
}
8585

@@ -88,12 +88,12 @@ public BenchmarkBarrel(SampleContext context) : base(context)
8888
for (int i = 0; i < 100; ++i)
8989
{
9090
B2Polygon box = b2MakeOffsetBox(0.5f * gridSize, 0.5f * gridSize, new B2Vec2(x, y), b2Rot_identity);
91-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
91+
b2CreatePolygonShape(groundId, shapeDef, box);
9292
y += gridSize;
9393
}
9494

9595
B2Segment segment = new B2Segment(new B2Vec2(-800.0f, -80.0f), new B2Vec2(800.0f, -80.0f));
96-
b2CreateSegmentShape(groundId, ref shapeDef, ref segment);
96+
b2CreateSegmentShape(groundId, shapeDef, segment);
9797
}
9898

9999
for (int i = 0; i < e_maxRows * e_maxColumns; ++i)
@@ -179,20 +179,20 @@ void CreateScene()
179179

180180
B2Vec2[] points = new B2Vec2[3] { new B2Vec2(-0.1f, -0.5f), new B2Vec2(0.1f, -0.5f), new B2Vec2(0.0f, 0.5f) };
181181
B2Hull wedgeHull = b2ComputeHull(points, 3);
182-
B2Polygon wedge = b2MakePolygon(ref wedgeHull, 0.0f);
182+
B2Polygon wedge = b2MakePolygon(wedgeHull, 0.0f);
183183

184184
B2Vec2[] vertices = new B2Vec2[3];
185185
vertices[0] = new B2Vec2(-1.0f, 0.0f);
186186
vertices[1] = new B2Vec2(0.5f, 1.0f);
187187
vertices[2] = new B2Vec2(0.0f, 2.0f);
188188
B2Hull hull = b2ComputeHull(vertices, 3);
189-
B2Polygon left = b2MakePolygon(ref hull, 0.0f);
189+
B2Polygon left = b2MakePolygon(hull, 0.0f);
190190

191191
vertices[0] = new B2Vec2(1.0f, 0.0f);
192192
vertices[1] = new B2Vec2(-0.5f, 1.0f);
193193
vertices[2] = new B2Vec2(0.0f, 2.0f);
194194
hull = b2ComputeHull(vertices, 3);
195-
B2Polygon right = b2MakePolygon(ref hull, 0.0f);
195+
B2Polygon right = b2MakePolygon(hull, 0.0f);
196196

197197
// b2Polygon top = b2MakeOffsetBox(0.8f, 0.2f, {0.0f, 0.8f}, 0.0f);
198198
// b2Polygon leftLeg = b2MakeOffsetBox(0.2f, 0.5f, {-0.6f, 0.5f}, 0.0f);
@@ -245,7 +245,7 @@ void CreateScene()
245245
capsule.center1 = new B2Vec2(0.0f, -0.5f * length);
246246
capsule.center2 = new B2Vec2(0.0f, 0.5f * length);
247247
shapeDef.material.rollingResistance = 0.2f;
248-
b2CreateCapsuleShape(m_bodies[index], ref shapeDef, ref capsule);
248+
b2CreateCapsuleShape(m_bodies[index], shapeDef, capsule);
249249
}
250250
else if (m_shapeType == ShapeType.e_mixShape)
251251
{
@@ -263,7 +263,7 @@ void CreateScene()
263263
float length = RandomFloatRange(0.25f, 1.0f);
264264
capsule.center1 = new B2Vec2(0.0f, -0.5f * length);
265265
capsule.center2 = new B2Vec2(0.0f, 0.5f * length);
266-
b2CreateCapsuleShape(m_bodies[index], ref shapeDef, ref capsule);
266+
b2CreateCapsuleShape(m_bodies[index], shapeDef, capsule);
267267
}
268268
else if (mod == 2)
269269
{
@@ -274,20 +274,20 @@ void CreateScene()
274274
// Don't put a function call into a macro.
275275
float value = RandomFloatRange(-1.0f, 1.0f);
276276
box.radius = 0.25f * b2MaxFloat(0.0f, value);
277-
b2CreatePolygonShape(m_bodies[index], ref shapeDef, ref box);
277+
b2CreatePolygonShape(m_bodies[index], shapeDef, box);
278278
}
279279
else
280280
{
281281
wedge.radius = RandomFloatRange(0.1f, 0.25f);
282-
b2CreatePolygonShape(m_bodies[index], ref shapeDef, ref wedge);
282+
b2CreatePolygonShape(m_bodies[index], shapeDef, wedge);
283283
}
284284
}
285285
else if (m_shapeType == ShapeType.e_compoundShape)
286286
{
287287
m_bodies[index] = b2CreateBody(m_worldId, bodyDef);
288288

289-
b2CreatePolygonShape(m_bodies[index], ref shapeDef, ref left);
290-
b2CreatePolygonShape(m_bodies[index], ref shapeDef, ref right);
289+
b2CreatePolygonShape(m_bodies[index], shapeDef, left);
290+
b2CreatePolygonShape(m_bodies[index], shapeDef, right);
291291
// b2CreatePolygonShape(m_bodies[index], &shapeDef, &top);
292292
// b2CreatePolygonShape(m_bodies[index], &shapeDef, &leftLeg);
293293
// b2CreatePolygonShape(m_bodies[index], &shapeDef, &rightLeg);

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkBarrel24.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ public BenchmarkBarrel24(SampleContext context) : base(context)
3636

3737
B2Polygon box = b2MakeBox(groundSize, 1.2f);
3838
B2ShapeDef shapeDef = b2DefaultShapeDef();
39-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
39+
b2CreatePolygonShape(groundId, shapeDef, box);
4040

4141
bodyDef.rotation = b2MakeRot(0.5f * B2_PI);
4242
bodyDef.position = new B2Vec2(groundSize, 2.0f * groundSize);
4343
groundId = b2CreateBody(m_worldId, bodyDef);
4444

4545
box = b2MakeBox(2.0f * groundSize, 1.2f);
46-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
46+
b2CreatePolygonShape(groundId, shapeDef, box);
4747

4848
bodyDef.position = new B2Vec2(-groundSize, 2.0f * groundSize);
4949
groundId = b2CreateBody(m_worldId, bodyDef);
50-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
50+
b2CreatePolygonShape(groundId, shapeDef, box);
5151
}
5252

5353
int num = 26;
@@ -87,7 +87,7 @@ public BenchmarkBarrel24(SampleContext context) : base(context)
8787
bodyDef.position = new B2Vec2(x, y);
8888

8989
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
90-
b2CreatePolygonShape(bodyId, ref shapeDef, ref cuboid);
90+
b2CreatePolygonShape(bodyId, shapeDef, cuboid);
9191
}
9292
}
9393
}

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCapacity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public BenchmarkCapacity(SampleContext context) : base(context)
4343

4444
B2Polygon box = b2MakeBox(800.0f, 5.0f);
4545
B2ShapeDef shapeDef = b2DefaultShapeDef();
46-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
46+
b2CreatePolygonShape(groundId, shapeDef, box);
4747
}
4848

4949
m_square = b2MakeSquare(0.5f);
@@ -96,7 +96,7 @@ public override void Step()
9696
bodyDef.position.Y += 0.5f;
9797

9898
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
99-
b2CreatePolygonShape(bodyId, ref shapeDef, ref m_square);
99+
b2CreatePolygonShape(bodyId, shapeDef, m_square);
100100

101101
x += 2.0f;
102102
}

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCast.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void BuildScene()
145145
shapeDef.material.customColor = (uint)B2HexColor.b2_colorBox2DGreen;
146146
}
147147

148-
b2CreatePolygonShape(bodyId, ref shapeDef, ref box);
148+
b2CreatePolygonShape(bodyId, shapeDef, box);
149149
}
150150

151151
x += m_grid;

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCompound.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public BenchmarkCompound(SampleContext context) : base(context)
4747
{
4848
float x = grid * j;
4949
B2Polygon square = b2MakeOffsetBox(0.5f * grid, 0.5f * grid, new B2Vec2(x, y), b2Rot_identity);
50-
b2CreatePolygonShape(groundId, ref shapeDef, ref square);
50+
b2CreatePolygonShape(groundId, shapeDef, square);
5151
}
5252
}
5353

@@ -58,7 +58,7 @@ public BenchmarkCompound(SampleContext context) : base(context)
5858
{
5959
float x = -grid * j;
6060
B2Polygon square = b2MakeOffsetBox(0.5f * grid, 0.5f * grid, new B2Vec2(x, y), b2Rot_identity);
61-
b2CreatePolygonShape(groundId, ref shapeDef, ref square);
61+
b2CreatePolygonShape(groundId, shapeDef, square);
6262
}
6363
}
6464
}
@@ -95,7 +95,7 @@ public BenchmarkCompound(SampleContext context) : base(context)
9595
{
9696
float x = j * grid;
9797
B2Polygon square = b2MakeOffsetBox(0.5f * grid, 0.5f * grid, new B2Vec2(x, y), b2Rot_identity);
98-
b2CreatePolygonShape(bodyId, ref shapeDef, ref square);
98+
b2CreatePolygonShape(bodyId, shapeDef, square);
9999
}
100100
}
101101

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCreateDestroy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public BenchmarkCreateDestroy(SampleContext context) : base(context)
4848

4949
B2Polygon box = b2MakeBox(groundSize, 1.0f);
5050
B2ShapeDef shapeDef = b2DefaultShapeDef();
51-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
51+
b2CreatePolygonShape(groundId, shapeDef, box);
5252

5353
for (int i = 0; i < e_maxBodyCount; ++i)
5454
{
@@ -107,7 +107,7 @@ void CreateScene()
107107

108108
B2_ASSERT(index < e_maxBodyCount);
109109
m_bodies[index] = b2CreateBody(m_worldId, bodyDef);
110-
b2CreatePolygonShape(m_bodies[index], ref shapeDef, ref box);
110+
b2CreatePolygonShape(m_bodies[index], shapeDef, box);
111111

112112
index += 1;
113113
}

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkKinematic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public BenchmarkKinematic(SampleContext context) : base(context)
5555
{
5656
float x = j * grid;
5757
B2Polygon square = b2MakeOffsetBox(0.5f * grid, 0.5f * grid, new B2Vec2(x, y), b2Rot_identity);
58-
b2CreatePolygonShape(bodyId, ref shapeDef, ref square);
58+
b2CreatePolygonShape(bodyId, shapeDef, square);
5959
}
6060
}
6161

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkManyTumblers.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ void CreateTumbler(B2Vec2 position, int index)
8181

8282
B2Polygon polygon;
8383
polygon = b2MakeOffsetBox(0.25f, 2.0f, new B2Vec2(2.0f, 0.0f), b2Rot_identity);
84-
b2CreatePolygonShape(bodyId, ref shapeDef, ref polygon);
84+
b2CreatePolygonShape(bodyId, shapeDef, polygon);
8585
polygon = b2MakeOffsetBox(0.25f, 2.0f, new B2Vec2(-2.0f, 0.0f), b2Rot_identity);
86-
b2CreatePolygonShape(bodyId, ref shapeDef, ref polygon);
86+
b2CreatePolygonShape(bodyId, shapeDef, polygon);
8787
polygon = b2MakeOffsetBox(2.0f, 0.25f, new B2Vec2(0.0f, 2.0f), b2Rot_identity);
88-
b2CreatePolygonShape(bodyId, ref shapeDef, ref polygon);
88+
b2CreatePolygonShape(bodyId, shapeDef, polygon);
8989
polygon = b2MakeOffsetBox(2.0f, 0.25f, new B2Vec2(0.0f, -2.0f), b2Rot_identity);
90-
b2CreatePolygonShape(bodyId, ref shapeDef, ref polygon);
90+
b2CreatePolygonShape(bodyId, shapeDef, polygon);
9191
}
9292

9393
void CreateScene()
@@ -189,7 +189,7 @@ public override void Step()
189189
bodyDef.type = B2BodyType.b2_dynamicBody;
190190
bodyDef.position = m_positions[i];
191191
m_bodyIds[m_bodyIndex] = b2CreateBody(m_worldId, bodyDef);
192-
b2CreateCapsuleShape(m_bodyIds[m_bodyIndex], ref shapeDef, ref capsule);
192+
b2CreateCapsuleShape(m_bodyIds[m_bodyIndex], shapeDef, capsule);
193193

194194
m_bodyIndex += 1;
195195
}

src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkSensor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public BenchmarkSensor(SampleContext context) : base(context)
6767
for (int i = 0; i < 81; ++i)
6868
{
6969
B2Polygon box = b2MakeOffsetBox(0.5f * gridSize, 0.5f * gridSize, new B2Vec2(x, y), b2Rot_identity);
70-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
70+
b2CreatePolygonShape(groundId, shapeDef, box);
7171
x += gridSize;
7272
}
7373
}
@@ -108,7 +108,7 @@ public BenchmarkSensor(SampleContext context) : base(context)
108108
{
109109
float x = i * shift - xCenter;
110110
B2Polygon box = b2MakeOffsetRoundedBox(0.5f, 0.5f, new B2Vec2(x, y), b2Rot_identity, 0.1f);
111-
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
111+
b2CreatePolygonShape(groundId, shapeDef, box);
112112
}
113113
}
114114
}
@@ -199,7 +199,7 @@ public override void Step()
199199
// Modify color while overlapped with a sensor
200200
B2SurfaceMaterial surfaceMaterial = b2Shape_GetSurfaceMaterial(@event.visitorShapeId);
201201
surfaceMaterial.customColor = (uint)B2HexColor.b2_colorLime;
202-
b2Shape_SetSurfaceMaterial(@event.visitorShapeId, ref surfaceMaterial);
202+
b2Shape_SetSurfaceMaterial(@event.visitorShapeId, surfaceMaterial);
203203
}
204204
}
205205

@@ -215,7 +215,7 @@ public override void Step()
215215
// Restore color to default
216216
B2SurfaceMaterial surfaceMaterial = b2Shape_GetSurfaceMaterial(@event.visitorShapeId);
217217
surfaceMaterial.customColor = 0;
218-
b2Shape_SetSurfaceMaterial(@event.visitorShapeId, ref surfaceMaterial);
218+
b2Shape_SetSurfaceMaterial(@event.visitorShapeId, surfaceMaterial);
219219
}
220220

221221
foreach (B2BodyId bodyId in zombies)

0 commit comments

Comments
 (0)