Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Python script to prepare a lua code for reloading
Browse files Browse the repository at this point in the history
Removes all comments and escapes '<'.

Usage:

cd courseplay\reload
python reload.py ..\mode6.lua

Then, in the dev console:
cpLoadFile reload.xml
  • Loading branch information
pvaiko committed Dec 12, 2018
1 parent c87a5b5 commit 1e429f7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -3,6 +3,10 @@
*.gitattributes
*.idea
*.iml
out
reload/venv
reload.xml


# Windows image file caches
Thumbs.db
Expand Down
55 changes: 55 additions & 0 deletions reload/reload.py
@@ -0,0 +1,55 @@
import sys
import os


def escape_and_write(outfile, c):
if c == '<':
outfile.write('&lt;')
else:
outfile.write(c)


def read_until_end_of_comment(infile, outfile):
c =infile.read(1)
if c == '-':
# this is the second - character, start of a comment
in_block_comment = False
while c and (in_block_comment or not c == '\n'):
prevC = c
c = infile.read(1)
if c == '[' and prevC == '[':
in_block_comment = True
if c == ']' and prevC == ']':
in_block_comment = False
else:
# write the - which triggered this call
escape_and_write(outfile, '-')
# and write the character we read afterwards (non -)
escape_and_write(outfile, c)


luaFile = sys.argv[1]
dirName = os.path.dirname(luaFile)

in_string = False
open_string = ''

with open(luaFile, mode='r') as infile, open('../reload.xml', 'w') as outfile:
outfile.write('<code>')
while True:
c = infile.read(1)
if not c:
break
if c == '"' or c == "'":
if not in_string:
open_string = c
in_string = True
elif open_string == c:
in_string = False

if c == '-' and not in_string:
read_until_end_of_comment(infile, outfile)
else:
escape_and_write(outfile, c)

outfile.write('</code>')

0 comments on commit 1e429f7

Please sign in to comment.