Updated python scripts

This commit is contained in:
Pax1601 2024-05-29 15:49:37 +02:00
parent 6db4455d5a
commit c477dcd065
12 changed files with 1155 additions and 245 deletions

View File

@ -5,12 +5,71 @@
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"name": "Add missing units",
"type": "python",
"request": "launch",
"program": "${file}",
"program": "addMissingUnits.py",
"console": "integratedTerminal",
"args": ["${input:unitType}", "${input:searchFolder}"],
"justMyCode": true
},
{
"name": "Add liveries",
"type": "python",
"request": "launch",
"program": "addLiveries.py",
"console": "integratedTerminal",
"args": ["${input:unitType}", "${input:searchFolder}"],
"justMyCode": true
},
{
"name": "Generate payload tables",
"type": "python",
"request": "launch",
"program": "generatePayloadTables.py",
"console": "integratedTerminal",
"args": ["${input:searchFolder}"],
"justMyCode": true
},
{
"name": "Add loadouts",
"type": "python",
"request": "launch",
"program": "addLoadouts.py",
"console": "integratedTerminal",
"args": ["${input:unitType}", "${input:searchFolder}"],
"justMyCode": true
},
{
"name": "Add ranges",
"type": "python",
"request": "launch",
"program": "addRanges.py",
"console": "integratedTerminal",
"args": ["${input:unitType}", "${input:searchFolder}"],
"justMyCode": true
}
],
"inputs": [
{
"id": "unitType",
"type": "command",
"command": "extension.commandvariable.promptStringRemember",
"args": {
"key": "type",
"description": "Unit type (aircraft, helicopter, groundunit, navyunit)",
"default": "aircraft"
}
},
{
"id": "searchFolder",
"type": "command",
"command": "extension.commandvariable.promptStringRemember",
"args": {
"key": "folder",
"description": "DCS folder location",
"default": "E:\\Eagle Dynamics\\DCS World (Open Beta)"
}
}
]
}

View File

@ -4,9 +4,7 @@ import inspect
import difflib
from slpp import slpp as lua
SEARCH_FOLDER = "D:\\Eagle Dynamics\\DCS World OpenBeta"
sys.path.append("..\\..\\..\\dcs-master\\dcs-master")
SEARCH_FOLDER = sys.argv[2]
from dcs.weapons_data import Weapons
from dcs.planes import *

View File

