Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
custom.py
Go to the documentation of this file.
1 import VS
2 import traceback
3 import sys
4 
5 procedures = {
6  }
7 
8 def add(name, proc):
9  procedures[name.lower()] = proc
10 
11 import weapons_lib #adds to procedures list
12 import guilds #adds to procedures list
13 import campaign_lib
14 import dialog_box
15 import computer
16 
17 running_cmds = {}
18 
19 maxid = 2
20 def generateID():
21  global maxid;
22  maxid=maxid+1
23  return str(maxid)
24 
25 def splitArgs(argstr):
26  ret=[]
27  while argstr:
28  arg = ''
29  qadd = ''
30  empty = True
31  while argstr and argstr[0]=='"':
32  end=argstr.find('"', 1)
33  if end!=-1:
34  arg += qadd + argstr[1:end]
35  qadd='"'
36  argstr = argstr[end+1:]
37  empty = False
38  else:
39  arg = argstr[1:]
40  empty = False
41  argstr = ''
42  space = argstr.find(' ')
43  if space != -1:
44  arg += argstr[:space]
45  argstr = argstr[space+1:]
46  else:
47  arg += argstr
48  argstr = ''
49  if arg or not empty:
50  ret.append(arg)
51  return ret
52 
53 def joinArgs(arglist):
54  ret = ''
55  for arg in arglist:
56  if ret:
57  ret += ' '
58  arg = str(arg).replace('\'','') # Remove all single-quotes
59  space = arg.find(' ')
60  quote = arg.find('"')
61  newstr = arg.replace('"','""')
62  if not newstr or space!=-1 or quote!=-1:
63  ret += '"' + newstr + '"'
64  else:
65  ret += newstr
66  return ret
67 
68 def putFunction(continuation, id, cp):
69  global running_cmds;
70  if not id:
71  id = generateID()
72  key = str(cp)+","+id
73  running_cmds[key] = continuation
74  return id
75 
76 def getFunction(id, cp):
77  key = str(cp)+","+id
78  if running_cmds.has_key(key):
79  func = running_cmds[key]
80  del running_cmds[key]
81  return func
82  return None
83 
84 # custom.run should be the last thing that happens in a function/
85 # it might either be synchronous or asynchronous (this could be considered a bug)
86 
87 def run(cmd, args, continuation, id=None, cp=-1):
88  if -1==cp:
89  cp = VS.getCurrentPlayer()
90  if continuation:
91  id = putFunction(continuation, id, cp)
92  if not isinstance(id,str):
93  id = "null"
94  print "running: "+cmd+", "+str(args)+"; id: "+id
95  VS.sendCustom(cp, cmd, joinArgs(args), id)
96  return id
97 
98 def respond(args, continuation, id, cp=-1):
99  run("response", args, continuation, id, cp)
100 
102  def __init__(self,cpnum):
103  self.line=''
104  if cpnum<0:
105  self.cpstr='all'
106  else:
107  self.cpstr = 'p'+str(cpnum)
108  def write(self, text):
109  lines = text.split('\n')
110  self.line=lines[-1]
111  lines = lines[:-1]
112  for l in lines:
113  VS.IOmessage(0,"game",self.cpstr,l)
114 
115 
116 def processMessage(local, cmd, argstr, id):
117  cp = VS.getCurrentPlayer();
118  cmd = cmd.lower()
119  print "======= Processing message "+str(id)+" ======="
120  try:
121  args = splitArgs(argstr)
122  print "Command: "+cmd
123  for arg in args:
124  print arg
125  if cmd=='reloadlib' and local and len(args)>=1:
126  reload(__import__(args[0]))
127  VS.IOmessage(0, "game", "p"+str(cp), "Reloaded "+str(args[0]))
128  elif cmd=='local':
129  # simple way of bouncing back message to client....
130  if id:
131  def localresponse(args):
132  respond(args, None, id, cp)
133  else:
134  localresponse = None
135  run(args[0], args[1:], localresponse, id, cp)
136  elif (cmd=='response'):
137  func = getFunction(id, cp)
138  if func:
139  ret = func(args)
140  if ret and isinstance(ret, tuple) and len(ret)==2:
141  respond(ret[0], ret[1], id, cp)
142  elif ret==True:
143  putFunction(func, id, cp)
144  elif ret:
145  respond(ret, None, id, cp)
146  elif procedures.has_key(cmd):
147  ret = procedures[cmd](local, cmd, args, id)
148  if ret and isinstance(ret, tuple) and len(ret)==2:
149  respond(ret[0], ret[1], id, cp)
150  elif ret:
151  respond(ret, None, id, cp)
152  elif VS.isserver():
153  import server
154  server.processMessage(cp, local, cmd, args, id)
155  else:
156  print "Command "+repr(cmd)+" does not exist. Available functions:"
157  print procedures.keys()
158  except:
159  if id or cp<0:
160  writer = sys.stderr
161  else:
162  writer = IOmessageWriter(cp)
163  writer.write("An error occurred when processing custom command: \n"
164  + str(cmd)+" "+argstr + "\n")
165  traceback.print_exc(file=writer)
166  print "-------------------------- " +str(id)+" -------"
167