astralax_compiler.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import tempfile
  2. import os
  3. import subprocess
  4. import platform
  5. import struct
  6. import time
  7. from tools import pylz4
  8. from ctypes import *
  9. ERROR_OK=0
  10. ERROR_INVALID_IN_PATH=1
  11. ERROR_ASTRALAX_NOT_FOUND=3
  12. ERROR_ASTRALAX_FAILED=4
  13. def process(in_path,out_path,csa_path,convert,astralax_path, lib_path):
  14. in_path=os.path.abspath(in_path)
  15. csa_path=os.path.abspath(csa_path)
  16. if out_path is None:
  17. convert=False
  18. out_path=tempfile.mktemp(suffix=".ptc", dir=tempfile.gettempdir())
  19. out_path=os.path.abspath(out_path)
  20. cmd_line=[]
  21. cmd_line.extend([astralax_path])
  22. cmd_line.extend(['/c'])
  23. cmd_line.extend([in_path])
  24. cmd_line.extend([out_path])
  25. cmd_line.extend([csa_path])
  26. try:
  27. startupinfo = None
  28. if platform.system() == 'Windows':
  29. startupinfo = subprocess.STARTUPINFO()
  30. startupinfo.wShowWindow = subprocess.SW_HIDE
  31. p = subprocess.Popen(cmd_line, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
  32. out, err = p.communicate()
  33. exitcode = p.returncode
  34. if exitcode != 0:
  35. return ERROR_ASTRALAX_NOT_FOUND,None
  36. except Exception as ex:
  37. print(ex)
  38. return ERROR_ASTRALAX_NOT_FOUND,None
  39. MagicLibrary=cdll.LoadLibrary(lib_path)
  40. with open(out_path,'rb') as f:
  41. data=f.read()
  42. mf=MagicLibrary.Magic_OpenFileInMemory(data)
  43. if convert is True:
  44. compressed=pylz4.pylz4_compressHC(data, 12)
  45. class Header(Structure):
  46. _fields_=[
  47. ('number',c_int),
  48. ('version',c_int),
  49. ('crc32',c_int),
  50. ('uncompressed_size',c_int),
  51. ('compressed_size',c_int),
  52. ]
  53. header=Header()
  54. header.number=struct.unpack('<i', b'PTZ2')[0]
  55. header.version=2
  56. header.crc32=0
  57. header.uncompressed_size=len(data)
  58. header.compressed_size=len(compressed)
  59. converted_path=out_path.rsplit('.',1)[0]+'.ptz'
  60. with open(converted_path,'wb') as f:
  61. f.write(header)
  62. f.write(compressed)
  63. atlas_count=MagicLibrary.Magic_GetStaticAtlasCount(mf)
  64. class StaticAtlas(Structure):
  65. _fields_=[
  66. ('file',c_char_p),
  67. ('path',c_char_p),
  68. ('width',c_int),
  69. ('height',c_int),
  70. ('ptc_id',c_int),
  71. ]
  72. PStaticAtlas=POINTER(StaticAtlas)
  73. atlas=StaticAtlas()
  74. s=[]
  75. for n in range(0,atlas_count):
  76. MagicLibrary.Magic_GetStaticAtlas(mf,n,PStaticAtlas(atlas))
  77. s.append((atlas.file, 0, 0, atlas.width, atlas.height))
  78. MagicLibrary.Magic_CloseFile(mf)
  79. return ERROR_OK, s