Skip to content

Commit 887ef8c

Browse files
committed
Simplified a bunch of stuff related to the text formatter
1 parent 0d07103 commit 887ef8c

File tree

7 files changed

+63
-117
lines changed

7 files changed

+63
-117
lines changed

Assets/DebugOverlay/Console.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,28 +336,28 @@ public void _Write(char[] buf, int length)
336336
}
337337
}
338338

339-
char[] _buf = new char[1024];
340-
public void _Write<T>(string format, T argList) where T : IArgList
341-
{
342-
var length = StringFormatter.__Write<T>(ref _buf, 0, format, argList);
343-
_Write(_buf, length);
344-
}
339+
static char[] _buf = new char[1024];
345340

346341
public void Write(string format)
347342
{
348-
_Write(format, new ArgList0());
343+
var l = StringFormatter.Write(ref _buf, 0, format);
344+
_Write(_buf, l);
349345
}
346+
350347
public void Write<T>(string format, T arg)
351348
{
352-
_Write(format, new ArgList1<T>(arg));
349+
var l = StringFormatter.Write(ref _buf, 0, format, arg);
350+
_Write(_buf, l);
353351
}
354352
public void Write<T0, T1>(string format, T0 arg0, T1 arg1)
355353
{
356-
_Write(format, new ArgList2<T0, T1>(arg0, arg1));
354+
var l = StringFormatter.Write(ref _buf, 0, format, arg0, arg1);
355+
_Write(_buf, l);
357356
}
358357
public void Write<T0, T1, T2>(string format, T0 arg0, T1 arg1, T2 arg2)
359358
{
360-
_Write(format, new ArgList3<T0, T1, T2>(arg0, arg1, arg2));
359+
var l = StringFormatter.Write(ref _buf, 0, format, arg0, arg1, arg2);
360+
_Write(_buf, l);
361361
}
362362

363363
void Type(char c)

Assets/DebugOverlay/DebugOverlay.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,41 +113,47 @@ public static void Write(float x, float y, string format)
113113
{
114114
if (instance == null)
115115
return;
116-
instance._Write(x, y, format, new ArgList0());
116+
var l = StringFormatter.Write(ref _buf, 0, format);
117+
instance._DrawText(x, y, ref _buf, l);
117118
}
118119
public static void Write<T>(float x, float y, string format, T arg)
119120
{
120121
if (instance == null)
121122
return;
122-
instance._Write(x, y, format, new ArgList1<T>(arg));
123+
var l = StringFormatter.Write<T>(ref _buf, 0, format, arg);
124+
instance._DrawText(x, y, ref _buf, l);
123125
}
124126
public static void Write<T>(Color col, float x, float y, string format, T arg)
125127
{
126128
if (instance == null)
127129
return;
128130
Color c = instance.m_CurrentColor;
129131
instance.m_CurrentColor = col;
130-
instance._Write(x, y, format, new ArgList1<T>(arg));
132+
var l = StringFormatter.Write(ref _buf, 0, format, arg);
133+
instance._DrawText(x, y, ref _buf, l);
131134
instance.m_CurrentColor = c;
132135
}
133136
public static void Write<T0, T1>(float x, float y, string format, T0 arg0, T1 arg1)
134137
{
135138
if (instance == null)
136139
return;
137-
instance._Write(x, y, format, new ArgList2<T0, T1>(arg0, arg1));
140+
var l = StringFormatter.Write(ref _buf, 0, format, arg0, arg1);
141+
instance._DrawText(x, y, ref _buf, l);
138142
}
139143
public static void Write<T0, T1, T2>(float x, float y, string format, T0 arg0, T1 arg1, T2 arg2)
140144
{
141145
if (instance == null)
142146
return;
143-
instance._Write(x, y, format, new ArgList3<T0, T1, T2>(arg0, arg1, arg2));
147+
var l = StringFormatter.Write(ref _buf, 0, format, arg0, arg1, arg2);
148+
instance._DrawText(x, y, ref _buf, l);
144149
}
145150

146151
public static void Write<T0, T1, T2, T3>(float x, float y, string format, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
147152
{
148153
if (instance == null)
149154
return;
150-
instance._Write(x, y, format, new ArgList4<T0, T1, T2, T3>(arg0, arg1, arg2, arg3));
155+
var l = StringFormatter.Write(ref _buf, 0, format, arg0, arg1, arg2, arg3);
156+
instance._DrawText(x, y, ref _buf, l);
151157
}
152158

153159
// Draw a histogram of one set of data. Data must contain non-negative datapoints.
@@ -349,12 +355,7 @@ void _Clear()
349355
SetOrigin(0, 0);
350356
}
351357

