Author Topic: 3.8.0 beta1, 64bit archlinux: XML-parser can’t handle ‘less than’ sign in lua  (Read 867 times)

Baŝto

  • Summoner
  • **
  • Posts: 59
    • View Profile
    • find me on diaspora
Everytime I use a „<“ in my scripts, the XML-parser crashes, because he expects an XML-tag.
If I use external scripts everything is working fine, but I don’t think that’s a good solution since it requires a disabled sandbox.

Megaglest version: 3.8.0 beta1
OS: 64bit archlinux

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
This bug has actually been known for quite a long time. As you can tell, XML doesn't really like working with Lua in the same file. External scripts are a good work around, but there's no easy way to insert a less than sign (opening triangle bracket). There's just no way to differentiate the less than operation from an XML tag. Instead, consider reversing all conditionals to use the greater than sign exclusively.

For example, instead of:

Code: [Select]
if numberOfWorkers < 10 do
doSomething()
end

Use:
Code: [Select]
if ~(10 > numberOfWorkers) do   -- If 10 is NOT greater than the number of workers
doSomething()
end

Of course, it is a bit annoying to be forced to write like this (especially since it's often more natural to write in the form of a "less than" equation, especially for loops that use a counter), so an external script is usually a better idea.

One alternative we could implement is to use CDATA tags on the content of XML tags (that is, the script portions). This means that the content of the tags (the "CDATA section") must be enclosed by <![CDATA[ and ]]>. So the above code could end up looking something like:

Code: [Select]
<unitAttacked>
<![CDATA[
if numberOfWorkers < 10 do
doSomething()
end
]]>
</unitAttacked>

Those crazy characters explicitly mark the beginning and end of the XML tag content. Thus, we can assume that any "less than" signs inside the CDATA section must be part of the Lua code, since XML does not resume until the CDATA section is over. CDATA is commonly used to nest content in XML files. This would be, in my opinion, the best way to deal with this problem, but we'd have to make sure not to break existing scenarios, which will not contain the CDATA sections. Thus, the sections must be optional.

I imagine that any good XML parser should have built in support for CDATA sections. A quick google search seems to indicate that the Xerces library does support CDATA (we still use Xerces, right?).

TL;DR: We should support CDATA sections in scenario XMLs (and encourage their use)
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

tomreyn

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 2,764
    • View Profile
    • MegaGlest - the free and open source cross platform 3D real-time strategy game
I'd also go with CDATA. It doesn't currently work, though - I just tested. The game will just ignore any script content enclosed in a CDATA tag.
« Last Edit: 17 August 2013, 07:42:26 by tomreyn »
atibox: Ryzen 1800X (8 cores @3.6GHz), 32 GB RAM, MSI Radeon RX 580 Gaming X 8G, PCI subsystem ID [1462:3417], (Radeon RX 580 chipset, POLARIS10) @3440x1440; latest stable Ubuntu release, (open source) radeon (amdgpu) / mesa video driver
atibox (old): Core2Quad Q9400 (4 cores @2.66GHz), 8 GB RAM, XFX HD-467X-DDF2, PCI subsystem ID [1682:2931], (Radeon HD 4670, RV730 XT) @1680x1050; latest stable Ubuntu release, (open source) radeon / mesa video driver
notebook: HP envy13d020ng
internet access: VDSL2+

· · · How YOU can contribute to MG · Latest development snapshot · How to build yourself · Megapack techtree · Currently hosted MG games · · ·

Baŝto

  • Summoner
  • **
  • Posts: 59
    • View Profile
    • find me on diaspora
I gonna use, I it’s less confusing this way

Code: [Select]
--lesser than
function lt(a,b)
return not(a >= b);
end

--lesser than or equal to
function le(a,b)
return not(a > b);
end

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
The lua is embedded in xml. This all characters must conform to xml standards. You must 'escape' such characters for example:

Code: [Select]
if (i > 0) then
should be:

Code: [Select]
if (i &gt; 0) then