Skip to content

Commit 4837c6a

Browse files
committed
[#142] StringName => string
1 parent 456394a commit 4837c6a

File tree

3 files changed

+45
-78
lines changed

3 files changed

+45
-78
lines changed

Godot 4 Tests/TestScenes/Feature148.ResourceTree/ResourceTreeTests.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ void TestRootResWithResPaths()
6969
typeof(RootResWithResPaths).ShouldConsistOf(Properties: ["DefaultBusLayoutTres"], NestedTypes: ["Assets"]);
7070
typeof(RootResWithResPaths.Assets).ShouldConsistOf(Properties: ["IconSvg"], NestedTypes: ["Tr"]);
7171
typeof(RootResWithResPaths.Assets.Tr).ShouldConsistOf(Properties: ["TrEnTranslation", "TrFrTranslation", "TrDeTranslation", "TrJpTranslation"]);
72-
RootResWithResPaths.DefaultBusLayoutTres.Should().Be((StringName)"res://default_bus_layout.tres");
73-
RootResWithResPaths.Assets.IconSvg.Should().Be((StringName)"res://Assets/icon.svg");
74-
RootResWithResPaths.Assets.Tr.TrEnTranslation.Should().Be((StringName)"res://Assets/tr/tr.en.translation");
72+
RootResWithResPaths.DefaultBusLayoutTres.Should().Be("res://default_bus_layout.tres");
73+
RootResWithResPaths.Assets.IconSvg.Should().Be("res://Assets/icon.svg");
74+
RootResWithResPaths.Assets.Tr.TrEnTranslation.Should().Be("res://Assets/tr/tr.en.translation");
7575
}
7676

7777
void TestRootResWithDirPaths()
7878
{
7979
typeof(RootResWithDirPaths).ShouldConsistOf(Properties: ["ResPath"], NestedTypes: ["Assets"]);
8080
typeof(RootResWithDirPaths.Assets).ShouldConsistOf(Properties: ["ResPath"], NestedTypes: ["Tr"]);
8181
typeof(RootResWithDirPaths.Assets.Tr).ShouldConsistOf(Properties: ["ResPath"]);
82-
RootResWithDirPaths.ResPath.Should().Be((StringName)"res://");
83-
RootResWithDirPaths.Assets.ResPath.Should().Be((StringName)"res://Assets");
84-
RootResWithDirPaths.Assets.Tr.ResPath.Should().Be((StringName)"res://Assets/tr");
82+
RootResWithDirPaths.ResPath.Should().Be("res://");
83+
RootResWithDirPaths.Assets.ResPath.Should().Be("res://Assets");
84+
RootResWithDirPaths.Assets.Tr.ResPath.Should().Be("res://Assets/tr");
8585
}
8686

8787
void TestRootResWithLoadAndResPaths()
@@ -93,9 +93,9 @@ void TestRootResWithLoadAndResPaths()
9393
typeof(RootResWithLoadAndResPaths.DefaultBusLayoutTres).ShouldConsistOf(Properties: ["ResPath"], Methods: ["Load"]);
9494
typeof(RootResWithLoadAndResPaths.Assets.Tr.TrEnTranslation).ShouldConsistOf(Properties: ["ResPath"], Methods: ["Load"]);
9595

96-
RootResWithLoadAndResPaths.Assets.IconSvg.ResPath.Should().Be((StringName)"res://Assets/icon.svg");
97-
RootResWithLoadAndResPaths.DefaultBusLayoutTres.ResPath.Should().Be((StringName)"res://default_bus_layout.tres");
98-
RootResWithLoadAndResPaths.Assets.Tr.TrEnTranslation.ResPath.Should().Be((StringName)"res://Assets/tr/tr.en.translation");
96+
RootResWithLoadAndResPaths.Assets.IconSvg.ResPath.Should().Be("res://Assets/icon.svg");
97+
RootResWithLoadAndResPaths.DefaultBusLayoutTres.ResPath.Should().Be("res://default_bus_layout.tres");
98+
RootResWithLoadAndResPaths.Assets.Tr.TrEnTranslation.ResPath.Should().Be("res://Assets/tr/tr.en.translation");
9999

100100
RootResWithLoadAndResPaths.Assets.IconSvg.Load().Should().BeOfType<CompressedTexture2D>().And.NotBeNull();
101101
RootResWithLoadAndResPaths.DefaultBusLayoutTres.Load().Should().BeOfType<AudioBusLayout>().And.NotBeNull();
@@ -104,25 +104,25 @@ void TestRootResWithLoadAndResPaths()
104104
}
105105

