1
2from __future__ import print_function # so that print(blah) will work in console
3
4from ie_stats import *
5from GUIDefines import *
6import GemRB
7from GemRB import * # we dont want to have to type 'GemRB.Command' instead of simply 'Command'
8
9def OnLoad():
10	consoleWin = GemRB.LoadWindow(0, "console", WINDOW_TOP|WINDOW_HCENTER)
11	consoleWin.SetFlags(IE_GUI_VIEW_INVISIBLE | WF_BORDERLESS | WF_ALPHA_CHANNEL, OP_OR)
12	consoleWin.SetFlags(WF_DESTROY_ON_CLOSE, OP_NAND);
13	consoleWin.AddAlias("WIN_CON")
14	consoleWin.SetBackground({'r' : 0, 'g' : 0, 'b' : 0, 'a' : 200})
15
16	histLabel = consoleWin.GetControl(2)
17	histLabel.SetText ("History:")
18	hist = consoleWin.GetControl(3)
19	hist.SetColor (ColorWhitish, TA_COLOR_OPTIONS)
20
21	console = consoleWin.GetControl(0)
22	console = consoleWin.ReplaceSubview (0, IE_GUI_CONSOLE, hist)
23
24	consoleWin.SetAction(lambda: console.Focus(), ACTION_WINDOW_FOCUS_GAINED)
25
26	consoleOut = consoleWin.GetControl(1)
27	consoleOut.SetFlags (IE_GUI_TEXTAREA_AUTOSCROLL)
28	consoleOut.AddAlias("CONSOLE", 1);
29	consoleOut.SetBackground({'r' : 0, 'g' : 0, 'b' : 0, 'a' : 200})
30
31def ToggleConsole():
32	consoleWin = GemRB.GetView("WIN_CON")
33	if consoleWin is None: # if outside of a game
34		return
35
36	if consoleWin.IsVisible():
37		consoleWin.Close()
38	else:
39		consoleWin.Focus()
40
41# /handy/ shorthand forms
42def gps(stat, base=0):
43	print (GemRB.GetPlayerStat(GemRB.GameGetFirstSelectedPC(), stat, base))
44
45def sps(stat, value, pcf=1):
46	GemRB.SetPlayerStat(GemRB.GameGetFirstSelectedPC(), stat, value, pcf)
47
48def mta(area):
49	GemRB.MoveToArea(area)
50
51def debug(level):
52	GemRB.ConsoleWindowLog (level)
53
54def cast(spellRes):
55	GemRB.SpellCast (GemRB.GameGetFirstSelectedPC (), -3, 0, spellRes)
56
57def cc(cre, px=-1, py=-1):
58	GemRB.CreateCreature(GemRB.GameGetFirstSelectedPC(), cre, px, py)
59
60def ci(item, slot=-1, c0=1, c1=0, c2=0):
61	GemRB.CreateItem(GemRB.GameGetFirstSelectedPC(), item, slot, c0, c1, c2)
62
63def cv(var, context="GLOBAL"):
64	GemRB.CheckVar(var, context)
65
66def ex(cmd):
67	GemRB.ExecuteString(cmd)
68
69def ev(trigger):
70	GemRB.EvaluateString(trigger)
71
72# the actual function that the GemRB::Console calls
73def Exec(cmd):
74	import sys
75
76	con = GemRB.GetView("CONSOLE", 1)
77
78	class OutputCapture(object):
79		def __init__(self, out):
80			self.out = out
81			self.buffer = ""
82
83		def write(self, message):
84			self.out.write(message)
85			self.buffer += str(message)
86			if self.buffer.endswith("\n"):
87				out = self.buffer
88				if out:
89					con.Append("[color=ffffff]" + cmd + ": [/color][color=00ff00]" + out + "[/color]\n")
90				self.buffer = ""
91
92	try:
93		stdout = sys.stdout
94
95		if con:
96			sys.stdout = OutputCapture(stdout)
97
98		return eval(cmd)
99	except (SyntaxError, NameError, TypeError, ZeroDivisionError) as error:
100		if con:
101			con.Append("[color=ffffff]" + cmd + ": [/color][color=ff0000]" + str(error) + "[/color]\n")
102
103		sys.stderr.write(error)
104	finally:
105		sys.stdout = stdout
106