[ZScript] Parse INI-style data in lumps

Post your example zscripts/ACS scripts/etc here.
Forum rules
The Projects forums are only for projects. If you are asking questions about a project, either find that project's thread, or start a thread in the General section instead.

Got a cool project idea but nothing else? Put it in the project ideas thread instead!

Projects for any Doom-based engine (especially 3DGE) are perfectly acceptable here too.

Please read the full rules for more details.
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

[ZScript] Parse INI-style data in lumps

Post by argv »

I've written a library for parsing INI-style files from ZScript. See it on GitHub.
User avatar
Nash
 
 
Posts: 17448
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: [ZScript] Parse INI-style data in lumps

Post by Nash »

Sick!!! Thanks for putting this out. I'll most definitely have a use for this (with credits to you, of course).
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Re: [ZScript] Parse INI-style data in lumps

Post by argv »

I've added several more features! Check out the updated README.
User avatar
Nash
 
 
Posts: 17448
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: [ZScript] Parse INI-style data in lumps

Post by Nash »

Thanks so much argv. Just curious, would it be hard to write a JSON or XML parser too?
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Re: [ZScript] Parse INI-style data in lumps

Post by argv »

Impossible to do correctly, actually. ZScript cannot (as far as I know) decode UTF-8, which all JSON and most XML is encoded in.

Even with that, though, it would be very hard. Parsers in ZScript have to be written entirely by hand, since there are no parser generator tools or libraries for it (and they'd probably be agonizingly slow anyway). INI is a pretty trivial format, and it takes a fair bit of ZScript code to parse even that.

GZDoom actually comes with a JSON parser, which it uses for saved games. GZDoom save files are zips containing JSON files. It's not exposed to ZScript, though. If you want that, you could ask Graf to expose it.
User avatar
The Zombie Killer
Posts: 1528
Joined: Thu Jul 14, 2011 12:06 am
Location: Gold Coast, Queensland, Australia

Re: [ZScript] Parse INI-style data in lumps

Post by The Zombie Killer »

argv wrote:Impossible to do correctly, actually. ZScript cannot (as far as I know) decode UTF-8, which all JSON and most XML is encoded in.

Even with that, though, it would be very hard. Parsers in ZScript have to be written entirely by hand, since there are no parser generator tools or libraries for it (and they'd probably be agonizingly slow anyway). INI is a pretty trivial format, and it takes a fair bit of ZScript code to parse even that.

GZDoom actually comes with a JSON parser, which it uses for saved games. GZDoom save files are zips containing JSON files. It's not exposed to ZScript, though. If you want that, you could ask Graf to expose it.
I have a number of classes and functions I've written that you may find useful for this, if you're interested in tackling it. Namely, I have Stream classes that support UTF-8, a hash map, a tokenizer, a StringUtility class that can handle Unicode and convert it to UTF-8, etc.

You can find it here: https://gitlab.com/TheZombieKiller/zk/t ... er/zk-core

I definitely agree that getting the JSON parser already present in the engine exposed to ZScript is the superior solution, though.
User avatar
Nash
 
 
Posts: 17448
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: [ZScript] Parse INI-style data in lumps

Post by Nash »

2 recent commits (this and this) is causing VM aborts (array out of bounds) with this library.

In particular, MergeByActorClass(); setting either purgeSuperSubSections or purgeNoMatch to true will cause the array out of bounds abort.

I'm too tired to look into this in more detail, I'm just putting this here so I can come back to this eventually
User avatar
randi
Site Admin
Posts: 7746
Joined: Wed Jul 09, 2003 10:30 pm

Re: [ZScript] Parse INI-style data in lumps

Post by randi »

Problem is this:

Code: Select all

for (uint sectionIndex = Sections.Size() - 1; sectionIndex >= 0; sectionIndex--)
An unsigned integer can never be less than 0. sectionIndex was effectively a signed int before; now it gets to stay as unsigned. One fix would be to rewrite it so that you test with the pre-decremented value:

Code: Select all

for (uint sectionIndex = Sections.Size(); sectionIndex-- > 0; )
or declare sectionIndex as int instead.

Also, the second commit you linked is for making unsigned immediate values work. These are numbers that end with a u, like 0u, 32u, etc. So not applicable here.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49091
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: [ZScript] Parse INI-style data in lumps

Post by Graf Zahl »

In cases like this I'd normally say "fix the mod", but if this had been used in released content that might be a problem.
If it can be version tested in the compiler it should be.
User avatar
Nash
 
 
Posts: 17448
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: [ZScript] Parse INI-style data in lumps

Post by Nash »

randi wrote: Also, the second commit you linked is for making unsigned immediate values work. These are numbers that end with a u, like 0u, 32u, etc. So not applicable here.
Ah, woops, my bad, apologies - as I said, was tired when I made the post, I didn't look properly :oops:

As far as this library goes - its advocated usage was "embed it into your mods" so who knows how many mods have embedded this library directly.

I can fix my local copy, of course, but I can't say the same for others...
User avatar
Sir Robin
Posts: 537
Joined: Wed Dec 22, 2021 7:02 pm
Graphics Processor: Intel (Modern GZDoom)
Location: Medellin, Colombia

Re: [ZScript] Parse INI-style data in lumps

Post by Sir Robin »

FYI, regarding XML, I haven't tried it, but I saw this thread
User avatar
Nash
 
 
Posts: 17448
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: [ZScript] Parse INI-style data in lumps

Post by Nash »

With the version set to 4.11.1 - there are now warnings about multi-returns: "Got 1, but expected 3" -- and furthermore, upgrading to 4.12 simply aborts startup with error.

Sample:

Code: Select all

	String, DisdainINISection, uint Get(String sectionName, String key, String default = "") const {
		let section = Section(sectionName);
		if (!section)
			return default, null, 0;

		String value;
		uint keyIndex;
		[value, keyIndex] = section.Get(key);

		return value, section, keyIndex;
	}

Return to “Script Library”