home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 56 / CDPowerplay56Disc2.iso / demos / blade / data1.cab / Program_Executable_Files / Scripts / PickInit.py < prev    next >
Encoding:
Text File  |  2000-10-27  |  5.8 KB  |  290 lines

  1.  
  2.  
  3.  
  4. import Bladex
  5. import copy_reg
  6. import types
  7.  
  8.  
  9. GlobalModulesCache=None
  10. GlobalFunctionsCache=None
  11. GlobalCFunctionsCache=None
  12.  
  13. def GetGlobalsAux():
  14.     import sys
  15.     try:
  16.         1 + ''
  17.     except:
  18.         frame = sys.exc_info()[2].tb_frame.f_back
  19.  
  20.     while frame:
  21.         globs=frame.f_globals
  22.         frame=frame.f_back
  23.  
  24.     return globs
  25.  
  26.  
  27. def GetGlobalsAux2(req_type):
  28. ##    global GlobalModulesCache
  29. ##    global GlobalFunctionsCache
  30. ##    global GlobalCFunctionsCache
  31. ##
  32. ##    if req_type==types.ModuleType and GlobalModulesCache:
  33. ##        return GlobalModulesCache
  34. ##    elif (req_type==types.FunctionType or req_type==types.MethodType) and GlobalFunctionsCache:
  35. ##        return GlobalFunctionsCache
  36. ##    elif req_type==types.BuiltinFunctionType and GlobalCFunctionsCache:
  37. ##        return GlobalCFunctionsCache
  38.  
  39.     g=GetGlobalsAux()
  40.     elems=[]
  41.     for i in g.items():
  42.         if type(i[1])==req_type:
  43.             elems.append(i)
  44.  
  45. ##    if req_type==types.ModuleType:
  46. ##        GlobalModulesCache=elems
  47. ##    elif req_type==types.FunctionType or req_type==types.MethodType:
  48. ##        GlobalFunctionsCache=elems
  49. ##    elif req_type==types.BuiltinFunctionType:
  50. ##        GlobalCFunctionsCache=elems
  51.     return elems
  52.  
  53.  
  54. def ConstSound(sound_name,sound_file,volume,base_volume,min_distance,max_distance,scale,send_notify):
  55.   s=Bladex.CreateSound(sound_file,sound_name)
  56. ##  print s
  57.   if not s:
  58.     return
  59.   s.Volume=volume
  60.   s.BaseVolume=base_volume
  61.   s.MinDistance=min_distance
  62.   s.MaxDistance=max_distance
  63.   s.Scale=scale
  64.   s.SendNotify=send_notify
  65.   return s
  66.  
  67.  
  68. def RedSound(s):
  69.   return ConstSound,(s.Name,"",s.Volume,s.BaseVolume,s.MinDistance,s.MaxDistance,s.Scale,s.SendNotify)
  70.  
  71.  
  72. def RegisterPickSound():
  73.   #Creo uno cualquiera.  ┐Revisar?
  74.   gmadlig=Bladex.CreateSound('../../sounds/golpe-madera-mediana.wav', 'GolpeMaderaMediana')
  75.  
  76.   copy_reg.pickle(type(gmadlig),RedSound,ConstSound)
  77.  
  78.  
  79.  
  80.  
  81.  
  82. def ConstEntity(ent_name):
  83.   e=Bladex.GetEntity(ent_name)
  84.   return e
  85.  
  86.  
  87. def RedEntity(e):
  88.  
  89.   if e:
  90.     try:
  91.       return ConstEntity,(e.Name,)
  92.     except:
  93.       print "PickInit.RedEntity() can not get entity name"
  94.       return ConstEntity,("Invalid Entity",)
  95.  
  96.   return ConstEntity,("Invalid Entity",)
  97.  
  98.  
  99. def RegisterPickEntity():
  100.   #Creo uno cualquiera.  ┐Revisar?
  101.   #gmadlig=Bladex.CreateEntity('PickEntity','Entity Spot',0,0,0)
  102.   gmadlig=Bladex.GetEntity(0)
  103.  
  104.   copy_reg.pickle(type(gmadlig),RedEntity,ConstEntity)
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. def FindFunctionAux(module,fun_name):
  116.  
  117.   if module.__dict__.has_key(fun_name):
  118.     return module.__dict__[fun_name]
  119.   return None
  120.  
  121.  
  122.  
  123. def ConstFunction(fun_name,lib_name):
  124.  
  125.   funcs=GetGlobalsAux2(types.FunctionType)
  126.   for i in funcs:
  127.     if i[1].func_name==fun_name:
  128.       return i[1]
  129.  
  130.   # La busco en los modulos
  131.   global_mods=GetGlobalsAux2(types.ModuleType)
  132.   # Primero miro si hay concordancia con lib_name
  133.   for i in global_mods:
  134.       if i[0]==lib_name and i[1].__dict__.has_key(fun_name):
  135.           return i[1].__dict__[fun_name]
  136.  
  137.   for i in global_mods:
  138.     func=FindFunctionAux(i[1],fun_name)
  139.     if func:
  140.         return func
  141.  
  142.   print "Warning, can't find global function",fun_name,lib_name
  143.   return None
  144.  
  145.  
  146.  
  147. def RedFunction(f):
  148.   import GameStateAux
  149.   s=GameStateAux.GetFunctionFile(f)
  150.  
  151.   return ConstFunction,(f.func_name,s)
  152.  
  153.  
  154. def RegisterPickFunction():
  155.   import types
  156.  
  157.   copy_reg.pickle(types.FunctionType,RedFunction,ConstFunction)
  158.  
  159.  
  160.  
  161.  
  162.  
  163. def ConstMethod(obj_id,method_name):
  164.   import types
  165.   import ObjStore
  166.  
  167.   try:
  168.     obj=ObjStore.ObjectsStore[obj_id]
  169.     assign_func=eval("obj."+method_name)
  170.     return assign_func
  171.   except Exception,exc:
  172.     print "PickInit.ConstMethod() can not find method",obj_id,method_name
  173.     print "Exception",exc
  174.     return None
  175.  
  176. def RedMethod(f):
  177.   try:
  178.     return ConstMethod,(f.im_class.persistent_id(f.im_self),f.im_func.func_name)
  179.   except:
  180.     print "PickInit.RedMethod() can not register method",f
  181.     return ConstMethod,(None,None)
  182.  
  183.  
  184. def RegisterPickMethod():
  185.   import types
  186.  
  187.   copy_reg.pickle(types.MethodType,RedMethod,ConstMethod)
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. def ConstCFunction(fun_name):
  195.  
  196.   print "ConstCFunction",fun_name
  197.   # La busco en funciones C
  198.   funcs=GetGlobalsAux2(types.BuiltinFunctionType)
  199.   for i in funcs:
  200.     if i[1].__name__==fun_name:
  201.       return i[1]
  202.   # La busco en los modulos
  203.   # Primero en los de Blade
  204.   import Bladex
  205.   import Traps_C
  206.   import B3DLib
  207.   mods=(Bladex,B3DLib,Traps_C)
  208.   for i in mods:
  209.     func=FindFunctionAux(i,fun_name)
  210.     if func:
  211.         return func
  212.   # Y luego en los otros
  213.   global_mods=GetGlobalsAux2(types.ModuleType)
  214.   for i in global_mods:
  215.     if i not in mods:
  216.         func=FindFunctionAux(i[1],fun_name)
  217.         if func:
  218.             return func
  219.  
  220.   print "Warning, can't find global function",fun_name
  221.   return None
  222.  
  223.  
  224.  
  225. def RedCFunction(f):
  226.   return ConstCFunction,(f.__name__,)
  227.  
  228.  
  229. def RegisterPickCFunction():
  230.   import types
  231.  
  232.   copy_reg.pickle(types.BuiltinFunctionType,RedCFunction,ConstCFunction)
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. def ConstSector(sec_idx):
  251.   e=Bladex.GetSector(sec_idx)
  252.   return e
  253.  
  254. def RedSector(s):
  255.   return ConstSector,(s.Index,)
  256.  
  257.  
  258. def RegisterPickSector():
  259.   gmadlig=Bladex.GetSector(0)
  260.   if not gmadlig:
  261.       print "ERROR in RegisterPickSector()"
  262.       return
  263.  
  264.   copy_reg.pickle(type(gmadlig),RedSector,ConstSector)
  265.  
  266.  
  267.  
  268.  
  269. def ClearCaches():
  270.   global GlobalModulesCache
  271.   global GlobalFunctionsCache
  272.   global GlobalCFunctionsCache
  273.  
  274.   GlobalModulesCache=None
  275.   GlobalFunctionsCache=None
  276.   GlobalCFunctionsCache=None
  277.  
  278.  
  279.  
  280.  
  281. def Init():
  282.   ClearCaches()
  283.   RegisterPickSound()
  284.   RegisterPickEntity()
  285.   RegisterPickFunction()
  286.   RegisterPickSector()
  287.   RegisterPickMethod()
  288.   RegisterPickCFunction()
  289.   print "Executed PickInit.Init()"
  290.