All pastes #1265359 Raw Edit

python unitsync wrapper

public python v1 · immutable
#1265359 ·published 2008-11-23 21:39 UTC
rendered paste body
import osfrom ctypes import c_char, c_char_p, c_int, c_uint, c_float, Structure, create_string_buffer, castimport ctypes# todo - set argtypes - incoming and outgoing# todo - make script to convert unitsync.h into this# todo - passing buffersclass StartPos(Structure):	_fields_ = [('x', c_int), ('y', c_int)]	def __str__(self):		return '(%i, %i)' % (self.x, self.y)class MapInfo(Structure):    _fields_ = [('description', c_char_p),    		('tidalStrength', c_int),    		('gravity', c_int),    		('maxMetal', c_float),    		('extractorRadius', c_int),    		('minWind', c_int),    		('maxWind', c_int),    		('width', c_int),    		('height', c_int),    		('posCount', c_int),    		('StartPos', StartPos * 16),    		('author', c_char_p)]class Unitsync:	def __init__(self, location):		if location.endswith('.so'): self.unitsync = ctypes.cdll.LoadLibrary(location)		elif location.endswith('.dll'): self.unitsync = ctypes.windll.LoadLibrary(location)		def GetSpringVersion(self):					return c_char_p(self.unitsync.GetSpringVersion()).value		def Message(self, message):					pass		def Init(self, isServer, cid):					return c_int(self.unitsync.Init(isServer, cid)).value	def UnInit(self):						self.unitsync.UnInit()		def ProcessUnits(self):					return c_int(self.unitsync.ProcessUnits()).value	def ProcessUnitsNoChecksum(self):				return c_int(self.unitsync.ProcessUnitsNoChecksum()).value		def GetUnitCount(self):					return c_int(self.unitsync.GetUnitCount()).value	def GetUnitName(self, uid):					return c_char_p(self.unitsync.GetUnitName(uid)).value	def GetUnitFullName(self, uid):				return c_char_p(self.unitsync.GetUnitFullName(uid)).value		def AddArchive(self, name):					self.unitsync.AddArchive(name)	def AddAllArchives(self, root):				self.unitsync.AddAllArchives(root)	def GetArchiveChecksum(self, arname):				return c_uint(self.unitsync.GetArchiveChecksum(arname)).value	def GetArchivePath(self, arname):				return c_char_p(self.unitsync.GetArchivePath(arname)).value		def GetMapCount(self):						return c_int(self.unitsync.GetMapCount()).value	def GetMapName(self, index):					return c_char_p(self.unitsync.GetMapName(index)).value	def GetMapInfoEx(self, name, outInfo, version):		return c_int(self.unitsync.GetMapInfoEx(name, outInfo, version)).value	def GetMapInfo(self, name, outInfo):				return c_int(self.unitsync.GetMapInfo(name, outInfo)).value	def GetMapArchiveCount(self, mapName):				return c_int(self.unitsync.GetMapArchiveCount(mapName)).value	def GetMapChecksum(self, index):				return c_uint(self.unitsync.GetMapChecksum(index)).value	def GetMapChecksumFromName(self, mapName):			return c_uint(self.unitsync.GetMapChecksumFromName(mapName)).value	def GetMinimap(self, filename, miplevel):			self.unitsync.GetMinimap(filename, miplevel)	def GetInfoMapSize(self, filename, name, width, height):	return c_int(self.unitsync.GetInfoMapSize(filename, name, width, height)).value	def GetInfoMap(self, filename, name, data, typeHint):		return c_int(self.unitsync.GetInfoMap(filename, name, data, typeHint)).value	def GetPrimaryModCount(self):					return c_int(self.unitsync.GetPrimaryModCount()).value	def GetPrimaryModName(self, index):				return c_char_p(self.unitsync.GetPrimaryModName(index)).value	def GetPrimaryModShortName(self, index):			return c_char_p(self.unitsync.GetPrimaryModShortName(index)).value	def GetPrimaryModVersion(self, index):				return c_char_p(self.unitsync.GetPrimaryModVersion(index)).value	def GetPrimaryModMutator(self, index):				return c_char_p(self.unitsync.GetPrimaryModMutator(index)).value	def GetPrimaryModGame(self, index):				return c_char_p(self.unitsync.GetPrimaryModGame(index)).value	def GetPrimaryModShortGame(self, index):			return c_char_p(self.unitsync.GetPrimaryModShortGame(index)).value	def GetPrimaryModDescription(self, index):			return c_char_p(self.unitsync.GetPrimaryModDescription(index)).value	def GetPrimaryModArchive(self, index):				return c_char_p(self.unitsync.GetPrimaryModArchive(index)).value	def GetPrimaryModArchiveCount(self, index):			return c_int(self.unitsync.GetPrimaryModArchiveCount(index)).value	def GetPrimaryModArchiveList(self, arnr):			return c_char_p(self.unitsync.GetPrimaryModArchiveList(arnr)).value	def GetPrimaryModIndex(self, name):				return c_char_p(self.unitsync.GetPrimaryModIndex(name)).value	def GetPrimaryModChecksum(self, index):			return c_uint(self.unitsync.GetPrimaryModChecksum(index)).value	def GetPrimaryModChecksumFromName(self, name):			return c_uint(self.unitsync.GetPrimaryModChecksumFromName(index)).value		def GetSideCount(self):					return c_int(self.unitsync.GetSideCount()).value	def GetSideName(self, side):					return c_char_p(self.unitsync.GetSideName(side)).value	def GetSideStartUnit(self, side):				return c_char_p(self.unitsync.GetSideStartUnit(side)).value		def GetLuaAICount(self):					return c_int(self.unitsync.GetLuaAICount()).value	def GetLuaAIName(self, aiIndex):				return c_char_p(self.unitsync.GetLuaAIName(aiIndex)).value	def GetLuaAIDesc(self, aiIndex):				return c_char_p(self.unitsync.GetLuaAIDesc(aiIndex)).value		def GetMapOptionCount(self, name):				return c_int(self.unitsync.GetMapOptionCount(name)).value	def GetModOptionCount(self):					return c_int(self.unitsync.GetModOptionCount()).value	def GetOptionKey(self, optIndex):				return c_char_p(self.unitsync.GetOptionKey(optIndex)).value	def GetOptionName(self, optIndex):				return c_char_p(self.unitsync.GetOptionName(optIndex)).value	def GetOptionSection(self, optIndex):				return c_char_p(self.unitsync.GetOptionSection(optIndex)).value	def GetOptionStyle(self, optIndex):				return c_char_p(self.unitsync.GetOptionStyle(optIndex)).value	def GetOptionDesc(self, optIndex):				return c_char_p(self.unitsync.GetOptionDesc(optIndex)).value	def GetOptionType(self, optIndex):				return c_int(self.unitsync.GetOptionType(optIndex)).value	def GetOptionBoolDef(self, optIndex):				return c_int(self.unitsync.GetOptionBoolDef(optIndex)).value	def GetOptionNumberDef(self, optIndex):			return c_float(self.unitsync.GetOptionNumberDef(optIndex)).value	def GetOptionNumberMin(self, optIndex):			return c_float(self.unitsync.GetOptionNumberMin(optIndex)).value	def GetOptionNumberMax(self, optIndex):			return c_float(self.unitsync.GetOptionNumberMax(optIndex)).value	def GetOptionNumberStep(self, optIndex):			return c_float(self.unitsync.GetOptionNumberStep(optIndex)).value	def GetOptionStringDef(self, optIndex):			return c_char_p(self.unitsync.GetOptionStringDef(optIndex)).value	def GetOptionStringMaxLen(self, optIndex):			return c_int(self.unitsync.GetOptionStringMaxLen(optIndex)).value	def GetOptionListCount(self, optIndex):			return c_int(self.unitsync.GetOptionListCount(optIndex)).value	def GetOptionListDef(self, optIndex):				return c_char_p(self.unitsync.GetOptionListCount(optIndex)).value	def GetOptionListItemKey(self, optIndex, itemIndex):		return c_char_p(self.unitsync.GetOptionListItemKey(optIndex, itemIndex)).value	def GetOptionListItemName(self, optIndex, itemIndex):		return c_char_p(self.unitsync.GetOptionListItemName(optIndex, itemIndex)).value	def GetOptionListItemDesc(self, optIndex, itemIndex):		return c_char_p(self.unitsync.GetOptionListItemDesc(optIndex, itemIndex)).value		def GetModValidMapCount(self):					return c_int(self.unitsync.GetModValidMapCount()).value	def GetModValidMap(self, index):				return c_char_p(self.unitsync.GetModValidMap(index)).value		def OpenFileVFS(self, name):					return c_int(self.unitsync.OpenFileVFS(name)).value	def CloseFileVFS(self, handle):				self.unitsync.CloseFileVFS(handle)	def ReadFileVFS(self, handle, buf, length):			self.unitsync.ReadFileVFS(handle, buf, length)	def FileSizeVFS(self, handle):					return c_int(self.unitsync.FileSizeVFS(handle)).value		def InitFindVFS(self, pattern):				return c_int(self.unitsync.InitFindVFS(pattern)).value	def InitDirListVFS(self, path, pattern, modes):		return c_int(self.unitsync.InitDirListVFS(path, pattern, modes)).value	def InitSubDirsVFS(self, path, pattern, modes):		return c_int(self.unitsync.InitSubDirsVFS(path, pattern, modes)).value	def FindFilesVFS(self, handle, nameBuf, size):			return c_int(self.unitsync.FindFilesVFS(handle, nameBuf, size)).value		def OpenArchive(self, name):					return c_int(self.unitsync.OpenArchive(name))	def OpenArchiveType(self, name, type):				return c_int(self.unitsync.OpenArchiveType(name, type))	def CloseArchive(self, archive):				self.unitsync.CloseArchive(archive)	def FindFilesArchive(self, archive, cur, nameBuf, size):	return c_int(self.unitsync.FindFilesArchive(archive, cur, nameBuf, size))	def OpenArchiveFile(self, archive, name):			return c_int(self.unitsync.OpenArchiveFile(archive, name))	def ReadArchiveFile(self, archive, handle, buffer, numBytes):	return c_int(self.unitsync.ReadArchiveFile(archive, handle, buffer, numBytes))	def CloseArchiveFile(self, archive, handle):			self.unitsync.CloseArchiveFile(archive, handle)	def SizeArchiveFile(self, archive, handle):			return c_int(self.unitsync.SizeArchiveFile(archive, handle))		def SetSpringConfigFile(self, filenameAsAbsolutePath):		self.unitsync.SetSpringConfigFile(filenameAsAbsolutePath)	def GetSpringConfigFile(self):					return c_char_p(self.unitsync.GetSpringConfigFile())	def GetSpringConfigString(self, name, defvalue):		return c_char_p(self.unitsync.GetSpringConfigString(name, defvalue))	def GetSpringConfigInt(self, name, defvalue):			return c_int(self.unitsync.GetSpringConfigInt(name, defvalue))	def GetSpringConfigFloat(self, name, defvalue):		return c_float(self.unitsync.GetSpringConfigFloat(name, defvalue))	def SetSpringConfigString(self, name, value):			self.unitsync.SetSpringConfigString(name, value)	def SetSpringConfigInt(self, name, value):			self.unitsync.SetSpringConfigInt(name, value)	def SetSpringConfigFloat(self, name, value):			self.unitsync.SetSpringConfigFloat(name, value)	# LuaParser	def lpClose(self):						self.unitsync.lpClose()	def lpOpenFile(self, filename, fileModes, accessModes):	return c_int(self.unitsync.lpOpenFile(filename, fileModes, accessModes))	def lpOpenSource(self, source, accessModes):			return c_int(self.unitsync.lpOpenSource(source, accessModes))	def lpExecute(self):						return c_int(self.unitsync.lpExecute())	def lpErrorLog(self):						return c_char_p(self.unitsync.lpErrorLog())		def lpAddTableInt(self, key, override):			self.unitsync.lpAddTableInt(key, override)	def lpAddTableStr(self, key, override):			self.unitsync.lpAddTableStr(key, override)	def lpEndTable(self):						self.unitsync.lpEndTable()	def lpAddIntKeyIntVal(self, key, val):				self.unitsync.lpAddIntKeyIntVal(key, val)	def lpAddStrKeyIntVal(self, key, val):				self.unitsync.lpAddStrKeyIntVal(key, val)	def lpAddIntKeyBoolVal(self, key, val):			self.unitsync.lpAddIntKeyBoolVal(key, val)	def lpAddStrKeyBoolVal(self, key, val):			self.unitsync.lpAddStrKeyBoolVal(key, val)	def lpAddIntKeyFloatVal(self, key, val):			self.unitsync.lpAddIntKeyFloatVal(key, val)	def lpAddStrKeyFloatVal(self, key, val):			self.unitsync.lpAddStrKeyFloatVal(key, val)	def lpAddIntKeyStrVal(self, key, val):				self.unitsync.lpAddIntKeyStrVal(key, val)	def lpAddStrKeyStrVal(self, key, val):				self.unitsync.lpAddStrKeyStrVal(key, val)		def lpRootTable(self):						return c_int(self.unitsync.lpRootTable())	def lpRootTableExpr(self, expr):				return c_int(self.unitsync.lpRootTableExpr(expr))	def lpSubTableInt(self, key):					return c_int(self.unitsync.lpSubTableInt(key))	def lpSubTableStr(self, key):					return c_int(self.unitsync.lpSubTableStr(key))	def lpSubTableExpr(self, expr):				return c_int(self.unitsync.lpSubTableExpr(expr))	def lpPopTable(self):						self.unitsync.lpPopTable()	def lpGetKeyExistsInt(self, key):				return c_int(self.unitsync.lpGetKeyExistsInt(key))	def lpGetKeyExistsStr(self, key):				return c_int(self.unitsync.lpGetKeyExistsStr(key))		def lpGetIntKeyType(self, key):				return c_int(self.unitsync.lpGetIntKeyType(key))	def lpGetStrKeyType(self, key):				return c_int(self.unitsync.lpGetStrKeyType(key))		def lpGetIntKeyListCount(self):				return c_int(self.unitsync.lpGetIntKeyListCount())	def lpGetIntKeyListEntry(self, index):				return c_int(self.unitsync.lpGetIntKeyListEntry(index))	def lpGetStrKeyListCount(self):				return c_int(self.unitsync.lpGetStrKeyListCount())	def lpGetStrKeyListEntry(self, index):				return c_char_p(self.unitsync.lpGetStrKeyListEntry(index))		def lpGetIntKeyIntVal(self, key, defVal):			return c_int(self.unitsync.lpGetIntKeyIntVal(key, defVal))	def lpGetStrKeyIntVal(self, key, defVal):			return c_int(self.unitsync.lpGetStrKeyIntVal(key, defVal))	def lpGetIntKeyBoolVal(self, key, defVal):			return c_int(self.unitsync.lpGetIntKeyBoolVal(key, defVal))	def lpGetStrKeyBoolVal(self, key, defVal):			return c_int(self.unitsync.lpGetStrKeyBoolVal(key, defVal))	def lpGetIntKeyFloatVal(self, key, defVal):			return c_float(self.unitsync.GetIntKeyFloatVal(key, defVal))	def lpGetStrKeyFloatVal(self, key, defVal):			return c_float(self.unitsync.GetStrKeyFloatVal(key, defVal))	def lpGetIntKeyStrVal(self, key, defVal):			return c_char_p(self.unitsync.GetIntKeyStrVal(key, defVal))	def lpGetStrKeyStrVal(self, key, defVal):			return c_char_p(self.unitsync.GetStrKeyStrVal(key, defVal))if __name__ == '__main__':	if os.name == 'posix': location = '/usr/lib/spring/unitsync.so'	elif os.name == 'nt': location = 'unitsync.dll'	unitsync = Unitsync(location)	print 'init:', unitsync.Init(True, 1)	print 'spring version:', unitsync.GetSpringVersion()		map_count = unitsync.GetMapCount()	print 'map count:', map_count	maps = {}	map_info = MapInfo()	map_info.author = cast(create_string_buffer(200), c_char_p)	map_info.description = cast(create_string_buffer(254), c_char_p)	print 'loading maps...'	for index in xrange(map_count):		name = unitsync.GetMapName(index)		print name		unitsync.GetMapInfoEx(name, ctypes.pointer(map_info), 1)#ctypes.byref(map_info))		mapd = {}		mapd['name'] = name		mapd['description'] = map_info.description		mapd['tidalStrength'] = map_info.tidalStrength		mapd['gravity'] = map_info.gravity		mapd['maxMetal'] = map_info.maxMetal		mapd['extractorRadius'] = map_info.extractorRadius		mapd['minWind'] = map_info.minWind		mapd['maxWind'] = map_info.maxWind		mapd['width'] = map_info.width		mapd['height'] = map_info.height		mapd['posCount'] = map_info.posCount		startPos = []		for i in xrange(map_info.posCount):			startP = map_info.StartPos[i]			startPos.append((startP.x, startP.y))		mapd['startPos'] = startPos		mapd['author'] = map_info.author		mapd['checksum'] = unitsync.GetMapChecksum(index)		maps[name] = mapd		mod_count = unitsync.GetPrimaryModCount()	print 'mod count:', mod_count	mods = {}	print 'loading mods...'	for index in xrange(mod_count):		name = unitsync.GetPrimaryModName(index)		print name		shortname = unitsync.GetPrimaryModShortName(index)		version = unitsync.GetPrimaryModVersion(index)		mutator = unitsync.GetPrimaryModMutator(index)		game = unitsync.GetPrimaryModGame(index)		shortgame = unitsync.GetPrimaryModShortGame(index)		description = unitsync.GetPrimaryModDescription(index)		archive = unitsync.GetPrimaryModArchive(index)		archive_count = unitsync.GetPrimaryModArchiveCount(index)		checksum = unitsync.GetPrimaryModChecksum(index)		for index in xrange(archive_count):			archive_list = unitsync.GetPrimaryModArchiveList(index)		mods[name] = {'name':name, 'shortname':shortname, 'version':version, 'mutator':mutator, 'game':game, 'shortgame':shortgame, 'description':description, 'archive':archive, 'archive_count':archive_count, 'archive_list':archive_list, 'checksum':checksum}