/usr/share/pyshared/libavg/gameapp.py is in python-libavg 1.7.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | # libavg - Media Playback Engine.
# Copyright (C) 2003-2011 Ulrich von Zadow
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Current versions can be found at www.libavg.de
#
# Original author of this file is OXullo Intersecans <x@brainrapers.org>
#
'''
Transitional class to fulfill some of the missing features of AVGApp,
currently needed for multitouch games.
'''
from __future__ import with_statement
import os
import sys
import cPickle as pickle
import libavg
from libavg import avg
g_Player = avg.Player.get()
g_Log = avg.Logger.get()
class Datastore(object):
'''
General purpose persistent object (state in self.data)
Even if it's possible to use Datastore independently from GameApp, the suggested
use is described with the following case (given MyApp a subclass of GameApp):
>>> from mypackage.app import MyApp
>>> g_App = MyApp.get()
>>> def validate(data):
>>> return type(data) == list and isinstance(data[2], Bar)
>>> myInitialData = [1, 'foo', Bar(12)]
>>> myDs = g_App.initDatastore('hiscore', myInitialData, validate)
>>> myDs.data[2].foobarize(1)
>>> myDs.data[0] = 2
>>> myDs.commit()
>>> againMyDs = g_App.getDatastore('hiscore')
'''
def __init__(self, dumpFile, initialData, validator, autoCommit):
'''
dumpFile: path to a file which is used for storing and retrieving data
initialData: a callable (class, method) or an instance that is used to initialize
the datastore when the dumpfile either doesn't exist, it's corrupted, or
cannot be accessed or when the loaded data don't comply with the optional
validator. Defaults to an empty dict.
validator: a callable to which stored data are passed and that is able to
determine if the data format is expected. If the call returns True, the
proposed data are accepted and set. With False as return value the
Datastore uses initialData instead.
autoCommit: if True, the datastore sync itself when the program exits (uses
atexit). It's always possible to commit() manually.
'''
self.__dumpFile = dumpFile
if hasattr(initialData, '__call__'):
initialData = initialData()
elif initialData is None:
initialData = dict()
if os.path.exists(self.__dumpFile):
if not os.path.isfile:
raise RuntimeError('%s dump file '
'is not a plain file' % self)
elif not os.access(self.__dumpFile, os.R_OK | os.W_OK):
raise RuntimeError('%s dump file'
'cannot be accessed with r/w permissions' % self)
try:
f = open(self.__dumpFile)
except IOError:
g_Log.trace(g_Log.APP, 'Initializing %s' % self)
self.data = initialData
self.commit()
else:
try:
self.data = pickle.load(f)
except:
f.close()
g_Log.trace(g_Log.ERROR, 'Datastore %s is corrupted, '
'reinitializing' % self)
self.data = initialData
self.commit()
else:
f.close()
if not validator(self.data):
g_Log.trace(g_Log.ERROR, 'Sanity check failed for %s: '
'reinitializing' % self)
self.data = initialData
self.commit()
else:
g_Log.trace(g_Log.APP, '%s successfully '
'loaded' % self)
if autoCommit:
import atexit
atexit.register(self.commit)
def commit(self):
'''
Dump Datastore data to disk
'''
import time
tempFile = self.__dumpFile + '.tmp.' + str(int(time.time() * 1000))
try:
with open(tempFile, 'wb') as f:
pickle.dump(self.data, f)
except Exception, e:
g_Log.trace(g_Log.ERROR, 'Cannot save '
'%s (%s)' % (self.__dumpFile, str(e)))
return False
else:
if os.path.exists(self.__dumpFile):
try:
os.remove(self.__dumpFile)
except Exception, e:
g_Log.trace(g_Log.ERROR, 'Cannot overwrite '
'dump file %s (%s)' % (self, str(e)))
return False
try:
os.rename(tempFile, self.__dumpFile)
except Exception, e:
g_Log.trace(g_Log.ERROR, 'Cannot save '
'%s (%s)' % (self, str(e)))
os.remove(tempFile)
return False
else:
g_Log.trace(g_Log.APP, '%s saved' % self)
return True
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, self.__dumpFile)
class GameApp(libavg.AVGApp):
'''
Derivation from this class adds a command line parser, which can be used to
define fullscreen behavior and resolution. It defaults to fullscreen and
sets the resolution as the current desktop's one.
Multitouch is enabled by default.
'''
multitouch = True
instances = {}
def __init__(self, *args, **kwargs):
appname = self.__class__.__name__
if appname in GameApp.instances:
raise RuntimeError('App %s already setup' % appname)
GameApp.instances[appname] = self
super(GameApp, self).__init__(*args, **kwargs)
self.__datastores = {}
pkgpath = self._getPackagePath()
if pkgpath is not None:
avg.WordsNode.addFontDir(libavg.utils.getMediaDir(pkgpath, 'fonts'))
self._parentNode.mediadir = libavg.utils.getMediaDir(pkgpath)
@classmethod
def get(cls):
'''
Get the Application instance
Note: this class method has to be called from the top-level app class:
>>> class MyApp(gameapp.GameApp):
... pass
>>> instance = MyApp.get()
'''
return cls.instances.get(cls.__name__, None)
@classmethod
def start(cls, *args, **kwargs):
import optparse
parser = optparse.OptionParser()
parser.add_option('-r', '--resolution', dest='resolution',
default=None, help='set an explicit resolution', metavar='WIDTHxHEIGHT')
parser.add_option('-w', '--window', dest='window', action='store_true',
default=False, help='run the game in a window')
(options, args) = parser.parse_args()
if options.resolution is not None:
import re
m = re.match('^(\d+)x(\d+)$', options.resolution)
if m is None:
sys.stderr.write('\n** ERROR: invalid resolution '
'specification %s\n\n' % options.resolution)
parser.print_help()
sys.exit(1)
else:
kwargs['resolution'] = map(int, m.groups())
elif not 'resolution' in kwargs:
kwargs['resolution'] = g_Player.getScreenResolution()
if options.window:
if options.resolution is None:
sys.stderr.write('\n** ERROR: in window mode the resolution '
'must be set\n\n')
parser.print_help()
sys.exit(1)
else:
if 'AVG_DEPLOY' in os.environ:
del os.environ['AVG_DEPLOY']
else:
os.environ['AVG_DEPLOY'] = '1'
g_Log.trace(g_Log.APP, 'Setting resolution to: %s' % str(kwargs['resolution']))
super(GameApp, cls).start(*args, **kwargs)
def quit(self):
'''
Quit the application
The player is stopped if the application has been started by its own
(eg: no appchooser)
'''
self.leave()
if self.getStarter() is not None:
g_Player.get().stop()
def getUserdataPath(self, fname):
'''
Return a path which is platform dependent and that points to a directory
which can be used to store games data.
These data will belong to the current user and are not meant for system-wide
settings.
The path is constructed using the name of the app's class and it is created
if it doesn't exist.
'''
if os.name == 'posix':
path = os.path.join(os.environ['HOME'], '.avg',
self.__class__.__name__.lower())
elif os.name == 'nt':
path = os.path.join(os.environ['APPDATA'], 'Avg',
self.__class__.__name__.lower())
else:
raise RuntimeError('Unsupported system %s' % os.name)
try:
os.makedirs(path)
except OSError, e:
import errno
if e.errno != errno.EEXIST:
raise
return os.path.join(path, fname)
def initDatastore(self, tag, initialData=None, validator=lambda ds: True,
autoCommit=True):
'''
Initialize and return a Datastore instance
tag: a string that references the datastore. Such tags are uniques within
the application's scope and may be retrieved from different parts of
the game's package with getDatastore() method.
See the documentation on Datastore class for the rest of the parameters.
>>> myDs = MyApp.get().initDatastore('hiscore', [])
>>> myDs.data.append(ScoreEntry(12321, 'oxi'))
>>> myDs.commit()
'''
if type(tag) != str:
raise TypeError('Tag must be a string instead of %s' % type(tag))
if tag in self.__datastores:
raise RuntimeError('Datastore %s already initialized')
ds = Datastore(self.getUserdataPath(tag + '.pkl'), initialData, validator,
autoCommit)
self.__datastores[tag] = ds
return ds
def getDatastore(self, tag):
'''
Return an initialized Datastore instance given its tag
'''
return self.__datastores.get(tag, None)
def _getPackagePath(self):
'''
Overload this method in your App class if you want to get 'media' and 'fonts'
easily set up (respectively as mediadir for App._parentNode and for local font
path).
This method should return a relative (to CWD) or absolute path. It can be a file
path as well.
Eg:
def _getPackagePath(self):
return __file__
'''
return None
|