1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-08-13 16:53:59 -04:00

pymml: last_error fix & more unified standards.

This commit is contained in:
Merlijn Wajer 2010-09-12 14:28:19 +02:00
parent 4b5f7d0305
commit 688e3e9ef3
5 changed files with 82 additions and 58 deletions

View File

@ -16,7 +16,7 @@ Const
MOUSE_DOWN = 1;
var
last_error: PChar;
last_error: String;
debug: boolean;
function init: integer;
@ -31,7 +31,7 @@ begin
result := Assigned(C);
if not result then
begin
last_error := PChar('PClient is NULL');
last_error := 'PClient is NULL';
if debug then
writeln(last_error);
end;
@ -48,7 +48,7 @@ begin
begin
writeln('ERROR');
result := PtrUInt(RESULT_ERROR);
last_error := PChar(e.Message);
last_error := e.Message;
end;
end;
writeln(format('C: %d, IOManager: %d', [PtrUInt(C), PtrUInt(C.IOManager)]));
@ -73,9 +73,15 @@ begin
exit(debug);
end;
{
VERY IMPORTANT: If you use get_last_error, you must immediately store the
resulting string somewhere else. As soon as you do other calls, the last error
may be reset or assigned a different memory position, making your old
pointer invalid.
}
function get_last_error: pchar;
begin
exit(last_error);
exit(@last_error[1]);
end;
function array_to_ptr(ptr: Pointer; size: PtrUInt; objsize: PtrUInt): Pointer;
@ -122,7 +128,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := PChar(e.Message);
last_error := e.Message;
end;
end;
end;
@ -140,7 +146,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := PChar(e.Message);
last_error := e.Message;
end;
end;
end;
@ -168,7 +174,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := PChar(e.Message);
last_error := e.Message;
end;
end;
end;
@ -193,7 +199,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := PChar(e.Message);
last_error := e.Message;
end;
end;
end;
@ -213,7 +219,8 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := PChar(e.Message);
last_error := e.Message;
writeln('last_error: ' + last_error);
end;
end;
end;
@ -234,7 +241,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := PChar(e.Message);
last_error := e.Message;
end;
end;
end;
@ -254,7 +261,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := PChar(e.Message);
last_error := e.Message;
end;
end;
@ -278,7 +285,7 @@ begin
except on e : Exception do
begin
result := RESULT_ERROR;
last_error := PChar(e.Message);
last_error := e.Message;
end;
end;

View File

@ -21,10 +21,20 @@ class MMLCore(object):
self.dll.init.argtypes = None
self.dll.create_client.restype = c_ulong
self.dll.create_client.argtypes = None
self.dll.get_last_error.restype = c_char_p
self.dll.get_last_error.argtypes = None
if self.dll.init() != 0:
del self.dll
raise MMLCoreException("Could not initialize the DLL")
def get_last_error(self):
t = self.dll.get_last_error()
s = str(t)
del t
return s
def __del__(self):
del self.dll
@ -38,14 +48,15 @@ if __name__ == '__main__':
raise Exception('Could create a client');
c = Color(DLL, client)
ret = c.find((0, 0, 100000, 10000), 0)
ret = c.find((0, 0, 100, 100), 0)
print ret
ret = c.findAll((0, 0, 100, 100), 0)
print ret
raise Exception('WAT')
m = Mouse(DLL, client)
@ -59,14 +70,10 @@ if __name__ == '__main__':
sleep(2)
print 'Done'
#
# # Reset all buttons..
m[(Mouse.Left, Mouse.Right, Mouse.Middle)] = [False for x in range(3)]
for v in zip((Mouse.Left, Mouse.Right), m[(Mouse.Left, Mouse.Right)]):
print v
print m.getPos()
# if hasattr(ret,'__iter__'):
# m.setPos(ret)
del DLL

View File

@ -0,0 +1,24 @@
#!/usr/bin/env python
class Bitmap(object):
# Pixels, alleen ter illustratie atm
pixels = None
# Index van de managed bitmaps
_index = None
def __init__(self):
self.pixels = [range(100) for x in range(100)]
pass
def __getitem__(self, item):
if iterable(item):
return self.pixels[item[0]][item[1]]
def find(self, searchbox = (), tol = 0, frm = (), _type = 'default', client
= None):
if iterable(searchbox):
if len(searchbox) != 4:
raise Exception("Invalid argument")
# Hier volgen gewoon de juiste calls

View File

@ -15,7 +15,6 @@ class ColorException(Exception):
Exception.__init__(self, err)
# FIXME: Complete...
class Color(object):
'''
@ -48,6 +47,9 @@ class Color(object):
if ret is RESULT_OK:
return (x, y)
elif ret is RESULT_ERROR:
print self._mc
raise ColorException(self._mc.get_last_error())
return None
@ -86,3 +88,4 @@ class Color(object):
self._mc.dll.find_colors_tolerance.argtypes = [c_ulong,
POINTER(PPOINT), POINTER(c_int), c_int, c_int,
c_int, c_int, c_int, c_int]

View File

@ -1,48 +1,31 @@
#!/usr/bin/env python
class Bitmap(object):
# Pixels, alleen ter illustratie atm
pixels = None
from ctypes import *
from mmltypes import RESULT_OK, RESULT_FALSE, RESULT_ERROR
# Index van de managed bitmaps
_index = None
def __init__(self):
self.pixels = [range(100) for x in range(100)]
pass
# TODO:
# Load from user defined points. (So called dynamic)
def __getitem__(self, item):
if iterable(item):
return self.pixels[item[0]][item[1]]
def find(self, searchbox = (), tol = 0, frm = (), _type = 'default', client
= None):
if iterable(searchbox):
if len(searchbox) != 4:
raise Exception("Invalid argument")
# Hier volgen gewoon de juiste calls
class Mouse(object):
lastPolledPos = (0,0)
states = None
class DTM(object):
def __init__(self):
self.states = {'left': 'down', 'right' : 'up', 'middle' : ' up'}
pass
def _getButtonState(self, button):
return self.states[button]
def __del__(self):
pass
def __getitem__(self, item):
if iterable(item):
if item['state'] in ('left', 'right', 'middle'):
return self._getButtonState(item['state'])
def __repr__(self):
pass
iterable = lambda x: hasattr(x, '__iter__')
def to_str(self):
pass
def from_str(self):
pass
def find(self):
pass
def set_name(self):
pass
m = Mouse()
print m[{'state' : 'left'}]
a = Bitmap()
print a[(2,3)]
a.find()