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 migration tools. #185

Merged
merged 7 commits into from
Sep 22, 2020
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
65 changes: 65 additions & 0 deletions Tools/barrellength_volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3

# Make sure you have python3 installed.
# Ensure that the json_formatter is kept in Tools with this script. They must be in the same folder!
# For Windows:
# Using command prompt type "python barrellength_volume.py"
# For Max OS X or Linux:
# Swap any "\\" with "/", then run the script as in windows.

import json
import os


def gen_new(path):
change = False
with open(path, "r") as json_file:
try:
json_data = json.load(json_file)
except UnicodeDecodeError:
print("UnicodeDecodeError in {0}".format(path))
return None
except json.decoder.JSONDecodeError:
print("JSONDecodeError in {0}".format(path))
return None
for jo in json_data:
# We only want JsonObjects
if type(jo) is str:
continue
if type(jo.get("volume")) != str and jo.get("volume") \
and jo.get("type") not in ["sound_effect", "speech"]:
volume = jo["volume"]
volume *= 250
if volume > 10000 and volume % 1000 == 0:
jo["volume"] = str(int(volume/1000)) + " L"
else:
jo["volume"] = str(volume) + " ml"
change = True
if jo.get("barrel_length"):
if type(jo["barrel_length"]) == int:
barrel_length = jo["barrel_length"]
barrel_length *= 250
jo["barrel_volume"] = str(barrel_length) + " ml"
elif type(jo["barrel_length"]) == str:
jo["barrel_volume"] = jo["barrel_length"]
del jo["barrel_length"]
change = True

return json_data if change else None


def change_file(json_dir):
for root, directories, filenames in os.walk("..\\{0}".format(json_dir)):
for filename in filenames:
path = os.path.join(root, filename)
if path.endswith(".json"):
new = gen_new(path)
if new is not None:
with open(path, "w") as jf:
json.dump(new, jf, ensure_ascii=False)
os.system(".\\json_formatter.exe {0}".format(path))


if __name__ == "__main__":
json_dir = input("What directory are the json files in? ")
change_file(json_dir)
82 changes: 82 additions & 0 deletions Tools/covers_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3

# Make sure you have python3 installed.
# Ensure that the json_formatter is kept in Tools with this script. They must be in the same folder!
# For Windows:
# Using command prompt type "python covers_update.py"
# For Max OS X or Linux:
# Swap any "\\" with "/", then run the script as in windows.

import json
import os


def gen_new(path):
change = False
with open(path, "r") as json_file:
try:
json_data = json.load(json_file)
except UnicodeDecodeError:
print("UnicodeDecodeError in {0}".format(path))
return None
except json.decoder.JSONDecodeError:
print("JSONDecodeError in {0}".format(path))
return None
for jo in json_data:
# We only want JsonObjects
if type(jo) is str:
continue
# And only if they have coverage
if not jo.get("covers"):
continue
joc = []
for bodypart in jo["covers"]:
if bodypart.endswith("_EITHER"):
bodypart = bodypart[:-7]
jo["sided"] = True
bodypart = bodypart + "s"
if bodypart == "ARMS":
joc.append("arm_l")
joc.append("arm_r")
elif bodypart == "EYES":
joc.append("eyes")
elif bodypart in ["FEET", "FOOTS"]:
joc.append("foot_l")
joc.append("foot_r")
elif bodypart == "HANDS":
joc.append("hand_l")
joc.append("hand_r")
elif bodypart == "HEAD":
joc.append("head")
elif bodypart == "LEGS":
joc.append("leg_l")
joc.append("leg_r")
elif bodypart == "MOUTH":
joc.append("mouth")
elif bodypart == "TORSO":
joc.append("torso")
else:
joc.append(bodypart)
if jo["covers"] == joc:
continue
jo["covers"] = joc
change = True

return json_data if change else None


