Advertisement
Guest User

ecstool.py

a guest
Jan 2nd, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.18 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Utility to do some of the ECS monkey work
  4.  
  5. # Format:
  6. #  ./ecstool.py create component CSpr
  7. #  ./ecstool.py create usystem UAnimSpr +CAnimSpr +CPos +CObj -CIso *CDir *CIsoDir
  8. #  ./ecstool.py create dsystem DParticles
  9. #  ./ecstool.py create singlesystem SCollision
  10.  
  11. import sys
  12. from string import Template
  13. from os.path import exists
  14.  
  15. def confirm_choice(msg):
  16.     confirm = input(str(msg) + "[Y/N]: ")
  17.     return confirm == 'Y' or confirm == 'y'
  18.  
  19. # +Something *Something -Something
  20. def gen_aspects(args):
  21.     aspects=""
  22.     if args:
  23.         asp_all=[]
  24.         asp_one=[]
  25.         asp_exc=[]
  26.         for a in args:
  27.             if a[0] == "+":
  28.                 asp_all.append("typeof(" + a[1:] + ")")
  29.             elif a[0] == "*":
  30.                 asp_one.append("typeof(" + a[1:] + ")")
  31.             elif a[0] == "-":
  32.                 asp_exc.append("typeof(" + a[1:] + ")")
  33.         if len(asp_all) > 0:
  34.             aspects+="\n\t\t.All("
  35.             for a in asp_all:
  36.                 aspects+=a + ", "
  37.             aspects=aspects[:-2]+")"
  38.         if len(asp_one) > 0:
  39.             aspects+="\n\t\t.One("
  40.             for a in asp_one:
  41.                 aspects+=a + ", "
  42.             aspects=aspects[:-2]+")"
  43.         if len(asp_exc) > 0:
  44.             aspects+="\n\t\t.Exclude("
  45.             for a in asp_exc:
  46.                 aspects+=a + ", "
  47.             aspects=aspects[:-2]+")"
  48.         if len(aspects) > 0:
  49.             aspects = "Aspect" + aspects
  50.     return aspects
  51.  
  52. def gen_mappers(args):
  53.     defs=""
  54.     inits=""
  55.     if args:
  56.         rel_comps = []
  57.         for a in args:
  58.             if a[0] == "+":
  59.                 rel_comps.append(a[1:])
  60.             elif a[0] == "*":
  61.                 rel_comps.append(a[1:])
  62.         if len(rel_comps) > 0:
  63.             for c in rel_comps:
  64.                 lc=c.lower()
  65.                 # Defs
  66.                 defs += "\tprivate ComponentMapper<{tname}> {mname};\n".format(
  67.                     tname=c, mname=lc)
  68.                 # Inits
  69.                 inits += "\t\t{mname} = mapper.GetMapper<{tname}>();\n".format(
  70.                     tname=c, mname=lc)
  71.     return defs, inits
  72.  
  73. def create_component(name, args):
  74.     temp = Template(
  75. """public class $name {
  76.    public $name() {
  77.    }
  78. }
  79. """)
  80.     out = temp.safe_substitute(name=name)
  81.     return out, "./Components/" + name + ".cs"
  82.  
  83. def create_usystem(name, args):
  84.     temp = Template(
  85. """using Microsoft.Xna.Framework;
  86. using MonoGame.Extended.Entities;
  87. using MonoGame.Extended.Entities.Systems;
  88.  
  89. public class $name : EntityUpdateSystem{
  90. $mapper_defs
  91.    public $name() : base($aspects) {
  92.  
  93.    }
  94.    public override void Initialize(IComponentMapperService mapper) {
  95. $mapper_init
  96.    }
  97.    public override void Update(GameTime gameTime) {
  98.        foreach (var entity in ActiveEntities) {
  99.  
  100.        }
  101.    }
  102. }
  103. """)
  104.     mdefs, minits = gen_mappers(args)
  105.     out = temp.safe_substitute(name=name, aspects=gen_aspects(args),
  106.         mapper_defs=mdefs, mapper_init=minits)
  107.     return out, "./Systems/" + name + ".cs"
  108.  
  109. def create_dsystem(name, args):
  110.     temp = Template(
  111. """using Microsoft.Xna.Framework;
  112. using MonoGame.Extended.Entities;
  113. using MonoGame.Extended.Entities.Systems;
  114.  
  115. public class $name : EntityDrawSystem{
  116. $mapper_defs
  117.    public $name() : base($aspects) {
  118.        
  119.    }
  120.    public override void Initialize(IComponentMapperService mapper) {
  121. $mapper_init
  122.    }
  123.    public override void Draw(GameTime gameTime) {
  124.        foreach (var entity in ActiveEntities) {
  125.            
  126.        }
  127.    }
  128. }
  129. """)
  130.     mdefs, minits = gen_mappers(args)
  131.     out = temp.safe_substitute(name=name, aspects=gen_aspects(args),
  132.         mapper_defs=mdefs, mapper_init=minits)
  133.     return out, "./Systems/" + name + ".cs"
  134.  
  135. def create_singlesystem(name, args):
  136.     pass
  137.  
  138. factories={
  139.     "component": create_component,
  140.     "usystem": create_usystem,
  141.     "dsystem": create_dsystem,
  142.     "singlesystem": create_singlesystem
  143. }
  144.  
  145. def exec_fail(reason = "Error"):
  146.     print(str(reason) + "\nFormat:\n" +
  147.         "  ecstool.py create {component|usystem|dsystem|singlesystem} Name {aspects}")
  148.     print("   Aspect alls: +CSpr +CPos")
  149.     print("          ones: *CSpr *CPos")
  150.     print("          excludes: -CSpr")
  151.     exit(1)
  152.  
  153. def run():
  154.     if len(sys.argv) < 4:
  155.         exec_fail("Incorrect Args")
  156.     if sys.argv[1] == "create":
  157.         print("CREATE")
  158.         what = sys.argv[2]
  159.         print("  " + what)
  160.         if what in factories:
  161.             name = sys.argv[3]
  162.             print("    " + name)
  163.             args=len(sys.argv)>4 and sys.argv[4:] or None
  164.             content, where = factories[what](name, args)
  165.             if exists(where):
  166.                 print("This " + what + " already exists.")
  167.                 choice = confirm_choice("Are you sure you want to overwrite it? ")
  168.                 if not choice:
  169.                     print("- Cancelled by user")
  170.                     exit(0)
  171.             f = open(where, "w")
  172.             f.write(content)
  173.             f.close()
  174.             print("Created: " + where)
  175.         else:
  176.             exec_fail("Unknown type")
  177.     else:
  178.         exec_fail("Unknown command")
  179.  
  180.  
  181. if __name__ == "__main__":
  182.     run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement