Skip to content

Commit

Permalink
Merge pull request #169 from ChrisNeedham24/allowWindowsFromSource
Browse files Browse the repository at this point in the history
Reinstated ability to play from source on Windows.
  • Loading branch information
ChrisNeedham24 authored Jan 12, 2025
2 parents 837d879 + 471337a commit 14e8ab5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 7 additions & 3 deletions source/networking/event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
# For Windows clients we need to ensure that the miniupnpc DLL is loaded before attempting to import the module.
if platform.system() == "Windows":
from ctypes import cdll, CDLL
import sys
# Clients playing via the bundled EXE should already have the DLL loaded, since it's bundled into the EXE itself.
try:
CDLL("miniupnpc.dll")
# However, clients playing from source or via a pip install will need to load the DLL manually.
# However, clients playing via a pip install or from source will need to load the DLL manually.
except FileNotFoundError:
site_packages_path: str = next(path for path in getsitepackages() if path.endswith("site-packages"))
cdll.LoadLibrary(f"{site_packages_path}/microcosm/source/resources/dll/miniupnpc.dll")
if "microcosm" in sys.modules:
site_packages_path: str = next(path for path in getsitepackages() if path.endswith("site-packages"))
cdll.LoadLibrary(f"{site_packages_path}/microcosm/source/resources/dll/miniupnpc.dll")
else:
cdll.LoadLibrary("source/resources/dll/miniupnpc.dll")
# We need to disable a lint rule for the miniupnpc import because it doesn't actually declare UPnP in its module. This
# isn't our fault, so we can just disable the rule.
# pylint: disable=no-name-in-module
Expand Down
17 changes: 13 additions & 4 deletions source/tests/test_event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import sched
import socket
import sys
import unittest
from copy import deepcopy
from threading import Thread
Expand Down Expand Up @@ -113,9 +114,6 @@ def test_windows_dll_verification(self,
Ensure that the miniupnpc DLL is correctly manually loaded or not manually loaded, depending on whether it has
already been automatically loaded.
"""
# Mock out the site-packages path.
site_packages_path: str = "/tmp/site-packages"
site_packages_mock.return_value = [site_packages_path]
# First we need to reload the import since naturally event_listener.py has already been imported in this test
# suite.
importlib.reload(event_listener)
Expand All @@ -125,10 +123,21 @@ def test_windows_dll_verification(self,
# We simulate the playing from source/package cases by raising an error when constructing the DLL object, since
# it's not present in those cases.
cdll_construction_mock.side_effect = FileNotFoundError()
# Simulate the source case.
importlib.reload(event_listener)
# In these cases, we expect an attempt to be made to construct the DLL object, but ultimately for the DLL to be
# In this case, we expect an attempt to be made to construct the DLL object, but ultimately for the DLL to be
# manually loaded from source.
cdll_construction_mock.assert_called_with("miniupnpc.dll")
cdll_load_mock.assert_called_with("source/resources/dll/miniupnpc.dll")
# Simulate the package case by mocking out the site-packages path and sys.modules.
site_packages_path: str = "/tmp/site-packages"
site_packages_mock.return_value = [site_packages_path]
# This mock is definitely not normal, but it does serve our purpose by defining the key against a real module.
sys.modules["microcosm"] = event_listener
importlib.reload(event_listener)
# In this case, we also expect an attempt to be made to construct the DLL object, but this time we expect the
# DLL to be manually loaded from site-packages.
cdll_construction_mock.assert_called_with("miniupnpc.dll")
cdll_load_mock.assert_called_with(f"{site_packages_path}/microcosm/source/resources/dll/miniupnpc.dll")

def test_handle(self):
Expand Down

0 comments on commit 14e8ab5

Please sign in to comment.