Files
red-valley/_merge_items.py

96 lines
3.4 KiB
Python
Raw Normal View History

import re, os
def extract_items_with_defs(path):
"""Extract item keys and their full definitions from a lua items file."""
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
items = {}
# Match multi-line item blocks: ['key'] = { ... },
pattern = r"\['([a-zA-Z0-9_]+)'\]\s*=\s*\{([^}]+)\}"
for match in re.finditer(pattern, content):
key = match.group(1)
body = match.group(2)
# Only count items that have ['name'] inside
if "['name']" in body:
items[key] = body.strip()
return items
def extract_field(body, field):
"""Extract a field value from item body."""
m = re.search(r"\['" + field + r"'\]\s*=\s*(.+?)(?:,\s*$|,\s*\[)", body, re.MULTILINE)
if m:
return m.group(1).strip().rstrip(',')
# try simpler
m = re.search(r"\['" + field + r"'\]\s*=\s*([^,]+)", body)
if m:
return m.group(1).strip()
return None
def to_qbcore_line(key, body):
"""Convert a qs-inventory item block to a qb-core single-line format."""
fields = ['name', 'label', 'weight', 'type', 'image', 'unique', 'useable', 'shouldClose', 'combinable', 'description']
vals = {}
for f in fields:
v = extract_field(body, f)
if v is not None:
vals[f] = v
# Build the line in qb-core style
# Pad the key to align
padded_key = f"['{key}']"
padded_key = padded_key.ljust(40)
parts = []
for f in fields:
if f in vals:
padded_f = f"['{f}']"
parts.append(f"{padded_f} = {vals[f]}")
line = f" {padded_key} = {{{', '.join(parts)}}},"
return line
# Paths
preload_path = r'E:\FiveMserver\server\_preLoad\qs-inventory\[inventory]\qs-inventory\shared\items.lua'
qbcore_path = r'E:\FiveMserver\server\resources\[framework]\[core]\qb-core\shared\items.lua'
# Extract items from both
preload_items = extract_items_with_defs(preload_path)
qbcore_items = extract_items_with_defs(qbcore_path)
# Find items only in preload
only_preload = sorted(set(preload_items.keys()) - set(qbcore_items.keys()))
print(f"Items to add: {len(only_preload)}")
# Generate qb-core lines
new_lines = []
new_lines.append("")
new_lines.append(" -- ═══════════════════════════════════════════════════")
new_lines.append(" -- ITEMS IMPORTATE DIN QS-INVENTORY (preLoad update)")
new_lines.append(" -- ═══════════════════════════════════════════════════")
new_lines.append("")
for key in only_preload:
body = preload_items[key]
line = to_qbcore_line(key, body)
new_lines.append(line)
# Read qb-core file
with open(qbcore_path, 'r', encoding='utf-8', errors='ignore') as f:
qb_content = f.read()
# Find the last } that closes QBShared.Items
# Insert before the last }
last_brace = qb_content.rfind('}')
if last_brace == -1:
print("ERROR: Could not find closing brace")
else:
new_content = qb_content[:last_brace] + '\n'.join(new_lines) + '\n' + qb_content[last_brace:]
with open(qbcore_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"SUCCESS: Added {len(only_preload)} items to qb-core items.lua")
print("First 5 items added:")
for k in only_preload[:5]:
print(f" + {k}")
print(f" ... and {len(only_preload)-5} more")