From 36534cc11c71e634aca4a6b556d2601195f10358 Mon Sep 17 00:00:00 2001 From: RobinCode Date: Tue, 3 Dec 2024 22:40:47 +0800 Subject: [PATCH] Fix kv commnets --- __tests__/KeyValues.test.ts | 10 ++++++++++ __tests__/KeyValues_comment1.txt | 10 ++++++++++ __tests__/KeyValues_comment2.txt | 9 +++++++++ package.json | 2 +- package/package.json | 2 +- src/KeyValues.ts | 3 +++ 6 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 __tests__/KeyValues_comment1.txt create mode 100644 __tests__/KeyValues_comment2.txt diff --git a/__tests__/KeyValues.test.ts b/__tests__/KeyValues.test.ts index 5ceacce..0e46a14 100644 --- a/__tests__/KeyValues.test.ts +++ b/__tests__/KeyValues.test.ts @@ -303,4 +303,14 @@ describe('KeyValues', () => { expect(root.FindIDTraverse(d.ID) === d).toBe(true); expect(root.FindIDTraverse(e.ID) === e).toBe(true); }); + + test('Check comments', async () => { + const kv = await KeyValues.Load(join(__dirname, 'KeyValues_comment1.txt')); + expect(kv.IsRoot()).toBe(true); + expect(kv.GetChildCount()).toBe(0); + + const kv2 = await KeyValues.Load(join(__dirname, 'KeyValues_comment2.txt')); + expect(kv2.IsRoot()).toBe(true); + expect(kv2.GetChildCount()).toBe(1); + }); }); diff --git a/__tests__/KeyValues_comment1.txt b/__tests__/KeyValues_comment1.txt new file mode 100644 index 0000000..18b64b9 --- /dev/null +++ b/__tests__/KeyValues_comment1.txt @@ -0,0 +1,10 @@ +// #base "npc/file01.txt" // file01 +// #base "npc/file02.txt" // file02 +// "DOTAAbilities" +// { +// "ability01" +// { +// "BaseClass" "ability_datadriven" +// "AbilityBehavior" "DOTA_ABILITY_BEHAVIOR_POINT" +// } +// } \ No newline at end of file diff --git a/__tests__/KeyValues_comment2.txt b/__tests__/KeyValues_comment2.txt new file mode 100644 index 0000000..90cd9d6 --- /dev/null +++ b/__tests__/KeyValues_comment2.txt @@ -0,0 +1,9 @@ +"DOTAAbilities" +{ + "ability01" + { + "BaseClass" "ability_datadriven" + "AbilityBehavior" "DOTA_ABILITY_BEHAVIOR_POINT" + } +} +// test comment \ No newline at end of file diff --git a/package.json b/package.json index 06f85cd..3f58f40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easy-keyvalues", - "version": "1.2.6", + "version": "1.2.7", "description": "Parse Valve KeyValues Format and easy to use in nodejs or browser.", "main": "dist/node.js", "engines": { diff --git a/package/package.json b/package/package.json index ac84141..fe29a70 100644 --- a/package/package.json +++ b/package/package.json @@ -1,6 +1,6 @@ { "name": "easy-keyvalues", - "version": "1.2.6", + "version": "1.2.7", "description": "Parse Valve KeyValues Format and easy to use in nodejs or browser.", "main": "node.js", "engines": { diff --git a/src/KeyValues.ts b/src/KeyValues.ts index 61ce959..4f8f7f1 100644 --- a/src/KeyValues.ts +++ b/src/KeyValues.ts @@ -508,6 +508,9 @@ export default class KeyValues { } if (data.body[data.pos + 1] === '/') { const endIndex = data.body.indexOf('\n', data.pos + 1); + if (endIndex < 0) { + break; + } const comment = data.body.slice(data.pos + 2, endIndex).trim(); if (comment) { if (isEndOfLineComment) {