commit 4ca6b1c7a063a6a834ed709d2d1327bf68c68c3d Author: khuxkm Date: Fri Jul 13 08:34:01 2018 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..edcab64 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# p8btools - Build tools for PICO-8 + +Submodule in, and you can use the submodule as a python module. + +Use a Makefile like so: +``` +NAME:=blazzle +SRC:=$(wildcard *.lua) +$(NAME).p8: $(SRC) + python combine.py $(NAME).p8 base.p8 $(SRC) + +.PHONY: clean +clean: + rm $(NAME).p8 +``` + +A combine.py file like so: +```python +import p8btools,argparse,io + +parser = argparse.ArgumentParser(prog="python combine.py",description="Combines a lua file into a .p8 cart.") +parser.add_argument("result",help="The filename for the resulting cart.") +parser.add_argument("p8cart",help="A .p8 cart.") +parser.add_argument("luafile",help="The lua file to inject into the .p8 cart.",nargs="+") +args = parser.parse_args() + +cart = p8btools.Cart(args.p8cart) +t = "" +for luafile in args.luafile: + lua = p8btools.LuaCode(luafile) + lua.parse() + t += lua.text + t += "\n" +cart.lua = t.rstrip() +with io.open(args.result,"w",encoding="utf-8") as f: + f.write(cart.fulltext) +``` + +and a base.p8 like so: +``` +pico-8 cartridge // http://www.pico-8.com +version 16 +__lua__ + +``` diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..62cc165 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from .preprocessor import Cart, LuaCode + +__all__ = ['Cart','LuaCode'] diff --git a/preprocessor.py b/preprocessor.py new file mode 100644 index 0000000..fd1e86a --- /dev/null +++ b/preprocessor.py @@ -0,0 +1,115 @@ +import io +class Cart: + def __init__(self,basecart): + contents = "" + with io.open(basecart,encoding="utf-8") as f: + contents = f.read() + lines = contents.split("\n") + if "__gfx__" not in lines: + lines.append("__gfx__") + self.header = "\n".join(lines[:3]) + self.rest = "\n".join(lines[lines.index("__gfx__"):]) + self.lua = "\n".join(lines[3:lines.index("__gfx__")]) + + @property + def fulltext(self): + return "\n".join(getattr(self,k) for k in ("header","lua","rest")) + +class LuaCode: + def __init__(self,filename): + lines = [] + with io.open(filename,encoding="utf-8") as f: + lines = [l.rstrip() for l in f if l] + self.lines = [] + i=0 + while i0 and (p in s): + yield o+s.find(p) + o+=s.find(p)+len(p) + s = s[s.find(p)+len(p):] + +if __name__=="__main__": + l=LuaCode("test.lua") + l.parse() + print(l.text)