Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: support multiple branches for compatibility #59

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions bananas_server/application/bananas_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,32 @@ def get_by_unique_id(self, content_type, unique_id):
def get_by_unique_id_and_md5sum(self, content_type, unique_id, md5sum):
return self._by_unique_id_and_md5sum[content_type].get(unique_id, {}).get(md5sum)

async def receive_PACKET_CONTENT_CLIENT_INFO_LIST(self, source, content_type, openttd_version):
version_major = (openttd_version >> 24) & 0xFF
version_minor = (openttd_version >> 20) & 0xF

if version_major > 16 + 11:
# Since OpenTTD 12, major is 8 bytes and minor is 4 bytes, and
# no more patch. The major also needs to be subtracted by 16 to
# get to the real version.
version = [version_major - 16, version_minor]
async def receive_PACKET_CONTENT_CLIENT_INFO_LIST(self, source, content_type, openttd_version, branch_versions):
if openttd_version != 0xFFFFFFFF:
version_major = (openttd_version >> 24) & 0xFF
version_minor = (openttd_version >> 20) & 0xF

if version_major > 16 + 11:
# Since OpenTTD 12, major is 8 bytes and minor is 4 bytes, and
# no more patch. The major also needs to be subtracted by 16 to
# get to the real version.
version = [version_major - 16, version_minor]
else:
# Pre OpenTTD 12 version.
version_major = (openttd_version >> 28) & 0xF
version_minor = (openttd_version >> 24) & 0xF
version_patch = (openttd_version >> 20) & 0xF

version = [version_major, version_minor, version_patch]

versions = {
"official": version,
}
else:
# Pre OpenTTD 12 version.
version_major = (openttd_version >> 28) & 0xF
version_minor = (openttd_version >> 24) & 0xF
version_patch = (openttd_version >> 20) & 0xF
versions = {}

version = [version_major, version_minor, version_patch]
for branch, version in branch_versions.items():
versions[branch] = [int(p) for p in version.split(".")]

bootstrap_content_entry = None

Expand All @@ -95,10 +105,27 @@ async def receive_PACKET_CONTENT_CLIENT_INFO_LIST(self, source, content_type, op
if content_entry == bootstrap_content_entry:
continue

if content_entry.min_version and version < content_entry.min_version:
continue
if content_entry.max_version and version >= content_entry.max_version:
continue
# If no compatibility is given, it is compatible with every client.
# So only run the check if it contains anything.
if content_entry.compatibility:
for name, version in versions.items():
if name not in content_entry.compatibility:
continue

min_version, max_version = content_entry.compatibility[name]
if min_version and version < min_version:
continue
if max_version and version >= max_version:
continue

# Branch is in the compatibility matrix and we are in the
# version range. We break here, so the else below is not
# executed. This means we add the entry to the list.
break
else:
# We never found a branch for which we were compatible. So
# we will be skipping this entry.
continue

await self._send_content_entry(source, content_entry)

Expand Down
24 changes: 10 additions & 14 deletions bananas_server/index/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def __init__(
upload_date,
md5sum,
dependencies,
min_version,
max_version,
compatibility,
tags,
):
super().__init__()
Expand All @@ -48,8 +47,7 @@ def __init__(
self.md5sum = md5sum
self.raw_dependencies = dependencies
self.dependencies = None
self.min_version = min_version
self.max_version = max_version
self.compatibility = compatibility
self.tags = tags

def calculate_dependencies(self, by_unique_id_and_md5sum):
Expand Down Expand Up @@ -79,8 +77,7 @@ def __repr__(self):
f"upload_date={self.upload_date!r}, "
f"md5sum={self.md5sum!r}, "
f"dependencies={self.dependencies!r}, "
f"min_version={self.min_version!r}, "
f"max_version={self.max_version!r}, "
f"compatibility={self.compatibility!r}, "
f"tags={self.tags!r})"
)

Expand All @@ -105,11 +102,10 @@ def _read_content_entry_version(self, content_type, unique_id, data, md5sum_mapp

dependencies.append((dep_content_type, dep_unique_id, dep_md5sum))

min_version = None
max_version = None
compatibility = {}
for com in data.get("compatibility", {}):
if com["name"] != "vanilla":
continue
min_version = None
max_version = None

for conditions in com["conditions"]:
if conditions.startswith(">="):
Expand All @@ -119,6 +115,8 @@ def _read_content_entry_version(self, content_type, unique_id, data, md5sum_mapp
else:
raise Exception("Invalid compatibility flag", com)

compatibility[com["name"]] = [min_version, max_version]

# Validate the object to make sure all fields are within set limits.
ContentEntryTest().load(
{
Expand All @@ -131,8 +129,7 @@ def _read_content_entry_version(self, content_type, unique_id, data, md5sum_mapp
"description": data.get("description", ""),
"unique-id": unique_id,
"md5sum": md5sum,
"min-version": min_version,
"max-version": max_version,
"compatibility": compatibility,
"tags": data.get("tags", []),
"raw-dependencies": dependencies,
}
Expand Down Expand Up @@ -167,8 +164,7 @@ def _read_content_entry_version(self, content_type, unique_id, data, md5sum_mapp
upload_date=data["upload-date"],
md5sum=md5sum,
dependencies=dependencies,
min_version=min_version,
max_version=max_version,
compatibility=compatibility,
tags=data.get("tags", []),
)

Expand Down
11 changes: 9 additions & 2 deletions bananas_server/index/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ class Meta:
description = fields.String(validate=validate.Length(max=511))
tags = fields.List(fields.String(validate=validate.Length(max=31)))
md5sum = fields.Raw(validate=validate.Length(min=16, max=16))
min_version = fields.List(fields.Integer(), data_key="min-version", missing=None)
max_version = fields.List(fields.Integer(), data_key="max-version", missing=None)
compatibility = fields.Dict(
keys=fields.String(),
values=fields.Tuple(
(
fields.List(fields.Integer(), missing=None),
fields.List(fields.Integer(), missing=None),
)
),
)
raw_dependencies = fields.List(
fields.Tuple(
(
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ marshmallow==3.13.0
marshmallow-enum==1.5.1
multidict==5.1.0
openttd-helpers==1.0.1
openttd-protocol==1.2.1
openttd-protocol==1.3.0
python-dateutil==2.8.2
PyYAML==5.4.1
s3transfer==0.5.0
Expand Down