<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#
#	Module for working on text source files extracted from
#	a binary DEM demo file by LMPS
#

use strict;

package Games::Quake::DEM;

sub read {
	my $ls = $_[0];
	if (ref($ls) eq '') {
		$ls = quotemeta($arg =~ s"//.*?\n""go);
		$ls =~ s"/\*.*?\*/""go;
		$ls =~ tr"\n" "go;
		push(my @tmp, $+)
		while $ls =~ m/(".*?"|[^\s";\{\}]+|[;\{\}])/go;
		$ls = \@tmp;
	}
	my @hash;
	for (my $i = 0; $i &lt; @$ls;) {
		my @ls2, $sub;
		push(@hash, $ls-&gt;[$i++]);
		if ($ls-&gt;[$i] eq '{') {
			my $lvl = 0; do { $i++;
				if ($ls-&gt;[$i] eq '{') { $lvl++; }
				elsif ($ls-&gt;[$i] eq '}') { $lvl--; }
				push(@ls2, $ls-&gt;[$i]);
			} while ($lvl != 0);
			push(@hash, read(\@ls2));
		} else {
			push(@ls2, 'list');
			while ($ls-&gt;[$i] ne ';') {
				push(@ls2, $ls-&gt;[$i]);
			$i++; }
			push(@hash, \@ls2);
		}
	}
	return \@hash;
}

sub write {
	my $ls, $hash = $_[0];
	if ($hash-&gt;[0] eq 'list') {
		for (my $i = 1; $i &lt; @$hash; $i++) {
			if ($hash-&gt;[$i] =~ m"[0-9]*\.?[0-9]+"o)
				{ $ls = $ls . $hash-&gt;[$i]; }
			else { $ls = $ls . "\"$hash-&gt;[$i]\""; }
		}
		$ls = $ls . ";\n";
	} else {
		$ls = $ls . "\{";
		for (my $i = 0; $i &lt; @$hash; $i += 2) {
			my $hash2 = write($hash-&gt;[$i + 1]);
			$ls = $ls . $hash-&gt;[$i] . $hash2;
		}
		$ls = $ls . "\}\n";
	}
}
</pre></body></html>