106106
static void TestAbsoluteRes()
107-
=> AbsoluteRes.ResPath.Should().Be((StringName)"res://Assets");
107+
=> AbsoluteRes.ResPath.Should().Be("res://Assets");
108108

109109
static void TestRelativeRes()
110-
=> RelativeRes.ResPath.Should().Be((StringName)"res://TestScenes/Feature148.ResourceTree/Resources");
110+
=> RelativeRes.ResPath.Should().Be("res://TestScenes/Feature148.ResourceTree/Resources");
111111

112112
static void TestResTypes()
113113
{
114114
ResTypes.MyCsShaderCs.Should().BeOfType<CSharpScript>().And.NotBeNull();
115-
ResTypes.MyCsShaderCsUid.Should().Be((StringName)"uid://dmex1g7fv35a");
115+
ResTypes.MyCsShaderCsUid.Should().Be("uid://dmex1g7fv35a");
116116
ResTypes.MyCsShaderTres.Should().BeOfType<MyCsShader>().And.NotBeNull();
117117

118118
ResTypes.MyGdShaderGd.Should().BeOfType<GDScript>().And.NotBeNull();
119-
ResTypes.MyGdShaderGdUid.Should().Be((StringName)"uid://dwdevd03t3rpx");
119+
ResTypes.MyGdShaderGdUid.Should().Be("uid://dwdevd03t3rpx");
120120
ResTypes.MyGdShaderTres.Should().BeOfType<ShaderMaterial>().And.NotBeNull();
121121

122-
ResTypes.Xtras._3DModelTxt.Should().Be((StringName)"res://TestScenes/Feature148.ResourceTree/Resources/xtras/3DModel.txt");
123-
ResTypes.Xtras.Model3DTxt.Should().Be((StringName)"res://TestScenes/Feature148.ResourceTree/Resources/xtras/Model3D.txt");
124-
ResTypes.Xtras.MyResCfg.Should().Be((StringName)"res://TestScenes/Feature148.ResourceTree/Resources/xtras/MyRes.cfg");
125-
ResTypes.Xtras.MyResCsv.Should().Be((StringName)"res://TestScenes/Feature148.ResourceTree/Resources/xtras/MyRes.csv");
122+
ResTypes.Xtras._3DModelTxt.Should().Be("res://TestScenes/Feature148.ResourceTree/Resources/xtras/3DModel.txt");
123+
ResTypes.Xtras.Model3DTxt.Should().Be("res://TestScenes/Feature148.ResourceTree/Resources/xtras/Model3D.txt");
124+
ResTypes.Xtras.MyResCfg.Should().Be("res://TestScenes/Feature148.ResourceTree/Resources/xtras/MyRes.cfg");
125+
ResTypes.Xtras.MyResCsv.Should().Be("res://TestScenes/Feature148.ResourceTree/Resources/xtras/MyRes.csv");
126126

127127
ResTypesWithScenes.ResourceTreeTestsTscn.Should().BeOfType<PackedScene>().And.NotBeNull();
128128
}