def change_file(json_dir):
for root, directories, filenames in os.walk("..\\{0}".format(json_dir)):
for filename in filenames:
path = os.path.join(root, filename)
if path.endswith(".json"):
new = gen_new(path)
if new is not None:
with open(path, "w") as jf:
json.dump(new, jf, ensure_ascii=False)
os.system(".\\json_formatter.exe {0}".format(path))


if __name__ == "__main__":
json_dir = input("What directory are the json files in? ")
change_file(json_dir)
55 changes: 55 additions & 0 deletions Tools/material_ammo_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

# Make sure you have python3 installed.
# Ensure that the json_formatter is kept in Tools with this script. They must be in the same folder!
# For Windows:
# Using command prompt type "python material_ammo_update.py"
# For Max OS X or Linux:
# Swap any "\\" with "/", then run the script as in windows.

import json
import os


def gen_new(path):
change = False
with open(path, "r") as json_file:
try:
json_data = json.load(json_file)
except UnicodeDecodeError:
print("UnicodeDecodeError in {0}".format(path))
return None
except json.decoder.JSONDecodeError:
print("JSONDecodeError in {0}".format(path))
return None
for jo in json_data:
# We only want JsonObjects
if type(jo) is str:
continue
if type(jo.get('material')) == str:
material = jo['material']
jo['material'] = [material]
change = True
if type(jo.get('ammo')) == str and jo.get('type') == 'ammo':
ammo = jo['ammo']
jo['ammo'] = [ammo]
change = True

return json_data if change else None


def change_file(json_dir):
for root, directories, filenames in os.walk("..\\{0}".format(json_dir)):
for filename in filenames:
path = os.path.join(root, filename)
if path.endswith(".json"):
new = gen_new(path)
if new is not None:
with open(path, "w") as jf:
json.dump(new, jf, ensure_ascii=False)
os.system(".\\json_formatter.exe {0}".format(path))


if __name__ == "__main__":
json_dir = input("What directory are the json files in? ")
change_file(json_dir)
67 changes: 67 additions & 0 deletions Tools/name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

# Make sure you have python3 installed.
# Ensure that the json_formatter is kept in Tools with this script. They must be in the same folder!
# For Windows:
# Using command prompt type "python barrellength_volume.py"
# For Max OS X or Linux:
# Swap any "\\" with "/", then run the script as in windows.

import json
import os


def gen_new(path):
change = False
with open(path, "r") as json_file:
try:
json_data = json.load(json_file)
except UnicodeDecodeError:
print("UnicodeDecodeError in {0}".format(path))
return None
for jo in json_data:
# We only want JsonObjects
if type(jo) is str:
continue
if not jo.get('name'):
continue
if type(jo['name']) == dict:
continue
if jo['type'] not in ['GENERIC', 'AMMO', 'MAGAZINE', 'ARMOR', 'PET_ARMOR', 'BOOK',
'BIONIC_ITEM', 'COMESTIBLE', 'GUN', 'GUNMOD', 'BATTERY', 'TOOL',
'SPELL', 'ENGINE', 'WHEEL', 'vitamin', 'ITEM_CATEGORY', 'bionic',
'npc_class', 'mission_definition', 'TOOLMOD', 'mutation', 'fault',
'martial_art', 'MONSTER', 'proficiency', 'tool_quality',
'map_extra', 'skill', 'TOOL_ARMOR', 'item_action', 'vehicle_part']:
continue
if jo.get('name_plural'):
if jo['name'] == jo['name_plural']:
name_obj = {'str_sp': jo['name']}
else:
name_obj = {'str': jo['name'], 'str_pl': jo['name_plural']}
jo['name'] = name_obj
del jo['name_plural']
change = True
else:
name_obj = {'str': jo['name']}
jo['name'] = name_obj
change = True

return json_data if change else None


