Skip to content

Commit 3407c20

Browse files
Merge pull request #1 from fernforestgames/custom-resource-classes
Add support for `script_class` attribute in resource headers
2 parents b16853d + 3850ed6 commit 3407c20

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fernforestgames/godot-resource-parser",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Parser for Godot 4 .tscn and .tres files",
55
"author": "Justin Spahr-Summers <[email protected]> (https://fernforestgames.com)",
66
"license": "MIT",

src/parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ export class Parser {
108108
if (attrs['uid'] !== undefined) {
109109
header.uid = attrs['uid'] as string
110110
}
111+
if (attrs['script_class'] !== undefined) {
112+
header.scriptClass = attrs['script_class'] as string
113+
}
111114
return header
112115
} else {
113116
throw ParseSyntaxError.withContext(

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,15 @@ export interface GdSceneHeader {
248248
}
249249

250250
/**
251-
* File header for resources: [gd_resource type="X" load_steps=Y format=Z uid="..."]
251+
* File header for resources: [gd_resource type="X" load_steps=Y format=Z uid="..." script_class="..."]
252252
*/
253253
export interface GdResourceHeader {
254254
type: 'gd_resource'
255255
resourceType: string
256256
loadSteps?: number
257257
format: number
258258
uid?: string
259+
scriptClass?: string
259260
}
260261

261262
export type FileHeader = GdSceneHeader | GdResourceHeader

tests/fixtures/player_data.tres

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[gd_resource type="Resource" script_class="CustomData" load_steps=2 format=3]
2+
3+
[ext_resource type="Script" path="res://custom_data.gd" id="1_a2b3c"]
4+
5+
[resource]
6+
script = ExtResource("1_a2b3c")
7+
player_name = "Hero"
8+
score = 1500
9+
level = 5
10+
inventory = Array[String](["sword", "shield", "potion"])
11+
stats = {
12+
"health": 100,
13+
"mana": 50,
14+
"strength": 15
15+
}

tests/player_data.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { describe, expect, test } from '@jest/globals'
2+
import { readFileSync } from 'fs'
3+
import { join } from 'path'
4+
import { parseResource } from '../src/parser.js'
5+
import { isGodotResource } from '../src/types.js'
6+
7+
describe('Player data resource parsing', () => {
8+
test('parses player_data.tres with script_class attribute', () => {
9+
const content = readFileSync(join(__dirname, 'fixtures', 'player_data.tres'), 'utf-8')
10+
const result = parseResource(content)
11+
12+
expect(isGodotResource(result)).toBe(true)
13+
14+
// Check header with script_class
15+
expect(result.header.type).toBe('gd_resource')
16+
expect(result.header.resourceType).toBe('Resource')
17+
expect(result.header.scriptClass).toBe('CustomData')
18+
expect(result.header.format).toBe(3)
19+
expect(result.header.loadSteps).toBe(2)
20+
21+
// Check ext_resource
22+
expect(result.extResources).toHaveLength(1)
23+
expect(result.extResources[0]).toEqual({
24+
type: 'Script',
25+
path: 'res://custom_data.gd',
26+
id: '1_a2b3c',
27+
})
28+
29+
// Check resource properties
30+
expect(result.resource).toBeDefined()
31+
expect(result.resource?.properties['player_name']).toBe('Hero')
32+
expect(result.resource?.properties['score']).toBe(1500)
33+
expect(result.resource?.properties['level']).toBe(5)
34+
35+
// Check typed array
36+
expect(result.resource?.properties['inventory']).toEqual({
37+
type: 'array',
38+
elementType: 'String',
39+
values: ['sword', 'shield', 'potion'],
40+
})
41+
42+
// Check dictionary
43+
expect(result.resource?.properties['stats']).toEqual({
44+
health: 100,
45+
mana: 50,
46+
strength: 15,
47+
})
48+
49+
// Check script reference
50+
expect(result.resource?.properties['script']).toEqual({
51+
type: 'ExtResource',
52+
id: '1_a2b3c',
53+
})
54+
})
55+
})

0 commit comments

Comments
 (0)