Skip to content

Commit fdc2267

Browse files
authored
More robust JSON processing with tests and CI (#14)
- Fix issue when buffer uri has spaces - More robust JSON processing - Add tests with CI - Fix error handling UI - Add support for EXT_lights_ies
1 parent 3baae06 commit fdc2267

Some content is hidden

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

46 files changed

+2838
-347
lines changed

.github/workflows/main.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: main
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
Build:
11+
runs-on: windows-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
with:
17+
lfs: true
18+
19+
- name: Restore packages
20+
run: nuget restore
21+
working-directory: ./Source
22+
23+
- name: Setup build
24+
uses: microsoft/setup-msbuild@v2
25+
26+
- name: Build targets
27+
run: msbuild /t:ExplorerCommand /t:glTF /t:Tests /p:Configuration=Release /p:Platform=x64
28+
working-directory: ./Source
29+
30+
- name: Run tests
31+
run: dotnet test ./Build/Tests/bin/Release/AnyCPU/Tests.dll -s ./Source/.runsettings -v n

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ Build
55

66
# published files end up here
77
/Source/glTF/bin
8+
9+
# launc settings
10+
/Source/glTF/Properties/launchSettings.json

Source/.runsettings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<RunConfiguration>
4+
<ResultsDirectory>..\Build\Tests\Results</ResultsDirectory>
5+
</RunConfiguration>
6+
</RunSettings>
File renamed without changes.

Source/Core/Core.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<RootNamespace>glTF</RootNamespace>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Nodes;
3+
4+
namespace glTF
5+
{
6+
internal static class JsonExtensions
7+
{
8+
public static JsonArray? GetArray(this JsonNode node, string propertyName, JsonArray? defaultValue = null)
9+
{
10+
var propertyNode = node[propertyName];
11+
if (propertyNode == null || propertyNode.GetValueKind() != JsonValueKind.Array)
12+
{
13+
return defaultValue;
14+
}
15+
16+
return propertyNode.AsArray();
17+
}
18+
19+
public static int GetInt(this JsonNode node, string propertyName, int defaultValue = -1)
20+
{
21+
var propertyNode = node[propertyName];
22+
if (propertyNode == null || propertyNode.GetValueKind() != JsonValueKind.Number)
23+
{
24+
return defaultValue;
25+
}
26+
27+
return propertyNode.GetValue<int>();
28+
}
29+
30+
public static string? GetString(this JsonNode node, string propertyName, string? defaultValue = null)
31+
{
32+
var propertyNode = node[propertyName];
33+
if (propertyNode == null || propertyNode.GetValueKind() != JsonValueKind.String)
34+
{
35+
return defaultValue;
36+
}
37+
38+
return propertyNode.GetValue<string>();
39+
}
40+
41+
public static string? GetLocalPath(this JsonNode node, string propertyName, Uri baseUri, string? defaultValue = null)
42+
{
43+
var uriString = node.GetString(propertyName);
44+
if (uriString == null)
45+
{
46+
return defaultValue;
47+
}
48+
49+
if (!Uri.TryCreate(baseUri, uriString, out var uri) || !uri.IsFile)
50+
{
51+
return defaultValue;
52+
}
53+
54+
return uri.LocalPath;
55+
}
56+
57+
public static void SetInt(this JsonNode jsonNode, string propertyName, int value, int defaultValue)
58+
{
59+
if (value == defaultValue)
60+
{
61+
jsonNode.AsObject().Remove(propertyName);
62+
}
63+
else
64+
{
65+
jsonNode[propertyName] = JsonValue.Create(value);
66+
}
67+
}
68+
69+
public static bool Remove(this JsonNode node, string propertyName)
70+
{
71+
if (node.GetValueKind() != JsonValueKind.Object)
72+
{
73+
return false;
74+
}
75+
76+
return node.AsObject().Remove(propertyName);
77+
}
78+
}
79+
}

Source/Core/MimeType.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace glTF
2+
{
3+
internal class MimeType
4+
{
5+
public static string ToFileExtension(string? mimeType)
6+
{
7+
switch (mimeType)
8+
{
9+
case "image/png":
10+
return ".png";
11+
case "image/jpeg":
12+
return ".jpg";
13+
case "image/vnd-ms.dds":
14+
return ".dds";
15+
case "image/ktx2":
16+
return ".ktx2";
17+
case "image/webp":
18+
return ".webp";
19+
}
20+
21+
return ".bin";
22+
}
23+
24+
public static string FromFileExtension(string? fileExtension)
25+
{
26+
if (fileExtension != null)
27+
{
28+
switch (fileExtension.ToLower())
29+
{
30+
case ".png":
31+
return "image/png";
32+
case ".jpg":
33+
case ".jpeg":
34+
return "image/jpeg";
35+
case ".dds":
36+
return "image/vnd-ms.dds";
37+
case ".ktx2":
38+
return "image/ktx2";
39+
case ".webp":
40+
return "image/webp";
41+
}
42+
}
43+
44+
return "application/octet-stream";
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)