Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Commit

Permalink
Added a logger class to create and manage log files internally. Added…
Browse files Browse the repository at this point in the history
… an auto-suspend functionality where the macro automatically suspends unused instances. Added a keybind to suspend and unsuspend instances manually as well. Changes to .gitignore and README.md.
  • Loading branch information
sathya-pramodh committed Jun 14, 2022
1 parent 19b6d73 commit 8ad824c
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ If applicable, add screenshots to help explain your problem.

**Additional context**
Add any other context about the problem here.

**Log Files**
Attach the necessary log files for the debugging process.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
__pycache__/
*.py[cod]
*$py.class
log/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,18 @@ sudo python3 multi_instance.py
```
## Some important instructions
- This macro also assumes that you are using Atum and FastReset mods for Minecraft 1.16.1. It is hardcoded assuming so.
- Log files are located in the 'log' directory in the project script's main folder. So issues must be submitted with the relevant log files attached to them.
- The 'testing' branch is meant only for testing purposes and releases from that branch are created with the '-testing' tag at the end of them.

# Contribution
- Code contributions can be made to the testing branch. Pull requests must be made with proper comments and documentation.
- The code must follow all PEP8 conventions and must be in python3.x ONLY.

# Default Keybinds
- `Ctrl+R` - Reset all instances (RSG)
- `Shift+R` - Reset the current instance (RSG)
- `Ctrl+S` - Suspend all instances other than the active one.
- `Alt+S` - Unsuspend all instances.
- `Ctrl+1` - Switch to Instance 1
- `Ctrl+2` - Switch to Instance 2
- `Ctrl+3` - Switch to Instance 3
Expand All @@ -105,3 +113,5 @@ sudo python3 multi_instance.py
- SWITCH_INSTANCES - The list of keybinds (in order of instance number) to switch to that respective instance.
- RESET_ALL_INSTANCES - The list of keybinds to reset all instances.
- RESET_CURRENT_INSTANCE - The list of keybinds to reset the current instance.
- SUSPEND_ALL_INSTANCES - The list of keybinds for suspending instances other than the active instance.
- UNSUSPEND_ALL_INSTANCES - The list of keybinds for un-suspending all instances.
12 changes: 12 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@
# Note: Do not use 'alt' instead of 'shift' here because the macro contains 'Tab' as a part of it and 'alt+tab' is already a keybind in most desktop environments.
# The default is ["shift+r"]
RESET_CURRENT_INSTANCE = ["shift+r"]

# The list of keybinds for suspending instances other than the active instance.
# Each element of the list must adhere to the allowed values of the keyboard module, else the script will exit out with an error message.
# The allowed values of the keyboard module can be found at: https://github.com/boppreh/keyboard
# The default is ["ctrl+s"]
SUSPEND_ALL_INSTANCES = ["ctrl+s"]

# The list of keybinds for un-suspending all instances.
# Each element of the list must adhere to the allowed values of the keyboard module, else the script will exit out with an error message.
# The allowed values of the keyboard module can be found at: https://github.com/boppreh/keyboard
# The default is ["alt+s"]
UNSUSPEND_ALL_INSTANCES = ["alt+s"]
72 changes: 72 additions & 0 deletions helper_scripts/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
This is the module that implements a logger as a class.
Custom designed for the project(MultiInstanceLinux).
"""
# Author: sathya-pramodh
# Github: https://github.com/sathya-pramodh

# Software Licensed under the MIT License.

# License terms:

# MIT License

# Copyright (c) 2022 sathya-pramodh

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Imports
from datetime import datetime


class Logging:
"""
Custom implementation of a logging class.
"""

def __init__(self, output_path):
"""
Initializer method for the class.
output_path
The full path to the folder to which you want to output the logs to.
"""
self._path = output_path
self._session_log_file = (
output_path + datetime.now().strftime("%Y-%m-%d") + ".log"
)

def log(self, message):
"""
Method to log a message to a log file.
message
A string data type containing the message to be logged.
Returns None
"""
with open(self._session_log_file, "a") as file:
write_string = (
"\n"
+ "[MultiInstanceLinux {}]".format(datetime.now().strftime("%H:%M:%S"))
+ ": "
+ message
)
file.write(write_string)
28 changes: 20 additions & 8 deletions macro_handlers/instance_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@
import subprocess


def instance_switch_macro(instance_keybinds, keybind, hex_codes):
def instance_switch_macro(instance_keybinds, keybind, hex_codes, pids):
"""
Handles the switch instances macro.
instance_keybinds
A list of keybinds as strings.
A list of keybinds as strings.
keybind
A string denoting the keybind pressed.
A string denoting the keybind pressed.
hex_codes
A list of the hex codes of the open Minecraft instances.
A list of the hex codes of the open Minecraft instances.
pids
A dictionary of process IDs of the open Minecraft instances.
Returns None
Returns the hex code of the instance that was switched to for logging purposes.
"""
instance_number = instance_keybinds.index(keybind)
Expand All @@ -58,9 +63,16 @@ def instance_switch_macro(instance_keybinds, keybind, hex_codes):
["xdotool", "getactivewindow"]
).decode("UTF-8")
current_hex_code = hex(int(current_hex_code_in_base_ten))
os.system("wmctrl -i -a " + current_hex_code)
time.sleep(0.25)
os.system("xdotool key --window " + current_hex_code + " Escape")
if len(current_hex_code.split("x")[1]) == 7:
current_hex_code = (
current_hex_code.split("x")[0] + "x0" + current_hex_code.split("x")[1]
)
for hex_code in hex_codes:
if hex_code != current_hex_code and hex_code != target_hex_code:
os.system("kill -STOP " + pids[hex_code])
os.system("kill -CONT " + pids[target_hex_code])
os.system("wmctrl -i -a " + target_hex_code)
time.sleep(0.25)
os.system("xdotool key --window " + target_hex_code + " Escape")

return target_hex_code
12 changes: 9 additions & 3 deletions macro_handlers/reset_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@
import os


def reset_all_macro(hex_codes):
def reset_all_macro(hex_codes, pids):
"""
Handles the resetting macro on all instances.
It is hardcoded for Minecraft 1.16.1.
hex_codes
A list of the hex codes of the open Minecraft Instances.
A list of the hex codes of the open Minecraft Instances.
pids
A list of PIDs of all the open instances.
Returns None
"""
macro = " Tab+" * 8 + "Enter"
for pid in pids.values():
os.system("kill -CONT " + pid)
current_hex_code_in_base_ten = subprocess.check_output(
["xdotool", "getwindowfocus"]
).decode("UTF-8")
Expand All @@ -66,7 +71,7 @@ def reset_current_macro():
Handles the reset instance macro for the current window in focus.
It is hardcoded for Minecraft 1.16.1.
Returns None
Returns the hex code of the instance that was reset for logging purposes
"""
macro = " Tab+" * 8 + "Enter"
hex_code_in_base_ten = subprocess.check_output(
Expand All @@ -77,3 +82,4 @@ def reset_current_macro():
time.sleep(0.25)
os.system("xdotool key --window " + hex_code + " Escape")
os.system("xdotool key --window " + hex_code + macro)
return hex_code
69 changes: 69 additions & 0 deletions macro_handlers/suspend_handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
This is a helper script that handles the suspend keybinds.
Called from handle_instance_keybinds() in multi_instance.py
"""
# Author: sathya-pramodh
# Github: https://github.com/sathya-pramodh
# Software licensed under the MIT license.
# License Terms:

# MIT License

# Copyright (c) 2022 sathya-pramodh

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import subprocess


def suspend_all_macro(pids):
"""
The function that handles suspending all instances other than the current active instance.
pids
A list of the PIDs of all the open instances.
Returns the hex code of the current active instance for logging purposes.
"""
current_hex_code_in_base_ten = subprocess.check_output(
["xdotool", "getwindowfocus"]
).decode("UTF-8")
current_hex_code = hex(int(current_hex_code_in_base_ten))
if len(current_hex_code.split("x")[1]) == 7:
current_hex_code = (
current_hex_code.split("x")[0] + "x0" + current_hex_code.split("x")[1]
)
for hex_code, pid in pids.items():
if hex_code != current_hex_code:
os.system("kill -STOP " + pid)

return current_hex_code


def unsuspend_all_macro(pids):
"""
The function that handles unsuspending all instances other than the current active instance.
pids
A list of PIDs of all the open instances.
Returns the hex code of the current active instance for logging purposes.
"""
for hex_code, pid in pids.items():
os.system("kill -CONT " + pid)
Loading

0 comments on commit 8ad824c

Please sign in to comment.