Skip to content

Instantly share code, notes, and snippets.

@douglasg14b
Created May 12, 2019 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasg14b/5ba3c2a7c4d02af1bdd8fd909f0d0592 to your computer and use it in GitHub Desktop.
Save douglasg14b/5ba3c2a7c4d02af1bdd8fd909f0d0592 to your computer and use it in GitHub Desktop.
Will fix the lua dofile calls for FAF on Linux. Enabling 1v1, Phantom, Murderparty, Nomads...etc
#!/usr/bin/python3
import os
import time
import signal
import sys
import itertools
USERHOME = os.path.expanduser('~')
INITFILES = []
FILE_PATHS = [
USERHOME + '/.faforever/bin/init.lua',
USERHOME +'/.faforever/bin/SupComDataPath.lua'
]
CAN_LOOP = True
SPINNER = itertools.cycle(['|','/','-','\\']) #Just for visuals
def main():
global CAN_LOOP
global SPINNER
print("==== Starting FAF File Watcher ====\n")
print(" Ctrl+C to exit \n")
for path in FILE_PATHS:
INITFILES.append(InitFile(path))
while CAN_LOOP:
writeSpinner()
for initFile in INITFILES:
initFile.replaceIfModified()
time.sleep(0.5)
print("\n==== Exiting FAF File Watcher ====")
sys.exit(0)
def writeSpinner():
sys.stdout.write(next(SPINNER))
sys.stdout.flush()
sys.stdout.write('\b')
def signal_handler(sig, frame):
global CAN_LOOP
print('\nSIGINT Recieved, Cleanly Exiting')
CAN_LOOP = False
class InitFile():
replace = "dofile('init_faf.lua')"
replaceWith = "dofile(InitFileDir .. '\\\\init_faf.lua')"
path = ''
lastReplaceTime = -1 #Causes it to replace the text first pass
def __init__(self, path):
self.path = path
def modifiedAfterReplace(self):
modified = os.path.getmtime(self.path) > self.lastReplaceTime
return modified
def replaceIfModified(self):
if self.modifiedAfterReplace():
print(f"File modified at {os.path.getmtime(self.path)}, reading and replacing")
self.readAndReplace()
def readAndReplace(self):
filedata = ''
print('Reading: ' + self.path)
with open(self.path, 'r') as file :
filedata = file.read()
filedata = filedata.replace(self.replace, self.replaceWith)
print('Writing: ' + self.path)
with open(self.path, 'w') as file:
file.write(filedata)
self.lastReplaceTime = os.path.getmtime(self.path)
print(f"Read and Replace Complete At: {self.lastReplaceTime}")
signal.signal(signal.SIGINT, signal_handler)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment