def Extract(self, fname): if self.libstorm is None: return None elif not self.mpqh: return None # Open file fileh = c_void_p() ret = self.libstorm.SFileOpenFileEx( self.mpqh, u2b(fname), 0, byref(fileh) ) if not ret: return None # Get file size & allocate buffer # Note : this version only supports 32bit mpq file fsize = self.libstorm.SFileGetFileSize(fileh, 0) fdata = create_string_buffer(fsize) # Read file pfread = c_int() self.libstorm.SFileReadFile(fileh, fdata, fsize, byref(pfread), 0) self.libstorm.SFileCloseFile(fileh) if pfread.value == fsize: return fdata.raw else: return None
def Extract(self, fname): if self.libstorm is None: return None elif not self.mpqh: return None # Open file fileh = c_void_p() ret = self.libstorm.SFileOpenFileEx(self.mpqh, u2b(fname), 0, byref(fileh)) if not ret: return None # Get file size & allocate buffer # Note : this version only supports 32bit mpq file fsize = self.libstorm.SFileGetFileSize(fileh, 0) fdata = create_string_buffer(fsize) # Read file pfread = c_int() self.libstorm.SFileReadFile(fileh, fdata, fsize, byref(pfread), 0) self.libstorm.SFileCloseFile(fileh) if pfread.value == fsize: return fdata.raw else: return None
def AddString(self, string): string = ut.u2b(string) # Starcraft uses multibyte encoding. if not isinstance(string, bytes): raise ut.EPError('Invalid type for string') stringindex = len(self._dataindextb) # If duplicate text exist -> just proxy it try: repr_stringid = self._stringmap[string] dataindex = self._dataindextb[repr_stringid] self._dataindextb.append(dataindex) self._capacity += 2 # just string offset # Else -> Create new entry except KeyError: dataindex = len(self._datatb) self._stringmap[string] = stringindex self._datatb.append(string) self._dataindextb.append(dataindex) # string + b'\0' + string offset self._capacity += len(string) + 1 + 2 ut.ep_assert(self._capacity < 65536, 'String table overflow') return stringindex
def AddString(self, string): string = ut.u2b(string) # Starcraft uses multibyte encoding. if not isinstance(string, bytes): raise ut.EPError('Invalid type for string') stringindex = len(self._dataindextb) # If duplicate text exist -> just proxy it try: repr_stringid = self._stringmap[string] dataindex = self._dataindextb[repr_stringid] self._dataindextb.append(dataindex) self._capacity += 2 # just string offset # Else -> Create new entry except KeyError: dataindex = len(self._datatb) self._stringmap[string] = stringindex self._datatb.append(string) self._dataindextb.append(dataindex) # string + b'\0' + string offset self._capacity += len(string) + 1 + 2 ut.ep_assert(self._capacity < 65536, 'String table overflow') return stringindex
def Extract(self, fname): if self.libstorm is None: return None elif not self.mpqh: return None # Open file fileh = ffi.new("HANDLE*") ret = self.libstorm.SFileOpenFileEx( self.mpqh, u2b(fname), 0, fileh ) if not ret: return None fileh = fileh[0] # Get file size & allocate buffer # Note : this version only supports 32bit mpq file fsize = self.libstorm.SFileGetFileSize(fileh, ffi.NULL) fdata = ffi.new("BYTE[]", fsize) # Read file pfread = ffi.new("DWORD*") self.libstorm.SFileReadFile(fileh, fdata, fsize, pfread, ffi.NULL) self.libstorm.SFileCloseFile(fileh) if pfread[0] == fsize: return ffi.buffer(fdata)[:] else: return None
def Extract(self, fname): if self.libstorm is None: return None elif not self.mpqh: return None # Open file fileh = ffi.new("HANDLE*") ret = self.libstorm.SFileOpenFileEx(self.mpqh, u2b(fname), 0, fileh) if not ret: return None fileh = fileh[0] # Get file size & allocate buffer # Note : this version only supports 32bit mpq file fsize = self.libstorm.SFileGetFileSize(fileh, ffi.NULL) fdata = ffi.new("BYTE[]", fsize) # Read file pfread = ffi.new("DWORD*") self.libstorm.SFileReadFile(fileh, fdata, fsize, pfread, ffi.NULL) self.libstorm.SFileCloseFile(fileh) if pfread[0] == fsize: return ffi.buffer(fdata)[:] else: return None
def epsCompile(filename, bCode): filename = u2b(filename) output = libeps.compileString(filename, bCode) if not output or libeps.getErrorCount(): return None outputStr = c_char_p(output).value libeps.freeCompiledResult(output) return outputStr
def epsCompile(filename, bCode): filename = u2b(filename) output = libeps.compileString(filename, bCode) if not output or libeps.getErrorCount(): return None outputStr = c_char_p(output).value libeps.freeCompiledResult(output) return outputStr
def PutFile(self, fname, buffer): if not self.mpqh: return None # Create temporary file f = NamedTemporaryFile(delete=False) f.write(buffer) tmpfname = f.name f.close() # Add to mpq ret = self.libstorm.SFileAddFile( self.mpqh, u2b(tmpfname), u2b(fname), MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY, ) os.unlink(tmpfname) return ret
def GetStringIndex(self, string): string = ut.u2b(string) if not isinstance(string, bytes): raise ut.EPError('Invalid type for string') try: return self._stringmap[string] + 1 except KeyError: return self.AddString(string) + 1
def GetStringIndex(self, string): string = ut.u2b(string) if not isinstance(string, bytes): raise ut.EPError('Invalid type for string') try: return self._stringmap[string] + 1 except KeyError: return self.AddString(string) + 1
def PutFile(self, fname, buffer): if not self.mpqh: return None # Create temporary file f = NamedTemporaryFile(delete=False) f.write(buffer) tmpfname = f.name f.close() # Add to mpq ret = self.libstorm.SFileAddFile( self.mpqh, u2b(tmpfname), u2b(fname), MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY, ) os.unlink(tmpfname) return ret
def f_dbstr_print(dst, *args): """Print multiple string / number to dst. :param dst: Destination address (Not EPD player) :param args: Things to print """ if ut.isUnproxyInstance(dst, DBString): dst = dst.GetStringMemoryAddr() args = ut.FlattenList(args) for arg in args: if ut.isUnproxyInstance(arg, bytes): dst = f_dbstr_addstr(dst, c.Db(arg + b'\0')) elif ut.isUnproxyInstance(arg, str): dst = f_dbstr_addstr(dst, c.Db(ut.u2b(arg) + b'\0')) elif ut.isUnproxyInstance(arg, DBString): dst = f_dbstr_addstr(dst, arg.GetStringMemoryAddr()) elif ut.isUnproxyInstance(arg, int): # int and c.EUDVariable should act the same if possible. # EUDVariable has a value of 32bit unsigned integer. # So we adjust arg to be in the same range. dst = f_dbstr_addstr(dst, c.Db( ut.u2b(str(arg & 0xFFFFFFFF)) + b'\0')) elif ut.isUnproxyInstance(arg, c.EUDVariable): dst = f_dbstr_adddw(dst, arg) elif c.IsConstExpr(arg): dst = f_dbstr_adddw(dst, arg) elif ut.isUnproxyInstance(arg, hptr): dst = f_dbstr_addptr(dst, arg._value) else: raise ut.EPError( 'Object wit unknown parameter type %s given to f_eudprint.' % type(arg) ) return dst
def __init__(self, content): """Constructor for DBString :param content: Initial DBString content / capacity. Capacity of DBString is determined by size of this. If content is integer, then initial capacity and content of DBString will be set to content(int) and empty string. :type content: str, bytes, int """ super().__init__() if isinstance(content, int): self.content = bytes(content) else: self.content = ut.u2b(content)
def __init__(self, content): """Constructor for DBString :param content: Initial DBString content / capacity. Capacity of DBString is determined by size of this. If content is integer, then initial capacity and content of DBString will be set to content(int) and empty string. :type content: str, bytes, int """ super().__init__() if isinstance(content, int): self.content = bytes(content) else: self.content = ut.u2b(content)
def EncodeAIScript(ais, issueError=False): ais = ut.unProxy(ais) if type(ais) is str: ais = ut.u2b(ais) if type(ais) is bytes: ut.ep_assert(len(ais) >= 4, 'AIScript name too short') if len(ais) > 4: return ut.b2i4(DefAIScriptDict[ais]) elif len(ais) == 4: return ut.b2i4(ais) else: return ais
def EncodeAIScript(ais, issueError=False): ais = ut.unProxy(ais) if type(ais) is str: ais = ut.u2b(ais) if type(ais) is bytes: ut.ep_assert(len(ais) >= 4, 'AIScript name too short') if len(ais) > 4: return ut.b2i4(DefAIScriptDict[ais]) elif len(ais) == 4: return ut.b2i4(ais) else: return ais
def setEpsGlobals(globalList): globalList_C = b'\0'.join(u2b(g) for g in globalList) + b'\0' libeps.registerPlibConstants(globalList_C)
def setEpsGlobals(globalList): globalList_C = b'\0'.join(u2b(g) for g in globalList) + b'\0' libeps.registerPlibConstants(globalList_C)