/PyPRP/prp_PyPack.py

https://github.com/H-uru/PyPRP
Python | 124 lines | 53 code | 19 blank | 52 comment | 3 complexity | 266b91769d2b955aeead40457224a26c MD5 | raw file
  1. # Copyright (C) 2008 Guild of Writers PyPRP Project Team
  2. # See the file AUTHORS for more info about the team
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. #
  18. # Please see the file LICENSE for the full license.
  19. import struct, time, glob, os
  20. import os.path
  21. from prp_Types import *
  22. class alcpyfile:
  23. def __init__(self):
  24. self.name=""
  25. self.data=None
  26. def setName(self,name):
  27. self.name=name + "c"
  28. def read(self,buf):
  29. size, = struct.unpack("<I",buf.read(4))
  30. self.data=cStringIO.StringIO()
  31. #print size
  32. self.data.write(buf.read(size))
  33. def write(self,buf):
  34. self.data.read()
  35. buf.write(struct.pack("<I",self.data.tell()))
  36. self.data.seek(0)
  37. buf.write(self.data.read())
  38. class alcpypack:
  39. def __init__(self,version=5):
  40. self.list=[]
  41. self.version=version
  42. def read(self,buf):
  43. num, = struct.unpack("<I",buf.read(4))
  44. for i in range(num):
  45. name = Ustr()
  46. name.setType(self.version)
  47. name.read(buf)
  48. off, = struct.unpack("<I",buf.read(4))
  49. cat = buf.tell()
  50. buf.seek(off)
  51. file = alcpyfile()
  52. file.setName(str(name))
  53. print "Reading %s" %str(name)
  54. file.read(buf)
  55. buf.seek(cat)
  56. self.list.append(file)
  57. def write(self,buf):
  58. buf.write(struct.pack("<I",len(self.list)))
  59. size=4
  60. for i in self.list:
  61. size=size + 4 + len(i.name) + 2
  62. for i in self.list:
  63. name = Ustr()
  64. name.setType(self.version)
  65. name.set(i.name)
  66. name.write(buf)
  67. buf.write(struct.pack("<I",size))
  68. me = buf.tell()
  69. buf.seek(size)
  70. i.write(buf)
  71. size = buf.tell()
  72. buf.seek(me)
  73. ##f = file("python.pak.dec","rb")
  74. ##
  75. ##pak = alcpypack()
  76. ##pak.read(f)
  77. ##f.close()
  78. ##
  79. ##for i in pak.list:
  80. ## print "writting %s" %i.name
  81. ## f = file("out/%s" %i.name,"wb")
  82. ## f.write(struct.pack("I",0x0A0DF23B))
  83. ## f.write(struct.pack("I",time.time()))
  84. ## i.data.seek(0)
  85. ## f.write(i.data.read())
  86. ## f.close()
  87. ####out = file("out.pak","wb")
  88. ####
  89. ####pkg = alcpypack()
  90. ####
  91. ####for i in glob.glob("newpak/*.pyc"):
  92. #### print "packing %s" %i
  93. #### f = file(i,"rb")
  94. #### f.read(8)
  95. #### buf = cStringIO.StringIO()
  96. #### buf.write(f.read())
  97. #### f.close()
  98. #### pyf = alcpyfile()
  99. #### pyf.data=buf
  100. #### pyf.name=os.path.basename((i[:len(i)-1]))
  101. #### #print pyf.name
  102. #### pkg.list.append(pyf)
  103. ####
  104. ####pkg.write(out)
  105. ####out.close()