Skip to content

Commit da211d0

Browse files
committed
fix: trim.. again, add callbacks and such
1 parent ce03e3e commit da211d0

16 files changed

Lines changed: 229 additions & 79 deletions

src/NativeCodeGen.Cli/TrimmerRoots.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
<type fullname="NativeCodeGen.Core.Export.ExportSharedExample" preserve="all" />
2222
<type fullname="NativeCodeGen.Core.Export.ExportSharedExampleCode" preserve="all" />
2323
<type fullname="NativeCodeGen.Core.Registry.SharedExampleFrontmatter" preserve="all" />
24+
<type fullname="NativeCodeGen.Core.Parsing.Frontmatter" preserve="all" />
25+
<type fullname="NativeCodeGen.Core.Export.ExportCallout" preserve="all" />
26+
<type fullname="NativeCodeGen.Core.Models.CalloutType" preserve="all" />
2427
</assembly>
2528
</linker>

src/NativeCodeGen.Core/Export/DatabaseConverter.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,22 @@ private static ExportNative ConvertNative(NativeDefinition native)
9898
ReturnDescription = native.ReturnDescription,
9999
Aliases = native.Aliases.Count > 0 ? native.Aliases : null,
100100
RelatedExamples = native.RelatedExamples.Count > 0 ? native.RelatedExamples : null,
101+
Callouts = native.Callouts.Count > 0 ? native.Callouts.Select(ConvertCallout).ToList() : null,
101102
ApiSet = native.ApiSet,
102103
Parameters = native.Parameters.Select(ConvertParameter).ToList()
103104
};
104105
}
105106

107+
private static ExportCallout ConvertCallout(Callout callout)
108+
{
109+
return new ExportCallout
110+
{
111+
Type = callout.Type,
112+
Title = callout.Title,
113+
Description = callout.Description
114+
};
115+
}
116+
106117
private static ExportParameter ConvertParameter(NativeParameter param)
107118
{
108119
return new ExportParameter

src/NativeCodeGen.Core/Export/ExportModels.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public partial class ExportNative
7777
[ProtoMember(11)]
7878
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
7979
public List<string>? RelatedExamples { get; set; }
80+
81+
[ProtoMember(12)]
82+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
83+
public List<ExportCallout>? Callouts { get; set; }
8084
}
8185

8286
[ProtoContract]
@@ -206,3 +210,18 @@ public partial class ExportSharedExampleCode
206210
[ProtoMember(2)]
207211
public string? Language { get; set; }
208212
}
213+
214+
[ProtoContract]
215+
public partial class ExportCallout
216+
{
217+
[ProtoMember(1)]
218+
[JsonConverter(typeof(JsonStringEnumConverter<CalloutType>))]
219+
public CalloutType Type { get; set; }
220+
221+
[ProtoMember(2)]
222+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
223+
public string? Title { get; set; }
224+
225+
[ProtoMember(3)]
226+
public string Description { get; set; } = string.Empty;
227+
}

src/NativeCodeGen.Core/Export/JsonContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ namespace NativeCodeGen.Core.Export;
2424
[JsonSerializable(typeof(ExportSharedExample))]
2525
[JsonSerializable(typeof(ExportSharedExampleCode))]
2626
[JsonSerializable(typeof(List<ExportSharedExampleCode>))]
27+
[JsonSerializable(typeof(ExportCallout))]
28+
[JsonSerializable(typeof(List<ExportCallout>))]
29+
[JsonSerializable(typeof(CalloutType))]
2730
[JsonSerializable(typeof(ParamFlags))]
2831
[JsonSerializable(typeof(FieldFlags))]
2932
[JsonSerializable(typeof(List<ExportNative>))]

src/NativeCodeGen.Core/Export/natives.proto

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ message Native {
4545
repeated string used_enums = 9;
4646
string apiset = 10;
4747
repeated string related_examples = 11;
48+
repeated Callout callouts = 12;
4849
}
4950

5051
message Parameter {
@@ -99,3 +100,16 @@ message SharedExampleCode {
99100
string content = 1;
100101
optional string language = 2;
101102
}
103+
104+
enum CalloutType {
105+
NOTE = 0;
106+
WARNING = 1;
107+
INFO = 2;
108+
DANGER = 3;
109+
}
110+
111+
message Callout {
112+
CalloutType type = 1;
113+
optional string title = 2;
114+
string description = 3;
115+
}

src/NativeCodeGen.Core/Generation/CodeBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ public class CodeBuilder
99
{
1010
private readonly StringBuilder _sb = new();
1111
private readonly string _indentChar;
12+
private readonly string[] _cachedIndents;
1213
private int _indentLevel;
1314

1415
public CodeBuilder(string indentChar = " ")
1516
{
1617
_indentChar = indentChar;
18+
_cachedIndents = Enumerable.Range(0, 16)
19+
.Select(i => string.Concat(Enumerable.Repeat(indentChar, i)))
20+
.ToArray();
1721
}
1822

1923
public CodeBuilder Indent()
@@ -87,7 +91,9 @@ public CodeBuilder AppendLines(params string[] lines)
8791
return this;
8892
}
8993

90-
private string GetIndent() => string.Concat(Enumerable.Repeat(_indentChar, _indentLevel));
94+
private string GetIndent() => _indentLevel < _cachedIndents.Length
95+
? _cachedIndents[_indentLevel]
96+
: string.Concat(Enumerable.Repeat(_indentChar, _indentLevel));
9197

9298
public override string ToString() => _sb.ToString();
9399

src/NativeCodeGen.Core/Generation/RawNativeBuilder.cs

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public void EmitImports(bool singleFile = false)
4545
var prefix = singleFile ? "./" : "../";
4646
_cb.AppendLine($"import {{ Vector3 }} from '{prefix}types/Vector3';");
4747
_cb.AppendLine();
48-
EmitTypeScriptAliases();
48+
EmitTypeScriptAliases(_cb);
4949
}
5050
else
5151
{
52-
EmitLuaAliases();
52+
EmitLuaAliases(_cb);
5353
}
5454
}
5555

