Compare commits
336 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8548cce4e5 | ||
|
|
ad2aca1803 | ||
|
|
8cf500ce44 | ||
|
|
ceda7f370e | ||
|
|
0692a10253 | ||
|
|
c1c2a96295 | ||
|
|
b4be9fa757 | ||
|
|
a8967f0b7d | ||
|
|
10af5cc250 | ||
|
|
0ea1b0324e | ||
|
|
4ece6efe60 | ||
|
|
15bc8e0ed2 | ||
|
|
33fad6339f | ||
|
|
93916104db | ||
|
|
3bb4fb0640 | ||
|
|
c9495763e5 | ||
|
|
a7825a881c | ||
|
|
a6bbd97b98 | ||
|
|
3500212da0 | ||
|
|
01ea9d7879 | ||
|
|
f19e1b8bfb | ||
|
|
f2202556d7 | ||
|
|
5a5b471fe4 | ||
|
|
ff0f20d1dd | ||
|
|
4898c852c1 | ||
|
|
adf5295e2b | ||
|
|
7514baaa5f | ||
|
|
0ba1a779ef | ||
|
|
3ea8a27914 | ||
|
|
2451ad3674 | ||
|
|
25804df238 | ||
|
|
474173ba54 | ||
|
|
049a3b703f | ||
|
|
ac77fde892 | ||
|
|
6ee9b22923 | ||
|
|
f355138cd2 | ||
|
|
478d135d1f | ||
|
|
80c9acdabe | ||
|
|
d4eaa7c543 | ||
|
|
2571550da4 | ||
|
|
b3ee3a3506 | ||
|
|
11feccd93b | ||
|
|
bb6ce5c013 | ||
|
|
a35aa9046e | ||
|
|
6c32da878d | ||
|
|
49c54bc896 | ||
|
|
4c9fa38ffb | ||
|
|
2856e78f16 | ||
|
|
33884925f4 | ||
|
|
a11ef96d9a | ||
|
|
7b6239d66a | ||
|
|
2c3bd140ff | ||
|
|
9d2087a0fb | ||
|
|
67db8110a1 | ||
|
|
ab1c56ff3e | ||
|
|
142f2e42ca | ||
|
|
e7764c1665 | ||
|
|
582cfea47c | ||
|
|
6f38d84a1c | ||
|
|
1fc46e66bc | ||
|
|
167673aa08 | ||
|
|
5ad8ee986f | ||
|
|
e9d7ee7e8e | ||
|
|
d21c3ec004 | ||
|
|
01c6931d53 | ||
|
|
493563bb6f | ||
|
|
ca5c71402b | ||
|
|
ad765a1ede | ||
|
|
9adee14e0f | ||
|
|
57c4bd0e7c | ||
|
|
1085dbc8ab | ||
|
|
fb9740003e | ||
|
|
087f16fbc5 | ||
|
|
fa96e80590 | ||
|
|
539d38fb4a | ||
|
|
caaf0b0e13 | ||
|
|
16958e516d | ||
|
|
74e4e0c4ec | ||
|
|
3efeb46500 | ||
|
|
0f2e933be1 | ||
|
|
a7f40b0d15 | ||
|
|
e6ac99458f | ||
|
|
92cadf26e9 | ||
|
|
305038a31d | ||
|
|
bd67d6f19f | ||
|
|
81eae4edbf | ||
|
|
776ef71574 | ||
|
|
31125ca489 | ||
|
|
29ab108764 | ||
|
|
61820f1670 | ||
|
|
7fafb8b5ae | ||
|
|
28e84c0c5a | ||
|
|
e629214bef | ||
|
|
5e9433b4a4 | ||
|
|
5f2082c6e9 | ||
|
|
12c0deadee | ||
|
|
6da766ef22 | ||
|
|
f278a4bfcf | ||
|
|
631fe91049 | ||
|
|
159f39227a | ||
|
|
670acef0b4 | ||
|
|
1165769aca | ||
|
|
613dd32a40 | ||
|
|
d7a88f904e | ||
|
|
a8344a231b | ||
|
|
11043e365a | ||
|
|
ad34ba78ea | ||
|
|
f9b4ae1308 | ||
|
|
7fee8f6bfe | ||
|
|
2e0ca3649c | ||
|
|
e0d44741e9 | ||
|
|
008d59c7d6 | ||
|
|
ed03b0d49f | ||
|
|
4cc1513e58 | ||
|
|
c768aeaf40 | ||
|
|
42ebb0e915 | ||
|
|
31ba9635eb | ||
|
|
dc58512ee6 | ||
|
|
4a58731441 | ||
|
|
c2b92d2d7d | ||
|
|
640b384d27 | ||
|
|
a2ef3d9f8e | ||
|
|
0456b4b62d | ||
|
|
92c9c82e73 | ||
|
|
c5ed1cff24 | ||
|
|
0710735546 | ||
|
|
7869df224e | ||
|
|
6f6274ec7d | ||
|
|
40da130066 | ||
|
|
5947d52c8d | ||
|
|
e4b73a7196 | ||
|
|
1ded1180dc | ||
|
|
5517d60e7a | ||
|
|
ed7e42625e | ||
|
|
d5cde896fb | ||
|
|
007c79f4a7 | ||
|
|
f1b523b5de | ||
|
|
c42e0d7291 | ||
|
|
1ee1b9acc6 | ||
|
|
9904633a99 | ||
|
|
c8791db75e | ||
|
|
21d1c7ebfe | ||
|
|
996a43be5b | ||
|
|
9e8127e577 | ||
|
|
cfcd324a11 | ||
|
|
6872634bf4 | ||
|
|
091090c6fd | ||
|
|
bd4fff4200 | ||
|
|
52dfd0be05 | ||
|
|
60f1737115 | ||
|
|
7a5d6baf02 | ||
|
|
44a332a77b | ||
|
|
beb0a2d6a4 | ||
|
|
9be66df52b | ||
|
|
da0117db1b | ||
|
|
4dbf01a604 | ||
|
|
36858ed3e2 | ||
|
|
370c586582 | ||
|
|
fdfffd96c9 | ||
|
|
6da9f58b23 | ||
|
|
12e3912a37 | ||
|
|
8147e7e1d7 | ||
|
|
19dba6651c | ||
|
|
274f96c710 | ||
|
|
09e1d0b6fc | ||
|
|
f4fb68e310 | ||
|
|
8edf7ae89b | ||
|
|
b6458e9eb7 | ||
|
|
375af1e7f6 | ||
|
|
76d0a72590 | ||
|
|
3255556835 | ||
|
|
d19122c039 | ||
|
|
5692f7b8b6 | ||
|
|
21cea0f009 | ||
|
|
193d762132 | ||
|
|
227fbf7a2e | ||
|
|
25a397bcc5 | ||
|
|
b0dca80b87 | ||
|
|
ea475b528f | ||
|
|
2036e3c5b3 | ||
|
|
584d9f11e8 | ||
|
|
df020281f1 | ||
|
|
78c1b8869e | ||
|
|
87d5e8340b | ||
|
|
e6423d2f43 | ||
|
|
fac44a12b0 | ||
|
|
99ca7b1674 | ||
|
|
e066724a2f | ||
|
|
dce032de31 | ||
|
|
2f578b2bc4 | ||
|
|
0c1656e6ab | ||
|
|
2b6d8a70f4 | ||
|
|
1a308e9671 | ||
|
|
7b21e5634c | ||
|
|
e4548a285d | ||
|
|
72e926f04c | ||
|
|
d9fa14b17c | ||
|
|
33c5abaaf4 | ||
|
|
2dfd61fcc5 | ||
|
|
eb58e747ce | ||
|
|
1d221a2289 | ||
|
|
2ffd0458d0 | ||
|
|
25f533a31b | ||
|
|
570dbce181 | ||
|
|
ccb63e971b | ||
|
|
8be4bce8bc | ||
|
|
e945706d2b | ||
|
|
6c748a6ab2 | ||
|
|
6abc7ca7d2 | ||
|
|
c57e0e467c | ||
|
|
e46b4adad2 | ||
|
|
5ef9b5354a | ||
|
|
34ca7d54be | ||
|
|
cb316f1992 | ||
|
|
da05a6cf1f | ||
|
|
f06c31e225 | ||
|
|
b4e5596ca2 | ||
|
|
49a54ce099 | ||
|
|
0349fd9078 | ||
|
|
118ef2813a | ||
|
|
256f74b71a | ||
|
|
4a84453ca4 | ||
|
|
34316cb166 | ||
|
|
0f7d35cdca | ||
|
|
2ee8a6f008 | ||
|
|
848a6745c0 | ||
|
|
0cbbedd27b | ||
|
|
e951a5b5c3 | ||
|
|
68bf3ba4a2 | ||
|
|
5b4f8f03dc | ||
|
|
d7c2215cbc | ||
|
|
629e59d3f9 | ||
|
|
8f68bc219e | ||
|
|
ba296377de | ||
|
|
e34927a996 | ||
|
|
3c6a917550 | ||
|
|
dbae2acf27 | ||
|
|
722e8eeabf | ||
|
|
a6a26a9999 | ||
|
|
a6328d5aee | ||
|
|
4e76ebe7cf | ||
|
|
c0a26ffb57 | ||
|
|
7dfb10cb51 | ||
|
|
de33906db5 | ||
|
|
605337b280 | ||
|
|
235cd4929f | ||
|
|
220a02543e | ||
|
|
8ac47c2397 | ||
|
|
d384978322 | ||
|
|
f02a479834 | ||
|
|
b5e8b36173 | ||
|
|
08a39f4df7 | ||
|
|
61ec51beec | ||
|
|
9adbdcdcc8 | ||
|
|
e7b05f72ca | ||
|
|
75f2f363a4 | ||
|
|
cc1bb9ac1d | ||
|
|
d498d1f2c8 | ||
|
|
8c0635bb2a | ||
|
|
309dbeeb52 | ||
|
|
4cc87bf81e | ||
|
|
f34bb42dcb | ||
|
|
59ec99809a | ||
|
|
4b963f96d2 | ||
|
|
58db8f66de | ||
|
|
95623eba58 | ||
|
|
8dba0617bd | ||
|
|
289073be8e | ||
|
|
f3c8015366 | ||
|
|
99e8118458 | ||
|
|
80745cfd1c | ||
|
|
92a06bccaf | ||
|
|
fde9ddf4d9 | ||
|
|
03a56c9982 | ||
|
|
d07a0df0fd | ||
|
|
848397fe63 | ||
|
|
0f9246c5c6 | ||
|
|
2e7f887970 | ||
|
|
ef9df6b058 | ||
|
|
baae0f6d6e | ||
|
|
0f369b682d | ||
|
|
1f1e4de254 | ||
|
|
75ddc0a5ba | ||
|
|
e4cb168138 | ||
|
|
63aebba754 | ||
|
|
8cf1a43d59 | ||
|
|
bbc8813b61 | ||
|
|
5b51dbd30f | ||
|
|
295c7972e7 | ||
|
|
b034661c38 | ||
|
|
f12fd95ee1 | ||
|
|
bc33313fc7 | ||
|
|
affc7fcf89 | ||
|
|
b8f1593a2c | ||
|
|
7879f4e118 | ||
|
|
4ba611ae01 | ||
|
|
82ff6d9c64 | ||
|
|
f603ea6186 | ||
|
|
fcf6a4568b | ||
|
|
2ad6cc1b51 | ||
|
|
025f7d31f2 | ||
|
|
9fdb281e4a | ||
|
|
11e28bde2a | ||
|
|
1faa6f977c | ||
|
|
6866e7397f | ||
|
|
fa0b3a5340 | ||
|
|
16c808bce8 | ||
|
|
ec4b2d0770 | ||
|
|
3b610fdfd1 | ||
|
|
8b93c5eefa | ||
|
|
f4bb9eae8f | ||
|
|
ecb14197cf | ||
|
|
95fd58e25a | ||
|
|
afc333ab49 | ||
|
|
eb6406bca4 | ||
|
|
d486aa130d | ||
|
|
d66d5226a2 | ||
|
|
d86da70eeb | ||
|
|
aa0b4b63a9 | ||
|
|
5f479e46b4 | ||
|
|
1e55d5a9d8 | ||
|
|
077a95b5e7 | ||
|
|
4f1399cf66 | ||
|
|
9590b30e66 | ||
|
|
34f3ee4c3e | ||
|
|
7d655543f5 | ||
|
|
5de3ed0d5e | ||
|
|
74c3287cc0 | ||
|
|
3a7f8072a0 | ||
|
|
5fa91580eb | ||
|
|
d8fbb55438 | ||
|
|
99eb4fed74 | ||
|
|
6b78b841df | ||
|
|
dae852db69 | ||
|
|
0c0de2bcbc | ||
|
|
9f2d2f2194 |
@@ -9,4 +9,4 @@ exclude_lines =
|
||||
def __repr__
|
||||
raise AssertionError
|
||||
assert False
|
||||
pass
|
||||
^\s*pass\b
|
||||
|
||||
33
.github/workflows/bundle_windows.yml
vendored
33
.github/workflows/bundle_windows.yml
vendored
@@ -1,22 +1,29 @@
|
||||
# Have to manually unzip this (it gets double zipped) and add it
|
||||
# onto the release after it gets created. Don't want actions with repo write.
|
||||
name: Bundle Windows EXE
|
||||
|
||||
|
||||
on:
|
||||
# Only trigger on release creation
|
||||
release:
|
||||
types:
|
||||
- created
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
ref_name:
|
||||
description: Name to use for the release
|
||||
env:
|
||||
target_tag: ${{ github.ref_name || github.event.inputs.ref_name }}
|
||||
sha: ${{ github.sha || github.event.inputs.ref_name }}
|
||||
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: windows-latest
|
||||
runs-on: windows-2019
|
||||
permissions:
|
||||
contents: write
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: [3.9]
|
||||
python-version: ["3.11"]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
@@ -29,18 +36,30 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
pip install -e .
|
||||
pip install cx_freeze
|
||||
|
||||
- name: Bundle with cx_Freeze
|
||||
shell: bash
|
||||
run: |
|
||||
python setup_cxfreeze.py build_exe
|
||||
pip install pip-licenses
|
||||
pip-licenses --format=plain-vertical --with-license-file --no-license-path --output-file=lib_licenses.txt
|
||||
python setup_cxfreeze.py finalize_cxfreeze
|
||||
# Should only be one, but we don't know what it's named
|
||||
mv ./dist/*.zip hippolyzer-windows-${{ env.target_tag }}.zip
|
||||
|
||||
- name: Upload the artifact
|
||||
uses: actions/upload-artifact@v2
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: hippolyzer-gui-windows-${{ github.sha }}
|
||||
path: ./dist/**
|
||||
name: hippolyzer-windows-${{ env.sha }}
|
||||
path: ./hippolyzer-windows-${{ env.target_tag }}.zip
|
||||
|
||||
- uses: ncipollo/release-action@v1.10.0
|
||||
if: github.event_name != 'workflow_dispatch'
|
||||
with:
|
||||
artifacts: hippolyzer-windows-${{ env.target_tag }}.zip
|
||||
tag: ${{ env.target_tag }}
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
allowUpdates: true
|
||||
|
||||
2
.github/workflows/pypi_publish.yml
vendored
2
.github/workflows/pypi_publish.yml
vendored
@@ -19,7 +19,7 @@ jobs:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: 3.9
|
||||
python-version: "3.10"
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
|
||||
13
.github/workflows/pytest.yml
vendored
13
.github/workflows/pytest.yml
vendored
@@ -1,6 +1,12 @@
|
||||
name: Run Python Tests
|
||||
|
||||
on: [push, pull_request]
|
||||
on:
|
||||
push:
|
||||
paths-ignore:
|
||||
- '*.md'
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- '*.md'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -8,7 +14,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: [3.8, 3.9]
|
||||
python-version: ["3.10", "3.12"]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
@@ -20,10 +26,11 @@ jobs:
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install --upgrade pip wheel
|
||||
pip install -r requirements.txt
|
||||
pip install -r requirements-test.txt
|
||||
sudo apt-get install libopenjp2-7
|
||||
pip install -e .
|
||||
- name: Run Flake8
|
||||
run: |
|
||||
flake8 .
|
||||
|
||||
39
README.md
39
README.md
@@ -27,7 +27,7 @@ with low-level SL details. See the [Local Animation addon example](https://githu
|
||||
|
||||
### From Source
|
||||
|
||||
* Python 3.8 or above is **required**. If you're unable to upgrade your system Python package due to
|
||||
* Python 3.10 or above is **required**. If you're unable to upgrade your system Python package due to
|
||||
being on a stable distro, you can use [pyenv](https://github.com/pyenv/pyenv) to create
|
||||
a self-contained Python install with the appropriate version.
|
||||
* [Create a clean Python 3 virtualenv](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment)
|
||||
@@ -48,8 +48,7 @@ A proxy is provided with both a CLI and Qt-based interface. The proxy applicatio
|
||||
custom SOCKS 5 UDP proxy, as well as an HTTP proxy based on [mitmproxy](https://mitmproxy.org/).
|
||||
|
||||
Multiple clients are supported at a time, and UDP messages may be injected in either
|
||||
direction. The proxy UI was inspired by the Message Log and Message Builder as present in
|
||||
the [Alchemy](https://github.com/AlchemyViewer/Alchemy) viewer.
|
||||
direction.
|
||||
|
||||
### Proxy Setup
|
||||
|
||||
@@ -83,6 +82,10 @@ SOCKS 5 works correctly on these platforms, so you can just configure it through
|
||||
the `no_proxy` env var appropriately. For ex. `no_proxy="asset-cdn.glb.agni.lindenlab.com" ./firestorm`.
|
||||
* Log in!
|
||||
|
||||
Or, if you're on Linux, you can instead use [LinHippoAutoProxy](https://github.com/SaladDais/LinHippoAutoProxy)
|
||||
to launch your viewer, which will configure everything for you. Note that connections from the in-viewer browser will
|
||||
likely _not_ be run through Hippolyzer when using LinHippoAutoProxy.
|
||||
|
||||
### Filtering
|
||||
|
||||
By default, the proxy's display filter is configured to ignore many high-frequency messages.
|
||||
@@ -311,6 +314,22 @@ If you are a viewer developer, please put them in a viewer.
|
||||
apply the mesh to the local mesh target. It works on attachments too. Useful for testing rigs before a
|
||||
final, real upload.
|
||||
|
||||
## REPL
|
||||
|
||||
A quick and dirty REPL is also included for when you want to do ad-hoc introspection of proxy state.
|
||||
It can be launched at any time by typing `/524 spawn_repl` in chat.
|
||||
|
||||

|
||||
|
||||
The REPL is fully async aware and allows awaiting events without blocking:
|
||||
|
||||
```python
|
||||
>>> from hippolyzer.lib.client.object_manager import ObjectUpdateType
|
||||
>>> evt = await session.objects.events.wait_for((ObjectUpdateType.UPDATE,), timeout=2.0)
|
||||
>>> evt.updated
|
||||
{'Position'}
|
||||
```
|
||||
|
||||
## Potential Changes
|
||||
|
||||
* AISv3 wrapper?
|
||||
@@ -375,11 +394,21 @@ To have your client's traffic proxied through Hippolyzer the general flow is:
|
||||
* The proxy needs to use content sniffing to figure out which requests are login requests,
|
||||
so make sure your request would pass `MITMProxyEventManager._is_login_request()`
|
||||
|
||||
#### Do I have to do all that?
|
||||
|
||||
You might be able to automate some of it on Linux by using
|
||||
[LinHippoAutoProxy](https://github.com/SaladDais/LinHippoAutoProxy). If you're on Windows or MacOS the
|
||||
above is your only option.
|
||||
|
||||
### Should I use this library to make an SL client in Python?
|
||||
|
||||
No. If you just want to write a client in Python, you should instead look at using
|
||||
Probably not. If you just want to write a client in Python, you should instead look at using
|
||||
[libremetaverse](https://github.com/cinderblocks/libremetaverse/) via pythonnet.
|
||||
I removed the client-related code inherited from PyOGP because libremetaverse's was simply better.
|
||||
I removed the client-related code inherited from PyOGP because libremetaverse's was simply better
|
||||
for general use.
|
||||
|
||||
<https://github.com/CasperTech/node-metaverse/> also looks like a good, modern wrapper if you
|
||||
prefer TypeScript.
|
||||
|
||||
There is, however, a very low-level `HippoClient` class provided for testing, but it's unlikely
|
||||
to be what you want for writing a general-purpose bot.
|
||||
|
||||
@@ -11,7 +11,7 @@ import enum
|
||||
import os.path
|
||||
from typing import *
|
||||
|
||||
from PySide2 import QtCore, QtGui, QtWidgets
|
||||
from PySide6 import QtCore, QtGui, QtWidgets
|
||||
|
||||
from hippolyzer.lib.base.datatypes import Vector3
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
@@ -80,7 +80,7 @@ class BlueishObjectListGUIAddon(BaseAddon):
|
||||
raise
|
||||
|
||||
def _highlight_object(self, session: Session, obj: Object):
|
||||
session.main_region.circuit.send_message(Message(
|
||||
session.main_region.circuit.send(Message(
|
||||
"ForceObjectSelect",
|
||||
Block("Header", ResetList=False),
|
||||
Block("Data", LocalID=obj.LocalID),
|
||||
@@ -88,7 +88,7 @@ class BlueishObjectListGUIAddon(BaseAddon):
|
||||
))
|
||||
|
||||
def _teleport_to_object(self, session: Session, obj: Object):
|
||||
session.main_region.circuit.send_message(Message(
|
||||
session.main_region.circuit.send(Message(
|
||||
"TeleportLocationRequest",
|
||||
Block("AgentData", AgentID=session.agent_id, SessionID=session.id),
|
||||
Block(
|
||||
@@ -114,7 +114,7 @@ class BlueishObjectListGUIAddon(BaseAddon):
|
||||
region.objects.request_missing_objects()
|
||||
|
||||
def handle_object_updated(self, session: Session, region: ProxiedRegion,
|
||||
obj: Object, updated_props: Set[str]):
|
||||
obj: Object, updated_props: Set[str], msg: Optional[Message]):
|
||||
if self.blueish_model is None:
|
||||
return
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from hippolyzer.lib.proxy.sessions import Session
|
||||
def handle_lludp_message(session: Session, region: ProxiedRegion, message: Message):
|
||||
# addon_ctx will persist across addon reloads, use for storing data that
|
||||
# needs to survive across calls to this function
|
||||
ctx = session.addon_ctx
|
||||
ctx = session.addon_ctx[__name__]
|
||||
if message.name == "ChatFromViewer":
|
||||
chat = message["ChatData"]["Message"]
|
||||
if chat == "COUNT":
|
||||
|
||||
@@ -4,8 +4,13 @@ Helper for making deformer anims. This could have a GUI I guess.
|
||||
import dataclasses
|
||||
from typing import *
|
||||
|
||||
import numpy as np
|
||||
import transformations
|
||||
|
||||
from hippolyzer.lib.base.datatypes import Vector3, Quaternion, UUID
|
||||
from hippolyzer.lib.base.llanim import Joint, Animation, PosKeyframe, RotKeyframe
|
||||
from hippolyzer.lib.base.mesh import MeshAsset, SegmentHeaderDict, SkinSegmentDict, LLMeshSerializer
|
||||
from hippolyzer.lib.base.serialization import BufferWriter
|
||||
from hippolyzer.lib.proxy.addon_utils import show_message, BaseAddon, SessionProperty
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.commands import handle_command, Parameter
|
||||
@@ -45,6 +50,58 @@ def build_deformer(joints: Dict[str, DeformerJoint]) -> bytes:
|
||||
return anim.to_bytes()
|
||||
|
||||
|
||||
def build_mesh_deformer(joints: Dict[str, DeformerJoint]) -> bytes:
|
||||
skin_seg = SkinSegmentDict(
|
||||
joint_names=[],
|
||||
bind_shape_matrix=identity_mat4(),
|
||||
inverse_bind_matrix=[],
|
||||
alt_inverse_bind_matrix=[],
|
||||
pelvis_offset=0.0,
|
||||
lock_scale_if_joint_position=False
|
||||
)
|
||||
for joint_name, joint in joints.items():
|
||||
# We can only represent joint translations, ignore this joint if it doesn't have any.
|
||||
if not joint.pos:
|
||||
continue
|
||||
skin_seg['joint_names'].append(joint_name)
|
||||
# Inverse bind matrix isn't actually used, so we can just give it a placeholder value of the
|
||||
# identity mat4. This might break things in weird ways because the matrix isn't actually sensible.
|
||||
skin_seg['inverse_bind_matrix'].append(identity_mat4())
|
||||
# Create a flattened mat4 that only has a translation component of our joint pos
|
||||
# The viewer ignores any other component of these matrices so no point putting shear
|
||||
# or perspective or whatever :)
|
||||
joint_mat4 = pos_to_mat4(joint.pos)
|
||||
# Ask the viewer to override this joint's usual parent-relative position with our matrix
|
||||
skin_seg['alt_inverse_bind_matrix'].append(joint_mat4)
|
||||
|
||||
# Make a dummy mesh and shove our skin segment onto it. None of the tris are rigged, so the
|
||||
# viewer will freak out and refuse to display the tri, only the joint translations will be used.
|
||||
# Supposedly a mesh with a `skin` segment but no weights on the material should just result in an
|
||||
# effectively unrigged material, but that's not the case. Oh well.
|
||||
mesh = MeshAsset.make_triangle()
|
||||
mesh.header['skin'] = SegmentHeaderDict(offset=0, size=0)
|
||||
mesh.segments['skin'] = skin_seg
|
||||
|
||||
writer = BufferWriter("!")
|
||||
writer.write(LLMeshSerializer(), mesh)
|
||||
return writer.copy_buffer()
|
||||
|
||||
|
||||
def identity_mat4() -> List[float]:
|
||||
"""
|
||||
Return an "Identity" mat4
|
||||
|
||||
Effectively represents a transform of no rot, no translation, no shear, no perspective
|
||||
and scaling by 1.0 on every axis.
|
||||
"""
|
||||
return list(np.identity(4).flatten('F'))
|
||||
|
||||
|
||||
def pos_to_mat4(pos: Vector3) -> List[float]:
|
||||
"""Convert a position Vector3 to a Translation Mat4"""
|
||||
return list(transformations.compose_matrix(translate=tuple(pos)).flatten('F'))
|
||||
|
||||
|
||||
class DeformerAddon(BaseAddon):
|
||||
deform_joints: Dict[str, DeformerJoint] = SessionProperty(dict)
|
||||
|
||||
@@ -95,7 +152,7 @@ class DeformerAddon(BaseAddon):
|
||||
local_anim.LocalAnimAddon.apply_local_anim(session, region, "deformer_addon", anim_data)
|
||||
|
||||
def handle_rlv_command(self, session: Session, region: ProxiedRegion, source: UUID,
|
||||
cmd: str, options: List[str], param: str):
|
||||
behaviour: str, options: List[str], param: str):
|
||||
# An object in-world can also tell the client how to deform itself via
|
||||
# RLV-style commands.
|
||||
|
||||
@@ -103,9 +160,9 @@ class DeformerAddon(BaseAddon):
|
||||
if param != "force":
|
||||
return
|
||||
|
||||
if cmd == "stop_deforming":
|
||||
if behaviour == "stop_deforming":
|
||||
self.deform_joints.clear()
|
||||
elif cmd == "deform_joints":
|
||||
elif behaviour == "deform_joints":
|
||||
self.deform_joints.clear()
|
||||
for joint_data in options:
|
||||
joint_split = joint_data.split("|")
|
||||
@@ -118,5 +175,41 @@ class DeformerAddon(BaseAddon):
|
||||
self._reapply_deformer(session, region)
|
||||
return True
|
||||
|
||||
@handle_command()
|
||||
async def save_deformer_as_mesh(self, _session: Session, _region: ProxiedRegion):
|
||||
"""
|
||||
Export the deformer as a crafted rigged mesh rather than an animation
|
||||
|
||||
Mesh deformers have the advantage that they don't cause your joints to "stick"
|
||||
like animations do when using animations with pos keyframes.
|
||||
"""
|
||||
filename = await AddonManager.UI.save_file(filter_str="LL Mesh (*.llmesh)")
|
||||
if not filename:
|
||||
return
|
||||
with open(filename, "wb") as f:
|
||||
f.write(build_mesh_deformer(self.deform_joints))
|
||||
|
||||
@handle_command()
|
||||
async def upload_deformer_as_mesh(self, _session: Session, region: ProxiedRegion):
|
||||
"""Same as save_deformer_as_mesh, but uploads the mesh directly to SL."""
|
||||
|
||||
mesh_bytes = build_mesh_deformer(self.deform_joints)
|
||||
try:
|
||||
# Send off mesh to calculate upload cost
|
||||
upload_token = await region.asset_uploader.initiate_mesh_upload("deformer", mesh_bytes)
|
||||
except Exception as e:
|
||||
show_message(e)
|
||||
raise
|
||||
|
||||
if not await AddonManager.UI.confirm("Upload", f"Spend {upload_token.linden_cost}L on upload?"):
|
||||
return
|
||||
|
||||
# Do the actual upload
|
||||
try:
|
||||
await region.asset_uploader.complete_upload(upload_token)
|
||||
except Exception as e:
|
||||
show_message(e)
|
||||
raise
|
||||
|
||||
|
||||
addons = [DeformerAddon()]
|
||||
|
||||
158
addon_examples/demo_autoattacher.py
Normal file
158
addon_examples/demo_autoattacher.py
Normal file
@@ -0,0 +1,158 @@
|
||||
"""
|
||||
Detect receipt of a marketplace order for a demo, and auto-attach the most appropriate object
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
from typing import List, Tuple, Dict, Optional, Sequence
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.message.message import Message, Block
|
||||
from hippolyzer.lib.base.templates import InventoryType, Permissions, FolderType
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon, show_message
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import Session
|
||||
|
||||
|
||||
MARKETPLACE_TRANSACTION_ID = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
|
||||
|
||||
|
||||
class DemoAutoAttacher(BaseAddon):
|
||||
def handle_eq_event(self, session: Session, region: ProxiedRegion, event: dict):
|
||||
if event["message"] != "BulkUpdateInventory":
|
||||
return
|
||||
# Check that this update even possibly came from the marketplace
|
||||
if event["body"]["AgentData"][0]["TransactionID"] != MARKETPLACE_TRANSACTION_ID:
|
||||
return
|
||||
# Make sure that the transaction targeted our real received items folder
|
||||
folders = event["body"]["FolderData"]
|
||||
received_folder = folders[0]
|
||||
if received_folder["Name"] != "Received Items":
|
||||
return
|
||||
skel = session.login_data['inventory-skeleton']
|
||||
actual_received = [x for x in skel if x['type_default'] == FolderType.INBOX]
|
||||
assert actual_received
|
||||
if UUID(actual_received[0]['folder_id']) != received_folder["FolderID"]:
|
||||
show_message(f"Strange received folder ID spoofing? {folders!r}")
|
||||
return
|
||||
|
||||
if not re.match(r".*\bdemo\b.*", folders[1]["Name"], flags=re.I):
|
||||
return
|
||||
# Alright, so we have a demo... thing from the marketplace. What now?
|
||||
items = event["body"]["ItemData"]
|
||||
object_items = [x for x in items if x["InvType"] == InventoryType.OBJECT]
|
||||
if not object_items:
|
||||
return
|
||||
self._schedule_task(self._attach_best_object(session, region, object_items))
|
||||
|
||||
async def _attach_best_object(self, session: Session, region: ProxiedRegion, object_items: List[Dict]):
|
||||
own_body_type = await self._guess_own_body(session, region)
|
||||
show_message(f"Trying to find demo for {own_body_type}")
|
||||
guess_patterns = self.BODY_CLOTHING_PATTERNS.get(own_body_type)
|
||||
to_attach = []
|
||||
if own_body_type and guess_patterns:
|
||||
matching_items = self._get_matching_items(object_items, guess_patterns)
|
||||
if matching_items:
|
||||
# Only take the first one
|
||||
to_attach.append(matching_items[0])
|
||||
if not to_attach:
|
||||
# Don't know what body's being used or couldn't figure out what item
|
||||
# would work best with our body. Just attach the first object in the folder.
|
||||
to_attach.append(object_items[0])
|
||||
|
||||
# Also attach whatever HUDs, maybe we need them.
|
||||
for hud in self._get_matching_items(object_items, ("hud",)):
|
||||
if hud not in to_attach:
|
||||
to_attach.append(hud)
|
||||
|
||||
region.circuit.send(Message(
|
||||
'RezMultipleAttachmentsFromInv',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id),
|
||||
Block('HeaderData', CompoundMsgID=UUID.random(), TotalObjects=len(to_attach), FirstDetachAll=0),
|
||||
*[Block(
|
||||
'ObjectData',
|
||||
ItemID=o["ItemID"],
|
||||
OwnerID=session.agent_id,
|
||||
# 128 = "add", uses whatever attachmentpt was defined on the object
|
||||
AttachmentPt=128,
|
||||
ItemFlags_=(),
|
||||
GroupMask_=(),
|
||||
EveryoneMask_=(),
|
||||
NextOwnerMask_=(Permissions.COPY | Permissions.MOVE),
|
||||
Name=o["Name"],
|
||||
Description=o["Description"],
|
||||
) for o in to_attach]
|
||||
))
|
||||
|
||||
def _get_matching_items(self, items: List[dict], patterns: Sequence[str]):
|
||||
# Loop over patterns to search for our body type, in order of preference
|
||||
matched = []
|
||||
for guess_pattern in patterns:
|
||||
# Check each item for that pattern
|
||||
for item in items:
|
||||
if re.match(rf".*\b{guess_pattern}\b.*", item["Name"], re.I):
|
||||
matched.append(item)
|
||||
return matched
|
||||
|
||||
# We scan the agent's attached objects to guess what kind of body they use
|
||||
BODY_PREFIXES = {
|
||||
"-Belleza- Jake ": "jake",
|
||||
"-Belleza- Freya ": "freya",
|
||||
"-Belleza- Isis ": "isis",
|
||||
"-Belleza- Venus ": "venus",
|
||||
"[Signature] Gianni Body": "gianni",
|
||||
"[Signature] Geralt Body": "geralt",
|
||||
"Maitreya Mesh Body - Lara": "maitreya",
|
||||
"Slink Physique Hourglass Petite": "hg_petite",
|
||||
"Slink Physique Mesh Body Hourglass": "hourglass",
|
||||
"Slink Physique Original Petite": "phys_petite",
|
||||
"Slink Physique Mesh Body Original": "physique",
|
||||
"[BODY] Legacy (f)": "legacy_f",
|
||||
"[BODY] Legacy (m)": "legacy_m",
|
||||
"[Signature] Alice Body": "sig_alice",
|
||||
"Slink Physique MALE Mesh Body": "slink_male",
|
||||
"AESTHETIC - [Mesh Body]": "aesthetic",
|
||||
}
|
||||
|
||||
# Different bodies' clothes have different naming conventions according to different merchants.
|
||||
# These are common naming patterns we use to choose objects to attach, in order of preference.
|
||||
BODY_CLOTHING_PATTERNS: Dict[str, Tuple[str, ...]] = {
|
||||
"jake": ("jake", "belleza"),
|
||||
"freya": ("freya", "belleza"),
|
||||
"isis": ("isis", "belleza"),
|
||||
"venus": ("venus", "belleza"),
|
||||
"gianni": ("gianni", "signature", "sig"),
|
||||
"geralt": ("geralt", "signature", "sig"),
|
||||
"hg_petite": ("hourglass petite", "hg petite", "hourglass", "hg", "slink"),
|
||||
"hourglass": ("hourglass", "hg", "slink"),
|
||||
"phys_petite": ("physique petite", "phys petite", "physique", "phys", "slink"),
|
||||
"physique": ("physique", "phys", "slink"),
|
||||
"legacy_f": ("legacy",),
|
||||
"legacy_m": ("legacy",),
|
||||
"sig_alice": ("alice", "signature"),
|
||||
"slink_male": ("physique", "slink"),
|
||||
"aesthetic": ("aesthetic",),
|
||||
}
|
||||
|
||||
async def _guess_own_body(self, session: Session, region: ProxiedRegion) -> Optional[str]:
|
||||
agent_obj = region.objects.lookup_fullid(session.agent_id)
|
||||
if not agent_obj:
|
||||
return None
|
||||
# We probably won't know the names for all of our attachments, request them.
|
||||
# Could be obviated by looking at the COF, not worth it for this.
|
||||
try:
|
||||
await asyncio.wait(region.objects.request_object_properties(agent_obj.Children), timeout=0.5)
|
||||
except asyncio.TimeoutError:
|
||||
# We expect that we just won't ever receive some property requests, that's fine
|
||||
pass
|
||||
|
||||
for prefix, body_type in self.BODY_PREFIXES.items():
|
||||
for obj in agent_obj.Children:
|
||||
if not obj.Name:
|
||||
continue
|
||||
if obj.Name.startswith(prefix):
|
||||
return body_type
|
||||
return None
|
||||
|
||||
|
||||
addons = [DemoAutoAttacher()]
|
||||
119
addon_examples/get_task_inventory_cap.py
Normal file
119
addon_examples/get_task_inventory_cap.py
Normal file
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
Loading task inventory doesn't actually need to be slow.
|
||||
|
||||
By using a cap instead of the slow xfer path and sending the LLSD inventory
|
||||
model we get 15x speedups even when mocking things behind the scenes by using
|
||||
a hacked up version of xfer. See turbo_object_inventory.py
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
import asgiref.wsgi
|
||||
from typing import *
|
||||
|
||||
from flask import Flask, Response, request
|
||||
|
||||
from hippolyzer.lib.base import llsd
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.inventory import InventoryModel, InventoryObject
|
||||
from hippolyzer.lib.base.message.message import Message, Block
|
||||
from hippolyzer.lib.base.templates import XferFilePath, AssetType
|
||||
from hippolyzer.lib.proxy import addon_ctx
|
||||
from hippolyzer.lib.proxy.webapp_cap_addon import WebAppCapAddon
|
||||
|
||||
app = Flask("GetTaskInventoryCapApp")
|
||||
|
||||
|
||||
@app.route('/', methods=["GET"])
|
||||
async def get_task_inventory():
|
||||
# Should always have the current region, the cap handler is bound to one.
|
||||
# Just need to pull it from the `addon_ctx` module's global.
|
||||
region = addon_ctx.region.get()
|
||||
session = addon_ctx.session.get()
|
||||
obj_id = UUID(request.args["task_id"])
|
||||
obj = region.objects.lookup_fullid(obj_id)
|
||||
if not obj:
|
||||
return Response(f"Couldn't find {obj_id}", status=404, mimetype="text/plain")
|
||||
request_msg = Message(
|
||||
'RequestTaskInventory',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id),
|
||||
Block('InventoryData', LocalID=obj.LocalID),
|
||||
)
|
||||
# Keep around a dict of chunks we saw previously in case we have to restart
|
||||
# an Xfer due to missing chunks. We don't expect chunks to change across Xfers
|
||||
# so this can be used to recover from dropped SendXferPackets in subsequent attempts
|
||||
existing_chunks: Dict[int, bytes] = {}
|
||||
for _ in range(3):
|
||||
# Any previous requests will have triggered a delete of the inventory file
|
||||
# by marking it complete on the server-side. Re-send our RequestTaskInventory
|
||||
# To make sure there's a fresh copy.
|
||||
region.circuit.send(request_msg.take())
|
||||
inv_message = await region.message_handler.wait_for(
|
||||
('ReplyTaskInventory',),
|
||||
predicate=lambda x: x["InventoryData"]["TaskID"] == obj.FullID,
|
||||
timeout=5.0,
|
||||
)
|
||||
# No task inventory, send the reply as-is
|
||||
file_name = inv_message["InventoryData"]["Filename"]
|
||||
if not file_name:
|
||||
# The "Contents" folder always has to be there, if we don't put it here
|
||||
# then the viewer will have to lie about it being there itself.
|
||||
return Response(
|
||||
llsd.format_xml({
|
||||
"inventory": [
|
||||
InventoryObject(
|
||||
name="Contents",
|
||||
parent_id=UUID.ZERO,
|
||||
type=AssetType.CATEGORY,
|
||||
obj_id=obj_id
|
||||
).to_llsd()
|
||||
],
|
||||
"inv_serial": inv_message["InventoryData"]["Serial"],
|
||||
}),
|
||||
headers={"Content-Type": "application/llsd+xml"},
|
||||
status=200,
|
||||
)
|
||||
|
||||
last_serial = request.args.get("last_serial", None)
|
||||
if last_serial:
|
||||
last_serial = int(last_serial)
|
||||
if inv_message["InventoryData"]["Serial"] == last_serial:
|
||||
# Nothing has changed since the version of the inventory they say they have, say so.
|
||||
return Response("", status=304)
|
||||
|
||||
xfer = region.xfer_manager.request(
|
||||
file_name=file_name,
|
||||
file_path=XferFilePath.CACHE,
|
||||
turbo=True,
|
||||
)
|
||||
xfer.chunks.update(existing_chunks)
|
||||
try:
|
||||
await xfer
|
||||
except asyncio.TimeoutError:
|
||||
# We likely failed the request due to missing chunks, store
|
||||
# the chunks that we _did_ get for the next attempt.
|
||||
existing_chunks.update(xfer.chunks)
|
||||
continue
|
||||
|
||||
inv_model = InventoryModel.from_str(xfer.reassemble_chunks().decode("utf8"))
|
||||
|
||||
return Response(
|
||||
llsd.format_xml({
|
||||
"inventory": inv_model.to_llsd(),
|
||||
"inv_serial": inv_message["InventoryData"]["Serial"],
|
||||
}),
|
||||
headers={"Content-Type": "application/llsd+xml"},
|
||||
)
|
||||
raise asyncio.TimeoutError("Failed to get inventory after 3 tries")
|
||||
|
||||
|
||||
class GetTaskInventoryCapExampleAddon(WebAppCapAddon):
|
||||
# A cap URL with this name will be tied to each region when
|
||||
# the sim is first connected to. The URL will be returned to the
|
||||
# viewer in the Seed if the viewer requests it by name.
|
||||
CAP_NAME = "GetTaskInventoryExample"
|
||||
# Any asgi app should be fine.
|
||||
APP = asgiref.wsgi.WsgiToAsgi(app)
|
||||
|
||||
|
||||
addons = [GetTaskInventoryCapExampleAddon()]
|
||||
50
addon_examples/leap_example.py
Normal file
50
addon_examples/leap_example.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""
|
||||
Example of how to control a viewer over LEAP
|
||||
|
||||
Must launch the viewer with `outleap-agent` LEAP script.
|
||||
See https://github.com/SaladDais/outleap/ for more info on LEAP / outleap.
|
||||
"""
|
||||
|
||||
import outleap
|
||||
from outleap.scripts.inspector import LEAPInspectorGUI
|
||||
|
||||
from hippolyzer.lib.proxy.addon_utils import send_chat, BaseAddon, show_message
|
||||
from hippolyzer.lib.proxy.commands import handle_command
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import Session, SessionManager
|
||||
|
||||
|
||||
# Path found using `outleap-inspector`
|
||||
FPS_PATH = outleap.UIPath("/main_view/menu_stack/status_bar_container/status/time_and_media_bg/FPSText")
|
||||
|
||||
|
||||
class LEAPExampleAddon(BaseAddon):
|
||||
async def handle_leap_client_added(self, session_manager: SessionManager, leap_client: outleap.LEAPClient):
|
||||
# You can do things as soon as the LEAP client connects, like if you want to automate
|
||||
# login or whatever.
|
||||
viewer_control_api = outleap.LLViewerControlAPI(leap_client)
|
||||
# Ask for a config value and print it in the viewer logs
|
||||
print(await viewer_control_api.get("Global", "StatsPilotFile"))
|
||||
|
||||
@handle_command()
|
||||
async def show_ui_inspector(self, session: Session, _region: ProxiedRegion):
|
||||
"""Spawn a GUI for inspecting the UI state"""
|
||||
if not session.leap_client:
|
||||
show_message("No LEAP client connected?")
|
||||
return
|
||||
LEAPInspectorGUI(session.leap_client).show()
|
||||
|
||||
@handle_command()
|
||||
async def say_fps(self, session: Session, _region: ProxiedRegion):
|
||||
"""Say your current FPS in chat"""
|
||||
if not session.leap_client:
|
||||
show_message("No LEAP client connected?")
|
||||
return
|
||||
|
||||
window_api = outleap.LLWindowAPI(session.leap_client)
|
||||
fps = (await window_api.get_info(path=FPS_PATH))['value']
|
||||
|
||||
send_chat(f"LEAP says I'm running at {fps} FPS!")
|
||||
|
||||
|
||||
addons = [LEAPExampleAddon()]
|
||||
@@ -20,15 +20,17 @@ bulk upload, like changing priority or removing a joint.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import logging
|
||||
import pathlib
|
||||
from abc import abstractmethod
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base import serialization as se
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.helpers import get_mtime
|
||||
from hippolyzer.lib.base.llanim import Animation
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.base.message.msgtypes import PacketFlags
|
||||
from hippolyzer.lib.proxy import addon_ctx
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon, SessionProperty, GlobalProperty, show_message
|
||||
@@ -39,13 +41,6 @@ from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import Session, SessionManager
|
||||
|
||||
|
||||
def _get_mtime(path: str):
|
||||
try:
|
||||
return os.stat(path).st_mtime
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
class LocalAnimAddon(BaseAddon):
|
||||
# name -> path, only for anims actually from files
|
||||
local_anim_paths: Dict[str, str] = SessionProperty(dict)
|
||||
@@ -112,19 +107,22 @@ class LocalAnimAddon(BaseAddon):
|
||||
if not anim_id:
|
||||
continue
|
||||
# is playing right now, check if there's a newer version
|
||||
self.apply_local_anim_from_file(session, region, anim_name, only_if_changed=True)
|
||||
try:
|
||||
self.apply_local_anim_from_file(session, region, anim_name, only_if_changed=True)
|
||||
except Exception:
|
||||
logging.exception("Exploded while replaying animation")
|
||||
await asyncio.sleep(1.0)
|
||||
|
||||
def handle_rlv_command(self, session: Session, region: ProxiedRegion, source: UUID,
|
||||
cmd: str, options: List[str], param: str):
|
||||
behaviour: str, options: List[str], param: str):
|
||||
# We only handle commands
|
||||
if param != "force":
|
||||
return
|
||||
|
||||
if cmd == "stop_local_anim":
|
||||
if behaviour == "stop_local_anim":
|
||||
self.apply_local_anim(session, region, options[0], new_data=None)
|
||||
return True
|
||||
elif cmd == "start_local_anim":
|
||||
elif behaviour == "start_local_anim":
|
||||
self.apply_local_anim_from_file(session, region, options[0])
|
||||
return True
|
||||
|
||||
@@ -140,6 +138,7 @@ class LocalAnimAddon(BaseAddon):
|
||||
AgentID=session.agent_id,
|
||||
SessionID=session.id,
|
||||
),
|
||||
flags=PacketFlags.RELIABLE,
|
||||
)
|
||||
|
||||
# Stop any old version of the anim that might be playing first
|
||||
@@ -166,7 +165,7 @@ class LocalAnimAddon(BaseAddon):
|
||||
cls.local_anim_playing_ids.pop(anim_name, None)
|
||||
cls.local_anim_bytes.pop(anim_name, None)
|
||||
|
||||
region.circuit.send_message(new_msg)
|
||||
region.circuit.send(new_msg)
|
||||
print(f"Changing {anim_name} to {next_id}")
|
||||
|
||||
@classmethod
|
||||
@@ -176,11 +175,10 @@ class LocalAnimAddon(BaseAddon):
|
||||
anim_data = None
|
||||
if anim_path:
|
||||
old_mtime = cls.local_anim_mtimes.get(anim_name)
|
||||
mtime = _get_mtime(anim_path)
|
||||
mtime = get_mtime(anim_path)
|
||||
if only_if_changed and old_mtime == mtime:
|
||||
return
|
||||
|
||||
cls.local_anim_mtimes[anim_name] = mtime
|
||||
# file might not even exist anymore if mtime is `None`,
|
||||
# anim will automatically stop if that happens.
|
||||
if mtime:
|
||||
@@ -192,6 +190,7 @@ class LocalAnimAddon(BaseAddon):
|
||||
with open(anim_path, "rb") as f:
|
||||
anim_data = f.read()
|
||||
anim_data = cls._mangle_anim(anim_data)
|
||||
cls.local_anim_mtimes[anim_name] = mtime
|
||||
else:
|
||||
print(f"Unknown anim {anim_name!r}")
|
||||
cls.apply_local_anim(session, region, anim_name, new_data=anim_data)
|
||||
|
||||
@@ -81,17 +81,16 @@ class MeshUploadInterceptingAddon(BaseAddon):
|
||||
|
||||
@handle_command()
|
||||
async def set_local_mesh_target(self, session: Session, region: ProxiedRegion):
|
||||
"""Set the currently selected object as the target for local mesh"""
|
||||
parent_object = region.objects.lookup_localid(session.selected.object_local)
|
||||
if not parent_object:
|
||||
"""Set the currently selected objects as the target for local mesh"""
|
||||
selected_links = [region.objects.lookup_localid(l_id) for l_id in session.selected.object_locals]
|
||||
selected_links = [o for o in selected_links if o is not None]
|
||||
if not selected_links:
|
||||
show_message("Nothing selected")
|
||||
return
|
||||
linkset_objects = [parent_object] + parent_object.Children
|
||||
|
||||
old_locals = self.local_mesh_target_locals
|
||||
self.local_mesh_target_locals = [
|
||||
x.LocalID
|
||||
for x in linkset_objects
|
||||
for x in selected_links
|
||||
if ExtraParamType.MESH in x.ExtraParams
|
||||
]
|
||||
|
||||
@@ -231,7 +230,7 @@ class MeshUploadInterceptingAddon(BaseAddon):
|
||||
show_message("Mangled upload request")
|
||||
|
||||
def handle_object_updated(self, session: Session, region: ProxiedRegion,
|
||||
obj: Object, updated_props: Set[str]):
|
||||
obj: Object, updated_props: Set[str], msg: Optional[Message]):
|
||||
if obj.LocalID not in self.local_mesh_target_locals:
|
||||
return
|
||||
if "Name" not in updated_props or obj.Name is None:
|
||||
|
||||
@@ -16,18 +16,23 @@ import local_mesh
|
||||
AddonManager.hot_reload(local_mesh, require_addons_loaded=True)
|
||||
|
||||
|
||||
def _reorient_coord(coord, orientation):
|
||||
def _reorient_coord(coord, orientation, normals=False):
|
||||
coords = []
|
||||
for axis in orientation:
|
||||
axis_idx = abs(axis) - 1
|
||||
coords.append(coord[axis_idx] if axis >= 0 else 1.0 - coord[axis_idx])
|
||||
if normals:
|
||||
# Normals have a static domain from -1.0 to 1.0, just negate.
|
||||
new_coord = coord[axis_idx] if axis >= 0 else -coord[axis_idx]
|
||||
else:
|
||||
new_coord = coord[axis_idx] if axis >= 0 else 1.0 - coord[axis_idx]
|
||||
coords.append(new_coord)
|
||||
if coord.__class__ in (list, tuple):
|
||||
return coord.__class__(coords)
|
||||
return coord.__class__(*coords)
|
||||
|
||||
|
||||
def _reorient_coord_list(coord_list, orientation):
|
||||
return [_reorient_coord(x, orientation) for x in coord_list]
|
||||
def _reorient_coord_list(coord_list, orientation, normals=False):
|
||||
return [_reorient_coord(x, orientation, normals) for x in coord_list]
|
||||
|
||||
|
||||
def reorient_mesh(orientation):
|
||||
@@ -42,7 +47,7 @@ def reorient_mesh(orientation):
|
||||
# flipping the axes around.
|
||||
material["Position"] = _reorient_coord_list(material["Position"], orientation)
|
||||
# Are you even supposed to do this to the normals?
|
||||
material["Normal"] = _reorient_coord_list(material["Normal"], orientation)
|
||||
material["Normal"] = _reorient_coord_list(material["Normal"], orientation, normals=True)
|
||||
return mesh
|
||||
return _reorienter
|
||||
|
||||
|
||||
@@ -126,14 +126,14 @@ class MessageMirrorAddon(BaseAddon):
|
||||
|
||||
# Send the message normally first if we're mirroring
|
||||
if message.name in MIRROR:
|
||||
region.circuit.send_message(message)
|
||||
region.circuit.send(message)
|
||||
|
||||
# We're going to send the message on a new circuit, we need to take
|
||||
# it so we get a new packet ID and clean ACKs
|
||||
message = message.take()
|
||||
|
||||
self._lludp_fixups(target_session, message)
|
||||
target_region.circuit.send_message(message)
|
||||
target_region.circuit.send(message)
|
||||
return True
|
||||
|
||||
def _lludp_fixups(self, target_session: Session, message: Message):
|
||||
@@ -206,7 +206,7 @@ class MessageMirrorAddon(BaseAddon):
|
||||
return
|
||||
caps_source = target_region
|
||||
|
||||
new_base_url = caps_source.caps.get(cap_data.cap_name)
|
||||
new_base_url = caps_source.cap_urls.get(cap_data.cap_name)
|
||||
if not new_base_url:
|
||||
print("No equiv cap?")
|
||||
return
|
||||
|
||||
49
addon_examples/mock_proxy_cap.py
Normal file
49
addon_examples/mock_proxy_cap.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
Example of proxy-provided caps
|
||||
|
||||
Useful for mocking out a cap that isn't actually implemented by the server
|
||||
while developing the viewer-side pieces of it.
|
||||
|
||||
Implements a cap that accepts an `obj_id` UUID query parameter and returns
|
||||
the name of the object.
|
||||
"""
|
||||
import asyncio
|
||||
import asgiref.wsgi
|
||||
|
||||
from flask import Flask, Response, request
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.proxy import addon_ctx
|
||||
from hippolyzer.lib.proxy.webapp_cap_addon import WebAppCapAddon
|
||||
|
||||
app = Flask("GetObjectNameCapApp")
|
||||
|
||||
|
||||
@app.route('/')
|
||||
async def get_object_name():
|
||||
# Should always have the current region, the cap handler is bound to one.
|
||||
# Just need to pull it from the `addon_ctx` module's global.
|
||||
obj_mgr = addon_ctx.region.get().objects
|
||||
obj_id = UUID(request.args['obj_id'])
|
||||
obj = obj_mgr.lookup_fullid(obj_id)
|
||||
if not obj:
|
||||
return Response(f"Couldn't find {obj_id!r}", status=404, mimetype="text/plain")
|
||||
|
||||
try:
|
||||
await asyncio.wait_for(obj_mgr.request_object_properties(obj)[0], 1.0)
|
||||
except asyncio.TimeoutError:
|
||||
return Response(f"Timed out requesting {obj_id!r}'s properties", status=500, mimetype="text/plain")
|
||||
|
||||
return Response(obj.Name, mimetype="text/plain")
|
||||
|
||||
|
||||
class MockProxyCapExampleAddon(WebAppCapAddon):
|
||||
# A cap URL with this name will be tied to each region when
|
||||
# the sim is first connected to. The URL will be returned to the
|
||||
# viewer in the Seed if the viewer requests it by name.
|
||||
CAP_NAME = "GetObjectNameExample"
|
||||
# Any asgi app should be fine.
|
||||
APP = asgiref.wsgi.WsgiToAsgi(app)
|
||||
|
||||
|
||||
addons = [MockProxyCapExampleAddon()]
|
||||
@@ -27,7 +27,7 @@ from mitmproxy.http import HTTPFlow
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.jp2_utils import BufferedJp2k
|
||||
from hippolyzer.lib.base.multiprocessing_utils import ParentProcessWatcher
|
||||
from hippolyzer.lib.base.templates import TextureEntry
|
||||
from hippolyzer.lib.base.templates import TextureEntryCollection
|
||||
from hippolyzer.lib.proxy.addon_utils import AssetAliasTracker, BaseAddon, GlobalProperty, AddonProcess
|
||||
from hippolyzer.lib.proxy.http_flow import HippoHTTPFlow
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
@@ -148,7 +148,7 @@ class MonochromeAddon(BaseAddon):
|
||||
message["RegionInfo"][field_name] = tracker.get_alias_uuid(val)
|
||||
|
||||
@staticmethod
|
||||
def _make_te_monochrome(tracker: AssetAliasTracker, parsed_te: TextureEntry):
|
||||
def _make_te_monochrome(tracker: AssetAliasTracker, parsed_te: TextureEntryCollection):
|
||||
# Need a deepcopy because TEs are owned by the ObjectManager
|
||||
# and we don't want to change the canonical view.
|
||||
parsed_te = copy.deepcopy(parsed_te)
|
||||
|
||||
111
addon_examples/object_management_validator.py
Normal file
111
addon_examples/object_management_validator.py
Normal file
@@ -0,0 +1,111 @@
|
||||
"""
|
||||
Check object manager state against region ViewerObject cache
|
||||
|
||||
Can't look at every object we've tracked and every object in VOCache
|
||||
and report mismatches due to weird VOCache cache eviction criteria and certain
|
||||
cacheable objects not being added to the VOCache.
|
||||
|
||||
Off the top of my head, animesh objects get explicit KillObjects at extreme
|
||||
view distances same as avatars, but will still be present in the cache even
|
||||
though they will not be in gObjectList.
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.objects import normalize_object_update_compressed_data
|
||||
from hippolyzer.lib.base.templates import ObjectUpdateFlags, PCode
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon, GlobalProperty
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import SessionManager, Session
|
||||
from hippolyzer.lib.proxy.vocache import is_valid_vocache_dir, RegionViewerObjectCacheChain
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ObjectManagementValidator(BaseAddon):
|
||||
base_cache_path: Optional[str] = GlobalProperty(None)
|
||||
orig_auto_request: Optional[bool] = GlobalProperty(None)
|
||||
|
||||
def handle_init(self, session_manager: SessionManager):
|
||||
if self.orig_auto_request is None:
|
||||
self.orig_auto_request = session_manager.settings.ALLOW_AUTO_REQUEST_OBJECTS
|
||||
session_manager.settings.ALLOW_AUTO_REQUEST_OBJECTS = False
|
||||
|
||||
async def _choose_cache_path():
|
||||
while not self.base_cache_path:
|
||||
cache_dir = await AddonManager.UI.open_dir("Choose the base cache directory")
|
||||
if not cache_dir:
|
||||
return
|
||||
if not is_valid_vocache_dir(cache_dir):
|
||||
continue
|
||||
self.base_cache_path = cache_dir
|
||||
|
||||
if not self.base_cache_path:
|
||||
self._schedule_task(_choose_cache_path(), session_scoped=False)
|
||||
|
||||
def handle_unload(self, session_manager: SessionManager):
|
||||
session_manager.settings.ALLOW_AUTO_REQUEST_OBJECTS = self.orig_auto_request
|
||||
|
||||
def handle_session_init(self, session: Session):
|
||||
# Use only the specified cache path for the vocache
|
||||
session.cache_dir = self.base_cache_path
|
||||
|
||||
def handle_lludp_message(self, session: Session, region: ProxiedRegion, message: Message):
|
||||
if message.name != "DisableSimulator":
|
||||
return
|
||||
# Send it off to the client without handling it normally,
|
||||
# we need to defer region teardown in the proxy
|
||||
region.circuit.send(message)
|
||||
self._schedule_task(self._check_cache_before_region_teardown(region))
|
||||
return True
|
||||
|
||||
async def _check_cache_before_region_teardown(self, region: ProxiedRegion):
|
||||
await asyncio.sleep(0.5)
|
||||
print("Ok, checking cache differences")
|
||||
try:
|
||||
# Index will have been rewritten, so re-read it.
|
||||
region_cache_chain = RegionViewerObjectCacheChain.for_region(
|
||||
handle=region.handle,
|
||||
cache_id=region.cache_id,
|
||||
cache_dir=self.base_cache_path
|
||||
)
|
||||
if not region_cache_chain.region_caches:
|
||||
print(f"no caches for {region!r}?")
|
||||
return
|
||||
all_full_ids = set()
|
||||
for obj in region.objects.all_objects:
|
||||
cacheable = True
|
||||
orig_obj = obj
|
||||
# Walk along the ancestry checking for things that would make the tree non-cacheable
|
||||
while obj is not None:
|
||||
if obj.UpdateFlags & ObjectUpdateFlags.TEMPORARY_ON_REZ:
|
||||
cacheable = False
|
||||
if obj.PCode == PCode.AVATAR:
|
||||
cacheable = False
|
||||
obj = obj.Parent
|
||||
if cacheable:
|
||||
all_full_ids.add(orig_obj.FullID)
|
||||
|
||||
for key in all_full_ids:
|
||||
obj = region.objects.lookup_fullid(key)
|
||||
cached_data = region_cache_chain.lookup_object_data(obj.LocalID, obj.CRC)
|
||||
if not cached_data:
|
||||
continue
|
||||
orig_dict = obj.to_dict()
|
||||
parsed_data = normalize_object_update_compressed_data(cached_data)
|
||||
updated = obj.update_properties(parsed_data)
|
||||
# Can't compare this yet
|
||||
updated -= {"TextureEntry"}
|
||||
if updated:
|
||||
print(key)
|
||||
for attr in updated:
|
||||
print("\t", attr, orig_dict[attr], parsed_data[attr])
|
||||
finally:
|
||||
# Ok to teardown region in the proxy now
|
||||
region.mark_dead()
|
||||
|
||||
|
||||
addons = [ObjectManagementValidator()]
|
||||
@@ -10,6 +10,7 @@ before you start tracking can help too.
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.objects import Object
|
||||
from hippolyzer.lib.base.templates import PCode
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon, show_message, SessionProperty
|
||||
@@ -20,7 +21,7 @@ from hippolyzer.lib.proxy.sessions import Session
|
||||
|
||||
class ObjectUpdateBlameAddon(BaseAddon):
|
||||
update_blame_counter: Counter[UUID] = SessionProperty(Counter)
|
||||
track_update_blame: bool = SessionProperty(False)
|
||||
should_track_update_blame: bool = SessionProperty(False)
|
||||
|
||||
@handle_command()
|
||||
async def precache_objects(self, _session: Session, region: ProxiedRegion):
|
||||
@@ -38,11 +39,11 @@ class ObjectUpdateBlameAddon(BaseAddon):
|
||||
|
||||
@handle_command()
|
||||
async def track_update_blame(self, _session: Session, _region: ProxiedRegion):
|
||||
self.track_update_blame = True
|
||||
self.should_track_update_blame = True
|
||||
|
||||
@handle_command()
|
||||
async def untrack_update_blame(self, _session: Session, _region: ProxiedRegion):
|
||||
self.track_update_blame = False
|
||||
self.should_track_update_blame = False
|
||||
|
||||
@handle_command()
|
||||
async def clear_update_blame(self, _session: Session, _region: ProxiedRegion):
|
||||
@@ -57,8 +58,8 @@ class ObjectUpdateBlameAddon(BaseAddon):
|
||||
print(f"{obj_id} ({name!r}): {count}")
|
||||
|
||||
def handle_object_updated(self, session: Session, region: ProxiedRegion,
|
||||
obj: Object, updated_props: Set[str]):
|
||||
if not self.track_update_blame:
|
||||
obj: Object, updated_props: Set[str], msg: Optional[Message]):
|
||||
if not self.should_track_update_blame:
|
||||
return
|
||||
if region != session.main_region:
|
||||
return
|
||||
|
||||
21
addon_examples/packet_stats.py
Normal file
21
addon_examples/packet_stats.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import collections
|
||||
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon, GlobalProperty
|
||||
from hippolyzer.lib.proxy.commands import handle_command
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import Session
|
||||
|
||||
|
||||
class PacketStatsAddon(BaseAddon):
|
||||
packet_stats: collections.Counter = GlobalProperty(collections.Counter)
|
||||
|
||||
def handle_lludp_message(self, session: Session, region: ProxiedRegion, message: Message):
|
||||
self.packet_stats[message.name] += 1
|
||||
|
||||
@handle_command()
|
||||
async def print_packet_stats(self, _session: Session, _region: ProxiedRegion):
|
||||
print(self.packet_stats.most_common(10))
|
||||
|
||||
|
||||
addons = [PacketStatsAddon()]
|
||||
@@ -37,7 +37,7 @@ class PaydayAddon(BaseAddon):
|
||||
chat_type=ChatType.SHOUT,
|
||||
)
|
||||
# Do the traditional money dance.
|
||||
session.main_region.circuit.send_message(Message(
|
||||
session.main_region.circuit.send(Message(
|
||||
"AgentAnimation",
|
||||
Block("AgentData", AgentID=session.agent_id, SessionID=session.id),
|
||||
Block("AnimationList", AnimID=UUID("928cae18-e31d-76fd-9cc9-2f55160ff818"), StartAnim=True),
|
||||
|
||||
@@ -9,13 +9,14 @@ import asyncio
|
||||
import struct
|
||||
from typing import *
|
||||
|
||||
from PySide2.QtGui import QImage
|
||||
from PySide6.QtGui import QImage
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID, Vector3, Quaternion
|
||||
from hippolyzer.lib.base.helpers import to_chunks
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.base.templates import ObjectUpdateFlags, PCode, MCode, MultipleObjectUpdateFlags, TextureEntry
|
||||
from hippolyzer.lib.client.object_manager import ObjectEvent, UpdateType
|
||||
from hippolyzer.lib.base.templates import ObjectUpdateFlags, PCode, MCode, MultipleObjectUpdateFlags, \
|
||||
TextureEntryCollection, JUST_CREATED_FLAGS
|
||||
from hippolyzer.lib.client.object_manager import ObjectEvent, ObjectUpdateType
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.commands import handle_command
|
||||
@@ -24,7 +25,6 @@ from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import Session
|
||||
|
||||
|
||||
JUST_CREATED_FLAGS = (ObjectUpdateFlags.CREATE_SELECTED | ObjectUpdateFlags.OBJECT_YOU_OWNER)
|
||||
PRIM_SCALE = 0.2
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ class PixelArtistAddon(BaseAddon):
|
||||
return
|
||||
img = QImage()
|
||||
with open(filename, "rb") as f:
|
||||
img.loadFromData(f.read(), aformat=None)
|
||||
img.loadFromData(f.read(), format=None)
|
||||
img = img.convertToFormat(QImage.Format_RGBA8888)
|
||||
height = img.height()
|
||||
width = img.width()
|
||||
@@ -72,15 +72,14 @@ class PixelArtistAddon(BaseAddon):
|
||||
# Watch for any newly created prims, this is basically what the viewer does to find
|
||||
# prims that it just created with the build tool.
|
||||
with session.objects.events.subscribe_async(
|
||||
(UpdateType.OBJECT_UPDATE,),
|
||||
(ObjectUpdateType.UPDATE,),
|
||||
predicate=lambda e: e.object.UpdateFlags & JUST_CREATED_FLAGS and "LocalID" in e.updated
|
||||
) as get_events:
|
||||
# Create a pool of prims to use for building the pixel art
|
||||
for _ in range(needed_prims):
|
||||
# TODO: We don't track the land group or user's active group, so
|
||||
# "anyone can build" must be on for rezzing to work.
|
||||
group_id = UUID()
|
||||
region.circuit.send_message(Message(
|
||||
# TODO: Can't get land group atm, just tries to rez with the user's active group
|
||||
group_id = session.active_group
|
||||
region.circuit.send(Message(
|
||||
'ObjectAdd',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id, GroupID=group_id),
|
||||
Block(
|
||||
@@ -124,12 +123,12 @@ class PixelArtistAddon(BaseAddon):
|
||||
y = i // width
|
||||
obj = created_prims[prim_idx]
|
||||
# Set a blank texture on all faces
|
||||
te = TextureEntry()
|
||||
te = TextureEntryCollection()
|
||||
te.Textures[None] = UUID('5748decc-f629-461c-9a36-a35a221fe21f')
|
||||
# Set the prim color to the color from the pixel
|
||||
te.Color[None] = pixel_color
|
||||
# Set the prim texture and color
|
||||
region.circuit.send_message(Message(
|
||||
region.circuit.send(Message(
|
||||
'ObjectImage',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id),
|
||||
Block('ObjectData', ObjectLocalID=obj.LocalID, MediaURL=b'', TextureEntry_=te),
|
||||
@@ -149,7 +148,7 @@ class PixelArtistAddon(BaseAddon):
|
||||
|
||||
# Move the "pixels" to their correct position in chunks
|
||||
for chunk in to_chunks(positioning_blocks, 25):
|
||||
region.circuit.send_message(Message(
|
||||
region.circuit.send(Message(
|
||||
'MultipleObjectUpdate',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id),
|
||||
*chunk,
|
||||
|
||||
111
addon_examples/puppetry_example.py
Normal file
111
addon_examples/puppetry_example.py
Normal file
@@ -0,0 +1,111 @@
|
||||
"""
|
||||
Control a puppetry-enabled viewer and make your neck spin like crazy
|
||||
|
||||
It currently requires a custom rebased Firestorm with puppetry applied on top,
|
||||
and patches applied on top to make startup LEAP scripts be treated as puppetry modules.
|
||||
Basically, you probably don't want to use this yet. But hey, Puppetry is still only
|
||||
on the beta grid anyway.
|
||||
"""
|
||||
import asyncio
|
||||
import enum
|
||||
import logging
|
||||
import math
|
||||
from typing import *
|
||||
|
||||
import outleap
|
||||
|
||||
from hippolyzer.lib.base.datatypes import Quaternion
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon, SessionProperty
|
||||
from hippolyzer.lib.proxy.sessions import Session
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BodyPartMask(enum.IntFlag):
|
||||
"""Which joints to send the viewer as part of "move" puppetry command"""
|
||||
HEAD = 1 << 0
|
||||
FACE = 1 << 1
|
||||
LHAND = 1 << 2
|
||||
RHAND = 1 << 3
|
||||
FINGERS = 1 << 4
|
||||
|
||||
|
||||
def register_puppetry_command(func: Callable[[dict], Awaitable[None]]):
|
||||
"""Register a method as handling inbound puppetry commands from the viewer"""
|
||||
func._puppetry_command = True
|
||||
return func
|
||||
|
||||
|
||||
class PuppetryExampleAddon(BaseAddon):
|
||||
server_skeleton: Dict[str, Dict[str, Any]] = SessionProperty(dict)
|
||||
camera_num: int = SessionProperty(0)
|
||||
parts_active: BodyPartMask = SessionProperty(lambda: BodyPartMask(0x1F))
|
||||
puppetry_api: Optional[outleap.LLPuppetryAPI] = SessionProperty(None)
|
||||
leap_client: Optional[outleap.LEAPClient] = SessionProperty(None)
|
||||
|
||||
def handle_session_init(self, session: Session):
|
||||
if not session.leap_client:
|
||||
return
|
||||
self.puppetry_api = outleap.LLPuppetryAPI(session.leap_client)
|
||||
self.leap_client = session.leap_client
|
||||
self._schedule_task(self._serve())
|
||||
self._schedule_task(self._exorcist(session))
|
||||
|
||||
@register_puppetry_command
|
||||
async def enable_parts(self, args: dict):
|
||||
if (new_mask := args.get("parts_mask")) is not None:
|
||||
self.parts_active = BodyPartMask(new_mask)
|
||||
|
||||
@register_puppetry_command
|
||||
async def set_camera(self, args: dict):
|
||||
if (camera_num := args.get("camera_num")) is not None:
|
||||
self.camera_num = camera_num
|
||||
|
||||
@register_puppetry_command
|
||||
async def stop(self, _args: dict):
|
||||
LOG.info("Viewer asked us to stop puppetry")
|
||||
|
||||
@register_puppetry_command
|
||||
async def log(self, _args: dict):
|
||||
# Intentionally ignored, we don't care about things the viewer
|
||||
# asked us to log
|
||||
pass
|
||||
|
||||
@register_puppetry_command
|
||||
async def set_skeleton(self, args: dict):
|
||||
# Don't really care about what the viewer thinks the view of the skeleton is.
|
||||
# Just log store it.
|
||||
self.server_skeleton = args
|
||||
|
||||
async def _serve(self):
|
||||
"""Handle inbound puppetry commands from viewer in a loop"""
|
||||
async with self.leap_client.listen_scoped("puppetry.controller") as listener:
|
||||
while True:
|
||||
msg = await listener.get()
|
||||
cmd = msg["command"]
|
||||
handler = getattr(self, cmd, None)
|
||||
if handler is None or not hasattr(handler, "_puppetry_command"):
|
||||
LOG.warning(f"Unknown puppetry command {cmd!r}: {msg!r}")
|
||||
continue
|
||||
await handler(msg.get("args", {}))
|
||||
|
||||
async def _exorcist(self, session):
|
||||
"""Do the Linda Blair thing with your neck"""
|
||||
spin_rad = 0.0
|
||||
while True:
|
||||
await asyncio.sleep(0.05)
|
||||
if not session.main_region:
|
||||
continue
|
||||
# Wrap spin_rad around if necessary
|
||||
while spin_rad > math.pi:
|
||||
spin_rad -= math.pi * 2
|
||||
|
||||
# LEAP wants rot as a quaternion with just the imaginary parts.
|
||||
neck_rot = Quaternion.from_euler(0, 0, spin_rad).data(3)
|
||||
self.puppetry_api.move({
|
||||
"mNeck": {"no_constraint": True, "local_rot": neck_rot},
|
||||
})
|
||||
spin_rad += math.pi / 25
|
||||
|
||||
|
||||
addons = [PuppetryExampleAddon()]
|
||||
@@ -116,7 +116,7 @@ class RecapitatorAddon(BaseAddon):
|
||||
except:
|
||||
logging.exception("Exception while recapitating")
|
||||
# Tell the viewer about the status of its original upload
|
||||
region.circuit.send_message(Message(
|
||||
region.circuit.send(Message(
|
||||
"AssetUploadComplete",
|
||||
Block("AssetBlock", UUID=asset_id, Type=asset_block["Type"], Success=success),
|
||||
direction=Direction.IN,
|
||||
|
||||
22
addon_examples/simulate_packet_loss.py
Normal file
22
addon_examples/simulate_packet_loss.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import random
|
||||
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import Session
|
||||
|
||||
|
||||
class SimulatePacketLossAddon(BaseAddon):
|
||||
def handle_lludp_message(self, session: Session, region: ProxiedRegion, message: Message):
|
||||
# Messing with these may kill your circuit
|
||||
if message.name in {"PacketAck", "StartPingCheck", "CompletePingCheck", "UseCircuitCode",
|
||||
"CompleteAgentMovement", "AgentMovementComplete"}:
|
||||
return
|
||||
# Simulate 30% packet loss
|
||||
if random.random() > 0.7:
|
||||
# Do nothing, drop this packet on the floor
|
||||
return True
|
||||
return
|
||||
|
||||
|
||||
addons = [SimulatePacketLossAddon()]
|
||||
@@ -13,7 +13,7 @@ def _to_spongecase(val):
|
||||
|
||||
|
||||
def handle_lludp_message(session: Session, _region: ProxiedRegion, message: Message):
|
||||
ctx = session.addon_ctx
|
||||
ctx = session.addon_ctx[__name__]
|
||||
ctx.setdefault("spongecase", False)
|
||||
if message.name == "ChatFromViewer":
|
||||
chat = message["ChatData"]["Message"]
|
||||
|
||||
@@ -3,7 +3,7 @@ Example of how to request a Transfer
|
||||
"""
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.legacy_inv import InventoryModel, InventoryItem
|
||||
from hippolyzer.lib.base.inventory import InventoryModel, InventoryItem
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.base.templates import (
|
||||
AssetType,
|
||||
@@ -35,7 +35,7 @@ class TransferExampleAddon(BaseAddon):
|
||||
async def get_first_script(self, session: Session, region: ProxiedRegion):
|
||||
"""Get the contents of the first script in the selected object"""
|
||||
# Ask for the object inventory so we can find a script
|
||||
region.circuit.send_message(Message(
|
||||
region.circuit.send(Message(
|
||||
'RequestTaskInventory',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id),
|
||||
Block('InventoryData', LocalID=session.selected.object_local),
|
||||
@@ -47,7 +47,7 @@ class TransferExampleAddon(BaseAddon):
|
||||
file_name=inv_message["InventoryData"]["Filename"], file_path=XferFilePath.CACHE)
|
||||
inv_model = InventoryModel.from_bytes(xfer.reassemble_chunks())
|
||||
first_script: Optional[InventoryItem] = None
|
||||
for item in inv_model.items.values():
|
||||
for item in inv_model.all_items:
|
||||
if item.type == "lsltext":
|
||||
first_script = item
|
||||
if not first_script:
|
||||
|
||||
@@ -64,12 +64,12 @@ class TurboObjectInventoryAddon(BaseAddon):
|
||||
# Any previous requests will have triggered a delete of the inventory file
|
||||
# by marking it complete on the server-side. Re-send our RequestTaskInventory
|
||||
# To make sure there's a fresh copy.
|
||||
region.circuit.send_message(request_msg.take())
|
||||
region.circuit.send(request_msg.take())
|
||||
inv_message = await region.message_handler.wait_for(('ReplyTaskInventory',), timeout=5.0)
|
||||
# No task inventory, send the reply as-is
|
||||
file_name = inv_message["InventoryData"]["Filename"]
|
||||
if not file_name:
|
||||
region.circuit.send_message(inv_message)
|
||||
region.circuit.send(inv_message)
|
||||
return
|
||||
|
||||
xfer = region.xfer_manager.request(
|
||||
@@ -87,7 +87,7 @@ class TurboObjectInventoryAddon(BaseAddon):
|
||||
continue
|
||||
|
||||
# Send the original ReplyTaskInventory to the viewer so it knows the file is ready
|
||||
region.circuit.send_message(inv_message)
|
||||
region.circuit.send(inv_message)
|
||||
proxied_xfer = Xfer(data=xfer.reassemble_chunks())
|
||||
|
||||
# Wait for the viewer to request the inventory file
|
||||
|
||||
@@ -2,21 +2,17 @@
|
||||
Example of how to upload assets, assumes assets are already encoded
|
||||
in the appropriate format.
|
||||
|
||||
/524 upload <asset type>
|
||||
/524 upload_asset <asset type>
|
||||
"""
|
||||
import pprint
|
||||
from pathlib import Path
|
||||
from typing import *
|
||||
|
||||
import aiohttp
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.base.mesh import LLMeshSerializer
|
||||
from hippolyzer.lib.base.serialization import BufferReader
|
||||
from hippolyzer.lib.base.templates import AssetType
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.addon_utils import ais_item_to_inventory_data, show_message, BaseAddon
|
||||
from hippolyzer.lib.proxy.addon_utils import show_message, BaseAddon
|
||||
from hippolyzer.lib.proxy.commands import handle_command, Parameter
|
||||
from hippolyzer.lib.base.network.transport import Direction
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import Session
|
||||
|
||||
@@ -29,7 +25,6 @@ class UploaderAddon(BaseAddon):
|
||||
async def upload_asset(self, _session: Session, region: ProxiedRegion,
|
||||
asset_type: AssetType, flags: Optional[int] = None):
|
||||
"""Upload a raw asset with optional flags"""
|
||||
inv_type = asset_type.inventory_type
|
||||
file = await AddonManager.UI.open_file()
|
||||
if not file:
|
||||
return
|
||||
@@ -42,67 +37,32 @@ class UploaderAddon(BaseAddon):
|
||||
with open(file, "rb") as f:
|
||||
file_body = f.read()
|
||||
|
||||
params = {
|
||||
"asset_type": asset_type.human_name,
|
||||
"description": "(No Description)",
|
||||
"everyone_mask": 0,
|
||||
"group_mask": 0,
|
||||
"folder_id": UUID(), # Puts it in the default folder, I guess. Undocumented.
|
||||
"inventory_type": inv_type.human_name,
|
||||
"name": name,
|
||||
"next_owner_mask": 581632,
|
||||
}
|
||||
if flags is not None:
|
||||
params['flags'] = flags
|
||||
try:
|
||||
if asset_type == AssetType.MESH:
|
||||
# Kicking off a mesh upload works a little differently internally
|
||||
# Half-parse the mesh so that we can figure out how many faces it has
|
||||
reader = BufferReader("!", file_body)
|
||||
mesh = reader.read(LLMeshSerializer(parse_segment_contents=False))
|
||||
upload_token = await region.asset_uploader.initiate_mesh_upload(
|
||||
name, mesh, flags=flags
|
||||
)
|
||||
else:
|
||||
upload_token = await region.asset_uploader.initiate_asset_upload(
|
||||
name, asset_type, file_body, flags=flags,
|
||||
)
|
||||
except Exception as e:
|
||||
show_message(e)
|
||||
raise
|
||||
|
||||
caps = region.caps_client
|
||||
async with aiohttp.ClientSession() as sess:
|
||||
async with caps.post('NewFileAgentInventory', llsd=params, session=sess) as resp:
|
||||
parsed = await resp.read_llsd()
|
||||
if "uploader" not in parsed:
|
||||
show_message(f"Upload error!: {parsed!r}")
|
||||
return
|
||||
print("Got upload URL, uploading...")
|
||||
if not await AddonManager.UI.confirm("Upload", f"Spend {upload_token.linden_cost}L on upload?"):
|
||||
return
|
||||
|
||||
async with caps.post(parsed["uploader"], data=file_body, session=sess) as resp:
|
||||
upload_parsed = await resp.read_llsd()
|
||||
|
||||
if "new_inventory_item" not in upload_parsed:
|
||||
show_message(f"Got weird upload resp: {pprint.pformat(upload_parsed)}")
|
||||
return
|
||||
|
||||
await self._force_inv_update(region, upload_parsed['new_inventory_item'])
|
||||
|
||||
@handle_command(item_id=UUID)
|
||||
async def force_inv_update(self, _session: Session, region: ProxiedRegion, item_id: UUID):
|
||||
"""Force an inventory update for a given item id"""
|
||||
await self._force_inv_update(region, item_id)
|
||||
|
||||
async def _force_inv_update(self, region: ProxiedRegion, item_id: UUID):
|
||||
session = region.session()
|
||||
ais_req_data = {
|
||||
"items": [
|
||||
{
|
||||
"owner_id": session.agent_id,
|
||||
"item_id": item_id,
|
||||
}
|
||||
]
|
||||
}
|
||||
async with region.caps_client.post('FetchInventory2', llsd=ais_req_data) as resp:
|
||||
ais_item = (await resp.read_llsd())["items"][0]
|
||||
|
||||
message = Message(
|
||||
"UpdateCreateInventoryItem",
|
||||
Block(
|
||||
"AgentData",
|
||||
AgentID=session.agent_id,
|
||||
SimApproved=1,
|
||||
TransactionID=UUID.random(),
|
||||
),
|
||||
ais_item_to_inventory_data(ais_item),
|
||||
direction=Direction.IN
|
||||
)
|
||||
region.circuit.send_message(message)
|
||||
# Do the actual upload
|
||||
try:
|
||||
await region.asset_uploader.complete_upload(upload_token)
|
||||
except Exception as e:
|
||||
show_message(e)
|
||||
raise
|
||||
|
||||
|
||||
addons = [UploaderAddon()]
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
Example of how to request an Xfer
|
||||
"""
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.legacy_inv import InventoryModel
|
||||
from hippolyzer.lib.base.inventory import InventoryModel
|
||||
from hippolyzer.lib.base.templates import XferFilePath, AssetType, InventoryType, WearableType
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon, show_message
|
||||
@@ -15,7 +15,7 @@ class XferExampleAddon(BaseAddon):
|
||||
@handle_command()
|
||||
async def get_mute_list(self, session: Session, region: ProxiedRegion):
|
||||
"""Fetch the current user's mute list"""
|
||||
region.circuit.send_message(Message(
|
||||
region.circuit.send(Message(
|
||||
'MuteListRequest',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id),
|
||||
Block("MuteData", MuteCRC=0),
|
||||
@@ -35,7 +35,7 @@ class XferExampleAddon(BaseAddon):
|
||||
@handle_command()
|
||||
async def get_task_inventory(self, session: Session, region: ProxiedRegion):
|
||||
"""Get the inventory of the currently selected object"""
|
||||
region.circuit.send_message(Message(
|
||||
region.circuit.send(Message(
|
||||
'RequestTaskInventory',
|
||||
# If no session is passed in we'll use the active session when the coro was created
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id),
|
||||
@@ -57,7 +57,7 @@ class XferExampleAddon(BaseAddon):
|
||||
await xfer
|
||||
|
||||
inv_model = InventoryModel.from_bytes(xfer.reassemble_chunks())
|
||||
item_names = [item.name for item in inv_model.items.values()]
|
||||
item_names = [item.name for item in inv_model.all_items]
|
||||
show_message(item_names)
|
||||
|
||||
@handle_command()
|
||||
@@ -98,7 +98,7 @@ textures 1
|
||||
data=asset_data,
|
||||
transaction_id=transaction_id
|
||||
)
|
||||
region.circuit.send_message(Message(
|
||||
region.circuit.send(Message(
|
||||
'CreateInventoryItem',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id),
|
||||
Block(
|
||||
|
||||
53
client_examples/hello_client.py
Normal file
53
client_examples/hello_client.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
A simple client that just says hello to people
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import pprint
|
||||
from contextlib import aclosing
|
||||
import os
|
||||
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.templates import ChatType, ChatSourceType
|
||||
from hippolyzer.lib.client.hippo_client import HippoClient
|
||||
|
||||
|
||||
async def amain():
|
||||
client = HippoClient()
|
||||
|
||||
async def _respond_to_chat(message: Message):
|
||||
if message["ChatData"]["SourceID"] == client.session.agent_id:
|
||||
return
|
||||
if message["ChatData"]["SourceType"] != ChatSourceType.AGENT:
|
||||
return
|
||||
if "hello" not in message["ChatData"]["Message"].lower():
|
||||
return
|
||||
await client.send_chat(f'Hello {message["ChatData"]["FromName"]}!', chat_type=ChatType.SHOUT)
|
||||
|
||||
async with aclosing(client):
|
||||
await client.login(
|
||||
username=os.environ["HIPPO_USERNAME"],
|
||||
password=os.environ["HIPPO_PASSWORD"],
|
||||
start_location=os.environ.get("HIPPO_START_LOCATION", "last"),
|
||||
)
|
||||
print("I'm here")
|
||||
|
||||
# Wait until we have details about parcels and print them
|
||||
await client.main_region.parcel_manager.parcels_downloaded.wait()
|
||||
pprint.pprint(client.main_region.parcel_manager.parcels)
|
||||
|
||||
await client.send_chat("Hello World!", chat_type=ChatType.SHOUT)
|
||||
client.session.message_handler.subscribe("ChatFromSimulator", _respond_to_chat)
|
||||
# Example of how to work with caps
|
||||
async with client.main_caps_client.get("SimulatorFeatures") as features_resp:
|
||||
print("Features:", await features_resp.read_llsd())
|
||||
|
||||
while True:
|
||||
try:
|
||||
await asyncio.sleep(0.001)
|
||||
except (KeyboardInterrupt, asyncio.CancelledError):
|
||||
await client.send_chat("Goodbye World!", chat_type=ChatType.SHOUT)
|
||||
return
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(amain())
|
||||
@@ -191,7 +191,7 @@
|
||||
</size>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true">color: rgb(80, 0, 0)</string>
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<property name="tabChangesFocus">
|
||||
<bool>true</bool>
|
||||
|
||||
@@ -2,7 +2,7 @@ import enum
|
||||
import logging
|
||||
import typing
|
||||
|
||||
from PySide2 import QtCore, QtGui
|
||||
from PySide6 import QtCore, QtGui
|
||||
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.message_logger import FilteringMessageLogger
|
||||
|
||||
@@ -7,14 +7,16 @@ import sys
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
import mitmproxy.ctx
|
||||
import mitmproxy.exceptions
|
||||
import outleap
|
||||
|
||||
from hippolyzer.lib.base import llsd
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.addon_utils import BaseAddon
|
||||
from hippolyzer.lib.proxy.ca_utils import setup_ca
|
||||
from hippolyzer.lib.proxy.commands import handle_command
|
||||
from hippolyzer.lib.proxy.http_proxy import create_http_proxy, create_proxy_master, HTTPFlowContext
|
||||
from hippolyzer.lib.proxy.http_proxy import create_http_proxy, HTTPFlowContext
|
||||
from hippolyzer.lib.proxy.http_event_manager import MITMProxyEventManager
|
||||
from hippolyzer.lib.proxy.lludp_proxy import SLSOCKS5Server
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
@@ -75,6 +77,15 @@ class SelectionManagerAddon(BaseAddon):
|
||||
selected.task_item = parsed["item-id"]
|
||||
|
||||
|
||||
class AgentUpdaterAddon(BaseAddon):
|
||||
def handle_eq_event(self, session: Session, region: ProxiedRegion, event: dict):
|
||||
if event['message'] != 'AgentGroupDataUpdate':
|
||||
return
|
||||
session.groups.clear()
|
||||
for group in event['body']['GroupData']:
|
||||
session.groups.add(group['GroupID'])
|
||||
|
||||
|
||||
class REPLAddon(BaseAddon):
|
||||
@handle_command()
|
||||
async def spawn_repl(self, session: Session, region: ProxiedRegion):
|
||||
@@ -83,31 +94,36 @@ class REPLAddon(BaseAddon):
|
||||
AddonManager.spawn_repl()
|
||||
|
||||
|
||||
def run_http_proxy_process(proxy_host, http_proxy_port, flow_context: HTTPFlowContext):
|
||||
def run_http_proxy_process(proxy_host, http_proxy_port, flow_context: HTTPFlowContext, ssl_insecure=False):
|
||||
mitm_loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(mitm_loop)
|
||||
mitmproxy_master = create_http_proxy(proxy_host, http_proxy_port, flow_context)
|
||||
mitmproxy_master.start_server()
|
||||
gc.freeze()
|
||||
mitm_loop.run_forever()
|
||||
|
||||
async def mitmproxy_loop():
|
||||
mitmproxy_master = create_http_proxy(proxy_host, http_proxy_port, flow_context, ssl_insecure=ssl_insecure)
|
||||
gc.freeze()
|
||||
await mitmproxy_master.run()
|
||||
|
||||
asyncio.run(mitmproxy_loop())
|
||||
|
||||
|
||||
def start_proxy(session_manager: SessionManager, extra_addons: Optional[list] = None,
|
||||
extra_addon_paths: Optional[list] = None, proxy_host=None):
|
||||
extra_addon_paths: Optional[list] = None, proxy_host=None, ssl_insecure=False):
|
||||
extra_addons = extra_addons or []
|
||||
extra_addon_paths = extra_addon_paths or []
|
||||
extra_addons.append(SelectionManagerAddon())
|
||||
extra_addons.append(REPLAddon())
|
||||
extra_addons.append(AgentUpdaterAddon())
|
||||
|
||||
root_log = logging.getLogger()
|
||||
root_log.addHandler(logging.StreamHandler())
|
||||
root_log.setLevel(logging.INFO)
|
||||
logging.basicConfig()
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
|
||||
udp_proxy_port = session_manager.settings.SOCKS_PROXY_PORT
|
||||
http_proxy_port = session_manager.settings.HTTP_PROXY_PORT
|
||||
leap_port = session_manager.settings.LEAP_PORT
|
||||
if proxy_host is None:
|
||||
proxy_host = session_manager.settings.PROXY_BIND_ADDR
|
||||
|
||||
@@ -117,25 +133,28 @@ def start_proxy(session_manager: SessionManager, extra_addons: Optional[list] =
|
||||
# TODO: argparse
|
||||
if len(sys.argv) == 3:
|
||||
if sys.argv[1] == "--setup-ca":
|
||||
try:
|
||||
mitmproxy_master = create_http_proxy(proxy_host, http_proxy_port, flow_context)
|
||||
except mitmproxy.exceptions.MitmproxyException:
|
||||
# Proxy already running, create the master so we don't try to bind to a port
|
||||
mitmproxy_master = create_proxy_master(proxy_host, http_proxy_port, flow_context)
|
||||
mitmproxy_master = create_http_proxy(proxy_host, http_proxy_port, flow_context)
|
||||
setup_ca(sys.argv[2], mitmproxy_master)
|
||||
return sys.exit(0)
|
||||
|
||||
http_proc = multiprocessing.Process(
|
||||
target=run_http_proxy_process,
|
||||
args=(proxy_host, http_proxy_port, flow_context),
|
||||
args=(proxy_host, http_proxy_port, flow_context, ssl_insecure),
|
||||
daemon=True,
|
||||
)
|
||||
http_proc.start()
|
||||
# These need to be set for mitmproxy's ASGIApp serving code to work.
|
||||
mitmproxy.ctx.master = None
|
||||
mitmproxy.ctx.log = logging.getLogger("mitmproxy log")
|
||||
|
||||
server = SLSOCKS5Server(session_manager)
|
||||
coro = asyncio.start_server(server.handle_connection, proxy_host, udp_proxy_port)
|
||||
async_server = loop.run_until_complete(coro)
|
||||
|
||||
leap_server = outleap.LEAPBridgeServer(session_manager.leap_client_connected)
|
||||
coro = asyncio.start_server(leap_server.handle_connection, proxy_host, leap_port)
|
||||
async_leap_server = loop.run_until_complete(coro)
|
||||
|
||||
event_manager = MITMProxyEventManager(session_manager, flow_context)
|
||||
loop.create_task(event_manager.run())
|
||||
|
||||
@@ -162,6 +181,8 @@ def start_proxy(session_manager: SessionManager, extra_addons: Optional[list] =
|
||||
# Close the server
|
||||
print("Closing SOCKS server")
|
||||
async_server.close()
|
||||
print("Shutting down LEAP server")
|
||||
async_leap_server.close()
|
||||
print("Shutting down addons")
|
||||
AddonManager.shutdown()
|
||||
print("Waiting for SOCKS server to close")
|
||||
|
||||
@@ -18,13 +18,13 @@ from typing import *
|
||||
|
||||
import multidict
|
||||
from qasync import QEventLoop, asyncSlot
|
||||
from PySide2 import QtCore, QtWidgets, QtGui
|
||||
from PySide6 import QtCore, QtWidgets, QtGui
|
||||
|
||||
from hippolyzer.apps.model import MessageLogModel, MessageLogHeader, RegionListModel
|
||||
from hippolyzer.apps.proxy import start_proxy
|
||||
from hippolyzer.lib.base import llsd
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.helpers import bytes_unescape, bytes_escape, get_resource_filename
|
||||
from hippolyzer.lib.base.helpers import bytes_unescape, bytes_escape, get_resource_filename, create_logged_task
|
||||
from hippolyzer.lib.base.message.llsd_msg_serializer import LLSDMessageSerializer
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.base.message.message_formatting import (
|
||||
@@ -35,13 +35,15 @@ from hippolyzer.lib.base.message.message_formatting import (
|
||||
)
|
||||
from hippolyzer.lib.base.message.msgtypes import MsgType
|
||||
from hippolyzer.lib.base.message.template_dict import DEFAULT_TEMPLATE_DICT
|
||||
from hippolyzer.lib.base.settings import SettingDescriptor
|
||||
from hippolyzer.lib.base.ui_helpers import loadUi
|
||||
import hippolyzer.lib.base.serialization as se
|
||||
from hippolyzer.lib.base.network.transport import Direction, SocketUDPTransport
|
||||
from hippolyzer.lib.client.state import BaseClientSessionManager
|
||||
from hippolyzer.lib.proxy.addons import BaseInteractionManager, AddonManager
|
||||
from hippolyzer.lib.proxy.ca_utils import setup_ca_everywhere
|
||||
from hippolyzer.lib.proxy.caps_client import ProxyCapsClient
|
||||
from hippolyzer.lib.proxy.http_proxy import create_proxy_master, HTTPFlowContext
|
||||
from hippolyzer.lib.proxy.http_proxy import create_http_proxy, HTTPFlowContext
|
||||
from hippolyzer.lib.proxy.message_logger import LLUDPMessageLogEntry, AbstractMessageLogEntry, WrappingMessageLogger, \
|
||||
import_log_entries, export_log_entries
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
@@ -61,7 +63,7 @@ def show_error_message(error_msg, parent=None):
|
||||
error_dialog = QtWidgets.QErrorMessage(parent=parent)
|
||||
# No obvious way to set this to plaintext, yuck...
|
||||
error_dialog.showMessage(html.escape(error_msg))
|
||||
error_dialog.exec_()
|
||||
error_dialog.exec()
|
||||
error_dialog.raise_()
|
||||
|
||||
|
||||
@@ -70,6 +72,7 @@ class GUISessionManager(SessionManager, QtCore.QObject):
|
||||
regionRemoved = QtCore.Signal(ProxiedRegion)
|
||||
|
||||
def __init__(self, settings):
|
||||
BaseClientSessionManager.__init__(self)
|
||||
SessionManager.__init__(self, settings)
|
||||
QtCore.QObject.__init__(self)
|
||||
self.all_regions = []
|
||||
@@ -88,13 +91,13 @@ class GUISessionManager(SessionManager, QtCore.QObject):
|
||||
self.all_regions = new_regions
|
||||
|
||||
|
||||
class GUIInteractionManager(BaseInteractionManager, QtCore.QObject):
|
||||
def __init__(self, parent):
|
||||
class GUIInteractionManager(BaseInteractionManager):
|
||||
def __init__(self, parent: QtWidgets.QWidget):
|
||||
BaseInteractionManager.__init__(self)
|
||||
QtCore.QObject.__init__(self, parent=parent)
|
||||
self._parent = parent
|
||||
|
||||
def main_window_handle(self) -> Any:
|
||||
return self.parent()
|
||||
return self._parent
|
||||
|
||||
def _dialog_async_exec(self, dialog: QtWidgets.QDialog):
|
||||
future = asyncio.Future()
|
||||
@@ -106,7 +109,7 @@ class GUIInteractionManager(BaseInteractionManager, QtCore.QObject):
|
||||
self, caption: str, directory: str, filter_str: str, mode: QtWidgets.QFileDialog.FileMode,
|
||||
default_suffix: str = '',
|
||||
) -> Tuple[bool, QtWidgets.QFileDialog]:
|
||||
dialog = QtWidgets.QFileDialog(self.parent(), caption=caption, directory=directory, filter=filter_str)
|
||||
dialog = QtWidgets.QFileDialog(self._parent, caption=caption, directory=directory, filter=filter_str)
|
||||
dialog.setFileMode(mode)
|
||||
if mode == QtWidgets.QFileDialog.FileMode.AnyFile:
|
||||
dialog.setAcceptMode(QtWidgets.QFileDialog.AcceptMode.AcceptSave)
|
||||
@@ -154,7 +157,7 @@ class GUIInteractionManager(BaseInteractionManager, QtCore.QObject):
|
||||
title,
|
||||
caption,
|
||||
QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel,
|
||||
self.parent(),
|
||||
self._parent,
|
||||
)
|
||||
fut = asyncio.Future()
|
||||
msg.finished.connect(lambda r: fut.set_result(r))
|
||||
@@ -163,6 +166,8 @@ class GUIInteractionManager(BaseInteractionManager, QtCore.QObject):
|
||||
|
||||
|
||||
class GUIProxySettings(ProxySettings):
|
||||
FIRST_RUN: bool = SettingDescriptor(True)
|
||||
|
||||
"""Persistent settings backed by QSettings"""
|
||||
def __init__(self, settings: QtCore.QSettings):
|
||||
super().__init__()
|
||||
@@ -228,7 +233,8 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
"AvatarRenderInfo FirestormBridge ObjectAnimation ParcelDwellRequest ParcelAccessListRequest " \
|
||||
"ParcelDwellReply ParcelAccessListReply AttachedSoundGainChange " \
|
||||
"ParcelPropertiesRequest ParcelProperties GetObjectCost GetObjectPhysicsData ObjectImage " \
|
||||
"ViewerAsset GetTexture SetAlwaysRun GetDisplayNames MapImageService MapItemReply".split(" ")
|
||||
"ViewerAsset GetTexture SetAlwaysRun GetDisplayNames MapImageService MapItemReply " \
|
||||
"AgentFOV GenericStreamingMessage".split(" ")
|
||||
DEFAULT_FILTER = f"!({' || '.join(ignored for ignored in DEFAULT_IGNORE)})"
|
||||
|
||||
textRequest: QtWidgets.QTextEdit
|
||||
@@ -265,15 +271,17 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
self.lineEditFilter.editingFinished.connect(self.setFilter)
|
||||
self.btnMessageBuilder.clicked.connect(self._sendToMessageBuilder)
|
||||
self.btnCopyRepr.clicked.connect(self._copyRepr)
|
||||
self.actionInstallHTTPSCerts.triggered.connect(self._installHTTPSCerts)
|
||||
self.actionInstallHTTPSCerts.triggered.connect(self.installHTTPSCerts)
|
||||
self.actionManageAddons.triggered.connect(self._manageAddons)
|
||||
self.actionManageFilters.triggered.connect(self._manageFilters)
|
||||
self.actionOpenMessageBuilder.triggered.connect(self._openMessageBuilder)
|
||||
|
||||
self.actionProxyRemotelyAccessible.setChecked(self.settings.REMOTELY_ACCESSIBLE)
|
||||
self.actionProxySSLInsecure.setChecked(self.settings.SSL_INSECURE)
|
||||
self.actionUseViewerObjectCache.setChecked(self.settings.USE_VIEWER_OBJECT_CACHE)
|
||||
self.actionRequestMissingObjects.setChecked(self.settings.AUTOMATICALLY_REQUEST_MISSING_OBJECTS)
|
||||
self.actionProxyRemotelyAccessible.triggered.connect(self._setProxyRemotelyAccessible)
|
||||
self.actionProxySSLInsecure.triggered.connect(self._setProxySSLInsecure)
|
||||
self.actionUseViewerObjectCache.triggered.connect(self._setUseViewerObjectCache)
|
||||
self.actionRequestMissingObjects.triggered.connect(self._setRequestMissingObjects)
|
||||
self.actionOpenNewMessageLogWindow.triggered.connect(self._openNewMessageLogWindow)
|
||||
@@ -300,7 +308,7 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
|
||||
def _populateFilterMenu(self):
|
||||
def _addFilterAction(text, filter_str):
|
||||
filter_action = QtWidgets.QAction(text, self)
|
||||
filter_action = QtGui.QAction(text, self)
|
||||
filter_action.triggered.connect(lambda: self.setFilter(filter_str))
|
||||
self._filterMenu.addAction(filter_action)
|
||||
|
||||
@@ -311,13 +319,16 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
for preset_name, preset_filter in filters.items():
|
||||
_addFilterAction(preset_name, preset_filter)
|
||||
|
||||
def getFilterDict(self):
|
||||
return self.settings.FILTERS
|
||||
|
||||
def setFilterDict(self, val: dict):
|
||||
self.settings.FILTERS = val
|
||||
self._populateFilterMenu()
|
||||
|
||||
def _manageFilters(self):
|
||||
dialog = FilterDialog(self)
|
||||
dialog.exec_()
|
||||
dialog.exec()
|
||||
|
||||
@nonFatalExceptions
|
||||
def setFilter(self, filter_str=None):
|
||||
@@ -354,21 +365,20 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
beautify=self.checkBeautify.isChecked(),
|
||||
replacements=buildReplacements(entry.session, entry.region),
|
||||
)
|
||||
highlight_range = None
|
||||
if isinstance(req, SpannedString):
|
||||
match_result = self.model.filter.match(entry)
|
||||
# Match result was a tuple indicating what matched
|
||||
if isinstance(match_result, tuple):
|
||||
highlight_range = req.spans.get(match_result)
|
||||
|
||||
self.textRequest.setPlainText(req)
|
||||
if highlight_range:
|
||||
cursor = self.textRequest.textCursor()
|
||||
cursor.setPosition(highlight_range[0], QtGui.QTextCursor.MoveAnchor)
|
||||
cursor.setPosition(highlight_range[1], QtGui.QTextCursor.KeepAnchor)
|
||||
highlight_format = QtGui.QTextBlockFormat()
|
||||
highlight_format.setBackground(QtCore.Qt.yellow)
|
||||
cursor.setBlockFormat(highlight_format)
|
||||
# The string has a map of fields and their associated positions within the string,
|
||||
# use that to highlight any individual fields the filter matched on.
|
||||
if isinstance(req, SpannedString):
|
||||
for field in self.model.filter.match(entry, short_circuit=False).fields:
|
||||
field_span = req.spans.get(field)
|
||||
if not field_span:
|
||||
continue
|
||||
cursor = self.textRequest.textCursor()
|
||||
cursor.setPosition(field_span[0], QtGui.QTextCursor.MoveAnchor)
|
||||
cursor.setPosition(field_span[1], QtGui.QTextCursor.KeepAnchor)
|
||||
highlight_format = QtGui.QTextBlockFormat()
|
||||
highlight_format.setBackground(QtCore.Qt.yellow)
|
||||
cursor.setBlockFormat(highlight_format)
|
||||
|
||||
resp = entry.response(beautify=self.checkBeautify.isChecked())
|
||||
if resp:
|
||||
@@ -441,10 +451,10 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
with open(log_file, "wb") as f:
|
||||
f.write(export_log_entries(self.model))
|
||||
|
||||
def _installHTTPSCerts(self):
|
||||
def installHTTPSCerts(self):
|
||||
msg = QtWidgets.QMessageBox()
|
||||
msg.setText("This will install the proxy's HTTPS certificate in the config dir"
|
||||
" of any installed viewers, continue?")
|
||||
msg.setText("Would you like to install the proxy's HTTPS certificate in the config dir"
|
||||
" of any installed viewers so that HTTPS connections will work?")
|
||||
yes_btn = msg.addButton("Yes", QtWidgets.QMessageBox.NoRole)
|
||||
msg.addButton("No", QtWidgets.QMessageBox.NoRole)
|
||||
msg.exec()
|
||||
@@ -452,7 +462,7 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
if clicked_btn is not yes_btn:
|
||||
return
|
||||
|
||||
master = create_proxy_master("127.0.0.1", -1, HTTPFlowContext())
|
||||
master = create_http_proxy("127.0.0.1", -1, HTTPFlowContext())
|
||||
dirs = setup_ca_everywhere(master)
|
||||
|
||||
msg = QtWidgets.QMessageBox()
|
||||
@@ -468,6 +478,12 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
msg.setText("Remote accessibility setting changes will take effect on next run")
|
||||
msg.exec()
|
||||
|
||||
def _setProxySSLInsecure(self, checked: bool):
|
||||
self.sessionManager.settings.SSL_INSECURE = checked
|
||||
msg = QtWidgets.QMessageBox()
|
||||
msg.setText("SSL security setting changes will take effect on next run")
|
||||
msg.exec()
|
||||
|
||||
def _setUseViewerObjectCache(self, checked: bool):
|
||||
self.sessionManager.settings.USE_VIEWER_OBJECT_CACHE = checked
|
||||
|
||||
@@ -476,7 +492,7 @@ class MessageLogWindow(QtWidgets.QMainWindow):
|
||||
|
||||
def _manageAddons(self):
|
||||
dialog = AddonDialog(self)
|
||||
dialog.exec_()
|
||||
dialog.exec()
|
||||
|
||||
def getAddonList(self) -> List[str]:
|
||||
return self.sessionManager.settings.ADDON_SCRIPTS
|
||||
@@ -560,12 +576,12 @@ class MessageBuilderWindow(QtWidgets.QMainWindow):
|
||||
message_names = sorted(x.name for x in self.templateDict)
|
||||
|
||||
for message_name in message_names:
|
||||
if self.templateDict[message_name].msg_trust:
|
||||
if self.templateDict[message_name].trusted:
|
||||
self.comboTrusted.addItem(message_name)
|
||||
else:
|
||||
self.comboUntrusted.addItem(message_name)
|
||||
|
||||
cap_names = sorted(set(itertools.chain(*[r.caps.keys() for r in self.regionModel.regions])))
|
||||
cap_names = sorted(set(itertools.chain(*[r.cap_urls.keys() for r in self.regionModel.regions])))
|
||||
for cap_name in cap_names:
|
||||
if cap_name.endswith("ProxyWrapper"):
|
||||
continue
|
||||
@@ -596,7 +612,7 @@ class MessageBuilderWindow(QtWidgets.QMainWindow):
|
||||
break
|
||||
self.textRequest.setPlainText(
|
||||
f"""{method} [[{cap_name}]]{path}{params} HTTP/1.1
|
||||
# {region.caps.get(cap_name, "<unknown URI>")}
|
||||
# {region.cap_urls.get(cap_name, "<unknown URI>")}
|
||||
{headers}
|
||||
{body}"""
|
||||
)
|
||||
@@ -691,13 +707,11 @@ class MessageBuilderWindow(QtWidgets.QMainWindow):
|
||||
msg = HumanMessageSerializer.from_human_string(msg_text, replacements, env, safe=False)
|
||||
if self.checkLLUDPViaCaps.isChecked():
|
||||
if msg.direction == Direction.IN:
|
||||
region.eq_manager.inject_event(
|
||||
self.llsdSerializer.serialize(msg, as_dict=True)
|
||||
)
|
||||
region.eq_manager.inject_message(msg)
|
||||
else:
|
||||
self._sendHTTPRequest(
|
||||
"POST",
|
||||
region.caps["UntrustedSimulatorMessage"],
|
||||
region.cap_urls["UntrustedSimulatorMessage"],
|
||||
{"Content-Type": "application/llsd+xml", "Accept": "application/llsd+xml"},
|
||||
self.llsdSerializer.serialize(msg),
|
||||
)
|
||||
@@ -706,18 +720,25 @@ class MessageBuilderWindow(QtWidgets.QMainWindow):
|
||||
off_circuit = self.checkOffCircuit.isChecked()
|
||||
if off_circuit:
|
||||
transport = SocketUDPTransport(socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
|
||||
region.circuit.send_message(msg, transport=transport)
|
||||
region.circuit.send(msg, transport=transport)
|
||||
if off_circuit:
|
||||
transport.close()
|
||||
|
||||
def _sendEQMessage(self, session, region: Optional[ProxiedRegion], msg_text: str, _replacements: dict):
|
||||
def _sendEQMessage(self, session, region: Optional[ProxiedRegion], msg_text: str, replacements: dict):
|
||||
if not session or not region:
|
||||
raise RuntimeError("Need a valid session and region to send EQ event")
|
||||
message_line, _, body = (x.strip() for x in msg_text.partition("\n"))
|
||||
message_name = message_line.rsplit(" ", 1)[-1]
|
||||
|
||||
env = self._buildEnv(session, region)
|
||||
|
||||
def directive_handler(m):
|
||||
return self._handleHTTPDirective(env, replacements, False, m)
|
||||
body = re.sub(rb"<!HIPPO(\w+)\[\[(.*?)]]>", directive_handler, body.encode("utf8"), flags=re.S)
|
||||
|
||||
region.eq_manager.inject_event({
|
||||
"message": message_name,
|
||||
"body": llsd.parse_xml(body.encode("utf8")),
|
||||
"body": llsd.parse_xml(body),
|
||||
})
|
||||
|
||||
def _sendHTTPMessage(self, session, region, msg_text: str, replacements: dict):
|
||||
@@ -741,7 +762,7 @@ class MessageBuilderWindow(QtWidgets.QMainWindow):
|
||||
cap_name = match.group(1)
|
||||
cap_url = session.global_caps.get(cap_name)
|
||||
if not cap_url:
|
||||
cap_url = region.caps.get(cap_name)
|
||||
cap_url = region.cap_urls.get(cap_name)
|
||||
if not cap_url:
|
||||
raise ValueError("Don't have a Cap for %s" % cap_name)
|
||||
uri = cap_url + match.group(2)
|
||||
@@ -781,7 +802,10 @@ class MessageBuilderWindow(QtWidgets.QMainWindow):
|
||||
val = subfield_eval(contents.decode("utf8").strip(), globals_={**env, **replacements})
|
||||
val = _coerce_to_bytes(val)
|
||||
elif directive == b"REPL":
|
||||
val = _coerce_to_bytes(replacements[contents.decode("utf8").strip()])
|
||||
repl = replacements[contents.decode("utf8").strip()]
|
||||
if callable(repl):
|
||||
repl = repl()
|
||||
val = _coerce_to_bytes(repl)
|
||||
else:
|
||||
raise ValueError(f"Unknown directive {directive}")
|
||||
|
||||
@@ -802,7 +826,7 @@ class MessageBuilderWindow(QtWidgets.QMainWindow):
|
||||
# enough for the full response to pass through the proxy
|
||||
await resp.read()
|
||||
|
||||
asyncio.create_task(_send_request())
|
||||
create_logged_task(_send_request(), "Send HTTP Request")
|
||||
|
||||
|
||||
class AddonDialog(QtWidgets.QDialog):
|
||||
@@ -915,10 +939,15 @@ def gui_main():
|
||||
http_host = None
|
||||
if window.sessionManager.settings.REMOTELY_ACCESSIBLE:
|
||||
http_host = "0.0.0.0"
|
||||
if settings.FIRST_RUN:
|
||||
settings.FIRST_RUN = False
|
||||
# Automatically offer to install the HTTPS certs on first run.
|
||||
window.installHTTPSCerts()
|
||||
start_proxy(
|
||||
session_manager=window.sessionManager,
|
||||
extra_addon_paths=window.getAddonList(),
|
||||
proxy_host=http_host,
|
||||
ssl_insecure=settings.SSL_INSECURE,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -193,7 +193,7 @@
|
||||
</size>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true">color: rgb(80, 0, 0)</string>
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<property name="tabChangesFocus">
|
||||
<bool>true</bool>
|
||||
@@ -213,7 +213,7 @@
|
||||
</widget>
|
||||
<widget class="QPlainTextEdit" name="textResponse">
|
||||
<property name="styleSheet">
|
||||
<string notr="true">color: rgb(0, 0, 80)</string>
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<property name="tabChangesFocus">
|
||||
<bool>true</bool>
|
||||
@@ -245,7 +245,7 @@
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>700</width>
|
||||
<height>22</height>
|
||||
<height>29</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QMenu" name="menuFile">
|
||||
@@ -268,6 +268,7 @@
|
||||
<addaction name="actionProxyRemotelyAccessible"/>
|
||||
<addaction name="actionUseViewerObjectCache"/>
|
||||
<addaction name="actionRequestMissingObjects"/>
|
||||
<addaction name="actionProxySSLInsecure"/>
|
||||
</widget>
|
||||
<addaction name="menuFile"/>
|
||||
</widget>
|
||||
@@ -342,6 +343,17 @@
|
||||
<string>Export Log Entries</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionProxySSLInsecure">
|
||||
<property name="checkable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Allow Insecure SSL Connections</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Allow invalid SSL certificates from upstream connections</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
||||
330
hippolyzer/lib/base/colladatools.py
Normal file
330
hippolyzer/lib/base/colladatools.py
Normal file
@@ -0,0 +1,330 @@
|
||||
# This currently implements basic LLMesh -> Collada.
|
||||
#
|
||||
# TODO:
|
||||
# * inverse, Collada -> LLMesh (for simple cases, maybe using impasse rather than pycollada)
|
||||
# * round-tripping tests, LLMesh->Collada->LLMesh
|
||||
# * * Can't really test using Collada->LLMesh->Collada because Collada->LLMesh is almost always
|
||||
# going to be lossy due to how SL represents vertex data and materials compared to what
|
||||
# Collada allows.
|
||||
# * Eventually scrap this and just use GLTF instead once we know we have the semantics correct
|
||||
# * * Collada was just easier to bootstrap given that it's the only officially supported input format
|
||||
# * * Collada tooling sucks and even LL is moving away from it
|
||||
# * * Ensuring LLMesh->Collada and LLMesh->GLTF conversion don't differ semantically is easy via assimp.
|
||||
|
||||
import logging
|
||||
import os.path
|
||||
import secrets
|
||||
import sys
|
||||
from typing import Dict, Optional
|
||||
|
||||
import collada
|
||||
import collada.source
|
||||
from collada import E
|
||||
from lxml import etree
|
||||
import numpy as np
|
||||
import transformations
|
||||
|
||||
from hippolyzer.lib.base.helpers import get_resource_filename
|
||||
from hippolyzer.lib.base.serialization import BufferReader
|
||||
from hippolyzer.lib.base.mesh import (
|
||||
LLMeshSerializer,
|
||||
MeshAsset,
|
||||
positions_from_domain,
|
||||
SkinSegmentDict,
|
||||
llsd_to_mat4,
|
||||
)
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
|
||||
def mat4_to_collada(mat: np.ndarray) -> np.ndarray:
|
||||
return mat.flatten(order='C')
|
||||
|
||||
|
||||
def mesh_to_collada(ll_mesh: MeshAsset, include_skin=True) -> collada.Collada:
|
||||
dae = collada.Collada()
|
||||
axis = collada.asset.UP_AXIS.Z_UP
|
||||
dae.assetInfo.upaxis = axis
|
||||
scene = collada.scene.Scene("scene", [llmesh_to_node(ll_mesh, dae, include_skin=include_skin)])
|
||||
|
||||
dae.scenes.append(scene)
|
||||
dae.scene = scene
|
||||
return dae
|
||||
|
||||
|
||||
def llmesh_to_node(ll_mesh: MeshAsset, dae: collada.Collada, uniq=None,
|
||||
include_skin=True, node_transform: Optional[np.ndarray] = None) -> collada.scene.Node:
|
||||
if node_transform is None:
|
||||
node_transform = np.identity(4)
|
||||
|
||||
should_skin = False
|
||||
skin_seg = ll_mesh.segments.get('skin')
|
||||
bind_shape_matrix = None
|
||||
if include_skin and skin_seg:
|
||||
bind_shape_matrix = llsd_to_mat4(skin_seg["bind_shape_matrix"])
|
||||
should_skin = True
|
||||
# Transform from the skin will be applied on the controller, not the node
|
||||
node_transform = np.identity(4)
|
||||
|
||||
if not uniq:
|
||||
uniq = secrets.token_urlsafe(4)
|
||||
|
||||
geom_nodes = []
|
||||
node_name = f"mainnode{uniq}"
|
||||
# TODO: do the other LODs?
|
||||
for submesh_num, submesh in enumerate(ll_mesh.segments["high_lod"]):
|
||||
# Make sure none of our IDs collide with those of other nodes
|
||||
sub_uniq = uniq + str(submesh_num)
|
||||
|
||||
range_xyz = positions_from_domain(submesh["Position"], submesh["PositionDomain"])
|
||||
xyz = np.array([x.data() for x in range_xyz])
|
||||
|
||||
range_uv = positions_from_domain(submesh['TexCoord0'], submesh['TexCoord0Domain'])
|
||||
uv = np.array([x.data() for x in range_uv]).flatten()
|
||||
|
||||
norms = np.array([x.data() for x in submesh["Normal"]])
|
||||
|
||||
effect = collada.material.Effect(
|
||||
id=f"effect{sub_uniq}",
|
||||
params=[],
|
||||
specular=(0.0, 0.0, 0.0, 0.0),
|
||||
reflectivity=(0.0, 0.0, 0.0, 0.0),
|
||||
emission=(0.0, 0.0, 0.0, 0.0),
|
||||
ambient=(0.0, 0.0, 0.0, 0.0),
|
||||
reflective=0.0,
|
||||
shadingtype="blinn",
|
||||
shininess=0.0,
|
||||
diffuse=(1.0, 1.0, 1.0),
|
||||
)
|
||||
mat = collada.material.Material(f"material{sub_uniq}", f"material{sub_uniq}", effect)
|
||||
|
||||
dae.materials.append(mat)
|
||||
dae.effects.append(effect)
|
||||
|
||||
vert_src = collada.source.FloatSource(f"verts-array{sub_uniq}", xyz.flatten(), ("X", "Y", "Z"))
|
||||
norm_src = collada.source.FloatSource(f"norms-array{sub_uniq}", norms.flatten(), ("X", "Y", "Z"))
|
||||
# UV maps have to have the same name or they'll behave weirdly when objects are merged.
|
||||
uv_src = collada.source.FloatSource("uvs-array", np.array(uv), ("U", "V"))
|
||||
|
||||
geom = collada.geometry.Geometry(dae, f"geometry{sub_uniq}", "geometry", [vert_src, norm_src, uv_src])
|
||||
|
||||
input_list = collada.source.InputList()
|
||||
input_list.addInput(0, 'VERTEX', f'#verts-array{sub_uniq}', set="0")
|
||||
input_list.addInput(0, 'NORMAL', f'#norms-array{sub_uniq}', set="0")
|
||||
input_list.addInput(0, 'TEXCOORD', '#uvs-array', set="0")
|
||||
|
||||
tri_idxs = np.array(submesh["TriangleList"]).flatten()
|
||||
matnode = collada.scene.MaterialNode(f"materialref{sub_uniq}", mat, inputs=[])
|
||||
tri_set = geom.createTriangleSet(tri_idxs, input_list, f'materialref{sub_uniq}')
|
||||
geom.primitives.append(tri_set)
|
||||
dae.geometries.append(geom)
|
||||
|
||||
if should_skin:
|
||||
joint_names = np.array(skin_seg['joint_names'], dtype=object)
|
||||
joints_source = collada.source.NameSource(f"joint-names{sub_uniq}", joint_names, ("JOINT",))
|
||||
# PyCollada has a bug where it doesn't set the source URI correctly. Fix it.
|
||||
accessor = joints_source.xmlnode.find(f"{dae.tag('technique_common')}/{dae.tag('accessor')}")
|
||||
if not accessor.get('source').startswith('#'):
|
||||
accessor.set('source', f"#{accessor.get('source')}")
|
||||
|
||||
flattened_bind_poses = []
|
||||
for bind_pose in skin_seg['inverse_bind_matrix']:
|
||||
flattened_bind_poses.append(mat4_to_collada(llsd_to_mat4(bind_pose)))
|
||||
flattened_bind_poses = np.array(flattened_bind_poses)
|
||||
inv_bind_source = _create_mat4_source(f"bind-poses{sub_uniq}", flattened_bind_poses, "TRANSFORM")
|
||||
|
||||
weight_joint_idxs = []
|
||||
weights = []
|
||||
vert_weight_counts = []
|
||||
cur_weight_idx = 0
|
||||
for vert_weights in submesh['Weights']:
|
||||
vert_weight_counts.append(len(vert_weights))
|
||||
for vert_weight in vert_weights:
|
||||
weights.append(vert_weight.weight)
|
||||
weight_joint_idxs.append(vert_weight.joint_idx)
|
||||
weight_joint_idxs.append(cur_weight_idx)
|
||||
cur_weight_idx += 1
|
||||
|
||||
weights_source = collada.source.FloatSource(f"skin-weights{sub_uniq}", np.array(weights), ("WEIGHT",))
|
||||
# We need to make a controller for each material since materials are essentially distinct meshes
|
||||
# in SL, with their own distinct sets of weights and vertex data.
|
||||
controller_node = E.controller(
|
||||
E.skin(
|
||||
E.bind_shape_matrix(' '.join(str(x) for x in mat4_to_collada(bind_shape_matrix))),
|
||||
joints_source.xmlnode,
|
||||
inv_bind_source.xmlnode,
|
||||
weights_source.xmlnode,
|
||||
E.joints(
|
||||
E.input(semantic="JOINT", source=f"#joint-names{sub_uniq}"),
|
||||
E.input(semantic="INV_BIND_MATRIX", source=f"#bind-poses{sub_uniq}")
|
||||
),
|
||||
E.vertex_weights(
|
||||
E.input(semantic="JOINT", source=f"#joint-names{sub_uniq}", offset="0"),
|
||||
E.input(semantic="WEIGHT", source=f"#skin-weights{sub_uniq}", offset="1"),
|
||||
E.vcount(' '.join(str(x) for x in vert_weight_counts)),
|
||||
E.v(' '.join(str(x) for x in weight_joint_idxs)),
|
||||
count=str(len(submesh['Weights']))
|
||||
),
|
||||
source=f"#geometry{sub_uniq}"
|
||||
),
|
||||
id=f"Armature-{sub_uniq}",
|
||||
name=node_name
|
||||
)
|
||||
controller = collada.controller.Controller.load(dae, {}, controller_node)
|
||||
dae.controllers.append(controller)
|
||||
geom_node = collada.scene.ControllerNode(controller, [matnode])
|
||||
else:
|
||||
geom_node = collada.scene.GeometryNode(geom, [matnode])
|
||||
|
||||
geom_nodes.append(geom_node)
|
||||
|
||||
node = collada.scene.Node(
|
||||
node_name,
|
||||
children=geom_nodes,
|
||||
transforms=[collada.scene.MatrixTransform(mat4_to_collada(node_transform))],
|
||||
)
|
||||
if should_skin:
|
||||
# We need a skeleton per _mesh asset_ because you could have incongruous skeletons
|
||||
# within the same linkset.
|
||||
# TODO: can we maintain some kind of skeleton cache, where if this skeleton has no conflicts
|
||||
# with another skeleton in the cache, we just use that skeleton and add any additional joints?
|
||||
skel_root = load_skeleton_nodes()
|
||||
transform_skeleton(skel_root, dae, skin_seg)
|
||||
skel = collada.scene.Node.load(dae, skel_root, {})
|
||||
skel.children.append(node)
|
||||
skel.id = f"Skel-{uniq}"
|
||||
skel.save()
|
||||
node = skel
|
||||
return node
|
||||
|
||||
|
||||
def load_skeleton_nodes() -> etree.ElementBase:
|
||||
# TODO: this sucks. Can't we construct nodes with the appropriate transformation
|
||||
# matrices from the data in `avatar_skeleton.xml`?
|
||||
skel_path = get_resource_filename("lib/base/data/male_collada_joints.xml")
|
||||
with open(skel_path, 'r') as f:
|
||||
return etree.fromstring(f.read())
|
||||
|
||||
|
||||
def transform_skeleton(skel_root: etree.ElementBase, dae: collada.Collada, skin_seg: SkinSegmentDict,
|
||||
include_unreferenced_bones=False):
|
||||
"""Update skeleton XML nodes to account for joint translations in the mesh"""
|
||||
joint_nodes: Dict[str, collada.scene.Node] = {}
|
||||
for skel_node in skel_root.iter():
|
||||
# xpath is loathsome so this is easier.
|
||||
if skel_node.tag != dae.tag('node') or skel_node.get('type') != 'JOINT':
|
||||
continue
|
||||
joint_nodes[skel_node.get('name')] = collada.scene.Node.load(dae, skel_node, {})
|
||||
for joint_name, matrix in zip(skin_seg['joint_names'], skin_seg.get('alt_inverse_bind_matrix', [])):
|
||||
joint_node = joint_nodes[joint_name]
|
||||
joint_decomp = transformations.decompose_matrix(llsd_to_mat4(matrix))
|
||||
joint_node.matrix = mat4_to_collada(transformations.compose_matrix(translate=joint_decomp[3]))
|
||||
# Update the underlying XML element with the new transform matrix
|
||||
joint_node.save()
|
||||
|
||||
if not include_unreferenced_bones:
|
||||
needed_heirarchy = set()
|
||||
for skel_node in joint_nodes.values():
|
||||
skel_node = skel_node.xmlnode
|
||||
if skel_node.get('name') in skin_seg['joint_names']:
|
||||
# Add this joint and any ancestors the list of needed joints
|
||||
while skel_node is not None:
|
||||
needed_heirarchy.add(skel_node.get('name'))
|
||||
skel_node = skel_node.getparent()
|
||||
|
||||
for skel_node in joint_nodes.values():
|
||||
skel_node = skel_node.xmlnode
|
||||
if skel_node.get('name') not in needed_heirarchy:
|
||||
skel_node.getparent().remove(skel_node)
|
||||
|
||||
pelvis_offset = skin_seg.get('pelvis_offset')
|
||||
|
||||
# TODO: should we even do this here? It's not present in the collada, just
|
||||
# something that's specified in the uploader before conversion to LLMesh.
|
||||
if pelvis_offset and 'mPelvis' in joint_nodes:
|
||||
pelvis_node = joint_nodes['mPelvis']
|
||||
# Column-major!
|
||||
pelvis_node.matrix[3][2] += pelvis_offset
|
||||
pelvis_node.save()
|
||||
|
||||
|
||||
def _create_mat4_source(name: str, data: np.ndarray, semantic: str):
|
||||
# PyCollada has no way to make a source with a float4x4 semantic. Do it a bad way.
|
||||
# Note that collada demands column-major matrices whereas LLSD mesh has them row-major!
|
||||
source = collada.source.FloatSource(name, data, tuple(f"M{x}" for x in range(16)))
|
||||
accessor = source.xmlnode[1][0]
|
||||
for child in list(accessor):
|
||||
accessor.remove(child)
|
||||
accessor.append(E.param(name=semantic, type="float4x4"))
|
||||
return source
|
||||
|
||||
|
||||
def fix_weird_bind_matrices(skin_seg: SkinSegmentDict) -> None:
|
||||
"""
|
||||
Fix weird-looking bind matrices to have sensible scaling and rotations
|
||||
|
||||
Sometimes we get enormous inverse bind matrices (each component 10k+) and tiny
|
||||
bind shape matrix components. This detects inverse bind shape matrices
|
||||
with weird scales and tries to set them to what they "should" be without
|
||||
the weird inverted scaling.
|
||||
"""
|
||||
|
||||
# Sometimes we get mesh assets that have the vertex data naturally in y-up orientation,
|
||||
# and get re-oriented to z-up not through the bind shape matrix, but through the
|
||||
# transforms in the inverse bind matrices!
|
||||
#
|
||||
# Blender, for one, does not like this very much, and generally won't generate mesh
|
||||
# assets like this, as explained here https://developer.blender.org/T38660.
|
||||
# In vanilla Blender, these mesh assets will show up scaled and rotated _only_ according
|
||||
# to the bind shape matrix, which may end up with the model 25 meters tall and sitting
|
||||
# on its side.
|
||||
#
|
||||
# https://avalab.org/avastar/292/knowledge/compare-workbench/, while somewhat outdated,
|
||||
# has some information on rest pose vs default pose and scaling that I believe is relevant.
|
||||
# https://github.com/KhronosGroup/glTF-Blender-IO/issues/994 as well.
|
||||
#
|
||||
# While trying to figure out what was going on, I searched for something like
|
||||
# "inverse bind matrix scale collada", "bind pose scale blender", etc. Pretty much every
|
||||
# result was either a bug filed by, or a question asked by the creator of Avastar, or an SL user.
|
||||
# I think that says a lot about how annoying it is to author mesh for SL in particular.
|
||||
#
|
||||
# I spent a good month or so tearing my hair out over this wondering how these values could
|
||||
# even be possible. I wasn't sure how I should write mesh import code if I don't understand
|
||||
# how to interpret existing data, or how it even ended up the way it did. Turns out I wasn't
|
||||
# misinterpreting the data, the data really is just weird.
|
||||
#
|
||||
# I'd also had the idea that you could sniff which body a given rigged asset was meant
|
||||
# for by doing trivial matching on the inverse bind matrices, but obviously that isn't true!
|
||||
#
|
||||
# Basically:
|
||||
# 1) Maya is evil and generates evil, this evil bleeds into SL's assets through transforms.
|
||||
# 2) Blender is also evil, but in a manner that doesn't agree with Maya's evil.
|
||||
# 3) Collada was a valiant effort, but is evil in practice. Seemingly simple Collada
|
||||
# files are interpreted completely differently by Blender, Maya, and sometimes SL.
|
||||
# 4) Those three evils collude to make an interop nightmare for everyone like "oh my rigger
|
||||
# rigs using Maya and now my model is huge and all my normals are fucked on reimport"
|
||||
# 5) Yes, there's still good reasons to be using Avastar in 2022 even though nobody authoring
|
||||
# rigged mesh for any other use has to use something similar.
|
||||
|
||||
if not skin_seg['joint_names']:
|
||||
return
|
||||
|
||||
# TODO: calculate the correct inverse bind matrix scale & rotations from avatar_skeleton.xml
|
||||
# definitions. If the rotation and scale factors are the same across all inverse bind matrices then
|
||||
# they can be moved over to the bind shape matrix to keep Blender happy.
|
||||
# Maybe add a scaled / rotated empty as a parent for the armature instead?
|
||||
return
|
||||
|
||||
|
||||
def main():
|
||||
# Take an llmesh file as an argument and spit out basename-converted.dae
|
||||
with open(sys.argv[1], "rb") as f:
|
||||
reader = BufferReader("<", f.read())
|
||||
|
||||
mesh = mesh_to_collada(reader.read(LLMeshSerializer(parse_segment_contents=True)))
|
||||
mesh.write(sys.argv[1].rsplit(".", 1)[0] + "-converted.dae")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
485
hippolyzer/lib/base/data/male_collada_joints.xml
Normal file
485
hippolyzer/lib/base/data/male_collada_joints.xml
Normal file
@@ -0,0 +1,485 @@
|
||||
<!-- from http://wiki.secondlife.com/wiki/Project_Bento_Resources_and_Information collada -->
|
||||
<node id="Avatar" name="Avatar" type="NODE" xmlns="http://www.collada.org/2005/11/COLLADASchema">
|
||||
<translate sid="location">0 0 0</translate>
|
||||
<rotate sid="rotationZ">0 0 1 0</rotate>
|
||||
<rotate sid="rotationY">0 1 0 0</rotate>
|
||||
<rotate sid="rotationX">1 0 0 0</rotate>
|
||||
<scale sid="scale">1 1 1</scale>
|
||||
<node id="mPelvis" name="mPelvis" sid="mPelvis" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 1.067 0 0 0 1</matrix>
|
||||
<node id="PELVIS" name="PELVIS" sid="PELVIS" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.01 0 1 0 0 0 0 1 -0.02 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="BUTT" name="BUTT" sid="BUTT" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.06 0 1 0 0 0 0 1 -0.1 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mSpine1" name="mSpine1" sid="mSpine1" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0.084 0 0 0 1</matrix>
|
||||
<node id="mSpine2" name="mSpine2" sid="mSpine2" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 -0.084 0 0 0 1</matrix>
|
||||
<node id="mTorso" name="mTorso" sid="mTorso" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0.084 0 0 0 1</matrix>
|
||||
<node id="BELLY" name="BELLY" sid="BELLY" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.028 0 1 0 0 0 0 1 0.04 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="LEFT_HANDLE" name="LEFT_HANDLE" sid="LEFT_HANDLE" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0.1 0 0 1 0.058 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="RIGHT_HANDLE" name="RIGHT_HANDLE" sid="RIGHT_HANDLE" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 -0.1 0 0 1 0.058 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="LOWER_BACK" name="LOWER_BACK" sid="LOWER_BACK" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0.023 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mSpine3" name="mSpine3" sid="mSpine3" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.015 0 1 0 0 0 0 1 0.205 0 0 0 1</matrix>
|
||||
<node id="mSpine4" name="mSpine4" sid="mSpine4" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.015 0 1 0 0 0 0 1 -0.205 0 0 0 1</matrix>
|
||||
<node id="mChest" name="mChest" sid="mChest" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.015 0 1 0 0 0 0 1 0.205 0 0 0 1</matrix>
|
||||
<node id="CHEST" name="CHEST" sid="CHEST" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.028 0 1 0 0 0 0 1 0.07 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="LEFT_PEC" name="LEFT_PEC" sid="LEFT_PEC" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.119 0 1 0 0.082 0 0 1 0.042 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="RIGHT_PEC" name="RIGHT_PEC" sid="RIGHT_PEC" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.119 0 1 0 -0.082 0 0 1 0.042 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="UPPER_BACK" name="UPPER_BACK" sid="UPPER_BACK" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0.017 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mNeck" name="mNeck" sid="mNeck" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.01 0 1 0 0 0 0 1 0.251 0 0 0 1</matrix>
|
||||
<node id="NECK" name="NECK" sid="NECK" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0.02 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mHead" name="mHead" sid="mHead" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0.076 0 0 0 1</matrix>
|
||||
<node id="HEAD" name="HEAD" sid="HEAD" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.02 0 1 0 0 0 0 1 0.07 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mSkull" name="mSkull" sid="mSkull" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0.079 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mEyeRight" name="mEyeRight" sid="mEyeRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.098 0 1 0 -0.036 0 0 1 0.079 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mEyeLeft" name="mEyeLeft" sid="mEyeLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.098 0 1 0 0.036 0 0 1 0.079 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceRoot" name="mFaceRoot" sid="mFaceRoot" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.025 0 1 0 0 0 0 1 0.045 0 0 0 1</matrix>
|
||||
<node id="mFaceEyeAltRight" name="mFaceEyeAltRight" sid="mFaceEyeAltRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.073 0 1 0 -0.036 0 0 1 0.034 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyeAltLeft" name="mFaceEyeAltLeft" sid="mFaceEyeAltLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.073 0 1 0 0.036 0 0 1 0.034 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceForeheadLeft" name="mFaceForeheadLeft" sid="mFaceForeheadLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.061 0 1 0 0.035 0 0 1 0.083 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceForeheadRight" name="mFaceForeheadRight" sid="mFaceForeheadRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.061 0 1 0 -0.035 0 0 1 0.083 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyebrowOuterLeft" name="mFaceEyebrowOuterLeft" sid="mFaceEyebrowOuterLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.064 0 1 0 0.051 0 0 1 0.048 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyebrowCenterLeft" name="mFaceEyebrowCenterLeft" sid="mFaceEyebrowCenterLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.07 0 1 0 0.043 0 0 1 0.056 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyebrowInnerLeft" name="mFaceEyebrowInnerLeft" sid="mFaceEyebrowInnerLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.075 0 1 0 0.022 0 0 1 0.051 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyebrowOuterRight" name="mFaceEyebrowOuterRight" sid="mFaceEyebrowOuterRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.064 0 1 0 -0.051 0 0 1 0.048 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyebrowCenterRight" name="mFaceEyebrowCenterRight" sid="mFaceEyebrowCenterRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.07 0 1 0 -0.043 0 0 1 0.056 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyebrowInnerRight" name="mFaceEyebrowInnerRight" sid="mFaceEyebrowInnerRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.075 0 1 0 -0.022 0 0 1 0.051 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyeLidUpperLeft" name="mFaceEyeLidUpperLeft" sid="mFaceEyeLidUpperLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.073 0 1 0 0.036 0 0 1 0.034 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyeLidLowerLeft" name="mFaceEyeLidLowerLeft" sid="mFaceEyeLidLowerLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.073 0 1 0 0.036 0 0 1 0.034 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyeLidUpperRight" name="mFaceEyeLidUpperRight" sid="mFaceEyeLidUpperRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.073 0 1 0 -0.036 0 0 1 0.034 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyeLidLowerRight" name="mFaceEyeLidLowerRight" sid="mFaceEyeLidLowerRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.073 0 1 0 -0.036 0 0 1 0.034 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEar1Left" name="mFaceEar1Left" sid="mFaceEar1Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0.08 0 0 1 0.002 0 0 0 1</matrix>
|
||||
<node id="mFaceEar2Left" name="mFaceEar2Left" sid="mFaceEar2Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.019 0 1 0 0.018 0 0 1 0.025 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mFaceEar1Right" name="mFaceEar1Right" sid="mFaceEar1Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 -0.08 0 0 1 0.002 0 0 0 1</matrix>
|
||||
<node id="mFaceEar2Right" name="mFaceEar2Right" sid="mFaceEar2Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.019 0 1 0 -0.018 0 0 1 0.025 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mFaceNoseLeft" name="mFaceNoseLeft" sid="mFaceNoseLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.086 0 1 0 0.015 0 0 1 -0.004 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceNoseCenter" name="mFaceNoseCenter" sid="mFaceNoseCenter" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.102 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceNoseRight" name="mFaceNoseRight" sid="mFaceNoseRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.086 0 1 0 -0.015 0 0 1 -0.004 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceCheekLowerLeft" name="mFaceCheekLowerLeft" sid="mFaceCheekLowerLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.05 0 1 0 0.034 0 0 1 -0.031 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceCheekUpperLeft" name="mFaceCheekUpperLeft" sid="mFaceCheekUpperLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.07 0 1 0 0.034 0 0 1 -0.005 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceCheekLowerRight" name="mFaceCheekLowerRight" sid="mFaceCheekLowerRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.05 0 1 0 -0.034 0 0 1 -0.031 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceCheekUpperRight" name="mFaceCheekUpperRight" sid="mFaceCheekUpperRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.07 0 1 0 -0.034 0 0 1 -0.005 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceJaw" name="mFaceJaw" sid="mFaceJaw" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.001 0 1 0 0 0 0 1 -0.015 0 0 0 1</matrix>
|
||||
<node id="mFaceChin" name="mFaceChin" sid="mFaceChin" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.074 0 1 0 0 0 0 1 -0.054 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceTeethLower" name="mFaceTeethLower" sid="mFaceTeethLower" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.021 0 1 0 0 0 0 1 -0.039 0 0 0 1</matrix>
|
||||
<node id="mFaceLipLowerLeft" name="mFaceLipLowerLeft" sid="mFaceLipLowerLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.045 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceLipLowerRight" name="mFaceLipLowerRight" sid="mFaceLipLowerRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.045 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceLipLowerCenter" name="mFaceLipLowerCenter" sid="mFaceLipLowerCenter" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.045 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceTongueBase" name="mFaceTongueBase" sid="mFaceTongueBase" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.039 0 1 0 0 0 0 1 0.005 0 0 0 1</matrix>
|
||||
<node id="mFaceTongueTip" name="mFaceTongueTip" sid="mFaceTongueTip" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.022 0 1 0 0 0 0 1 0.007 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mFaceJawShaper" name="mFaceJawShaper" sid="mFaceJawShaper" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceForeheadCenter" name="mFaceForeheadCenter" sid="mFaceForeheadCenter" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.069 0 1 0 0 0 0 1 0.065 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceNoseBase" name="mFaceNoseBase" sid="mFaceNoseBase" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.094 0 1 0 0 0 0 1 -0.016 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceTeethUpper" name="mFaceTeethUpper" sid="mFaceTeethUpper" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.02 0 1 0 0 0 0 1 -0.03 0 0 0 1</matrix>
|
||||
<node id="mFaceLipUpperLeft" name="mFaceLipUpperLeft" sid="mFaceLipUpperLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.045 0 1 0 0 0 0 1 -0.003 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceLipUpperRight" name="mFaceLipUpperRight" sid="mFaceLipUpperRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.045 0 1 0 0 0 0 1 -0.003 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceLipCornerLeft" name="mFaceLipCornerLeft" sid="mFaceLipCornerLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.028 0 1 0 -0.019 0 0 1 -0.01 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceLipCornerRight" name="mFaceLipCornerRight" sid="mFaceLipCornerRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.028 0 1 0 0.019 0 0 1 -0.01 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceLipUpperCenter" name="mFaceLipUpperCenter" sid="mFaceLipUpperCenter" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.045 0 1 0 0 0 0 1 -0.003 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mFaceEyecornerInnerLeft" name="mFaceEyecornerInnerLeft" sid="mFaceEyecornerInnerLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.075 0 1 0 0.017 0 0 1 0.032 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceEyecornerInnerRight" name="mFaceEyecornerInnerRight" sid="mFaceEyecornerInnerRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.075 0 1 0 -0.017 0 0 1 0.032 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFaceNoseBridge" name="mFaceNoseBridge" sid="mFaceNoseBridge" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.091 0 1 0 0 0 0 1 0.02 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mCollarLeft" name="mCollarLeft" sid="mCollarLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.021 0 1 0 0.085 0 0 1 0.165 0 0 0 1</matrix>
|
||||
<node id="L_CLAVICLE" name="L_CLAVICLE" sid="L_CLAVICLE" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.02 0 1 0 0 0 0 1 0.02 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mShoulderLeft" name="mShoulderLeft" sid="mShoulderLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0.079 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="L_UPPER_ARM" name="L_UPPER_ARM" sid="L_UPPER_ARM" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0.12 0 0 1 0.01 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mElbowLeft" name="mElbowLeft" sid="mElbowLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0.248 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="L_LOWER_ARM" name="L_LOWER_ARM" sid="L_LOWER_ARM" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0.1 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mWristLeft" name="mWristLeft" sid="mWristLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 0.205 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="L_HAND" name="L_HAND" sid="L_HAND" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.01 0 1 0 0.05 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mHandMiddle1Left" name="mHandMiddle1Left" sid="mHandMiddle1Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.013 0 1 0 0.101 0 0 1 0.015 0 0 0 1</matrix>
|
||||
<node id="mHandMiddle2Left" name="mHandMiddle2Left" sid="mHandMiddle2Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.001 0 1 0 0.04 0 0 1 -0.006 0 0 0 1</matrix>
|
||||
<node id="mHandMiddle3Left" name="mHandMiddle3Left" sid="mHandMiddle3Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.001 0 1 0 0.049 0 0 1 -0.008 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHandIndex1Left" name="mHandIndex1Left" sid="mHandIndex1Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.038 0 1 0 0.097 0 0 1 0.015 0 0 0 1</matrix>
|
||||
<node id="mHandIndex2Left" name="mHandIndex2Left" sid="mHandIndex2Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.017 0 1 0 0.036 0 0 1 -0.006 0 0 0 1</matrix>
|
||||
<node id="mHandIndex3Left" name="mHandIndex3Left" sid="mHandIndex3Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.014 0 1 0 0.032 0 0 1 -0.006 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHandRing1Left" name="mHandRing1Left" sid="mHandRing1Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.01 0 1 0 0.099 0 0 1 0.009 0 0 0 1</matrix>
|
||||
<node id="mHandRing2Left" name="mHandRing2Left" sid="mHandRing2Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.013 0 1 0 0.038 0 0 1 -0.008 0 0 0 1</matrix>
|
||||
<node id="mHandRing3Left" name="mHandRing3Left" sid="mHandRing3Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.013 0 1 0 0.04 0 0 1 -0.009 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHandPinky1Left" name="mHandPinky1Left" sid="mHandPinky1Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.031 0 1 0 0.095 0 0 1 0.003 0 0 0 1</matrix>
|
||||
<node id="mHandPinky2Left" name="mHandPinky2Left" sid="mHandPinky2Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.024 0 1 0 0.025 0 0 1 -0.006 0 0 0 1</matrix>
|
||||
<node id="mHandPinky3Left" name="mHandPinky3Left" sid="mHandPinky3Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.015 0 1 0 0.018 0 0 1 -0.004 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHandThumb1Left" name="mHandThumb1Left" sid="mHandThumb1Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.031 0 1 0 0.026 0 0 1 0.004 0 0 0 1</matrix>
|
||||
<node id="mHandThumb2Left" name="mHandThumb2Left" sid="mHandThumb2Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.028 0 1 0 0.032 0 0 1 -0.001 0 0 0 1</matrix>
|
||||
<node id="mHandThumb3Left" name="mHandThumb3Left" sid="mHandThumb3Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.023 0 1 0 0.031 0 0 1 -0.001 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mCollarRight" name="mCollarRight" sid="mCollarRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.021 0 1 0 -0.085 0 0 1 0.165 0 0 0 1</matrix>
|
||||
<node id="R_CLAVICLE" name="R_CLAVICLE" sid="R_CLAVICLE" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.02 0 1 0 0 0 0 1 0.02 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mShoulderRight" name="mShoulderRight" sid="mShoulderRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 -0.079 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="R_UPPER_ARM" name="R_UPPER_ARM" sid="R_UPPER_ARM" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 -0.12 0 0 1 0.01 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mElbowRight" name="mElbowRight" sid="mElbowRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 -0.248 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="R_LOWER_ARM" name="R_LOWER_ARM" sid="R_LOWER_ARM" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 -0.1 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mWristRight" name="mWristRight" sid="mWristRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0 0 1 0 -0.205 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="R_HAND" name="R_HAND" sid="R_HAND" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.01 0 1 0 -0.05 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mHandMiddle1Right" name="mHandMiddle1Right" sid="mHandMiddle1Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.013 0 1 0 -0.101 0 0 1 0.015 0 0 0 1</matrix>
|
||||
<node id="mHandMiddle2Right" name="mHandMiddle2Right" sid="mHandMiddle2Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.001 0 1 0 -0.04 0 0 1 -0.006 0 0 0 1</matrix>
|
||||
<node id="mHandMiddle3Right" name="mHandMiddle3Right" sid="mHandMiddle3Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.001 0 1 0 -0.049 0 0 1 -0.008 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHandIndex1Right" name="mHandIndex1Right" sid="mHandIndex1Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.038 0 1 0 -0.097 0 0 1 0.015 0 0 0 1</matrix>
|
||||
<node id="mHandIndex2Right" name="mHandIndex2Right" sid="mHandIndex2Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.017 0 1 0 -0.036 0 0 1 -0.006 0 0 0 1</matrix>
|
||||
<node id="mHandIndex3Right" name="mHandIndex3Right" sid="mHandIndex3Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.014 0 1 0 -0.032 0 0 1 -0.006 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHandRing1Right" name="mHandRing1Right" sid="mHandRing1Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.01 0 1 0 -0.099 0 0 1 0.009 0 0 0 1</matrix>
|
||||
<node id="mHandRing2Right" name="mHandRing2Right" sid="mHandRing2Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.013 0 1 0 -0.038 0 0 1 -0.008 0 0 0 1</matrix>
|
||||
<node id="mHandRing3Right" name="mHandRing3Right" sid="mHandRing3Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.013 0 1 0 -0.04 0 0 1 -0.009 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHandPinky1Right" name="mHandPinky1Right" sid="mHandPinky1Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.031 0 1 0 -0.095 0 0 1 0.003 0 0 0 1</matrix>
|
||||
<node id="mHandPinky2Right" name="mHandPinky2Right" sid="mHandPinky2Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.024 0 1 0 -0.025 0 0 1 -0.006 0 0 0 1</matrix>
|
||||
<node id="mHandPinky3Right" name="mHandPinky3Right" sid="mHandPinky3Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.015 0 1 0 -0.018 0 0 1 -0.004 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHandThumb1Right" name="mHandThumb1Right" sid="mHandThumb1Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.031 0 1 0 -0.026 0 0 1 0.004 0 0 0 1</matrix>
|
||||
<node id="mHandThumb2Right" name="mHandThumb2Right" sid="mHandThumb2Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.028 0 1 0 -0.032 0 0 1 -0.001 0 0 0 1</matrix>
|
||||
<node id="mHandThumb3Right" name="mHandThumb3Right" sid="mHandThumb3Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.023 0 1 0 -0.031 0 0 1 -0.001 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mWingsRoot" name="mWingsRoot" sid="mWingsRoot" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.014 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="mWing1Left" name="mWing1Left" sid="mWing1Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.099 0 1 0 0.105 0 0 1 0.181 0 0 0 1</matrix>
|
||||
<node id="mWing2Left" name="mWing2Left" sid="mWing2Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.168 0 1 0 0.169 0 0 1 0.067 0 0 0 1</matrix>
|
||||
<node id="mWing3Left" name="mWing3Left" sid="mWing3Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.181 0 1 0 0.183 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="mWing4Left" name="mWing4Left" sid="mWing4Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.171 0 1 0 0.173 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mWing4FanLeft" name="mWing4FanLeft" sid="mWing4FanLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.171 0 1 0 0.173 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mWing1Right" name="mWing1Right" sid="mWing1Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.099 0 1 0 -0.105 0 0 1 0.181 0 0 0 1</matrix>
|
||||
<node id="mWing2Right" name="mWing2Right" sid="mWing2Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.168 0 1 0 -0.169 0 0 1 0.067 0 0 0 1</matrix>
|
||||
<node id="mWing3Right" name="mWing3Right" sid="mWing3Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.181 0 1 0 -0.183 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="mWing4Right" name="mWing4Right" sid="mWing4Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.171 0 1 0 -0.173 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mWing4FanRight" name="mWing4FanRight" sid="mWing4FanRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.171 0 1 0 -0.173 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHipRight" name="mHipRight" sid="mHipRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.034 0 1 0 -0.129 0 0 1 -0.041 0 0 0 1</matrix>
|
||||
<node id="R_UPPER_LEG" name="R_UPPER_LEG" sid="R_UPPER_LEG" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.02 0 1 0 0.05 0 0 1 -0.22 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mKneeRight" name="mKneeRight" sid="mKneeRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.001 0 1 0 0.049 0 0 1 -0.491 0 0 0 1</matrix>
|
||||
<node id="R_LOWER_LEG" name="R_LOWER_LEG" sid="R_LOWER_LEG" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.02 0 1 0 0 0 0 1 -0.2 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mAnkleRight" name="mAnkleRight" sid="mAnkleRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.029 0 1 0 0 0 0 1 -0.468 0 0 0 1</matrix>
|
||||
<node id="R_FOOT" name="R_FOOT" sid="R_FOOT" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.077 0 1 0 0 0 0 1 -0.041 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFootRight" name="mFootRight" sid="mFootRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.112 0 1 0 0 0 0 1 -0.061 0 0 0 1</matrix>
|
||||
<node id="mToeRight" name="mToeRight" sid="mToeRight" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.109 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHipLeft" name="mHipLeft" sid="mHipLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.034 0 1 0 0.127 0 0 1 -0.041 0 0 0 1</matrix>
|
||||
<node id="L_UPPER_LEG" name="L_UPPER_LEG" sid="L_UPPER_LEG" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.02 0 1 0 -0.05 0 0 1 -0.22 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mKneeLeft" name="mKneeLeft" sid="mKneeLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.001 0 1 0 -0.046 0 0 1 -0.491 0 0 0 1</matrix>
|
||||
<node id="L_LOWER_LEG" name="L_LOWER_LEG" sid="L_LOWER_LEG" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.02 0 1 0 0 0 0 1 -0.2 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mAnkleLeft" name="mAnkleLeft" sid="mAnkleLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.029 0 1 0 0.001 0 0 1 -0.468 0 0 0 1</matrix>
|
||||
<node id="L_FOOT" name="L_FOOT" sid="L_FOOT" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.077 0 1 0 0 0 0 1 -0.041 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mFootLeft" name="mFootLeft" sid="mFootLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.112 0 1 0 0 0 0 1 -0.061 0 0 0 1</matrix>
|
||||
<node id="mToeLeft" name="mToeLeft" sid="mToeLeft" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.109 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mTail1" name="mTail1" sid="mTail1" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.116 0 1 0 0 0 0 1 0.047 0 0 0 1</matrix>
|
||||
<node id="mTail2" name="mTail2" sid="mTail2" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.197 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="mTail3" name="mTail3" sid="mTail3" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.168 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="mTail4" name="mTail4" sid="mTail4" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.142 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="mTail5" name="mTail5" sid="mTail5" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.112 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
<node id="mTail6" name="mTail6" sid="mTail6" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.094 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mGroin" name="mGroin" sid="mGroin" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.064 0 1 0 0 0 0 1 -0.097 0 0 0 1</matrix>
|
||||
</node>
|
||||
<node id="mHindLimbsRoot" name="mHindLimbsRoot" sid="mHindLimbsRoot" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.2 0 1 0 0 0 0 1 0.084 0 0 0 1</matrix>
|
||||
<node id="mHindLimb1Left" name="mHindLimb1Left" sid="mHindLimb1Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.204 0 1 0 0.129 0 0 1 -0.125 0 0 0 1</matrix>
|
||||
<node id="mHindLimb2Left" name="mHindLimb2Left" sid="mHindLimb2Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.002 0 1 0 -0.046 0 0 1 -0.491 0 0 0 1</matrix>
|
||||
<node id="mHindLimb3Left" name="mHindLimb3Left" sid="mHindLimb3Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.03 0 1 0 -0.003 0 0 1 -0.468 0 0 0 1</matrix>
|
||||
<node id="mHindLimb4Left" name="mHindLimb4Left" sid="mHindLimb4Left" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.112 0 1 0 0 0 0 1 -0.061 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node id="mHindLimb1Right" name="mHindLimb1Right" sid="mHindLimb1Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.204 0 1 0 -0.129 0 0 1 -0.125 0 0 0 1</matrix>
|
||||
<node id="mHindLimb2Right" name="mHindLimb2Right" sid="mHindLimb2Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.002 0 1 0 0.046 0 0 1 -0.491 0 0 0 1</matrix>
|
||||
<node id="mHindLimb3Right" name="mHindLimb3Right" sid="mHindLimb3Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 -0.03 0 1 0 0.003 0 0 1 -0.468 0 0 0 1</matrix>
|
||||
<node id="mHindLimb4Right" name="mHindLimb4Right" sid="mHindLimb4Right" type="JOINT">
|
||||
<matrix sid="transform">1 0 0 0.112 0 1 0 0 0 0 1 -0.061 0 0 0 1</matrix>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
@@ -18,6 +18,8 @@ You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
import enum
|
||||
import hashlib
|
||||
@@ -27,6 +29,7 @@ import math
|
||||
from typing import *
|
||||
|
||||
import recordclass
|
||||
import transformations
|
||||
|
||||
logger = getLogger('hippolyzer.lib.base.datatypes')
|
||||
|
||||
@@ -36,12 +39,13 @@ class _IterableStub:
|
||||
__iter__: Callable
|
||||
|
||||
|
||||
class TupleCoord(recordclass.datatuple, _IterableStub): # type: ignore
|
||||
__options__ = {
|
||||
"fast_new": False,
|
||||
}
|
||||
RAD_TO_DEG = 180 / math.pi
|
||||
|
||||
|
||||
class TupleCoord(recordclass.RecordClass, _IterableStub):
|
||||
def __init__(self, *args):
|
||||
# Only to help typing, doesn't actually do anything.
|
||||
# All the important stuff happens in `__new__()`
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
@@ -58,6 +62,9 @@ class TupleCoord(recordclass.datatuple, _IterableStub): # type: ignore
|
||||
def __abs__(self):
|
||||
return self.__class__(*(abs(x) for x in self))
|
||||
|
||||
def __neg__(self):
|
||||
return self.__class__(*(-x for x in self))
|
||||
|
||||
def __add__(self, other):
|
||||
return self.__class__(*(x + y for x, y in zip(self, other)))
|
||||
|
||||
@@ -215,6 +222,15 @@ class Quaternion(TupleCoord):
|
||||
)
|
||||
return super().__mul__(other)
|
||||
|
||||
@classmethod
|
||||
def from_transformations(cls, coord) -> Quaternion:
|
||||
"""Convert to W (S) last form"""
|
||||
return cls(coord[1], coord[2], coord[3], coord[0])
|
||||
|
||||
def to_transformations(self) -> Tuple[float, float, float, float]:
|
||||
"""Convert to W (S) first form for use with the transformations lib"""
|
||||
return self.W, self.X, self.Y, self.Z
|
||||
|
||||
@classmethod
|
||||
def from_euler(cls, roll, pitch, yaw, degrees=False):
|
||||
if degrees:
|
||||
@@ -236,6 +252,9 @@ class Quaternion(TupleCoord):
|
||||
|
||||
return cls(X=x, Y=y, Z=z, W=w)
|
||||
|
||||
def to_euler(self) -> Vector3:
|
||||
return Vector3(*transformations.euler_from_quaternion(self.to_transformations()))
|
||||
|
||||
def data(self, wanted_components=None):
|
||||
if wanted_components == 3:
|
||||
return self.X, self.Y, self.Z
|
||||
@@ -244,6 +263,7 @@ class Quaternion(TupleCoord):
|
||||
|
||||
class UUID(uuid.UUID):
|
||||
_NULL_UUID_STR = '00000000-0000-0000-0000-000000000000'
|
||||
ZERO: UUID
|
||||
__slots__ = ()
|
||||
|
||||
def __init__(self, val: Union[uuid.UUID, str, None] = None, bytes=None, int=None):
|
||||
@@ -268,18 +288,25 @@ class UUID(uuid.UUID):
|
||||
return self.__class__(int=self.int ^ other.int)
|
||||
|
||||
|
||||
UUID.ZERO = UUID()
|
||||
|
||||
|
||||
class JankStringyBytes(bytes):
|
||||
"""
|
||||
Treat bytes as UTF8 if used in string context
|
||||
|
||||
Sinful, but necessary evil for now since templates don't specify what's
|
||||
binary and what's a string.
|
||||
binary and what's a string. There are also certain fields where the value
|
||||
may be either binary _or_ a string, depending on the context.
|
||||
"""
|
||||
__slots__ = ()
|
||||
|
||||
def __str__(self):
|
||||
return self.rstrip(b"\x00").decode("utf8", errors="replace")
|
||||
|
||||
def __bool__(self):
|
||||
return not (super().__eq__(b"") or super().__eq__(b"\x00"))
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, str):
|
||||
return str(self) == other
|
||||
@@ -288,6 +315,41 @@ class JankStringyBytes(bytes):
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __contains__(self, item):
|
||||
if isinstance(item, str):
|
||||
return item in str(self)
|
||||
return item in bytes(self)
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, bytes):
|
||||
return JankStringyBytes(bytes(self) + other)
|
||||
return str(self) + other
|
||||
|
||||
def __radd__(self, other):
|
||||
if isinstance(other, bytes):
|
||||
return JankStringyBytes(other + bytes(self))
|
||||
return other + str(self)
|
||||
|
||||
def lower(self):
|
||||
return str(self).lower()
|
||||
|
||||
def upper(self):
|
||||
return str(self).upper()
|
||||
|
||||
def startswith(self, __prefix, __start=None, __end=None):
|
||||
if __start or __end:
|
||||
raise RuntimeError("Can't handle __start or __end")
|
||||
if isinstance(__prefix, str):
|
||||
return str(self).startswith(__prefix)
|
||||
return self.startswith(__prefix)
|
||||
|
||||
def endswith(self, __prefix, __start=None, __end=None):
|
||||
if __start or __end:
|
||||
raise RuntimeError("Can't handle __start or __end")
|
||||
if isinstance(__prefix, str):
|
||||
return str(self).endswith(__prefix)
|
||||
return self.endswith(__prefix)
|
||||
|
||||
|
||||
class RawBytes(bytes):
|
||||
__slots__ = ()
|
||||
@@ -336,7 +398,7 @@ def flags_to_pod(flag_cls: Type[enum.IntFlag], val: int) -> Tuple[Union[str, int
|
||||
return tuple(flag.name for flag in iter(flag_cls) if val & flag.value) + extra
|
||||
|
||||
|
||||
class TaggedUnion(recordclass.datatuple): # type: ignore
|
||||
class TaggedUnion(recordclass.RecordClass):
|
||||
tag: Any
|
||||
value: Any
|
||||
|
||||
@@ -344,5 +406,5 @@ class TaggedUnion(recordclass.datatuple): # type: ignore
|
||||
__all__ = [
|
||||
"Vector3", "Vector4", "Vector2", "Quaternion", "TupleCoord",
|
||||
"UUID", "RawBytes", "StringEnum", "JankStringyBytes", "TaggedUnion",
|
||||
"IntEnum", "IntFlag", "flags_to_pod", "Pretty"
|
||||
"IntEnum", "IntFlag", "flags_to_pod", "Pretty", "RAD_TO_DEG"
|
||||
]
|
||||
|
||||
@@ -18,17 +18,20 @@ You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from logging import getLogger
|
||||
from hippolyzer.lib.base.helpers import create_logged_task
|
||||
|
||||
logger = getLogger('utilities.events')
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Event:
|
||||
""" an object containing data which will be passed out to all subscribers """
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, name=None):
|
||||
self.subscribers = []
|
||||
self.name = name
|
||||
|
||||
def subscribe(self, handler, *args, one_shot=False, predicate=None, **kwargs):
|
||||
""" establish the subscribers (handlers) to this event """
|
||||
@@ -38,7 +41,8 @@ class Event:
|
||||
|
||||
return self
|
||||
|
||||
def _handler_key(self, handler):
|
||||
@staticmethod
|
||||
def _handler_key(handler):
|
||||
return handler[:3]
|
||||
|
||||
def unsubscribe(self, handler, *args, **kwargs):
|
||||
@@ -52,24 +56,37 @@ class Event:
|
||||
raise ValueError(f"Handler {handler!r} is not subscribed to this event.")
|
||||
return self
|
||||
|
||||
def _create_async_wrapper(self, handler, args, inner_args, kwargs):
|
||||
# Note that unsubscription may be delayed due to asyncio scheduling :)
|
||||
async def _run_handler_wrapper():
|
||||
unsubscribe = await handler(args, *inner_args, **kwargs)
|
||||
if unsubscribe:
|
||||
_ = self.unsubscribe(handler, *inner_args, **kwargs)
|
||||
return _run_handler_wrapper
|
||||
|
||||
def notify(self, args):
|
||||
for handler in self.subscribers[:]:
|
||||
instance, inner_args, kwargs, one_shot, predicate = handler
|
||||
for subscriber in self.subscribers[:]:
|
||||
handler, inner_args, kwargs, one_shot, predicate = subscriber
|
||||
if predicate and not predicate(args):
|
||||
continue
|
||||
if one_shot:
|
||||
self.unsubscribe(instance, *inner_args, **kwargs)
|
||||
if instance(args, *inner_args, **kwargs):
|
||||
self.unsubscribe(instance, *inner_args, **kwargs)
|
||||
self.unsubscribe(handler, *inner_args, **kwargs)
|
||||
if asyncio.iscoroutinefunction(handler):
|
||||
create_logged_task(self._create_async_wrapper(handler, args, inner_args, kwargs)(), self.name, LOG)
|
||||
else:
|
||||
try:
|
||||
if handler(args, *inner_args, **kwargs) and not one_shot:
|
||||
self.unsubscribe(handler, *inner_args, **kwargs)
|
||||
except:
|
||||
# One handler failing shouldn't prevent notification of other handlers.
|
||||
LOG.exception(f"Failed in handler for {self.name}")
|
||||
|
||||
def get_subscriber_count(self):
|
||||
def __len__(self):
|
||||
return len(self.subscribers)
|
||||
|
||||
def clear_subscribers(self):
|
||||
self.subscribers.clear()
|
||||
return self
|
||||
|
||||
__iadd__ = subscribe
|
||||
__isub__ = unsubscribe
|
||||
__call__ = notify
|
||||
__len__ = get_subscriber_count
|
||||
|
||||
@@ -176,7 +176,7 @@ class MessageTemplateNotFound(MessageSystemError):
|
||||
self.template = template
|
||||
|
||||
def __str__(self):
|
||||
return "No message template found, context: '%s'" % self.context
|
||||
return "No message template found for %s, context: '%s'" % (self.template, self.context)
|
||||
|
||||
|
||||
class MessageTemplateParsingError(MessageSystemError):
|
||||
|
||||
528
hippolyzer/lib/base/gltftools.py
Normal file
528
hippolyzer/lib/base/gltftools.py
Normal file
@@ -0,0 +1,528 @@
|
||||
"""
|
||||
WIP LLMesh -> glTF converter, for testing eventual glTF -> LLMesh conversion logic.
|
||||
"""
|
||||
# TODO:
|
||||
# * Simple tests
|
||||
# * Round-tripping skinning data from Blender-compatible glTF back to LLMesh (maybe through rig retargeting?)
|
||||
# * Panda3D-glTF viewer for LLMesh? The glTFs seem to work fine in Panda3D-glTF's `gltf-viewer`.
|
||||
# * Check if skew and projection components of transform matrices are ignored in practice as the spec requires.
|
||||
# I suppose this would render some real assets impossible to represent with glTF.
|
||||
|
||||
import dataclasses
|
||||
import math
|
||||
import pprint
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import *
|
||||
|
||||
import gltflib
|
||||
import numpy as np
|
||||
import transformations
|
||||
|
||||
from hippolyzer.lib.base.datatypes import Vector3
|
||||
from hippolyzer.lib.base.mesh import (
|
||||
LLMeshSerializer, MeshAsset, positions_from_domain, SkinSegmentDict, VertexWeight, llsd_to_mat4
|
||||
)
|
||||
from hippolyzer.lib.base.mesh_skeleton import AVATAR_SKELETON
|
||||
from hippolyzer.lib.base.serialization import BufferReader
|
||||
|
||||
|
||||
class IdentityList(list):
|
||||
"""
|
||||
List, but does index() by object identity, not equality
|
||||
|
||||
GLTF references objects by their index within some list, but we prefer to pass around
|
||||
actual object references internally. If we don't do this, then when we try and get
|
||||
a GLTF reference to a given object via `.index()` then we could end up actually getting
|
||||
a reference to some other object that just happens to be equal. This was causing issues
|
||||
with all primitives ending up with the same material, due to the default material's value
|
||||
being the same across all primitives.
|
||||
"""
|
||||
def index(self, value, start: Optional[int] = None, stop: Optional[int] = None) -> int:
|
||||
view = self[start:stop]
|
||||
for i, x in enumerate(view):
|
||||
if x is value:
|
||||
if start:
|
||||
return i + start
|
||||
return i
|
||||
raise ValueError(value)
|
||||
|
||||
|
||||
def sl_to_gltf_coords(coords):
|
||||
"""
|
||||
SL (X, Y, Z) -> GL (X, Z, Y), as GLTF commandeth
|
||||
|
||||
Note that this will only work when reordering axes, flipping an axis is more complicated.
|
||||
"""
|
||||
return coords[0], coords[2], coords[1], *coords[3:]
|
||||
|
||||
|
||||
def sl_to_gltf_uv(uv):
|
||||
"""Flip the V coordinate of a UV to match glTF convention"""
|
||||
return [uv[0], -uv[1]]
|
||||
|
||||
|
||||
def sl_mat4_to_gltf(mat: np.ndarray) -> List[float]:
|
||||
"""
|
||||
Convert an SL Mat4 to the glTF coordinate system
|
||||
|
||||
This should only be done immediately before storing the matrix in a glTF structure!
|
||||
"""
|
||||
# TODO: This is probably not correct. We definitely need to flip Z but there's
|
||||
# probably a better way to do it.
|
||||
decomp = [sl_to_gltf_coords(x) for x in transformations.decompose_matrix(mat)]
|
||||
trans = decomp[3]
|
||||
decomp[3] = (trans[0], trans[1], -trans[2])
|
||||
return list(transformations.compose_matrix(*decomp).flatten(order='F'))
|
||||
|
||||
|
||||
# Mat3 to convert points from SL coordinate space to GLTF coordinate space
|
||||
POINT_TO_GLTF_MAT = transformations.compose_matrix(angles=(-(math.pi / 2), 0, 0))[:3, :3]
|
||||
|
||||
|
||||
def sl_vec3_array_to_gltf(vec_list: np.ndarray) -> np.ndarray:
|
||||
new_array = []
|
||||
for x in vec_list:
|
||||
new_array.append(POINT_TO_GLTF_MAT.dot(x))
|
||||
return np.array(new_array)
|
||||
|
||||
|
||||
def sl_weights_to_gltf(sl_weights: List[List[VertexWeight]]) -> Tuple[np.ndarray, np.ndarray]:
|
||||
"""Convert SL Weights to separate JOINTS_0 and WEIGHTS_0 vec4 arrays"""
|
||||
joints = np.zeros((len(sl_weights), 4), dtype=np.uint8)
|
||||
weights = np.zeros((len(sl_weights), 4), dtype=np.float32)
|
||||
|
||||
for i, vert_weights in enumerate(sl_weights):
|
||||
# We need to re-normalize these since the quantization can mess them up
|
||||
collected_weights = []
|
||||
for j, vert_weight in enumerate(vert_weights):
|
||||
joints[i, j] = vert_weight.joint_idx
|
||||
collected_weights.append(vert_weight.weight)
|
||||
weight_sum = sum(collected_weights)
|
||||
if weight_sum:
|
||||
for j, weight in enumerate(collected_weights):
|
||||
weights[i, j] = weight / weight_sum
|
||||
|
||||
return joints, weights
|
||||
|
||||
|
||||
def normalize_vec3(a):
|
||||
norm = np.linalg.norm(a)
|
||||
if norm == 0:
|
||||
return a
|
||||
return a / norm
|
||||
|
||||
|
||||
def apply_bind_shape_matrix(bind_shape_matrix: np.ndarray, verts: np.ndarray, norms: np.ndarray) \
|
||||
-> Tuple[np.ndarray, np.ndarray]:
|
||||
"""
|
||||
Apply the bind shape matrix to the mesh data
|
||||
|
||||
glTF expects all verts and normals to be in armature-local space so that mesh data can be shared
|
||||
between differently-oriented armatures. Or something.
|
||||
# https://github.com/KhronosGroup/glTF-Blender-IO/issues/566#issuecomment-523119339
|
||||
|
||||
glTF also doesn't have a concept of a "bind shape matrix" like Collada does
|
||||
per its skinning docs, so we have to mix it into the mesh data manually.
|
||||
See https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_020_Skins.md
|
||||
"""
|
||||
scale, _, angles, translation, _ = transformations.decompose_matrix(bind_shape_matrix)
|
||||
scale_mat = transformations.compose_matrix(scale=scale)[:3, :3]
|
||||
rot_mat = transformations.euler_matrix(*angles)[:3, :3]
|
||||
rot_scale_mat = scale_mat @ np.linalg.inv(rot_mat)
|
||||
|
||||
# Apply the SRT transform to each vert
|
||||
verts = (verts @ rot_scale_mat) + translation
|
||||
|
||||
# Our scale is unlikely to be uniform, so we have to fix up our normals as well.
|
||||
# https://paroj.github.io/gltut/Illumination/Tut09%20Normal%20Transformation.html
|
||||
inv_transpose_mat = np.transpose(np.linalg.inv(bind_shape_matrix)[:3, :3])
|
||||
new_norms = [normalize_vec3(inv_transpose_mat @ norm) for norm in norms]
|
||||
|
||||
return verts, np.array(new_norms)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class JointContext:
|
||||
node: gltflib.Node
|
||||
# Original matrix for the bone, may have custom translation, but otherwise the same.
|
||||
orig_matrix: np.ndarray
|
||||
# xform that must be applied to inverse bind matrices to account for the changed bone
|
||||
fixup_matrix: np.ndarray
|
||||
|
||||
|
||||
JOINT_CONTEXT_DICT = Dict[str, JointContext]
|
||||
|
||||
|
||||
class GLTFBuilder:
|
||||
def __init__(self, blender_compatibility=False):
|
||||
self.scene = gltflib.Scene(nodes=IdentityList())
|
||||
self.model = gltflib.GLTFModel(
|
||||
asset=gltflib.Asset(version="2.0"),
|
||||
accessors=IdentityList(),
|
||||
nodes=IdentityList(),
|
||||
materials=IdentityList(),
|
||||
buffers=IdentityList(),
|
||||
bufferViews=IdentityList(),
|
||||
meshes=IdentityList(),
|
||||
skins=IdentityList(),
|
||||
scenes=IdentityList((self.scene,)),
|
||||
extensionsUsed=["KHR_materials_specular"],
|
||||
scene=0,
|
||||
)
|
||||
self.gltf = gltflib.GLTF(
|
||||
model=self.model,
|
||||
resources=IdentityList(),
|
||||
)
|
||||
self.blender_compatibility = blender_compatibility
|
||||
|
||||
def add_nodes_from_llmesh(self, mesh: MeshAsset, name: str, mesh_transform: Optional[np.ndarray] = None):
|
||||
"""Build a glTF version of a mesh asset, appending it and its armature to the scene root"""
|
||||
# TODO: mesh data instancing?
|
||||
# consider https://github.com/KhronosGroup/glTF-Blender-IO/issues/1634.
|
||||
if mesh_transform is None:
|
||||
mesh_transform = np.identity(4)
|
||||
|
||||
skin_seg: Optional[SkinSegmentDict] = mesh.segments.get('skin')
|
||||
skin = None
|
||||
if skin_seg:
|
||||
mesh_transform = llsd_to_mat4(skin_seg['bind_shape_matrix'])
|
||||
joint_ctxs = self.add_joints(skin_seg)
|
||||
|
||||
# Give our armature a root node and parent the pelvis to it
|
||||
armature_node = self.add_node("Armature")
|
||||
self.scene.nodes.append(self.model.nodes.index(armature_node))
|
||||
armature_node.children.append(self.model.nodes.index(joint_ctxs['mPelvis'].node))
|
||||
skin = self.add_skin("Armature", joint_ctxs, skin_seg)
|
||||
skin.skeleton = self.model.nodes.index(armature_node)
|
||||
|
||||
primitives = []
|
||||
# Just the high LOD for now
|
||||
for submesh in mesh.segments['high_lod']:
|
||||
verts = np.array(positions_from_domain(submesh['Position'], submesh['PositionDomain']))
|
||||
norms = np.array(submesh['Normal'])
|
||||
tris = np.array(submesh['TriangleList'])
|
||||
joints = np.array([])
|
||||
weights = np.array([])
|
||||
range_uv = np.array([])
|
||||
if "TexCoord0" in submesh:
|
||||
range_uv = np.array(positions_from_domain(submesh['TexCoord0'], submesh['TexCoord0Domain']))
|
||||
if 'Weights' in submesh:
|
||||
joints, weights = sl_weights_to_gltf(submesh['Weights'])
|
||||
|
||||
if skin:
|
||||
# Convert verts and norms to armature-local space
|
||||
verts, norms = apply_bind_shape_matrix(mesh_transform, verts, norms)
|
||||
|
||||
primitives.append(self.add_primitive(
|
||||
tris=tris,
|
||||
positions=verts,
|
||||
normals=norms,
|
||||
uvs=range_uv,
|
||||
joints=joints,
|
||||
weights=weights,
|
||||
))
|
||||
|
||||
mesh_node = self.add_node(
|
||||
name,
|
||||
self.add_mesh(name, primitives),
|
||||
transform=mesh_transform,
|
||||
)
|
||||
if skin:
|
||||
# Node translation isn't relevant, we're going to use the bind matrices
|
||||
# If you pull this into Blender you may want to untick "Guess Original Bind Pose",
|
||||
# it guesses that based on the inverse bind matrices which may have Maya poisoning.
|
||||
# TODO: Maybe we could automatically undo that by comparing expected bone scale and rot
|
||||
# to scale and rot in the inverse bind matrices, and applying fixups to the
|
||||
# bind shape matrix and inverse bind matrices?
|
||||
mesh_node.matrix = None
|
||||
mesh_node.skin = self.model.skins.index(skin)
|
||||
|
||||
self.scene.nodes.append(self.model.nodes.index(mesh_node))
|
||||
|
||||
def add_node(
|
||||
self,
|
||||
name: str,
|
||||
mesh: Optional[gltflib.Mesh] = None,
|
||||
transform: Optional[np.ndarray] = None,
|
||||
) -> gltflib.Node:
|
||||
node = gltflib.Node(
|
||||
name=name,
|
||||
mesh=self.model.meshes.index(mesh) if mesh else None,
|
||||
matrix=sl_mat4_to_gltf(transform) if transform is not None else None,
|
||||
children=[],
|
||||
)
|
||||
self.model.nodes.append(node)
|
||||
return node
|
||||
|
||||
def add_mesh(
|
||||
self,
|
||||
name: str,
|
||||
primitives: List[gltflib.Primitive],
|
||||
) -> gltflib.Mesh:
|
||||
for i, prim in enumerate(primitives):
|
||||
# Give the materials a name relating to what "face" they belong to
|
||||
self.model.materials[prim.material].name = f"{name}.{i:03}"
|
||||
mesh = gltflib.Mesh(name=name, primitives=primitives)
|
||||
self.model.meshes.append(mesh)
|
||||
return mesh
|
||||
|
||||
def add_primitive(
|
||||
self,
|
||||
tris: np.ndarray,
|
||||
positions: np.ndarray,
|
||||
normals: np.ndarray,
|
||||
uvs: np.ndarray,
|
||||
weights: np.ndarray,
|
||||
joints: np.ndarray,
|
||||
) -> gltflib.Primitive:
|
||||
# Make a Material for the primitive. Materials pretty much _are_ the primitives in
|
||||
# LLMesh, so just make them both in one go. We need a unique material for each primitive.
|
||||
material = gltflib.Material(
|
||||
pbrMetallicRoughness=gltflib.PBRMetallicRoughness(
|
||||
baseColorFactor=[1.0, 1.0, 1.0, 1.0],
|
||||
metallicFactor=0.0,
|
||||
roughnessFactor=0.0,
|
||||
),
|
||||
extensions={
|
||||
"KHR_materials_specular": {
|
||||
"specularFactor": 0.0,
|
||||
"specularColorFactor": [0, 0, 0]
|
||||
},
|
||||
}
|
||||
)
|
||||
self.model.materials.append(material)
|
||||
|
||||
attributes = gltflib.Attributes(
|
||||
POSITION=self.maybe_add_vec_array(sl_vec3_array_to_gltf(positions), gltflib.AccessorType.VEC3),
|
||||
NORMAL=self.maybe_add_vec_array(sl_vec3_array_to_gltf(normals), gltflib.AccessorType.VEC3),
|
||||
TEXCOORD_0=self.maybe_add_vec_array(np.array([sl_to_gltf_uv(uv) for uv in uvs]), gltflib.AccessorType.VEC2),
|
||||
JOINTS_0=self.maybe_add_vec_array(joints, gltflib.AccessorType.VEC4, gltflib.ComponentType.UNSIGNED_BYTE),
|
||||
WEIGHTS_0=self.maybe_add_vec_array(weights, gltflib.AccessorType.VEC4),
|
||||
)
|
||||
|
||||
return gltflib.Primitive(
|
||||
attributes=attributes,
|
||||
indices=self.model.accessors.index(self.add_scalars(tris)),
|
||||
material=self.model.materials.index(material),
|
||||
mode=gltflib.PrimitiveMode.TRIANGLES,
|
||||
)
|
||||
|
||||
def add_scalars(self, scalars: np.ndarray) -> gltflib.Accessor:
|
||||
"""
|
||||
Add a potentially multidimensional array of scalars, returning the accessor
|
||||
|
||||
Generally only used for triangle indices
|
||||
"""
|
||||
scalar_bytes = scalars.astype(np.uint32).flatten().tobytes()
|
||||
buffer_view = self.add_buffer_view(scalar_bytes, None)
|
||||
accessor = gltflib.Accessor(
|
||||
bufferView=self.model.bufferViews.index(buffer_view),
|
||||
componentType=gltflib.ComponentType.UNSIGNED_INT,
|
||||
count=scalars.size, # use the flattened size!
|
||||
type=gltflib.AccessorType.SCALAR.value, # type: ignore
|
||||
min=[int(scalars.min())], # type: ignore
|
||||
max=[int(scalars.max())], # type: ignore
|
||||
)
|
||||
self.model.accessors.append(accessor)
|
||||
return accessor
|
||||
|
||||
def maybe_add_vec_array(
|
||||
self,
|
||||
vecs: np.ndarray,
|
||||
vec_type: gltflib.AccessorType,
|
||||
component_type: gltflib.ComponentType = gltflib.ComponentType.FLOAT,
|
||||
) -> Optional[int]:
|
||||
if not vecs.size:
|
||||
return None
|
||||
accessor = self.add_vec_array(vecs, vec_type, component_type)
|
||||
return self.model.accessors.index(accessor)
|
||||
|
||||
def add_vec_array(
|
||||
self,
|
||||
vecs: np.ndarray,
|
||||
vec_type: gltflib.AccessorType,
|
||||
component_type: gltflib.ComponentType = gltflib.ComponentType.FLOAT
|
||||
) -> gltflib.Accessor:
|
||||
"""
|
||||
Add a two-dimensional array of vecs (positions, normals, weights, UVs) returning the accessor
|
||||
|
||||
Vec type may be a vec2, vec3, or a vec4.
|
||||
"""
|
||||
# Pretty much all of these are float32 except the ones that aren't
|
||||
dtype = np.float32
|
||||
if component_type == gltflib.ComponentType.UNSIGNED_BYTE:
|
||||
dtype = np.uint8
|
||||
vec_data = vecs.astype(dtype).tobytes()
|
||||
buffer_view = self.add_buffer_view(vec_data, target=None)
|
||||
accessor = gltflib.Accessor(
|
||||
bufferView=self.model.bufferViews.index(buffer_view),
|
||||
componentType=component_type,
|
||||
count=len(vecs),
|
||||
type=vec_type.value, # type: ignore
|
||||
min=vecs.min(axis=0).tolist(), # type: ignore
|
||||
max=vecs.max(axis=0).tolist(), # type: ignore
|
||||
)
|
||||
self.model.accessors.append(accessor)
|
||||
return accessor
|
||||
|
||||
def add_buffer_view(self, data: bytes, target: Optional[gltflib.BufferTarget]) -> gltflib.BufferView:
|
||||
"""Create a buffer view and associated buffer and resource for a blob of data"""
|
||||
resource = gltflib.FileResource(filename=f"res-{uuid.uuid4()}.bin", data=data)
|
||||
self.gltf.resources.append(resource)
|
||||
|
||||
buffer = gltflib.Buffer(uri=resource.filename, byteLength=len(resource.data))
|
||||
self.model.buffers.append(buffer)
|
||||
|
||||
buffer_view = gltflib.BufferView(
|
||||
buffer=self.model.buffers.index(buffer),
|
||||
byteLength=buffer.byteLength,
|
||||
byteOffset=0,
|
||||
target=target
|
||||
)
|
||||
self.model.bufferViews.append(buffer_view)
|
||||
return buffer_view
|
||||
|
||||
def add_joints(self, skin: SkinSegmentDict) -> JOINT_CONTEXT_DICT:
|
||||
# There may be some joints not present in the mesh that we need to add to reach the mPelvis root
|
||||
required_joints = set()
|
||||
for joint_name in skin['joint_names']:
|
||||
joint_node = AVATAR_SKELETON[joint_name]
|
||||
required_joints.add(joint_node)
|
||||
required_joints.update(joint_node.ancestors)
|
||||
|
||||
# If this is present, it may override the joint positions from the skeleton definition
|
||||
if 'alt_inverse_bind_matrix' in skin:
|
||||
joint_overrides = dict(zip(skin['joint_names'], skin['alt_inverse_bind_matrix']))
|
||||
else:
|
||||
joint_overrides = {}
|
||||
|
||||
built_joints: JOINT_CONTEXT_DICT = {}
|
||||
for joint in required_joints:
|
||||
joint_matrix = joint.matrix
|
||||
|
||||
# Do we have a joint position override that would affect joint_matrix?
|
||||
override = joint_overrides.get(joint.name)
|
||||
if override:
|
||||
decomp = list(transformations.decompose_matrix(joint_matrix))
|
||||
# We specifically only want the translation from the override!
|
||||
translation = transformations.translation_from_matrix(llsd_to_mat4(override))
|
||||
# Only do it if the difference is over 0.1mm though
|
||||
if Vector3.dist(Vector3(*translation), joint.translation) > 0.0001:
|
||||
decomp[3] = translation
|
||||
joint_matrix = transformations.compose_matrix(*decomp)
|
||||
|
||||
# Do we need to mess with the bone's matrices to make Blender cooperate?
|
||||
orig_matrix = joint_matrix
|
||||
fixup_matrix = np.identity(4)
|
||||
if self.blender_compatibility:
|
||||
joint_matrix, fixup_matrix = self._fix_blender_joint(joint_matrix)
|
||||
|
||||
# TODO: populate "extras" here with the metadata the Blender collada stuff uses to store
|
||||
# "bind_mat" and "rest_mat" so we can go back to our original matrices when exporting
|
||||
# from blender to .dae!
|
||||
gltf_joint = self.add_node(joint.name, transform=joint_matrix)
|
||||
|
||||
# Store the node along with any fixups we may need to apply to the bind matrices later
|
||||
built_joints[joint.name] = JointContext(gltf_joint, orig_matrix, fixup_matrix)
|
||||
|
||||
# Add each joint to the child list of their respective parent
|
||||
for joint_name, joint_ctx in built_joints.items():
|
||||
if parent := AVATAR_SKELETON[joint_name].parent:
|
||||
built_joints[parent().name].node.children.append(self.model.nodes.index(joint_ctx.node))
|
||||
return built_joints
|
||||
|
||||
def _fix_blender_joint(self, joint_matrix: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
||||
"""
|
||||
Split a joint matrix into a joint matrix and fixup matrix
|
||||
|
||||
If we don't account for weird scaling on the collision volumes, then
|
||||
Blender freaks out. This is an issue in blender where it doesn't
|
||||
apply the inverse bind matrices relative to the scale and rotation of
|
||||
the bones themselves, as it should per the glTF spec. Blender's glTF loader
|
||||
tries to recover from this by applying certain transforms as a pose, but
|
||||
the damage has been done by that point. Nobody else runs really runs into
|
||||
this because they have the good sense to not use some nightmare abomination
|
||||
rig with scaling and rotation on the skeleton like SL does.
|
||||
|
||||
Blender will _only_ correctly handle the translation component of the joint,
|
||||
any other transforms need to be mixed into the inverse bind matrices themselves.
|
||||
There's no internal concept of bone scale or rot in Blender right now.
|
||||
|
||||
Should investigate an Avastar-style approach of optionally retargeting
|
||||
to a Blender-compatible rig with translation-only bones, and modify
|
||||
the bind matrices to accommodate. The glTF importer supports metadata through
|
||||
the "extras" fields, so we can potentially abuse the "bind_mat" metadata field
|
||||
that Blender already uses for the "Keep Bind Info" Collada import / export hack.
|
||||
|
||||
For context:
|
||||
* https://github.com/KhronosGroup/glTF-Blender-IO/issues/1305
|
||||
* https://developer.blender.org/T38660 (these are Collada, but still relevant)
|
||||
* https://developer.blender.org/T29246
|
||||
* https://developer.blender.org/T50412
|
||||
* https://developer.blender.org/T53620 (FBX but still relevant)
|
||||
"""
|
||||
scale, shear, angles, translate, projection = transformations.decompose_matrix(joint_matrix)
|
||||
joint_matrix = transformations.compose_matrix(translate=translate)
|
||||
fixup_matrix = transformations.compose_matrix(scale=scale, angles=angles)
|
||||
return joint_matrix, fixup_matrix
|
||||
|
||||
def add_skin(self, name: str, joint_nodes: JOINT_CONTEXT_DICT, skin_seg: SkinSegmentDict) -> gltflib.Skin:
|
||||
joints_arr = []
|
||||
for joint_name in skin_seg['joint_names']:
|
||||
joint_ctx = joint_nodes[joint_name]
|
||||
joints_arr.append(self.model.nodes.index(joint_ctx.node))
|
||||
|
||||
inv_binds = []
|
||||
for joint_name, inv_bind in zip(skin_seg['joint_names'], skin_seg['inverse_bind_matrix']):
|
||||
joint_ctx = joint_nodes[joint_name]
|
||||
inv_bind = joint_ctx.fixup_matrix @ llsd_to_mat4(inv_bind)
|
||||
inv_binds.append(sl_mat4_to_gltf(inv_bind))
|
||||
inv_binds_data = np.array(inv_binds, dtype=np.float32).tobytes()
|
||||
buffer_view = self.add_buffer_view(inv_binds_data, target=None)
|
||||
accessor = gltflib.Accessor(
|
||||
bufferView=self.model.bufferViews.index(buffer_view),
|
||||
componentType=gltflib.ComponentType.FLOAT,
|
||||
count=len(inv_binds),
|
||||
type=gltflib.AccessorType.MAT4.value, # type: ignore
|
||||
)
|
||||
self.model.accessors.append(accessor)
|
||||
accessor_idx = self.model.accessors.index(accessor)
|
||||
|
||||
skin = gltflib.Skin(name=name, joints=joints_arr, inverseBindMatrices=accessor_idx)
|
||||
self.model.skins.append(skin)
|
||||
return skin
|
||||
|
||||
def finalize(self):
|
||||
"""Clean up the mesh to pass the glTF smell test, should be done last"""
|
||||
def _nullify_empty_lists(dc):
|
||||
for field in dataclasses.fields(dc):
|
||||
# Empty lists should be replaced with None
|
||||
if getattr(dc, field.name) == []:
|
||||
setattr(dc, field.name, None)
|
||||
|
||||
for node in self.model.nodes:
|
||||
_nullify_empty_lists(node)
|
||||
_nullify_empty_lists(self.model)
|
||||
return self.gltf
|
||||
|
||||
|
||||
def main():
|
||||
# Take an llmesh file as an argument and spit out basename-converted.gltf
|
||||
with open(sys.argv[1], "rb") as f:
|
||||
reader = BufferReader("<", f.read())
|
||||
|
||||
filename = Path(sys.argv[1]).stem
|
||||
mesh: MeshAsset = reader.read(LLMeshSerializer(parse_segment_contents=True))
|
||||
|
||||
builder = GLTFBuilder(blender_compatibility=True)
|
||||
builder.add_nodes_from_llmesh(mesh, filename)
|
||||
gltf = builder.finalize()
|
||||
|
||||
pprint.pprint(gltf.model)
|
||||
gltf.export_glb(sys.argv[1].rsplit(".", 1)[0] + "-converted.gltf")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,7 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import codecs
|
||||
import functools
|
||||
import logging
|
||||
import os
|
||||
|
||||
import lazy_object_proxy
|
||||
import pkg_resources
|
||||
import re
|
||||
import weakref
|
||||
@@ -17,7 +22,7 @@ def _with_patched_multidict(f):
|
||||
# There's no way to tell pprint "hey, this is a dict,
|
||||
# this is how you access its items." A lot of the formatting logic
|
||||
# is in the module-level `_safe_repr()` which we don't want to mess with.
|
||||
# Instead, pretend our MultiDict has dict's __repr__ and while we're inside
|
||||
# Instead, pretend our MultiDict has dict's __repr__ while we're inside
|
||||
# calls to pprint. Hooray.
|
||||
orig_repr = MultiDict.__repr__
|
||||
if orig_repr is dict.__repr__:
|
||||
@@ -65,6 +70,9 @@ class HippoPrettyPrinter(PrettyPrinter):
|
||||
return f"({reprs})"
|
||||
|
||||
def pformat(self, obj: object, *args, **kwargs) -> str:
|
||||
# Unwrap lazy object proxies before pprinting them
|
||||
if isinstance(obj, lazy_object_proxy.Proxy):
|
||||
obj = obj.__wrapped__
|
||||
if isinstance(obj, (bytes, str)):
|
||||
return self._str_format(obj)
|
||||
return self._base_pformat(obj, *args, **kwargs)
|
||||
@@ -126,6 +134,13 @@ def proxify(obj: Union[Callable[[], _T], weakref.ReferenceType, _T]) -> _T:
|
||||
return obj
|
||||
|
||||
|
||||
class BiDiDict(Generic[_T]):
|
||||
"""Dictionary for bidirectional lookups"""
|
||||
def __init__(self, values: Dict[_T, _T]):
|
||||
self.forward = {**values}
|
||||
self.backward = {value: key for (key, value) in values.items()}
|
||||
|
||||
|
||||
def bytes_unescape(val: bytes) -> bytes:
|
||||
# Only in CPython. bytes -> bytes with escape decoding.
|
||||
# https://stackoverflow.com/a/23151714
|
||||
@@ -141,7 +156,42 @@ def get_resource_filename(resource_filename: str):
|
||||
return pkg_resources.resource_filename("hippolyzer", resource_filename)
|
||||
|
||||
|
||||
def to_chunks(chunkable: Sequence[_T], chunk_size: int) -> Generator[_T, None, None]:
|
||||
def to_chunks(chunkable: Sequence[_T], chunk_size: int) -> Generator[Sequence[_T], None, None]:
|
||||
while chunkable:
|
||||
yield chunkable[:chunk_size]
|
||||
chunkable = chunkable[chunk_size:]
|
||||
|
||||
|
||||
def get_mtime(path):
|
||||
try:
|
||||
return os.stat(path).st_mtime
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def fut_logger(name: str, logger: logging.Logger, fut: asyncio.Future, *args) -> None:
|
||||
"""Callback suitable for exception logging in `Future.add_done_callback()`"""
|
||||
if not fut.cancelled() and fut.exception():
|
||||
if isinstance(fut.exception(), asyncio.CancelledError):
|
||||
# Don't really care if the task was just cancelled
|
||||
return
|
||||
logger.exception(f"Failed in task for {name}", exc_info=fut.exception())
|
||||
|
||||
|
||||
def add_future_logger(
|
||||
fut: asyncio.Future,
|
||||
name: Optional[str] = None,
|
||||
logger: Optional[logging.Logger] = None,
|
||||
):
|
||||
"""Add a logger to Futures that will never be directly `await`ed, logging exceptions"""
|
||||
fut.add_done_callback(functools.partial(fut_logger, name, logger or logging.getLogger()))
|
||||
|
||||
|
||||
def create_logged_task(
|
||||
coro: Coroutine,
|
||||
name: Optional[str] = None,
|
||||
logger: Optional[logging.Logger] = None,
|
||||
) -> asyncio.Task:
|
||||
task = asyncio.create_task(coro, name=name)
|
||||
add_future_logger(task, name, logger)
|
||||
return task
|
||||
|
||||
693
hippolyzer/lib/base/inventory.py
Normal file
693
hippolyzer/lib/base/inventory.py
Normal file
@@ -0,0 +1,693 @@
|
||||
"""
|
||||
Parse the horrible legacy inventory-related format.
|
||||
|
||||
It's typically only used for object contents now.
|
||||
"""
|
||||
|
||||
# TODO: Maybe handle CRC calculation? Does anything care about that?
|
||||
# I don't think anything in the viewer actually looks at the result
|
||||
# of the CRC check for UDP stuff.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
import asyncio
|
||||
import dataclasses
|
||||
import datetime as dt
|
||||
import inspect
|
||||
import logging
|
||||
import secrets
|
||||
import struct
|
||||
import weakref
|
||||
from io import StringIO
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.legacy_schema import (
|
||||
parse_schema_line,
|
||||
SchemaBase,
|
||||
SchemaDate,
|
||||
SchemaFieldSerializer,
|
||||
SchemaHexInt,
|
||||
SchemaInt,
|
||||
SchemaLLSD,
|
||||
SchemaMultilineStr,
|
||||
SchemaParsingError,
|
||||
SchemaStr,
|
||||
SchemaUUID,
|
||||
schema_field,
|
||||
)
|
||||
from hippolyzer.lib.base.message.message import Block
|
||||
from hippolyzer.lib.base.templates import SaleType, InventoryType, LookupIntEnum, AssetType, FolderType
|
||||
|
||||
MAGIC_ID = UUID("3c115e51-04f4-523c-9fa6-98aff1034730")
|
||||
LOG = logging.getLogger(__name__)
|
||||
_T = TypeVar("_T")
|
||||
|
||||
|
||||
class SchemaFlagField(SchemaHexInt):
|
||||
"""Like a hex int, but must be serialized as bytes in LLSD due to being a U32"""
|
||||
@classmethod
|
||||
def from_llsd(cls, val: Any, flavor: str) -> int:
|
||||
# Sometimes values in S32 range will just come through normally
|
||||
if isinstance(val, int):
|
||||
return val
|
||||
|
||||
if flavor == "legacy":
|
||||
return struct.unpack("!I", val)[0]
|
||||
return val
|
||||
|
||||
@classmethod
|
||||
def to_llsd(cls, val: int, flavor: str) -> Any:
|
||||
if flavor == "legacy":
|
||||
return struct.pack("!I", val)
|
||||
return val
|
||||
|
||||
|
||||
class SchemaEnumField(SchemaStr, Generic[_T]):
|
||||
def __init__(self, enum_cls: Type[LookupIntEnum]):
|
||||
super().__init__()
|
||||
self._enum_cls = enum_cls
|
||||
|
||||
def deserialize(self, val: str) -> _T:
|
||||
return self._enum_cls.from_lookup_name(val)
|
||||
|
||||
def serialize(self, val: _T) -> str:
|
||||
return self._enum_cls(val).to_lookup_name()
|
||||
|
||||
def from_llsd(self, val: Union[str, int], flavor: str) -> _T:
|
||||
if flavor == "legacy":
|
||||
return self.deserialize(val)
|
||||
return self._enum_cls(val)
|
||||
|
||||
def to_llsd(self, val: _T, flavor: str) -> Union[int, str]:
|
||||
if flavor == "legacy":
|
||||
return self.serialize(val)
|
||||
return int(val)
|
||||
|
||||
|
||||
def _yield_schema_tokens(reader: StringIO):
|
||||
in_bracket = False
|
||||
# empty str == EOF in Python
|
||||
while line := reader.readline():
|
||||
line = line.strip()
|
||||
# Whitespace-only lines are automatically skipped
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
key, val = parse_schema_line(line)
|
||||
except SchemaParsingError:
|
||||
# Can happen if there's a malformed multi-line string, just
|
||||
# skip by it.
|
||||
LOG.warning(f"Found invalid inventory line {line!r}")
|
||||
continue
|
||||
if key == "{":
|
||||
if in_bracket:
|
||||
LOG.warning("Found multiple opening brackets inside structure, "
|
||||
"was a nested structure not handled?")
|
||||
in_bracket = True
|
||||
continue
|
||||
if key == "}":
|
||||
if not in_bracket:
|
||||
LOG.warning("Unexpected closing bracket")
|
||||
in_bracket = False
|
||||
break
|
||||
yield key, val
|
||||
if in_bracket:
|
||||
LOG.warning("Reached EOF while inside a bracket")
|
||||
|
||||
|
||||
class InventoryBase(SchemaBase):
|
||||
SCHEMA_NAME: ClassVar[str]
|
||||
|
||||
@classmethod
|
||||
def from_reader(cls, reader: StringIO, read_header=False) -> InventoryBase:
|
||||
tok_iter = _yield_schema_tokens(reader)
|
||||
# Someone else hasn't already read the header for us
|
||||
if read_header:
|
||||
schema_name, _ = next(tok_iter)
|
||||
if schema_name != cls.SCHEMA_NAME:
|
||||
raise ValueError(f"Expected schema name {schema_name!r} to be {cls.SCHEMA_NAME!r}")
|
||||
|
||||
fields = cls._get_fields_dict()
|
||||
obj_dict = {}
|
||||
for key, val in tok_iter:
|
||||
if key in fields:
|
||||
field: dataclasses.Field = fields[key]
|
||||
spec = field.metadata.get("spec")
|
||||
# Not a real key, an internal var on our dataclass
|
||||
if not spec:
|
||||
LOG.warning(f"Internal key {key!r}")
|
||||
continue
|
||||
|
||||
spec_cls = spec
|
||||
if not inspect.isclass(spec_cls):
|
||||
spec_cls = spec_cls.__class__
|
||||
# some kind of nested structure like sale_info
|
||||
if issubclass(spec_cls, SchemaBase):
|
||||
obj_dict[key] = spec.from_reader(reader)
|
||||
elif issubclass(spec_cls, SchemaFieldSerializer):
|
||||
obj_dict[key] = spec.deserialize(val)
|
||||
else:
|
||||
raise ValueError(f"Unsupported spec for {key!r}, {spec!r}")
|
||||
else:
|
||||
LOG.warning(f"Unknown key {key!r}")
|
||||
return cls._obj_from_dict(obj_dict)
|
||||
|
||||
def to_writer(self, writer: StringIO):
|
||||
writer.write(f"\t{self.SCHEMA_NAME}")
|
||||
if self.SCHEMA_NAME == "permissions":
|
||||
writer.write(" 0\n")
|
||||
else:
|
||||
writer.write("\t0\n")
|
||||
writer.write("\t{\n")
|
||||
|
||||
# Make sure the ID field always comes first, if there is one.
|
||||
fields_dict = {}
|
||||
if hasattr(self, "ID_ATTR"):
|
||||
fields_dict = {getattr(self, "ID_ATTR"): None}
|
||||
# update()ing will put all fields that aren't yet in the dict after the ID attr.
|
||||
fields_dict.update(self._get_fields_dict())
|
||||
|
||||
for field_name, field in fields_dict.items():
|
||||
spec = field.metadata.get("spec")
|
||||
# Not meant to be serialized
|
||||
if not spec:
|
||||
continue
|
||||
if field.metadata.get("llsd_only"):
|
||||
continue
|
||||
|
||||
val = getattr(self, field_name)
|
||||
if val is None and not field.metadata.get("include_none"):
|
||||
continue
|
||||
|
||||
spec_cls = spec
|
||||
if not inspect.isclass(spec_cls):
|
||||
spec_cls = spec_cls.__class__
|
||||
# Some kind of nested structure like sale_info
|
||||
if isinstance(val, SchemaBase):
|
||||
val.to_writer(writer)
|
||||
elif issubclass(spec_cls, SchemaFieldSerializer):
|
||||
writer.write(f"\t\t{field_name}\t{spec.serialize(val)}\n")
|
||||
else:
|
||||
raise ValueError(f"Bad inventory spec {spec!r}")
|
||||
writer.write("\t}\n")
|
||||
|
||||
|
||||
class InventoryDifferences(NamedTuple):
|
||||
changed: List[InventoryNodeBase]
|
||||
removed: List[InventoryNodeBase]
|
||||
|
||||
|
||||
class InventoryModel(InventoryBase):
|
||||
def __init__(self):
|
||||
self.nodes: Dict[UUID, InventoryNodeBase] = {}
|
||||
self.root: Optional[InventoryContainerBase] = None
|
||||
self.any_dirty = asyncio.Event()
|
||||
|
||||
@classmethod
|
||||
def from_reader(cls, reader: StringIO, read_header=False) -> InventoryModel:
|
||||
model = cls()
|
||||
for key, value in _yield_schema_tokens(reader):
|
||||
if key == "inv_object":
|
||||
obj = InventoryObject.from_reader(reader)
|
||||
if obj is not None:
|
||||
model.add(obj)
|
||||
elif key == "inv_category":
|
||||
cat = InventoryCategory.from_reader(reader)
|
||||
if cat is not None:
|
||||
model.add(cat)
|
||||
elif key == "inv_item":
|
||||
item = InventoryItem.from_reader(reader)
|
||||
if item is not None:
|
||||
model.add(item)
|
||||
else:
|
||||
LOG.warning("Unknown key {0}".format(key))
|
||||
return model
|
||||
|
||||
@classmethod
|
||||
def from_llsd(cls, llsd_val: List[Dict], flavor: str = "legacy") -> InventoryModel:
|
||||
model = cls()
|
||||
for obj_dict in llsd_val:
|
||||
for inv_type in INVENTORY_TYPES:
|
||||
if inv_type.ID_ATTR in obj_dict:
|
||||
if (obj := inv_type.from_llsd(obj_dict, flavor)) is not None:
|
||||
model.add(obj)
|
||||
break
|
||||
LOG.warning(f"Unknown object type {obj_dict!r}")
|
||||
return model
|
||||
|
||||
@property
|
||||
def ordered_nodes(self) -> Iterable[InventoryNodeBase]:
|
||||
yield from self.all_containers
|
||||
yield from self.all_items
|
||||
|
||||
@property
|
||||
def all_containers(self) -> Iterable[InventoryContainerBase]:
|
||||
for node in self.nodes.values():
|
||||
if isinstance(node, InventoryContainerBase):
|
||||
yield node
|
||||
|
||||
@property
|
||||
def dirty_categories(self) -> Iterable[InventoryCategory]:
|
||||
for node in self.nodes:
|
||||
if isinstance(node, InventoryCategory) and node.version == InventoryCategory.VERSION_NONE:
|
||||
yield node
|
||||
|
||||
@property
|
||||
def all_items(self) -> Iterable[InventoryItem]:
|
||||
for node in self.nodes.values():
|
||||
if not isinstance(node, InventoryContainerBase):
|
||||
yield node
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, InventoryModel):
|
||||
return False
|
||||
return set(self.nodes.values()) == set(other.nodes.values())
|
||||
|
||||
def to_writer(self, writer: StringIO):
|
||||
for node in self.ordered_nodes:
|
||||
node.to_writer(writer)
|
||||
|
||||
def to_llsd(self, flavor: str = "legacy"):
|
||||
return list(node.to_llsd(flavor) for node in self.ordered_nodes)
|
||||
|
||||
def add(self, node: InventoryNodeBase):
|
||||
if node.node_id in self.nodes:
|
||||
raise KeyError(f"{node.node_id} already exists in the inventory model")
|
||||
|
||||
self.nodes[node.node_id] = node
|
||||
if isinstance(node, InventoryContainerBase):
|
||||
if node.parent_id == UUID.ZERO:
|
||||
self.root = node
|
||||
node.model = weakref.proxy(self)
|
||||
return node
|
||||
|
||||
def update(self, node: InventoryNodeBase, update_fields: Optional[Iterable[str]] = None) -> InventoryNodeBase:
|
||||
"""Update an existing node, optionally only updating specific fields"""
|
||||
if node.node_id not in self.nodes:
|
||||
raise KeyError(f"{node.node_id} not in the inventory model")
|
||||
|
||||
orig_node = self.nodes[node.node_id]
|
||||
if node.__class__ != orig_node.__class__:
|
||||
raise ValueError(f"Tried to update {orig_node!r} from non-matching {node!r}")
|
||||
|
||||
if not update_fields:
|
||||
# Update everything but the model parameter
|
||||
update_fields = node.get_field_names()
|
||||
for field_name in update_fields:
|
||||
setattr(orig_node, field_name, getattr(node, field_name))
|
||||
return orig_node
|
||||
|
||||
def upsert(self, node: InventoryNodeBase, update_fields: Optional[Iterable[str]] = None) -> InventoryNodeBase:
|
||||
"""Add or update a node"""
|
||||
if node.node_id in self.nodes:
|
||||
return self.update(node, update_fields)
|
||||
return self.add(node)
|
||||
|
||||
def unlink(self, node: InventoryNodeBase, single_only: bool = False) -> Sequence[InventoryNodeBase]:
|
||||
"""Unlink a node and its descendants from the tree, returning the removed nodes"""
|
||||
assert node.model == self
|
||||
if node == self.root:
|
||||
self.root = None
|
||||
unlinked = [node]
|
||||
if isinstance(node, InventoryContainerBase) and not single_only:
|
||||
for child in node.children:
|
||||
unlinked.extend(self.unlink(child))
|
||||
self.nodes.pop(node.node_id, None)
|
||||
node.model = None
|
||||
return unlinked
|
||||
|
||||
def get_differences(self, other: InventoryModel) -> InventoryDifferences:
|
||||
# Includes modified things with the same ID
|
||||
changed_in_other = []
|
||||
removed_in_other = []
|
||||
|
||||
other_keys = set(other.nodes.keys())
|
||||
our_keys = set(self.nodes.keys())
|
||||
|
||||
# Removed
|
||||
for key in our_keys - other_keys:
|
||||
removed_in_other.append(self.nodes[key])
|
||||
|
||||
# Updated
|
||||
for key in other_keys.intersection(our_keys):
|
||||
other_node = other.nodes[key]
|
||||
if other_node != self.nodes[key]:
|
||||
changed_in_other.append(other_node)
|
||||
|
||||
# Added
|
||||
for key in other_keys - our_keys:
|
||||
changed_in_other.append(other.nodes[key])
|
||||
return InventoryDifferences(
|
||||
changed=changed_in_other,
|
||||
removed=removed_in_other,
|
||||
)
|
||||
|
||||
def flag_if_dirty(self):
|
||||
if any(self.dirty_categories):
|
||||
self.any_dirty.set()
|
||||
|
||||
def __getitem__(self, item: UUID) -> InventoryNodeBase:
|
||||
return self.nodes[item]
|
||||
|
||||
def __contains__(self, item: UUID):
|
||||
return item in self.nodes
|
||||
|
||||
def get(self, item: UUID) -> Optional[InventoryNodeBase]:
|
||||
return self.nodes.get(item)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryPermissions(InventoryBase):
|
||||
SCHEMA_NAME: ClassVar[str] = "permissions"
|
||||
|
||||
base_mask: int = schema_field(SchemaHexInt)
|
||||
owner_mask: int = schema_field(SchemaHexInt)
|
||||
group_mask: int = schema_field(SchemaHexInt)
|
||||
everyone_mask: int = schema_field(SchemaHexInt)
|
||||
next_owner_mask: int = schema_field(SchemaHexInt)
|
||||
creator_id: UUID = schema_field(SchemaUUID)
|
||||
owner_id: UUID = schema_field(SchemaUUID)
|
||||
last_owner_id: UUID = schema_field(SchemaUUID)
|
||||
group_id: UUID = schema_field(SchemaUUID)
|
||||
# Nothing actually cares about this, but it could be there.
|
||||
# It's kind of redundant since it just means owner_id == NULL_KEY && group_id != NULL_KEY.
|
||||
is_owner_group: Optional[int] = schema_field(SchemaInt, default=None, llsd_only=True)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventorySaleInfo(InventoryBase):
|
||||
SCHEMA_NAME: ClassVar[str] = "sale_info"
|
||||
|
||||
sale_type: SaleType = schema_field(SchemaEnumField(SaleType))
|
||||
sale_price: int = schema_field(SchemaInt)
|
||||
|
||||
|
||||
class _HasName(abc.ABC):
|
||||
"""
|
||||
Only exists so that we can assert that all subclasses should have this without forcing
|
||||
a particular serialization order, as would happen if this was present on InventoryNodeBase.
|
||||
"""
|
||||
name: str
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryNodeBase(InventoryBase, _HasName):
|
||||
ID_ATTR: ClassVar[str]
|
||||
|
||||
parent_id: Optional[UUID] = schema_field(SchemaUUID)
|
||||
|
||||
model: Optional[InventoryModel] = dataclasses.field(
|
||||
default=None, init=False, hash=False, compare=False, repr=False
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_field_names(cls) -> Set[str]:
|
||||
return set(cls._get_fields_dict().keys()) - {"model"}
|
||||
|
||||
@property
|
||||
def node_id(self) -> UUID:
|
||||
return getattr(self, self.ID_ATTR)
|
||||
|
||||
@node_id.setter
|
||||
def node_id(self, val: UUID):
|
||||
setattr(self, self.ID_ATTR, val)
|
||||
|
||||
@property
|
||||
def parent(self) -> Optional[InventoryContainerBase]:
|
||||
return self.model.nodes.get(self.parent_id)
|
||||
|
||||
def unlink(self) -> Sequence[InventoryNodeBase]:
|
||||
return self.model.unlink(self)
|
||||
|
||||
@classmethod
|
||||
def _obj_from_dict(cls, obj_dict):
|
||||
# Bad entry, ignore
|
||||
# TODO: Check on these. might be symlinks or something.
|
||||
if obj_dict.get("type") == "-1":
|
||||
LOG.warning(f"Skipping bad object with type == -1: {obj_dict!r}")
|
||||
return None
|
||||
return super()._obj_from_dict(obj_dict)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.node_id)
|
||||
|
||||
def __iter__(self) -> Iterator[InventoryNodeBase]:
|
||||
return iter(())
|
||||
|
||||
def __contains__(self, item) -> bool:
|
||||
return item in tuple(self)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryContainerBase(InventoryNodeBase):
|
||||
type: AssetType = schema_field(SchemaEnumField(AssetType))
|
||||
|
||||
@property
|
||||
def children(self) -> Sequence[InventoryNodeBase]:
|
||||
return tuple(
|
||||
x for x in self.model.nodes.values()
|
||||
if x.parent_id == self.node_id
|
||||
)
|
||||
|
||||
def __getitem__(self, item: Union[int, str]) -> InventoryNodeBase:
|
||||
if isinstance(item, int):
|
||||
return self.children[item]
|
||||
|
||||
for child in self.children:
|
||||
if child.name == item:
|
||||
return child
|
||||
raise KeyError(f"{item!r} not found in children")
|
||||
|
||||
def __iter__(self) -> Iterator[InventoryNodeBase]:
|
||||
return iter(self.children)
|
||||
|
||||
def get_or_create_subcategory(self, name: str) -> InventoryCategory:
|
||||
for child in self:
|
||||
if child.name == name and isinstance(child, InventoryCategory):
|
||||
return child
|
||||
child = InventoryCategory(
|
||||
name=name,
|
||||
cat_id=UUID.random(),
|
||||
parent_id=self.node_id,
|
||||
type=AssetType.CATEGORY,
|
||||
pref_type=FolderType.NONE,
|
||||
owner_id=getattr(self, 'owner_id', UUID.ZERO),
|
||||
version=1,
|
||||
)
|
||||
self.model.add(child)
|
||||
return child
|
||||
|
||||
# So autogenerated __hash__ doesn't kill our inherited one
|
||||
__hash__ = InventoryNodeBase.__hash__
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryObject(InventoryContainerBase):
|
||||
SCHEMA_NAME: ClassVar[str] = "inv_object"
|
||||
ID_ATTR: ClassVar[str] = "obj_id"
|
||||
|
||||
obj_id: UUID = schema_field(SchemaUUID)
|
||||
name: str = schema_field(SchemaMultilineStr)
|
||||
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None, include_none=True)
|
||||
|
||||
__hash__ = InventoryNodeBase.__hash__
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryCategory(InventoryContainerBase):
|
||||
ID_ATTR: ClassVar[str] = "cat_id"
|
||||
# AIS calls this something else...
|
||||
ID_ATTR_AIS: ClassVar[str] = "category_id"
|
||||
SCHEMA_NAME: ClassVar[str] = "inv_category"
|
||||
VERSION_NONE: ClassVar[int] = -1
|
||||
|
||||
cat_id: UUID = schema_field(SchemaUUID)
|
||||
pref_type: FolderType = schema_field(SchemaEnumField(FolderType), llsd_name="preferred_type")
|
||||
name: str = schema_field(SchemaMultilineStr)
|
||||
owner_id: Optional[UUID] = schema_field(SchemaUUID, default=None)
|
||||
version: int = schema_field(SchemaInt, default=VERSION_NONE, llsd_only=True)
|
||||
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None, include_none=False)
|
||||
|
||||
def to_folder_data(self) -> Block:
|
||||
return Block(
|
||||
"FolderData",
|
||||
FolderID=self.cat_id,
|
||||
ParentID=self.parent_id,
|
||||
CallbackID=0,
|
||||
Type=self.pref_type,
|
||||
Name=self.name,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_folder_data(cls, block: Block):
|
||||
return cls(
|
||||
cat_id=block["FolderID"],
|
||||
parent_id=block["ParentID"],
|
||||
pref_type=block["Type"],
|
||||
name=block["Name"],
|
||||
type=AssetType.CATEGORY,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_llsd(cls, inv_dict: Dict, flavor: str = "legacy"):
|
||||
if flavor == "ais" and "type" not in inv_dict:
|
||||
inv_dict = inv_dict.copy()
|
||||
inv_dict["type"] = AssetType.CATEGORY
|
||||
return super().from_llsd(inv_dict, flavor)
|
||||
|
||||
def to_llsd(self, flavor: str = "legacy"):
|
||||
payload = super().to_llsd(flavor)
|
||||
if flavor == "ais":
|
||||
# AIS already knows the inventory type is category
|
||||
payload.pop("type", None)
|
||||
return payload
|
||||
|
||||
@classmethod
|
||||
def _get_fields_dict(cls, llsd_flavor: Optional[str] = None):
|
||||
fields = super()._get_fields_dict(llsd_flavor)
|
||||
if llsd_flavor == "ais":
|
||||
# These have different names though
|
||||
fields["type_default"] = fields.pop("preferred_type")
|
||||
fields["agent_id"] = fields.pop("owner_id")
|
||||
fields["category_id"] = fields.pop("cat_id")
|
||||
return fields
|
||||
|
||||
__hash__ = InventoryNodeBase.__hash__
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryItem(InventoryNodeBase):
|
||||
SCHEMA_NAME: ClassVar[str] = "inv_item"
|
||||
ID_ATTR: ClassVar[str] = "item_id"
|
||||
|
||||
item_id: UUID = schema_field(SchemaUUID)
|
||||
permissions: InventoryPermissions = schema_field(InventoryPermissions)
|
||||
asset_id: Optional[UUID] = schema_field(SchemaUUID, default=None)
|
||||
shadow_id: Optional[UUID] = schema_field(SchemaUUID, default=None)
|
||||
type: Optional[AssetType] = schema_field(SchemaEnumField(AssetType), default=None)
|
||||
inv_type: Optional[InventoryType] = schema_field(SchemaEnumField(InventoryType), default=None)
|
||||
flags: Optional[int] = schema_field(SchemaFlagField, default=None)
|
||||
sale_info: Optional[InventorySaleInfo] = schema_field(InventorySaleInfo, default=None)
|
||||
name: Optional[str] = schema_field(SchemaMultilineStr, default=None)
|
||||
desc: Optional[str] = schema_field(SchemaMultilineStr, default=None)
|
||||
metadata: Optional[Dict[str, Any]] = schema_field(SchemaLLSD, default=None, include_none=True)
|
||||
creation_date: Optional[dt.datetime] = schema_field(SchemaDate, llsd_name="created_at", default=None)
|
||||
|
||||
__hash__ = InventoryNodeBase.__hash__
|
||||
|
||||
@property
|
||||
def true_asset_id(self) -> UUID:
|
||||
if self.asset_id is not None:
|
||||
return self.asset_id
|
||||
return self.shadow_id ^ MAGIC_ID
|
||||
|
||||
def to_inventory_data(self) -> Block:
|
||||
return Block(
|
||||
"InventoryData",
|
||||
ItemID=self.item_id,
|
||||
FolderID=self.parent_id,
|
||||
CallbackID=0,
|
||||
CreatorID=self.permissions.creator_id,
|
||||
OwnerID=self.permissions.owner_id,
|
||||
GroupID=self.permissions.group_id,
|
||||
BaseMask=self.permissions.base_mask,
|
||||
OwnerMask=self.permissions.owner_mask,
|
||||
GroupMask=self.permissions.group_mask,
|
||||
EveryoneMask=self.permissions.everyone_mask,
|
||||
NextOwnerMask=self.permissions.next_owner_mask,
|
||||
GroupOwned=self.permissions.owner_id == UUID.ZERO and self.permissions.group_id != UUID.ZERO,
|
||||
AssetID=self.true_asset_id,
|
||||
Type=self.type,
|
||||
InvType=self.inv_type,
|
||||
Flags=self.flags,
|
||||
SaleType=self.sale_info.sale_type,
|
||||
SalePrice=self.sale_info.sale_price,
|
||||
Name=self.name,
|
||||
Description=self.desc,
|
||||
CreationDate=SchemaDate.to_llsd(self.creation_date, "legacy"),
|
||||
# Meaningless here
|
||||
CRC=secrets.randbits(32),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_inventory_data(cls, block: Block):
|
||||
return cls(
|
||||
item_id=block["ItemID"],
|
||||
# Might be under one of two names
|
||||
parent_id=block.get("ParentID", block["FolderID"]),
|
||||
permissions=InventoryPermissions(
|
||||
creator_id=block["CreatorID"],
|
||||
owner_id=block["OwnerID"],
|
||||
# Unknown, not sent in this schema
|
||||
last_owner_id=block.get("LastOwnerID", UUID.ZERO),
|
||||
group_id=block["GroupID"],
|
||||
base_mask=block["BaseMask"],
|
||||
owner_mask=block["OwnerMask"],
|
||||
group_mask=block["GroupMask"],
|
||||
everyone_mask=block["EveryoneMask"],
|
||||
next_owner_mask=block["NextOwnerMask"],
|
||||
),
|
||||
# May be missing in UpdateInventoryItem
|
||||
asset_id=block.get("AssetID"),
|
||||
type=AssetType(block["Type"]),
|
||||
inv_type=InventoryType(block["InvType"]),
|
||||
flags=block["Flags"],
|
||||
sale_info=InventorySaleInfo(
|
||||
sale_type=SaleType(block["SaleType"]),
|
||||
sale_price=block["SalePrice"],
|
||||
),
|
||||
name=block["Name"],
|
||||
desc=block["Description"],
|
||||
creation_date=block["CreationDate"],
|
||||
)
|
||||
|
||||
def to_llsd(self, flavor: str = "legacy"):
|
||||
val = super().to_llsd(flavor=flavor)
|
||||
if flavor == "ais":
|
||||
# There's little chance this differs from owner ID, just place it.
|
||||
val["agent_id"] = val["permissions"]["owner_id"]
|
||||
if val["type"] == AssetType.LINK:
|
||||
# For link items, there is no asset, only a linked ID.
|
||||
val["linked_id"] = val.pop("asset_id")
|
||||
# These don't exist either
|
||||
val.pop("permissions", None)
|
||||
val.pop("sale_info", None)
|
||||
return val
|
||||
|
||||
@classmethod
|
||||
def from_llsd(cls, inv_dict: Dict, flavor: str = "legacy"):
|
||||
if flavor == "ais" and "linked_id" in inv_dict:
|
||||
# Links get represented differently than other items for whatever reason.
|
||||
# This is incredibly annoying, under *NIX there's nothing really special about symlinks.
|
||||
inv_dict = inv_dict.copy()
|
||||
# Fill this in since it needs to be there
|
||||
if "permissions" not in inv_dict:
|
||||
inv_dict["permissions"] = InventoryPermissions(
|
||||
base_mask=0xFFffFFff,
|
||||
owner_mask=0xFFffFFff,
|
||||
group_mask=0xFFffFFff,
|
||||
everyone_mask=0,
|
||||
next_owner_mask=0xFFffFFff,
|
||||
creator_id=UUID.ZERO,
|
||||
owner_id=UUID.ZERO,
|
||||
last_owner_id=UUID.ZERO,
|
||||
group_id=UUID.ZERO,
|
||||
).to_llsd("ais")
|
||||
if "sale_info" not in inv_dict:
|
||||
inv_dict["sale_info"] = InventorySaleInfo(
|
||||
sale_type=SaleType.NOT,
|
||||
sale_price=0,
|
||||
).to_llsd("ais")
|
||||
if "type" not in inv_dict:
|
||||
inv_dict["type"] = AssetType.LINK
|
||||
|
||||
# In the context of symlinks, asset id means linked item ID.
|
||||
# This is also how indra stores symlinks. Why the asymmetry in AIS if none of the
|
||||
# consumers actually want it? Who knows.
|
||||
inv_dict["asset_id"] = inv_dict.pop("linked_id")
|
||||
return super().from_llsd(inv_dict, flavor)
|
||||
|
||||
|
||||
INVENTORY_TYPES: Tuple[Type[InventoryNodeBase], ...] = (InventoryCategory, InventoryObject, InventoryItem)
|
||||
@@ -1,7 +1,6 @@
|
||||
import os
|
||||
import tempfile
|
||||
from io import BytesIO
|
||||
from typing import *
|
||||
|
||||
import defusedxml.ElementTree
|
||||
from glymur import jp2box, Jp2k
|
||||
@@ -10,12 +9,6 @@ from glymur import jp2box, Jp2k
|
||||
jp2box.ET = defusedxml.ElementTree
|
||||
|
||||
|
||||
SL_DEFAULT_ENCODE = {
|
||||
"cratios": (1920.0, 480.0, 120.0, 30.0, 10.0),
|
||||
"irreversible": True,
|
||||
}
|
||||
|
||||
|
||||
class BufferedJp2k(Jp2k):
|
||||
"""
|
||||
For manipulating JP2K from within a binary buffer.
|
||||
@@ -24,12 +17,7 @@ class BufferedJp2k(Jp2k):
|
||||
based on filename, so this is the least brittle approach.
|
||||
"""
|
||||
|
||||
def __init__(self, contents: bytes, encode_kwargs: Optional[Dict] = None):
|
||||
if encode_kwargs is None:
|
||||
self.encode_kwargs = SL_DEFAULT_ENCODE.copy()
|
||||
else:
|
||||
self.encode_kwargs = encode_kwargs
|
||||
|
||||
def __init__(self, contents: bytes):
|
||||
stream = BytesIO(contents)
|
||||
self.temp_file = tempfile.NamedTemporaryFile(delete=False)
|
||||
stream.seek(0)
|
||||
@@ -44,11 +32,12 @@ class BufferedJp2k(Jp2k):
|
||||
os.remove(self.temp_file.name)
|
||||
self.temp_file = None
|
||||
|
||||
def _write(self, img_array, verbose=False, **kwargs):
|
||||
# Glymur normally only lets you control encode params when a write happens within
|
||||
# the constructor. Keep around the encode params from the constructor and pass
|
||||
# them to successive write calls.
|
||||
return super()._write(img_array, verbose=False, **self.encode_kwargs, **kwargs)
|
||||
def _populate_cparams(self, img_array):
|
||||
if self._cratios is None:
|
||||
self._cratios = (1920.0, 480.0, 120.0, 30.0, 10.0)
|
||||
if self._irreversible is None:
|
||||
self.irreversible = True
|
||||
return super()._populate_cparams(img_array)
|
||||
|
||||
def __bytes__(self):
|
||||
with open(self.temp_file.name, "rb") as f:
|
||||
|
||||
@@ -1,275 +0,0 @@
|
||||
"""
|
||||
Parse the horrible legacy inventory-related format.
|
||||
|
||||
It's typically only used for object contents now.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import datetime as dt
|
||||
import itertools
|
||||
import logging
|
||||
import weakref
|
||||
from io import StringIO
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.legacy_schema import (
|
||||
parse_schema_line,
|
||||
SchemaBase,
|
||||
SchemaDate,
|
||||
SchemaFieldSerializer,
|
||||
SchemaHexInt,
|
||||
SchemaInt,
|
||||
SchemaMultilineStr,
|
||||
SchemaParsingError,
|
||||
SchemaStr,
|
||||
SchemaUUID,
|
||||
schema_field,
|
||||
)
|
||||
|
||||
MAGIC_ID = UUID("3c115e51-04f4-523c-9fa6-98aff1034730")
|
||||
LOG = logging.getLogger(__name__)
|
||||
_T = TypeVar("_T")
|
||||
|
||||
|
||||
def _yield_schema_tokens(reader: StringIO):
|
||||
in_bracket = False
|
||||
# empty str == EOF in Python
|
||||
while line := reader.readline():
|
||||
line = line.strip()
|
||||
# Whitespace-only lines are automatically skipped
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
key, val = parse_schema_line(line)
|
||||
except SchemaParsingError:
|
||||
# Can happen if there's a malformed multi-line string, just
|
||||
# skip by it.
|
||||
LOG.warning(f"Found invalid inventory line {line!r}")
|
||||
continue
|
||||
if key == "{":
|
||||
if in_bracket:
|
||||
LOG.warning("Found multiple opening brackets inside structure, "
|
||||
"was a nested structure not handled?")
|
||||
in_bracket = True
|
||||
continue
|
||||
if key == "}":
|
||||
if not in_bracket:
|
||||
LOG.warning("Unexpected closing bracket")
|
||||
in_bracket = False
|
||||
break
|
||||
yield key, val
|
||||
if in_bracket:
|
||||
LOG.warning("Reached EOF while inside a bracket")
|
||||
|
||||
|
||||
class InventoryBase(SchemaBase):
|
||||
SCHEMA_NAME: ClassVar[str]
|
||||
|
||||
@classmethod
|
||||
def from_reader(cls, reader: StringIO, read_header=False) -> InventoryBase:
|
||||
tok_iter = _yield_schema_tokens(reader)
|
||||
# Someone else hasn't already read the header for us
|
||||
if read_header:
|
||||
schema_name, _ = next(tok_iter)
|
||||
if schema_name != cls.SCHEMA_NAME:
|
||||
raise ValueError(f"Expected schema name {schema_name!r} to be {cls.SCHEMA_NAME!r}")
|
||||
|
||||
fields = cls._fields_dict()
|
||||
obj_dict = {}
|
||||
for key, val in tok_iter:
|
||||
if key in fields:
|
||||
field: dataclasses.Field = fields[key]
|
||||
spec = field.metadata.get("spec")
|
||||
# Not a real key, an internal var on our dataclass
|
||||
if not spec:
|
||||
LOG.warning(f"Internal key {key!r}")
|
||||
continue
|
||||
# some kind of nested structure like sale_info
|
||||
if issubclass(spec, SchemaBase):
|
||||
obj_dict[key] = spec.from_reader(reader)
|
||||
elif issubclass(spec, SchemaFieldSerializer):
|
||||
obj_dict[key] = spec.deserialize(val)
|
||||
else:
|
||||
raise ValueError(f"Unsupported spec for {key!r}, {spec!r}")
|
||||
else:
|
||||
LOG.warning(f"Unknown key {key!r}")
|
||||
return cls._obj_from_dict(obj_dict)
|
||||
|
||||
def to_writer(self, writer: StringIO):
|
||||
writer.write(f"\t{self.SCHEMA_NAME}\t0\n")
|
||||
writer.write("\t{\n")
|
||||
for field_name, field in self._fields_dict().items():
|
||||
spec = field.metadata.get("spec")
|
||||
# Not meant to be serialized
|
||||
if not spec:
|
||||
continue
|
||||
|
||||
val = getattr(self, field_name)
|
||||
if val is None:
|
||||
continue
|
||||
|
||||
# Some kind of nested structure like sale_info
|
||||
if isinstance(val, SchemaBase):
|
||||
val.to_writer(writer)
|
||||
elif issubclass(spec, SchemaFieldSerializer):
|
||||
writer.write(f"\t\t{field_name}\t{spec.serialize(val)}\n")
|
||||
else:
|
||||
raise ValueError(f"Bad inventory spec {spec!r}")
|
||||
writer.write("\t}\n")
|
||||
|
||||
|
||||
class InventoryModel(InventoryBase):
|
||||
def __init__(self):
|
||||
self.containers: Dict[UUID, InventoryContainerBase] = {}
|
||||
self.items: Dict[UUID, InventoryItem] = {}
|
||||
self.root: Optional[InventoryContainerBase] = None
|
||||
|
||||
@classmethod
|
||||
def from_reader(cls, reader: StringIO, read_header=False) -> InventoryModel:
|
||||
model = cls()
|
||||
for key, value in _yield_schema_tokens(reader):
|
||||
if key == "inv_object":
|
||||
obj = InventoryObject.from_reader(reader)
|
||||
if obj is not None:
|
||||
model.add_container(obj)
|
||||
elif key == "inv_category":
|
||||
cat = InventoryCategory.from_reader(reader)
|
||||
if cat is not None:
|
||||
model.add_container(cat)
|
||||
elif key == "inv_item":
|
||||
item = InventoryItem.from_reader(reader)
|
||||
if item is not None:
|
||||
model.add_item(item)
|
||||
else:
|
||||
LOG.warning("Unknown key {0}".format(key))
|
||||
model.reparent_nodes()
|
||||
return model
|
||||
|
||||
def to_writer(self, writer: StringIO):
|
||||
for container in self.containers.values():
|
||||
container.to_writer(writer)
|
||||
for item in self.items.values():
|
||||
item.to_writer(writer)
|
||||
|
||||
def add_container(self, container: InventoryContainerBase):
|
||||
self.containers[container.node_id] = container
|
||||
container.model = weakref.proxy(self)
|
||||
|
||||
def add_item(self, item: InventoryItem):
|
||||
self.items[item.item_id] = item
|
||||
item.model = weakref.proxy(self)
|
||||
|
||||
def reparent_nodes(self):
|
||||
self.root = None
|
||||
for container in self.containers.values():
|
||||
container.children.clear()
|
||||
if container.parent_id == UUID():
|
||||
self.root = container
|
||||
for obj in itertools.chain(self.items.values(), self.containers.values()):
|
||||
if not obj.parent_id or obj.parent_id == UUID():
|
||||
continue
|
||||
parent_container = self.containers.get(obj.parent_id)
|
||||
if not parent_container:
|
||||
LOG.warning("{0} had an invalid parent {1}".format(obj, obj.parent_id))
|
||||
continue
|
||||
parent_container.children.append(obj)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryPermissions(InventoryBase):
|
||||
SCHEMA_NAME: ClassVar[str] = "permissions"
|
||||
|
||||
base_mask: int = schema_field(SchemaHexInt)
|
||||
owner_mask: int = schema_field(SchemaHexInt)
|
||||
group_mask: int = schema_field(SchemaHexInt)
|
||||
everyone_mask: int = schema_field(SchemaHexInt)
|
||||
next_owner_mask: int = schema_field(SchemaHexInt)
|
||||
creator_id: UUID = schema_field(SchemaUUID)
|
||||
owner_id: UUID = schema_field(SchemaUUID)
|
||||
last_owner_id: UUID = schema_field(SchemaUUID)
|
||||
group_id: UUID = schema_field(SchemaUUID)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventorySaleInfo(InventoryBase):
|
||||
SCHEMA_NAME: ClassVar[str] = "sale_info"
|
||||
|
||||
sale_type: str = schema_field(SchemaStr)
|
||||
sale_price: int = schema_field(SchemaInt)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryNodeBase(InventoryBase):
|
||||
ID_ATTR: ClassVar[str]
|
||||
|
||||
parent_id: Optional[UUID] = schema_field(SchemaUUID)
|
||||
model: Optional[InventoryModel] = dataclasses.field(default=None, init=False)
|
||||
|
||||
@property
|
||||
def node_id(self) -> UUID:
|
||||
return getattr(self, self.ID_ATTR)
|
||||
|
||||
@property
|
||||
def parent(self):
|
||||
return self.model.containers.get(self.parent_id)
|
||||
|
||||
@classmethod
|
||||
def _obj_from_dict(cls, obj_dict):
|
||||
# Bad entry, ignore
|
||||
# TODO: Check on these. might be symlinks or something.
|
||||
if obj_dict.get("type") == "-1":
|
||||
LOG.warning(f"Skipping bad object with type == -1: {obj_dict!r}")
|
||||
return None
|
||||
return super()._obj_from_dict(obj_dict)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryContainerBase(InventoryNodeBase):
|
||||
type: str = schema_field(SchemaStr)
|
||||
name: str = schema_field(SchemaMultilineStr)
|
||||
children: List[InventoryNodeBase] = dataclasses.field(default_factory=list, init=False)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryObject(InventoryContainerBase):
|
||||
SCHEMA_NAME: ClassVar[str] = "inv_object"
|
||||
ID_ATTR: ClassVar[str] = "obj_id"
|
||||
|
||||
obj_id: UUID = schema_field(SchemaUUID)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryCategory(InventoryContainerBase):
|
||||
ID_ATTR: ClassVar[str] = "cat_id"
|
||||
SCHEMA_NAME: ClassVar[str] = "inv_object"
|
||||
|
||||
cat_id: UUID = schema_field(SchemaUUID)
|
||||
pref_type: str = schema_field(SchemaStr)
|
||||
owner_id: UUID = schema_field(SchemaUUID)
|
||||
version: int = schema_field(SchemaInt)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class InventoryItem(InventoryNodeBase):
|
||||
SCHEMA_NAME: ClassVar[str] = "inv_item"
|
||||
ID_ATTR: ClassVar[str] = "item_id"
|
||||
|
||||
item_id: UUID = schema_field(SchemaUUID)
|
||||
type: str = schema_field(SchemaStr)
|
||||
inv_type: str = schema_field(SchemaStr)
|
||||
flags: int = schema_field(SchemaHexInt)
|
||||
name: str = schema_field(SchemaMultilineStr)
|
||||
desc: str = schema_field(SchemaMultilineStr)
|
||||
creation_date: dt.datetime = schema_field(SchemaDate)
|
||||
permissions: InventoryPermissions = schema_field(InventoryPermissions)
|
||||
sale_info: InventorySaleInfo = schema_field(InventorySaleInfo)
|
||||
asset_id: Optional[UUID] = schema_field(SchemaUUID, default=None)
|
||||
shadow_id: Optional[UUID] = schema_field(SchemaUUID, default=None)
|
||||
|
||||
@property
|
||||
def true_asset_id(self) -> UUID:
|
||||
if self.asset_id is not None:
|
||||
return self.asset_id
|
||||
return self.shadow_id ^ MAGIC_ID
|
||||
@@ -9,11 +9,14 @@ import abc
|
||||
import calendar
|
||||
import dataclasses
|
||||
import datetime as dt
|
||||
import inspect
|
||||
import logging
|
||||
import re
|
||||
from io import StringIO
|
||||
from typing import *
|
||||
|
||||
import hippolyzer.lib.base.llsd as llsd
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
@@ -31,6 +34,14 @@ class SchemaFieldSerializer(abc.ABC, Generic[_T]):
|
||||
def serialize(cls, val: _T) -> str:
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def from_llsd(cls, val: Any, flavor: str) -> _T:
|
||||
return val
|
||||
|
||||
@classmethod
|
||||
def to_llsd(cls, val: _T, flavor: str) -> Any:
|
||||
return val
|
||||
|
||||
|
||||
class SchemaDate(SchemaFieldSerializer[dt.datetime]):
|
||||
@classmethod
|
||||
@@ -41,6 +52,14 @@ class SchemaDate(SchemaFieldSerializer[dt.datetime]):
|
||||
def serialize(cls, val: dt.datetime) -> str:
|
||||
return str(calendar.timegm(val.utctimetuple()))
|
||||
|
||||
@classmethod
|
||||
def from_llsd(cls, val: Any, flavor: str) -> dt.datetime:
|
||||
return dt.datetime.utcfromtimestamp(val)
|
||||
|
||||
@classmethod
|
||||
def to_llsd(cls, val: dt.datetime, flavor: str):
|
||||
return calendar.timegm(val.utctimetuple())
|
||||
|
||||
|
||||
class SchemaHexInt(SchemaFieldSerializer[int]):
|
||||
@classmethod
|
||||
@@ -85,6 +104,13 @@ class SchemaStr(SchemaFieldSerializer[str]):
|
||||
|
||||
|
||||
class SchemaUUID(SchemaFieldSerializer[UUID]):
|
||||
@classmethod
|
||||
def from_llsd(cls, val: Any, flavor: str) -> UUID:
|
||||
# FetchInventory2 will return a string, but we want a UUID. It's not an issue
|
||||
# for us to return a UUID later there because it'll just cast to string if
|
||||
# that's what it wants
|
||||
return UUID(val)
|
||||
|
||||
@classmethod
|
||||
def deserialize(cls, val: str) -> UUID:
|
||||
return UUID(val)
|
||||
@@ -94,11 +120,28 @@ class SchemaUUID(SchemaFieldSerializer[UUID]):
|
||||
return str(val)
|
||||
|
||||
|
||||
def schema_field(spec: Type[Union[SchemaBase, SchemaFieldSerializer]], *, default=dataclasses.MISSING, init=True,
|
||||
repr=True, hash=None, compare=True) -> dataclasses.Field: # noqa
|
||||
class SchemaLLSD(SchemaFieldSerializer[_T]):
|
||||
"""Arbitrary LLSD embedded in a field"""
|
||||
@classmethod
|
||||
def deserialize(cls, val: str) -> _T:
|
||||
return llsd.parse_xml(val.partition("|")[0].encode("utf8"))
|
||||
|
||||
@classmethod
|
||||
def serialize(cls, val: _T) -> str:
|
||||
# Don't include the XML header
|
||||
return llsd.format_xml(val).split(b">", 1)[1].decode("utf8") + "\n|"
|
||||
|
||||
|
||||
_SCHEMA_SPEC = Union[Type[Union["SchemaBase", SchemaFieldSerializer]], SchemaFieldSerializer]
|
||||
|
||||
|
||||
def schema_field(spec: _SCHEMA_SPEC, *, default=dataclasses.MISSING, init=True,
|
||||
repr=True, hash=None, compare=True, llsd_name=None, llsd_only=False,
|
||||
include_none=False) -> dataclasses.Field: # noqa
|
||||
"""Describe a field in the inventory schema and the shape of its value"""
|
||||
return dataclasses.field(
|
||||
metadata={"spec": spec}, default=default, init=init, repr=repr, hash=hash, compare=compare
|
||||
return dataclasses.field( # noqa
|
||||
metadata={"spec": spec, "llsd_name": llsd_name, "llsd_only": llsd_only, "include_none": include_none},
|
||||
default=default, init=init, repr=repr, hash=hash, compare=compare,
|
||||
)
|
||||
|
||||
|
||||
@@ -121,8 +164,14 @@ def parse_schema_line(line: str):
|
||||
@dataclasses.dataclass
|
||||
class SchemaBase(abc.ABC):
|
||||
@classmethod
|
||||
def _fields_dict(cls):
|
||||
return {f.name: f for f in dataclasses.fields(cls)}
|
||||
def _get_fields_dict(cls, llsd_flavor: Optional[str] = None):
|
||||
fields_dict = {}
|
||||
for field in dataclasses.fields(cls):
|
||||
field_name = field.name
|
||||
if llsd_flavor:
|
||||
field_name = field.metadata.get("llsd_name") or field_name
|
||||
fields_dict[field_name] = field
|
||||
return fields_dict
|
||||
|
||||
@classmethod
|
||||
def from_str(cls, text: str):
|
||||
@@ -137,6 +186,42 @@ class SchemaBase(abc.ABC):
|
||||
def from_bytes(cls, data: bytes):
|
||||
return cls.from_str(data.decode("utf8"))
|
||||
|
||||
@classmethod
|
||||
def from_llsd(cls, inv_dict: Dict, flavor: str = "legacy"):
|
||||
fields = cls._get_fields_dict(llsd_flavor=flavor)
|
||||
obj_dict = {}
|
||||
try:
|
||||
for key, val in inv_dict.items():
|
||||
if key in fields:
|
||||
field: dataclasses.Field = fields[key]
|
||||
key = field.name
|
||||
spec = field.metadata.get("spec")
|
||||
# Not a real key, an internal var on our dataclass
|
||||
if not spec:
|
||||
LOG.warning(f"Internal key {key!r}")
|
||||
continue
|
||||
|
||||
spec_cls = spec
|
||||
if not inspect.isclass(spec_cls):
|
||||
spec_cls = spec_cls.__class__
|
||||
|
||||
# some kind of nested structure like sale_info
|
||||
if issubclass(spec_cls, SchemaBase):
|
||||
obj_dict[key] = spec.from_llsd(val, flavor)
|
||||
elif issubclass(spec_cls, SchemaFieldSerializer):
|
||||
obj_dict[key] = spec.from_llsd(val, flavor)
|
||||
else:
|
||||
raise ValueError(f"Unsupported spec for {key!r}, {spec!r}")
|
||||
else:
|
||||
if flavor != "ais":
|
||||
# AIS has a number of different fields that are irrelevant depending on
|
||||
# what exactly sent the payload
|
||||
LOG.warning(f"Unknown key {key!r}")
|
||||
except:
|
||||
LOG.error(f"Failed to parse inventory schema: {inv_dict!r}")
|
||||
raise
|
||||
return cls._obj_from_dict(obj_dict)
|
||||
|
||||
def to_bytes(self) -> bytes:
|
||||
return self.to_str().encode("utf8")
|
||||
|
||||
@@ -146,6 +231,32 @@ class SchemaBase(abc.ABC):
|
||||
writer.seek(0)
|
||||
return writer.read()
|
||||
|
||||
def to_llsd(self, flavor: str = "legacy"):
|
||||
obj_dict = {}
|
||||
for field_name, field in self._get_fields_dict(llsd_flavor=flavor).items():
|
||||
spec = field.metadata.get("spec")
|
||||
# Not meant to be serialized
|
||||
if not spec:
|
||||
continue
|
||||
|
||||
val = getattr(self, field.name)
|
||||
if val is None:
|
||||
continue
|
||||
|
||||
spec_cls = spec
|
||||
if not inspect.isclass(spec_cls):
|
||||
spec_cls = spec_cls.__class__
|
||||
|
||||
# Some kind of nested structure like sale_info
|
||||
if isinstance(val, SchemaBase):
|
||||
val = val.to_llsd(flavor)
|
||||
elif issubclass(spec_cls, SchemaFieldSerializer):
|
||||
val = spec.to_llsd(val, flavor)
|
||||
else:
|
||||
raise ValueError(f"Bad inventory spec {spec!r}")
|
||||
obj_dict[field_name] = val
|
||||
return obj_dict
|
||||
|
||||
@abc.abstractmethod
|
||||
def to_writer(self, writer: StringIO):
|
||||
pass
|
||||
|
||||
@@ -1,20 +1,27 @@
|
||||
import calendar
|
||||
import datetime
|
||||
import struct
|
||||
import typing
|
||||
import uuid
|
||||
import zlib
|
||||
|
||||
from llbase.llsd import *
|
||||
from llsd import *
|
||||
# So we can directly reference the original wrapper funcs where necessary
|
||||
import llbase.llsd
|
||||
import llsd as base_llsd
|
||||
from llsd.base import is_string, is_unicode
|
||||
|
||||
from hippolyzer.lib.base.datatypes import *
|
||||
|
||||
|
||||
class HippoLLSDBaseFormatter(llbase.llsd.LLSDBaseFormatter):
|
||||
class HippoLLSDBaseFormatter(base_llsd.base.LLSDBaseFormatter):
|
||||
UUID: callable
|
||||
ARRAY: callable
|
||||
BINARY: callable
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.type_map[UUID] = self.UUID
|
||||
self.type_map[JankStringyBytes] = self.BINARY
|
||||
self.type_map[Vector2] = self.TUPLECOORD
|
||||
self.type_map[Vector3] = self.TUPLECOORD
|
||||
self.type_map[Vector4] = self.TUPLECOORD
|
||||
@@ -24,44 +31,125 @@ class HippoLLSDBaseFormatter(llbase.llsd.LLSDBaseFormatter):
|
||||
return self.ARRAY(v.data())
|
||||
|
||||
|
||||
class HippoLLSDXMLFormatter(llbase.llsd.LLSDXMLFormatter, HippoLLSDBaseFormatter):
|
||||
class HippoLLSDXMLFormatter(base_llsd.serde_xml.LLSDXMLFormatter, HippoLLSDBaseFormatter):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
|
||||
class HippoLLSDXMLPrettyFormatter(llbase.llsd.LLSDXMLPrettyFormatter, HippoLLSDBaseFormatter):
|
||||
class HippoLLSDXMLPrettyFormatter(base_llsd.serde_xml.LLSDXMLPrettyFormatter, HippoLLSDBaseFormatter):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
|
||||
def format_pretty_xml(val: typing.Any):
|
||||
def format_pretty_xml(val: typing.Any) -> bytes:
|
||||
return HippoLLSDXMLPrettyFormatter().format(val)
|
||||
|
||||
|
||||
def format_xml(val: typing.Any):
|
||||
def format_xml(val: typing.Any) -> bytes:
|
||||
return HippoLLSDXMLFormatter().format(val)
|
||||
|
||||
|
||||
class HippoLLSDNotationFormatter(llbase.llsd.LLSDNotationFormatter, HippoLLSDBaseFormatter):
|
||||
class HippoLLSDNotationFormatter(base_llsd.serde_notation.LLSDNotationFormatter, HippoLLSDBaseFormatter):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def STRING(self, v):
|
||||
# llbase's notation LLSD encoder isn't suitable for generating line-delimited
|
||||
# LLSD because the string formatter leaves \n unencoded, unlike indra's llcommon.
|
||||
# Add our own escaping rule.
|
||||
return super().STRING(v).replace(b"\n", b"\\n")
|
||||
|
||||
def format_notation(val: typing.Any):
|
||||
|
||||
def format_notation(val: typing.Any) -> bytes:
|
||||
return HippoLLSDNotationFormatter().format(val)
|
||||
|
||||
|
||||
def format_binary(val: typing.Any, with_header=True):
|
||||
val = llbase.llsd.format_binary(val)
|
||||
if not with_header:
|
||||
return val.split(b"\n", 1)[1]
|
||||
def format_binary(val: typing.Any, with_header=True) -> bytes:
|
||||
val = _format_binary_recurse(val)
|
||||
if with_header:
|
||||
return b'<?llsd/binary?>\n' + val
|
||||
return val
|
||||
|
||||
|
||||
class HippoLLSDBinaryParser(llbase.llsd.LLSDBinaryParser):
|
||||
# This is copied almost wholesale from https://bitbucket.org/lindenlab/llbase/src/master/llbase/llsd.py
|
||||
# With a few minor changes to make serialization round-trip correctly. It's evil.
|
||||
def _format_binary_recurse(something) -> bytes:
|
||||
"""Binary formatter workhorse."""
|
||||
def _format_list(list_something):
|
||||
array_builder = [b'[' + struct.pack('!i', len(list_something))]
|
||||
for item in list_something:
|
||||
array_builder.append(_format_binary_recurse(item))
|
||||
array_builder.append(b']')
|
||||
return b''.join(array_builder)
|
||||
|
||||
if something is None:
|
||||
return b'!'
|
||||
elif isinstance(something, LLSD):
|
||||
return _format_binary_recurse(something.thing)
|
||||
elif isinstance(something, bool):
|
||||
if something:
|
||||
return b'1'
|
||||
else:
|
||||
return b'0'
|
||||
elif isinstance(something, int):
|
||||
try:
|
||||
return b'i' + struct.pack('!i', something)
|
||||
except (OverflowError, struct.error) as exc:
|
||||
raise LLSDSerializationError(str(exc), something)
|
||||
elif isinstance(something, float):
|
||||
try:
|
||||
return b'r' + struct.pack('!d', something)
|
||||
except SystemError as exc:
|
||||
raise LLSDSerializationError(str(exc), something)
|
||||
elif isinstance(something, uuid.UUID):
|
||||
return b'u' + something.bytes
|
||||
elif isinstance(something, (binary, JankStringyBytes)):
|
||||
return b'b' + struct.pack('!i', len(something)) + something
|
||||
elif is_string(something):
|
||||
if is_unicode(something):
|
||||
something = something.encode("utf8")
|
||||
return b's' + struct.pack('!i', len(something)) + something
|
||||
elif isinstance(something, uri):
|
||||
return b'l' + struct.pack('!i', len(something)) + something.encode("utf8")
|
||||
elif isinstance(something, datetime.datetime):
|
||||
return b'd' + struct.pack('<d', something.timestamp())
|
||||
elif isinstance(something, datetime.date):
|
||||
seconds_since_epoch = calendar.timegm(something.timetuple())
|
||||
return b'd' + struct.pack('<d', seconds_since_epoch)
|
||||
elif isinstance(something, (list, tuple)):
|
||||
return _format_list(something)
|
||||
elif isinstance(something, dict):
|
||||
map_builder = [b'{' + struct.pack('!i', len(something))]
|
||||
for key, value in something.items():
|
||||
if isinstance(key, str):
|
||||
key = key.encode("utf8")
|
||||
map_builder.append(b'k' + struct.pack('!i', len(key)) + key)
|
||||
map_builder.append(_format_binary_recurse(value))
|
||||
map_builder.append(b'}')
|
||||
return b''.join(map_builder)
|
||||
else:
|
||||
try:
|
||||
return _format_list(list(something))
|
||||
except TypeError:
|
||||
raise LLSDSerializationError(
|
||||
"Cannot serialize unknown type: %s (%s)" %
|
||||
(type(something), something))
|
||||
|
||||
|
||||
class HippoLLSDBinaryParser(base_llsd.serde_binary.LLSDBinaryParser):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._dispatch[ord('u')] = lambda: UUID(bytes=self._getc(16))
|
||||
self._dispatch[ord('d')] = self._parse_date
|
||||
|
||||
def _parse_date(self):
|
||||
seconds = struct.unpack("<d", self._getc(8))[0]
|
||||
try:
|
||||
return datetime.datetime.fromtimestamp(seconds, tz=datetime.timezone.utc)
|
||||
except OverflowError as exc:
|
||||
# A garbage seconds value can cause utcfromtimestamp() to raise
|
||||
# OverflowError: timestamp out of range for platform time_t
|
||||
self._error(exc, -8)
|
||||
|
||||
def _parse_string(self):
|
||||
# LLSD's C++ API lets you stuff binary in a string field even though it's only
|
||||
@@ -74,22 +162,26 @@ class HippoLLSDBinaryParser(llbase.llsd.LLSDBinaryParser):
|
||||
return bytes_val
|
||||
|
||||
|
||||
# Python uses one, C++ uses the other, and everyone's unhappy.
|
||||
_BINARY_HEADERS = (b'<? LLSD/Binary ?>', b'<?llsd/binary?>')
|
||||
|
||||
|
||||
def parse_binary(data: bytes):
|
||||
if data.startswith(b'<?llsd/binary?>'):
|
||||
if any(data.startswith(x) for x in _BINARY_HEADERS):
|
||||
data = data.split(b'\n', 1)[1]
|
||||
return HippoLLSDBinaryParser().parse(data)
|
||||
|
||||
|
||||
def parse_xml(data: bytes):
|
||||
return llbase.llsd.parse_xml(data)
|
||||
return base_llsd.parse_xml(data)
|
||||
|
||||
|
||||
def parse_notation(data: bytes):
|
||||
return llbase.llsd.parse_notation(data)
|
||||
return base_llsd.parse_notation(data)
|
||||
|
||||
|
||||
def zip_llsd(val: typing.Any):
|
||||
return zlib.compress(format_binary(val, with_header=False))
|
||||
return zlib.compress(format_binary(val, with_header=False), level=zlib.Z_BEST_COMPRESSION)
|
||||
|
||||
|
||||
def unzip_llsd(data: bytes):
|
||||
@@ -101,13 +193,13 @@ def parse(data: bytes):
|
||||
# content-type is usually nonsense.
|
||||
try:
|
||||
data = data.lstrip()
|
||||
if data.startswith(b'<?llsd/binary?>'):
|
||||
if any(data.startswith(x) for x in _BINARY_HEADERS):
|
||||
return parse_binary(data)
|
||||
elif data.startswith(b'<'):
|
||||
return parse_xml(data)
|
||||
else:
|
||||
return parse_notation(data)
|
||||
except KeyError as e:
|
||||
raise llbase.llsd.LLSDParseError('LLSD could not be parsed: %s' % (e,))
|
||||
raise base_llsd.LLSDParseError('LLSD could not be parsed: %s' % (e,))
|
||||
except TypeError as e:
|
||||
raise llbase.llsd.LLSDParseError('Input stream not of type bytes. %s' % (e,))
|
||||
raise base_llsd.LLSDParseError('Input stream not of type bytes. %s' % (e,))
|
||||
|
||||
@@ -11,21 +11,75 @@ from typing import *
|
||||
import zlib
|
||||
from copy import deepcopy
|
||||
|
||||
import numpy as np
|
||||
import recordclass
|
||||
|
||||
from hippolyzer.lib.base import serialization as se
|
||||
from hippolyzer.lib.base.datatypes import Vector3, Vector2, UUID, TupleCoord
|
||||
from hippolyzer.lib.base.llsd import zip_llsd, unzip_llsd
|
||||
from hippolyzer.lib.base.serialization import ParseContext
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def llsd_to_mat4(mat: Union[np.ndarray, Sequence[float]]) -> np.ndarray:
|
||||
return np.array(mat).reshape((4, 4), order='F')
|
||||
|
||||
|
||||
def mat4_to_llsd(mat: np.ndarray) -> List[float]:
|
||||
return list(mat.flatten(order='F'))
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class MeshAsset:
|
||||
header: MeshHeaderDict = dataclasses.field(default_factory=dict)
|
||||
segments: MeshSegmentDict = dataclasses.field(default_factory=dict)
|
||||
raw_segments: Dict[str, bytes] = dataclasses.field(default_factory=dict)
|
||||
|
||||
@classmethod
|
||||
def make_triangle(cls) -> MeshAsset:
|
||||
"""Make an asset representing an un-rigged single-sided mesh triangle"""
|
||||
inst = cls()
|
||||
inst.header = {
|
||||
"version": 1,
|
||||
"high_lod": {"offset": 0, "size": 0},
|
||||
"physics_mesh": {"offset": 0, "size": 0},
|
||||
"physics_convex": {"offset": 0, "size": 0},
|
||||
}
|
||||
base_lod: LODSegmentDict = {
|
||||
'Normal': [
|
||||
Vector3(-0.0, -0.0, -1.0),
|
||||
Vector3(-0.0, -0.0, -1.0),
|
||||
Vector3(-0.0, -0.0, -1.0)
|
||||
],
|
||||
'PositionDomain': {'Max': [0.5, 0.5, 0.0], 'Min': [-0.5, -0.5, 0.0]},
|
||||
'Position': [
|
||||
Vector3(0.0, 0.0, 0.0),
|
||||
Vector3(1.0, 0.0, 0.0),
|
||||
Vector3(0.5, 1.0, 0.0)
|
||||
],
|
||||
'TexCoord0Domain': {'Max': [1.0, 1.0], 'Min': [0.0, 0.0]},
|
||||
'TexCoord0': [
|
||||
Vector2(0.0, 0.0),
|
||||
Vector2(1.0, 0.0),
|
||||
Vector2(0.5, 1.0)
|
||||
],
|
||||
'TriangleList': [[0, 1, 2]],
|
||||
}
|
||||
inst.segments['physics_mesh'] = [deepcopy(base_lod)]
|
||||
inst.segments['high_lod'] = [deepcopy(base_lod)]
|
||||
convex_segment: PhysicsConvexSegmentDict = {
|
||||
'BoundingVerts': [
|
||||
Vector3(-0.0, 1.0, -1.0),
|
||||
Vector3(-1.0, -1.0, -1.0),
|
||||
Vector3(1.0, -1.0, -1.0)
|
||||
],
|
||||
'Max': [0.5, 0.5, 0.0],
|
||||
'Min': [-0.5, -0.5, 0.0]
|
||||
}
|
||||
inst.segments['physics_convex'] = convex_segment
|
||||
return inst
|
||||
|
||||
def iter_lods(self) -> Generator[List[LODSegmentDict], None, None]:
|
||||
for lod_name, lod_val in self.segments.items():
|
||||
if lod_name.endswith("_lod"):
|
||||
@@ -124,7 +178,7 @@ class DomainDict(TypedDict):
|
||||
Min: List[float]
|
||||
|
||||
|
||||
class VertexWeight(recordclass.datatuple): # type: ignore
|
||||
class VertexWeight(recordclass.RecordClass):
|
||||
"""Vertex weight for a specific joint on a specific vertex"""
|
||||
# index of the joint within the joint_names list in the skin segment
|
||||
joint_idx: int
|
||||
@@ -135,20 +189,26 @@ class VertexWeight(recordclass.datatuple): # type: ignore
|
||||
class SkinSegmentDict(TypedDict, total=False):
|
||||
"""Rigging information"""
|
||||
joint_names: List[str]
|
||||
# model -> world transform matrix for model
|
||||
# model -> world transform mat4 for model
|
||||
bind_shape_matrix: List[float]
|
||||
# world -> joint local transform matrices
|
||||
# world -> joint local transform mat4s
|
||||
inverse_bind_matrix: List[List[float]]
|
||||
# offset matrices for joints, translation-only.
|
||||
# Not sure what these are relative to, base joint or model <0,0,0>.
|
||||
# Transform mat4s for the joint nodes themselves.
|
||||
# The matrices may have scale or other components, but only the
|
||||
# translation component will be used by the viewer.
|
||||
# All translations are relative to the joint's parent.
|
||||
alt_inverse_bind_matrix: List[List[float]]
|
||||
lock_scale_if_joint_position: bool
|
||||
pelvis_offset: float
|
||||
|
||||
|
||||
class PhysicsConvexSegmentDict(DomainDict, total=False):
|
||||
"""Data for convex hull collisions, populated by the client"""
|
||||
# Min / Max domain vals are inline, unlike for LODs
|
||||
"""
|
||||
Data for convex hull collisions, populated by the client
|
||||
|
||||
Min / Max pos domain vals are inline, unlike for LODs, so this inherits from DomainDict
|
||||
"""
|
||||
# Indices into the Positions list
|
||||
HullList: List[int]
|
||||
# -1.0 - 1.0, dequantized from binary field of U16s
|
||||
Positions: List[Vector3]
|
||||
@@ -158,13 +218,13 @@ class PhysicsConvexSegmentDict(DomainDict, total=False):
|
||||
|
||||
class PhysicsHavokSegmentDict(TypedDict, total=False):
|
||||
"""Cached data for Havok collisions, populated by sim and not used by client."""
|
||||
HullMassProps: MassPropsDict
|
||||
MOPP: MOPPDict
|
||||
MeshDecompMassProps: MassPropsDict
|
||||
HullMassProps: HavokMassPropsDict
|
||||
MOPP: HavokMOPPDict
|
||||
MeshDecompMassProps: HavokMassPropsDict
|
||||
WeldingData: bytes
|
||||
|
||||
|
||||
class MassPropsDict(TypedDict, total=False):
|
||||
class HavokMassPropsDict(TypedDict, total=False):
|
||||
# Vec, center of mass
|
||||
CoM: List[float]
|
||||
# 9 floats, Mat3?
|
||||
@@ -173,7 +233,7 @@ class MassPropsDict(TypedDict, total=False):
|
||||
volume: float
|
||||
|
||||
|
||||
class MOPPDict(TypedDict, total=False):
|
||||
class HavokMOPPDict(TypedDict, total=False):
|
||||
"""Memory Optimized Partial Polytope"""
|
||||
BuildType: int
|
||||
MoppData: bytes
|
||||
@@ -205,7 +265,6 @@ def positions_to_domain(positions: Iterable[TupleCoord], domain: DomainDict):
|
||||
|
||||
class VertexWeights(se.SerializableBase):
|
||||
"""Serializer for a list of joint weights on a single vertex"""
|
||||
INFLUENCE_SER = se.QuantizedFloat(se.U16, 0.0, 1.0)
|
||||
INFLUENCE_LIMIT = 4
|
||||
INFLUENCE_TERM = 0xFF
|
||||
|
||||
@@ -216,18 +275,30 @@ class VertexWeights(se.SerializableBase):
|
||||
for val in vals:
|
||||
joint_idx, influence = val
|
||||
writer.write(se.U8, joint_idx)
|
||||
writer.write(cls.INFLUENCE_SER, influence, ctx=ctx)
|
||||
writer.write(se.U16, round(influence * 0xFFff), ctx=ctx)
|
||||
if len(vals) != cls.INFLUENCE_LIMIT:
|
||||
writer.write(se.U8, cls.INFLUENCE_TERM)
|
||||
|
||||
@classmethod
|
||||
def deserialize(cls, reader: se.Reader, ctx=None):
|
||||
# NOTE: normally you'd want to do something like arrange this into a nicely
|
||||
# aligned byte array with zero padding so that you could vectorize the decoding.
|
||||
# In cases where having a vertex with no weights is semantically equivalent to
|
||||
# having a vertex _with_ weights of a value of 0.0 that's fine. This isn't the case
|
||||
# in LL's implementation of mesh:
|
||||
#
|
||||
# https://bitbucket.org/lindenlab/viewer/src/d31a83fb946c49a38376ea3b312b5380d0c8c065/indra/llmath/llvolume.cpp#lines-2560:2628
|
||||
#
|
||||
# Consider the difference between handling of b"\x00\x00\x00\xFF" and b"\xFF" with the above logic.
|
||||
# To simplify round-tripping while preserving those semantics, we don't do a vectorized decode.
|
||||
# I had a vectorized numpy version, but those requirements made everything a bit of a mess.
|
||||
influence_list = []
|
||||
for _ in range(cls.INFLUENCE_LIMIT):
|
||||
joint_idx = reader.read(se.U8)
|
||||
joint_idx = reader.read_bytes(1)[0]
|
||||
if joint_idx == cls.INFLUENCE_TERM:
|
||||
break
|
||||
influence_list.append(VertexWeight(joint_idx, reader.read(cls.INFLUENCE_SER, ctx=ctx)))
|
||||
weight = reader.read(se.U16, ctx=ctx) / 0xFFff
|
||||
influence_list.append(VertexWeight(joint_idx, weight))
|
||||
return influence_list
|
||||
|
||||
|
||||
@@ -262,16 +333,46 @@ class SegmentSerializer:
|
||||
return new_segment
|
||||
|
||||
|
||||
class VecListAdapter(se.Adapter):
|
||||
def __init__(self, child_spec: se.SERIALIZABLE_TYPE, vec_type: Type):
|
||||
super().__init__(child_spec)
|
||||
self.vec_type = vec_type
|
||||
|
||||
def encode(self, val: Any, ctx: Optional[ParseContext]) -> Any:
|
||||
return val
|
||||
|
||||
def decode(self, val: Any, ctx: Optional[ParseContext], pod: bool = False) -> Any:
|
||||
new_vals = []
|
||||
for elem in val:
|
||||
new_vals.append(self.vec_type(*elem))
|
||||
return new_vals
|
||||
|
||||
|
||||
LE_U16: np.dtype = np.dtype(np.uint16).newbyteorder('<') # noqa
|
||||
|
||||
|
||||
LOD_SEGMENT_SERIALIZER = SegmentSerializer({
|
||||
# 16-bit indices to the verts making up the tri. Imposes a 16-bit
|
||||
# upper limit on verts in any given material in the mesh.
|
||||
"TriangleList": se.Collection(None, se.Collection(3, se.U16)),
|
||||
"TriangleList": se.ExprAdapter(
|
||||
se.NumPyArray(se.BytesGreedy(), LE_U16, 3),
|
||||
decode_func=lambda x: x.tolist(),
|
||||
),
|
||||
# These are used to interpolate between values in their respective domains
|
||||
# Each position represents a single vert.
|
||||
"Position": se.Collection(None, se.Vector3U16(0.0, 1.0)),
|
||||
"TexCoord0": se.Collection(None, se.Vector2U16(0.0, 1.0)),
|
||||
# Normals have a static domain between -1 and 1
|
||||
"Normal": se.Collection(None, se.Vector3U16(0.0, 1.0)),
|
||||
"Position": VecListAdapter(
|
||||
se.QuantizedNumPyArray(se.NumPyArray(se.BytesGreedy(), LE_U16, 3), 0.0, 1.0),
|
||||
Vector3,
|
||||
),
|
||||
"TexCoord0": VecListAdapter(
|
||||
se.QuantizedNumPyArray(se.NumPyArray(se.BytesGreedy(), LE_U16, 2), 0.0, 1.0),
|
||||
Vector2,
|
||||
),
|
||||
# Normals have a static domain between -1 and 1, so we just use that rather than 0.0 - 1.0.
|
||||
"Normal": VecListAdapter(
|
||||
se.QuantizedNumPyArray(se.NumPyArray(se.BytesGreedy(), LE_U16, 3), -1.0, 1.0),
|
||||
Vector3,
|
||||
),
|
||||
"Weights": se.Collection(None, VertexWeights)
|
||||
})
|
||||
|
||||
|
||||
121
hippolyzer/lib/base/mesh_skeleton.py
Normal file
121
hippolyzer/lib/base/mesh_skeleton.py
Normal file
@@ -0,0 +1,121 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import weakref
|
||||
from typing import *
|
||||
|
||||
import transformations
|
||||
from lxml import etree
|
||||
|
||||
from hippolyzer.lib.base.datatypes import Vector3, RAD_TO_DEG
|
||||
from hippolyzer.lib.base.helpers import get_resource_filename
|
||||
|
||||
|
||||
MAYBE_JOINT_REF = Optional[Callable[[], "JointNode"]]
|
||||
SKELETON_REF = Optional[Callable[[], "Skeleton"]]
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class JointNode:
|
||||
name: str
|
||||
parent: MAYBE_JOINT_REF
|
||||
skeleton: SKELETON_REF
|
||||
translation: Vector3
|
||||
pivot: Vector3 # pivot point for the joint, generally the same as translation
|
||||
rotation: Vector3 # Euler rotation in degrees
|
||||
scale: Vector3
|
||||
type: str # bone or collision_volume
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.name, self.type))
|
||||
|
||||
@property
|
||||
def matrix(self):
|
||||
return transformations.compose_matrix(
|
||||
scale=tuple(self.scale),
|
||||
angles=tuple(self.rotation / RAD_TO_DEG),
|
||||
translate=tuple(self.translation),
|
||||
)
|
||||
|
||||
@property
|
||||
def index(self) -> int:
|
||||
bone_idx = 0
|
||||
for node in self.skeleton().joint_dict.values():
|
||||
if node.type != "bone":
|
||||
continue
|
||||
if self is node:
|
||||
return bone_idx
|
||||
bone_idx += 1
|
||||
raise KeyError(f"{self.name!r} doesn't exist in skeleton")
|
||||
|
||||
@property
|
||||
def ancestors(self) -> Sequence[JointNode]:
|
||||
joint_node = self
|
||||
ancestors = []
|
||||
while joint_node.parent:
|
||||
joint_node = joint_node.parent()
|
||||
ancestors.append(joint_node)
|
||||
return ancestors
|
||||
|
||||
@property
|
||||
def children(self) -> Sequence[JointNode]:
|
||||
children = []
|
||||
for node in self.skeleton().joint_dict.values():
|
||||
if node.parent and node.parent() == self:
|
||||
children.append(node)
|
||||
return children
|
||||
|
||||
@property
|
||||
def descendents(self) -> Set[JointNode]:
|
||||
descendents = set()
|
||||
ancestors = {self}
|
||||
last_ancestors = set()
|
||||
while last_ancestors != ancestors:
|
||||
last_ancestors = ancestors
|
||||
for node in self.skeleton().joint_dict.values():
|
||||
if node.parent and node.parent() in ancestors:
|
||||
ancestors.add(node)
|
||||
descendents.add(node)
|
||||
return descendents
|
||||
|
||||
|
||||
class Skeleton:
|
||||
def __init__(self, root_node: etree.ElementBase):
|
||||
self.joint_dict: Dict[str, JointNode] = {}
|
||||
self._parse_node_children(root_node, None)
|
||||
|
||||
def __getitem__(self, item: str) -> JointNode:
|
||||
return self.joint_dict[item]
|
||||
|
||||
def _parse_node_children(self, node: etree.ElementBase, parent: MAYBE_JOINT_REF):
|
||||
name = node.get('name')
|
||||
joint = JointNode(
|
||||
name=name,
|
||||
parent=parent,
|
||||
skeleton=weakref.ref(self),
|
||||
translation=_get_vec_attr(node, "pos", Vector3()),
|
||||
pivot=_get_vec_attr(node, "pivot", Vector3()),
|
||||
rotation=_get_vec_attr(node, "rot", Vector3()),
|
||||
scale=_get_vec_attr(node, "scale", Vector3(1, 1, 1)),
|
||||
type=node.tag,
|
||||
)
|
||||
self.joint_dict[name] = joint
|
||||
for child in node.iterchildren():
|
||||
self._parse_node_children(child, weakref.ref(joint))
|
||||
|
||||
|
||||
def _get_vec_attr(node, attr_name: str, default: Vector3) -> Vector3:
|
||||
attr_val = node.get(attr_name, None)
|
||||
if not attr_val:
|
||||
return default
|
||||
return Vector3(*(float(x) for x in attr_val.split(" ") if x))
|
||||
|
||||
|
||||
def load_avatar_skeleton() -> Skeleton:
|
||||
skel_path = get_resource_filename("lib/base/data/avatar_skeleton.xml")
|
||||
with open(skel_path, 'r') as f:
|
||||
skel_root = etree.fromstring(f.read())
|
||||
return Skeleton(skel_root.getchildren()[0])
|
||||
|
||||
|
||||
AVATAR_SKELETON = load_avatar_skeleton()
|
||||
@@ -1,8 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
import asyncio
|
||||
import copy
|
||||
import dataclasses
|
||||
import datetime as dt
|
||||
import logging
|
||||
from collections import deque
|
||||
from typing import *
|
||||
from typing import Optional
|
||||
|
||||
@@ -13,15 +17,32 @@ from .msgtypes import PacketFlags
|
||||
from .udpserializer import UDPMessageSerializer
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class ReliableResendInfo:
|
||||
last_resent: dt.datetime
|
||||
message: Message
|
||||
completed: asyncio.Future = dataclasses.field(default_factory=asyncio.Future)
|
||||
tries_left: int = 10
|
||||
|
||||
|
||||
class Circuit:
|
||||
def __init__(self, near_host: Optional[ADDR_TUPLE], far_host: ADDR_TUPLE, transport):
|
||||
def __init__(
|
||||
self,
|
||||
near_host: Optional[ADDR_TUPLE],
|
||||
far_host: ADDR_TUPLE,
|
||||
transport: Optional[AbstractUDPTransport] = None,
|
||||
):
|
||||
self.near_host: Optional[ADDR_TUPLE] = near_host
|
||||
self.host: ADDR_TUPLE = far_host
|
||||
self.is_alive = True
|
||||
self.transport: Optional[AbstractUDPTransport] = transport
|
||||
self.transport = transport
|
||||
self.serializer = UDPMessageSerializer()
|
||||
self.last_packet_at = dt.datetime.now()
|
||||
self.packet_id_base = 0
|
||||
self.unacked_reliable: Dict[Tuple[Direction, int], ReliableResendInfo] = {}
|
||||
self.resend_every: float = 3.0
|
||||
# Reliable messages that we've already seen and handled, for resend suppression
|
||||
self.seen_reliable: deque[int] = deque(maxlen=1_000)
|
||||
|
||||
def _send_prepared_message(self, message: Message, transport=None):
|
||||
try:
|
||||
@@ -31,6 +52,11 @@ class Circuit:
|
||||
raise
|
||||
return self.send_datagram(serialized, message.direction, transport=transport)
|
||||
|
||||
def disconnect(self):
|
||||
self.packet_id_base = 0
|
||||
self.unacked_reliable.clear()
|
||||
self.is_alive = False
|
||||
|
||||
def send_datagram(self, data: bytes, direction: Direction, transport=None):
|
||||
self.last_packet_at = dt.datetime.now()
|
||||
src_addr, dst_addr = self.host, self.near_host
|
||||
@@ -46,24 +72,74 @@ class Circuit:
|
||||
raise RuntimeError(f"Trying to re-send finalized {message!r}")
|
||||
message.packet_id = self.packet_id_base
|
||||
self.packet_id_base += 1
|
||||
if not message.acks:
|
||||
message.send_flags &= PacketFlags.ACK
|
||||
if message.acks:
|
||||
message.send_flags |= PacketFlags.ACK
|
||||
else:
|
||||
message.send_flags &= ~PacketFlags.ACK
|
||||
# If it was queued, it's not anymore
|
||||
message.queued = False
|
||||
message.finalized = True
|
||||
return True
|
||||
|
||||
def send_message(self, message: Message, transport=None):
|
||||
def send(self, message: Message, transport=None) -> UDPPacket:
|
||||
if self.prepare_message(message):
|
||||
# If the message originates from us then we're responsible for resends.
|
||||
if message.reliable and message.synthetic:
|
||||
self.unacked_reliable[(message.direction, message.packet_id)] = ReliableResendInfo(
|
||||
last_resent=dt.datetime.now(),
|
||||
message=message,
|
||||
)
|
||||
return self._send_prepared_message(message, transport)
|
||||
|
||||
def send_reliable(self, message: Message, transport=None) -> asyncio.Future:
|
||||
"""send() wrapper that always sends reliably and allows `await`ing ACK receipt"""
|
||||
if not message.synthetic:
|
||||
raise ValueError("Not able to send non-synthetic message reliably!")
|
||||
message.send_flags |= PacketFlags.RELIABLE
|
||||
self.send(message, transport)
|
||||
return self.unacked_reliable[(message.direction, message.packet_id)].completed
|
||||
|
||||
def collect_acks(self, message: Message):
|
||||
effective_acks = list(message.acks)
|
||||
if message.name == "PacketAck":
|
||||
effective_acks.extend(x["ID"] for x in message["Packets"])
|
||||
for ack in effective_acks:
|
||||
resend_info = self.unacked_reliable.pop((~message.direction, ack), None)
|
||||
if resend_info:
|
||||
resend_info.completed.set_result(None)
|
||||
|
||||
def resend_unacked(self):
|
||||
for resend_info in list(self.unacked_reliable.values()):
|
||||
# Not time to attempt a resend yet
|
||||
if dt.datetime.now() - resend_info.last_resent < dt.timedelta(seconds=self.resend_every):
|
||||
continue
|
||||
|
||||
msg = copy.copy(resend_info.message)
|
||||
resend_info.tries_left -= 1
|
||||
# We were on our last try and we never received an ack
|
||||
if not resend_info.tries_left:
|
||||
logging.warning(f"Giving up on unacked {msg.packet_id}")
|
||||
del self.unacked_reliable[(msg.direction, msg.packet_id)]
|
||||
resend_info.completed.set_exception(TimeoutError("Exceeded resend limit"))
|
||||
continue
|
||||
resend_info.last_resent = dt.datetime.now()
|
||||
msg.send_flags |= PacketFlags.RESENT
|
||||
self._send_prepared_message(msg)
|
||||
|
||||
def send_acks(self, to_ack: Sequence[int], direction=Direction.OUT, packet_id=None):
|
||||
logging.debug("%r acking %r" % (direction, to_ack))
|
||||
# TODO: maybe tack this onto `.acks` for next message?
|
||||
message = Message('PacketAck', *[Block('Packets', ID=x) for x in to_ack])
|
||||
message.packet_id = packet_id
|
||||
message.direction = direction
|
||||
message.injected = True
|
||||
self.send_message(message)
|
||||
self.send(message)
|
||||
|
||||
def track_reliable(self, packet_id: int) -> bool:
|
||||
"""Tracks a reliable packet, returning if it's a new message"""
|
||||
if packet_id in self.seen_reliable:
|
||||
return False
|
||||
self.seen_reliable.append(packet_id)
|
||||
return True
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %r : %r>" % (self.__class__.__name__, self.near_host, self.host)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -29,7 +29,10 @@ from hippolyzer.lib.base.message.msgtypes import MsgType
|
||||
|
||||
PACKER = Callable[[Any], bytes]
|
||||
UNPACKER = Callable[[bytes], Any]
|
||||
LLSD_PACKER = Callable[[Any], Any]
|
||||
LLSD_UNPACKER = Callable[[Any], Any]
|
||||
SPEC = Tuple[UNPACKER, PACKER]
|
||||
LLSD_SPEC = Tuple[LLSD_UNPACKER, LLSD_PACKER]
|
||||
|
||||
|
||||
def _pack_string(pack_string):
|
||||
@@ -64,6 +67,21 @@ def _make_tuplecoord_spec(typ: Type[TupleCoord], struct_fmt: str,
|
||||
return lambda x: typ(*struct_obj.unpack(x)), _packer
|
||||
|
||||
|
||||
def _make_llsd_tuplecoord_spec(typ: Type[TupleCoord], needed_elems: Optional[int] = None):
|
||||
if needed_elems is None:
|
||||
# Number of elems needed matches the number in the coord type
|
||||
def _packer(x):
|
||||
return list(x)
|
||||
else:
|
||||
# Special case, we only want to pack some of the components.
|
||||
# Mostly for Quaternion since we don't actually need to send W.
|
||||
def _packer(x):
|
||||
if isinstance(x, TupleCoord):
|
||||
x = x.data()
|
||||
return list(x.data(needed_elems))
|
||||
return lambda x: typ(*x), _packer
|
||||
|
||||
|
||||
def _unpack_specs(cls):
|
||||
cls.UNPACKERS = {k: v[0] for (k, v) in cls.SPECS.items()}
|
||||
cls.PACKERS = {k: v[1] for (k, v) in cls.SPECS.items()}
|
||||
@@ -78,7 +96,7 @@ class TemplateDataPacker:
|
||||
MsgType.MVT_S8: _make_struct_spec('b'),
|
||||
MsgType.MVT_U8: _make_struct_spec('B'),
|
||||
MsgType.MVT_BOOL: _make_struct_spec('B'),
|
||||
MsgType.MVT_LLUUID: (lambda x: UUID(bytes=bytes(x)), lambda x: x.bytes),
|
||||
MsgType.MVT_LLUUID: (lambda x: UUID(bytes=bytes(x)), lambda x: UUID(x).bytes),
|
||||
MsgType.MVT_IP_ADDR: (socket.inet_ntoa, socket.inet_aton),
|
||||
MsgType.MVT_IP_PORT: _make_struct_spec('!H'),
|
||||
MsgType.MVT_U16: _make_struct_spec('<H'),
|
||||
@@ -110,10 +128,15 @@ class TemplateDataPacker:
|
||||
class LLSDDataPacker(TemplateDataPacker):
|
||||
# Some template var types aren't directly representable in LLSD, so they
|
||||
# get encoded to binary fields.
|
||||
SPECS = {
|
||||
SPECS: Dict[MsgType, LLSD_SPEC] = {
|
||||
MsgType.MVT_IP_ADDR: (socket.inet_ntoa, socket.inet_aton),
|
||||
# LLSD ints are technically bound to S32 range.
|
||||
MsgType.MVT_U32: _make_struct_spec('!I'),
|
||||
MsgType.MVT_U64: _make_struct_spec('!Q'),
|
||||
MsgType.MVT_S64: _make_struct_spec('!q'),
|
||||
# These are arrays in LLSD, we need to turn them into coords.
|
||||
MsgType.MVT_LLVector3: _make_llsd_tuplecoord_spec(Vector3),
|
||||
MsgType.MVT_LLVector3d: _make_llsd_tuplecoord_spec(Vector3),
|
||||
MsgType.MVT_LLVector4: _make_llsd_tuplecoord_spec(Vector4),
|
||||
MsgType.MVT_LLQuaternion: _make_llsd_tuplecoord_spec(Quaternion, needed_elems=3)
|
||||
}
|
||||
|
||||
@@ -75,8 +75,8 @@ class Block:
|
||||
for var_name, val in kwargs.items():
|
||||
self[var_name] = val
|
||||
|
||||
def get_variable(self, var_name):
|
||||
return self.vars.get(var_name)
|
||||
def get(self, var_name, default: Optional[VAR_TYPE] = None) -> Optional[VAR_TYPE]:
|
||||
return self.vars.get(var_name, default)
|
||||
|
||||
def __contains__(self, item):
|
||||
return item in self.vars
|
||||
@@ -188,7 +188,7 @@ class MsgBlockList(List["Block"]):
|
||||
class Message:
|
||||
__slots__ = ("name", "send_flags", "packet_id", "acks", "body_boundaries", "queued",
|
||||
"offset", "raw_extra", "raw_body", "deserializer", "_blocks", "finalized",
|
||||
"direction", "meta", "injected", "dropped", "sender")
|
||||
"direction", "meta", "synthetic", "dropped", "sender", "unknown_message")
|
||||
|
||||
def __init__(self, name, *args, packet_id=None, flags=0, acks=None, direction=None):
|
||||
# TODO: Do this on a timer or something.
|
||||
@@ -200,6 +200,7 @@ class Message:
|
||||
|
||||
self.acks = acks if acks is not None else tuple()
|
||||
self.body_boundaries = (-1, -1)
|
||||
self.unknown_message = False
|
||||
self.offset = 0
|
||||
self.raw_extra = b""
|
||||
self.direction: Direction = direction if direction is not None else Direction.OUT
|
||||
@@ -213,7 +214,7 @@ class Message:
|
||||
self.queued: bool = False
|
||||
self._blocks: BLOCK_DICT = {}
|
||||
self.meta = {}
|
||||
self.injected = False
|
||||
self.synthetic = packet_id is None
|
||||
self.dropped = False
|
||||
self.sender: Optional[ADDR_TUPLE] = None
|
||||
|
||||
@@ -222,7 +223,7 @@ class Message:
|
||||
def add_blocks(self, block_list):
|
||||
# can have a list of blocks if it is multiple or variable
|
||||
for block in block_list:
|
||||
if type(block) == list:
|
||||
if type(block) is list:
|
||||
for bl in block:
|
||||
self.add_block(bl)
|
||||
else:
|
||||
@@ -288,7 +289,7 @@ class Message:
|
||||
|
||||
def ensure_parsed(self):
|
||||
# This is a little magic, think about whether we want this.
|
||||
if self.raw_body and self.deserializer():
|
||||
if self.raw_body and self.deserializer and self.deserializer():
|
||||
self.deserializer().parse_message_body(self)
|
||||
|
||||
def to_dict(self, extended=False):
|
||||
@@ -312,7 +313,7 @@ class Message:
|
||||
"packet_id": self.packet_id,
|
||||
"meta": self.meta.copy(),
|
||||
"dropped": self.dropped,
|
||||
"injected": self.injected,
|
||||
"synthetic": self.synthetic,
|
||||
"direction": self.direction.name,
|
||||
"send_flags": int(self.send_flags),
|
||||
"extra": self.extra,
|
||||
@@ -334,13 +335,28 @@ class Message:
|
||||
msg.packet_id = dict_val['packet_id']
|
||||
msg.meta = dict_val['meta']
|
||||
msg.dropped = dict_val['dropped']
|
||||
msg.injected = dict_val['injected']
|
||||
msg.synthetic = dict_val['synthetic']
|
||||
msg.direction = Direction[dict_val['direction']]
|
||||
msg.send_flags = dict_val['send_flags']
|
||||
msg.extra = dict_val['extra']
|
||||
msg.acks = dict_val['acks']
|
||||
return msg
|
||||
|
||||
@classmethod
|
||||
def from_eq_event(cls, event) -> Message:
|
||||
# If this isn't a templated message (like some EQ-only events are),
|
||||
# then we wrap it in a synthetic `Message` so that the API for handling
|
||||
# both EQ-only and templated message events can be the same. Ick.
|
||||
msg = cls(event["message"])
|
||||
if isinstance(event["body"], dict):
|
||||
msg.add_block(Block("EventData", **event["body"]))
|
||||
else:
|
||||
# Shouldn't be any events that have anything other than a dict
|
||||
# as a body, but just to be sure...
|
||||
msg.add_block(Block("EventData", Data=event["body"]))
|
||||
msg.synthetic = True
|
||||
return msg
|
||||
|
||||
def invalidate_caches(self):
|
||||
# Don't have any caches if we haven't even parsed
|
||||
if self.raw_body:
|
||||
@@ -386,6 +402,7 @@ class Message:
|
||||
message_copy.packet_id = None
|
||||
message_copy.dropped = False
|
||||
message_copy.finalized = False
|
||||
message_copy.queued = False
|
||||
return message_copy
|
||||
|
||||
def to_summary(self):
|
||||
|
||||
@@ -20,7 +20,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
"""
|
||||
from logging import getLogger
|
||||
|
||||
from llbase import llsd
|
||||
import llsd
|
||||
|
||||
from hippolyzer.lib.base.message.data import msg_details
|
||||
|
||||
|
||||
@@ -62,9 +62,16 @@ class HumanMessageSerializer:
|
||||
continue
|
||||
|
||||
if first_line:
|
||||
direction, message_name = line.split(" ", 1)
|
||||
first_split = [x for x in line.split(" ") if x]
|
||||
direction, message_name = first_split[:2]
|
||||
options = [x.strip("[]") for x in first_split[2:]]
|
||||
msg = Message(message_name)
|
||||
msg.direction = Direction[direction.upper()]
|
||||
for option in options:
|
||||
if option in PacketFlags.__members__:
|
||||
msg.send_flags |= PacketFlags[option]
|
||||
elif re.match(r"^\d+$", option):
|
||||
msg.send_flags |= int(option)
|
||||
first_line = False
|
||||
continue
|
||||
|
||||
@@ -137,9 +144,17 @@ class HumanMessageSerializer:
|
||||
if msg.direction is not None:
|
||||
string += f'{msg.direction.name} '
|
||||
string += msg.name
|
||||
flags = msg.send_flags
|
||||
for poss_flag in iter(PacketFlags):
|
||||
if flags & poss_flag:
|
||||
flags &= ~poss_flag
|
||||
string += f" [{poss_flag.name}]"
|
||||
# Make sure flags with unknown meanings don't get lost
|
||||
if flags:
|
||||
string += f" [{int(flags)}]"
|
||||
if msg.packet_id is not None:
|
||||
string += f'\n# {msg.packet_id}: {PacketFlags(msg.send_flags)!r}'
|
||||
string += f'{", DROPPED" if msg.dropped else ""}{", INJECTED" if msg.injected else ""}'
|
||||
string += f'\n# ID: {msg.packet_id}'
|
||||
string += f'{", DROPPED" if msg.dropped else ""}{", SYNTHETIC" if msg.synthetic else ""}'
|
||||
if msg.extra:
|
||||
string += f'\n# EXTRA: {msg.extra!r}'
|
||||
string += '\n\n'
|
||||
|
||||
@@ -31,7 +31,8 @@ _T = TypeVar("_T")
|
||||
_K = TypeVar("_K", bound=Hashable)
|
||||
MESSAGE_HANDLER = Callable[[_T], Any]
|
||||
PREDICATE = Callable[[_T], bool]
|
||||
MESSAGE_NAMES = Iterable[_K]
|
||||
# TODO: Can't do `Iterable[Union[_K, Literal["*"]]]` apparently?
|
||||
MESSAGE_NAMES = Iterable[Union[_K, str]]
|
||||
|
||||
|
||||
class MessageHandler(Generic[_T, _K]):
|
||||
@@ -41,12 +42,11 @@ class MessageHandler(Generic[_T, _K]):
|
||||
|
||||
def register(self, message_name: _K) -> Event:
|
||||
LOG.debug('Creating a monitor for %s' % message_name)
|
||||
return self.handlers.setdefault(message_name, Event())
|
||||
return self.handlers.setdefault(message_name, Event(message_name))
|
||||
|
||||
def subscribe(self, message_name: _K, handler: MESSAGE_HANDLER) -> Event:
|
||||
def subscribe(self, message_name: Union[_K, Literal["*"]], handler: MESSAGE_HANDLER):
|
||||
notifier = self.register(message_name)
|
||||
notifier.subscribe(handler)
|
||||
return notifier
|
||||
|
||||
def _subscribe_all(self, message_names: MESSAGE_NAMES, handler: MESSAGE_HANDLER,
|
||||
predicate: Optional[PREDICATE] = None) -> List[Event]:
|
||||
@@ -107,12 +107,14 @@ class MessageHandler(Generic[_T, _K]):
|
||||
take = self.take_by_default
|
||||
notifiers = [self.register(name) for name in message_names]
|
||||
|
||||
fut = asyncio.get_event_loop().create_future()
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
fut = loop.create_future()
|
||||
timeout_task = None
|
||||
|
||||
async def _canceller():
|
||||
await asyncio.sleep(timeout)
|
||||
fut.set_exception(asyncio.exceptions.TimeoutError("Timed out waiting for packet"))
|
||||
if not fut.done():
|
||||
fut.set_exception(asyncio.exceptions.TimeoutError("Timed out waiting for packet"))
|
||||
for n in notifiers:
|
||||
n.unsubscribe(_handler)
|
||||
|
||||
@@ -125,7 +127,8 @@ class MessageHandler(Generic[_T, _K]):
|
||||
# Whatever was awaiting this future now owns this message
|
||||
if take:
|
||||
message = message.take()
|
||||
fut.set_result(message)
|
||||
if not fut.done():
|
||||
fut.set_result(message)
|
||||
# Make sure to unregister this handler for all message types
|
||||
for n in notifiers:
|
||||
n.unsubscribe(_handler)
|
||||
@@ -142,7 +145,7 @@ class MessageHandler(Generic[_T, _K]):
|
||||
# Always try to call wildcard handlers
|
||||
self._handle_type('*', message)
|
||||
|
||||
def _handle_type(self, name: _K, message: _T):
|
||||
def _handle_type(self, name: Union[_K, Literal["*"]], message: _T):
|
||||
handler = self.handlers.get(name)
|
||||
if not handler:
|
||||
return
|
||||
|
||||
@@ -47,7 +47,6 @@ class MsgBlockType:
|
||||
MBT_SINGLE = 0
|
||||
MBT_MULTIPLE = 1
|
||||
MBT_VARIABLE = 2
|
||||
MBT_String_List = ['Single', 'Multiple', 'Variable']
|
||||
|
||||
|
||||
class PacketFlags(enum.IntFlag):
|
||||
@@ -55,6 +54,8 @@ class PacketFlags(enum.IntFlag):
|
||||
RELIABLE = 0x40
|
||||
RESENT = 0x20
|
||||
ACK = 0x10
|
||||
# Not a real flag, just used for display.
|
||||
EQ = 1 << 10
|
||||
|
||||
|
||||
# frequency for messages
|
||||
@@ -62,28 +63,23 @@ class PacketFlags(enum.IntFlag):
|
||||
# = '\xFF\xFF'
|
||||
# = '\xFF'
|
||||
# = ''
|
||||
class MsgFrequency:
|
||||
FIXED_FREQUENCY_MESSAGE = -1 # marking it
|
||||
LOW_FREQUENCY_MESSAGE = 4
|
||||
MEDIUM_FREQUENCY_MESSAGE = 2
|
||||
HIGH_FREQUENCY_MESSAGE = 1
|
||||
class MsgFrequency(enum.IntEnum):
|
||||
FIXED = -1 # marking it
|
||||
LOW = 4
|
||||
MEDIUM = 2
|
||||
HIGH = 1
|
||||
|
||||
|
||||
class MsgTrust:
|
||||
LL_NOTRUST = 0
|
||||
LL_TRUSTED = 1
|
||||
class MsgEncoding(enum.IntEnum):
|
||||
UNENCODED = 0
|
||||
ZEROCODED = 1
|
||||
|
||||
|
||||
class MsgEncoding:
|
||||
LL_UNENCODED = 0
|
||||
LL_ZEROCODED = 1
|
||||
|
||||
|
||||
class MsgDeprecation:
|
||||
LL_DEPRECATED = 0
|
||||
LL_UDPDEPRECATED = 1
|
||||
LL_UDPBLACKLISTED = 2
|
||||
LL_NOTDEPRECATED = 3
|
||||
class MsgDeprecation(enum.IntEnum):
|
||||
DEPRECATED = 0
|
||||
UDPDEPRECATED = 1
|
||||
UDPBLACKLISTED = 2
|
||||
NOTDEPRECATED = 3
|
||||
|
||||
|
||||
# message variable types
|
||||
|
||||
@@ -21,7 +21,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
import typing
|
||||
|
||||
from .msgtypes import MsgType, MsgBlockType
|
||||
from .msgtypes import MsgType, MsgBlockType, MsgFrequency
|
||||
from ..datatypes import UUID
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ class MessageTemplateVariable:
|
||||
return f"{self.__class__.__name__}(name={self.name!r}, tp={self.type!r}, size={self.size!r})"
|
||||
|
||||
@property
|
||||
def probably_binary(self):
|
||||
def probably_binary(self) -> bool:
|
||||
if self._probably_binary is not None:
|
||||
return self._probably_binary
|
||||
|
||||
@@ -49,7 +49,7 @@ class MessageTemplateVariable:
|
||||
return self._probably_binary
|
||||
|
||||
@property
|
||||
def probably_text(self):
|
||||
def probably_text(self) -> bool:
|
||||
if self._probably_text is not None:
|
||||
return self._probably_text
|
||||
|
||||
@@ -97,49 +97,36 @@ class MessageTemplateBlock:
|
||||
self.block_type: MsgBlockType = MsgBlockType.MBT_SINGLE
|
||||
self.number = 0
|
||||
|
||||
def add_variable(self, var):
|
||||
def add_variable(self, var: MessageTemplateVariable):
|
||||
self.variable_map[var.name] = var
|
||||
self.variables.append(var)
|
||||
|
||||
def get_variable(self, name):
|
||||
def get_variable(self, name) -> MessageTemplateVariable:
|
||||
return self.variable_map[name]
|
||||
|
||||
|
||||
class MessageTemplate(object):
|
||||
frequency_strings = {-1: 'fixed', 1: 'high', 2: 'medium', 4: 'low'} # strings for printout
|
||||
deprecation_strings = ["Deprecated", "UDPDeprecated", "UDPBlackListed", "NotDeprecated"] # using _as_string methods
|
||||
encoding_strings = ["Unencoded", "Zerocoded"] # etc
|
||||
trusted_strings = ["Trusted", "NotTrusted"] # etc LDE 24oct2008
|
||||
|
||||
class MessageTemplate:
|
||||
def __init__(self, name):
|
||||
self.blocks: typing.List[MessageTemplateBlock] = []
|
||||
self.block_map: typing.Dict[str, MessageTemplateBlock] = {}
|
||||
|
||||
# this is the function or object that will handle this type of message
|
||||
self.received_count = 0
|
||||
|
||||
self.name = name
|
||||
self.frequency = None
|
||||
self.msg_num = 0
|
||||
self.msg_freq_num_bytes = None
|
||||
self.msg_trust = None
|
||||
self.msg_deprecation = None
|
||||
self.msg_encoding = None
|
||||
self.frequency: typing.Optional[MsgFrequency] = None
|
||||
self.num = 0
|
||||
# Frequency + msg num as bytes
|
||||
self.freq_num_bytes = None
|
||||
self.trusted = False
|
||||
self.deprecation = None
|
||||
self.encoding = None
|
||||
|
||||
def add_block(self, block):
|
||||
def add_block(self, block: MessageTemplateBlock):
|
||||
self.block_map[block.name] = block
|
||||
self.blocks.append(block)
|
||||
|
||||
def get_block(self, name):
|
||||
def get_block(self, name) -> MessageTemplateBlock:
|
||||
return self.block_map[name]
|
||||
|
||||
def get_msg_freq_num_len(self):
|
||||
if self.frequency == -1:
|
||||
if self.frequency == MsgFrequency.FIXED:
|
||||
return 4
|
||||
return self.frequency
|
||||
|
||||
def get_frequency_as_string(self):
|
||||
return MessageTemplate.frequency_strings[self.frequency]
|
||||
|
||||
def get_deprecation_as_string(self):
|
||||
return MessageTemplate.deprecation_strings[self.msg_deprecation]
|
||||
|
||||
@@ -43,7 +43,7 @@ class TemplateDictionary:
|
||||
|
||||
self.template_list: typing.List[MessageTemplate] = []
|
||||
# maps name to template
|
||||
self.message_templates = {}
|
||||
self.message_templates: typing.Dict[str, MessageTemplate] = {}
|
||||
|
||||
# maps (freq,num) to template
|
||||
self.message_dict = {}
|
||||
@@ -68,32 +68,32 @@ class TemplateDictionary:
|
||||
|
||||
# do a mapping of type to a string for easier reference
|
||||
frequency_str = ''
|
||||
if template.frequency == MsgFrequency.FIXED_FREQUENCY_MESSAGE:
|
||||
if template.frequency == MsgFrequency.FIXED:
|
||||
frequency_str = "Fixed"
|
||||
elif template.frequency == MsgFrequency.LOW_FREQUENCY_MESSAGE:
|
||||
elif template.frequency == MsgFrequency.LOW:
|
||||
frequency_str = "Low"
|
||||
elif template.frequency == MsgFrequency.MEDIUM_FREQUENCY_MESSAGE:
|
||||
elif template.frequency == MsgFrequency.MEDIUM:
|
||||
frequency_str = "Medium"
|
||||
elif template.frequency == MsgFrequency.HIGH_FREQUENCY_MESSAGE:
|
||||
elif template.frequency == MsgFrequency.HIGH:
|
||||
frequency_str = "High"
|
||||
|
||||
self.message_dict[(frequency_str,
|
||||
template.msg_num)] = template
|
||||
template.num)] = template
|
||||
|
||||
def build_message_ids(self):
|
||||
for template in list(self.message_templates.values()):
|
||||
frequency = template.frequency
|
||||
num_bytes = None
|
||||
if frequency == MsgFrequency.FIXED_FREQUENCY_MESSAGE:
|
||||
if frequency == MsgFrequency.FIXED:
|
||||
# have to do this because Fixed messages are stored as a long in the template
|
||||
num_bytes = b'\xff\xff\xff' + struct.pack("B", template.msg_num)
|
||||
elif frequency == MsgFrequency.LOW_FREQUENCY_MESSAGE:
|
||||
num_bytes = b'\xff\xff' + struct.pack("!H", template.msg_num)
|
||||
elif frequency == MsgFrequency.MEDIUM_FREQUENCY_MESSAGE:
|
||||
num_bytes = b'\xff' + struct.pack("B", template.msg_num)
|
||||
elif frequency == MsgFrequency.HIGH_FREQUENCY_MESSAGE:
|
||||
num_bytes = struct.pack("B", template.msg_num)
|
||||
template.msg_freq_num_bytes = num_bytes
|
||||
num_bytes = b'\xff\xff\xff' + struct.pack("B", template.num)
|
||||
elif frequency == MsgFrequency.LOW:
|
||||
num_bytes = b'\xff\xff' + struct.pack("!H", template.num)
|
||||
elif frequency == MsgFrequency.MEDIUM:
|
||||
num_bytes = b'\xff' + struct.pack("B", template.num)
|
||||
elif frequency == MsgFrequency.HIGH:
|
||||
num_bytes = struct.pack("B", template.num)
|
||||
template.freq_num_bytes = num_bytes
|
||||
|
||||
def get_template_by_name(self, template_name) -> typing.Optional[MessageTemplate]:
|
||||
return self.message_templates.get(template_name)
|
||||
|
||||
@@ -22,7 +22,7 @@ import struct
|
||||
import re
|
||||
|
||||
from . import template
|
||||
from .msgtypes import MsgFrequency, MsgTrust, MsgEncoding
|
||||
from .msgtypes import MsgFrequency, MsgEncoding
|
||||
from .msgtypes import MsgDeprecation, MsgBlockType, MsgType
|
||||
from ..exc import MessageTemplateParsingError, MessageTemplateNotFound
|
||||
|
||||
@@ -112,67 +112,69 @@ class MessageTemplateParser:
|
||||
frequency = None
|
||||
freq_str = match.group(2)
|
||||
if freq_str == 'Low':
|
||||
frequency = MsgFrequency.LOW_FREQUENCY_MESSAGE
|
||||
frequency = MsgFrequency.LOW
|
||||
elif freq_str == 'Medium':
|
||||
frequency = MsgFrequency.MEDIUM_FREQUENCY_MESSAGE
|
||||
frequency = MsgFrequency.MEDIUM
|
||||
elif freq_str == 'High':
|
||||
frequency = MsgFrequency.HIGH_FREQUENCY_MESSAGE
|
||||
frequency = MsgFrequency.HIGH
|
||||
elif freq_str == 'Fixed':
|
||||
frequency = MsgFrequency.FIXED_FREQUENCY_MESSAGE
|
||||
frequency = MsgFrequency.FIXED
|
||||
|
||||
new_template.frequency = frequency
|
||||
|
||||
msg_num = int(match.group(3), 0)
|
||||
if frequency == MsgFrequency.FIXED_FREQUENCY_MESSAGE:
|
||||
if frequency == MsgFrequency.FIXED:
|
||||
# have to do this because Fixed messages are stored as a long in the template
|
||||
msg_num &= 0xff
|
||||
msg_num_bytes = struct.pack('!BBBB', 0xff, 0xff, 0xff, msg_num)
|
||||
elif frequency == MsgFrequency.LOW_FREQUENCY_MESSAGE:
|
||||
elif frequency == MsgFrequency.LOW:
|
||||
msg_num_bytes = struct.pack('!BBH', 0xff, 0xff, msg_num)
|
||||
elif frequency == MsgFrequency.MEDIUM_FREQUENCY_MESSAGE:
|
||||
elif frequency == MsgFrequency.MEDIUM:
|
||||
msg_num_bytes = struct.pack('!BB', 0xff, msg_num)
|
||||
elif frequency == MsgFrequency.HIGH_FREQUENCY_MESSAGE:
|
||||
elif frequency == MsgFrequency.HIGH:
|
||||
msg_num_bytes = struct.pack('!B', msg_num)
|
||||
else:
|
||||
raise Exception("don't know about frequency %s" % frequency)
|
||||
|
||||
new_template.msg_num = msg_num
|
||||
new_template.msg_freq_num_bytes = msg_num_bytes
|
||||
new_template.num = msg_num
|
||||
new_template.freq_num_bytes = msg_num_bytes
|
||||
|
||||
msg_trust = None
|
||||
msg_trust_str = match.group(4)
|
||||
if msg_trust_str == 'Trusted':
|
||||
msg_trust = MsgTrust.LL_TRUSTED
|
||||
msg_trust = True
|
||||
elif msg_trust_str == 'NotTrusted':
|
||||
msg_trust = MsgTrust.LL_NOTRUST
|
||||
msg_trust = False
|
||||
else:
|
||||
raise ValueError(f"Invalid trust {msg_trust_str}")
|
||||
|
||||
new_template.msg_trust = msg_trust
|
||||
new_template.trusted = msg_trust
|
||||
|
||||
msg_encoding = None
|
||||
msg_encoding_str = match.group(5)
|
||||
if msg_encoding_str == 'Unencoded':
|
||||
msg_encoding = MsgEncoding.LL_UNENCODED
|
||||
msg_encoding = MsgEncoding.UNENCODED
|
||||
elif msg_encoding_str == 'Zerocoded':
|
||||
msg_encoding = MsgEncoding.LL_ZEROCODED
|
||||
msg_encoding = MsgEncoding.ZEROCODED
|
||||
else:
|
||||
raise ValueError(f"Invalid encoding {msg_encoding_str}")
|
||||
|
||||
new_template.msg_encoding = msg_encoding
|
||||
new_template.encoding = msg_encoding
|
||||
|
||||
msg_dep = None
|
||||
msg_dep_str = match.group(7)
|
||||
if msg_dep_str:
|
||||
if msg_dep_str == 'Deprecated':
|
||||
msg_dep = MsgDeprecation.LL_DEPRECATED
|
||||
msg_dep = MsgDeprecation.DEPRECATED
|
||||
elif msg_dep_str == 'UDPDeprecated':
|
||||
msg_dep = MsgDeprecation.LL_UDPDEPRECATED
|
||||
msg_dep = MsgDeprecation.UDPDEPRECATED
|
||||
elif msg_dep_str == 'UDPBlackListed':
|
||||
msg_dep = MsgDeprecation.LL_UDPBLACKLISTED
|
||||
msg_dep = MsgDeprecation.UDPBLACKLISTED
|
||||
elif msg_dep_str == 'NotDeprecated':
|
||||
msg_dep = MsgDeprecation.LL_NOTDEPRECATED
|
||||
msg_dep = MsgDeprecation.NOTDEPRECATED
|
||||
else:
|
||||
msg_dep = MsgDeprecation.LL_NOTDEPRECATED
|
||||
msg_dep = MsgDeprecation.NOTDEPRECATED
|
||||
if msg_dep is None:
|
||||
raise MessageTemplateParsingError("Unknown msg_dep field %s" % match.group(0))
|
||||
new_template.msg_deprecation = msg_dep
|
||||
new_template.deprecation = msg_dep
|
||||
|
||||
return new_template
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ class UDPMessageDeserializer:
|
||||
self.settings = settings or Settings()
|
||||
self.template_dict = self.DEFAULT_TEMPLATE
|
||||
|
||||
def deserialize(self, msg_buff: bytes):
|
||||
def deserialize(self, msg_buff: bytes) -> Message:
|
||||
msg = self._parse_message_header(msg_buff)
|
||||
if not self.settings.ENABLE_DEFERRED_PACKET_PARSING:
|
||||
try:
|
||||
@@ -85,6 +85,7 @@ class UDPMessageDeserializer:
|
||||
reader = se.BufferReader("!", data)
|
||||
|
||||
msg: Message = Message("Placeholder")
|
||||
msg.synthetic = False
|
||||
msg.send_flags = reader.read(se.U8)
|
||||
msg.packet_id = reader.read(se.U32)
|
||||
|
||||
@@ -125,8 +126,14 @@ class UDPMessageDeserializer:
|
||||
frequency, num = _parse_msg_num(reader)
|
||||
current_template = self.template_dict.get_template_by_pair(frequency, num)
|
||||
if current_template is None:
|
||||
raise exc.MessageTemplateNotFound("deserializing data")
|
||||
msg.name = current_template.name
|
||||
if self.settings.ALLOW_UNKNOWN_MESSAGES:
|
||||
LOG.warning(f"Unknown message type {frequency}:{num}")
|
||||
msg.unknown_message = True
|
||||
msg.name = "UnknownMessage:%d" % num
|
||||
else:
|
||||
raise exc.MessageTemplateNotFound("deserializing data", f"{frequency}:{num}")
|
||||
else:
|
||||
msg.name = current_template.name
|
||||
|
||||
# extra field, see note regarding msg.offset
|
||||
msg.raw_extra = reader.read_bytes(msg.offset)
|
||||
@@ -142,6 +149,12 @@ class UDPMessageDeserializer:
|
||||
# Already parsed if we don't have a raw body
|
||||
if not raw_body:
|
||||
return
|
||||
|
||||
if msg.unknown_message:
|
||||
# We can't parse this, we don't know anything about it
|
||||
msg.deserializer = None
|
||||
return
|
||||
|
||||
msg.raw_body = None
|
||||
msg.deserializer = None
|
||||
|
||||
@@ -156,7 +169,6 @@ class UDPMessageDeserializer:
|
||||
reader.seek(current_template.get_msg_freq_num_len() + msg.offset)
|
||||
|
||||
for tmpl_block in current_template.blocks:
|
||||
LOG.debug("Parsing %s:%s" % (msg.name, tmpl_block.name))
|
||||
# EOF?
|
||||
if not len(reader):
|
||||
# Seems like even some "Single" blocks are optional?
|
||||
@@ -179,7 +191,6 @@ class UDPMessageDeserializer:
|
||||
|
||||
for i in range(repeat_count):
|
||||
current_block = Block(tmpl_block.name)
|
||||
LOG.debug("Adding block %s" % current_block.name)
|
||||
msg.add_block(current_block)
|
||||
|
||||
for tmpl_variable in tmpl_block.variables:
|
||||
@@ -221,11 +232,17 @@ class UDPMessageDeserializer:
|
||||
if tmpl_variable.probably_binary:
|
||||
return unpacked_data
|
||||
# Truncated strings need to be treated carefully
|
||||
if tmpl_variable.probably_text and unpacked_data.endswith(b"\x00"):
|
||||
try:
|
||||
return unpacked_data.decode("utf8").rstrip("\x00")
|
||||
except UnicodeDecodeError:
|
||||
return JankStringyBytes(unpacked_data)
|
||||
if tmpl_variable.probably_text:
|
||||
# If it has a null terminator, let's try to decode it first.
|
||||
# We don't want to do this if there isn't one, because that may change
|
||||
# the meaning of the data.
|
||||
if unpacked_data.endswith(b"\x00"):
|
||||
try:
|
||||
return unpacked_data.decode("utf8").rstrip("\x00")
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
# Failed, return jank stringy bytes
|
||||
return JankStringyBytes(unpacked_data)
|
||||
elif tmpl_variable.type in {MsgType.MVT_FIXED, MsgType.MVT_VARIABLE}:
|
||||
# No idea if this should be bytes or a string... make an object that's sort of both.
|
||||
return JankStringyBytes(unpacked_data)
|
||||
|
||||
@@ -45,7 +45,7 @@ class UDPMessageSerializer:
|
||||
|
||||
def serialize(self, msg: Message):
|
||||
current_template = self.template_dict.get_template_by_name(msg.name)
|
||||
if current_template is None:
|
||||
if current_template is None and msg.raw_body is None:
|
||||
raise exc.MessageSerializationError("message name", "invalid message name")
|
||||
|
||||
# Header and trailers are all big-endian
|
||||
@@ -69,7 +69,7 @@ class UDPMessageSerializer:
|
||||
# frequency and message number. The template stores it because it doesn't
|
||||
# change per template.
|
||||
body_writer = se.BufferWriter("<")
|
||||
body_writer.write_bytes(current_template.msg_freq_num_bytes)
|
||||
body_writer.write_bytes(current_template.freq_num_bytes)
|
||||
body_writer.write_bytes(msg.extra)
|
||||
|
||||
# We're going to pop off keys as we go, so shallow copy the dict.
|
||||
|
||||
@@ -82,8 +82,9 @@ CAPS_DICT = Union[
|
||||
|
||||
|
||||
class CapsClient:
|
||||
def __init__(self, caps: Optional[CAPS_DICT] = None):
|
||||
def __init__(self, caps: Optional[CAPS_DICT] = None, session: Optional[aiohttp.ClientSession] = None) -> None:
|
||||
self._caps = caps
|
||||
self._session = session
|
||||
|
||||
def _request_fixups(self, cap_or_url: str, headers: Dict, proxy: Optional[bool], ssl: Any):
|
||||
return cap_or_url, headers, proxy, ssl
|
||||
@@ -117,6 +118,7 @@ class CapsClient:
|
||||
session_owned = False
|
||||
# Use an existing session if we have one to take advantage of connection pooling
|
||||
# otherwise create one
|
||||
session = session or self._session
|
||||
if session is None:
|
||||
session_owned = True
|
||||
session = aiohttp.ClientSession(
|
||||
|
||||
@@ -46,6 +46,9 @@ class UDPPacket:
|
||||
return self.dst_addr
|
||||
return self.src_addr
|
||||
|
||||
def __repr__(self):
|
||||
return f"<{self.__class__.__name__} src_addr={self.src_addr!r} dst_addr={self.dst_addr!r} data={self.data!r}>"
|
||||
|
||||
|
||||
class AbstractUDPTransport(abc.ABC):
|
||||
__slots__ = ()
|
||||
|
||||
@@ -35,12 +35,7 @@ import hippolyzer.lib.base.serialization as se
|
||||
import hippolyzer.lib.base.templates as tmpls
|
||||
|
||||
|
||||
class Object(recordclass.datatuple): # type: ignore
|
||||
__options__ = {
|
||||
"use_weakref": True,
|
||||
}
|
||||
__weakref__: Any
|
||||
|
||||
class Object(recordclass.RecordClass, use_weakref=True): # type: ignore
|
||||
LocalID: Optional[int] = None
|
||||
State: Optional[int] = None
|
||||
FullID: Optional[UUID] = None
|
||||
@@ -71,7 +66,7 @@ class Object(recordclass.datatuple): # type: ignore
|
||||
ProfileBegin: Optional[int] = None
|
||||
ProfileEnd: Optional[int] = None
|
||||
ProfileHollow: Optional[int] = None
|
||||
TextureEntry: Optional[tmpls.TextureEntry] = None
|
||||
TextureEntry: Optional[tmpls.TextureEntryCollection] = None
|
||||
TextureAnim: Optional[tmpls.TextureAnim] = None
|
||||
NameValue: Optional[Any] = None
|
||||
Data: Optional[Any] = None
|
||||
@@ -199,6 +194,28 @@ class Object(recordclass.datatuple): # type: ignore
|
||||
del val["Parent"]
|
||||
return val
|
||||
|
||||
@property
|
||||
def Ancestors(self) -> List[Object]:
|
||||
obj = self
|
||||
ancestors = []
|
||||
while obj.Parent:
|
||||
obj = obj.Parent
|
||||
ancestors.append(obj)
|
||||
return ancestors
|
||||
|
||||
@property
|
||||
def Descendents(self) -> List[Object]:
|
||||
new_children = [self]
|
||||
descendents = []
|
||||
while new_children:
|
||||
to_check = new_children[:]
|
||||
new_children.clear()
|
||||
for obj in to_check:
|
||||
for child in obj.Children:
|
||||
new_children.append(child)
|
||||
descendents.append(child)
|
||||
return descendents
|
||||
|
||||
|
||||
def handle_to_gridxy(handle: int) -> Tuple[int, int]:
|
||||
return (handle >> 32) // 256, (handle & 0xFFffFFff) // 256
|
||||
@@ -270,6 +287,9 @@ def normalize_object_update_compressed_data(data: bytes):
|
||||
# Only used for determining which sections are present
|
||||
del compressed["Flags"]
|
||||
|
||||
# Unlike other ObjectUpdate types, a null value in an ObjectUpdateCompressed
|
||||
# always means that there is no value, not that the value hasn't changed
|
||||
# from the client's view. Use the default value when that happens.
|
||||
ps_block = compressed.pop("PSBlockNew", None)
|
||||
if ps_block is None:
|
||||
ps_block = compressed.pop("PSBlock", None)
|
||||
@@ -278,6 +298,20 @@ def normalize_object_update_compressed_data(data: bytes):
|
||||
compressed.pop("PSBlock", None)
|
||||
if compressed["NameValue"] is None:
|
||||
compressed["NameValue"] = NameValueCollection()
|
||||
if compressed["Text"] is None:
|
||||
compressed["Text"] = b""
|
||||
compressed["TextColor"] = b""
|
||||
if compressed["MediaURL"] is None:
|
||||
compressed["MediaURL"] = b""
|
||||
if compressed["AngularVelocity"] is None:
|
||||
compressed["AngularVelocity"] = Vector3()
|
||||
if compressed["SoundFlags"] is None:
|
||||
compressed["SoundFlags"] = 0
|
||||
compressed["SoundGain"] = 0.0
|
||||
compressed["SoundRadius"] = 0.0
|
||||
compressed["Sound"] = UUID()
|
||||
if compressed["TextureEntry"] is None:
|
||||
compressed["TextureEntry"] = tmpls.TextureEntryCollection()
|
||||
|
||||
object_data = {
|
||||
"PSBlock": ps_block.value,
|
||||
@@ -286,9 +320,9 @@ def normalize_object_update_compressed_data(data: bytes):
|
||||
"LocalID": compressed.pop("ID"),
|
||||
**compressed,
|
||||
}
|
||||
if object_data["TextureEntry"] is None:
|
||||
object_data.pop("TextureEntry")
|
||||
# Don't clobber OwnerID in case the object has a proper one.
|
||||
# Don't clobber OwnerID in case the object has a proper one from
|
||||
# a previous ObjectProperties. OwnerID isn't expected to be populated
|
||||
# on ObjectUpdates unless an attached sound is playing.
|
||||
if object_data["OwnerID"] == UUID():
|
||||
del object_data["OwnerID"]
|
||||
return object_data
|
||||
|
||||
@@ -10,6 +10,7 @@ from io import SEEK_CUR, SEEK_SET, SEEK_END, RawIOBase, BufferedIOBase
|
||||
from typing import *
|
||||
|
||||
import lazy_object_proxy
|
||||
import numpy as np
|
||||
|
||||
import hippolyzer.lib.base.llsd as llsd
|
||||
import hippolyzer.lib.base.datatypes as dtypes
|
||||
@@ -27,6 +28,14 @@ class _Unserializable:
|
||||
return False
|
||||
|
||||
|
||||
class MissingType:
|
||||
"""Simple sentinel type like dataclasses._MISSING_TYPE"""
|
||||
pass
|
||||
|
||||
|
||||
MISSING = MissingType()
|
||||
|
||||
|
||||
UNSERIALIZABLE = _Unserializable()
|
||||
_T = TypeVar("_T")
|
||||
|
||||
@@ -288,7 +297,7 @@ class SerializableBase(abc.ABC):
|
||||
@classmethod
|
||||
def default_value(cls) -> Any:
|
||||
# None may be a valid default, so return MISSING as a sentinel val
|
||||
return dataclasses.MISSING
|
||||
return MISSING
|
||||
|
||||
|
||||
class Adapter(SerializableBase, abc.ABC):
|
||||
@@ -328,18 +337,18 @@ class ForwardSerializable(SerializableBase):
|
||||
def __init__(self, func: Callable[[], SERIALIZABLE_TYPE]):
|
||||
super().__init__()
|
||||
self._func = func
|
||||
self._wrapped = dataclasses.MISSING
|
||||
self._wrapped: Union[MissingType, SERIALIZABLE_TYPE] = MISSING
|
||||
|
||||
def _ensure_evaled(self):
|
||||
if self._wrapped is dataclasses.MISSING:
|
||||
if self._wrapped is MISSING:
|
||||
self._wrapped = self._func()
|
||||
|
||||
def __getattr__(self, attr):
|
||||
return getattr(self._wrapped, attr)
|
||||
|
||||
def default_value(self) -> Any:
|
||||
if self._wrapped is dataclasses.MISSING:
|
||||
return dataclasses.MISSING
|
||||
if self._wrapped is MISSING:
|
||||
return MISSING
|
||||
return self._wrapped.default_value()
|
||||
|
||||
def serialize(self, val, writer: BufferWriter, ctx: Optional[ParseContext]):
|
||||
@@ -357,10 +366,10 @@ class Template(SerializableBase):
|
||||
def __init__(self, template_spec: Dict[str, SERIALIZABLE_TYPE], skip_missing=False):
|
||||
self._template_spec = template_spec
|
||||
self._skip_missing = skip_missing
|
||||
self._size = dataclasses.MISSING
|
||||
self._size = MISSING
|
||||
|
||||
def calc_size(self):
|
||||
if self._size is not dataclasses.MISSING:
|
||||
if self._size is not MISSING:
|
||||
return self._size
|
||||
sum_bytes = 0
|
||||
for _, field_type in self._template_spec.items():
|
||||
@@ -830,7 +839,7 @@ class QuantizedFloat(QuantizedFloatBase):
|
||||
super().__init__(prim_spec, zero_median=False)
|
||||
self.lower = lower
|
||||
self.upper = upper
|
||||
# We know the range in `QuantizedFloat` when it's constructed, so we can infer
|
||||
# We know the range in `QuantizedFloat` when it's constructed, so we can infer
|
||||
# whether or not we should round towards zero in __init__
|
||||
max_error = (upper - lower) * self.step_mag
|
||||
midpoint = (upper + lower) / 2.0
|
||||
@@ -1196,9 +1205,9 @@ class ContextMixin(Generic[_T]):
|
||||
def _choose_option(self, ctx: Optional[ParseContext]) -> _T:
|
||||
idx = self._fun(ctx)
|
||||
if idx not in self._options:
|
||||
if dataclasses.MISSING not in self._options:
|
||||
if MISSING not in self._options:
|
||||
raise KeyError(f"{idx!r} not found in {self._options!r}")
|
||||
idx = dataclasses.MISSING
|
||||
idx = MISSING
|
||||
return self._options[idx]
|
||||
|
||||
|
||||
@@ -1339,6 +1348,12 @@ class TypedBytesBase(SerializableBase, abc.ABC):
|
||||
return self._spec.default_value()
|
||||
|
||||
|
||||
class TypedBytesGreedy(TypedBytesBase):
|
||||
def __init__(self, spec, empty_is_none=False, check_trailing_bytes=True, lazy=False):
|
||||
self._bytes_tmpl = BytesGreedy()
|
||||
super().__init__(spec, empty_is_none, check_trailing_bytes, lazy=lazy)
|
||||
|
||||
|
||||
class TypedByteArray(TypedBytesBase):
|
||||
def __init__(self, len_spec, spec, empty_is_none=False, check_trailing_bytes=True, lazy=False):
|
||||
self._bytes_tmpl = ByteArray(len_spec)
|
||||
@@ -1436,7 +1451,7 @@ class StringEnumAdapter(Adapter):
|
||||
class FixedPoint(SerializableBase):
|
||||
def __init__(self, ser_spec, int_bits, frac_bits, signed=False):
|
||||
# Should never be used due to how this handles signs :/
|
||||
assert(not ser_spec.is_signed)
|
||||
assert (not ser_spec.is_signed)
|
||||
|
||||
self._ser_spec: SerializablePrimitive = ser_spec
|
||||
self._signed = signed
|
||||
@@ -1446,7 +1461,7 @@ class FixedPoint(SerializableBase):
|
||||
self._min_val = ((1 << int_bits) * -1) if signed else 0
|
||||
self._max_val = 1 << int_bits
|
||||
|
||||
assert(required_bits == (ser_spec.calc_size() * 8))
|
||||
assert (required_bits == (ser_spec.calc_size() * 8))
|
||||
|
||||
def deserialize(self, reader: Reader, ctx):
|
||||
fixed_val = float(self._ser_spec.deserialize(reader, ctx))
|
||||
@@ -1476,8 +1491,8 @@ def _make_undefined_raiser():
|
||||
return f
|
||||
|
||||
|
||||
def dataclass_field(spec: Union[SERIALIZABLE_TYPE, Callable], *, default=dataclasses.MISSING,
|
||||
default_factory=dataclasses.MISSING, init=True, repr=True, # noqa
|
||||
def dataclass_field(spec: Union[SERIALIZABLE_TYPE, Callable], *, default: Any = dataclasses.MISSING,
|
||||
default_factory: Any = dataclasses.MISSING, init=True, repr=True, # noqa
|
||||
hash=None, compare=True) -> dataclasses.Field: # noqa
|
||||
enrich_factory = False
|
||||
# Lambda, need to defer evaluation of spec until it's actually used.
|
||||
@@ -1498,7 +1513,7 @@ def dataclass_field(spec: Union[SERIALIZABLE_TYPE, Callable], *, default=datacla
|
||||
metadata={"spec": spec}, default=default, default_factory=default_factory, init=init,
|
||||
repr=repr, hash=hash, compare=compare
|
||||
)
|
||||
# Need to stuff this on so it knows which field went unspecified.
|
||||
# Need to stuff this on, so it knows which field went unspecified.
|
||||
if enrich_factory:
|
||||
default_factory.field = field
|
||||
return field
|
||||
@@ -1565,8 +1580,16 @@ def bitfield_field(bits: int, *, adapter: Optional[Adapter] = None, default=0, i
|
||||
|
||||
|
||||
class BitfieldDataclass(DataclassAdapter):
|
||||
def __init__(self, data_cls: Type,
|
||||
prim_spec: Optional[SerializablePrimitive] = None, shift: bool = True):
|
||||
PRIM_SPEC: ClassVar[Optional[SerializablePrimitive]] = None
|
||||
|
||||
def __init__(self, data_cls: Optional[Type] = None,
|
||||
prim_spec: Optional[SerializablePrimitive] = None, shift: Optional[bool] = None):
|
||||
if not dataclasses.is_dataclass(data_cls):
|
||||
raise ValueError(f"{data_cls!r} is not a dataclass")
|
||||
if prim_spec is None:
|
||||
prim_spec = getattr(data_cls, 'PRIM_SPEC', None)
|
||||
if shift is None:
|
||||
shift = getattr(data_cls, 'SHIFT', True)
|
||||
super().__init__(data_cls, prim_spec)
|
||||
self._shift = shift
|
||||
self._bitfield_spec = self._build_bitfield(data_cls)
|
||||
@@ -1596,7 +1619,9 @@ class BitfieldDataclass(DataclassAdapter):
|
||||
|
||||
|
||||
class ExprAdapter(Adapter):
|
||||
def __init__(self, child_spec: SERIALIZABLE_TYPE, decode_func: Callable, encode_func: Callable):
|
||||
_ID = lambda x: x
|
||||
|
||||
def __init__(self, child_spec: SERIALIZABLE_TYPE, decode_func: Callable = _ID, encode_func: Callable = _ID):
|
||||
super().__init__(child_spec)
|
||||
self._decode_func = decode_func
|
||||
self._encode_func = encode_func
|
||||
@@ -1645,9 +1670,64 @@ class BinaryLLSD(SerializableBase):
|
||||
writer.write_bytes(llsd.format_binary(val, with_header=False))
|
||||
|
||||
|
||||
class NumPyArray(Adapter):
|
||||
"""
|
||||
An 2-dimensional, dynamic-length array of data from numpy. Greedy.
|
||||
|
||||
Unlike most other serializers, your endianness _must_ be specified in the dtype!
|
||||
"""
|
||||
__slots__ = ['dtype', 'elems']
|
||||
|
||||
def __init__(self, child_spec: Optional[SERIALIZABLE_TYPE], dtype: np.dtype, elems: int):
|
||||
super().__init__(child_spec)
|
||||
self.dtype = dtype
|
||||
self.elems = elems
|
||||
|
||||
def _pick_dtype(self, endian: str) -> np.dtype:
|
||||
return self.dtype.newbyteorder('>') if endian != "<" else self.dtype
|
||||
|
||||
def decode(self, val: Any, ctx: Optional[ParseContext], pod: bool = False) -> Any:
|
||||
num_elems = len(val) // self.dtype.itemsize
|
||||
num_ndims = num_elems // self.elems
|
||||
buf_array = np.frombuffer(val, dtype=self.dtype, count=num_elems)
|
||||
return buf_array.reshape((num_ndims, self.elems))
|
||||
|
||||
def encode(self, val, ctx: Optional[ParseContext]) -> Any:
|
||||
val: np.ndarray = np.array(val, dtype=self.dtype).flatten()
|
||||
return val.tobytes()
|
||||
|
||||
|
||||
class QuantizedNumPyArray(Adapter):
|
||||
"""Like QuantizedFloat. Only works correctly for unsigned types, no zero midpoint rounding!"""
|
||||
def __init__(self, child_spec: NumPyArray, lower: float, upper: float):
|
||||
super().__init__(child_spec)
|
||||
self.dtype = child_spec.dtype
|
||||
self.lower = lower
|
||||
self.upper = upper
|
||||
self.step_mag = 1.0 / ((2 ** (self.dtype.itemsize * 8)) - 1)
|
||||
|
||||
def encode(self, val: Any, ctx: Optional[ParseContext]) -> Any:
|
||||
val = np.array(val, dtype=np.float64)
|
||||
val = np.clip(val, self.lower, self.upper)
|
||||
delta = self.upper - self.lower
|
||||
if delta == 0.0:
|
||||
return np.zeros(val.shape, dtype=self.dtype)
|
||||
|
||||
val -= self.lower
|
||||
val /= delta
|
||||
val /= self.step_mag
|
||||
return np.rint(val).astype(self.dtype)
|
||||
|
||||
def decode(self, val: Any, ctx: Optional[ParseContext], pod: bool = False) -> Any:
|
||||
val = val.astype(np.float64)
|
||||
val *= self.step_mag
|
||||
val *= self.upper - self.lower
|
||||
val += self.lower
|
||||
return val
|
||||
|
||||
|
||||
def subfield_serializer(msg_name, block_name, var_name):
|
||||
def f(orig_cls):
|
||||
global SUBFIELD_SERIALIZERS
|
||||
SUBFIELD_SERIALIZERS[(msg_name, block_name, var_name)] = orig_cls
|
||||
return orig_cls
|
||||
return f
|
||||
@@ -1844,7 +1924,7 @@ class IntEnumSubfieldSerializer(AdapterInstanceSubfieldSerializer):
|
||||
val = super().deserialize(ctx_obj, val, pod=pod)
|
||||
# Don't pretend we were able to deserialize this if we
|
||||
# had to fall through to the `int` case.
|
||||
if pod and type(val) == int:
|
||||
if pod and type(val) is int:
|
||||
return UNSERIALIZABLE
|
||||
return val
|
||||
|
||||
@@ -1859,7 +1939,6 @@ class IntFlagSubfieldSerializer(AdapterInstanceSubfieldSerializer):
|
||||
|
||||
def http_serializer(msg_name):
|
||||
def f(orig_cls):
|
||||
global HTTP_SERIALIZERS
|
||||
HTTP_SERIALIZERS[msg_name] = orig_cls
|
||||
return orig_cls
|
||||
return f
|
||||
|
||||
@@ -55,6 +55,7 @@ class SettingDescriptor(Generic[_T]):
|
||||
|
||||
class Settings:
|
||||
ENABLE_DEFERRED_PACKET_PARSING: bool = SettingDescriptor(True)
|
||||
ALLOW_UNKNOWN_MESSAGES: bool = SettingDescriptor(True)
|
||||
|
||||
def __init__(self):
|
||||
self._settings: Dict[str, Any] = {}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
45
hippolyzer/lib/base/test_utils.py
Normal file
45
hippolyzer/lib/base/test_utils.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import asyncio
|
||||
from typing import Any, Optional, List, Tuple
|
||||
|
||||
from hippolyzer.lib.base.message.circuit import Circuit, ConnectionHolder
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.message.message_handler import MessageHandler
|
||||
from hippolyzer.lib.base.network.transport import AbstractUDPTransport, ADDR_TUPLE, UDPPacket
|
||||
|
||||
|
||||
class MockTransport(AbstractUDPTransport):
|
||||
def sendto(self, data: Any, addr: Optional[ADDR_TUPLE] = ...) -> None:
|
||||
pass
|
||||
|
||||
def abort(self) -> None:
|
||||
pass
|
||||
|
||||
def close(self) -> None:
|
||||
pass
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.packets: List[Tuple[bytes, Tuple[str, int]]] = []
|
||||
|
||||
def send_packet(self, packet: UDPPacket) -> None:
|
||||
self.packets.append((packet.data, packet.dst_addr))
|
||||
|
||||
|
||||
class MockHandlingCircuit(Circuit):
|
||||
def __init__(self, handler: MessageHandler[Message, str]):
|
||||
super().__init__(("127.0.0.1", 1), ("127.0.0.1", 2), None)
|
||||
self.handler = handler
|
||||
|
||||
def _send_prepared_message(self, message: Message, transport=None):
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
loop.call_soon(self.handler.handle, message)
|
||||
|
||||
|
||||
class MockConnectionHolder(ConnectionHolder):
|
||||
def __init__(self, circuit, message_handler):
|
||||
self.circuit = circuit
|
||||
self.message_handler = message_handler
|
||||
|
||||
|
||||
async def soon(awaitable) -> Message:
|
||||
return await asyncio.wait_for(awaitable, timeout=1.0)
|
||||
@@ -8,8 +8,10 @@ import dataclasses
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.helpers import create_logged_task
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.base.message.circuit import ConnectionHolder
|
||||
from hippolyzer.lib.base.message.msgtypes import PacketFlags
|
||||
from hippolyzer.lib.base.templates import (
|
||||
TransferRequestParamsBase,
|
||||
TransferChannelType,
|
||||
@@ -94,7 +96,7 @@ class TransferManager:
|
||||
if params_dict.get("SessionID", dataclasses.MISSING) is None:
|
||||
params.SessionID = self._session_id
|
||||
|
||||
self._connection_holder.circuit.send_message(Message(
|
||||
self._connection_holder.circuit.send(Message(
|
||||
'TransferRequest',
|
||||
Block(
|
||||
'TransferInfo',
|
||||
@@ -104,9 +106,10 @@ class TransferManager:
|
||||
Priority=priority,
|
||||
Params_=params,
|
||||
),
|
||||
flags=PacketFlags.RELIABLE,
|
||||
))
|
||||
transfer = Transfer(transfer_id)
|
||||
asyncio.create_task(self._pump_transfer_replies(transfer))
|
||||
create_logged_task(self._pump_transfer_replies(transfer), "Transfer Pump")
|
||||
return transfer
|
||||
|
||||
async def _pump_transfer_replies(self, transfer: Transfer):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from PySide2.QtCore import QMetaObject
|
||||
from PySide2.QtUiTools import QUiLoader
|
||||
from PySide6.QtCore import QMetaObject
|
||||
from PySide6.QtUiTools import QUiLoader
|
||||
|
||||
|
||||
class UiLoader(QUiLoader):
|
||||
|
||||
@@ -5,6 +5,7 @@ Body parts and linden clothing layers
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import enum
|
||||
import logging
|
||||
from io import StringIO
|
||||
from typing import *
|
||||
@@ -13,7 +14,7 @@ from xml.etree.ElementTree import parse as parse_etree
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.helpers import get_resource_filename
|
||||
from hippolyzer.lib.base.legacy_inv import InventorySaleInfo, InventoryPermissions
|
||||
from hippolyzer.lib.base.inventory import InventorySaleInfo, InventoryPermissions
|
||||
from hippolyzer.lib.base.legacy_schema import SchemaBase, parse_schema_line, SchemaParsingError
|
||||
from hippolyzer.lib.base.templates import WearableType
|
||||
|
||||
@@ -21,6 +22,60 @@ LOG = logging.getLogger(__name__)
|
||||
_T = TypeVar("_T")
|
||||
|
||||
WEARABLE_VERSION = "LLWearable version 22"
|
||||
DEFAULT_WEARABLE_TEX = UUID("c228d1cf-4b5d-4ba8-84f4-899a0796aa97")
|
||||
|
||||
|
||||
class AvatarTEIndex(enum.IntEnum):
|
||||
"""From llavatarappearancedefines.h"""
|
||||
HEAD_BODYPAINT = 0
|
||||
UPPER_SHIRT = enum.auto()
|
||||
LOWER_PANTS = enum.auto()
|
||||
EYES_IRIS = enum.auto()
|
||||
HAIR = enum.auto()
|
||||
UPPER_BODYPAINT = enum.auto()
|
||||
LOWER_BODYPAINT = enum.auto()
|
||||
LOWER_SHOES = enum.auto()
|
||||
HEAD_BAKED = enum.auto()
|
||||
UPPER_BAKED = enum.auto()
|
||||
LOWER_BAKED = enum.auto()
|
||||
EYES_BAKED = enum.auto()
|
||||
LOWER_SOCKS = enum.auto()
|
||||
UPPER_JACKET = enum.auto()
|
||||
LOWER_JACKET = enum.auto()
|
||||
UPPER_GLOVES = enum.auto()
|
||||
UPPER_UNDERSHIRT = enum.auto()
|
||||
LOWER_UNDERPANTS = enum.auto()
|
||||
SKIRT = enum.auto()
|
||||
SKIRT_BAKED = enum.auto()
|
||||
HAIR_BAKED = enum.auto()
|
||||
LOWER_ALPHA = enum.auto()
|
||||
UPPER_ALPHA = enum.auto()
|
||||
HEAD_ALPHA = enum.auto()
|
||||
EYES_ALPHA = enum.auto()
|
||||
HAIR_ALPHA = enum.auto()
|
||||
HEAD_TATTOO = enum.auto()
|
||||
UPPER_TATTOO = enum.auto()
|
||||
LOWER_TATTOO = enum.auto()
|
||||
HEAD_UNIVERSAL_TATTOO = enum.auto()
|
||||
UPPER_UNIVERSAL_TATTOO = enum.auto()
|
||||
LOWER_UNIVERSAL_TATTOO = enum.auto()
|
||||
SKIRT_TATTOO = enum.auto()
|
||||
HAIR_TATTOO = enum.auto()
|
||||
EYES_TATTOO = enum.auto()
|
||||
LEFT_ARM_TATTOO = enum.auto()
|
||||
LEFT_LEG_TATTOO = enum.auto()
|
||||
AUX1_TATTOO = enum.auto()
|
||||
AUX2_TATTOO = enum.auto()
|
||||
AUX3_TATTOO = enum.auto()
|
||||
LEFTARM_BAKED = enum.auto()
|
||||
LEFTLEG_BAKED = enum.auto()
|
||||
AUX1_BAKED = enum.auto()
|
||||
AUX2_BAKED = enum.auto()
|
||||
AUX3_BAKED = enum.auto()
|
||||
|
||||
@property
|
||||
def is_baked(self) -> bool:
|
||||
return self.name.endswith("_BAKED")
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
@@ -35,9 +90,8 @@ class VisualParam:
|
||||
|
||||
|
||||
class VisualParams(List[VisualParam]):
|
||||
def __init__(self):
|
||||
def __init__(self, lad_path):
|
||||
super().__init__()
|
||||
lad_path = get_resource_filename("lib/base/data/avatar_lad.xml")
|
||||
with open(lad_path, "rb") as f:
|
||||
doc = parse_etree(f)
|
||||
for param in doc.findall(".//param"):
|
||||
@@ -59,8 +113,11 @@ class VisualParams(List[VisualParam]):
|
||||
def by_wearable(self, wearable: str) -> List[VisualParam]:
|
||||
return [x for x in self if x.wearable == wearable]
|
||||
|
||||
def by_id(self, vparam_id: int) -> VisualParam:
|
||||
return [x for x in self if x.id == vparam_id][0]
|
||||
|
||||
VISUAL_PARAMS = VisualParams()
|
||||
|
||||
VISUAL_PARAMS = VisualParams(get_resource_filename("lib/base/data/avatar_lad.xml"))
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
|
||||
@@ -9,9 +9,10 @@ import random
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID, RawBytes
|
||||
from hippolyzer.lib.base.helpers import create_logged_task
|
||||
from hippolyzer.lib.base.message.data_packer import TemplateDataPacker
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.base.message.msgtypes import MsgType
|
||||
from hippolyzer.lib.base.message.msgtypes import MsgType, PacketFlags
|
||||
from hippolyzer.lib.base.network.transport import Direction
|
||||
from hippolyzer.lib.base.message.circuit import ConnectionHolder
|
||||
from hippolyzer.lib.base.templates import XferPacket, XferFilePath, AssetType, XferError
|
||||
@@ -110,7 +111,7 @@ class XferManager:
|
||||
direction: Direction = Direction.OUT,
|
||||
) -> Xfer:
|
||||
xfer_id = xfer_id if xfer_id is not None else random.getrandbits(64)
|
||||
self._connection_holder.circuit.send_message(Message(
|
||||
self._connection_holder.circuit.send(Message(
|
||||
'RequestXfer',
|
||||
Block(
|
||||
'XferID',
|
||||
@@ -125,7 +126,7 @@ class XferManager:
|
||||
direction=direction,
|
||||
))
|
||||
xfer = Xfer(xfer_id, direction=direction, turbo=turbo)
|
||||
asyncio.create_task(self._pump_xfer_replies(xfer))
|
||||
create_logged_task(self._pump_xfer_replies(xfer), "Xfer Pump")
|
||||
return xfer
|
||||
|
||||
async def _pump_xfer_replies(self, xfer: Xfer):
|
||||
@@ -174,10 +175,11 @@ class XferManager:
|
||||
to_ack = range(xfer.next_ackable, ack_max)
|
||||
xfer.next_ackable = ack_max
|
||||
for ack_id in to_ack:
|
||||
self._connection_holder.circuit.send_message(Message(
|
||||
self._connection_holder.circuit.send_reliable(Message(
|
||||
"ConfirmXferPacket",
|
||||
Block("XferID", ID=xfer.xfer_id, Packet=ack_id),
|
||||
direction=xfer.direction,
|
||||
flags=PacketFlags.RELIABLE,
|
||||
))
|
||||
|
||||
xfer.chunks[packet_id.PacketID] = packet_data
|
||||
@@ -216,7 +218,7 @@ class XferManager:
|
||||
else:
|
||||
inline_data = data
|
||||
|
||||
self._connection_holder.circuit.send_message(Message(
|
||||
self._connection_holder.circuit.send(Message(
|
||||
"AssetUploadRequest",
|
||||
Block(
|
||||
"AssetBlock",
|
||||
@@ -225,7 +227,8 @@ class XferManager:
|
||||
Tempfile=temp_file,
|
||||
StoreLocal=store_local,
|
||||
AssetData=inline_data,
|
||||
)
|
||||
),
|
||||
flags=PacketFlags.RELIABLE
|
||||
))
|
||||
fut = asyncio.Future()
|
||||
asyncio.create_task(self._pump_asset_upload(xfer, transaction_id, fut))
|
||||
@@ -267,17 +270,19 @@ class XferManager:
|
||||
xfer.xfer_id = request_msg["XferID"]["ID"]
|
||||
|
||||
packet_id = 0
|
||||
# TODO: No resend yet. If it's lost, it's lost.
|
||||
while xfer.chunks:
|
||||
chunk = xfer.chunks.pop(packet_id)
|
||||
# EOF if there are no chunks left
|
||||
packet_val = XferPacket(PacketID=packet_id, IsEOF=not bool(xfer.chunks))
|
||||
self._connection_holder.circuit.send_message(Message(
|
||||
# We just send reliably since I don't care to implement the Xfer-specific
|
||||
# resend-on-unacked nastiness
|
||||
_ = self._connection_holder.circuit.send_reliable(Message(
|
||||
"SendXferPacket",
|
||||
Block("XferID", ID=xfer.xfer_id, Packet_=packet_val),
|
||||
Block("DataPacket", Data=chunk),
|
||||
# Send this towards the sender of the RequestXfer
|
||||
direction=~request_msg.direction,
|
||||
flags=PacketFlags.RELIABLE,
|
||||
))
|
||||
# Don't care about the value, just want to know it was confirmed.
|
||||
if wait_for_confirm:
|
||||
|
||||
127
hippolyzer/lib/client/asset_uploader.py
Normal file
127
hippolyzer/lib/client/asset_uploader.py
Normal file
@@ -0,0 +1,127 @@
|
||||
from typing import NamedTuple, Union, Optional, List
|
||||
|
||||
import hippolyzer.lib.base.serialization as se
|
||||
from hippolyzer.lib.base import llsd
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.mesh import MeshAsset, LLMeshSerializer
|
||||
from hippolyzer.lib.base.templates import AssetType
|
||||
from hippolyzer.lib.client.state import BaseClientRegion
|
||||
|
||||
|
||||
class UploadError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UploadToken(NamedTuple):
|
||||
linden_cost: int
|
||||
uploader_url: str
|
||||
payload: bytes
|
||||
|
||||
|
||||
class MeshUploadDetails(NamedTuple):
|
||||
mesh_bytes: bytes
|
||||
num_faces: int
|
||||
|
||||
|
||||
class AssetUploader:
|
||||
def __init__(self, region: BaseClientRegion):
|
||||
self._region = region
|
||||
|
||||
async def initiate_asset_upload(self, name: str, asset_type: AssetType,
|
||||
body: bytes, flags: Optional[int] = None) -> UploadToken:
|
||||
payload = {
|
||||
"asset_type": asset_type.to_lookup_name(),
|
||||
"description": "(No Description)",
|
||||
"everyone_mask": 0,
|
||||
"group_mask": 0,
|
||||
"folder_id": UUID.ZERO, # Puts it in the default folder, I guess. Undocumented.
|
||||
"inventory_type": asset_type.inventory_type.to_lookup_name(),
|
||||
"name": name,
|
||||
"next_owner_mask": 581632,
|
||||
}
|
||||
if flags is not None:
|
||||
payload['flags'] = flags
|
||||
resp_payload = await self._make_newfileagentinventory_req(payload)
|
||||
|
||||
return UploadToken(resp_payload["upload_price"], resp_payload["uploader"], body)
|
||||
|
||||
async def _make_newfileagentinventory_req(self, payload: dict):
|
||||
async with self._region.caps_client.post("NewFileAgentInventory", llsd=payload) as resp:
|
||||
resp.raise_for_status()
|
||||
resp_payload = await resp.read_llsd()
|
||||
# Need to sniff the resp payload for this because SL sends a 200 status code on error
|
||||
if "error" in resp_payload:
|
||||
raise UploadError(resp_payload)
|
||||
return resp_payload
|
||||
|
||||
async def complete_upload(self, token: UploadToken) -> dict:
|
||||
async with self._region.caps_client.post(token.uploader_url, data=token.payload) as resp:
|
||||
resp.raise_for_status()
|
||||
resp_payload = await resp.read_llsd()
|
||||
# The actual upload endpoints return 200 on error, have to sniff the payload to figure
|
||||
# out if it actually failed...
|
||||
if "error" in resp_payload:
|
||||
raise UploadError(resp_payload)
|
||||
await self._handle_upload_complete(resp_payload)
|
||||
return resp_payload
|
||||
|
||||
async def _handle_upload_complete(self, resp_payload: dict):
|
||||
"""
|
||||
Generic hook called when any asset upload completes.
|
||||
|
||||
Could trigger an AIS fetch to send the viewer details about the item we just created,
|
||||
assuming we were in proxy context.
|
||||
"""
|
||||
pass
|
||||
|
||||
# The mesh upload flow is a little special, so it gets its own method
|
||||
async def initiate_mesh_upload(self, name: str, mesh: Union[MeshUploadDetails, MeshAsset],
|
||||
flags: Optional[int] = None) -> UploadToken:
|
||||
if isinstance(mesh, MeshAsset):
|
||||
writer = se.BufferWriter("!")
|
||||
writer.write(LLMeshSerializer(), mesh)
|
||||
mesh = MeshUploadDetails(writer.copy_buffer(), len(mesh.segments['high_lod']))
|
||||
|
||||
asset_resources = self._build_asset_resources(name, [mesh])
|
||||
payload = {
|
||||
'asset_resources': asset_resources,
|
||||
'asset_type': 'mesh',
|
||||
'description': '(No Description)',
|
||||
'everyone_mask': 0,
|
||||
'folder_id': UUID.ZERO,
|
||||
'group_mask': 0,
|
||||
'inventory_type': 'object',
|
||||
'name': name,
|
||||
'next_owner_mask': 581632,
|
||||
'texture_folder_id': UUID.ZERO
|
||||
}
|
||||
if flags is not None:
|
||||
payload['flags'] = flags
|
||||
resp_payload = await self._make_newfileagentinventory_req(payload)
|
||||
|
||||
upload_body = llsd.format_xml(asset_resources)
|
||||
return UploadToken(resp_payload["upload_price"], resp_payload["uploader"], upload_body)
|
||||
|
||||
def _build_asset_resources(self, name: str, meshes: List[MeshUploadDetails]) -> dict:
|
||||
instances = []
|
||||
for mesh in meshes:
|
||||
instances.append({
|
||||
'face_list': [{
|
||||
'diffuse_color': [1.0, 1.0, 1.0, 1.0],
|
||||
'fullbright': False
|
||||
}] * mesh.num_faces,
|
||||
'material': 3,
|
||||
'mesh': 0,
|
||||
'mesh_name': name,
|
||||
'physics_shape_type': 2,
|
||||
'position': [0.0, 0.0, 0.0],
|
||||
'rotation': [0.7071067690849304, 0.0, 0.0, 0.7071067690849304],
|
||||
'scale': [1.0, 1.0, 1.0]
|
||||
})
|
||||
|
||||
return {
|
||||
'instance_list': instances,
|
||||
'mesh_list': [mesh.mesh_bytes for mesh in meshes],
|
||||
'metric': 'MUT_Unspecified',
|
||||
'texture_list': []
|
||||
}
|
||||
778
hippolyzer/lib/client/hippo_client.py
Normal file
778
hippolyzer/lib/client/hippo_client.py
Normal file
@@ -0,0 +1,778 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
from importlib.metadata import version
|
||||
import logging
|
||||
import uuid
|
||||
import weakref
|
||||
import xmlrpc.client
|
||||
from typing import *
|
||||
|
||||
import aiohttp
|
||||
import multidict
|
||||
|
||||
from hippolyzer.lib.base.datatypes import Vector3, StringEnum
|
||||
from hippolyzer.lib.base.helpers import proxify, get_resource_filename, create_logged_task
|
||||
from hippolyzer.lib.base.message.circuit import Circuit
|
||||
from hippolyzer.lib.base.message.llsd_msg_serializer import LLSDMessageSerializer
|
||||
from hippolyzer.lib.base.message.message import Message, Block
|
||||
from hippolyzer.lib.base.message.message_dot_xml import MessageDotXML
|
||||
from hippolyzer.lib.base.message.message_handler import MessageHandler
|
||||
from hippolyzer.lib.base.message.udpdeserializer import UDPMessageDeserializer
|
||||
from hippolyzer.lib.base.network.caps_client import CapsClient, CAPS_DICT
|
||||
from hippolyzer.lib.base.network.transport import ADDR_TUPLE, Direction, SocketUDPTransport, AbstractUDPTransport
|
||||
from hippolyzer.lib.base.settings import Settings, SettingDescriptor
|
||||
from hippolyzer.lib.base.templates import RegionHandshakeReplyFlags, ChatType, ThrottleData
|
||||
from hippolyzer.lib.base.transfer_manager import TransferManager
|
||||
from hippolyzer.lib.base.xfer_manager import XferManager
|
||||
from hippolyzer.lib.client.asset_uploader import AssetUploader
|
||||
from hippolyzer.lib.client.inventory_manager import InventoryManager
|
||||
from hippolyzer.lib.client.object_manager import ClientObjectManager, ClientWorldObjectManager
|
||||
from hippolyzer.lib.client.parcel_manager import ParcelManager
|
||||
from hippolyzer.lib.client.state import BaseClientSession, BaseClientRegion, BaseClientSessionManager
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class StartLocation(StringEnum):
|
||||
LAST = "last"
|
||||
HOME = "home"
|
||||
|
||||
|
||||
class ClientSettings(Settings):
|
||||
SSL_VERIFY: bool = SettingDescriptor(False)
|
||||
"""Off by default for now, the cert validation is a big mess due to LL using an internal CA."""
|
||||
SSL_CERT_PATH: str = SettingDescriptor(get_resource_filename("lib/base/network/data/ca-bundle.crt"))
|
||||
USER_AGENT: str = SettingDescriptor(f"Hippolyzer/v{version('hippolyzer')}")
|
||||
SEND_AGENT_UPDATES: bool = SettingDescriptor(True)
|
||||
"""Generally you want to send these, lots of things will break if you don't send at least one."""
|
||||
AUTO_REQUEST_PARCELS: bool = SettingDescriptor(True)
|
||||
"""Automatically request all parcel details when connecting to a region"""
|
||||
AUTO_REQUEST_MATERIALS: bool = SettingDescriptor(True)
|
||||
"""Automatically request all materials when connecting to a region"""
|
||||
|
||||
|
||||
class HippoCapsClient(CapsClient):
|
||||
def __init__(
|
||||
self,
|
||||
settings: ClientSettings,
|
||||
caps: Optional[CAPS_DICT] = None,
|
||||
session: Optional[aiohttp.ClientSession] = None,
|
||||
) -> None:
|
||||
super().__init__(caps, session)
|
||||
self._settings = settings
|
||||
|
||||
def _request_fixups(self, cap_or_url: str, headers: Dict, proxy: Optional[bool], ssl: Any):
|
||||
headers["User-Agent"] = self._settings.USER_AGENT
|
||||
return cap_or_url, headers, proxy, self._settings.SSL_VERIFY
|
||||
|
||||
|
||||
class HippoClientProtocol(asyncio.DatagramProtocol):
|
||||
def __init__(self, session: HippoClientSession):
|
||||
self.session = proxify(session)
|
||||
self.message_xml = MessageDotXML()
|
||||
self.deserializer = UDPMessageDeserializer(
|
||||
settings=self.session.session_manager.settings,
|
||||
)
|
||||
|
||||
def datagram_received(self, data, source_addr: ADDR_TUPLE):
|
||||
region = self.session.region_by_circuit_addr(source_addr)
|
||||
if not region:
|
||||
logging.warning("Received packet from invalid address %s", source_addr)
|
||||
return
|
||||
|
||||
message = self.deserializer.deserialize(data)
|
||||
message.direction = Direction.IN
|
||||
message.sender = source_addr
|
||||
|
||||
if not self.message_xml.validate_udp_msg(message.name):
|
||||
LOG.warning(
|
||||
f"Received {message.name!r} over UDP, when it should come over the event queue. Discarding."
|
||||
)
|
||||
raise PermissionError(f"UDPBanned message {message.name}")
|
||||
|
||||
region.circuit.collect_acks(message)
|
||||
|
||||
should_handle = True
|
||||
if message.reliable:
|
||||
# This is a bit crap. We send an ACK immediately through a PacketAck.
|
||||
# This is pretty wasteful, we should batch them up and send them on a timer.
|
||||
# We should ACK even if it's a resend of something we've already handled, maybe
|
||||
# they never got the ACK.
|
||||
region.circuit.send_acks((message.packet_id,))
|
||||
should_handle = region.circuit.track_reliable(message.packet_id)
|
||||
|
||||
try:
|
||||
if should_handle:
|
||||
self.session.message_handler.handle(message)
|
||||
except:
|
||||
LOG.exception("Failed in session message handler")
|
||||
if should_handle:
|
||||
region.message_handler.handle(message)
|
||||
|
||||
|
||||
class HippoClientRegion(BaseClientRegion):
|
||||
def __init__(self, circuit_addr, seed_cap: Optional[str], session: HippoClientSession, handle=None):
|
||||
super().__init__()
|
||||
self.caps = multidict.MultiDict()
|
||||
self.message_handler: MessageHandler[Message, str] = MessageHandler(take_by_default=False)
|
||||
self.circuit_addr = circuit_addr
|
||||
self.handle = handle
|
||||
if seed_cap:
|
||||
self.caps["Seed"] = seed_cap
|
||||
self.session: Callable[[], HippoClientSession] = weakref.ref(session)
|
||||
self.caps_client = HippoCapsClient(session.session_manager.settings, self.caps, session.http_session)
|
||||
self.xfer_manager = XferManager(proxify(self), self.session().secure_session_id)
|
||||
self.transfer_manager = TransferManager(proxify(self), session.agent_id, session.id)
|
||||
self.asset_uploader = AssetUploader(proxify(self))
|
||||
self.parcel_manager = ParcelManager(proxify(self))
|
||||
self.objects = ClientObjectManager(self)
|
||||
self._llsd_serializer = LLSDMessageSerializer()
|
||||
self._eq_task: Optional[asyncio.Task] = None
|
||||
self.connected: asyncio.Future = asyncio.Future()
|
||||
|
||||
self.message_handler.subscribe("StartPingCheck", self._handle_ping_check)
|
||||
|
||||
def update_caps(self, caps: Mapping[str, str]) -> None:
|
||||
self.caps.update(caps)
|
||||
|
||||
@property
|
||||
def cap_urls(self) -> multidict.MultiDict:
|
||||
return self.caps.copy()
|
||||
|
||||
async def connect(self, main_region: bool = False):
|
||||
# Disconnect first if we're already connected
|
||||
if self.circuit and self.circuit.is_alive:
|
||||
self.disconnect()
|
||||
if self.connected.done():
|
||||
self.connected = asyncio.Future()
|
||||
|
||||
try:
|
||||
# TODO: What happens if a circuit code is invalid, again? Does it just refuse to ACK?
|
||||
await self.circuit.send_reliable(
|
||||
Message(
|
||||
"UseCircuitCode",
|
||||
Block(
|
||||
"CircuitCode",
|
||||
Code=self.session().circuit_code,
|
||||
SessionID=self.session().id,
|
||||
ID=self.session().agent_id,
|
||||
),
|
||||
)
|
||||
)
|
||||
self.circuit.is_alive = True
|
||||
|
||||
# Clear out any old caps urls except the seed URL, we're about to fetch new caps.
|
||||
seed_url = self.caps["Seed"]
|
||||
self.caps.clear()
|
||||
self.caps["Seed"] = seed_url
|
||||
|
||||
# Kick this off and await it later
|
||||
seed_resp_fut = self.caps_client.post("Seed", llsd=list(self.session().session_manager.SUPPORTED_CAPS))
|
||||
|
||||
# Register first so we can handle it even if the ack happens after the message is sent
|
||||
region_handshake_fut = self.message_handler.wait_for(("RegionHandshake",))
|
||||
|
||||
# If we're connecting to the main region, it won't even send us a RegionHandshake until we
|
||||
# first send a CompleteAgentMovement.
|
||||
if main_region:
|
||||
await self.complete_agent_movement()
|
||||
|
||||
self.name = str((await region_handshake_fut)["RegionInfo"][0]["SimName"])
|
||||
self.session().objects.track_region_objects(self.handle)
|
||||
await self.circuit.send_reliable(
|
||||
Message(
|
||||
"RegionHandshakeReply",
|
||||
Block("AgentData", AgentID=self.session().agent_id, SessionID=self.session().id),
|
||||
Block(
|
||||
"RegionInfo",
|
||||
Flags=(
|
||||
RegionHandshakeReplyFlags.SUPPORTS_SELF_APPEARANCE
|
||||
| RegionHandshakeReplyFlags.VOCACHE_CULLING_ENABLED
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
await self.circuit.send_reliable(
|
||||
Message(
|
||||
"AgentThrottle",
|
||||
Block(
|
||||
"AgentData",
|
||||
AgentID=self.session().agent_id,
|
||||
SessionID=self.session().id,
|
||||
CircuitCode=self.session().circuit_code,
|
||||
),
|
||||
Block(
|
||||
"Throttle",
|
||||
GenCounter=0,
|
||||
# Reasonable defaults, I guess
|
||||
Throttles_=ThrottleData(
|
||||
resend=207360.0,
|
||||
land=165376.0,
|
||||
wind=33075.19921875,
|
||||
cloud=33075.19921875,
|
||||
task=682700.75,
|
||||
texture=682700.75,
|
||||
asset=269312.0
|
||||
),
|
||||
)
|
||||
)
|
||||
)
|
||||
if self.session().session_manager.settings.SEND_AGENT_UPDATES:
|
||||
# Usually we want to send at least one, since lots of messages will never be sent by the sim
|
||||
# until we send at least one AgentUpdate. For example, ParcelOverlay and LayerData.
|
||||
await self.circuit.send_reliable(
|
||||
Message(
|
||||
"AgentUpdate",
|
||||
Block(
|
||||
'AgentData',
|
||||
AgentID=self.session().agent_id,
|
||||
SessionID=self.session().id,
|
||||
# Don't really care about the other fields.
|
||||
fill_missing=True,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
async with seed_resp_fut as seed_resp:
|
||||
seed_resp.raise_for_status()
|
||||
self.update_caps(await seed_resp.read_llsd())
|
||||
|
||||
self._eq_task = create_logged_task(self._poll_event_queue(), "EQ Poll")
|
||||
|
||||
settings = self.session().session_manager.settings
|
||||
if settings.AUTO_REQUEST_PARCELS:
|
||||
_ = create_logged_task(self.parcel_manager.request_dirty_parcels(), "Parcel Request")
|
||||
if settings.AUTO_REQUEST_MATERIALS:
|
||||
_ = create_logged_task(self.objects.request_all_materials(), "Request All Materials")
|
||||
|
||||
except Exception as e:
|
||||
# Let consumers who were `await`ing the connected signal know there was an error
|
||||
if not self.connected.done():
|
||||
self.connected.set_exception(e)
|
||||
raise
|
||||
|
||||
self.connected.set_result(None)
|
||||
|
||||
def disconnect(self) -> None:
|
||||
"""Simulator has gone away, disconnect. Should be synchronous"""
|
||||
if self._eq_task is not None:
|
||||
self._eq_task.cancel()
|
||||
self._eq_task = None
|
||||
self.circuit.disconnect()
|
||||
self.objects.clear()
|
||||
if self.connected.done():
|
||||
self.connected = asyncio.Future()
|
||||
# TODO: cancel XFers and Transfers and whatnot
|
||||
|
||||
async def complete_agent_movement(self) -> None:
|
||||
await self.circuit.send_reliable(
|
||||
Message(
|
||||
"CompleteAgentMovement",
|
||||
Block(
|
||||
"AgentData",
|
||||
AgentID=self.session().agent_id,
|
||||
SessionID=self.session().id,
|
||||
CircuitCode=self.session().circuit_code
|
||||
),
|
||||
)
|
||||
)
|
||||
self.session().main_region = self
|
||||
|
||||
async def _poll_event_queue(self):
|
||||
ack: Optional[int] = None
|
||||
while True:
|
||||
payload = {"ack": ack, "done": False}
|
||||
try:
|
||||
async with self.caps_client.post("EventQueueGet", llsd=payload) as resp:
|
||||
if resp.status != 200:
|
||||
await asyncio.sleep(0.1)
|
||||
continue
|
||||
polled = await resp.read_llsd()
|
||||
for event in polled["events"]:
|
||||
if self._llsd_serializer.can_handle(event["message"]):
|
||||
msg = self._llsd_serializer.deserialize(event)
|
||||
else:
|
||||
msg = Message.from_eq_event(event)
|
||||
msg.sender = self.circuit_addr
|
||||
msg.direction = Direction.IN
|
||||
self.session().message_handler.handle(msg)
|
||||
self.message_handler.handle(msg)
|
||||
ack = polled["id"]
|
||||
await asyncio.sleep(0.001)
|
||||
except aiohttp.client_exceptions.ServerDisconnectedError:
|
||||
# This is expected to happen during long-polling, just pick up again where we left off.
|
||||
await asyncio.sleep(0.001)
|
||||
|
||||
async def _handle_ping_check(self, message: Message):
|
||||
self.circuit.send(
|
||||
Message(
|
||||
"CompletePingCheck",
|
||||
Block("PingID", PingID=message["PingID"]["PingID"]),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class HippoClientSession(BaseClientSession):
|
||||
"""Represents a client's view of a remote session"""
|
||||
REGION_CLS = HippoClientRegion
|
||||
|
||||
region_by_handle: Callable[[int], Optional[HippoClientRegion]]
|
||||
region_by_circuit_addr: Callable[[ADDR_TUPLE], Optional[HippoClientRegion]]
|
||||
regions: List[HippoClientRegion]
|
||||
session_manager: HippoClient
|
||||
main_region: Optional[HippoClientRegion]
|
||||
|
||||
def __init__(self, id, secure_session_id, agent_id, circuit_code, session_manager: Optional[HippoClient] = None,
|
||||
login_data=None):
|
||||
super().__init__(id, secure_session_id, agent_id, circuit_code, session_manager, login_data=login_data)
|
||||
self.http_session = session_manager.http_session
|
||||
self.objects = ClientWorldObjectManager(proxify(self), session_manager.settings, None)
|
||||
self.inventory_manager = InventoryManager(proxify(self))
|
||||
self.transport: Optional[SocketUDPTransport] = None
|
||||
self.protocol: Optional[HippoClientProtocol] = None
|
||||
self.message_handler.take_by_default = False
|
||||
|
||||
for msg_name in ("DisableSimulator", "CloseCircuit"):
|
||||
self.message_handler.subscribe(msg_name, lambda msg: self.unregister_region(msg.sender))
|
||||
for msg_name in ("TeleportFinish", "CrossedRegion", "EstablishAgentCommunication"):
|
||||
self.message_handler.subscribe(msg_name, self._handle_register_region_message)
|
||||
|
||||
def register_region(self, circuit_addr: Optional[ADDR_TUPLE] = None, seed_url: Optional[str] = None,
|
||||
handle: Optional[int] = None) -> HippoClientRegion:
|
||||
return super().register_region(circuit_addr, seed_url, handle) # type:ignore
|
||||
|
||||
def unregister_region(self, circuit_addr: ADDR_TUPLE) -> None:
|
||||
for i, region in enumerate(self.regions):
|
||||
if region.circuit_addr == circuit_addr:
|
||||
self.regions[i].disconnect()
|
||||
del self.regions[i]
|
||||
return
|
||||
raise KeyError(f"No such region for {circuit_addr!r}")
|
||||
|
||||
def open_circuit(self, circuit_addr: ADDR_TUPLE):
|
||||
for region in self.regions:
|
||||
if region.circuit_addr == circuit_addr:
|
||||
valid_circuit = False
|
||||
if not region.circuit or not region.circuit.is_alive:
|
||||
region.circuit = Circuit(("127.0.0.1", 0), circuit_addr, self.transport)
|
||||
region.circuit.is_alive = False
|
||||
valid_circuit = True
|
||||
if region.circuit and region.circuit.is_alive:
|
||||
# Whatever, already open
|
||||
logging.debug("Tried to re-open circuit for %r" % (circuit_addr,))
|
||||
valid_circuit = True
|
||||
return valid_circuit
|
||||
return False
|
||||
|
||||
def _handle_register_region_message(self, msg: Message):
|
||||
# Handle events that inform us about new regions
|
||||
sim_addr, sim_handle, sim_seed = None, None, None
|
||||
moving_to_region = False
|
||||
# Sim is asking us to talk to a neighbour
|
||||
if msg.name == "EstablishAgentCommunication":
|
||||
ip_split = msg["EventData"]["sim-ip-and-port"].split(":")
|
||||
sim_addr = (ip_split[0], int(ip_split[1]))
|
||||
sim_seed = msg["EventData"]["seed-capability"]
|
||||
# We teleported or cross region, opening comms to new sim
|
||||
elif msg.name in ("TeleportFinish", "CrossedRegion"):
|
||||
sim_block = msg.get_block("RegionData", msg.get_block("Info"))[0]
|
||||
sim_addr = (sim_block["SimIP"], sim_block["SimPort"])
|
||||
sim_handle = sim_block["RegionHandle"]
|
||||
sim_seed = sim_block["SeedCapability"]
|
||||
moving_to_region = True
|
||||
# Sim telling us about a neighbour
|
||||
# elif msg.name == "EnableSimulator":
|
||||
# sim_block = msg["SimulatorInfo"][0]
|
||||
# sim_addr = (sim_block["IP"], sim_block["Port"])
|
||||
# sim_handle = sim_block["Handle"]
|
||||
# TODO: EnableSimulator is a little weird. It creates a region and establishes a
|
||||
# circuit, but with no seed cap. The viewer will send UseCircuitCode and all that,
|
||||
# but it's totally workable to just wait for an EstablishAgentCommunication to do that,
|
||||
# since that's when the region actually shows up. I guess EnableSimulator just gives the
|
||||
# viewer some lead time to set up the circuit before the region is actually shown through
|
||||
# EstablishAgentCommunication? Either way, messing around with regions that don't have seed
|
||||
# caps is annoying, so let's just not do it.
|
||||
|
||||
# Register a region if this message was telling us about a new one
|
||||
if sim_addr is not None:
|
||||
region = self.register_region(sim_addr, handle=sim_handle, seed_url=sim_seed)
|
||||
# We can't actually connect without a sim seed, mind you, when we receive and EnableSimulator
|
||||
# we have to wait for the EstablishAgentCommunication to actually connect.
|
||||
need_connect = (region.circuit and region.circuit.is_alive) or moving_to_region
|
||||
self.open_circuit(sim_addr)
|
||||
if need_connect:
|
||||
create_logged_task(region.connect(main_region=moving_to_region), "Region Connect")
|
||||
elif moving_to_region:
|
||||
# No need to connect, but we do need to complete agent movement.
|
||||
create_logged_task(region.complete_agent_movement(), "CompleteAgentMovement")
|
||||
|
||||
|
||||
class HippoClient(BaseClientSessionManager):
|
||||
"""A simple client, only connects to one region at a time currently."""
|
||||
|
||||
SUPPORTED_CAPS: Set[str] = {
|
||||
"AbuseCategories",
|
||||
"AcceptFriendship",
|
||||
"AcceptGroupInvite",
|
||||
"AgentPreferences",
|
||||
"AgentProfile",
|
||||
"AgentState",
|
||||
"AttachmentResources",
|
||||
"AvatarPickerSearch",
|
||||
"AvatarRenderInfo",
|
||||
"CharacterProperties",
|
||||
"ChatSessionRequest",
|
||||
"CopyInventoryFromNotecard",
|
||||
"CreateInventoryCategory",
|
||||
"DeclineFriendship",
|
||||
"DeclineGroupInvite",
|
||||
"DispatchRegionInfo",
|
||||
"DirectDelivery",
|
||||
"EnvironmentSettings",
|
||||
"EstateAccess",
|
||||
"DispatchOpenRegionSettings",
|
||||
"EstateChangeInfo",
|
||||
"EventQueueGet",
|
||||
"ExtEnvironment",
|
||||
"FetchLib2",
|
||||
"FetchLibDescendents2",
|
||||
"FetchInventory2",
|
||||
"FetchInventoryDescendents2",
|
||||
"IncrementCOFVersion",
|
||||
"InventoryAPIv3",
|
||||
"LibraryAPIv3",
|
||||
"InterestList",
|
||||
"InventoryThumbnailUpload",
|
||||
"GetDisplayNames",
|
||||
"GetExperiences",
|
||||
"AgentExperiences",
|
||||
"FindExperienceByName",
|
||||
"GetExperienceInfo",
|
||||
"GetAdminExperiences",
|
||||
"GetCreatorExperiences",
|
||||
"ExperiencePreferences",
|
||||
"GroupExperiences",
|
||||
"UpdateExperience",
|
||||
"IsExperienceAdmin",
|
||||
"IsExperienceContributor",
|
||||
"RegionExperiences",
|
||||
"ExperienceQuery",
|
||||
"GetMesh",
|
||||
"GetMesh2",
|
||||
"GetMetadata",
|
||||
"GetObjectCost",
|
||||
"GetObjectPhysicsData",
|
||||
"GetTexture",
|
||||
"GroupAPIv1",
|
||||
"GroupMemberData",
|
||||
"GroupProposalBallot",
|
||||
"HomeLocation",
|
||||
"LandResources",
|
||||
"LSLSyntax",
|
||||
"MapLayer",
|
||||
"MapLayerGod",
|
||||
"MeshUploadFlag",
|
||||
"NavMeshGenerationStatus",
|
||||
"NewFileAgentInventory",
|
||||
"ObjectAnimation",
|
||||
"ObjectMedia",
|
||||
"ObjectMediaNavigate",
|
||||
"ObjectNavMeshProperties",
|
||||
"ParcelPropertiesUpdate",
|
||||
"ParcelVoiceInfoRequest",
|
||||
"ProductInfoRequest",
|
||||
"ProvisionVoiceAccountRequest",
|
||||
"ReadOfflineMsgs",
|
||||
"RegionObjects",
|
||||
"RemoteParcelRequest",
|
||||
"RenderMaterials",
|
||||
"RequestTextureDownload",
|
||||
"ResourceCostSelected",
|
||||
"RetrieveNavMeshSrc",
|
||||
"SearchStatRequest",
|
||||
"SearchStatTracking",
|
||||
"SendPostcard",
|
||||
"SendUserReport",
|
||||
"SendUserReportWithScreenshot",
|
||||
"ServerReleaseNotes",
|
||||
"SetDisplayName",
|
||||
"SimConsoleAsync",
|
||||
"SimulatorFeatures",
|
||||
"StartGroupProposal",
|
||||
"TerrainNavMeshProperties",
|
||||
"TextureStats",
|
||||
"UntrustedSimulatorMessage",
|
||||
"UpdateAgentInformation",
|
||||
"UpdateAgentLanguage",
|
||||
"UpdateAvatarAppearance",
|
||||
"UpdateGestureAgentInventory",
|
||||
"UpdateGestureTaskInventory",
|
||||
"UpdateNotecardAgentInventory",
|
||||
"UpdateNotecardTaskInventory",
|
||||
"UpdateScriptAgent",
|
||||
"UpdateScriptTask",
|
||||
"UpdateSettingsAgentInventory",
|
||||
"UpdateSettingsTaskInventory",
|
||||
"UploadAgentProfileImage",
|
||||
"UploadBakedTexture",
|
||||
"UserInfo",
|
||||
"ViewerAsset",
|
||||
"ViewerBenefits",
|
||||
"ViewerMetrics",
|
||||
"ViewerStartAuction",
|
||||
"ViewerStats",
|
||||
}
|
||||
|
||||
DEFAULT_OPTIONS = {
|
||||
"inventory-root",
|
||||
"inventory-skeleton",
|
||||
"inventory-lib-root",
|
||||
"inventory-lib-owner",
|
||||
"inventory-skel-lib",
|
||||
"initial-outfit",
|
||||
"gestures",
|
||||
"display_names",
|
||||
"event_notifications",
|
||||
"classified_categories",
|
||||
"adult_compliant",
|
||||
"buddy-list",
|
||||
"newuser-config",
|
||||
"ui-config",
|
||||
"advanced-mode",
|
||||
"max-agent-groups",
|
||||
"map-server-url",
|
||||
"voice-config",
|
||||
"tutorial_setting",
|
||||
"login-flags",
|
||||
"global-textures",
|
||||
# Not an official option, just so this can be tracked.
|
||||
"pyogp-client",
|
||||
}
|
||||
|
||||
DEFAULT_LOGIN_URI = "https://login.agni.lindenlab.com/cgi-bin/login.cgi"
|
||||
|
||||
def __init__(self, options: Optional[Set[str]] = None):
|
||||
self._username: Optional[str] = None
|
||||
self._password: Optional[str] = None
|
||||
self._mac = uuid.getnode()
|
||||
self._options = options if options is not None else self.DEFAULT_OPTIONS
|
||||
self.http_session: Optional[aiohttp.ClientSession] = aiohttp.ClientSession(trust_env=True)
|
||||
self.session: Optional[HippoClientSession] = None
|
||||
self.settings = ClientSettings()
|
||||
self._resend_task: Optional[asyncio.Task] = None
|
||||
|
||||
@property
|
||||
def main_region(self) -> Optional[HippoClientRegion]:
|
||||
if not self.session:
|
||||
return None
|
||||
return self.session.main_region
|
||||
|
||||
@property
|
||||
def main_circuit(self) -> Optional[Circuit]:
|
||||
if not self.main_region:
|
||||
return None
|
||||
return self.main_region.circuit
|
||||
|
||||
@property
|
||||
def main_caps_client(self) -> Optional[CapsClient]:
|
||||
if not self.main_region:
|
||||
return None
|
||||
return self.main_region.caps_client
|
||||
|
||||
async def aclose(self):
|
||||
try:
|
||||
self.logout()
|
||||
finally:
|
||||
if self.http_session:
|
||||
await self.http_session.close()
|
||||
self.http_session = None
|
||||
|
||||
def __del__(self):
|
||||
# Make sure we don't leak resources if someone was lazy.
|
||||
try:
|
||||
self.logout()
|
||||
finally:
|
||||
if self.http_session:
|
||||
try:
|
||||
asyncio.create_task(self.http_session.close)
|
||||
except:
|
||||
pass
|
||||
self.http_session = None
|
||||
|
||||
async def _create_transport(self) -> Tuple[AbstractUDPTransport, HippoClientProtocol]:
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
transport, protocol = await loop.create_datagram_endpoint(
|
||||
lambda: HippoClientProtocol(self.session),
|
||||
local_addr=('0.0.0.0', 0))
|
||||
transport = SocketUDPTransport(transport)
|
||||
return transport, protocol
|
||||
|
||||
async def login(
|
||||
self,
|
||||
username: str,
|
||||
password: str,
|
||||
login_uri: Optional[str] = None,
|
||||
agree_to_tos: bool = False,
|
||||
start_location: Union[StartLocation, str, None] = StartLocation.LAST,
|
||||
connect: bool = True,
|
||||
):
|
||||
if self.session:
|
||||
raise RuntimeError("Already logged in!")
|
||||
|
||||
if not login_uri:
|
||||
login_uri = self.DEFAULT_LOGIN_URI
|
||||
|
||||
if start_location is None:
|
||||
start_location = StartLocation.LAST
|
||||
|
||||
# This isn't a symbolic start location and isn't a URI, must be a sim name.
|
||||
if start_location not in iter(StartLocation) and not start_location.startswith("uri:"):
|
||||
start_location = f"uri:{start_location}&128&128&128"
|
||||
|
||||
split_username = username.split(" ")
|
||||
if len(split_username) < 2:
|
||||
first_name = split_username[0]
|
||||
last_name = "Resident"
|
||||
else:
|
||||
first_name, last_name = split_username
|
||||
|
||||
payload = {
|
||||
"address_size": 64,
|
||||
"agree_to_tos": int(agree_to_tos),
|
||||
"channel": "Hippolyzer",
|
||||
"extended_errors": 1,
|
||||
"first": first_name,
|
||||
"last": last_name,
|
||||
"host_id": "",
|
||||
"id0": hashlib.md5(str(self._mac).encode("ascii")).hexdigest(),
|
||||
"mac": hashlib.md5(str(self._mac).encode("ascii")).hexdigest(),
|
||||
"mfa_hash": "",
|
||||
"passwd": "$1$" + hashlib.md5(str(password).encode("ascii")).hexdigest(),
|
||||
# TODO: actually get these
|
||||
"platform": "lnx",
|
||||
"platform_string": "Linux 6.6",
|
||||
# TODO: What is this?
|
||||
"platform_version": "2.38.0",
|
||||
"read_critical": 0,
|
||||
"start": str(start_location),
|
||||
"token": "",
|
||||
"version": version("hippolyzer"),
|
||||
"options": list(self._options),
|
||||
}
|
||||
async with self.http_session.post(
|
||||
login_uri,
|
||||
data=xmlrpc.client.dumps((payload,), "login_to_simulator"),
|
||||
headers={"Content-Type": "text/xml", "User-Agent": self.settings.USER_AGENT},
|
||||
ssl=self.settings.SSL_VERIFY,
|
||||
) as resp:
|
||||
resp.raise_for_status()
|
||||
login_data = xmlrpc.client.loads((await resp.read()).decode("utf8"))[0][0]
|
||||
self.session = HippoClientSession.from_login_data(login_data, self)
|
||||
|
||||
self.session.transport, self.session.protocol = await self._create_transport()
|
||||
self._resend_task = create_logged_task(self._attempt_resends(), "Circuit Resend")
|
||||
self.session.message_handler.subscribe("AgentDataUpdate", self._handle_agent_data_update)
|
||||
self.session.message_handler.subscribe("AgentGroupDataUpdate", self._handle_agent_group_data_update)
|
||||
|
||||
assert self.session.open_circuit(self.session.regions[-1].circuit_addr)
|
||||
if connect:
|
||||
region = self.session.regions[-1]
|
||||
await region.connect(main_region=True)
|
||||
|
||||
def logout(self):
|
||||
if not self.session:
|
||||
return
|
||||
if self._resend_task:
|
||||
self._resend_task.cancel()
|
||||
self._resend_task = None
|
||||
|
||||
if self.main_circuit and self.main_circuit.is_alive:
|
||||
# Don't need to send reliably, there's a good chance the server won't ACK anyway.
|
||||
self.main_circuit.send(
|
||||
Message(
|
||||
"LogoutRequest",
|
||||
Block("AgentData", AgentID=self.session.agent_id, SessionID=self.session.id),
|
||||
)
|
||||
)
|
||||
session = self.session
|
||||
self.session = None
|
||||
for region in session.regions:
|
||||
region.disconnect()
|
||||
session.transport.close()
|
||||
|
||||
def send_chat(self, message: Union[bytes, str], channel: int = 0, chat_type=ChatType.NORMAL) -> asyncio.Future:
|
||||
return self.main_circuit.send_reliable(Message(
|
||||
"ChatFromViewer",
|
||||
Block("AgentData", SessionID=self.session.id, AgentID=self.session.agent_id),
|
||||
Block("ChatData", Message=message, Channel=channel, Type=chat_type),
|
||||
))
|
||||
|
||||
def teleport(self, region_handle: int, local_pos=Vector3(0, 0, 0)) -> asyncio.Future:
|
||||
"""Synchronously requests a teleport, returning a Future for teleport completion"""
|
||||
teleport_fut = asyncio.Future()
|
||||
|
||||
# Send request synchronously, await asynchronously.
|
||||
send_fut = self.main_circuit.send_reliable(
|
||||
Message(
|
||||
'TeleportLocationRequest',
|
||||
Block('AgentData', AgentID=self.session.agent_id, SessionID=self.session.id),
|
||||
Block('Info', RegionHandle=region_handle, Position=local_pos, fill_missing=True),
|
||||
)
|
||||
)
|
||||
|
||||
async def _handle_teleport():
|
||||
# Subscribe first, we may receive an event before we receive the packet ACK.
|
||||
with self.session.message_handler.subscribe_async(
|
||||
("TeleportLocal", "TeleportFailed", "TeleportFinish"),
|
||||
) as get_tp_done_msg:
|
||||
try:
|
||||
await send_fut
|
||||
except Exception as e:
|
||||
# Pass along error if we failed to send reliably.
|
||||
teleport_fut.set_exception(e)
|
||||
return
|
||||
|
||||
# Wait for a message that says we're done the teleport
|
||||
msg = await get_tp_done_msg()
|
||||
if msg.name == "TeleportFailed":
|
||||
teleport_fut.set_exception(RuntimeError("Failed to teleport"))
|
||||
elif msg.name == "TeleportLocal":
|
||||
# Within the sim, nothing else we need to do
|
||||
teleport_fut.set_result(None)
|
||||
elif msg.name == "TeleportFinish":
|
||||
# Non-local TP, wait until we receive the AgentMovementComplete to
|
||||
# set the finished signal.
|
||||
|
||||
# Region should be registered by this point, wait for it to connect
|
||||
try:
|
||||
# just fail if it takes longer than 30 seconds for the handshake to complete
|
||||
await asyncio.wait_for(self.session.region_by_handle(region_handle).connected, 30)
|
||||
except Exception as e:
|
||||
teleport_fut.set_exception(e)
|
||||
return
|
||||
teleport_fut.set_result(None)
|
||||
|
||||
create_logged_task(_handle_teleport(), "Teleport")
|
||||
|
||||
return teleport_fut
|
||||
|
||||
async def _attempt_resends(self):
|
||||
while True:
|
||||
if self.session is None:
|
||||
break
|
||||
for region in self.session.regions:
|
||||
if not region.circuit.is_alive:
|
||||
continue
|
||||
region.circuit.resend_unacked()
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
def _handle_agent_data_update(self, msg: Message):
|
||||
self.session.active_group = msg["AgentData"]["ActiveGroupID"]
|
||||
|
||||
def _handle_agent_group_data_update(self, msg: Message):
|
||||
self.session.groups.clear()
|
||||
for block in msg["GroupData"]:
|
||||
self.session.groups.add(block["GroupID"])
|
||||
208
hippolyzer/lib/client/inventory_manager.py
Normal file
208
hippolyzer/lib/client/inventory_manager.py
Normal file
@@ -0,0 +1,208 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import gzip
|
||||
import itertools
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Union, List, Tuple, Set
|
||||
|
||||
from hippolyzer.lib.base import llsd
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.inventory import InventoryModel, InventoryCategory, InventoryItem
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.templates import AssetType, FolderType
|
||||
from hippolyzer.lib.client.state import BaseClientSession
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class InventoryManager:
|
||||
def __init__(self, session: BaseClientSession):
|
||||
self._session = session
|
||||
self.model: InventoryModel = InventoryModel()
|
||||
self._load_skeleton()
|
||||
self._session.message_handler.subscribe("BulkUpdateInventory", self._handle_bulk_update_inventory)
|
||||
self._session.message_handler.subscribe("UpdateCreateInventoryItem", self._handle_update_create_inventory_item)
|
||||
self._session.message_handler.subscribe("RemoveInventoryItem", self._handle_remove_inventory_item)
|
||||
self._session.message_handler.subscribe("MoveInventoryItem", self._handle_move_inventory_item)
|
||||
|
||||
def _load_skeleton(self):
|
||||
assert not self.model.nodes
|
||||
skel_cats: List[dict] = self._session.login_data.get('inventory-skeleton', [])
|
||||
for skel_cat in skel_cats:
|
||||
self.model.add(InventoryCategory(
|
||||
name=skel_cat["name"],
|
||||
cat_id=UUID(skel_cat["folder_id"]),
|
||||
parent_id=UUID(skel_cat["parent_id"]),
|
||||
# Don't use the version from the skeleton, this flags the inventory as needing
|
||||
# completion from the inventory cache. This matches indra's behavior.
|
||||
version=InventoryCategory.VERSION_NONE,
|
||||
type=AssetType.CATEGORY,
|
||||
pref_type=FolderType(skel_cat.get("type_default", FolderType.NONE)),
|
||||
owner_id=self._session.agent_id,
|
||||
))
|
||||
|
||||
def load_cache(self, path: Union[str, Path]):
|
||||
# Per indra, rough flow for loading inv on login is:
|
||||
# 1. Look at inventory skeleton from login response
|
||||
# 2. Pre-populate model with categories from the skeleton, including their versions
|
||||
# 3. Read the inventory cache, tracking categories and items separately
|
||||
# 4. Walk the list of categories in our cache. If the cat exists in the skeleton and the versions
|
||||
# match, then we may load the category and its descendants from cache.
|
||||
# 5. Any categories in the skeleton but not in the cache, or those with mismatched versions must be fetched.
|
||||
# The viewer does this by setting the local version of the cats to -1 and forcing a descendent fetch
|
||||
# over AIS.
|
||||
#
|
||||
# By the time you call this function call, you should have already loaded the inventory skeleton
|
||||
# into the model set its inventory category versions to VERSION_NONE.
|
||||
|
||||
skel_cats: List[dict] = self._session.login_data['inventory-skeleton']
|
||||
# UUID -> version map for inventory skeleton
|
||||
skel_versions = {UUID(cat["folder_id"]): cat["version"] for cat in skel_cats}
|
||||
LOG.info(f"Parsing inv cache at {path}")
|
||||
cached_categories, cached_items = self._parse_cache(path)
|
||||
LOG.info(f"Done parsing inv cache at {path}")
|
||||
loaded_cat_ids: Set[UUID] = set()
|
||||
|
||||
for cached_cat in cached_categories:
|
||||
existing_cat: InventoryCategory = self.model.get(cached_cat.cat_id) # noqa
|
||||
# Don't clobber an existing cat unless it just has a placeholder version,
|
||||
# maybe from loading the skeleton?
|
||||
if existing_cat and existing_cat.version != InventoryCategory.VERSION_NONE:
|
||||
continue
|
||||
# Cached cat isn't the same as what the inv server says it should be, can't use it.
|
||||
if cached_cat.version != skel_versions.get(cached_cat.cat_id):
|
||||
continue
|
||||
# Update any existing category in-place, or add if not present
|
||||
self.model.upsert(cached_cat)
|
||||
# Any items in this category in our cache file are usable and should be added
|
||||
loaded_cat_ids.add(cached_cat.cat_id)
|
||||
|
||||
for cached_item in cached_items:
|
||||
# The skeleton doesn't have any items, so if we run into any items they should be exactly the
|
||||
# same as what we're trying to add. No point clobbering.
|
||||
if cached_item.item_id in self.model:
|
||||
continue
|
||||
# The parent category didn't have a cache hit against the inventory skeleton, can't add!
|
||||
# We don't even know if this item would be in the current version of it's parent cat!
|
||||
if cached_item.parent_id not in loaded_cat_ids:
|
||||
continue
|
||||
self.model.add(cached_item)
|
||||
|
||||
self.model.flag_if_dirty()
|
||||
|
||||
def _parse_cache(self, path: Union[str, Path]) -> Tuple[List[InventoryCategory], List[InventoryItem]]:
|
||||
"""Warning, may be incredibly slow due to llsd.parse_notation() behavior"""
|
||||
categories: List[InventoryCategory] = []
|
||||
items: List[InventoryItem] = []
|
||||
# Parse our cached items and categories out of the compressed inventory cache
|
||||
first_line = True
|
||||
with gzip.open(path, "rb") as f:
|
||||
# Line-delimited LLSD notation!
|
||||
for line in f.readlines():
|
||||
# TODO: Parsing of invcache is dominated by `parse_notation()`. It's stupidly inefficient.
|
||||
node_llsd = llsd.parse_notation(line)
|
||||
if first_line:
|
||||
# First line is the file header
|
||||
first_line = False
|
||||
if node_llsd['inv_cache_version'] not in (2, 3):
|
||||
raise ValueError(f"Unknown cache version: {node_llsd!r}")
|
||||
continue
|
||||
|
||||
if InventoryCategory.ID_ATTR in node_llsd:
|
||||
if (cat_node := InventoryCategory.from_llsd(node_llsd)) is not None:
|
||||
categories.append(cat_node)
|
||||
elif InventoryItem.ID_ATTR in node_llsd:
|
||||
if (item_node := InventoryItem.from_llsd(node_llsd)) is not None:
|
||||
items.append(item_node)
|
||||
else:
|
||||
LOG.warning(f"Unknown node type in inv cache: {node_llsd!r}")
|
||||
return categories, items
|
||||
|
||||
def _handle_bulk_update_inventory(self, msg: Message):
|
||||
any_cats = False
|
||||
for folder_block in msg["FolderData"]:
|
||||
if folder_block["FolderID"] == UUID.ZERO:
|
||||
continue
|
||||
any_cats = True
|
||||
self.model.upsert(
|
||||
InventoryCategory.from_folder_data(folder_block),
|
||||
# Don't clobber version, we only want to fetch the folder if it's new
|
||||
# and hasn't just moved.
|
||||
update_fields={"parent_id", "name", "pref_type"},
|
||||
)
|
||||
for item_block in msg["ItemData"]:
|
||||
if item_block["ItemID"] == UUID.ZERO:
|
||||
continue
|
||||
self.model.upsert(InventoryItem.from_inventory_data(item_block))
|
||||
|
||||
if any_cats:
|
||||
self.model.flag_if_dirty()
|
||||
|
||||
def _validate_recipient(self, recipient: UUID):
|
||||
if self._session.agent_id != recipient:
|
||||
raise ValueError(f"AgentID Mismatch {self._session.agent_id} != {recipient}")
|
||||
|
||||
def _handle_update_create_inventory_item(self, msg: Message):
|
||||
self._validate_recipient(msg["AgentData"]["AgentID"])
|
||||
for inventory_block in msg["InventoryData"]:
|
||||
self.model.upsert(InventoryItem.from_inventory_data(inventory_block))
|
||||
|
||||
def _handle_remove_inventory_item(self, msg: Message):
|
||||
self._validate_recipient(msg["AgentData"]["AgentID"])
|
||||
for inventory_block in msg["InventoryData"]:
|
||||
node = self.model.get(inventory_block["ItemID"])
|
||||
if node:
|
||||
self.model.unlink(node)
|
||||
|
||||
def _handle_remove_inventory_folder(self, msg: Message):
|
||||
self._validate_recipient(msg["AgentData"]["AgentID"])
|
||||
for folder_block in msg["FolderData"]:
|
||||
node = self.model.get(folder_block["FolderID"])
|
||||
if node:
|
||||
self.model.unlink(node)
|
||||
|
||||
def _handle_move_inventory_item(self, msg: Message):
|
||||
for inventory_block in msg["InventoryData"]:
|
||||
node = self.model.get(inventory_block["ItemID"])
|
||||
if not node:
|
||||
LOG.warning(f"Missing inventory item {inventory_block['ItemID']}")
|
||||
continue
|
||||
if inventory_block["NewName"]:
|
||||
node.name = str(inventory_block["NewName"])
|
||||
node.parent_id = inventory_block['FolderID']
|
||||
|
||||
def process_aisv3_response(self, payload: dict):
|
||||
if "name" in payload:
|
||||
# Just a rough guess. Assume this response is updating something if there's
|
||||
# a "name" key.
|
||||
if InventoryCategory.ID_ATTR_AIS in payload:
|
||||
if (cat_node := InventoryCategory.from_llsd(payload, flavor="ais")) is not None:
|
||||
self.model.upsert(cat_node)
|
||||
elif InventoryItem.ID_ATTR in payload:
|
||||
if (item_node := InventoryItem.from_llsd(payload, flavor="ais")) is not None:
|
||||
self.model.upsert(item_node)
|
||||
else:
|
||||
LOG.warning(f"Unknown node type in AIS payload: {payload!r}")
|
||||
|
||||
# Parse the embedded stuff
|
||||
embedded_dict = payload.get("_embedded", {})
|
||||
for category_llsd in embedded_dict.get("categories", {}).values():
|
||||
self.model.upsert(InventoryCategory.from_llsd(category_llsd, flavor="ais"))
|
||||
for item_llsd in embedded_dict.get("items", {}).values():
|
||||
self.model.upsert(InventoryItem.from_llsd(item_llsd, flavor="ais"))
|
||||
for link_llsd in embedded_dict.get("links", {}).values():
|
||||
self.model.upsert(InventoryItem.from_llsd(link_llsd, flavor="ais"))
|
||||
|
||||
# Get rid of anything we were asked to
|
||||
for node_id in itertools.chain(
|
||||
payload.get("_broken_links_removed", ()),
|
||||
payload.get("_removed_items", ()),
|
||||
payload.get("_category_items_removed", ()),
|
||||
payload.get("_categories_removed", ()),
|
||||
):
|
||||
node = self.model.get(node_id)
|
||||
if node:
|
||||
# Presumably this list is exhaustive, so don't unlink children.
|
||||
self.model.unlink(node, single_only=True)
|
||||
@@ -17,6 +17,7 @@ from hippolyzer.lib.base.datatypes import UUID, Vector3
|
||||
from hippolyzer.lib.base.helpers import proxify
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
from hippolyzer.lib.base.message.message_handler import MessageHandler
|
||||
from hippolyzer.lib.base.message.msgtypes import PacketFlags
|
||||
from hippolyzer.lib.base.objects import (
|
||||
normalize_object_update,
|
||||
normalize_terse_object_update,
|
||||
@@ -26,16 +27,20 @@ from hippolyzer.lib.base.objects import (
|
||||
)
|
||||
from hippolyzer.lib.base.settings import Settings
|
||||
from hippolyzer.lib.client.namecache import NameCache, NameCacheEntry
|
||||
from hippolyzer.lib.client.state import BaseClientSession, BaseClientRegion
|
||||
from hippolyzer.lib.base.templates import PCode, ObjectStateSerializer
|
||||
from hippolyzer.lib.base import llsd
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from hippolyzer.lib.client.state import BaseClientRegion, BaseClientSession
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
OBJECT_OR_LOCAL = Union[Object, int]
|
||||
MATERIAL_MAP_TYPE = Dict[UUID, dict]
|
||||
|
||||
|
||||
class UpdateType(enum.IntEnum):
|
||||
OBJECT_UPDATE = enum.auto()
|
||||
class ObjectUpdateType(enum.IntEnum):
|
||||
UPDATE = enum.auto()
|
||||
PROPERTIES = enum.auto()
|
||||
FAMILY = enum.auto()
|
||||
COSTS = enum.auto()
|
||||
@@ -47,12 +52,13 @@ class ClientObjectManager:
|
||||
Object manager for a specific region
|
||||
"""
|
||||
|
||||
__slots__ = ("_region", "_world_objects", "state")
|
||||
__slots__ = ("_region", "_world_objects", "state", "__weakref__", "_requesting_all_mats_lock")
|
||||
|
||||
def __init__(self, region: BaseClientRegion):
|
||||
self._region: BaseClientRegion = proxify(region)
|
||||
self._world_objects: ClientWorldObjectManager = proxify(region.session().objects)
|
||||
self.state: RegionObjectsState = RegionObjectsState()
|
||||
self._requesting_all_mats_lock = asyncio.Lock()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.state.localid_lookup)
|
||||
@@ -70,7 +76,7 @@ class ClientObjectManager:
|
||||
if self._region.handle is not None:
|
||||
# We're tracked by the world object manager, tell it to untrack
|
||||
# any objects that we owned
|
||||
self._world_objects.clear_region_objects(self._region.handle)
|
||||
self._world_objects.untrack_region_objects(self._region.handle)
|
||||
|
||||
def lookup_localid(self, localid: int) -> Optional[Object]:
|
||||
return self.state.lookup_localid(localid)
|
||||
@@ -116,15 +122,15 @@ class ClientObjectManager:
|
||||
*[Block("ObjectData", ObjectLocalID=x) for x in ids_to_req[:255]],
|
||||
]
|
||||
# Selecting causes ObjectProperties to be sent
|
||||
self._region.circuit.send_message(Message("ObjectSelect", blocks))
|
||||
self._region.circuit.send_message(Message("ObjectDeselect", blocks))
|
||||
self._region.circuit.send(Message("ObjectSelect", blocks, flags=PacketFlags.RELIABLE))
|
||||
self._region.circuit.send(Message("ObjectDeselect", blocks, flags=PacketFlags.RELIABLE))
|
||||
ids_to_req = ids_to_req[255:]
|
||||
|
||||
futures = []
|
||||
for local_id in local_ids:
|
||||
if local_id in unselected_ids:
|
||||
# Need to wait until we get our reply
|
||||
fut = self.state.register_future(local_id, UpdateType.PROPERTIES)
|
||||
fut = self.state.register_future(local_id, ObjectUpdateType.PROPERTIES)
|
||||
else:
|
||||
# This was selected so we should already have up to date info
|
||||
fut = asyncio.Future()
|
||||
@@ -150,33 +156,81 @@ class ClientObjectManager:
|
||||
|
||||
ids_to_req = local_ids
|
||||
while ids_to_req:
|
||||
self._region.circuit.send_message(Message(
|
||||
self._region.circuit.send(Message(
|
||||
"RequestMultipleObjects",
|
||||
Block("AgentData", AgentID=session.agent_id, SessionID=session.id),
|
||||
*[Block("ObjectData", CacheMissType=0, ID=x) for x in ids_to_req[:255]],
|
||||
flags=PacketFlags.RELIABLE,
|
||||
))
|
||||
ids_to_req = ids_to_req[255:]
|
||||
|
||||
futures = []
|
||||
for local_id in local_ids:
|
||||
futures.append(self.state.register_future(local_id, UpdateType.OBJECT_UPDATE))
|
||||
futures.append(self.state.register_future(local_id, ObjectUpdateType.UPDATE))
|
||||
return futures
|
||||
|
||||
async def request_all_materials(self) -> MATERIAL_MAP_TYPE:
|
||||
"""
|
||||
Request all materials within the sim
|
||||
|
||||
Sigh, yes, this is best practice per indra :(
|
||||
"""
|
||||
if self._requesting_all_mats_lock.locked():
|
||||
# We're already requesting all materials, wait until the lock is free
|
||||
# and just return what was returned.
|
||||
async with self._requesting_all_mats_lock:
|
||||
return self.state.materials
|
||||
|
||||
async with self._requesting_all_mats_lock:
|
||||
async with self._region.caps_client.get("RenderMaterials") as resp:
|
||||
resp.raise_for_status()
|
||||
# Clear out all previous materials, this is a complete response.
|
||||
self.state.materials.clear()
|
||||
self._process_materials_response(await resp.read())
|
||||
return self.state.materials
|
||||
|
||||
async def request_materials(self, material_ids: Sequence[UUID]) -> MATERIAL_MAP_TYPE:
|
||||
if self._requesting_all_mats_lock.locked():
|
||||
# Just wait for the in-flight request for all materials to complete
|
||||
# if we have one in flight.
|
||||
async with self._requesting_all_mats_lock:
|
||||
# Wait for the lock to be released
|
||||
pass
|
||||
|
||||
not_found = set(x for x in material_ids if (x not in self.state.materials))
|
||||
if not_found:
|
||||
# Request any materials we don't already have, if there were any
|
||||
data = {"Zipped": llsd.zip_llsd([x.bytes for x in material_ids])}
|
||||
async with self._region.caps_client.post("RenderMaterials", data=data) as resp:
|
||||
resp.raise_for_status()
|
||||
self._process_materials_response(await resp.read())
|
||||
|
||||
# build up a dict of just the requested mats
|
||||
mats = {}
|
||||
for mat_id in material_ids:
|
||||
mats[mat_id] = self.state.materials[mat_id]
|
||||
return mats
|
||||
|
||||
def _process_materials_response(self, response: bytes):
|
||||
entries = llsd.unzip_llsd(llsd.parse_xml(response)["Zipped"])
|
||||
for entry in entries:
|
||||
self.state.materials[UUID(bytes=entry["ID"])] = entry["Material"]
|
||||
|
||||
|
||||
class ObjectEvent:
|
||||
__slots__ = ("object", "updated", "update_type")
|
||||
|
||||
object: Object
|
||||
updated: Set[str]
|
||||
update_type: UpdateType
|
||||
update_type: ObjectUpdateType
|
||||
|
||||
def __init__(self, obj: Object, updated: Set[str], update_type: UpdateType):
|
||||
def __init__(self, obj: Object, updated: Set[str], update_type: ObjectUpdateType):
|
||||
self.object = obj
|
||||
self.updated = updated
|
||||
self.update_type = update_type
|
||||
|
||||
@property
|
||||
def name(self) -> UpdateType:
|
||||
def name(self) -> ObjectUpdateType:
|
||||
return self.update_type
|
||||
|
||||
|
||||
@@ -186,7 +240,7 @@ class ClientWorldObjectManager:
|
||||
self._session: BaseClientSession = session
|
||||
self._settings = settings
|
||||
self.name_cache = name_cache or NameCache()
|
||||
self.events: MessageHandler[ObjectEvent, UpdateType] = MessageHandler(take_by_default=False)
|
||||
self.events: MessageHandler[ObjectEvent, ObjectUpdateType] = MessageHandler(take_by_default=False)
|
||||
self._fullid_lookup: Dict[UUID, Object] = {}
|
||||
self._avatars: Dict[UUID, Avatar] = {}
|
||||
self._avatar_objects: Dict[UUID, Object] = {}
|
||||
@@ -236,12 +290,14 @@ class ClientWorldObjectManager:
|
||||
if self._get_region_manager(handle) is None:
|
||||
self._region_managers[handle] = proxify(self._session.region_by_handle(handle).objects)
|
||||
|
||||
def clear_region_objects(self, handle: int):
|
||||
def untrack_region_objects(self, handle: int):
|
||||
"""Handle signal that a region object manager was just cleared"""
|
||||
# Make sure they're gone from our lookup table
|
||||
for obj in tuple(self._fullid_lookup.values()):
|
||||
if obj.RegionHandle == handle:
|
||||
del self._fullid_lookup[obj.FullID]
|
||||
if handle in self._region_managers:
|
||||
del self._region_managers[handle]
|
||||
self._rebuild_avatar_objects()
|
||||
|
||||
def _get_region_manager(self, handle: int) -> Optional[ClientObjectManager]:
|
||||
@@ -286,16 +342,17 @@ class ClientWorldObjectManager:
|
||||
obj = obj.Parent
|
||||
|
||||
def clear(self):
|
||||
for handle in tuple(self._region_managers.keys()):
|
||||
self.untrack_region_objects(handle)
|
||||
self._avatars.clear()
|
||||
for region_mgr in self._region_managers.values():
|
||||
region_mgr.clear()
|
||||
if self._fullid_lookup:
|
||||
LOG.warning(f"Had {len(self._fullid_lookup)} objects not tied to a region manager!")
|
||||
self._fullid_lookup.clear()
|
||||
self._rebuild_avatar_objects()
|
||||
self._region_managers.clear()
|
||||
|
||||
def _update_existing_object(self, obj: Object, new_properties: dict, update_type: UpdateType):
|
||||
def _update_existing_object(self, obj: Object, new_properties: dict, update_type: ObjectUpdateType,
|
||||
msg: Optional[Message]):
|
||||
old_parent_id = obj.ParentID
|
||||
new_parent_id = new_properties.get("ParentID", obj.ParentID)
|
||||
old_local_id = obj.LocalID
|
||||
@@ -338,23 +395,23 @@ class ClientWorldObjectManager:
|
||||
LOG.warning(f"Tried to move object {obj!r} to unknown region {new_region_handle}")
|
||||
|
||||
if obj.PCode == PCode.AVATAR:
|
||||
# `Avatar` instances are handled separately. Update all Avatar objects so
|
||||
# we can deal with the RegionHandle change.
|
||||
# `Avatar` instances are handled separately. Update all Avatar objects,
|
||||
# so we can deal with the RegionHandle change.
|
||||
self._rebuild_avatar_objects()
|
||||
elif new_parent_id != old_parent_id:
|
||||
# Parent ID changed, but we're in the same region
|
||||
new_region_state.handle_object_reparented(obj, old_parent_id=old_parent_id)
|
||||
|
||||
if actually_updated_props and new_region_state is not None:
|
||||
self._run_object_update_hooks(obj, actually_updated_props, update_type)
|
||||
self._run_object_update_hooks(obj, actually_updated_props, update_type, msg)
|
||||
|
||||
def _track_new_object(self, region: RegionObjectsState, obj: Object):
|
||||
def _track_new_object(self, region: RegionObjectsState, obj: Object, msg: Message):
|
||||
region.track_object(obj)
|
||||
self._fullid_lookup[obj.FullID] = obj
|
||||
if obj.PCode == PCode.AVATAR:
|
||||
self._avatar_objects[obj.FullID] = obj
|
||||
self._rebuild_avatar_objects()
|
||||
self._run_object_update_hooks(obj, set(obj.to_dict().keys()), UpdateType.OBJECT_UPDATE)
|
||||
self._run_object_update_hooks(obj, set(obj.to_dict().keys()), ObjectUpdateType.UPDATE, msg)
|
||||
|
||||
def _kill_object_by_local_id(self, region_state: RegionObjectsState, local_id: int):
|
||||
obj = region_state.lookup_localid(local_id)
|
||||
@@ -406,11 +463,11 @@ class ClientWorldObjectManager:
|
||||
# our view of the world then we want to move it to this region.
|
||||
obj = self.lookup_fullid(object_data["FullID"])
|
||||
if obj:
|
||||
self._update_existing_object(obj, object_data, UpdateType.OBJECT_UPDATE)
|
||||
self._update_existing_object(obj, object_data, ObjectUpdateType.UPDATE, msg)
|
||||
else:
|
||||
if region_state is None:
|
||||
continue
|
||||
self._track_new_object(region_state, Object(**object_data))
|
||||
self._track_new_object(region_state, Object(**object_data), msg)
|
||||
msg.meta["ObjectUpdateIDs"] = tuple(seen_locals)
|
||||
|
||||
def _handle_terse_object_update(self, msg: Message):
|
||||
@@ -430,7 +487,7 @@ class ClientWorldObjectManager:
|
||||
# Need the Object as context because decoding state requires PCode.
|
||||
state_deserializer = ObjectStateSerializer.deserialize
|
||||
object_data["State"] = state_deserializer(ctx_obj=obj, val=object_data["State"])
|
||||
self._update_existing_object(obj, object_data, UpdateType.OBJECT_UPDATE)
|
||||
self._update_existing_object(obj, object_data, ObjectUpdateType.UPDATE, msg)
|
||||
else:
|
||||
if region_state:
|
||||
region_state.missing_locals.add(object_data["LocalID"])
|
||||
@@ -458,7 +515,7 @@ class ClientWorldObjectManager:
|
||||
self._update_existing_object(obj, {
|
||||
"UpdateFlags": update_flags,
|
||||
"RegionHandle": handle,
|
||||
}, UpdateType.OBJECT_UPDATE)
|
||||
}, ObjectUpdateType.UPDATE, msg)
|
||||
continue
|
||||
|
||||
cached_obj_data = self._lookup_cache_entry(handle, block["ID"], block["CRC"])
|
||||
@@ -466,7 +523,7 @@ class ClientWorldObjectManager:
|
||||
cached_obj = normalize_object_update_compressed_data(cached_obj_data)
|
||||
cached_obj["UpdateFlags"] = update_flags
|
||||
cached_obj["RegionHandle"] = handle
|
||||
self._track_new_object(region_state, Object(**cached_obj))
|
||||
self._track_new_object(region_state, Object(**cached_obj), msg)
|
||||
continue
|
||||
|
||||
# Don't know about it and wasn't cached.
|
||||
@@ -497,11 +554,11 @@ class ClientWorldObjectManager:
|
||||
LOG.warning(f"Got ObjectUpdateCompressed for unknown region {handle}: {object_data!r}")
|
||||
obj = self.lookup_fullid(object_data["FullID"])
|
||||
if obj:
|
||||
self._update_existing_object(obj, object_data, UpdateType.OBJECT_UPDATE)
|
||||
self._update_existing_object(obj, object_data, ObjectUpdateType.UPDATE, msg)
|
||||
else:
|
||||
if region_state is None:
|
||||
continue
|
||||
self._track_new_object(region_state, Object(**object_data))
|
||||
self._track_new_object(region_state, Object(**object_data), msg)
|
||||
msg.meta["ObjectUpdateIDs"] = tuple(seen_locals)
|
||||
|
||||
def _handle_object_properties_generic(self, packet: Message):
|
||||
@@ -514,7 +571,7 @@ class ClientWorldObjectManager:
|
||||
obj = self.lookup_fullid(block["ObjectID"])
|
||||
if obj:
|
||||
seen_locals.append(obj.LocalID)
|
||||
self._update_existing_object(obj, object_properties, UpdateType.PROPERTIES)
|
||||
self._update_existing_object(obj, object_properties, ObjectUpdateType.PROPERTIES, packet)
|
||||
else:
|
||||
LOG.debug(f"Received {packet.name} for unknown {block['ObjectID']}")
|
||||
packet.meta["ObjectUpdateIDs"] = tuple(seen_locals)
|
||||
@@ -561,18 +618,23 @@ class ClientWorldObjectManager:
|
||||
LOG.debug(f"Received ObjectCost for unknown {object_id}")
|
||||
continue
|
||||
obj.ObjectCosts.update(object_costs)
|
||||
self._run_object_update_hooks(obj, {"ObjectCosts"}, UpdateType.COSTS)
|
||||
self._run_object_update_hooks(obj, {"ObjectCosts"}, ObjectUpdateType.COSTS, None)
|
||||
|
||||
def _run_object_update_hooks(self, obj: Object, updated_props: Set[str], update_type: UpdateType):
|
||||
def _run_object_update_hooks(self, obj: Object, updated_props: Set[str], update_type: ObjectUpdateType,
|
||||
msg: Optional[Message]):
|
||||
region_state = self._get_region_state(obj.RegionHandle)
|
||||
region_state.resolve_futures(obj, update_type)
|
||||
if region_state:
|
||||
region_state.resolve_futures(obj, update_type)
|
||||
else:
|
||||
LOG.warning(f"{obj} not tied to a region state")
|
||||
|
||||
if obj.PCode == PCode.AVATAR and "NameValue" in updated_props:
|
||||
if obj.NameValue:
|
||||
self.name_cache.update(obj.FullID, obj.NameValue.to_dict())
|
||||
self.events.handle(ObjectEvent(obj, updated_props, update_type))
|
||||
|
||||
def _run_kill_object_hooks(self, obj: Object):
|
||||
self.events.handle(ObjectEvent(obj, set(), UpdateType.KILL))
|
||||
self.events.handle(ObjectEvent(obj, set(), ObjectUpdateType.KILL))
|
||||
|
||||
def _rebuild_avatar_objects(self):
|
||||
# Get all avatars known through coarse locations and which region the location was in
|
||||
@@ -642,13 +704,14 @@ class RegionObjectsState:
|
||||
|
||||
__slots__ = (
|
||||
"handle", "missing_locals", "_orphans", "localid_lookup", "coarse_locations",
|
||||
"_object_futures"
|
||||
"_object_futures", "materials"
|
||||
)
|
||||
|
||||
def __init__(self):
|
||||
self.missing_locals = set()
|
||||
self.localid_lookup: Dict[int, Object] = {}
|
||||
self.coarse_locations: Dict[UUID, Vector3] = {}
|
||||
self.materials: MATERIAL_MAP_TYPE = {}
|
||||
self._object_futures: Dict[Tuple[int, int], List[asyncio.Future]] = {}
|
||||
self._orphans: Dict[int, List[int]] = collections.defaultdict(list)
|
||||
|
||||
@@ -661,6 +724,7 @@ class RegionObjectsState:
|
||||
self.coarse_locations.clear()
|
||||
self.missing_locals.clear()
|
||||
self.localid_lookup.clear()
|
||||
self.materials.clear()
|
||||
|
||||
def lookup_localid(self, localid: int) -> Optional[Object]:
|
||||
return self.localid_lookup.get(localid)
|
||||
@@ -754,7 +818,8 @@ class RegionObjectsState:
|
||||
def handle_object_reparented(self, obj: Object, old_parent_id: int):
|
||||
"""Recreate any links to ancestor Objects for obj due to parent changes"""
|
||||
self._unparent_object(obj, old_parent_id)
|
||||
self._parent_object(obj, insert_at_head=True)
|
||||
# Avatars get sent to the _end_ of the child list when reparented
|
||||
self._parent_object(obj, insert_at_head=obj.PCode != PCode.AVATAR)
|
||||
|
||||
def collect_orphans(self, parent_localid: int) -> Sequence[int]:
|
||||
"""Take ownership of any orphan IDs belonging to parent_localid"""
|
||||
@@ -779,7 +844,7 @@ class RegionObjectsState:
|
||||
del self._orphans[parent_id]
|
||||
return removed
|
||||
|
||||
def register_future(self, local_id: int, future_type: UpdateType) -> asyncio.Future[Object]:
|
||||
def register_future(self, local_id: int, future_type: ObjectUpdateType) -> asyncio.Future[Object]:
|
||||
fut = asyncio.Future()
|
||||
fut_key = (local_id, future_type)
|
||||
local_futs = self._object_futures.get(fut_key, [])
|
||||
@@ -788,7 +853,7 @@ class RegionObjectsState:
|
||||
fut.add_done_callback(local_futs.remove)
|
||||
return fut
|
||||
|
||||
def resolve_futures(self, obj: Object, update_type: UpdateType):
|
||||
def resolve_futures(self, obj: Object, update_type: ObjectUpdateType):
|
||||
futures = self._object_futures.get((obj.LocalID, update_type), [])
|
||||
for fut in futures[:]:
|
||||
fut.set_result(obj)
|
||||
|
||||
251
hippolyzer/lib/client/parcel_manager.py
Normal file
251
hippolyzer/lib/client/parcel_manager.py
Normal file
@@ -0,0 +1,251 @@
|
||||
import asyncio
|
||||
import dataclasses
|
||||
import logging
|
||||
from typing import *
|
||||
|
||||
import numpy as np
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID, Vector3, Vector2
|
||||
from hippolyzer.lib.base.message.message import Message, Block
|
||||
from hippolyzer.lib.base.templates import ParcelGridFlags, ParcelFlags
|
||||
from hippolyzer.lib.client.state import BaseClientRegion
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Parcel:
|
||||
local_id: int
|
||||
name: str
|
||||
flags: ParcelFlags
|
||||
group_id: UUID
|
||||
# TODO: More properties
|
||||
|
||||
|
||||
class ParcelManager:
|
||||
# We expect to receive this number of ParcelOverlay messages
|
||||
NUM_CHUNKS = 4
|
||||
# No, we don't support varregion or whatever.
|
||||
REGION_SIZE = 256
|
||||
# Basically, the minimum parcel size is 4 on either axis so each "point" in the
|
||||
# ParcelOverlay represents an area this size
|
||||
GRID_STEP = 4
|
||||
GRIDS_PER_EDGE = REGION_SIZE // GRID_STEP
|
||||
|
||||
def __init__(self, region: BaseClientRegion):
|
||||
# dimensions are south to north, west to east
|
||||
self.overlay = np.zeros((self.GRIDS_PER_EDGE, self.GRIDS_PER_EDGE), dtype=np.uint8)
|
||||
# 1-indexed parcel list index
|
||||
self.parcel_indices = np.zeros((self.GRIDS_PER_EDGE, self.GRIDS_PER_EDGE), dtype=np.uint16)
|
||||
self.parcels: List[Optional[Parcel]] = []
|
||||
self.overlay_chunks: List[Optional[bytes]] = [None] * self.NUM_CHUNKS
|
||||
self.overlay_complete = asyncio.Event()
|
||||
self.parcels_downloaded = asyncio.Event()
|
||||
self._parcels_dirty: bool = True
|
||||
self._region = region
|
||||
self._next_seq = 1
|
||||
self._region.message_handler.subscribe("ParcelOverlay", self._handle_parcel_overlay)
|
||||
|
||||
def _handle_parcel_overlay(self, message: Message):
|
||||
self.add_overlay_chunk(message["ParcelData"]["Data"], message["ParcelData"]["SequenceID"])
|
||||
|
||||
def add_overlay_chunk(self, chunk: bytes, chunk_num: int) -> bool:
|
||||
self.overlay_chunks[chunk_num] = chunk
|
||||
# Still have some pending chunks, don't try to parse this yet
|
||||
if not all(self.overlay_chunks):
|
||||
return False
|
||||
|
||||
new_overlay_data = b"".join(self.overlay_chunks)
|
||||
self.overlay_chunks = [None] * self.NUM_CHUNKS
|
||||
self._parcels_dirty = False
|
||||
if new_overlay_data != self.overlay.data[:]:
|
||||
# If the raw data doesn't match, then we have to parse again
|
||||
new_data = np.frombuffer(new_overlay_data, dtype=np.uint8).reshape(self.overlay.shape)
|
||||
np.copyto(self.overlay, new_data)
|
||||
self._parse_overlay()
|
||||
# We could optimize this by just marking specific squares dirty
|
||||
# if the parcel indices have changed between parses, but I don't care
|
||||
# to do that.
|
||||
self._parcels_dirty = True
|
||||
self.parcels_downloaded.clear()
|
||||
if not self.overlay_complete.is_set():
|
||||
self.overlay_complete.set()
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def _pos_to_grid_coords(cls, pos: Vector3) -> Tuple[int, int]:
|
||||
return round(pos.Y // cls.GRID_STEP), round(pos.X // cls.GRID_STEP)
|
||||
|
||||
def _parse_overlay(self):
|
||||
# Zero out all parcel indices
|
||||
self.parcel_indices[:, :] = 0
|
||||
next_parcel_idx = 1
|
||||
for y in range(0, self.GRIDS_PER_EDGE):
|
||||
for x in range(0, self.GRIDS_PER_EDGE):
|
||||
# We already have a parcel index for this grid, continue
|
||||
if self.parcel_indices[y, x]:
|
||||
continue
|
||||
|
||||
# Fill all adjacent grids with this parcel index
|
||||
self._flood_fill_parcel_index(y, x, next_parcel_idx)
|
||||
# SL doesn't allow disjoint grids to be part of the same parcel, so
|
||||
# whatever grid we find next without a parcel index must be a new parcel
|
||||
next_parcel_idx += 1
|
||||
|
||||
# Should have found at least one parcel
|
||||
assert next_parcel_idx >= 2
|
||||
|
||||
# Have a different number of parcels now, we can't use the existing parcel objects
|
||||
# because it's unlikely that just parcel boundaries have changed.
|
||||
if len(self.parcels) != next_parcel_idx - 1:
|
||||
# We don't know about any of these parcels yet, fill with none
|
||||
self.parcels = [None] * (next_parcel_idx - 1)
|
||||
|
||||
def _flood_fill_parcel_index(self, start_y, start_x, parcel_idx):
|
||||
"""Flood fill all neighboring grids with the parcel index, being mindful of parcel boundaries"""
|
||||
# We know the start grid is assigned to this parcel index
|
||||
self.parcel_indices[start_y, start_x] = parcel_idx
|
||||
# Queue of grids to test the neighbors of, start with the start grid.
|
||||
neighbor_test_queue: List[Tuple[int, int]] = [(start_y, start_x)]
|
||||
|
||||
while neighbor_test_queue:
|
||||
to_test = neighbor_test_queue.pop(0)
|
||||
test_grid = self.overlay[to_test]
|
||||
|
||||
for direction in ((-1, 0), (1, 0), (0, -1), (0, 1)):
|
||||
new_pos = to_test[0] + direction[0], to_test[1] + direction[1]
|
||||
|
||||
if any(x < 0 or x >= self.GRIDS_PER_EDGE for x in new_pos):
|
||||
# Outside bounds
|
||||
continue
|
||||
if self.parcel_indices[new_pos]:
|
||||
# Already set, skip
|
||||
continue
|
||||
|
||||
if direction[0] == -1 and test_grid & ParcelGridFlags.SOUTH_LINE:
|
||||
# Test grid is already on a south line, can't go south.
|
||||
continue
|
||||
if direction[1] == -1 and test_grid & ParcelGridFlags.WEST_LINE:
|
||||
# Test grid is already on a west line, can't go west.
|
||||
continue
|
||||
|
||||
grid = self.overlay[new_pos]
|
||||
|
||||
if direction[0] == 1 and grid & ParcelGridFlags.SOUTH_LINE:
|
||||
# Hit a south line going north, this is outside the current parcel
|
||||
continue
|
||||
if direction[1] == 1 and grid & ParcelGridFlags.WEST_LINE:
|
||||
# Hit a west line going east, this is outside the current parcel
|
||||
continue
|
||||
# This grid is within the current parcel, set the parcel index
|
||||
self.parcel_indices[new_pos] = parcel_idx
|
||||
# Append the grid to the neighbour testing queue
|
||||
neighbor_test_queue.append(new_pos)
|
||||
|
||||
async def request_dirty_parcels(self) -> Tuple[Parcel, ...]:
|
||||
if self._parcels_dirty:
|
||||
return await self.request_all_parcels()
|
||||
return tuple(self.parcels)
|
||||
|
||||
async def request_all_parcels(self) -> Tuple[Parcel, ...]:
|
||||
await self.overlay_complete.wait()
|
||||
# Because of how we build up the parcel index map, it's safe for us to
|
||||
# do this instead of keeping track of seen IDs in a set or similar
|
||||
last_seen_parcel_index = 0
|
||||
futs = []
|
||||
for y in range(0, self.GRIDS_PER_EDGE):
|
||||
for x in range(0, self.GRIDS_PER_EDGE):
|
||||
parcel_index = self.parcel_indices[y, x]
|
||||
assert parcel_index != 0
|
||||
if parcel_index <= last_seen_parcel_index:
|
||||
continue
|
||||
assert parcel_index == last_seen_parcel_index + 1
|
||||
last_seen_parcel_index = parcel_index
|
||||
# Request a position within the parcel
|
||||
futs.append(self.request_parcel_properties(
|
||||
Vector2(x * self.GRID_STEP + 1.0, y * self.GRID_STEP + 1.0)
|
||||
))
|
||||
|
||||
# Wait for all parcel properties to come in
|
||||
await asyncio.gather(*futs)
|
||||
self.parcels_downloaded.set()
|
||||
self._parcels_dirty = False
|
||||
return tuple(self.parcels)
|
||||
|
||||
async def request_parcel_properties(self, pos: Vector2) -> Parcel:
|
||||
await self.overlay_complete.wait()
|
||||
seq_id = self._next_seq
|
||||
# Register a wait on a ParcelProperties matching this seq
|
||||
parcel_props_fut = self._region.message_handler.wait_for(
|
||||
("ParcelProperties",),
|
||||
predicate=lambda msg: msg["ParcelData"]["SequenceID"] == seq_id,
|
||||
timeout=10.0,
|
||||
)
|
||||
# We don't care about when we receive an ack, we only care about when we receive the parcel props
|
||||
_ = self._region.circuit.send_reliable(Message(
|
||||
"ParcelPropertiesRequest",
|
||||
Block("AgentData", AgentID=self._region.session().agent_id, SessionID=self._region.session().id),
|
||||
Block(
|
||||
"ParcelData",
|
||||
SequenceID=seq_id,
|
||||
West=pos.X,
|
||||
East=pos.X,
|
||||
North=pos.Y,
|
||||
South=pos.Y,
|
||||
# What does this even mean?
|
||||
SnapSelection=0,
|
||||
),
|
||||
))
|
||||
self._next_seq += 1
|
||||
|
||||
return self._process_parcel_properties(await parcel_props_fut, pos)
|
||||
|
||||
def _process_parcel_properties(self, parcel_props: Message, pos: Optional[Vector2] = None) -> Parcel:
|
||||
data_block = parcel_props["ParcelData"][0]
|
||||
grid_coord = None
|
||||
# Parcel indices are one-indexed, convert to zero-indexed.
|
||||
if pos is not None:
|
||||
# We have a pos, figure out where in the grid we should look for the parcel index
|
||||
grid_coord = self._pos_to_grid_coords(pos)
|
||||
else:
|
||||
# Need to look at the parcel bitmap to figure out a valid grid coord.
|
||||
# This is a boolean array where each bit says whether the parcel occupies that grid.
|
||||
parcel_bitmap = data_block.deserialize_var("Bitmap")
|
||||
|
||||
for y in range(self.GRIDS_PER_EDGE):
|
||||
for x in range(self.GRIDS_PER_EDGE):
|
||||
if parcel_bitmap[y, x]:
|
||||
# This is the first grid the parcel occupies per the bitmap
|
||||
grid_coord = y, x
|
||||
break
|
||||
if grid_coord:
|
||||
break
|
||||
|
||||
parcel = Parcel(
|
||||
local_id=data_block["LocalID"],
|
||||
name=data_block["Name"],
|
||||
flags=ParcelFlags(data_block["ParcelFlags"]),
|
||||
group_id=data_block["GroupID"],
|
||||
# Parcel UUID isn't in this response :/
|
||||
)
|
||||
|
||||
# I guess the bitmap _could_ be empty, but probably not.
|
||||
if grid_coord is not None:
|
||||
parcel_idx = self.parcel_indices[grid_coord] - 1
|
||||
if len(self.parcels) > parcel_idx >= 0:
|
||||
# Okay, parcels list is sane, place the parcel in there.
|
||||
self.parcels[parcel_idx] = parcel
|
||||
else:
|
||||
LOG.warning(f"Received ParcelProperties with incomplete overlay for {grid_coord!r}")
|
||||
|
||||
return parcel
|
||||
|
||||
async def get_parcel_at(self, pos: Vector2, request_if_missing: bool = True) -> Optional[Parcel]:
|
||||
grid_coord = self._pos_to_grid_coords(pos)
|
||||
parcel = None
|
||||
if parcel_idx := self.parcel_indices[grid_coord]:
|
||||
parcel = self.parcels[parcel_idx - 1]
|
||||
if request_if_missing and parcel is None:
|
||||
return await self.request_parcel_properties(pos)
|
||||
return parcel
|
||||
51
hippolyzer/lib/client/rlv.py
Normal file
51
hippolyzer/lib/client/rlv.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from typing import NamedTuple, List, Sequence
|
||||
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.templates import ChatType
|
||||
|
||||
|
||||
class RLVCommand(NamedTuple):
|
||||
behaviour: str
|
||||
param: str
|
||||
options: List[str]
|
||||
|
||||
|
||||
class RLVParser:
|
||||
@staticmethod
|
||||
def is_rlv_message(msg: Message) -> bool:
|
||||
chat: str = msg["ChatData"]["Message"]
|
||||
chat_type: int = msg["ChatData"]["ChatType"]
|
||||
return chat and chat.startswith("@") and chat_type == ChatType.OWNER
|
||||
|
||||
@staticmethod
|
||||
def parse_chat(chat: str) -> List[RLVCommand]:
|
||||
assert chat.startswith("@")
|
||||
chat = chat.lstrip("@")
|
||||
commands = []
|
||||
for command_str in chat.split(","):
|
||||
if not command_str:
|
||||
continue
|
||||
# RLV-style command, `<cmd>(:<option1>;<option2>)?(=<param>)?`
|
||||
# Roughly (?<behaviour>[^:=]+)(:(?<option>[^=]*))?=(?<param>\w+)
|
||||
options, _, param = command_str.partition("=")
|
||||
behaviour, _, options = options.partition(":")
|
||||
# TODO: Not always correct, commands can specify their own parsing for the option field
|
||||
# maybe special-case these?
|
||||
options = options.split(";") if options else []
|
||||
commands.append(RLVCommand(behaviour, param, options))
|
||||
return commands
|
||||
|
||||
@staticmethod
|
||||
def format_chat(commands: Sequence[RLVCommand]) -> str:
|
||||
assert commands
|
||||
chat = ""
|
||||
for command in commands:
|
||||
if chat:
|
||||
chat += ","
|
||||
|
||||
chat += command.behaviour
|
||||
if command.options:
|
||||
chat += ":" + ";".join(command.options)
|
||||
if command.param:
|
||||
chat += "=" + command.param
|
||||
return "@" + chat
|
||||
@@ -4,17 +4,21 @@ Base classes for common session-related state shared between clients and proxies
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
import logging
|
||||
import weakref
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.message.circuit import ConnectionHolder
|
||||
import multidict
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID, Vector3
|
||||
from hippolyzer.lib.base.message.circuit import ConnectionHolder, Circuit
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.message.message_handler import MessageHandler
|
||||
from hippolyzer.lib.base.network.caps_client import CapsClient
|
||||
from hippolyzer.lib.base.network.transport import ADDR_TUPLE
|
||||
from hippolyzer.lib.base.objects import handle_to_global_pos
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from hippolyzer.lib.client.object_manager import ClientObjectManager, ClientWorldObjectManager
|
||||
from hippolyzer.lib.client.object_manager import ClientObjectManager, ClientWorldObjectManager
|
||||
|
||||
|
||||
class BaseClientRegion(ConnectionHolder, abc.ABC):
|
||||
@@ -24,6 +28,53 @@ class BaseClientRegion(ConnectionHolder, abc.ABC):
|
||||
session: Callable[[], BaseClientSession]
|
||||
objects: ClientObjectManager
|
||||
caps_client: CapsClient
|
||||
cap_urls: multidict.MultiDict[str]
|
||||
circuit_addr: ADDR_TUPLE
|
||||
circuit: Optional[Circuit]
|
||||
_name: Optional[str]
|
||||
|
||||
def __init__(self):
|
||||
self._name = None
|
||||
self.circuit = None
|
||||
|
||||
@abc.abstractmethod
|
||||
def update_caps(self, caps: Mapping[str, str]) -> None:
|
||||
pass
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
if self._name:
|
||||
return self._name
|
||||
return "Pending %r" % (self.circuit_addr,)
|
||||
|
||||
@name.setter
|
||||
def name(self, val):
|
||||
self._name = val
|
||||
|
||||
@property
|
||||
def global_pos(self) -> Vector3:
|
||||
if self.handle is None:
|
||||
raise ValueError("Can't determine global region position without handle")
|
||||
return handle_to_global_pos(self.handle)
|
||||
|
||||
@property
|
||||
def is_alive(self):
|
||||
if not self.circuit:
|
||||
return False
|
||||
return self.circuit.is_alive
|
||||
|
||||
def mark_dead(self):
|
||||
logging.info("Marking %r dead" % self)
|
||||
if self.circuit:
|
||||
self.circuit.is_alive = False
|
||||
self.objects.clear()
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s (%r)>" % (self.__class__.__name__, self.name, self.handle)
|
||||
|
||||
|
||||
class BaseClientSessionManager:
|
||||
pass
|
||||
|
||||
|
||||
class BaseClientSession(abc.ABC):
|
||||
@@ -31,8 +82,104 @@ class BaseClientSession(abc.ABC):
|
||||
id: UUID
|
||||
agent_id: UUID
|
||||
secure_session_id: UUID
|
||||
active_group: UUID
|
||||
groups: Set[UUID]
|
||||
message_handler: MessageHandler[Message, str]
|
||||
regions: Sequence[BaseClientRegion]
|
||||
regions: MutableSequence[BaseClientRegion]
|
||||
region_by_handle: Callable[[int], Optional[BaseClientRegion]]
|
||||
region_by_circuit_addr: Callable[[ADDR_TUPLE], Optional[BaseClientRegion]]
|
||||
objects: ClientWorldObjectManager
|
||||
login_data: Dict[str, Any]
|
||||
REGION_CLS = Type[BaseClientRegion]
|
||||
|
||||
def __init__(self, id, secure_session_id, agent_id, circuit_code,
|
||||
session_manager: Optional[BaseClientSessionManager], login_data=None):
|
||||
self.login_data = login_data or {}
|
||||
self.pending = True
|
||||
self.id: UUID = id
|
||||
self.secure_session_id: UUID = secure_session_id
|
||||
self.agent_id: UUID = agent_id
|
||||
self.circuit_code = circuit_code
|
||||
self.global_caps = {}
|
||||
self.session_manager = session_manager
|
||||
self.active_group: UUID = UUID.ZERO
|
||||
self.groups: Set[UUID] = set()
|
||||
self.regions = []
|
||||
self._main_region = None
|
||||
self.message_handler: MessageHandler[Message, str] = MessageHandler()
|
||||
super().__init__()
|
||||
|
||||
@classmethod
|
||||
def from_login_data(cls, login_data, session_manager):
|
||||
sess = cls(
|
||||
id=UUID(login_data["session_id"]),
|
||||
secure_session_id=UUID(login_data["secure_session_id"]),
|
||||
agent_id=UUID(login_data["agent_id"]),
|
||||
circuit_code=int(login_data["circuit_code"]),
|
||||
session_manager=session_manager,
|
||||
login_data=login_data,
|
||||
)
|
||||
appearance_service = login_data.get("agent_appearance_service")
|
||||
map_image_service = login_data.get("map-server-url")
|
||||
if appearance_service:
|
||||
sess.global_caps["AppearanceService"] = appearance_service
|
||||
if map_image_service:
|
||||
sess.global_caps["MapImageService"] = map_image_service
|
||||
# Login data also has details about the initial sim
|
||||
sess.register_region(
|
||||
circuit_addr=(login_data["sim_ip"], login_data["sim_port"]),
|
||||
handle=(login_data["region_x"] << 32) | login_data["region_y"],
|
||||
seed_url=login_data["seed_capability"],
|
||||
)
|
||||
return sess
|
||||
|
||||
def register_region(self, circuit_addr: Optional[ADDR_TUPLE] = None, seed_url: Optional[str] = None,
|
||||
handle: Optional[int] = None) -> BaseClientRegion:
|
||||
if not any((circuit_addr, seed_url)):
|
||||
raise ValueError("One of circuit_addr and seed_url must be defined!")
|
||||
|
||||
for region in self.regions:
|
||||
if region.circuit_addr == circuit_addr:
|
||||
if seed_url and region.cap_urls.get("Seed") != seed_url:
|
||||
region.update_caps({"Seed": seed_url})
|
||||
if handle:
|
||||
region.handle = handle
|
||||
return region
|
||||
if seed_url and region.cap_urls.get("Seed") == seed_url:
|
||||
return region
|
||||
|
||||
if not circuit_addr:
|
||||
raise ValueError("Can't create region without circuit addr!")
|
||||
|
||||
logging.info("Registering region for %r" % (circuit_addr,))
|
||||
region = self.REGION_CLS(circuit_addr, seed_url, self, handle=handle)
|
||||
self.regions.append(region)
|
||||
return region
|
||||
|
||||
@property
|
||||
def main_region(self) -> Optional[BaseClientRegion]:
|
||||
if self._main_region and self._main_region() in self.regions:
|
||||
return self._main_region()
|
||||
return None
|
||||
|
||||
@main_region.setter
|
||||
def main_region(self, val: BaseClientRegion):
|
||||
self._main_region = weakref.ref(val)
|
||||
|
||||
def transaction_to_assetid(self, transaction_id: UUID):
|
||||
return UUID.combine(transaction_id, self.secure_session_id)
|
||||
|
||||
def region_by_circuit_addr(self, circuit_addr) -> Optional[BaseClientRegion]:
|
||||
for region in self.regions:
|
||||
if region.circuit_addr == circuit_addr and region.circuit:
|
||||
return region
|
||||
return None
|
||||
|
||||
def region_by_handle(self, handle: int) -> Optional[BaseClientRegion]:
|
||||
for region in self.regions:
|
||||
if region.handle == handle:
|
||||
return region
|
||||
return None
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s>" % (self.__class__.__name__, self.id)
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import *
|
||||
import abc
|
||||
import copy
|
||||
import dataclasses
|
||||
import multiprocessing
|
||||
import pickle
|
||||
import secrets
|
||||
import warnings
|
||||
from typing import *
|
||||
|
||||
import outleap
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID, Vector3
|
||||
from hippolyzer.lib.base.message.message import Block, Message
|
||||
@@ -14,10 +17,11 @@ from hippolyzer.lib.proxy import addon_ctx
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.http_flow import HippoHTTPFlow
|
||||
from hippolyzer.lib.base.network.transport import UDPPacket, Direction
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
from hippolyzer.lib.proxy.sessions import SessionManager, Session
|
||||
from hippolyzer.lib.proxy.task_scheduler import TaskLifeScope
|
||||
from hippolyzer.lib.base.templates import ChatSourceType, ChatType
|
||||
if TYPE_CHECKING:
|
||||
from hippolyzer.lib.proxy.sessions import SessionManager, Session
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
|
||||
|
||||
class AssetAliasTracker:
|
||||
@@ -73,17 +77,17 @@ def show_message(text, session=None) -> None:
|
||||
direction=Direction.IN,
|
||||
)
|
||||
if session:
|
||||
session.main_region.circuit.send_message(message)
|
||||
session.main_region.circuit.send(message)
|
||||
else:
|
||||
for session in AddonManager.SESSION_MANAGER.sessions:
|
||||
session.main_region.circuit.send_message(copy.copy(message))
|
||||
session.main_region.circuit.send(copy.copy(message))
|
||||
|
||||
|
||||
def send_chat(message: Union[bytes, str], channel=0, chat_type=ChatType.NORMAL, session=None):
|
||||
session = session or addon_ctx.session.get(None) or None
|
||||
if not session:
|
||||
raise RuntimeError("Tried to send chat without session")
|
||||
session.main_region.circuit.send_message(Message(
|
||||
session.main_region.circuit.send(Message(
|
||||
"ChatFromViewer",
|
||||
Block(
|
||||
"AgentData",
|
||||
@@ -99,36 +103,32 @@ def send_chat(message: Union[bytes, str], channel=0, chat_type=ChatType.NORMAL,
|
||||
))
|
||||
|
||||
|
||||
def ais_item_to_inventory_data(ais_item: dict):
|
||||
return Block(
|
||||
"InventoryData",
|
||||
ItemID=ais_item["item_id"],
|
||||
FolderID=ais_item["parent_id"],
|
||||
CallbackID=0,
|
||||
CreatorID=ais_item["permissions"]["creator_id"],
|
||||
OwnerID=ais_item["permissions"]["owner_id"],
|
||||
GroupID=ais_item["permissions"]["group_id"],
|
||||
BaseMask=ais_item["permissions"]["base_mask"],
|
||||
OwnerMask=ais_item["permissions"]["owner_mask"],
|
||||
GroupMask=ais_item["permissions"]["group_mask"],
|
||||
EveryoneMask=ais_item["permissions"]["everyone_mask"],
|
||||
NextOwnerMask=ais_item["permissions"]["next_owner_mask"],
|
||||
GroupOwned=0,
|
||||
AssetID=ais_item["asset_id"],
|
||||
Type=ais_item["type"],
|
||||
InvType=ais_item["inv_type"],
|
||||
Flags=ais_item["flags"],
|
||||
SaleType=ais_item["sale_info"]["sale_type"],
|
||||
SalePrice=ais_item["sale_info"]["sale_price"],
|
||||
Name=ais_item["name"],
|
||||
Description=ais_item["desc"],
|
||||
CreationDate=ais_item["created_at"],
|
||||
# Meaningless here
|
||||
CRC=secrets.randbits(32),
|
||||
)
|
||||
class MetaBaseAddon(abc.ABCMeta):
|
||||
"""
|
||||
Metaclass for BaseAddon that prevents class member assignments from clobbering descriptors
|
||||
|
||||
Without this things like:
|
||||
|
||||
class Foo(BaseAddon):
|
||||
bar: int = GlobalProperty(0)
|
||||
|
||||
Foo.bar = 2
|
||||
|
||||
Won't work as you expect!
|
||||
"""
|
||||
def __setattr__(self, key: str, value):
|
||||
try:
|
||||
existing = object.__getattribute__(self, key)
|
||||
if existing and isinstance(existing, BaseAddonProperty):
|
||||
existing.__set__(self, value)
|
||||
return
|
||||
except AttributeError:
|
||||
# If the attribute doesn't exist then it's fine to use the base setattr.
|
||||
pass
|
||||
super().__setattr__(key, value)
|
||||
|
||||
|
||||
class BaseAddon(abc.ABC):
|
||||
class BaseAddon(metaclass=MetaBaseAddon):
|
||||
def _schedule_task(self, coro: Coroutine, session=None,
|
||||
region_scoped=False, session_scoped=True, addon_scoped=True):
|
||||
session = session or addon_ctx.session.get(None) or None
|
||||
@@ -172,7 +172,7 @@ class BaseAddon(abc.ABC):
|
||||
pass
|
||||
|
||||
def handle_object_updated(self, session: Session, region: ProxiedRegion,
|
||||
obj: Object, updated_props: Set[str]):
|
||||
obj: Object, updated_props: Set[str], msg: Optional[Message]):
|
||||
pass
|
||||
|
||||
def handle_object_killed(self, session: Session, region: ProxiedRegion, obj: Object):
|
||||
@@ -181,20 +181,26 @@ class BaseAddon(abc.ABC):
|
||||
def handle_region_changed(self, session: Session, region: ProxiedRegion):
|
||||
pass
|
||||
|
||||
def handle_region_registered(self, session: Session, region: ProxiedRegion):
|
||||
pass
|
||||
|
||||
def handle_circuit_created(self, session: Session, region: ProxiedRegion):
|
||||
pass
|
||||
|
||||
def handle_rlv_command(self, session: Session, region: ProxiedRegion, source: UUID,
|
||||
cmd: str, options: List[str], param: str):
|
||||
behaviour: str, options: List[str], param: str):
|
||||
pass
|
||||
|
||||
def handle_proxied_packet(self, session_manager: SessionManager, packet: UDPPacket,
|
||||
session: Optional[Session], region: Optional[ProxiedRegion]):
|
||||
pass
|
||||
|
||||
async def handle_leap_client_added(self, session_manager: SessionManager, leap_client: outleap.LEAPClient):
|
||||
pass
|
||||
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_U = TypeVar("_U", Session, SessionManager)
|
||||
_U = TypeVar("_U", "Session", "SessionManager")
|
||||
|
||||
|
||||
class BaseAddonProperty(abc.ABC, Generic[_T, _U]):
|
||||
@@ -205,13 +211,17 @@ class BaseAddonProperty(abc.ABC, Generic[_T, _U]):
|
||||
session_manager.addon_ctx dict, without any namespacing. Can be accessed either
|
||||
through `AddonClass.property_name` or `addon_instance.property_name`.
|
||||
"""
|
||||
__slots__ = ("name", "default")
|
||||
__slots__ = ("name", "default", "_owner")
|
||||
|
||||
def __init__(self, default=dataclasses.MISSING):
|
||||
self.default = default
|
||||
self._owner = None
|
||||
|
||||
def __set_name__(self, owner, name: str):
|
||||
self.name = name
|
||||
# Keep track of which addon "owns" this property so that we can shove
|
||||
# the data in a bucket specific to that addon name.
|
||||
self._owner = owner
|
||||
|
||||
def _make_default(self) -> _T:
|
||||
if self.default is not dataclasses.MISSING:
|
||||
@@ -229,21 +239,23 @@ class BaseAddonProperty(abc.ABC, Generic[_T, _U]):
|
||||
if ctx_obj is None:
|
||||
raise AttributeError(
|
||||
f"{self.__class__} {self.name} accessed outside proper context")
|
||||
addon_state = ctx_obj.addon_ctx[self._owner.__name__]
|
||||
# Set a default if we have one, otherwise let the keyerror happen.
|
||||
# Maybe we should do this at addon initialization instead of on get.
|
||||
if self.name not in ctx_obj.addon_ctx:
|
||||
if self.name not in addon_state:
|
||||
default = self._make_default()
|
||||
if default is not dataclasses.MISSING:
|
||||
ctx_obj.addon_ctx[self.name] = default
|
||||
addon_state[self.name] = default
|
||||
else:
|
||||
raise AttributeError(f"{self.name} is not set")
|
||||
return ctx_obj.addon_ctx[self.name]
|
||||
return addon_state[self.name]
|
||||
|
||||
def __set__(self, _obj, value: _T) -> None:
|
||||
self._get_context_obj().addon_ctx[self.name] = value
|
||||
addon_state = self._get_context_obj().addon_ctx[self._owner.__name__]
|
||||
addon_state[self.name] = value
|
||||
|
||||
|
||||
class SessionProperty(BaseAddonProperty[_T, Session]):
|
||||
class SessionProperty(BaseAddonProperty[_T, "Session"]):
|
||||
"""
|
||||
Property tied to the current session context
|
||||
|
||||
@@ -253,7 +265,7 @@ class SessionProperty(BaseAddonProperty[_T, Session]):
|
||||
return addon_ctx.session.get()
|
||||
|
||||
|
||||
class GlobalProperty(BaseAddonProperty[_T, SessionManager]):
|
||||
class GlobalProperty(BaseAddonProperty[_T, "SessionManager"]):
|
||||
"""
|
||||
Property tied to the global SessionManager context
|
||||
|
||||
|
||||
@@ -15,9 +15,13 @@ import time
|
||||
from types import ModuleType
|
||||
from typing import *
|
||||
|
||||
import outleap
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.helpers import get_mtime
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.network.transport import UDPPacket
|
||||
from hippolyzer.lib.client.rlv import RLVParser
|
||||
from hippolyzer.lib.proxy import addon_ctx
|
||||
from hippolyzer.lib.proxy.task_scheduler import TaskLifeScope, TaskScheduler
|
||||
|
||||
@@ -31,13 +35,6 @@ if TYPE_CHECKING:
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_mtime(path):
|
||||
try:
|
||||
return os.stat(path).st_mtime
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
class BaseInteractionManager:
|
||||
@abc.abstractmethod
|
||||
async def open_dir(self, caption: str = '', directory: str = '', filter_str: str = '') -> Optional[str]:
|
||||
@@ -64,6 +61,15 @@ class BaseInteractionManager:
|
||||
return None
|
||||
|
||||
|
||||
# Used to initialize a REPL environment with commonly desired helpers
|
||||
REPL_INITIALIZER = r"""
|
||||
from hippolyzer.lib.base.datatypes import *
|
||||
from hippolyzer.lib.base.templates import *
|
||||
from hippolyzer.lib.base.message.message import Block, Message, Direction
|
||||
from hippolyzer.lib.proxy.addon_utils import send_chat, show_message
|
||||
"""
|
||||
|
||||
|
||||
class AddonManager:
|
||||
COMMAND_CHANNEL = 524
|
||||
|
||||
@@ -139,6 +145,16 @@ class AddonManager:
|
||||
if _locals is None:
|
||||
_locals = stack.frame.f_locals
|
||||
|
||||
init_globals = {}
|
||||
exec(REPL_INITIALIZER, init_globals, None)
|
||||
# We're modifying the globals of the caller, be careful of things we imported
|
||||
# for the REPL initializer clobber things that already exist in the caller's globals.
|
||||
# Making our own mutable copy of the globals dict, mutating that and then passing it
|
||||
# to embed() is not an option due to https://github.com/prompt-toolkit/ptpython/issues/279
|
||||
for global_name, global_val in init_globals.items():
|
||||
if global_name not in _globals:
|
||||
_globals[global_name] = global_val
|
||||
|
||||
async def _wrapper():
|
||||
coro: Coroutine = ptpython.repl.embed( # noqa: the type signature lies
|
||||
globals=_globals,
|
||||
@@ -159,7 +175,10 @@ class AddonManager:
|
||||
def load_addon_from_path(cls, path, reload=False, raise_exceptions=True):
|
||||
path = pathlib.Path(path).absolute()
|
||||
mod_name = "hippolyzer.user_addon_%s" % path.stem
|
||||
cls.BASE_ADDON_SPECS.append(importlib.util.spec_from_file_location(mod_name, path))
|
||||
spec = importlib.util.spec_from_file_location(mod_name, path)
|
||||
if not spec:
|
||||
raise ValueError(f"Unable to load {path}")
|
||||
cls.BASE_ADDON_SPECS.append(spec)
|
||||
addon_dir = os.path.realpath(pathlib.Path(path).parent.absolute())
|
||||
|
||||
if addon_dir not in sys.path:
|
||||
@@ -186,9 +205,9 @@ class AddonManager:
|
||||
@classmethod
|
||||
def _check_hotreloads(cls):
|
||||
"""Mark addons that rely on changed files for reloading"""
|
||||
for filename, importers in cls.HOTRELOAD_IMPORTERS.items():
|
||||
mtime = _get_mtime(filename)
|
||||
if not mtime or mtime == cls.FILE_MTIMES.get(filename, None):
|
||||
for file_path, importers in cls.HOTRELOAD_IMPORTERS.items():
|
||||
mtime = get_mtime(file_path)
|
||||
if not mtime or mtime == cls.FILE_MTIMES.get(file_path, None):
|
||||
continue
|
||||
|
||||
# Mark anything that imported this as dirty too, handling circular
|
||||
@@ -207,16 +226,21 @@ class AddonManager:
|
||||
|
||||
_dirty_importers(importers)
|
||||
|
||||
if file_path not in cls.BASE_ADDON_SPECS:
|
||||
# Make sure we won't reload importers in a loop if this is actually something
|
||||
# that was dynamically imported, where `hot_reload()` might not be called again!
|
||||
cls.FILE_MTIMES[file_path] = mtime
|
||||
|
||||
@classmethod
|
||||
def hot_reload(cls, mod: Any, require_addons_loaded=False):
|
||||
# Solely to trick the type checker because ModuleType doesn't apply where it should
|
||||
# and Protocols aren't well supported yet.
|
||||
# and Protocols aren't well-supported yet.
|
||||
imported_mod: ModuleType = mod
|
||||
imported_file = imported_mod.__file__
|
||||
# Mark the caller as having imported (and being dependent on) `module`
|
||||
stack = inspect.stack()[1]
|
||||
cls.HOTRELOAD_IMPORTERS[imported_file].add(stack.filename)
|
||||
cls.FILE_MTIMES[imported_file] = _get_mtime(imported_file)
|
||||
cls.FILE_MTIMES[imported_file] = get_mtime(imported_file)
|
||||
|
||||
importing_spec = next((s for s in cls.BASE_ADDON_SPECS if s.origin == stack.filename), None)
|
||||
imported_spec = next((s for s in cls.BASE_ADDON_SPECS if s.origin == imported_file), None)
|
||||
@@ -262,9 +286,12 @@ class AddonManager:
|
||||
|
||||
new_addons = {}
|
||||
for spec in cls.BASE_ADDON_SPECS[:]:
|
||||
previous_mod = cls.FRESH_ADDON_MODULES.get(spec.name)
|
||||
# Whether we've EVER successfully loaded this module,
|
||||
# There may be a `None` entry in the dict if that's the case.
|
||||
had_mod = spec.name in cls.FRESH_ADDON_MODULES
|
||||
try:
|
||||
mtime = _get_mtime(spec.origin)
|
||||
mtime = get_mtime(spec.origin)
|
||||
mtime_changed = mtime != cls.FILE_MTIMES.get(spec.origin, None)
|
||||
if not mtime_changed and had_mod:
|
||||
continue
|
||||
@@ -276,20 +303,21 @@ class AddonManager:
|
||||
# Keep module loaded even if file went away.
|
||||
continue
|
||||
|
||||
if previous_mod:
|
||||
cls._unload_module(previous_mod)
|
||||
|
||||
logging.info("(Re)compiling addon %s" % spec.origin)
|
||||
old_mod = cls.FRESH_ADDON_MODULES.get(spec.name)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = mod
|
||||
spec.loader.exec_module(mod)
|
||||
cls.FILE_MTIMES[spec.origin] = mtime
|
||||
cls._unload_module(old_mod)
|
||||
|
||||
new_addons[spec.name] = mod
|
||||
|
||||
# Make sure module initialization happens after any pending task cancellations
|
||||
# due to module unloading.
|
||||
|
||||
asyncio.get_event_loop().call_soon(cls._init_module, mod)
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
loop.call_soon(cls._init_module, mod)
|
||||
except Exception as e:
|
||||
if had_mod:
|
||||
logging.exception("Exploded trying to reload addon %s" % spec.name)
|
||||
@@ -321,11 +349,11 @@ class AddonManager:
|
||||
cls.SCHEDULER.kill_matching_tasks(lifetime_mask=TaskLifeScope.ADDON, creator=addon)
|
||||
|
||||
@classmethod
|
||||
def _call_all_addon_hooks(cls, hook_name, *args, **kwargs):
|
||||
def _call_all_addon_hooks(cls, hook_name, *args, call_async=False, **kwargs) -> Optional[bool]:
|
||||
for module in cls.FRESH_ADDON_MODULES.values():
|
||||
if not module:
|
||||
continue
|
||||
ret = cls._call_module_hooks(module, hook_name, *args, **kwargs)
|
||||
ret = cls._call_module_hooks(module, hook_name, *args, call_async=call_async, **kwargs)
|
||||
if ret:
|
||||
return ret
|
||||
|
||||
@@ -356,15 +384,15 @@ class AddonManager:
|
||||
return commands
|
||||
|
||||
@classmethod
|
||||
def _call_module_hooks(cls, module, hook_name, *args, **kwargs):
|
||||
def _call_module_hooks(cls, module, hook_name, *args, call_async=False, **kwargs):
|
||||
for addon in cls._get_module_addons(module):
|
||||
ret = cls._try_call_hook(addon, hook_name, *args, **kwargs)
|
||||
ret = cls._try_call_hook(addon, hook_name, *args, call_async=call_async, **kwargs)
|
||||
if ret:
|
||||
return ret
|
||||
return cls._try_call_hook(module, hook_name, *args, **kwargs)
|
||||
return cls._try_call_hook(module, hook_name, *args, call_async=call_async, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def _try_call_hook(cls, addon, hook_name, *args, **kwargs):
|
||||
def _try_call_hook(cls, addon, hook_name, *args, call_async=False, **kwargs) -> Optional[bool]:
|
||||
if cls._SUBPROCESS:
|
||||
return
|
||||
|
||||
@@ -374,6 +402,20 @@ class AddonManager:
|
||||
if not hook_func:
|
||||
return
|
||||
try:
|
||||
if call_async:
|
||||
old_hook_func = hook_func
|
||||
|
||||
# Wrapper so we can invoke an async hook synchronously.
|
||||
def _wrapper(*w_args, **w_kwargs):
|
||||
cls.SCHEDULER.schedule_task(
|
||||
old_hook_func(*w_args, **w_kwargs),
|
||||
scope=TaskLifeScope.ADDON,
|
||||
creator=addon,
|
||||
)
|
||||
# Fall through to any other handlers as well,
|
||||
# async handlers don't chain.
|
||||
return None
|
||||
hook_func = _wrapper
|
||||
return hook_func(*args, **kwargs)
|
||||
except:
|
||||
logging.exception("Exploded in %r's %s hook" % (addon, hook_name))
|
||||
@@ -411,26 +453,36 @@ class AddonManager:
|
||||
raise
|
||||
return True
|
||||
if message.name == "ChatFromSimulator" and "ChatData" in message:
|
||||
chat: str = message["ChatData"]["Message"]
|
||||
chat_type: int = message["ChatData"]["ChatType"]
|
||||
# RLV-style OwnerSay?
|
||||
if chat and chat.startswith("@") and chat_type == 8:
|
||||
# RLV-style command, `@<cmd>(:<option1>;<option2>)?(=<param>)?`
|
||||
options, _, param = chat.rpartition("=")
|
||||
cmd, _, options = options.lstrip("@").partition(":")
|
||||
options = options.split(";")
|
||||
if RLVParser.is_rlv_message(message):
|
||||
# RLV allows putting multiple commands into one message, blindly splitting on ",".
|
||||
all_cmds_handled = True
|
||||
chat: str = message["ChatData"]["Message"]
|
||||
source = message["ChatData"]["SourceID"]
|
||||
try:
|
||||
with addon_ctx.push(session, region):
|
||||
handled = cls._call_all_addon_hooks("handle_rlv_command",
|
||||
session, region, source, cmd, options, param)
|
||||
if handled:
|
||||
region.circuit.drop_message(message)
|
||||
return True
|
||||
except:
|
||||
LOG.exception(f"Failed while handling command {chat!r}")
|
||||
if not cls._SWALLOW_ADDON_EXCEPTIONS:
|
||||
raise
|
||||
for command in RLVParser.parse_chat(chat):
|
||||
try:
|
||||
with addon_ctx.push(session, region):
|
||||
handled = cls._call_all_addon_hooks(
|
||||
"handle_rlv_command",
|
||||
session,
|
||||
region,
|
||||
source,
|
||||
command.behaviour,
|
||||
command.options,
|
||||
command.param,
|
||||
)
|
||||
if handled:
|
||||
region.circuit.drop_message(message)
|
||||
else:
|
||||
all_cmds_handled = False
|
||||
except:
|
||||
LOG.exception(f"Failed while handling command {command!r}")
|
||||
all_cmds_handled = False
|
||||
if not cls._SWALLOW_ADDON_EXCEPTIONS:
|
||||
raise
|
||||
# Drop the chat message if all commands it contained were handled by an addon
|
||||
if all_cmds_handled:
|
||||
return True
|
||||
|
||||
with addon_ctx.push(session, region):
|
||||
return cls._call_all_addon_hooks("handle_lludp_message", session, region, message)
|
||||
@@ -511,9 +563,9 @@ class AddonManager:
|
||||
|
||||
@classmethod
|
||||
def handle_object_updated(cls, session: Session, region: ProxiedRegion,
|
||||
obj: Object, updated_props: Set[str]):
|
||||
obj: Object, updated_props: Set[str], msg: Optional[Message]):
|
||||
with addon_ctx.push(session, region):
|
||||
return cls._call_all_addon_hooks("handle_object_updated", session, region, obj, updated_props)
|
||||
return cls._call_all_addon_hooks("handle_object_updated", session, region, obj, updated_props, msg)
|
||||
|
||||
@classmethod
|
||||
def handle_object_killed(cls, session: Session, region: ProxiedRegion, obj: Object):
|
||||
@@ -527,6 +579,11 @@ class AddonManager:
|
||||
with addon_ctx.push(session, region):
|
||||
return cls._call_all_addon_hooks("handle_region_changed", session, region)
|
||||
|
||||
@classmethod
|
||||
def handle_region_registered(cls, session: Session, region: ProxiedRegion):
|
||||
with addon_ctx.push(session, region):
|
||||
return cls._call_all_addon_hooks("handle_region_registered", session, region)
|
||||
|
||||
@classmethod
|
||||
def handle_circuit_created(cls, session: Session, region: ProxiedRegion):
|
||||
with addon_ctx.push(session, region):
|
||||
@@ -538,3 +595,7 @@ class AddonManager:
|
||||
with addon_ctx.push(session, region):
|
||||
return cls._call_all_addon_hooks("handle_proxied_packet", session_manager,
|
||||
packet, session, region)
|
||||
|
||||
@classmethod
|
||||
def handle_leap_client_added(cls, session_manager: SessionManager, leap_client: outleap.LEAPClient):
|
||||
return cls._call_all_addon_hooks("handle_leap_client_added", session_manager, leap_client, call_async=True)
|
||||
|
||||
39
hippolyzer/lib/proxy/asset_uploader.py
Normal file
39
hippolyzer/lib/proxy/asset_uploader.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.inventory import InventoryItem
|
||||
from hippolyzer.lib.base.message.message import Message, Block
|
||||
from hippolyzer.lib.base.network.transport import Direction
|
||||
from hippolyzer.lib.client.asset_uploader import AssetUploader
|
||||
|
||||
|
||||
class ProxyAssetUploader(AssetUploader):
|
||||
async def _handle_upload_complete(self, resp_payload: dict):
|
||||
# Check if this a failure response first, raising if it is
|
||||
await super()._handle_upload_complete(resp_payload)
|
||||
|
||||
# Fetch enough data from AIS to tell the viewer about the new inventory item
|
||||
session = self._region.session()
|
||||
item_id = resp_payload["new_inventory_item"]
|
||||
ais_req_data = {
|
||||
"items": [
|
||||
{
|
||||
"owner_id": session.agent_id,
|
||||
"item_id": item_id,
|
||||
}
|
||||
]
|
||||
}
|
||||
async with self._region.caps_client.post('FetchInventory2', llsd=ais_req_data) as resp:
|
||||
ais_item = InventoryItem.from_llsd((await resp.read_llsd())["items"][0], flavor="ais")
|
||||
|
||||
# Got it, ship it off to the viewer
|
||||
message = Message(
|
||||
"UpdateCreateInventoryItem",
|
||||
Block(
|
||||
"AgentData",
|
||||
AgentID=session.agent_id,
|
||||
SimApproved=1,
|
||||
TransactionID=UUID.random(),
|
||||
),
|
||||
ais_item.to_inventory_data(),
|
||||
direction=Direction.IN
|
||||
)
|
||||
self._region.circuit.send(message)
|
||||
@@ -24,6 +24,10 @@ class CapType(enum.Enum):
|
||||
WRAPPER = enum.auto()
|
||||
PROXY_ONLY = enum.auto()
|
||||
|
||||
@property
|
||||
def fake(self) -> bool:
|
||||
return self == CapType.PROXY_ONLY or self == CapType.WRAPPER
|
||||
|
||||
|
||||
class SerializedCapData(typing.NamedTuple):
|
||||
cap_name: typing.Optional[str] = None
|
||||
|
||||
@@ -20,7 +20,7 @@ class ProxyCapsClient(CapsClient):
|
||||
def _get_caps(self) -> Optional[CAPS_DICT]:
|
||||
if not self._region:
|
||||
return None
|
||||
return self._region.caps
|
||||
return self._region.cap_urls
|
||||
|
||||
def _request_fixups(self, cap_or_url: str, headers: Dict, proxy: Optional[bool], ssl: Any):
|
||||
# We want to proxy this through Hippolyzer
|
||||
@@ -28,7 +28,8 @@ class ProxyCapsClient(CapsClient):
|
||||
# We go through the proxy by default, tack on a header letting mitmproxy know the
|
||||
# request came from us so we can tag the request as injected. The header will be popped
|
||||
# off before passing through to the server.
|
||||
headers["X-Hippo-Injected"] = "1"
|
||||
if "X-Hippo-Injected" not in headers:
|
||||
headers["X-Hippo-Injected"] = "1"
|
||||
proxy_port = self._settings.HTTP_PROXY_PORT
|
||||
proxy = f"http://127.0.0.1:{proxy_port}"
|
||||
# TODO: set up the SSLContext to validate mitmproxy's cert
|
||||
|
||||
@@ -25,7 +25,7 @@ class ProxiedCircuit(Circuit):
|
||||
except:
|
||||
logging.exception(f"Failed to serialize: {message.to_dict()!r}")
|
||||
raise
|
||||
if self.logging_hook and message.injected:
|
||||
if self.logging_hook and message.synthetic:
|
||||
self.logging_hook(message)
|
||||
return self.send_datagram(serialized, message.direction, transport=transport)
|
||||
|
||||
@@ -34,47 +34,46 @@ class ProxiedCircuit(Circuit):
|
||||
return self.out_injections, self.in_injections
|
||||
return self.in_injections, self.out_injections
|
||||
|
||||
def prepare_message(self, message: Message, direction=None):
|
||||
def prepare_message(self, message: Message):
|
||||
if message.finalized:
|
||||
raise RuntimeError(f"Trying to re-send finalized {message!r}")
|
||||
if message.queued:
|
||||
# This is due to be dropped, nothing should be sending the original
|
||||
raise RuntimeError(f"Trying to send original of queued {message!r}")
|
||||
direction = direction or getattr(message, 'direction')
|
||||
fwd_injections, reverse_injections = self._get_injections(direction)
|
||||
fwd_injections, reverse_injections = self._get_injections(message.direction)
|
||||
|
||||
message.finalized = True
|
||||
|
||||
# Injected, let's gen an ID
|
||||
if message.packet_id is None:
|
||||
message.packet_id = fwd_injections.gen_injectable_id()
|
||||
message.injected = True
|
||||
else:
|
||||
message.synthetic = True
|
||||
# This message wasn't injected by the proxy so we need to rewrite packet IDs
|
||||
# to account for IDs the real creator of the packet couldn't have known about.
|
||||
elif not message.synthetic:
|
||||
# was_dropped needs the unmodified packet ID
|
||||
if fwd_injections.was_dropped(message.packet_id) and message.name != "PacketAck":
|
||||
logging.warning("Attempting to re-send previously dropped %s:%s, did we ack?" %
|
||||
(message.packet_id, message.name))
|
||||
message.packet_id = fwd_injections.get_effective_id(message.packet_id)
|
||||
fwd_injections.track_seen(message.packet_id)
|
||||
|
||||
message.finalized = True
|
||||
|
||||
if not message.injected:
|
||||
# This message wasn't injected by the proxy so we need to rewrite packet IDs
|
||||
# to account for IDs the other parties couldn't have known about.
|
||||
message.acks = tuple(
|
||||
reverse_injections.get_original_id(x) for x in message.acks
|
||||
if not reverse_injections.was_injected(x)
|
||||
)
|
||||
|
||||
if message.name == "PacketAck":
|
||||
if not self._rewrite_packet_ack(message, reverse_injections):
|
||||
logging.debug(f"Dropping {direction} ack for injected packets!")
|
||||
if not self._rewrite_packet_ack(message, reverse_injections) and not message.acks:
|
||||
logging.debug(f"Dropping {message.direction} ack for injected packets!")
|
||||
# Let caller know this shouldn't be sent at all, it's strictly ACKs for
|
||||
# injected packets.
|
||||
return False
|
||||
elif message.name == "StartPingCheck":
|
||||
self._rewrite_start_ping_check(message, fwd_injections)
|
||||
|
||||
if not message.acks:
|
||||
if message.acks:
|
||||
message.send_flags |= PacketFlags.ACK
|
||||
else:
|
||||
message.send_flags &= ~PacketFlags.ACK
|
||||
return True
|
||||
|
||||
@@ -100,15 +99,18 @@ class ProxiedCircuit(Circuit):
|
||||
new_id = fwd_injections.get_effective_id(orig_id)
|
||||
if orig_id != new_id:
|
||||
logging.debug("Rewrote oldest unacked %s -> %s" % (orig_id, new_id))
|
||||
# Get a list of unacked IDs for the direction this StartPingCheck is heading
|
||||
fwd_unacked = (a for (d, a) in self.unacked_reliable.keys() if d == message.direction)
|
||||
# Use the proxy's oldest unacked ID if it's older than the client's
|
||||
new_id = min((new_id, *fwd_unacked))
|
||||
message["PingID"]["OldestUnacked"] = new_id
|
||||
|
||||
def drop_message(self, message: Message, orig_direction=None):
|
||||
def drop_message(self, message: Message):
|
||||
if message.finalized:
|
||||
raise RuntimeError(f"Trying to drop finalized {message!r}")
|
||||
if message.packet_id is None:
|
||||
return
|
||||
orig_direction = orig_direction or message.direction
|
||||
fwd_injections, reverse_injections = self._get_injections(orig_direction)
|
||||
fwd_injections, reverse_injections = self._get_injections(message.direction)
|
||||
|
||||
fwd_injections.mark_dropped(message.packet_id)
|
||||
message.dropped = True
|
||||
@@ -116,7 +118,7 @@ class ProxiedCircuit(Circuit):
|
||||
|
||||
# Was sent reliably, tell the other end that we saw it and to shut up.
|
||||
if message.reliable:
|
||||
self.send_acks([message.packet_id], ~orig_direction)
|
||||
self.send_acks([message.packet_id], ~message.direction)
|
||||
|
||||
# This packet had acks for the other end, send them in a separate PacketAck
|
||||
effective_acks = tuple(
|
||||
@@ -124,7 +126,7 @@ class ProxiedCircuit(Circuit):
|
||||
if not reverse_injections.was_injected(x)
|
||||
)
|
||||
if effective_acks:
|
||||
self.send_acks(effective_acks, orig_direction, packet_id=message.packet_id)
|
||||
self.send_acks(effective_acks, message.direction, packet_id=message.packet_id)
|
||||
|
||||
|
||||
class InjectionTracker:
|
||||
|
||||
@@ -16,6 +16,8 @@ import mitmproxy.http
|
||||
from hippolyzer.lib.base import llsd
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.message.llsd_msg_serializer import LLSDMessageSerializer
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.network.transport import Direction
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.http_flow import HippoHTTPFlow
|
||||
from hippolyzer.lib.proxy.caps import CapData, CapType
|
||||
@@ -32,6 +34,9 @@ def apply_security_monkeypatches():
|
||||
apply_security_monkeypatches()
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MITMProxyEventManager:
|
||||
"""
|
||||
Handles HTTP request and response events from the mitmproxy process
|
||||
@@ -42,7 +47,7 @@ class MITMProxyEventManager:
|
||||
"UpdateNotecardAgentInventory", "UpdateNotecardTaskInventory",
|
||||
"UpdateScriptAgent", "UpdateScriptTask",
|
||||
"UpdateSettingsAgentInventory", "UpdateSettingsTaskInventory",
|
||||
"UploadBakedTexture",
|
||||
"UploadBakedTexture", "UploadAgentProfileImage",
|
||||
}
|
||||
|
||||
def __init__(self, session_manager: SessionManager, flow_context: HTTPFlowContext):
|
||||
@@ -58,7 +63,7 @@ class MITMProxyEventManager:
|
||||
try:
|
||||
await self.pump_proxy_event()
|
||||
except:
|
||||
logging.exception("Exploded when handling parsed packets")
|
||||
LOG.exception("Exploded when handling parsed packets")
|
||||
|
||||
async def pump_proxy_event(self):
|
||||
try:
|
||||
@@ -83,16 +88,19 @@ class MITMProxyEventManager:
|
||||
finally:
|
||||
# If someone has taken this request out of the regular callback flow,
|
||||
# they'll manually send a callback at some later time.
|
||||
if not flow.taken:
|
||||
self.to_proxy_queue.put(("callback", flow.id, flow.get_state()))
|
||||
if not flow.taken and not flow.resumed:
|
||||
# Addon hasn't taken ownership of this flow, send it back to mitmproxy
|
||||
# ourselves.
|
||||
flow.resume()
|
||||
|
||||
def _handle_request(self, flow: HippoHTTPFlow):
|
||||
url = flow.request.url
|
||||
cap_data = self.session_manager.resolve_cap(url)
|
||||
flow.cap_data = cap_data
|
||||
# Don't do anything special with the proxy's own requests,
|
||||
# we only pass it through for logging purposes.
|
||||
if flow.request_injected:
|
||||
# Don't do anything special with the proxy's own requests unless the requested
|
||||
# URL can only be handled by the proxy. Ideally we only pass the request through
|
||||
# for logging purposes.
|
||||
if flow.request_injected and (not cap_data or not cap_data.type.fake):
|
||||
return
|
||||
|
||||
# The local asset repo gets first bite at the apple
|
||||
@@ -104,7 +112,7 @@ class MITMProxyEventManager:
|
||||
AddonManager.handle_http_request(flow)
|
||||
if cap_data and cap_data.cap_name.endswith("ProxyWrapper"):
|
||||
orig_cap_name = cap_data.cap_name.rsplit("ProxyWrapper", 1)[0]
|
||||
orig_cap_url = cap_data.region().caps[orig_cap_name]
|
||||
orig_cap_url = cap_data.region().cap_urls[orig_cap_name]
|
||||
split_orig_url = urllib.parse.urlsplit(orig_cap_url)
|
||||
orig_cap_host = split_orig_url[1]
|
||||
|
||||
@@ -135,9 +143,9 @@ class MITMProxyEventManager:
|
||||
)
|
||||
elif cap_data and cap_data.asset_server_cap:
|
||||
# Both the wrapper request and the actual asset server request went through
|
||||
# the proxy
|
||||
# the proxy. Don't bother trying the redirect strategy anymore.
|
||||
self._asset_server_proxied = True
|
||||
logging.warning("noproxy not used, switching to URI rewrite strategy")
|
||||
LOG.warning("noproxy not used, switching to URI rewrite strategy")
|
||||
elif cap_data and cap_data.cap_name == "EventQueueGet":
|
||||
# HACK: The sim's EQ acking mechanism doesn't seem to actually work.
|
||||
# if the client drops the connection due to timeout before we can
|
||||
@@ -148,7 +156,7 @@ class MITMProxyEventManager:
|
||||
eq_manager = cap_data.region().eq_manager
|
||||
cached_resp = eq_manager.get_cached_poll_response(req_ack_id)
|
||||
if cached_resp:
|
||||
logging.warning("Had to serve a cached EventQueueGet due to client desync")
|
||||
LOG.warning("Had to serve a cached EventQueueGet due to client desync")
|
||||
flow.response = mitmproxy.http.Response.make(
|
||||
200,
|
||||
llsd.format_xml(cached_resp),
|
||||
@@ -159,6 +167,17 @@ class MITMProxyEventManager:
|
||||
"Connection": "close",
|
||||
},
|
||||
)
|
||||
elif cap_data and cap_data.cap_name == "Seed":
|
||||
# Drop any proxy-only caps from the seed request we send to the server,
|
||||
# add those cap names as metadata so we know to send their urls in the response
|
||||
parsed_seed: List[str] = llsd.parse_xml(flow.request.content)
|
||||
flow.metadata['needed_proxy_caps'] = []
|
||||
for known_cap_name, (known_cap_type, known_cap_url) in cap_data.region().caps.items():
|
||||
if known_cap_type == CapType.PROXY_ONLY and known_cap_name in parsed_seed:
|
||||
parsed_seed.remove(known_cap_name)
|
||||
flow.metadata['needed_proxy_caps'].append(known_cap_name)
|
||||
if flow.metadata['needed_proxy_caps']:
|
||||
flow.request.content = llsd.format_xml(parsed_seed)
|
||||
elif not cap_data:
|
||||
if self._is_login_request(flow):
|
||||
# Not strictly a Cap, but makes it easier to filter on.
|
||||
@@ -198,14 +217,23 @@ class MITMProxyEventManager:
|
||||
def _handle_response(self, flow: HippoHTTPFlow):
|
||||
message_logger = self.session_manager.message_logger
|
||||
if message_logger:
|
||||
message_logger.log_http_response(flow)
|
||||
try:
|
||||
message_logger.log_http_response(flow)
|
||||
except:
|
||||
LOG.exception("Failed while logging HTTP flow")
|
||||
|
||||
# Don't handle responses for requests injected by the proxy
|
||||
if flow.request_injected:
|
||||
# Don't process responses for requests or responses injected by the proxy.
|
||||
# We already processed it, it came from us!
|
||||
if flow.request_injected or flow.response_injected:
|
||||
return
|
||||
|
||||
status = flow.response.status_code
|
||||
cap_data: Optional[CapData] = flow.metadata["cap_data"]
|
||||
if not cap_data:
|
||||
# Make sure there's always cap data attached to the flow, even if it's
|
||||
# empty. Some consumers expect it to always be there, when it might not
|
||||
# be if the proxy barfed while handling the request.
|
||||
cap_data = flow.metadata["cap_data"] = CapData()
|
||||
|
||||
if status == 200 and cap_data and cap_data.cap_name == "FirestormBridge":
|
||||
# Fake FirestormBridge cap based on a bridge-like response coming from
|
||||
@@ -251,18 +279,21 @@ class MITMProxyEventManager:
|
||||
|
||||
if cap_data.cap_name == "Seed":
|
||||
parsed = llsd.parse_xml(flow.response.content)
|
||||
logging.debug("Got seed cap for %r : %r" % (cap_data, parsed))
|
||||
LOG.debug("Got seed cap for %r : %r" % (cap_data, parsed))
|
||||
region.update_caps(parsed)
|
||||
|
||||
# On LL's grid these URIs aren't unique across sessions or regions,
|
||||
# so we get request attribution by replacing them with a unique
|
||||
# alias URI.
|
||||
logging.debug("Replacing GetMesh caps with wrapped versions")
|
||||
LOG.debug("Replacing GetMesh caps with wrapped versions")
|
||||
wrappable_caps = {"GetMesh2", "GetMesh", "GetTexture", "ViewerAsset"}
|
||||
for cap_name in wrappable_caps:
|
||||
if cap_name in parsed:
|
||||
parsed[cap_name] = region.register_wrapper_cap(cap_name)
|
||||
flow.response.content = llsd.format_pretty_xml(parsed)
|
||||
# Send the client the URLs for any proxy-only caps it requested
|
||||
for cap_name in flow.metadata['needed_proxy_caps']:
|
||||
parsed[cap_name] = region.cap_urls[cap_name]
|
||||
flow.response.content = llsd.format_xml(parsed)
|
||||
elif cap_data.cap_name == "EventQueueGet":
|
||||
parsed_eq_resp = llsd.parse_xml(flow.response.content)
|
||||
if parsed_eq_resp:
|
||||
@@ -281,15 +312,15 @@ class MITMProxyEventManager:
|
||||
# HACK: see note in above request handler for EventQueueGet
|
||||
req_ack_id = llsd.parse_xml(flow.request.content)["ack"]
|
||||
eq_manager.cache_last_poll_response(req_ack_id, parsed_eq_resp)
|
||||
flow.response.content = llsd.format_pretty_xml(parsed_eq_resp)
|
||||
flow.response.content = llsd.format_xml(parsed_eq_resp)
|
||||
elif cap_data.cap_name in self.UPLOAD_CREATING_CAPS:
|
||||
if not region:
|
||||
return
|
||||
parsed = llsd.parse_xml(flow.response.content)
|
||||
if "uploader" in parsed:
|
||||
region.register_temporary_cap(cap_data.cap_name + "Uploader", parsed["uploader"])
|
||||
region.register_cap(cap_data.cap_name + "Uploader", parsed["uploader"], CapType.TEMPORARY)
|
||||
except:
|
||||
logging.exception("OOPS, blew up in HTTP proxy!")
|
||||
LOG.exception("OOPS, blew up in HTTP proxy!")
|
||||
|
||||
def _handle_login_flow(self, flow: HippoHTTPFlow):
|
||||
resp = xmlrpc.client.loads(flow.response.content)[0][0] # type: ignore
|
||||
@@ -298,20 +329,30 @@ class MITMProxyEventManager:
|
||||
flow.cap_data = CapData("LoginRequest", session=weakref.ref(sess))
|
||||
|
||||
def _handle_eq_event(self, session: Session, region: ProxiedRegion, event: Dict[str, Any]):
|
||||
logging.debug("Event received on %r: %r" % (self, event))
|
||||
LOG.debug("Event received on %r: %r" % (self, event))
|
||||
message_logger = self.session_manager.message_logger
|
||||
if message_logger:
|
||||
message_logger.log_eq_event(session, region, event)
|
||||
|
||||
if self.llsd_message_serializer.can_handle(event["message"]):
|
||||
msg = self.llsd_message_serializer.deserialize(event)
|
||||
else:
|
||||
msg = Message.from_eq_event(event)
|
||||
msg.sender = region.circuit_addr
|
||||
msg.direction = Direction.IN
|
||||
|
||||
try:
|
||||
region.message_handler.handle(msg)
|
||||
except:
|
||||
LOG.exception("Failed while handling EQ message")
|
||||
|
||||
handle_event = AddonManager.handle_eq_event(session, region, event)
|
||||
if handle_event is True:
|
||||
# Addon handled the event and didn't want it sent to the viewer
|
||||
return True
|
||||
|
||||
msg = None
|
||||
# Handle events that inform us about new regions
|
||||
sim_addr, sim_handle, sim_seed = None, None, None
|
||||
if self.llsd_message_serializer.can_handle(event["message"]):
|
||||
msg = self.llsd_message_serializer.deserialize(event)
|
||||
# Sim is asking us to talk to a neighbour
|
||||
if event["message"] == "EstablishAgentCommunication":
|
||||
ip_split = event["body"]["sim-ip-and-port"].split(":")
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import multiprocessing
|
||||
import weakref
|
||||
from typing import *
|
||||
from typing import Optional
|
||||
|
||||
@@ -20,16 +22,18 @@ class HippoHTTPFlow:
|
||||
Hides the nastiness of writing to flow.metadata so we can pass
|
||||
state back and forth between the two proxies
|
||||
"""
|
||||
__slots__ = ("flow",)
|
||||
__slots__ = ("flow", "callback_queue", "resumed", "taken")
|
||||
|
||||
def __init__(self, flow: HTTPFlow):
|
||||
def __init__(self, flow: HTTPFlow, callback_queue: Optional[multiprocessing.Queue] = None):
|
||||
self.flow: HTTPFlow = flow
|
||||
self.resumed = False
|
||||
self.taken = False
|
||||
self.callback_queue = weakref.ref(callback_queue) if callback_queue else None
|
||||
meta = self.flow.metadata
|
||||
meta.setdefault("taken", False)
|
||||
meta.setdefault("can_stream", True)
|
||||
meta.setdefault("response_injected", False)
|
||||
meta.setdefault("request_injected", False)
|
||||
meta.setdefault("cap_data", None)
|
||||
meta.setdefault("cap_data", CapData())
|
||||
meta.setdefault("from_browser", False)
|
||||
|
||||
@property
|
||||
@@ -91,12 +95,27 @@ class HippoHTTPFlow:
|
||||
|
||||
def take(self) -> HippoHTTPFlow:
|
||||
"""Don't automatically pass this flow back to mitmproxy"""
|
||||
self.metadata["taken"] = True
|
||||
# TODO: Having to explicitly take / release Flows to use them in an async
|
||||
# context is kind of janky. The HTTP callback handling code should probably
|
||||
# be made totally async, including the addon hooks. Would coroutine per-callback
|
||||
# be expensive?
|
||||
assert not self.taken and not self.resumed
|
||||
self.taken = True
|
||||
return self
|
||||
|
||||
@property
|
||||
def taken(self) -> bool:
|
||||
return self.metadata["taken"]
|
||||
def resume(self):
|
||||
"""Release the HTTP flow back to the normal processing flow"""
|
||||
assert self.callback_queue
|
||||
assert not self.resumed
|
||||
self.taken = False
|
||||
self.resumed = True
|
||||
self.callback_queue().put(("callback", self.flow.id, self.get_state()))
|
||||
|
||||
def preempt(self):
|
||||
# Must be some flow that we previously resumed, we're racing
|
||||
# the result from the server end.
|
||||
assert not self.taken and self.resumed
|
||||
self.callback_queue().put(("preempt", self.flow.id, self.get_state()))
|
||||
|
||||
@property
|
||||
def is_replay(self) -> bool:
|
||||
@@ -120,11 +139,14 @@ class HippoHTTPFlow:
|
||||
flow: Optional[HTTPFlow] = HTTPFlow.from_state(flow_state)
|
||||
assert flow is not None
|
||||
cap_data_ser = flow.metadata.get("cap_data_ser")
|
||||
callback_queue = None
|
||||
if session_manager:
|
||||
callback_queue = session_manager.flow_context.to_proxy_queue
|
||||
if cap_data_ser is not None:
|
||||
flow.metadata["cap_data"] = CapData.deserialize(cap_data_ser, session_manager)
|
||||
else:
|
||||
flow.metadata["cap_data"] = None
|
||||
return cls(flow)
|
||||
return cls(flow, callback_queue)
|
||||
|
||||
def copy(self) -> HippoHTTPFlow:
|
||||
# HACK: flow.copy() expects the flow to be fully JSON serializable, but
|
||||
|
||||
@@ -7,6 +7,8 @@ import sys
|
||||
import queue
|
||||
import typing
|
||||
import uuid
|
||||
import weakref
|
||||
from typing import Iterable
|
||||
|
||||
import mitmproxy.certs
|
||||
import mitmproxy.ctx
|
||||
@@ -14,20 +16,30 @@ import mitmproxy.log
|
||||
import mitmproxy.master
|
||||
import mitmproxy.options
|
||||
import mitmproxy.proxy
|
||||
from cryptography import x509
|
||||
from cryptography.x509 import GeneralNames
|
||||
from mitmproxy.addons import core, clientplayback, proxyserver, next_layer, disable_h2c
|
||||
from mitmproxy.certs import CertStoreEntry
|
||||
from mitmproxy.http import HTTPFlow
|
||||
from mitmproxy.proxy.layers import tls
|
||||
import OpenSSL
|
||||
|
||||
from hippolyzer.lib.base.helpers import get_resource_filename
|
||||
from hippolyzer.lib.base.helpers import get_resource_filename, create_logged_task
|
||||
from hippolyzer.lib.base.multiprocessing_utils import ParentProcessWatcher
|
||||
from hippolyzer.lib.proxy.caps import SerializedCapData
|
||||
|
||||
|
||||
class SLCertStore(mitmproxy.certs.CertStore):
|
||||
def get_cert(self, commonname: typing.Optional[str], sans: typing.List[str], *args, **kwargs):
|
||||
def get_cert(
|
||||
self,
|
||||
commonname: str | None,
|
||||
sans: Iterable[x509.GeneralName],
|
||||
organization: str | None = None,
|
||||
*args,
|
||||
**kwargs
|
||||
) -> CertStoreEntry:
|
||||
entry = super().get_cert(commonname, sans, *args, **kwargs)
|
||||
cert, privkey, chain = entry.cert, entry.privatekey, entry.chain_file
|
||||
cert, privkey, chain, chain_certs = entry.cert, entry.privatekey, entry.chain_file, entry.chain_certs
|
||||
x509 = cert.to_pyopenssl()
|
||||
# The cert must have a subject key ID or the viewer will reject it.
|
||||
for i in range(0, x509.get_extension_count()):
|
||||
@@ -47,10 +59,10 @@ class SLCertStore(mitmproxy.certs.CertStore):
|
||||
])
|
||||
x509.sign(OpenSSL.crypto.PKey.from_cryptography_key(privkey), "sha256") # type: ignore
|
||||
new_entry = mitmproxy.certs.CertStoreEntry(
|
||||
mitmproxy.certs.Cert.from_pyopenssl(x509), privkey, chain
|
||||
mitmproxy.certs.Cert.from_pyopenssl(x509), privkey, chain, chain_certs,
|
||||
)
|
||||
# Replace the cert that was created in the base `get_cert()` with our modified cert
|
||||
self.certs[(commonname, tuple(sans))] = new_entry
|
||||
self.certs[(commonname, GeneralNames(sans))] = new_entry
|
||||
self.expire_queue.pop(-1)
|
||||
self.expire(new_entry)
|
||||
return new_entry
|
||||
@@ -70,7 +82,7 @@ class SLTlsConfig(mitmproxy.addons.tlsconfig.TlsConfig):
|
||||
)
|
||||
self.certstore.certs = old_cert_store.certs
|
||||
|
||||
def tls_start_server(self, tls_start: tls.TlsStartData):
|
||||
def tls_start_server(self, tls_start: tls.TlsData):
|
||||
super().tls_start_server(tls_start)
|
||||
# Since 2000 the recommendation per RFCs has been to only check SANs and not the CN field.
|
||||
# Most browsers do this, as does mitmproxy. The viewer does not, and the sim certs have no SAN
|
||||
@@ -99,26 +111,14 @@ class IPCInterceptionAddon:
|
||||
"""
|
||||
def __init__(self, flow_context: HTTPFlowContext):
|
||||
self.mitmproxy_ready = flow_context.mitmproxy_ready
|
||||
self.intercepted_flows: typing.Dict[str, HTTPFlow] = {}
|
||||
self.flows: weakref.WeakValueDictionary[str, HTTPFlow] = weakref.WeakValueDictionary()
|
||||
self.from_proxy_queue: multiprocessing.Queue = flow_context.from_proxy_queue
|
||||
self.to_proxy_queue: multiprocessing.Queue = flow_context.to_proxy_queue
|
||||
self.shutdown_signal: multiprocessing.Event = flow_context.shutdown_signal
|
||||
|
||||
def add_log(self, entry: mitmproxy.log.LogEntry):
|
||||
if entry.level == "debug":
|
||||
logging.debug(entry.msg)
|
||||
elif entry.level in ("alert", "info"):
|
||||
# TODO: All mitmproxy infos are basically debugs, should
|
||||
# probably give these dedicated loggers
|
||||
logging.debug(entry.msg)
|
||||
elif entry.level == "warn":
|
||||
logging.warning(entry.msg)
|
||||
elif entry.level == "error":
|
||||
logging.error(entry.msg)
|
||||
|
||||
def running(self):
|
||||
# register to pump the events or something here
|
||||
asyncio.create_task(self._pump_callbacks())
|
||||
create_logged_task(self._pump_callbacks(), "Pump HTTP proxy callbacks")
|
||||
# Tell the main process mitmproxy is ready to handle requests
|
||||
self.mitmproxy_ready.set()
|
||||
|
||||
@@ -134,11 +134,13 @@ class IPCInterceptionAddon:
|
||||
await asyncio.sleep(0.001)
|
||||
continue
|
||||
if event_type == "callback":
|
||||
orig_flow = self.intercepted_flows.pop(flow_id)
|
||||
orig_flow = self.flows[flow_id]
|
||||
orig_flow.set_state(flow_state)
|
||||
# Remove the taken flag from the flow if present, the flow by definition
|
||||
# isn't take()n anymore once it's been passed back to the proxy.
|
||||
orig_flow.metadata.pop("taken", None)
|
||||
elif event_type == "preempt":
|
||||
orig_flow = self.flows.get(flow_id)
|
||||
if orig_flow:
|
||||
orig_flow.intercept()
|
||||
orig_flow.set_state(flow_state)
|
||||
elif event_type == "replay":
|
||||
flow: HTTPFlow = HTTPFlow.from_state(flow_state)
|
||||
# mitmproxy won't replay intercepted flows, this is an old flow so
|
||||
@@ -160,8 +162,8 @@ class IPCInterceptionAddon:
|
||||
from_browser = "Mozilla" in flow.request.headers.get("User-Agent", "")
|
||||
flow.metadata["from_browser"] = from_browser
|
||||
# Only trust the "injected" header if not from a browser
|
||||
was_injected = flow.request.headers.pop("X-Hippo-Injected", False)
|
||||
if was_injected and not from_browser:
|
||||
was_injected = flow.request.headers.pop("X-Hippo-Injected", "")
|
||||
if was_injected == "1" and not from_browser:
|
||||
flow.metadata["request_injected"] = True
|
||||
|
||||
# Does this request need the stupid hack around aiohttp's windows proactor bug
|
||||
@@ -172,13 +174,13 @@ class IPCInterceptionAddon:
|
||||
|
||||
def _queue_flow_interception(self, event_type: str, flow: HTTPFlow):
|
||||
flow.intercept()
|
||||
self.intercepted_flows[flow.id] = flow
|
||||
self.flows[flow.id] = flow
|
||||
self.from_proxy_queue.put((event_type, flow.get_state()), True)
|
||||
|
||||
def responseheaders(self, flow: HTTPFlow):
|
||||
# The response was injected earlier in an earlier handler,
|
||||
# we don't want to touch this anymore.
|
||||
if flow.metadata["response_injected"]:
|
||||
if flow.metadata.get("response_injected"):
|
||||
return
|
||||
|
||||
# Someone fucked up and put a mimetype in Content-Encoding.
|
||||
@@ -189,7 +191,10 @@ class IPCInterceptionAddon:
|
||||
flow.response.headers["Content-Encoding"] = "identity"
|
||||
|
||||
def response(self, flow: HTTPFlow):
|
||||
if flow.metadata["response_injected"]:
|
||||
cap_data: typing.Optional[SerializedCapData] = flow.metadata.get("cap_data")
|
||||
if flow.metadata.get("response_injected") and cap_data and cap_data.asset_server_cap:
|
||||
# Don't bother intercepting asset server requests where we injected a response.
|
||||
# We don't want to log them and they don't need any more processing by user hooks.
|
||||
return
|
||||
self._queue_flow_interception("response", flow)
|
||||
|
||||
@@ -197,10 +202,10 @@ class IPCInterceptionAddon:
|
||||
class SLMITMAddon(IPCInterceptionAddon):
|
||||
def responseheaders(self, flow: HTTPFlow):
|
||||
super().responseheaders(flow)
|
||||
cap_data: typing.Optional[SerializedCapData] = flow.metadata["cap_data_ser"]
|
||||
cap_data: typing.Optional[SerializedCapData] = flow.metadata.get("cap_data_ser")
|
||||
|
||||
# Request came from the proxy itself, don't touch it.
|
||||
if flow.metadata["request_injected"]:
|
||||
if flow.metadata.get("request_injected"):
|
||||
return
|
||||
|
||||
# This is an asset server response that we're not interested in intercepting.
|
||||
@@ -209,7 +214,7 @@ class SLMITMAddon(IPCInterceptionAddon):
|
||||
# Can't stream if we injected our own response or we were asked not to stream
|
||||
if not flow.metadata["response_injected"] and flow.metadata["can_stream"]:
|
||||
flow.response.stream = True
|
||||
elif not cap_data and not flow.metadata["from_browser"]:
|
||||
elif not cap_data and not flow.metadata.get("from_browser"):
|
||||
object_name = flow.response.headers.get("X-SecondLife-Object-Name", "")
|
||||
# Meh. Add some fake Cap data for this so it can be matched on.
|
||||
if object_name.startswith("#Firestorm LSL Bridge"):
|
||||
@@ -229,12 +234,8 @@ class SLMITMMaster(mitmproxy.master.Master):
|
||||
SLMITMAddon(flow_context),
|
||||
)
|
||||
|
||||
def start_server(self):
|
||||
self.start()
|
||||
asyncio.ensure_future(self.running())
|
||||
|
||||
|
||||
def create_proxy_master(host, port, flow_context: HTTPFlowContext): # pragma: no cover
|
||||
def create_http_proxy(host, port, flow_context: HTTPFlowContext, ssl_insecure=False): # pragma: no cover
|
||||
opts = mitmproxy.options.Options()
|
||||
master = SLMITMMaster(flow_context, opts)
|
||||
|
||||
@@ -249,10 +250,6 @@ def create_proxy_master(host, port, flow_context: HTTPFlowContext): # pragma: n
|
||||
ssl_verify_upstream_trusted_ca=ca_bundle,
|
||||
listen_host=host,
|
||||
listen_port=port,
|
||||
ssl_insecure=ssl_insecure,
|
||||
)
|
||||
return master
|
||||
|
||||
|
||||
def create_http_proxy(bind_host, port, flow_context: HTTPFlowContext): # pragma: no cover
|
||||
master = create_proxy_master(bind_host, port, flow_context)
|
||||
return master
|
||||
|
||||
107
hippolyzer/lib/proxy/inventory_manager.py
Normal file
107
hippolyzer/lib/proxy/inventory_manager.py
Normal file
@@ -0,0 +1,107 @@
|
||||
import asyncio
|
||||
import datetime as dt
|
||||
import functools
|
||||
import logging
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base import llsd
|
||||
from hippolyzer.lib.base.helpers import get_mtime, create_logged_task
|
||||
from hippolyzer.lib.client.inventory_manager import InventoryManager
|
||||
from hippolyzer.lib.proxy.http_flow import HippoHTTPFlow
|
||||
from hippolyzer.lib.proxy.viewer_settings import iter_viewer_cache_dirs
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from hippolyzer.lib.proxy.sessions import Session
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ProxyInventoryManager(InventoryManager):
|
||||
_session: "Session"
|
||||
|
||||
def __init__(self, session: "Session"):
|
||||
# These handlers all need their processing deferred until the cache has been loaded.
|
||||
# Since cache is loaded asynchronously, the viewer may get ahead of us due to parsing
|
||||
# the cache faster and start requesting inventory details we can't do anything with yet.
|
||||
self._handle_update_create_inventory_item = self._wrap_with_cache_defer(
|
||||
self._handle_update_create_inventory_item
|
||||
)
|
||||
self._handle_remove_inventory_item = self._wrap_with_cache_defer(
|
||||
self._handle_remove_inventory_item
|
||||
)
|
||||
self._handle_remove_inventory_folder = self._wrap_with_cache_defer(
|
||||
self._handle_remove_inventory_folder
|
||||
)
|
||||
self._handle_bulk_update_inventory = self._wrap_with_cache_defer(
|
||||
self._handle_bulk_update_inventory
|
||||
)
|
||||
self._handle_move_inventory_item = self._wrap_with_cache_defer(
|
||||
self._handle_move_inventory_item
|
||||
)
|
||||
self.process_aisv3_response = self._wrap_with_cache_defer(
|
||||
self.process_aisv3_response
|
||||
)
|
||||
|
||||
# Base constructor after, because it registers handlers to specific methods, which need to
|
||||
# be wrapped before we call they're registered. Handlers are registered by method reference,
|
||||
# not by name!
|
||||
super().__init__(session)
|
||||
session.http_message_handler.subscribe("InventoryAPIv3", self._handle_aisv3_flow)
|
||||
newest_cache = None
|
||||
newest_timestamp = dt.datetime(year=1970, month=1, day=1, tzinfo=dt.timezone.utc)
|
||||
# So consumers know when the inventory should be complete
|
||||
self.cache_loaded: asyncio.Event = asyncio.Event()
|
||||
self._cache_deferred_calls: List[Tuple[Callable[..., None], Tuple]] = []
|
||||
# Look for the newest version of the cached inventory and use that.
|
||||
# Not foolproof, but close enough if we're not sure what viewer is being used.
|
||||
for cache_dir in iter_viewer_cache_dirs():
|
||||
inv_cache_path = cache_dir / (str(session.agent_id) + ".inv.llsd.gz")
|
||||
if inv_cache_path.exists():
|
||||
mod = get_mtime(inv_cache_path)
|
||||
if not mod:
|
||||
continue
|
||||
mod_ts = dt.datetime.fromtimestamp(mod, dt.timezone.utc)
|
||||
if mod_ts <= newest_timestamp:
|
||||
continue
|
||||
newest_cache = inv_cache_path
|
||||
|
||||
if newest_cache:
|
||||
cache_load_fut = asyncio.ensure_future(asyncio.to_thread(self.load_cache, newest_cache))
|
||||
# Meh. Don't care if it fails.
|
||||
cache_load_fut.add_done_callback(lambda *args: self.cache_loaded.set())
|
||||
create_logged_task(self._apply_deferred_after_loaded(), "Apply deferred inventory", LOG)
|
||||
else:
|
||||
self.cache_loaded.set()
|
||||
|
||||
async def _apply_deferred_after_loaded(self):
|
||||
await self.cache_loaded.wait()
|
||||
LOG.info("Applying deferred inventory calls")
|
||||
deferred_calls = self._cache_deferred_calls[:]
|
||||
self._cache_deferred_calls.clear()
|
||||
for func, args in deferred_calls:
|
||||
try:
|
||||
func(*args)
|
||||
except:
|
||||
LOG.exception("Failed to apply deferred inventory call")
|
||||
|
||||
def _wrap_with_cache_defer(self, func: Callable[..., None]):
|
||||
@functools.wraps(func)
|
||||
def wrapped(*inner_args):
|
||||
if not self.cache_loaded.is_set():
|
||||
self._cache_deferred_calls.append((func, inner_args))
|
||||
else:
|
||||
func(*inner_args)
|
||||
return wrapped
|
||||
|
||||
def _handle_aisv3_flow(self, flow: HippoHTTPFlow):
|
||||
if flow.response.status_code < 200 or flow.response.status_code > 300:
|
||||
# Probably not a success
|
||||
return
|
||||
content_type = flow.response.headers.get("Content-Type", "")
|
||||
if "llsd" not in content_type:
|
||||
# Okay, probably still some kind of error...
|
||||
return
|
||||
|
||||
# Try and add anything from the response into the model
|
||||
self.process_aisv3_response(llsd.parse(flow.response.content))
|
||||
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import weakref
|
||||
from typing import Optional, Tuple
|
||||
@@ -35,6 +36,18 @@ class InterceptingLLUDPProxyProtocol(UDPProxyProtocol):
|
||||
)
|
||||
self.message_xml = MessageDotXML()
|
||||
self.session: Optional[Session] = None
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
self.resend_task = loop.create_task(self.attempt_resends())
|
||||
|
||||
async def attempt_resends(self):
|
||||
while True:
|
||||
await asyncio.sleep(0.1)
|
||||
if self.session is None:
|
||||
continue
|
||||
for region in self.session.regions:
|
||||
if not region.circuit or not region.circuit.is_alive:
|
||||
continue
|
||||
region.circuit.resend_unacked()
|
||||
|
||||
def _ensure_message_allowed(self, msg: Message):
|
||||
if not self.message_xml.validate_udp_msg(msg.name):
|
||||
@@ -99,6 +112,9 @@ class InterceptingLLUDPProxyProtocol(UDPProxyProtocol):
|
||||
LOG.error("No circuit for %r, dropping packet!" % (packet.far_addr,))
|
||||
return
|
||||
|
||||
# Process any ACKs for messages we injected first
|
||||
region.circuit.collect_acks(message)
|
||||
|
||||
if message.name == "AgentMovementComplete":
|
||||
self.session.main_region = region
|
||||
if region.handle is None:
|
||||
@@ -145,10 +161,12 @@ class InterceptingLLUDPProxyProtocol(UDPProxyProtocol):
|
||||
region.mark_dead()
|
||||
elif message.name == "RegionHandshake":
|
||||
region.name = str(message["RegionInfo"][0]["SimName"])
|
||||
elif message.name == "AgentDataUpdate" and self.session:
|
||||
self.session.active_group = message["AgentData"]["ActiveGroupID"]
|
||||
|
||||
# Send the message if it wasn't explicitly dropped or sent before
|
||||
if not message.finalized:
|
||||
region.circuit.send_message(message)
|
||||
region.circuit.send(message)
|
||||
|
||||
def close(self):
|
||||
super().close()
|
||||
@@ -156,3 +174,4 @@ class InterceptingLLUDPProxyProtocol(UDPProxyProtocol):
|
||||
AddonManager.handle_session_closed(self.session)
|
||||
self.session_manager.close_session(self.session)
|
||||
self.session = None
|
||||
self.resend_task.cancel()
|
||||
|
||||
@@ -3,7 +3,7 @@ import ast
|
||||
import typing
|
||||
|
||||
from arpeggio import Optional, ZeroOrMore, EOF, \
|
||||
ParserPython, PTNodeVisitor, visit_parse_tree, RegExMatch
|
||||
ParserPython, PTNodeVisitor, visit_parse_tree, RegExMatch, OneOrMore
|
||||
|
||||
|
||||
def literal():
|
||||
@@ -26,7 +26,9 @@ def literal():
|
||||
|
||||
|
||||
def identifier():
|
||||
return RegExMatch(r'[a-zA-Z*]([a-zA-Z0-9_*]+)?')
|
||||
# Identifiers are allowed to have "-". It's not a special character
|
||||
# in our grammar, and we expect them to show up some places, like header names.
|
||||
return RegExMatch(r'[a-zA-Z*]([a-zA-Z0-9_*-]+)?')
|
||||
|
||||
|
||||
def field_specifier():
|
||||
@@ -42,7 +44,7 @@ def unary_expression():
|
||||
|
||||
|
||||
def meta_field_specifier():
|
||||
return "Meta", ".", identifier
|
||||
return "Meta", OneOrMore(".", identifier)
|
||||
|
||||
|
||||
def enum_field_specifier():
|
||||
@@ -69,12 +71,17 @@ def message_filter():
|
||||
return expression, EOF
|
||||
|
||||
|
||||
MATCH_RESULT = typing.Union[bool, typing.Tuple]
|
||||
class MatchResult(typing.NamedTuple):
|
||||
result: bool
|
||||
fields: typing.List[typing.Tuple]
|
||||
|
||||
def __bool__(self):
|
||||
return self.result
|
||||
|
||||
|
||||
class BaseFilterNode(abc.ABC):
|
||||
@abc.abstractmethod
|
||||
def match(self, msg) -> MATCH_RESULT:
|
||||
def match(self, msg, short_circuit=True) -> MatchResult:
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
@@ -104,18 +111,36 @@ class BinaryFilterNode(BaseFilterNode, abc.ABC):
|
||||
|
||||
|
||||
class UnaryNotFilterNode(UnaryFilterNode):
|
||||
def match(self, msg) -> MATCH_RESULT:
|
||||
return not self.node.match(msg)
|
||||
def match(self, msg, short_circuit=True) -> MatchResult:
|
||||
# Should we pass fields up here? Maybe not.
|
||||
return MatchResult(not self.node.match(msg, short_circuit), [])
|
||||
|
||||
|
||||
class OrFilterNode(BinaryFilterNode):
|
||||
def match(self, msg) -> MATCH_RESULT:
|
||||
return self.left_node.match(msg) or self.right_node.match(msg)
|
||||
def match(self, msg, short_circuit=True) -> MatchResult:
|
||||
left_match = self.left_node.match(msg, short_circuit)
|
||||
if left_match and short_circuit:
|
||||
return MatchResult(True, left_match.fields)
|
||||
|
||||
right_match = self.right_node.match(msg, short_circuit)
|
||||
if right_match and short_circuit:
|
||||
return MatchResult(True, right_match.fields)
|
||||
|
||||
if left_match or right_match:
|
||||
# Fine since fields should be empty when result=False
|
||||
return MatchResult(True, left_match.fields + right_match.fields)
|
||||
return MatchResult(False, [])
|
||||
|
||||
|
||||
class AndFilterNode(BinaryFilterNode):
|
||||
def match(self, msg) -> MATCH_RESULT:
|
||||
return self.left_node.match(msg) and self.right_node.match(msg)
|
||||
def match(self, msg, short_circuit=True) -> MatchResult:
|
||||
left_match = self.left_node.match(msg, short_circuit)
|
||||
if not left_match:
|
||||
return MatchResult(False, [])
|
||||
right_match = self.right_node.match(msg, short_circuit)
|
||||
if not right_match:
|
||||
return MatchResult(False, [])
|
||||
return MatchResult(True, left_match.fields + right_match.fields)
|
||||
|
||||
|
||||
class MessageFilterNode(BaseFilterNode):
|
||||
@@ -124,15 +149,15 @@ class MessageFilterNode(BaseFilterNode):
|
||||
self.operator = operator
|
||||
self.value = value
|
||||
|
||||
def match(self, msg) -> MATCH_RESULT:
|
||||
return msg.matches(self)
|
||||
def match(self, msg, short_circuit=True) -> MatchResult:
|
||||
return msg.matches(self, short_circuit)
|
||||
|
||||
@property
|
||||
def children(self):
|
||||
return self.selector, self.operator, self.value
|
||||
|
||||
|
||||
class MetaFieldSpecifier(str):
|
||||
class MetaFieldSpecifier(tuple):
|
||||
pass
|
||||
|
||||
|
||||
@@ -158,7 +183,7 @@ class MessageFilterVisitor(PTNodeVisitor):
|
||||
return LiteralValue(ast.literal_eval(node.value))
|
||||
|
||||
def visit_meta_field_specifier(self, _node, children):
|
||||
return MetaFieldSpecifier(children[0])
|
||||
return MetaFieldSpecifier(children)
|
||||
|
||||
def visit_enum_field_specifier(self, _node, children):
|
||||
return EnumFieldSpecifier(*children)
|
||||
|
||||
@@ -16,12 +16,16 @@ import weakref
|
||||
from defusedxml import minidom
|
||||
|
||||
from hippolyzer.lib.base import serialization as se, llsd
|
||||
from hippolyzer.lib.base.message.llsd_msg_serializer import LLSDMessageSerializer
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.datatypes import TaggedUnion, UUID, TupleCoord
|
||||
from hippolyzer.lib.base.helpers import bytes_escape
|
||||
from hippolyzer.lib.base.message.message_formatting import HumanMessageSerializer
|
||||
from hippolyzer.lib.base.message.msgtypes import PacketFlags
|
||||
from hippolyzer.lib.base.message.template_dict import DEFAULT_TEMPLATE_DICT
|
||||
from hippolyzer.lib.base.network.transport import Direction
|
||||
from hippolyzer.lib.proxy.message_filter import MetaFieldSpecifier, compile_filter, BaseFilterNode, MessageFilterNode, \
|
||||
EnumFieldSpecifier
|
||||
EnumFieldSpecifier, MatchResult
|
||||
from hippolyzer.lib.proxy.http_flow import HippoHTTPFlow
|
||||
from hippolyzer.lib.proxy.caps import CapType, SerializedCapData
|
||||
|
||||
@@ -235,7 +239,7 @@ class AbstractMessageLogEntry(abc.ABC):
|
||||
obj = self.region.objects.lookup_localid(selected_local)
|
||||
return obj and obj.FullID
|
||||
|
||||
def _get_meta(self, name: str):
|
||||
def _get_meta(self, name: str) -> typing.Any:
|
||||
# Slight difference in semantics. Filters are meant to return the same
|
||||
# thing no matter when they're run, so SelectedLocal and friends resolve
|
||||
# to the selected items _at the time the message was logged_. To handle
|
||||
@@ -308,7 +312,9 @@ class AbstractMessageLogEntry(abc.ABC):
|
||||
|
||||
def _val_matches(self, operator, val, expected):
|
||||
if isinstance(expected, MetaFieldSpecifier):
|
||||
expected = self._get_meta(str(expected))
|
||||
if len(expected) != 1:
|
||||
raise ValueError(f"Can only support single-level Meta specifiers, not {expected!r}")
|
||||
expected = self._get_meta(str(expected[0]))
|
||||
if not isinstance(expected, (int, float, bytes, str, type(None), tuple)):
|
||||
if callable(expected):
|
||||
expected = expected()
|
||||
@@ -362,12 +368,18 @@ class AbstractMessageLogEntry(abc.ABC):
|
||||
if matcher.value or matcher.operator:
|
||||
return False
|
||||
return self._packet_root_matches(matcher.selector[0])
|
||||
if len(matcher.selector) == 2 and matcher.selector[0] == "Meta":
|
||||
return self._val_matches(matcher.operator, self._get_meta(matcher.selector[1]), matcher.value)
|
||||
if matcher.selector[0] == "Meta":
|
||||
if len(matcher.selector) == 2:
|
||||
return self._val_matches(matcher.operator, self._get_meta(matcher.selector[1]), matcher.value)
|
||||
elif len(matcher.selector) == 3:
|
||||
meta_dict = self._get_meta(matcher.selector[1])
|
||||
if not meta_dict or not hasattr(meta_dict, 'get'):
|
||||
return False
|
||||
return self._val_matches(matcher.operator, meta_dict.get(matcher.selector[2]), matcher.value)
|
||||
return None
|
||||
|
||||
def matches(self, matcher: "MessageFilterNode"):
|
||||
return self._base_matches(matcher) or False
|
||||
def matches(self, matcher: "MessageFilterNode", short_circuit=True) -> "MatchResult":
|
||||
return MatchResult(self._base_matches(matcher) or False, [])
|
||||
|
||||
@property
|
||||
def seq(self):
|
||||
@@ -388,6 +400,14 @@ class AbstractMessageLogEntry(abc.ABC):
|
||||
xmlified = re.sub(rb" <key>", b"<key>", xmlified)
|
||||
return xmlified.decode("utf8", errors="replace")
|
||||
|
||||
@staticmethod
|
||||
def _format_xml(content):
|
||||
beautified = minidom.parseString(content).toprettyxml(indent=" ")
|
||||
# kill blank lines. will break cdata sections. meh.
|
||||
beautified = re.sub(r'\n\s*\n', '\n', beautified, flags=re.MULTILINE)
|
||||
return re.sub(r'<(\w+)>\s*</\1>', r'<\1></\1>',
|
||||
beautified, flags=re.MULTILINE)
|
||||
|
||||
|
||||
class HTTPMessageLogEntry(AbstractMessageLogEntry):
|
||||
__slots__ = ["flow"]
|
||||
@@ -400,7 +420,7 @@ class HTTPMessageLogEntry(AbstractMessageLogEntry):
|
||||
|
||||
super().__init__(region, session)
|
||||
# This was a request the proxy made through itself
|
||||
self.meta["Injected"] = flow.request_injected
|
||||
self.meta["Synthetic"] = flow.request_injected
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
@@ -476,13 +496,17 @@ class HTTPMessageLogEntry(AbstractMessageLogEntry):
|
||||
if not beautified:
|
||||
content_type = self._guess_content_type(message)
|
||||
if content_type.startswith("application/llsd"):
|
||||
beautified = self._format_llsd(llsd.parse(message.content))
|
||||
try:
|
||||
beautified = self._format_llsd(llsd.parse(message.content))
|
||||
except llsd.LLSDParseError:
|
||||
# Sometimes LL sends plain XML with a Content-Type of application/llsd+xml.
|
||||
# Try to detect that case and work around it
|
||||
if content_type == "application/llsd+xml" and message.content.startswith(b'<'):
|
||||
beautified = self._format_xml(message.content)
|
||||
else:
|
||||
raise
|
||||
elif any(content_type.startswith(x) for x in ("application/xml", "text/xml")):
|
||||
beautified = minidom.parseString(message.content).toprettyxml(indent=" ")
|
||||
# kill blank lines. will break cdata sections. meh.
|
||||
beautified = re.sub(r'\n\s*\n', '\n', beautified, flags=re.MULTILINE)
|
||||
beautified = re.sub(r'<([\w]+)>\s*</\1>', r'<\1></\1>',
|
||||
beautified, flags=re.MULTILINE)
|
||||
beautified = self._format_xml(message.content)
|
||||
except:
|
||||
LOG.exception("Failed to beautify message")
|
||||
|
||||
@@ -502,7 +526,7 @@ class HTTPMessageLogEntry(AbstractMessageLogEntry):
|
||||
buf.write(bytes(headers).decode("utf8", errors="replace"))
|
||||
buf.write("\r\n")
|
||||
|
||||
buf.write(message_body)
|
||||
buf.write(message_body or "")
|
||||
return buf.getvalue()
|
||||
|
||||
def request(self, beautify=False, replacements=None):
|
||||
@@ -529,6 +553,12 @@ class HTTPMessageLogEntry(AbstractMessageLogEntry):
|
||||
return self._summary
|
||||
|
||||
def _guess_content_type(self, message):
|
||||
# SL's login service lies and says that its XML-RPC response is LLSD+XML.
|
||||
# It is not, and it blows up the parser. It's been broken ever since the
|
||||
# login rewrite and a fix is likely not forthcoming. I'm sick of seeing
|
||||
# the traceback, so just hack around it.
|
||||
if self.name == "LoginRequest":
|
||||
return "application/xml"
|
||||
content_type = message.headers.get("Content-Type", "")
|
||||
if not message.content or content_type.startswith("application/llsd"):
|
||||
return content_type
|
||||
@@ -541,6 +571,20 @@ class HTTPMessageLogEntry(AbstractMessageLogEntry):
|
||||
return "application/xml"
|
||||
return content_type
|
||||
|
||||
def _get_meta(self, name: str) -> typing.Any:
|
||||
lower_name = name.lower()
|
||||
if lower_name == "url":
|
||||
return self.flow.request.url
|
||||
elif lower_name == "reqheaders":
|
||||
return self.flow.request.headers
|
||||
elif lower_name == "respheaders":
|
||||
return self.flow.response.headers
|
||||
elif lower_name == "host":
|
||||
return self.flow.request.host.lower()
|
||||
elif lower_name == "status":
|
||||
return self.flow.response.status_code
|
||||
return super()._get_meta(name)
|
||||
|
||||
def to_dict(self):
|
||||
val = super().to_dict()
|
||||
val['flow'] = self.flow.get_state()
|
||||
@@ -574,6 +618,19 @@ class EQMessageLogEntry(AbstractMessageLogEntry):
|
||||
return "EQ"
|
||||
|
||||
def request(self, beautify=False, replacements=None):
|
||||
# TODO: This is a bit of a hack! Templated messages can be sent over the EQ, so let's
|
||||
# display them as template messages if that's what they are.
|
||||
if self.event['message'] in DEFAULT_TEMPLATE_DICT.message_templates:
|
||||
msg = LLSDMessageSerializer().deserialize(self.event)
|
||||
msg.synthetic = True
|
||||
msg.send_flags = PacketFlags.EQ
|
||||
msg.direction = Direction.IN
|
||||
# Annoyingly, templated messages sent over the EQ can have extra fields not specified
|
||||
# in the template, and this is often the case. ParcelProperties has fields that aren't
|
||||
# in the template. Luckily, we don't really care about extra fields, we just may not
|
||||
# be able to automatically decode U32 and friends without the hint from the template
|
||||
# that that is what they are.
|
||||
return HumanMessageSerializer.to_human_string(msg, replacements, beautify)
|
||||
return f'EQ {self.event["message"]}\n\n{self._format_llsd(self.event["body"])}'
|
||||
|
||||
@property
|
||||
@@ -613,7 +670,7 @@ class LLUDPMessageLogEntry(AbstractMessageLogEntry):
|
||||
super().__init__(region, session)
|
||||
|
||||
_MESSAGE_META_ATTRS = {
|
||||
"Injected", "Dropped", "Extra", "Resent", "Zerocoded", "Acks", "Reliable",
|
||||
"Synthetic", "Dropped", "Extra", "Resent", "Zerocoded", "Acks", "Reliable",
|
||||
}
|
||||
|
||||
def _get_meta(self, name: str):
|
||||
@@ -671,20 +728,21 @@ class LLUDPMessageLogEntry(AbstractMessageLogEntry):
|
||||
def request(self, beautify=False, replacements=None):
|
||||
return HumanMessageSerializer.to_human_string(self.message, replacements, beautify)
|
||||
|
||||
def matches(self, matcher):
|
||||
def matches(self, matcher, short_circuit=True) -> "MatchResult":
|
||||
base_matched = self._base_matches(matcher)
|
||||
if base_matched is not None:
|
||||
return base_matched
|
||||
return MatchResult(base_matched, [])
|
||||
|
||||
if not self._packet_root_matches(matcher.selector[0]):
|
||||
return False
|
||||
return MatchResult(False, [])
|
||||
|
||||
message = self.message
|
||||
|
||||
selector_len = len(matcher.selector)
|
||||
# name, block_name, var_name(, subfield_name)?
|
||||
if selector_len not in (3, 4):
|
||||
return False
|
||||
return MatchResult(False, [])
|
||||
found_field_keys = []
|
||||
for block_name in message.blocks:
|
||||
if not fnmatch.fnmatchcase(block_name, matcher.selector[1]):
|
||||
continue
|
||||
@@ -693,13 +751,13 @@ class LLUDPMessageLogEntry(AbstractMessageLogEntry):
|
||||
if not fnmatch.fnmatchcase(var_name, matcher.selector[2]):
|
||||
continue
|
||||
# So we know where the match happened
|
||||
span_key = (message.name, block_name, block_num, var_name)
|
||||
field_key = (message.name, block_name, block_num, var_name)
|
||||
if selector_len == 3:
|
||||
# We're just matching on the var existing, not having any particular value
|
||||
if matcher.value is None:
|
||||
return span_key
|
||||
if self._val_matches(matcher.operator, block[var_name], matcher.value):
|
||||
return span_key
|
||||
found_field_keys.append(field_key)
|
||||
elif self._val_matches(matcher.operator, block[var_name], matcher.value):
|
||||
found_field_keys.append(field_key)
|
||||
# Need to invoke a special unpacker
|
||||
elif selector_len == 4:
|
||||
try:
|
||||
@@ -710,15 +768,21 @@ class LLUDPMessageLogEntry(AbstractMessageLogEntry):
|
||||
if isinstance(deserialized, TaggedUnion):
|
||||
deserialized = deserialized.value
|
||||
if not isinstance(deserialized, dict):
|
||||
return False
|
||||
continue
|
||||
for key in deserialized.keys():
|
||||
if fnmatch.fnmatchcase(str(key), matcher.selector[3]):
|
||||
if matcher.value is None:
|
||||
return span_key
|
||||
if self._val_matches(matcher.operator, deserialized[key], matcher.value):
|
||||
return span_key
|
||||
# Short-circuiting checking individual subfields is fine since
|
||||
# we only highlight fields anyway.
|
||||
found_field_keys.append(field_key)
|
||||
break
|
||||
elif self._val_matches(matcher.operator, deserialized[key], matcher.value):
|
||||
found_field_keys.append(field_key)
|
||||
break
|
||||
|
||||
return False
|
||||
if short_circuit and found_field_keys:
|
||||
return MatchResult(True, found_field_keys)
|
||||
return MatchResult(bool(found_field_keys), found_field_keys)
|
||||
|
||||
@property
|
||||
def summary(self):
|
||||
|
||||
@@ -11,7 +11,7 @@ from hippolyzer.lib.base.templates import PCode
|
||||
from hippolyzer.lib.client.namecache import NameCache
|
||||
from hippolyzer.lib.client.object_manager import (
|
||||
ClientObjectManager,
|
||||
UpdateType, ClientWorldObjectManager,
|
||||
ObjectUpdateType, ClientWorldObjectManager,
|
||||
)
|
||||
|
||||
from hippolyzer.lib.base.objects import Object
|
||||
@@ -48,6 +48,7 @@ class ProxyObjectManager(ClientObjectManager):
|
||||
"RequestMultipleObjects",
|
||||
self._handle_request_multiple_objects,
|
||||
)
|
||||
region.http_message_handler.subscribe("RenderMaterials", self._handle_render_materials)
|
||||
|
||||
def load_cache(self):
|
||||
if not self.may_use_vo_cache or self.cache_loaded:
|
||||
@@ -63,18 +64,25 @@ class ProxyObjectManager(ClientObjectManager):
|
||||
cache_dir=self._region.session().cache_dir,
|
||||
)
|
||||
|
||||
def request_missed_cached_objects_soon(self):
|
||||
def request_missed_cached_objects_soon(self, report_only=False):
|
||||
if self._cache_miss_timer:
|
||||
self._cache_miss_timer.cancel()
|
||||
# Basically debounce. Will only trigger 0.2 seconds after the last time it's invoked to
|
||||
# deal with the initial flood of ObjectUpdateCached and the natural lag time between that
|
||||
# and the viewers' RequestMultipleObjects messages
|
||||
self._cache_miss_timer = asyncio.get_event_loop().call_later(
|
||||
0.2, self._request_missed_cached_objects)
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
self._cache_miss_timer = loop.call_later(0.2, self._request_missed_cached_objects, report_only)
|
||||
|
||||
def _request_missed_cached_objects(self):
|
||||
def _request_missed_cached_objects(self, report_only: bool):
|
||||
self._cache_miss_timer = None
|
||||
self.request_objects(self.queued_cache_misses)
|
||||
if not self.queued_cache_misses:
|
||||
# All the queued cache misses ended up being satisfied without us
|
||||
# having to request them, no need to fire off a request.
|
||||
return
|
||||
if report_only:
|
||||
print(f"Would have automatically requested {self.queued_cache_misses!r}")
|
||||
else:
|
||||
self.request_objects(self.queued_cache_misses)
|
||||
self.queued_cache_misses.clear()
|
||||
|
||||
def clear(self):
|
||||
@@ -93,6 +101,13 @@ class ProxyObjectManager(ClientObjectManager):
|
||||
# Remove any queued cache misses that the viewer just requested for itself
|
||||
self.queued_cache_misses -= {b["ID"] for b in msg["ObjectData"]}
|
||||
|
||||
def _handle_render_materials(self, flow: HippoHTTPFlow):
|
||||
if flow.response.status_code != 200:
|
||||
return
|
||||
if flow.request.method not in ("GET", "POST"):
|
||||
return
|
||||
self._process_materials_response(flow.response.content)
|
||||
|
||||
|
||||
class ProxyWorldObjectManager(ClientWorldObjectManager):
|
||||
_session: Session
|
||||
@@ -110,9 +125,12 @@ class ProxyWorldObjectManager(ClientWorldObjectManager):
|
||||
)
|
||||
|
||||
def _handle_object_update_cached_misses(self, region_handle: int, missing_locals: Set[int]):
|
||||
region_mgr: Optional[ProxyObjectManager] = self._get_region_manager(region_handle)
|
||||
if not self._settings.ALLOW_AUTO_REQUEST_OBJECTS:
|
||||
return
|
||||
if self._settings.AUTOMATICALLY_REQUEST_MISSING_OBJECTS:
|
||||
if self._settings.USE_VIEWER_OBJECT_CACHE:
|
||||
region_mgr.queued_cache_misses |= missing_locals
|
||||
region_mgr.request_missed_cached_objects_soon(report_only=True)
|
||||
elif self._settings.AUTOMATICALLY_REQUEST_MISSING_OBJECTS:
|
||||
# Schedule these local IDs to be requested soon if the viewer doesn't request
|
||||
# them itself. Ideally we could just mutate the CRC of the ObjectUpdateCached
|
||||
# to force a CRC cache miss in the viewer, but that appears to cause the viewer
|
||||
@@ -123,19 +141,20 @@ class ProxyWorldObjectManager(ClientWorldObjectManager):
|
||||
region_mgr.queued_cache_misses |= missing_locals
|
||||
region_mgr.request_missed_cached_objects_soon()
|
||||
|
||||
def _run_object_update_hooks(self, obj: Object, updated_props: Set[str], update_type: UpdateType):
|
||||
super()._run_object_update_hooks(obj, updated_props, update_type)
|
||||
def _run_object_update_hooks(self, obj: Object, updated_props: Set[str], update_type: ObjectUpdateType,
|
||||
msg: Optional[Message]):
|
||||
super()._run_object_update_hooks(obj, updated_props, update_type, msg)
|
||||
region = self._session.region_by_handle(obj.RegionHandle)
|
||||
if self._settings.ALLOW_AUTO_REQUEST_OBJECTS:
|
||||
if obj.PCode == PCode.AVATAR and "ParentID" in updated_props:
|
||||
if obj.ParentID and not region.objects.lookup_localid(obj.ParentID):
|
||||
# If an avatar just sat on an object we don't know about, add it to the queued
|
||||
# cache misses and request if if the viewer doesn't. This should happen
|
||||
# regardless of the auto-request object setting because otherwise we have no way
|
||||
# to get a sitting agent's true region location, even if it's ourself.
|
||||
# cache misses and request it if the viewer doesn't. This should happen
|
||||
# regardless of the auto-request missing objects setting because otherwise we
|
||||
# have no way to get a sitting agent's true region location, even if it's ourselves.
|
||||
region.objects.queued_cache_misses.add(obj.ParentID)
|
||||
region.objects.request_missed_cached_objects_soon()
|
||||
AddonManager.handle_object_updated(self._session, region, obj, updated_props)
|
||||
AddonManager.handle_object_updated(self._session, region, obj, updated_props, msg)
|
||||
|
||||
def _run_kill_object_hooks(self, obj: Object):
|
||||
super()._run_kill_object_hooks(obj)
|
||||
|
||||
18
hippolyzer/lib/proxy/parcel_manager.py
Normal file
18
hippolyzer/lib/proxy/parcel_manager.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from typing import *
|
||||
|
||||
from hippolyzer.lib.base.helpers import proxify
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.client.parcel_manager import ParcelManager
|
||||
if TYPE_CHECKING:
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
|
||||
|
||||
class ProxyParcelManager(ParcelManager):
|
||||
def __init__(self, region: "ProxiedRegion"):
|
||||
super().__init__(proxify(region))
|
||||
# Handle ParcelProperties messages that we didn't specifically ask for
|
||||
self._region.message_handler.subscribe("ParcelProperties", self._handle_parcel_properties)
|
||||
|
||||
def _handle_parcel_properties(self, msg: Message):
|
||||
self._process_parcel_properties(msg)
|
||||
return None
|
||||
@@ -1,6 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import hashlib
|
||||
import uuid
|
||||
import weakref
|
||||
@@ -9,11 +8,11 @@ import urllib.parse
|
||||
|
||||
import multidict
|
||||
|
||||
from hippolyzer.lib.base.datatypes import Vector3, UUID
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.helpers import proxify
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.message.llsd_msg_serializer import LLSDMessageSerializer
|
||||
from hippolyzer.lib.base.message.message import Message, Block
|
||||
from hippolyzer.lib.base.message.message_handler import MessageHandler
|
||||
from hippolyzer.lib.base.objects import handle_to_global_pos
|
||||
from hippolyzer.lib.client.state import BaseClientRegion
|
||||
from hippolyzer.lib.proxy.caps_client import ProxyCapsClient
|
||||
from hippolyzer.lib.proxy.circuit import ProxiedCircuit
|
||||
@@ -21,6 +20,8 @@ from hippolyzer.lib.proxy.caps import CapType
|
||||
from hippolyzer.lib.proxy.object_manager import ProxyObjectManager
|
||||
from hippolyzer.lib.base.transfer_manager import TransferManager
|
||||
from hippolyzer.lib.base.xfer_manager import XferManager
|
||||
from hippolyzer.lib.proxy.asset_uploader import ProxyAssetUploader
|
||||
from hippolyzer.lib.proxy.parcel_manager import ProxyParcelManager
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from hippolyzer.lib.proxy.sessions import Session
|
||||
@@ -42,19 +43,21 @@ class CapsMultiDict(multidict.MultiDict[Tuple[CapType, str]]):
|
||||
|
||||
|
||||
class ProxiedRegion(BaseClientRegion):
|
||||
circuit: Optional[ProxiedCircuit]
|
||||
|
||||
def __init__(self, circuit_addr, seed_cap: str, session: Session, handle=None):
|
||||
super().__init__()
|
||||
# A client may make a Seed request twice, and may get back two (valid!) sets of
|
||||
# Cap URIs. We need to be able to look up both, so MultiDict is necessary.
|
||||
self.handle: Optional[int] = handle
|
||||
self._name: Optional[str] = None
|
||||
# TODO: when does this change?
|
||||
self.cache_id: Optional[UUID] = None
|
||||
self.circuit: Optional[ProxiedCircuit] = None
|
||||
self.circuit_addr = circuit_addr
|
||||
self._caps = CapsMultiDict()
|
||||
self.caps = CapsMultiDict()
|
||||
# Reverse lookup for URL -> cap data
|
||||
self._caps_url_lookup: Dict[str, Tuple[CapType, str]] = {}
|
||||
if seed_cap:
|
||||
self._caps["Seed"] = (CapType.NORMAL, seed_cap)
|
||||
self.caps["Seed"] = (CapType.NORMAL, seed_cap)
|
||||
self.session: Callable[[], Session] = weakref.ref(session)
|
||||
self.message_handler: MessageHandler[Message, str] = MessageHandler()
|
||||
self.http_message_handler: MessageHandler[HippoHTTPFlow, str] = MessageHandler()
|
||||
@@ -64,43 +67,23 @@ class ProxiedRegion(BaseClientRegion):
|
||||
self.objects: ProxyObjectManager = ProxyObjectManager(self, may_use_vo_cache=True)
|
||||
self.xfer_manager = XferManager(proxify(self), self.session().secure_session_id)
|
||||
self.transfer_manager = TransferManager(proxify(self), session.agent_id, session.id)
|
||||
self.asset_uploader = ProxyAssetUploader(proxify(self))
|
||||
self.parcel_manager = ProxyParcelManager(proxify(self))
|
||||
self._recalc_caps()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
if self._name:
|
||||
return self._name
|
||||
return "Pending %r" % (self.circuit_addr,)
|
||||
|
||||
@name.setter
|
||||
def name(self, val):
|
||||
self._name = val
|
||||
|
||||
@property
|
||||
def caps(self):
|
||||
return multidict.MultiDict((x, y[1]) for x, y in self._caps.items())
|
||||
|
||||
@property
|
||||
def global_pos(self) -> Vector3:
|
||||
if self.handle is None:
|
||||
raise ValueError("Can't determine global region position without handle")
|
||||
return handle_to_global_pos(self.handle)
|
||||
|
||||
@property
|
||||
def is_alive(self):
|
||||
if not self.circuit:
|
||||
return False
|
||||
return self.circuit.is_alive
|
||||
def cap_urls(self) -> multidict.MultiDict[str]:
|
||||
return multidict.MultiDict((x, y[1]) for x, y in self.caps.items())
|
||||
|
||||
def update_caps(self, caps: Mapping[str, str]):
|
||||
for cap_name, cap_url in caps.items():
|
||||
if isinstance(cap_url, str) and cap_url.startswith('http'):
|
||||
self._caps.add(cap_name, (CapType.NORMAL, cap_url))
|
||||
self.caps.add(cap_name, (CapType.NORMAL, cap_url))
|
||||
self._recalc_caps()
|
||||
|
||||
def _recalc_caps(self):
|
||||
self._caps_url_lookup.clear()
|
||||
for name, cap_info in self._caps.items():
|
||||
for name, cap_info in self.caps.items():
|
||||
cap_type, cap_url = cap_info
|
||||
self._caps_url_lookup[cap_url] = (cap_type, name)
|
||||
|
||||
@@ -109,32 +92,35 @@ class ProxiedRegion(BaseClientRegion):
|
||||
Wrap an existing, non-unique cap with a unique URL
|
||||
|
||||
caps like ViewerAsset may be the same globally and wouldn't let us infer
|
||||
which session / region the request was related to without a wrapper
|
||||
which session / region the request was related to without a wrapper URL
|
||||
that we inject into the seed response sent to the viewer.
|
||||
"""
|
||||
parsed = list(urllib.parse.urlsplit(self._caps[name][1]))
|
||||
seed_id = self._caps["Seed"][1].split("/")[-1].encode("utf8")
|
||||
parsed = list(urllib.parse.urlsplit(self.caps[name][1]))
|
||||
seed_id = self.caps["Seed"][1].split("/")[-1].encode("utf8")
|
||||
# Give it a unique domain tied to the current Seed URI
|
||||
parsed[1] = f"{name.lower()}-{hashlib.sha256(seed_id).hexdigest()[:16]}.hippo-proxy.localhost"
|
||||
# Force the URL to HTTP, we're going to handle the request ourselves so it doesn't need
|
||||
# to be secure. This should save on expensive TLS context setup for each req.
|
||||
parsed[0] = "http"
|
||||
wrapper_url = urllib.parse.urlunsplit(parsed)
|
||||
self._caps.add(name + "ProxyWrapper", (CapType.WRAPPER, wrapper_url))
|
||||
self._recalc_caps()
|
||||
# Register it with "ProxyWrapper" appended so we don't shadow the real cap URL
|
||||
# in our own view of the caps
|
||||
self.register_cap(name + "ProxyWrapper", wrapper_url, CapType.WRAPPER)
|
||||
return wrapper_url
|
||||
|
||||
def register_proxy_cap(self, name: str):
|
||||
"""
|
||||
Register a cap to be completely handled by the proxy
|
||||
"""
|
||||
cap_url = f"https://caps.hippo-proxy.localhost/cap/{uuid.uuid4()!s}"
|
||||
self._caps.add(name, (CapType.PROXY_ONLY, cap_url))
|
||||
self._recalc_caps()
|
||||
"""Register a cap to be completely handled by the proxy"""
|
||||
if name in self.caps:
|
||||
# If we have an existing cap then we should just use that.
|
||||
cap_data = self.caps[name]
|
||||
if cap_data[1] == CapType.PROXY_ONLY:
|
||||
return cap_data[0]
|
||||
cap_url = f"http://{uuid.uuid4()!s}.caps.hippo-proxy.localhost"
|
||||
self.register_cap(name, cap_url, CapType.PROXY_ONLY)
|
||||
return cap_url
|
||||
|
||||
def register_temporary_cap(self, name: str, cap_url: str):
|
||||
"""Register a Cap that only has meaning the first time it's used"""
|
||||
self._caps.add(name, (CapType.TEMPORARY, cap_url))
|
||||
def register_cap(self, name: str, cap_url: str, cap_type: CapType = CapType.NORMAL):
|
||||
self.caps.add(name, (cap_type, cap_url))
|
||||
self._recalc_caps()
|
||||
|
||||
def resolve_cap(self, url: str, consume=True) -> Optional[Tuple[str, str, CapType]]:
|
||||
@@ -143,23 +129,17 @@ class ProxiedRegion(BaseClientRegion):
|
||||
cap_type, name = self._caps_url_lookup[cap_url]
|
||||
if cap_type == CapType.TEMPORARY and consume:
|
||||
# Resolving a temporary cap pops it out of the dict
|
||||
temporary_caps = self._caps.popall(name)
|
||||
temporary_caps = self.caps.popall(name)
|
||||
temporary_caps.remove((cap_type, cap_url))
|
||||
self._caps.extend((name, x) for x in temporary_caps)
|
||||
self.caps.extend((name, x) for x in temporary_caps)
|
||||
self._recalc_caps()
|
||||
return name, cap_url, cap_type
|
||||
return None
|
||||
|
||||
def mark_dead(self):
|
||||
logging.info("Marking %r dead" % self)
|
||||
if self.circuit:
|
||||
self.circuit.is_alive = False
|
||||
self.objects.clear()
|
||||
super().mark_dead()
|
||||
self.eq_manager.clear()
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s>" % (self.__class__.__name__, self.name)
|
||||
|
||||
|
||||
class EventQueueManager:
|
||||
def __init__(self, region: ProxiedRegion):
|
||||
@@ -168,9 +148,26 @@ class EventQueueManager:
|
||||
self._region = weakref.proxy(region)
|
||||
self._last_ack: Optional[int] = None
|
||||
self._last_payload: Optional[Any] = None
|
||||
self.llsd_message_serializer = LLSDMessageSerializer()
|
||||
|
||||
def inject_message(self, message: Message):
|
||||
self.inject_event(self.llsd_message_serializer.serialize(message, True))
|
||||
|
||||
def inject_event(self, event: dict):
|
||||
self._queued_events.append(event)
|
||||
if self._region:
|
||||
circuit: ProxiedCircuit = self._region.circuit
|
||||
session: Session = self._region.session()
|
||||
# Inject an outbound PlacesQuery message so we can trigger an inbound PlacesReply
|
||||
# over the EQ. That will allow us to shove our own event onto the response once it comes in,
|
||||
# otherwise we have to wait until the EQ legitimately returns 200 due to a new event.
|
||||
# May or may not work in OpenSim.
|
||||
circuit.send(Message(
|
||||
'PlacesQuery',
|
||||
Block('AgentData', AgentID=session.agent_id, SessionID=session.id, QueryID=UUID()),
|
||||
Block('TransactionData', TransactionID=UUID()),
|
||||
Block('QueryData', QueryText=b'', QueryFlags=64, Category=-1, SimName=b''),
|
||||
))
|
||||
|
||||
def take_injected_events(self):
|
||||
events = self._queued_events
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import collections
|
||||
import dataclasses
|
||||
import datetime
|
||||
import functools
|
||||
import logging
|
||||
import multiprocessing
|
||||
import weakref
|
||||
from typing import *
|
||||
from weakref import ref
|
||||
|
||||
from outleap import LEAPClient
|
||||
|
||||
from hippolyzer.lib.base.datatypes import UUID
|
||||
from hippolyzer.lib.base.message.message import Message
|
||||
from hippolyzer.lib.base.helpers import proxify
|
||||
from hippolyzer.lib.base.message.message_handler import MessageHandler
|
||||
from hippolyzer.lib.client.state import BaseClientSession
|
||||
from hippolyzer.lib.base.network.transport import ADDR_TUPLE
|
||||
from hippolyzer.lib.client.state import BaseClientSession, BaseClientSessionManager
|
||||
from hippolyzer.lib.proxy.addons import AddonManager
|
||||
from hippolyzer.lib.proxy.circuit import ProxiedCircuit
|
||||
from hippolyzer.lib.proxy.http_asset_repo import HTTPAssetRepo
|
||||
from hippolyzer.lib.proxy.http_proxy import HTTPFlowContext
|
||||
from hippolyzer.lib.proxy.caps import is_asset_server_cap_name, CapData, CapType
|
||||
from hippolyzer.lib.proxy.inventory_manager import ProxyInventoryManager
|
||||
from hippolyzer.lib.proxy.namecache import ProxyNameCache
|
||||
from hippolyzer.lib.proxy.object_manager import ProxyWorldObjectManager
|
||||
from hippolyzer.lib.proxy.region import ProxiedRegion
|
||||
@@ -29,27 +33,34 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
class Session(BaseClientSession):
|
||||
def __init__(self, session_id, secure_session_id, agent_id, circuit_code,
|
||||
regions: MutableSequence[ProxiedRegion]
|
||||
region_by_handle: Callable[[int], Optional[ProxiedRegion]]
|
||||
region_by_circuit_addr: Callable[[ADDR_TUPLE], Optional[ProxiedRegion]]
|
||||
main_region: Optional[ProxiedRegion]
|
||||
REGION_CLS = ProxiedRegion
|
||||
|
||||
def __init__(self, id, secure_session_id, agent_id, circuit_code,
|
||||
session_manager: Optional[SessionManager], login_data=None):
|
||||
self.login_data = login_data or {}
|
||||
self.pending = True
|
||||
self.id: UUID = session_id
|
||||
self.secure_session_id: UUID = secure_session_id
|
||||
self.agent_id: UUID = agent_id
|
||||
self.circuit_code = circuit_code
|
||||
self.global_caps = {}
|
||||
super().__init__(
|
||||
id=id,
|
||||
secure_session_id=secure_session_id,
|
||||
agent_id=agent_id,
|
||||
circuit_code=circuit_code,
|
||||
session_manager=session_manager,
|
||||
login_data=login_data,
|
||||
)
|
||||
# Bag of arbitrary data addons can use to persist data across addon reloads
|
||||
self.addon_ctx = {}
|
||||
self.session_manager: SessionManager = session_manager or None
|
||||
# Each addon name gets its own separate dict within this dict
|
||||
self.addon_ctx: Dict[str, Dict[str, Any]] = collections.defaultdict(dict)
|
||||
self.session_manager: SessionManager = session_manager
|
||||
self.selected: SelectionModel = SelectionModel()
|
||||
self.regions: List[ProxiedRegion] = []
|
||||
self.started_at = datetime.datetime.now()
|
||||
self.message_handler: MessageHandler[Message, str] = MessageHandler()
|
||||
self.http_message_handler: MessageHandler[HippoHTTPFlow, str] = MessageHandler()
|
||||
self.objects = ProxyWorldObjectManager(self, session_manager.settings, session_manager.name_cache)
|
||||
self.inventory = ProxyInventoryManager(proxify(self))
|
||||
self.leap_client: Optional[LEAPClient] = None
|
||||
# Base path of a newview type cache directory for this session
|
||||
self.cache_dir: Optional[str] = None
|
||||
self._main_region = None
|
||||
|
||||
@property
|
||||
def global_addon_ctx(self):
|
||||
@@ -57,76 +68,13 @@ class Session(BaseClientSession):
|
||||
return {}
|
||||
return self.session_manager.addon_ctx
|
||||
|
||||
@classmethod
|
||||
def from_login_data(cls, login_data, session_manager):
|
||||
sess = Session(
|
||||
session_id=UUID(login_data["session_id"]),
|
||||
secure_session_id=UUID(login_data["secure_session_id"]),
|
||||
agent_id=UUID(login_data["agent_id"]),
|
||||
circuit_code=int(login_data["circuit_code"]),
|
||||
session_manager=session_manager,
|
||||
login_data=login_data,
|
||||
)
|
||||
appearance_service = login_data.get("agent_appearance_service")
|
||||
map_image_service = login_data.get("map-server-url")
|
||||
if appearance_service:
|
||||
sess.global_caps["AppearanceService"] = appearance_service
|
||||
if map_image_service:
|
||||
sess.global_caps["MapImageService"] = map_image_service
|
||||
# Login data also has details about the initial sim
|
||||
sess.register_region(
|
||||
circuit_addr=(login_data["sim_ip"], login_data["sim_port"]),
|
||||
handle=(login_data["region_x"] << 32) | login_data["region_y"],
|
||||
seed_url=login_data["seed_capability"],
|
||||
)
|
||||
return sess
|
||||
|
||||
@property
|
||||
def main_region(self) -> Optional[ProxiedRegion]:
|
||||
if self._main_region and self._main_region() in self.regions:
|
||||
return self._main_region()
|
||||
return None
|
||||
|
||||
@main_region.setter
|
||||
def main_region(self, val: ProxiedRegion):
|
||||
self._main_region = weakref.ref(val)
|
||||
|
||||
def register_region(self, circuit_addr: Optional[Tuple[str, int]] = None,
|
||||
def register_region(self, circuit_addr: Optional[ADDR_TUPLE] = None,
|
||||
seed_url: Optional[str] = None,
|
||||
handle: Optional[int] = None) -> ProxiedRegion:
|
||||
if not any((circuit_addr, seed_url)):
|
||||
raise ValueError("One of circuit_addr and seed_url must be defined!")
|
||||
|
||||
for region in self.regions:
|
||||
if region.circuit_addr == circuit_addr:
|
||||
if seed_url and region.caps.get("Seed") != seed_url:
|
||||
region.update_caps({"Seed": seed_url})
|
||||
if handle:
|
||||
region.handle = handle
|
||||
return region
|
||||
if seed_url and region.caps.get("Seed") == seed_url:
|
||||
return region
|
||||
|
||||
if not circuit_addr:
|
||||
raise ValueError("Can't create region without circuit addr!")
|
||||
|
||||
logging.info("Registering region for %r" % (circuit_addr,))
|
||||
region = ProxiedRegion(circuit_addr, seed_url, self, handle=handle)
|
||||
self.regions.append(region)
|
||||
region: ProxiedRegion = super().register_region(circuit_addr, seed_url, handle) # type: ignore
|
||||
AddonManager.handle_region_registered(self, region)
|
||||
return region
|
||||
|
||||
def region_by_circuit_addr(self, circuit_addr) -> Optional[ProxiedRegion]:
|
||||
for region in self.regions:
|
||||
if region.circuit_addr == circuit_addr and region.circuit:
|
||||
return region
|
||||
return None
|
||||
|
||||
def region_by_handle(self, handle: int) -> Optional[ProxiedRegion]:
|
||||
for region in self.regions:
|
||||
if region.handle == handle:
|
||||
return region
|
||||
return None
|
||||
|
||||
def open_circuit(self, near_addr, circuit_addr, transport):
|
||||
for region in self.regions:
|
||||
if region.circuit_addr == circuit_addr:
|
||||
@@ -166,23 +114,19 @@ class Session(BaseClientSession):
|
||||
return CapData(cap_name, ref(region), ref(self), base_url, cap_type)
|
||||
return None
|
||||
|
||||
def transaction_to_assetid(self, transaction_id: UUID):
|
||||
return UUID.combine(transaction_id, self.secure_session_id)
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s>" % (self.__class__.__name__, self.id)
|
||||
|
||||
|
||||
class SessionManager:
|
||||
class SessionManager(BaseClientSessionManager):
|
||||
def __init__(self, settings: ProxySettings):
|
||||
BaseClientSessionManager.__init__(self)
|
||||
self.settings: ProxySettings = settings
|
||||
self.sessions: List[Session] = []
|
||||
self.shutdown_signal = multiprocessing.Event()
|
||||
self.flow_context = HTTPFlowContext()
|
||||
self.asset_repo = HTTPAssetRepo()
|
||||
self.message_logger: Optional[BaseMessageLogger] = None
|
||||
self.addon_ctx: Dict[str, Any] = {}
|
||||
self.addon_ctx: Dict[str, Dict[str, Any]] = collections.defaultdict(dict)
|
||||
self.name_cache = ProxyNameCache()
|
||||
self.pending_leap_clients: List[LEAPClient] = []
|
||||
|
||||
def create_session(self, login_data) -> Session:
|
||||
session = Session.from_login_data(login_data, self)
|
||||
@@ -191,6 +135,15 @@ class SessionManager:
|
||||
session.http_message_handler,
|
||||
)
|
||||
self.sessions.append(session)
|
||||
# TODO: less crap way of tying a LEAP client to a session
|
||||
while self.pending_leap_clients:
|
||||
leap_client = self.pending_leap_clients.pop(-1)
|
||||
# Client may have gone bad since it connected
|
||||
if not leap_client.connected:
|
||||
continue
|
||||
logging.info("Assigned LEAP client to session")
|
||||
session.leap_client = leap_client
|
||||
break
|
||||
logging.info("Created %r" % session)
|
||||
return session
|
||||
|
||||
@@ -205,6 +158,8 @@ class SessionManager:
|
||||
def close_session(self, session: Session):
|
||||
logging.info("Closed %r" % session)
|
||||
session.objects.clear()
|
||||
if session.leap_client:
|
||||
session.leap_client.disconnect()
|
||||
self.sessions.remove(session)
|
||||
|
||||
def resolve_cap(self, url: str) -> Optional["CapData"]:
|
||||
@@ -214,6 +169,10 @@ class SessionManager:
|
||||
return cap_data
|
||||
return CapData()
|
||||
|
||||
async def leap_client_connected(self, leap_client: LEAPClient):
|
||||
self.pending_leap_clients.append(leap_client)
|
||||
AddonManager.handle_leap_client_added(self, leap_client)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class SelectionModel:
|
||||
|
||||
@@ -25,6 +25,7 @@ class EnvSettingDescriptor(SettingDescriptor):
|
||||
class ProxySettings(Settings):
|
||||
SOCKS_PROXY_PORT: int = EnvSettingDescriptor(9061, "HIPPO_UDP_PORT", int)
|
||||
HTTP_PROXY_PORT: int = EnvSettingDescriptor(9062, "HIPPO_HTTP_PORT", int)
|
||||
LEAP_PORT: int = EnvSettingDescriptor(9063, "HIPPO_LEAP_PORT", int)
|
||||
PROXY_BIND_ADDR: str = EnvSettingDescriptor("127.0.0.1", "HIPPO_BIND_HOST", str)
|
||||
REMOTELY_ACCESSIBLE: bool = SettingDescriptor(False)
|
||||
USE_VIEWER_OBJECT_CACHE: bool = SettingDescriptor(False)
|
||||
@@ -34,3 +35,4 @@ class ProxySettings(Settings):
|
||||
AUTOMATICALLY_REQUEST_MISSING_OBJECTS: bool = SettingDescriptor(False)
|
||||
ADDON_SCRIPTS: List[str] = SettingDescriptor(list)
|
||||
FILTERS: Dict[str, str] = SettingDescriptor(dict)
|
||||
SSL_INSECURE: bool = SettingDescriptor(False)
|
||||
|
||||
@@ -83,7 +83,7 @@ class SOCKS5Server:
|
||||
try:
|
||||
# UDP Associate
|
||||
if cmd == 3:
|
||||
loop = asyncio.get_running_loop()
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
transport, protocol = await loop.create_datagram_endpoint(
|
||||
self._udp_protocol_creator(writer.get_extra_info("peername")),
|
||||
local_addr=('0.0.0.0', 0))
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user