352-
char[] _buf = new char[1024];
353-
void _Write<T>(float x, float y, string format, T argList) where T : IArgList
354-
{
355-
var num = StringFormatter.__Write<T>(ref _buf, 0, format, argList);
356-
_DrawText(x, y, ref _buf, num);
357-
}
358+
static char[] _buf = new char[1024];
358359

359360
public void Render()
360361
{

Assets/DebugOverlay/TextFormatter.cs

Lines changed: 29 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -124,110 +124,39 @@ void ConvertInt(ref char* dst, char* end, int value, int argWidth, int integerWi
124124
}
125125
}
126126

127-
public unsafe interface IArgList
127+
/// <summary>
128+
/// Garbage free string formatter
129+
/// </summary>
130+
public static unsafe class StringFormatter
128131
{
129-
int Count { get; }
130-
131-
void Format(ref char* dst, char* end, int argIndex, FormatSpec formatSpec);
132-
}
132+
private class NoArg {}
133133

134-
public unsafe struct ArgList0 : IArgList
135-
{
136-
public int Count { get { return 0; } }
137-
public void Format(ref char* dst, char* end, int argIndex, FormatSpec formatSpec)
134+
public static int Write(ref char[] dst, int destIdx, string format)
138135
{
136+
return Write<NoArg, NoArg, NoArg, NoArg, NoArg, NoArg>(ref dst, destIdx, format, null, null, null, null, null, null);
139137
}
140-
}
141-
public unsafe struct ArgList1<T0> : IArgList
142-
{
143-
public int Count { get { return 1; } }
144-
T0 t0;
145-
public ArgList1(T0 a0)
138+
public static int Write<T0>(ref char[] dst, int destIdx, string format, T0 arg0)
146139
{
147-
t0 = a0;
140+
return Write<T0, NoArg, NoArg, NoArg, NoArg, NoArg>(ref dst, destIdx, format, arg0, null, null, null, null, null);
148141
}
149-
public void Format(ref char* dst, char* end, int argIndex, FormatSpec formatSpec)
142+
public static int Write<T0,T1>(ref char[] dst, int destIdx, string format, T0 arg0, T1 arg1)
150143
{
151-
switch (argIndex)
152-
{
153-
case 0: (Converter.instance as IConverter<T0>).Convert(ref dst, end, t0, formatSpec); break;
154-
}
144+
return Write<T0, T1, NoArg, NoArg, NoArg, NoArg>(ref dst, destIdx, format, arg0, arg1, null, null, null, null);
155145
}
156-
}
157-
public unsafe struct ArgList2<T0, T1> : IArgList
158-
{
159-
public int Count { get { return 2; } }
160-
T0 t0;
161-
T1 t1;
162-
public ArgList2(T0 a0, T1 a1)
146+
public static int Write<T0,T1,T2>(ref char[] dst, int destIdx, string format, T0 arg0, T1 arg1, T2 arg2)
163147
{
164-
t0 = a0;
165-
t1 = a1;
148+
return Write<T0, T1, T2, NoArg, NoArg, NoArg>(ref dst, destIdx, format, arg0, arg1, arg2, null, null, null);
166149
}
167-
public void Format(ref char* dst, char* end, int argIndex, FormatSpec formatSpec)
150+
public static int Write<T0,T1,T2,T3>(ref char[] dst, int destIdx, string format, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
168151
{
169-
switch (argIndex)
170-
{
171-
case 0: (Converter.instance as IConverter<T0>).Convert(ref dst, end, t0, formatSpec); break;
172-
case 1: (Converter.instance as IConverter<T1>).Convert(ref dst, end, t1, formatSpec); break;
173-
}
152+
return Write<T0, T1, T2, T3, NoArg, NoArg>(ref dst, destIdx, format, arg0, arg1, arg2, arg3, null, null);
174153
}
175-
}
176-
public unsafe struct ArgList3<T0, T1, T2> : IArgList
177-
{
178-
public int Count { get { return 3; } }
179-
T0 t0;
180-
T1 t1;
181-
T2 t2;
182-
public ArgList3(T0 a0, T1 a1, T2 a2)
154+
public static int Write<T0,T1,T2,T3,T4>(ref char[] dst, int destIdx, string format, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
183155
{
184-
t0 = a0;
185-
t1 = a1;
186-
t2 = a2;
156+
return Write<T0, T1, T2, T3, T4, NoArg>(ref dst, destIdx, format, arg0, arg1, arg2, arg3, arg4, null);
187157
}
188-
public void Format(ref char* dst, char* end, int argIndex, FormatSpec formatSpec)
189-
{
190-
switch (argIndex)
191-
{
192-
case 0: (Converter.instance as IConverter<T0>).Convert(ref dst, end, t0, formatSpec); break;
193-
case 1: (Converter.instance as IConverter<T1>).Convert(ref dst, end, t1, formatSpec); break;
194-
case 2: (Converter.instance as IConverter<T2>).Convert(ref dst, end, t2, formatSpec); break;
195-
}
196-
}
197-
}
198158

199-
public unsafe struct ArgList4<T0, T1, T2, T3> : IArgList
200-
{
201-
public int Count { get { return 4; } }
202-
T0 t0;
203-
T1 t1;
204-
T2 t2;
205-
T3 t3;
206-
public ArgList4(T0 a0, T1 a1, T2 a2, T3 a3)
207-
{
208-
t0 = a0;
209-
t1 = a1;
210-
t2 = a2;
211-
t3 = a3;
212-
}
213-
public void Format(ref char* dst, char* end, int argIndex, FormatSpec formatSpec)
214-
{
215-
switch (argIndex)
216-
{
217-
case 0: (Converter.instance as IConverter<T0>).Convert(ref dst, end, t0, formatSpec); break;
218-
case 1: (Converter.instance as IConverter<T1>).Convert(ref dst, end, t1, formatSpec); break;
219-
case 2: (Converter.instance as IConverter<T2>).Convert(ref dst, end, t2, formatSpec); break;
220-
case 3: (Converter.instance as IConverter<T3>).Convert(ref dst, end, t3, formatSpec); break;
221-
}
222-
}
223-
}
224-
225-
/// <summary>
226-
/// Garbage free string formatter
227-
/// </summary>
228-
public static unsafe class StringFormatter
229-
{
230-
public static int __Write<T>(ref char[] dst, int destIdx, string format, T argList) where T : IArgList
159+
public static int Write<T0, T1, T2, T3, T4, T5>(ref char[] dst, int destIdx, string format, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
231160
{
232161
int written = 0;
233162
fixed (char* p = format, d = &dst[0])
@@ -299,10 +228,17 @@ public static int __Write<T>(ref char[] dst, int destIdx, string format, T argLi
299228
else
300229
src++;
301230

302-
if (argNum < 0 || argNum >= argList.Count)
303-
throw new IndexOutOfRangeException(argNum.ToString());
304-
305-
argList.Format(ref dest, end, argNum, s);
231+
switch(argNum)
232+
{
233+
case 0: ((IConverter<T0>)Converter.instance).Convert(ref dest, end, arg0, s); break;
234+
case 1: ((IConverter<T1>)Converter.instance).Convert(ref dest, end, arg1, s); break;
235+
case 2: ((IConverter<T2>)Converter.instance).Convert(ref dest, end, arg2, s); break;
236+
case 3: ((IConverter<T3>)Converter.instance).Convert(ref dest, end, arg3, s); break;
237+
case 4: ((IConverter<T4>)Converter.instance).Convert(ref dest, end, arg4, s); break;
238+
case 5: ((IConverter<T5>)Converter.instance).Convert(ref dest, end, arg5, s); break;
239+
default:
240+
throw new IndexOutOfRangeException(argNum.ToString());
241+
}
306242
}
307243
else
308244
{

ProjectSettings/DynamicsManager.asset

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--- !u!55 &1
44
PhysicsManager:
55
m_ObjectHideFlags: 0
6-
serializedVersion: 3
6+
serializedVersion: 5
77
m_Gravity: {x: 0, y: -9.81, z: 0}
88
m_DefaultMaterial: {fileID: 0}
99
m_BounceThreshold: 2
@@ -15,5 +15,9 @@ PhysicsManager:
1515
m_QueriesHitTriggers: 1
1616
m_EnableAdaptiveForce: 0
1717
m_EnablePCM: 1
18+
m_ClothInterCollisionDistance: 0
19+
m_ClothInterCollisionStiffness: 0
1820
m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1921
m_AutoSimulation: 1
22+
m_AutoSyncTransforms: 1
23+
m_ClothInterCollisionSettingsToggle: 0

ProjectSettings/Physics2DSettings.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Physics2DSettings:
2424
m_QueriesStartInColliders: 1
2525
m_ChangeStopsCallbacks: 0
2626
m_CallbacksOnDisable: 1
27+
m_AutoSyncTransforms: 1
2728
m_AlwaysShowColliders: 0
2829
m_ShowColliderSleep: 1
2930
m_ShowColliderContacts: 0

ProjectSettings/ProjectVersion.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
m_EditorVersion: 2017.1.0p5
1+
m_EditorVersion: 2017.3.0f3

UnityPackageManager/manifest.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"dependencies": {
3+
}
4+
}

0 commit comments

Comments
 (0)