Advertisement
cfox04

unkushort1_test

Oct 10th, 2022
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. import os
  2. import sys
  3. from xml.etree.ElementTree import Element
  4. from utils import walk_dir_xml
  5. from element_data import ElementData, ElementProperty, StringProperty, FloatProperty, IntProperty, Vector3Property, Vector4Property
  6. from dataclasses import dataclass
  7.  
  8. RESULTS_FILE = "results_unkushort1.txt"
  9. RESULTS_FILE_VERBOSE = "results_unkushort1_verbose.txt"
  10. XML_DIR = "./yft"
  11.  
  12. # Determine if an UnkUshort1 value exists in the file that either
  13. # 1. Is an invalid shader index
  14. # 2. Is a valid shader index but not a "vehicle_vehglass" shader
  15. def has_invalid_unkushort1(element: Element):
  16.     shaders = element.findall(".//Shaders/*")
  17.     unkushort1s = element.iterfind(".//UnkUshort1")
  18.  
  19.     for unkushort1 in unkushort1s:
  20.         index = int(unkushort1.attrib["value"]) - 1
  21.         if index >= len(shaders):
  22.             return True
  23.  
  24.         shader = shaders[index]
  25.         if not "vehicle_vehglass" in shader.find("Name").text:
  26.             return True
  27.  
  28.     return False
  29.  
  30.  
  31. def has_windows(element: Element):
  32.     return element.find(".//VehicleGlassWindows/*")
  33.  
  34.  
  35. def percent(value: float) -> str:
  36.     return f"{round(value, 3) * 100}%"
  37.  
  38.  
  39. def print_status(i, n):
  40.     j = (i + 1) / n
  41.     sys.stdout.write('\r')
  42.     # the exact output you're looking for:
  43.     sys.stdout.write("[%-20s] %d%%" % ('='*int(20*j), 100*j))
  44.     sys.stdout.flush()
  45.  
  46.  
  47. with open(RESULTS_FILE, "w") as f, open(RESULTS_FILE_VERBOSE, "w") as f2:
  48.     num_files = len(os.listdir(XML_DIR))
  49.     num_invalid_unkushort1s = 0
  50.     current_file = 1
  51.  
  52.     files_with_windows = []
  53.  
  54.     for filename, doc in walk_dir_xml(XML_DIR):
  55.         if not has_windows(doc):
  56.             continue
  57.  
  58.         files_with_windows.append((filename, doc))
  59.  
  60.     num_yfts_with_windows = len(files_with_windows)
  61.  
  62.     for filename, doc in files_with_windows:
  63.         # Messages for verbose file
  64.         verbose_messages = []
  65.        
  66.         # Progress bar
  67.         print_status(current_file, num_yfts_with_windows)
  68.  
  69.         if has_invalid_unkushort1(doc):
  70.             num_invalid_unkushort1s += 1
  71.             verbose_messages.append(
  72.                 "Has an UnkUshort1 that does not lead to a vehicle_vehglass shader!\n")
  73.  
  74.         current_file += 1
  75.  
  76.         if verbose_messages:
  77.             f2.write(f"======{filename}=======\n")
  78.             for msg in verbose_messages:
  79.                 f2.write(msg)
  80.  
  81.  
  82.     percent_invalid_unkushort1s = percent(
  83.         num_invalid_unkushort1s/num_yfts_with_windows)
  84.  
  85.     f.write(f"In {percent_invalid_unkushort1s} yft(s) with windows, there were UnkUshort1 values that did not lead to a vehicle_vehglass shader.\n")
  86.     f.write(f"Tested {num_yfts_with_windows} yft(s) with windows.")
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement