Guest User

Untitled

a guest
Oct 21st, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.78 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. def replace_sections(file1_path, file2_path, output_path):
  5.     """
  6.    Replace multiple sections in file1 with those from file2:
  7.    - FaceInfo section
  8.    - UsedGlyphRects section
  9.    - pointSizeSamplingMode section
  10.    - FaceInfo_Legacy section
  11.    - characterSequence value
  12.    - m_PathID value
  13.  
  14.    Args:
  15.        file1_path: Path to the first file (main content)
  16.        file2_path: Path to the second file (source of replacements)
  17.        output_path: Path where the result will be saved
  18.    """
  19.  
  20.     # Read both files
  21.     with open(file1_path, 'r', encoding='utf-8') as f:
  22.         file1_content = f.read()
  23.  
  24.     with open(file2_path, 'r', encoding='utf-8') as f:
  25.         file2_content = f.read()
  26.  
  27.     new_content = file1_content
  28.  
  29.     # ===== PART 1: Replace FaceInfo section =====
  30.     start_marker1 = " 0 FaceInfo m_FaceInfo"
  31.     end_marker1 = " 0 vector m_AtlasTextures"
  32.  
  33.     file1_start = new_content.find(start_marker1)
  34.     file1_end = new_content.find(end_marker1)
  35.  
  36.     file2_start = file2_content.find(start_marker1)
  37.     file2_end = file2_content.find(end_marker1)
  38.  
  39.     if file1_start == -1 or file1_end == -1:
  40.         raise ValueError("Could not find FaceInfo section markers in file1")
  41.     if file2_start == -1 or file2_end == -1:
  42.         raise ValueError("Could not find FaceInfo section markers in file2")
  43.  
  44.     # Extract the section from file2 (including start marker, excluding end marker)
  45.     file2_section1 = file2_content[file2_start:file2_end]
  46.  
  47.     # Replace in new_content
  48.     new_content = (
  49.             new_content[:file1_start] +
  50.             file2_section1 +
  51.             new_content[file1_end:]
  52.     )
  53.  
  54.     print(f"✓ Replaced FaceInfo section ({len(file2_section1)} characters)")
  55.  
  56.     # ===== PART 2: Replace UsedGlyphRects section =====
  57.     start_marker2 = " 0 GlyphRect m_UsedGlyphRects"
  58.     end_marker2 = " 0 FaceInfo_Legacy m_fontInfo"
  59.  
  60.     file1_start = new_content.find(start_marker2)
  61.     file1_end = new_content.find(end_marker2)
  62.  
  63.     file2_start = file2_content.find(start_marker2)
  64.     file2_end = file2_content.find(end_marker2)
  65.  
  66.     if file1_start == -1 or file1_end == -1:
  67.         raise ValueError("Could not find UsedGlyphRects section markers in file1")
  68.     if file2_start == -1 or file2_end == -1:
  69.         raise ValueError("Could not find UsedGlyphRects section markers in file2")
  70.  
  71.     # Extract the section from file2
  72.     file2_section2 = file2_content[file2_start:file2_end]
  73.  
  74.     # Replace in new_content
  75.     new_content = (
  76.             new_content[:file1_start] +
  77.             file2_section2 +
  78.             new_content[file1_end:]
  79.     )
  80.  
  81.     print(f"✓ Replaced UsedGlyphRects section ({len(file2_section2)} characters)")
  82.  
  83.     # ===== PART 3: Replace pointSizeSamplingMode section =====
  84.     start_marker3 = "  0 int pointSizeSamplingMode"
  85.     end_marker3 = "  1 string referencedFontAssetGUID"
  86.  
  87.     file1_start = new_content.find(start_marker3)
  88.     file1_end = new_content.find(end_marker3)
  89.  
  90.     file2_start = file2_content.find(start_marker3)
  91.     file2_end = file2_content.find(end_marker3)
  92.  
  93.     if file1_start == -1 or file1_end == -1:
  94.         raise ValueError("Could not find pointSizeSamplingMode section markers in file1")
  95.     if file2_start == -1 or file2_end == -1:
  96.         raise ValueError("Could not find pointSizeSamplingMode section markers in file2")
  97.  
  98.     # Extract the section from file2
  99.     file2_section3 = file2_content[file2_start:file2_end]
  100.  
  101.     # Replace in new_content
  102.     new_content = (
  103.             new_content[:file1_start] +
  104.             file2_section3 +
  105.             new_content[file1_end:]
  106.     )
  107.  
  108.     print(f"✓ Replaced pointSizeSamplingMode section ({len(file2_section3)} characters)")
  109.  
  110.     # ===== PART 4: Replace FaceInfo_Legacy section =====
  111.     start_marker4 = " 0 FaceInfo_Legacy m_fontInfo"
  112.     end_marker4 = " 0 FontAssetCreationSettings m_CreationSettings"
  113.  
  114.     file1_start = new_content.find(start_marker4)
  115.     file1_end = new_content.find(end_marker4)
  116.  
  117.     file2_start = file2_content.find(start_marker4)
  118.     file2_end = file2_content.find(end_marker4)
  119.  
  120.     if file1_start == -1 or file1_end == -1:
  121.         raise ValueError("Could not find FaceInfo_Legacy section markers in file1")
  122.     if file2_start == -1 or file2_end == -1:
  123.         raise ValueError("Could not find FaceInfo_Legacy section markers in file2")
  124.  
  125.     # Extract the section from file2
  126.     file2_section4 = file2_content[file2_start:file2_end]
  127.  
  128.     # Replace in new_content
  129.     new_content = (
  130.             new_content[:file1_start] +
  131.             file2_section4 +
  132.             new_content[file1_end:]
  133.     )
  134.  
  135.     print(f"✓ Replaced FaceInfo_Legacy section ({len(file2_section4)} characters)")
  136.  
  137.     # ===== PART 5: Replace characterSequence value =====
  138.     pattern_charseq = r'(  1 string characterSequence = )"[^"]*"'
  139.  
  140.     match_file2 = re.search(pattern_charseq, file2_content)
  141.     if not match_file2:
  142.         raise ValueError("Could not find characterSequence in file2")
  143.  
  144.     file2_charseq_line = match_file2.group(0)
  145.  
  146.     match_file1 = re.search(pattern_charseq, new_content)
  147.     if not match_file1:
  148.         raise ValueError("Could not find characterSequence in file1")
  149.  
  150.     new_content = new_content.replace(match_file1.group(0), file2_charseq_line)
  151.  
  152.     file2_charseq_value = match_file2.group(0).split('"')[1]
  153.     print(f"✓ Replaced characterSequence: {file2_charseq_value[:50]}...")
  154.  
  155.     # Write the result
  156.     with open(output_path, 'w', encoding='utf-8') as f:
  157.         f.write(new_content)
  158.  
  159.     print(f"\n✓ Success! Output written to: {output_path}")
  160.  
  161.  
  162. # Usage example:
  163. if __name__ == "__main__":
  164.     replace_sections(
  165.         file1_path="file1.txt",  # Your first file
  166.         file2_path="file2.txt",  # Your second file
  167.         output_path="output.txt"  # Where to save the result
  168.     )
Add Comment
Please, Sign In to add comment