Skip to content

Commit 71b34d6

Browse files
committed
[#142] Idiomatic source defaults
1 parent 41c0b0c commit 71b34d6

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,35 @@ namespace GodotTests.TestScenes;
88

99
#region Test Cases
1010

11-
[ResourceTree("", ResI.LoadRes, xclude: ["TestScenes"])]
11+
[ResourceTree("res://", ResI.LoadRes, xclude: ["TestScenes"])]
1212
public static partial class RootResWithLoad;
1313

14-
[ResourceTree("", ResI.ResPaths, xclude: ["TestScenes"])]
14+
[ResourceTree("res://", ResI.ResPaths, xclude: ["TestScenes"])]
1515
public static partial class RootResWithResPaths;
1616

17-
[ResourceTree("/", ResI.DirPaths, xclude: ["TestScenes"])]
17+
[ResourceTree("res://", ResI.DirPaths, xclude: ["TestScenes"])]
1818
public static partial class RootResWithDirPaths;
1919

20-
[ResourceTree(".", ResI.LoadRes | ResI.ResPaths, xclude: ["TestScenes"])]
20+
[ResourceTree("res://", ResI.LoadRes | ResI.ResPaths, xclude: ["TestScenes"])]
2121
public static partial class RootResWithLoadAndResPaths;
2222

23-
[ResourceTree("Assets", ResI.DirPaths)]
23+
[ResourceTree("/", ResI.DirPaths)]
2424
public static partial class AbsoluteRes;
25-
25+
[ResourceTree("Assets", ResI.DirPaths)]
26+
public static partial class AbsoluteResDir1;
27+
[ResourceTree("/Assets", ResI.DirPaths)]
28+
public static partial class AbsoluteResDir2;
29+
30+
[ResourceTree(null, ResI.DirPaths)]
31+
public static partial class RelativeRes1;
32+
[ResourceTree(".", ResI.DirPaths)]
33+
public static partial class RelativeRes2;
34+
[ResourceTree("", ResI.DirPaths)]
35+
public static partial class RelativeRes3;
2636
[ResourceTree("Resources", ResI.DirPaths)]
27-
public static partial class RelativeRes;
37+
public static partial class RelativeResDir1;
38+
[ResourceTree("./Resources", ResI.DirPaths)]
39+
public static partial class RelativeResDir2;
2840

2941
[ResourceTree("Resources", resx: ResX.All, xtras: ["csv", "cfg", "txt", "zip"])]
3042
public static partial class ResWithTypes;
@@ -104,10 +116,20 @@ void TestRootResWithLoadAndResPaths()
104116
}
105117

106118
static void TestAbsoluteRes()
107-
=> AbsoluteRes.ResPath.Should().Be("res://Assets");
119+
{
120+
AbsoluteRes.ResPath.Should().Be("res://");
121+
AbsoluteResDir1.ResPath.Should().Be("res://Assets");
122+
AbsoluteResDir2.ResPath.Should().Be("res://Assets");
123+
}
108124

109125
static void TestRelativeRes()
110-
=> RelativeRes.ResPath.Should().Be("res://TestScenes/Feature148.ResourceTree/Resources");
126+
{
127+
RelativeRes1.ResPath.Should().Be("res://TestScenes/Feature148.ResourceTree");
128+
RelativeRes2.ResPath.Should().Be("res://TestScenes/Feature148.ResourceTree");
129+
RelativeRes3.ResPath.Should().Be("res://TestScenes/Feature148.ResourceTree");
130+
RelativeResDir1.ResPath.Should().Be("res://TestScenes/Feature148.ResourceTree/Resources");
131+
RelativeResDir2.ResPath.Should().Be("res://TestScenes/Feature148.ResourceTree/Resources");
132+
}
111133

112134
static void TestResTypes()
113135
{

SourceGenerators/Utilities/Extensions/GodotExtensions.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ static string FILE(string resPath, SyntaxNode node, AnalyzerConfigOptions option
5050
if (!Path.HasExtension(resPath))
5151
resPath = Path.ChangeExtension(resPath, ext);
5252

53-
var absPath = Path.Combine(gdRoot, resPath.Replace("res://", ""));
54-
if (File.Exists(absPath)) return Path.GetFullPath(absPath);
55-
5653
var relPath = Path.Combine(Path.GetDirectoryName(csFile), resPath);
5754
if (File.Exists(relPath)) return Path.GetFullPath(relPath);
5855

56+
var absPath = Path.Combine(gdRoot, resPath.Replace("res://", ""));
57+
if (File.Exists(absPath)) return Path.GetFullPath(absPath);
58+
5959
//
6060
throw new Exception($"Could not find {resPath}\n - {absPath}\n - {relPath}");
6161
}
@@ -68,18 +68,20 @@ public static (string SystemPath, DiagnosticDetail Error) GetRealDir(string sour
6868

6969
static string DIR(string source, SyntaxNode node, AnalyzerConfigOptions options)
7070
{
71+
source = string.IsNullOrEmpty(source) ? "." : source;
72+
7173
var csFile = node.SyntaxTree.FilePath;
72-
if (source is null) return Path.GetDirectoryName(csFile);
74+
if (source is ".") return Path.GetDirectoryName(csFile);
7375

7476
var gdRoot = options.TryGetGodotProjectDir() ?? GetProjectRoot(csFile);
7577
if (source is "/") return gdRoot;
7678

77-
var absPath = Path.Combine(gdRoot, source.Replace("res://", ""));
78-
if (Directory.Exists(absPath)) return Path.GetFullPath(absPath);
79-
8079
var relPath = Path.Combine(Path.GetDirectoryName(csFile), source);
8180
if (Directory.Exists(relPath)) return Path.GetFullPath(relPath);
8281

82+
var absPath = Path.Combine(gdRoot, source.Replace("res://", "").TrimStart('/'));
83+
if (Directory.Exists(absPath)) return Path.GetFullPath(absPath);
84+
8385
//
8486
throw new Exception($"Could not find {source}\n - {absPath}\n - {relPath}");
8587
}

0 commit comments

Comments
 (0)