Mercurial > touhou
annotate pytouhou/vm/common.py @ 503:c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 04 Oct 2013 14:27:11 +0200 |
parents | 69ec72b990a4 |
children | e15672733c93 |
rev | line source |
---|---|
69
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
2 ## |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
4 ## |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
8 ## |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
12 ## GNU General Public License for more details. |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
13 ## |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
14 |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
15 |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
16 class MetaRegistry(type): |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
17 def __new__(mcs, name, bases, classdict): |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
18 instruction_handlers = {} |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
19 for item in classdict.itervalues(): |
376
69ec72b990a4
Support more than one version of a vm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
69
diff
changeset
|
20 if hasattr(item, '_instruction_ids'): |
69ec72b990a4
Support more than one version of a vm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
69
diff
changeset
|
21 for version, instruction_ids in item._instruction_ids.iteritems(): |
69ec72b990a4
Support more than one version of a vm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
69
diff
changeset
|
22 for id_ in instruction_ids: |
69ec72b990a4
Support more than one version of a vm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
69
diff
changeset
|
23 instruction_handlers.setdefault(version, {})[id_] = item |
69
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
24 classdict['_handlers'] = instruction_handlers |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
25 return type.__new__(mcs, name, bases, classdict) |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
26 |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
27 |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
28 |
376
69ec72b990a4
Support more than one version of a vm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
69
diff
changeset
|
29 def instruction(instruction_id, version=6): |
69
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
30 def _decorator(func): |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
31 if not hasattr(func, '_instruction_ids'): |
376
69ec72b990a4
Support more than one version of a vm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
69
diff
changeset
|
32 func._instruction_ids = {} |
69ec72b990a4
Support more than one version of a vm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
69
diff
changeset
|
33 func._instruction_ids.setdefault(version, set()).add(instruction_id) |
69
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
34 return func |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
35 return _decorator |