@@ -75,8 +75,6 @@ public static void EmitTypeScriptAliases(CodeBuilder cb)
7575
cb.AppendLine();
7676
}
7777

78-
private void EmitTypeScriptAliases() => EmitTypeScriptAliases(_cb);
79-
8078
/// <summary>
8179
/// Emits Lua aliases. Static so it can be used by other emitters.
8280
/// </summary>
@@ -97,8 +95,6 @@ public static void EmitLuaAliases(CodeBuilder cb)
9795
cb.AppendLine();
9896
}
9997

100-
private void EmitLuaAliases() => EmitLuaAliases(_cb);
101-
10298
public void EmitModuleHeader(string moduleName)
10399
{
104100
if (_lang == Language.Lua)
@@ -294,16 +290,10 @@ private void EmitInvokeNative(NativeDefinition native, List<NativeParameter> out
294290
else
295291
args.Add(native.Hash);
296292

297-
// Input params - use ArgumentBuilder shared logic
298-
foreach (var p in native.Parameters.Where(p => !p.IsPureOutput))
299-
{
300-
args.Add(GetArgumentExpression(p));
301-
}
302-
303-
// Output param pointers - use the type mapper
304-
foreach (var p in outputParams)
293+
// All params - ArgumentBuilder handles input, output, and in-out pointers
294+
foreach (var p in native.Parameters)
305295
{
306-
args.Add(_typeMapper.GetPointerPlaceholder(p.Type));
296+
args.Add(ArgumentBuilder.GetArgumentExpression(p, _typeMapper, _config, rawMode: true));
307297
}
308298

309299
// Result marker - use the type mapper
@@ -342,45 +332,6 @@ private void EmitInvokeNative(NativeDefinition native, List<NativeParameter> out
342332
}
343333
}
344334

345-
/// <summary>
346-
/// Gets the argument expression for a parameter.
347-
/// Handles float wrapping, hash wrapping, Vector3 expansion.
348-
/// </summary>
349-
private string GetArgumentExpression(NativeParameter p)
350-
{
351-
var f = _config.FloatWrapperAlias;
352-
var h = _config.HashWrapperAlias;
353-
var useFloat = _config.UseFloatWrapper;
354-
var useHash = _config.UseHashWrapper;
355-
356-
if (p.Type.Category == TypeCategory.Vector3)
357-
{
358-
// Vector3 components are floats - wrap if language needs it
359-
if (useFloat)
360-
return $"{f}({p.Name}.x), {f}({p.Name}.y), {f}({p.Name}.z)";
361-
else
362-
return $"{p.Name}.x, {p.Name}.y, {p.Name}.z";
363-
}
364-
else if (p.Type.Category == TypeCategory.Hash || p.Type.Name == "Hash")
365-
{
366-
if (useHash)
367-
return $"{h}({p.Name})";
368-
else
369-
return p.Name;
370-
}
371-
else if (p.Type.Name is "float" or "f32" or "f64" or "double")
372-
{
373-
if (useFloat)
374-
return $"{f}({p.Name})";
375-
else
376-
return p.Name;
377-
}
378-
else
379-
{
380-
return p.Name;
381-
}
382-
}
383-
384335
private void EmitComplexReturn(NativeDefinition native, List<NativeParameter> outputParams, List<string> args)
385336
{
386337
var isSingleVector3 = outputParams.Count == 1 &&

src/NativeCodeGen.Core/Generation/SharedClassGenerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace NativeCodeGen.Core.Generation;
1010
public class SharedClassGenerator
1111
{
1212
private readonly ILanguageEmitter _emitter;
13-
private static readonly HashSet<string> EntitySubclasses = new() { "Ped", "Vehicle", "Object", "Prop" };
1413

1514
public SharedClassGenerator(ILanguageEmitter emitter)
1615
{
@@ -306,7 +305,7 @@ private bool IsHandleMatch(TypeInfo type, string className)
306305
if (typeName == "Object" && className == "Prop")
307306
return true;
308307

309-
if (typeName == "Entity" && EntitySubclasses.Contains(className))
308+
if (typeName == "Entity" && NativeClassifier.EntitySubclasses.Contains(className))
310309
return true;
311310

312311
return false;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using ProtoBuf;
2+
3+
namespace NativeCodeGen.Core.Models;
4+
5+
[ProtoContract]
6+
public enum CalloutType
7+
{
8+
Note = 0,
9+
Warning = 1,
10+
Info = 2,
11+
Danger = 3
12+
}
13+
14+
public class Callout
15+
{
16+
public CalloutType Type { get; set; }
17+
public string? Title { get; set; }
18+
public required string Description { get; set; }
19+
}

src/NativeCodeGen.Core/Models/NativeDefinition.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class NativeDefinition
1414
public string ApiSet { get; set; } = "client";
1515
public List<string> UsedEnums { get; set; } = new();
1616
public List<string> RelatedExamples { get; set; } = new();
17+
public List<Callout> Callouts { get; set; } = new();
1718
public string? SourceFile { get; set; }
1819
}
1920

0 commit comments

Comments
 (0)