def change_file(json_dir):
for root, directories, filenames in os.walk("..\\{0}".format(json_dir)):
for filename in filenames:
path = os.path.join(root, filename)
if path.endswith(".json"):
new = gen_new(path)
if new is not None:
with open(path, "w") as jf:
json.dump(new, jf, ensure_ascii=False)
os.system(".\\json_formatter.exe {0}".format(path))


if __name__ == "__main__":
json_dir = input("What directory are the json files in? ")
change_file(json_dir)
9 changes: 8 additions & 1 deletion Tools/pl_script.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/usr/bin/env python3

# Make sure you have python3 installed.
# Ensure that the json_formatter is kept in Tools with this script. They must be in the same folder!
# For Windows:
# Using command prompt type 'python vehicle_code.py' and then the dir.
# For Max OS X or Linux:
# Swap any '\\' with '/', then run the script as in windows.

import argparse
import json
import os
Expand Down Expand Up @@ -42,4 +49,4 @@ def gen_new(path):
if new is not None:
with open(path, "w") as jf:
json.dump(new, jf, ensure_ascii=False)
os.system(f"./tools/format/json_formatter.cgi {path}")
os.system(f".\\json_formatter.exe {path}")
44 changes: 25 additions & 19 deletions Tools/vehicle_code.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,64 @@
#!/usr/bin/env python3

# Make sure you have python3 installed.
# Ensure that the json_formatter is kept in Tools with this script.
# Ensure that the json_formatter is kept in Tools with this script. They must be in the same folder!
# For Windows:
# Using command prompt type 'python vehicle_code.py'
# Using command prompt type "python vehicle_code.py"
# For Max OS X or Linux:
# Swap any '\\' with '/', then run the script as in windows.
# Swap any "\\" with "/", then run the script as in windows.

import json
import os


def gen_new(path):
change = False
with open(path, 'r') as json_file:
with open(path, "r") as json_file:
try:
json_data = json.load(json_file)
except UnicodeDecodeError:
print(f'UnicodeDecodeError in {path}')
print("UnicodeDecodeError in {0}".format(path))
return None
except json.decoder.JSONDecodeError:
print("JSONDecodeError in {0}".format(path))
return None
for jo in json_data:
locations = {}
# We only want JsonObjects
if type(jo) is str:
continue
# And only vehicles
if jo.get('type') != 'vehicle':
if jo.get("type") != "vehicle":
continue
for part in jo['parts']:
x, y = part['x'], part['y']
item = part.get('part')
if part.get('parts'):
locations[(x, y)] = {'x':x, 'y':y, 'parts':part['parts']}
for part in jo["parts"]:
x, y = part["x"], part["y"]
item = part.get("part")
if part.get("parts"):
if locations.get((x, y)):
locations[(x, y)]["parts"].extend(part["parts"])
else:
locations[(x, y)] = {"x": x, "y": y, "parts": part["parts"]}
elif (x, y) in locations:
locations[(x, y)]['parts'].append(item)
locations[(x, y)]["parts"].append(item)
else:
locations[(x, y)] = {'x':x, 'y':y, 'parts':[item]}
jo['parts'] = [locations[key] for key in locations]
locations[(x, y)] = {"x": x, "y": y, "parts": [item]}
jo["parts"] = [locations[key] for key in locations]
change = True
return json_data if change else None


def change_file(json_dir):
for root, directories, filenames in os.walk(f'../{json_dir}'):
for root, directories, filenames in os.walk("..\\{0}".format(json_dir)):
for filename in filenames:
path = os.path.join(root, filename)
if path.endswith('.json'):
if path.endswith(".json"):
new = gen_new(path)
if new is not None:
with open(path, 'w') as jf:
with open(path, "w") as jf:
json.dump(new, jf, ensure_ascii=False)
os.system(f'.\\json_formatter.exe {path}')
os.system(".\\json_formatter.exe {0}".format(path))


if __name__ == "__main__":
json_dir = input('What directory are the json files in? ')
json_dir = input("What directory are the json files in? ")
change_file(json_dir)
Loading