@ -4,15 +4,12 @@ import inspect
import difflib
from slpp import slpp as lua
SEARCH_FOLDER = "D:\\Eagle Dynamics\\DCS World OpenBeta"
sys.path.append("..\..\..\dcs-master\dcs-master")
SEARCH_FOLDER = sys.argv[2]
from dcs.weapons_data import Weapons
from dcs.planes import *
from dcs.helicopters import *
clsid_conversion = {
'ExtFuelTankID' : "{EFT_230GAL}" ,
'InternalFuelTank100' : "{IAFS_ComboPak_100}" ,

View File

@ -4,7 +4,7 @@ import inspect
import difflib
from slpp import slpp as lua
SEARCH_FOLDER = "D:\\Eagle Dynamics\\DCS World OpenBeta"
SEARCH_FOLDER = sys.argv[2]
from dcs.vehicles import *
from dcs.ships import *
@ -36,8 +36,8 @@ if len(sys.argv) > 1:
"name": unit.id,
"coalition": "",
"era": "",
"label": unit.name,
"shortLabel": unit.name,
"label": unit.livery_name,
"shortLabel": unit.livery_name,
"type": unit.__qualname__.split(".")[0],
"enabled": False,
"liveries": {}

View File

@ -4,9 +4,7 @@ import inspect
import difflib
from slpp import slpp as lua
SEARCH_FOLDER = "D:\\Eagle Dynamics\\DCS World OpenBeta"
sys.path.append("..\..\..\dcs-master\dcs-master")
SEARCH_FOLDER = sys.argv[2]
from dcs.vehicles import *
from dcs.ships import *

View File

@ -1,35 +0,0 @@
import sys
import json
import re
# The database file on which to operate is the first standard argument of the call
if len(sys.argv) > 1:
if (sys.argv[1] == "aircraft"):
filename = '..\\..\\databases\\units\\aircraftdatabase.json'
elif (sys.argv[1] == "helicopter"):
filename = '..\\..\\databases\\units\\helicopterdatabase.json'
elif (sys.argv[1] == "groundunit"):
filename = '..\\..\\databases\\units\\groundunitdatabase.json'
elif (sys.argv[1] == "navyunit"):
filename = '..\\..\\databases\\units\\navyunitdatabase.json'
# Loads the database
with open(filename) as f:
database = json.load(f)
for name in database:
label = database[name]['label']
print(label)
res = re.findall("\((.*?)\)", label)
for tag in res:
label = label.replace(f"({tag})", "")
label = database[name]['label'] = label
if len(res) > 0:
database[name]["tags"] = "".join([f'{tag}{", " if i < len(res) - 1 else ""}' for i, tag in enumerate(res)])
# Dump everything in the database
with open(filename, "w") as f:
json.dump(database, f, indent=2)
print("Done!")

View File

@ -4,9 +4,7 @@ import os
import json
import logging
sys.path.append("..\..\..\dcs-master\dcs-master")
SEARCH_FOLDER = "D:\\Eagle Dynamics\\DCS World OpenBeta"
SEARCH_FOLDER = sys.argv[1]
clsid_conversion = {
'ExtFuelTankID' : "{EFT_230GAL}" ,

View File

@ -1,23 +0,0 @@
import socket
from email.utils import formatdate
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 3003))
sock.listen(5)
count = 0
while True:
connection, address = sock.accept()
buf = connection.recv(1024)
print(buf.decode("utf-8"))
if "OPTIONS" in buf.decode("utf-8"):
resp = (f"""HTTP/1.1 200 OK\r\nDate: {formatdate(timeval=None, localtime=False, usegmt=True)}\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: PUT, GET, OPTIONS\r\nAccess-Control-Allow-Headers: *\r\nAccess-Control-Max-Age: 86400\r\nVary: Accept-Encoding, Origin\r\nKeep-Alive: timeout=2, max=100\r\nConnection: Keep-Alive\r\n""".encode("utf-8"))
connection.send(resp)
if not "PUT" in buf.decode("utf-8"):
connection.close()
else:
resp = (f"""HTTP/1.1 200 OK\r\nDate: {formatdate(timeval=None, localtime=False, usegmt=True)}\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: PUT, GET, OPTIONS\r\nAccess-Control-Allow-Headers: *\r\nAccess-Control-Max-Age: 86400\r\nVary: Accept-Encoding, Origin\r\nKeep-Alive: timeout=2, max=100\r\nConnection: Keep-Alive\r\n\r\n{{"Hi": "Wirts!"}}\r\n""".encode("utf-8"))
connection.send(resp)
connection.close()
count += 1

View File

