From 18b8ee55541d271bf7287201c22c9096c7763051 Mon Sep 17 00:00:00 2001 From: justuswolff Date: Sat, 8 Jun 2024 17:12:26 +0200 Subject: [PATCH] plugin system update --- main.py | 42 ++++++++++++++-------- testbuilds/plugin/out/game.HEGF | 2 +- testbuilds/plugin/out/main.py | 3 +- testbuilds/plugin/out/player.py | 23 ++++++++---- testbuilds/plugin/out/testplugins/base.plg | 4 +++ testplugins/base.plg | 4 +++ 6 files changed, 56 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index 799f09e..cb922fc 100644 --- a/main.py +++ b/main.py @@ -73,8 +73,8 @@ global cooldown global version global plugindir version = "HE2.2-Hashengine V2.2" -#plugindir = "plugins" -plugindir = "testplugins" +plugindir = "plugins" +#plugindir = "testplugins" cooldown = False gamedata = {} @@ -689,12 +689,13 @@ def testing(): global running global clog global APIPLUG + global RUNPLUG try: if running == True: return except: pass running = True - testproc = multiprocessing.Process(target=execgame, args=(prepspecified(gamedata), clog.get(), APIPLUG)) + testproc = multiprocessing.Process(target=execgame, args=(prepspecified(gamedata), clog.get(), APIPLUG, RUNPLUG)) testproc.start() def APIGEN(): @@ -778,6 +779,10 @@ def run(): #gameloopsc = script() #gameloopsc.code = gamescript #maingame.startscript(lambda: gameloopsc.execute(APIGEN(), log)) + log("executing plugins") + for i in RUNPLUG: + exec(i, globals()) + log("done") log("game test started!!!") log("---------------------") for i in scripts: @@ -831,10 +836,12 @@ import ast file = open("game.HEGF", 'r') file = file.read() file = ast.literal_eval(file) -player.execgame(file, fAPIPLUG=player.loadplugins(False)) +temp = fAPIPLUG=player.loadplugins(False) +player.execgame(file, fAPIPLUG=temp[0], fRUNPLUG=temp[1]) """) print("done.") print("building finished!") + messagebox.showinfo(LH.string("done"), LH.string("suc")) def importPS(): target = filedialog.askopenfile() @@ -948,15 +955,18 @@ def ungridobjtrees(): def loadplugins(GUIe=True): global APIPLUG for pluginname in os.listdir(plugindir): - file = open(plugindir+"/"+pluginname, 'r') - plugindata = file.read() - plugindata = ast.literal_eval(plugindata) - file.close() - if "init" in plugindata and GUIe == True: - exec(plugindata["init"], globals()) - if "API" in plugindata: - APIPLUG.append(plugindata["API"]) - return APIPLUG + if pluginname.split(".")[1] == "plg": + file = open(plugindir+"/"+pluginname, 'r') + plugindata = file.read() + plugindata = ast.literal_eval(plugindata) + file.close() + if "init" in plugindata and GUIe == True: + exec(plugindata["init"], globals()) + if "API" in plugindata: + APIPLUG.append(plugindata["API"]) + if "run" in plugindata: + RUNPLUG.append(plugindata["run"]) + return [APIPLUG, RUNPLUG] def GUIinit(): global container @@ -1170,13 +1180,15 @@ def aposy(old): return old return tempvar -def execgame(gametree, shouldlog=True, fAPIPLUG=[]): +def execgame(gametree, shouldlog=True, fAPIPLUG=[], fRUNPLUG=[]): global GUIe global preview global clog global models global APIPLUG + global RUNPLUG APIPLUG = fAPIPLUG + RUNPLUG = fRUNPLUG preview = hashengine.game(renderer=nullrend, sounddir="/") GUIe = False models = [] @@ -1214,6 +1226,8 @@ global DCTE global gamexsize global gameysize global APIPLUG +global RUNPLUG +RUNPLUG = [] APIPLUG = [] gamexsize = 10 gameysize = 10 diff --git a/testbuilds/plugin/out/game.HEGF b/testbuilds/plugin/out/game.HEGF index 5751f16..3e2a5b5 100644 --- a/testbuilds/plugin/out/game.HEGF +++ b/testbuilds/plugin/out/game.HEGF @@ -1 +1 @@ -[[{'id': 'script', 'name': 'Skript', 'SID': 'pESBBUtxhoOcWqInhNTdqqqxonEoVbvhJQjHsuSCvsKorTpsPXvWbfElhVTeFmwSZPkFdgBQIoeHNfiBmqXjnRzeIIyokRTJtXXeSyIkxyWqYDOnwNWnVFtWXWoLPJkfcxLFKNJvdClNbgEcZNWJOqgPbTfunPEFeXzUyFYZadEeCIndNkYhWNjaZkIhhEKvdgVCjrLjmpbosAvOeSlhxEyTfJnrBgJgMSMeJOSPQRuMQMwZgjyCSldZtPJuiXA', 'args': {'code': 'from tkinter import messagebox\nmessagebox.showinfo(plugin, plugin)'}}], {}, 10, 10] \ No newline at end of file +[[], {}, 10, 10] \ No newline at end of file diff --git a/testbuilds/plugin/out/main.py b/testbuilds/plugin/out/main.py index e1a384e..0b3f01c 100644 --- a/testbuilds/plugin/out/main.py +++ b/testbuilds/plugin/out/main.py @@ -4,4 +4,5 @@ import ast file = open("game.HEGF", 'r') file = file.read() file = ast.literal_eval(file) -player.execgame(file, fAPIPLUG=player.loadplugins(False)) +temp = fAPIPLUG=player.loadplugins(False) +player.execgame(file, fAPIPLUG=temp[0], fRUNPLUG=temp[1]) diff --git a/testbuilds/plugin/out/player.py b/testbuilds/plugin/out/player.py index 35e02e3..a8657c6 100644 --- a/testbuilds/plugin/out/player.py +++ b/testbuilds/plugin/out/player.py @@ -689,12 +689,13 @@ def testing(): global running global clog global APIPLUG + global RUNPLUG try: if running == True: return except: pass running = True - testproc = multiprocessing.Process(target=execgame, args=(prepspecified(gamedata), clog.get(), APIPLUG)) + testproc = multiprocessing.Process(target=execgame, args=(prepspecified(gamedata), clog.get(), APIPLUG, RUNPLUG)) testproc.start() def APIGEN(): @@ -705,7 +706,6 @@ def APIGEN(): for i in APIPLUG: exec(i, globals()) temp = globals()["PLUGINAPIFUNC"]() - print(temp) API.update(temp) return API @@ -779,6 +779,10 @@ def run(): #gameloopsc = script() #gameloopsc.code = gamescript #maingame.startscript(lambda: gameloopsc.execute(APIGEN(), log)) + log("executing plugins") + for i in RUNPLUG: + exec(i, globals()) + log("done") log("game test started!!!") log("---------------------") for i in scripts: @@ -832,7 +836,8 @@ import ast file = open("game.HEGF", 'r') file = file.read() file = ast.literal_eval(file) -player.execgame(file, fAPIPLUG=player.loadplugins(False)) +temp = fAPIPLUG=player.loadplugins(False) +player.execgame(file, fAPIPLUG=temp[0], fRUNPLUG=temp[1]) """) print("done.") print("building finished!") @@ -957,7 +962,9 @@ def loadplugins(GUIe=True): exec(plugindata["init"], globals()) if "API" in plugindata: APIPLUG.append(plugindata["API"]) - return APIPLUG + if "run" in plugindata: + RUNPLUG.append(plugindata["run"]) + return [APIPLUG, RUNPLUG] def GUIinit(): global container @@ -1062,7 +1069,7 @@ def GUIinit(): menu.add_cascade(label=LH.string("langs"), menu=langmenu) for i in LH.getlangs(): langmenu.add_command(label=i, command=lambda i=i: selectlang(i)) - + loadplugins() container.mainloop() # attribute changers @@ -1171,13 +1178,15 @@ def aposy(old): return old return tempvar -def execgame(gametree, shouldlog=True, fAPIPLUG=[]): +def execgame(gametree, shouldlog=True, fAPIPLUG=[], fRUNPLUG=[]): global GUIe global preview global clog global models global APIPLUG + global RUNPLUG APIPLUG = fAPIPLUG + RUNPLUG = fRUNPLUG preview = hashengine.game(renderer=nullrend, sounddir="/") GUIe = False models = [] @@ -1215,6 +1224,8 @@ global DCTE global gamexsize global gameysize global APIPLUG +global RUNPLUG +RUNPLUG = [] APIPLUG = [] gamexsize = 10 gameysize = 10 diff --git a/testbuilds/plugin/out/testplugins/base.plg b/testbuilds/plugin/out/testplugins/base.plg index b834012..f7a4258 100644 --- a/testbuilds/plugin/out/testplugins/base.plg +++ b/testbuilds/plugin/out/testplugins/base.plg @@ -13,4 +13,8 @@ addmenu = tk.Menu(menu) menu.add_cascade(label="exampleplugin", menu=addmenu) addmenu.add_command(label="NULL", command=print()) """, +"run": """ +from tkinter import messagebox +messagebox.showinfo("PLUGIN TEST", "PLUGIN TEST") +""", } \ No newline at end of file diff --git a/testplugins/base.plg b/testplugins/base.plg index b834012..f7a4258 100644 --- a/testplugins/base.plg +++ b/testplugins/base.plg @@ -13,4 +13,8 @@ addmenu = tk.Menu(menu) menu.add_cascade(label="exampleplugin", menu=addmenu) addmenu.add_command(label="NULL", command=print()) """, +"run": """ +from tkinter import messagebox +messagebox.showinfo("PLUGIN TEST", "PLUGIN TEST") +""", } \ No newline at end of file