README.md

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -255,78 +255,62 @@ Generates:
255255
```
256256
static partial class MyRes
257257
{
258-
private static StringName _ResPath; // -- (Res.DirPaths)
259-
public static StringName ResPath => _ResPath ??= "res://";
258+
public static string ResPath => "res://"; // -- (Res.DirPaths)
260259
261-
public static partial class Assets // -- (Each folder generates a nested type)
260+
public static partial class Assets // -- (Each folder generates a nested type)
262261
{
263-
private static StringName _ResPath; // -- (Res.DirPaths)
264-
public static StringName ResPath => _ResPath ??= "res://Assets";
262+
public static string ResPath => "res://Assets"; // -- (Res.DirPaths)
265263
266-
public static partial class IconSvg // -- (Res.ResPaths | Res.Load - generates nested type)
264+
public static partial class IconSvg // -- (Res.ResPaths | Res.Load - generates nested type)
267265
{
268-
private static StringName _ResPath;
269-
public static StringName ResPath => _ResPath ??= "res://Assets/icon.svg";
266+
public static string ResPath => "res://Assets/icon.svg";
270267
public static CompressedTexture2D Load() => GD.Load<CompressedTexture2D>(ResPath);
271268
}
272269
273-
private static StringName _HelpTxt; // -- (xtras - always generated as resource path)
274-
public static StringName HelpTxt => _HelpTxt ??= "res://Assets/Help.txt";
270+
public static string HelpTxt => "res://Assets/Help.txt"; // -- (xtras - always generated as resource path)
275271
276-
public static class Tr // -- (Only folders with discoverable resources are generated)
272+
public static class Tr // -- (Only folders with discoverable resources are generated)
277273
{
278-
private static StringName _ResPath;
279-
public static StringName ResPath => _ResPath ??= "res://Assets/tr";
274+
public static string ResPath => "res://Assets/tr"; // -- (Res.DirPaths)
280275
281-
// If an importer generates files, these are provided rather than the input file as the input file will not be available when project is exported
282-
283-
public static class TrEnTranslation
276+
public static class TrEnTranslation // -- (uses importer generated files instead of raw input file)
284277
{
285-
private static StringName _ResPath;
286-
public static StringName ResPath => _ResPath ??= "res://Assets/tr/tr.en.translation";
278+
public static string ResPath => "res://Assets/tr/tr.en.translation";
287279
public static OptimizedTranslation Load() => GD.Load<OptimizedTranslation>(ResPath);
288280
}
289281
290282
public static class TrFrTranslation
291283
{
292-
private static StringName _ResPath;
293-
public static StringName ResPath => _ResPath ??= "res://Assets/tr/tr.fr.translation";
284+
public static string ResPath => "res://Assets/tr/tr.fr.translation";
294285
public static OptimizedTranslation Load() => GD.Load<OptimizedTranslation>(ResPath);
295286
}
296287
}
297288
}
298289
299290
public static partial class Scenes
300291
{
301-
private static StringName _ResPath;
302-
public static StringName ResPath => _ResPath ??= "res://Scenes";
292+
public static string ResPath => "res://Scenes";
303293
304294
public static class MySceneTscn
305295
{
306-
private static StringName _ResPath;
307-
public static StringName ResPath => _ResPath ??= "res://Scenes/MyScene.tscn";
296+
public static string ResPath => "res://Scenes/MyScene.tscn";
308297
public static PackedScene Load() => GD.Load<PackedScene>(ResPath);
309298
}
310299
311300
public static class MySceneGd
312301
{
313-
private static StringName _ResPath;
314-
public static StringName ResPath => _ResPath ??= "res://Scenes/MyScene.gd";
302+
public static string ResPath => "res://Scenes/MyScene.gd";
315303
public static GDScript Load() => GD.Load<GDScript>(ResPath);
316304
}
317305
318306
public static class MySceneCs
319307
{
320-
private static StringName _ResPath;
321-
public static StringName ResPath => _ResPath ??= "res://Scenes/MyScene.cs";
308+
public static string ResPath => "res://Scenes/MyScene.cs";
322309
public static CSharpScript Load() => GD.Load<CSharpScript>(ResPath);
323310
}
324311
325-
private static StringName _MySceneCsUid; // -- (Res.Uid - always generated as uid)
326-
public static StringName MySceneCsUid => _MySceneCsUid ??= "uid://tyjsxc2njtw2";
327-
328-
private static StringName _MySceneGdUid;
329-
public static StringName MySceneGdUid => _MySceneGdUid ??= "uid://sho6tst545eo";
312+
public static string MySceneCsUid => "uid://tyjsxc2njtw2"; // -- (Res.Uid - always generated as uid)
313+
public static string MySceneGdUid => "uid://sho6tst545eo";
330314
}
331315
}
332316
```
@@ -341,16 +325,12 @@ static partial class MyRes
341325
{
342326
public static partial class Assets
343327
{
344-
public static CompressedTexture2D IconSvg
345-
=> GD.Load<CompressedTexture2D>("res://Assets/icon.svg");
328+
public static CompressedTexture2D IconSvg => GD.Load<CompressedTexture2D>("res://Assets/icon.svg");
346329
347330
public static class Tr
348331
{
349-
public static OptimizedTranslation TrEnTranslation
350-
=> GD.Load<OptimizedTranslation>("res://Assets/tr/tr.en.translation");
351-
352-
public static OptimizedTranslation TrFrTranslation
353-
=> GD.Load<OptimizedTranslation>("res://Assets/tr/tr.fr.translation");
332+
public static OptimizedTranslation TrEnTranslation => GD.Load<OptimizedTranslation>("res://Assets/tr/tr.en.translation");
333+
public static OptimizedTranslation TrFrTranslation => GD.Load<OptimizedTranslation>("res://Assets/tr/tr.fr.translation");
354334
}
355335
}
356336
}
@@ -366,16 +346,12 @@ static partial class MyRes
366346
{
367347
public static partial class Assets
368348
{
369-
private static StringName _IconSvg;
370-
public static StringName IconSvg => _IconSvg ??= "res://Assets/icon.svg";
349+
public static string IconSvg => "res://Assets/icon.svg";
371350
372351
public static class Tr
373352
{
374-
private static StringName _TrEnTranslation;
375-
public static StringName TrEnTranslation => _TrEnTranslation ??= "res://Assets/tr/tr.en.translation";
376-
377-
private static StringName _TrFrTranslation;
378-
public static StringName TrFrTranslation => _TrFrTranslation ??= "res://Assets/tr/tr.fr.translation";
353+
public static string TrEnTranslation => "res://Assets/tr/tr.en.translation";
354+
public static string TrFrTranslation => "res://Assets/tr/tr.fr.translation";
379355
}
380356
}
381357
}

SourceGenerators/ResourceTreeExtensions/ResourceTreeTemplate.scriban

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,28 @@
33
{{~ func RenderFile(node, depth) ~}}
44
{{~ if node.Value.Type ~}}
55
{{~ if node.Value.Show ~}}
6-
{{~ NL for.index > 0 || node.Parent.Value.Show ~}}
76
{{~ TAB depth}}public static class {{SAFE_NAME node depth}}
87
{{~ TAB depth}}{
9-
{{~ TAB depth}} private static StringName _ResPath;
10-
{{~ TAB depth}} public static StringName ResPath => _ResPath ??= "{{node.Value.Path}}";
8+
{{~ TAB depth}} public static string ResPath => "{{node.Value.Path}}";
119
{{~ TAB depth}} public static {{node.Value.Type}} Load() => GD.Load<{{node.Value.Type}}>(ResPath);
1210
{{~ TAB depth}}}
1311
{{~ else ~}}
14-
{{~ NL for.index > 0 || node.Parent.Value.Show ~}}
15-
{{~ TAB depth}}public static {{node.Value.Type}} {{node.Value.Name}}
16-
{{~ TAB depth}} => GD.Load<{{node.Value.Type}}>("{{node.Value.Path}}");
12+
{{~ TAB depth}}public static {{node.Value.Type}} {{node.Value.Name}} => GD.Load<{{node.Value.Type}}>("{{node.Value.Path}}");
1713
{{~ end ~}}
1814
{{~ else ~}}
1915
{{~ if node.Value.Show ~}}
20-
{{~ NL for.index > 0 || node.Parent.Value.Show ~}}
21-
{{~ TAB depth}}private static StringName _{{node.Value.Name}};
22-
{{~ TAB depth}}public static StringName {{node.Value.Name}} => _{{node.Value.Name}} ??= "{{node.Value.Path}}";
16+
{{~ TAB depth}}public static string {{node.Value.Name}} => "{{node.Value.Path}}";
2317
{{~ end ~}}
2418
{{~ end ~}}
2519
{{~ end ~}}
2620

2721
{{-#####-RenderDir-#####-}}
2822

2923
{{~ func RenderDir(node, depth) ~}}
30-
{{~ NL for.index > 0 || node.Parent.Value.Show ~}}
3124
{{~ TAB depth}}public static class {{SAFE_NAME node depth}}
3225
{{~ TAB depth}}{
3326
{{~ if node.Value.Show ~}}
34-
{{~ TAB depth}} private static StringName _ResPath;
35-
{{~ TAB depth}} public static StringName ResPath => _ResPath ??= "{{node.Value.Path}}";
27+
{{~ TAB depth}} public static string ResPath => "{{node.Value.Path}}";
3628
{{~ end ~}}
3729
{{~ for child in node.Children ~}}
3830
{{~ RenderNode child depth+1 ~}}
@@ -55,8 +47,7 @@
5547

5648
{{~ func RenderRoot(node) ~}}
5749
{{~ if node.Value.Show ~}}
58-
private static StringName _ResPath;
59-
public static StringName ResPath => _ResPath ??= "{{node.Value.Path}}";
50+
public static string ResPath => "{{node.Value.Path}}";
6051
{{~ end ~}}
6152
{{~ for child in node.Children ~}}
6253
{{~ RenderNode child ~}}

0 commit comments

Comments
 (0)