@ -1,98 +0,0 @@
import sys
import os
import numpy
from PIL import Image
from concurrent import futures
from os import listdir
from os.path import isfile, isdir, join
# global counters
fut_counter = 0
tot_futs = 0
def printProgressBar(iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '', printEnd = "\r"):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
def done_callback(fut):
global fut_counter, tot_futs
fut_counter += 1
printProgressBar(fut_counter, tot_futs)
def remove_black_areas(im):
data = numpy.array(im)
red, green, blue = data.T
# If present, remove any "black" areas
background_areas = (red < 10) & (blue < 10) & (green < 10)
data[..., :][background_areas.T] = (221, 221, 221)
return Image.fromarray(data)
def convert_tiles(source, dest, tile):
zoom = tile[0]
X = tile[1]
Y = tile[2]
if not os.path.exists(os.path.join(dest, str(zoom))):
try:
os.mkdir(os.path.join(dest, str(zoom)))
except FileExistsError:
# Ignore this error, it means one other thread has already created the folder
pass
except Exception as e:
raise e
if not os.path.exists(os.path.join(dest, str(zoom), str(X))):
try:
os.mkdir(os.path.join(dest, str(zoom), str(X)))
except FileExistsError:
# Ignore this error, it means one other thread has already created the folder
pass
except Exception as e:
raise e
remove_black_areas(Image.open(os.path.join(source, str(zoom), str(X), f"{Y}.png")).convert('RGB')).save(os.path.join(dest, str(zoom), str(X), f"{Y}.jpg"))
if len(sys.argv) < 3:
print("Please provide a source and a destination folder")
else:
source = sys.argv[1]
dest = sys.argv[2]
if not os.path.exists(dest):
try:
os.mkdir(dest)
except FileExistsError:
# Ignore this error, it means one other thread has already created the folder
pass
except Exception as e:
raise e
print(f"Listing source tiles...")
existing_tiles = []
zooms = [int(f) for f in listdir(source) if isdir(join(source, f))]
for zoom in zooms:
Xs = [int(f) for f in listdir(join(source, str(zoom))) if isdir(join(source, str(zoom), f))]
for X in Xs:
Ys = [int(f.removesuffix(".png")) for f in listdir(os.path.join(source, str(zoom), str(X))) if isfile(join(source, str(zoom), str(X), f))]
for Y in Ys:
existing_tiles.append((zoom, X, Y))
print(f"{len(existing_tiles)} tiles will be converted")
# Merge the tiles with parallel thread execution
with futures.ThreadPoolExecutor() as executor:
print(f"Converting tiles to jpg...")
print(f"Initializing exectuion pool")
futs = [executor.submit(convert_tiles, source, dest, tile) for tile in existing_tiles]
tot_futs = len(futs)
fut_counter = 0
[fut.add_done_callback(done_callback) for fut in futs]
[fut.result() for fut in futures.as_completed(futs)]

View File

@ -1,69 +0,0 @@
import os
# save DCS.openbeta\\mods\\aircraft path as variable
path = f"{os.environ['UserProfile']}\\Saved Games\\DCS.openbeta\\Mods\\aircraft"
# write everything to a working file - to be deleted after getting rid of unnecessary trailing commas
with open('working_file.lua', mode='w') as payload_file:
payload_file.write('Olympus.unitPayloads = {\n')
# iterate through everything in mod aircraft path
for f in os.listdir(path):
# modders are inconsistent with the lua filename, so grab that first file in the correct directory
try:
unitpayload_lua_dir = f"{path}\\{f}\\UnitPayloads"
try:
lua_filename = os.listdir(unitpayload_lua_dir)[0]
except IndexError:
pass
unitpayload_lua_path = f"{unitpayload_lua_dir}\\{lua_filename}"
# read the aircraft's payloads and write the relevant material into the working file
with open(unitpayload_lua_path) as payload_lua_file:
lines = payload_lua_file.readlines()
for line in lines:
if line.startswith(' ["name"]'):
ac_type = line.replace(' ["name"] = "', '').replace('",', '').rstrip()
ac_type = f'["{ac_type}"] =' + r' {'
payload_file.write(ac_type + '\n')
elif line.startswith(' ["name"]'):
loadout_name = line.replace(' ["name"] = "', '').replace('",', '').rstrip()
loadout_name = f'["{loadout_name}"] =' + r' {'
payload_file.write(loadout_name + '\n')
elif line.startswith(' ["CLSID"]'):
clsid = line.replace(' ', '').replace(',', '').rstrip()
clsid = r'{' + f'{clsid}' + r'}' + ','
elif line.startswith(' ["num"]'):
pylon = line.replace(' ["num"] = ', '').replace(',', '').rstrip()
pylon = f'[{pylon}] = '
payload_file.write(pylon + clsid + '\n')
elif line.startswith(' },'):
payload_file.write('},\n')
elif line.startswith('}'):
payload_file.write('},\n')
# skip directory if no directory or no file in directory
except FileNotFoundError:
pass
payload_file.write('}')
# parse the working file, get rid of trailing commas when next line starts with a '}'.
with open("working_file.lua") as working_file:
lines = working_file.readlines()
prev_line = ''
with open("payloads.lua", mode="w") as payload_file:
for ind, line in enumerate(lines):
try:
if lines[ind + 1].startswith('}') and line.endswith('},\n'):
new_line = line.replace(',', '')
payload_file.write(f'{new_line}')
else:
payload_file.write(line)
except IndexError:
payload_file.write(line)
# delete the working file
os.remove("working_file.lua")

File diff suppressed because it is too large Load Diff

Binary file not shown.