Generation XML diff on a fly for the scripts using XMLDIff with Visual Studio Code

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

User avatar
ChemODun
Posts: 436
Joined: Mon, 12. Feb 07, 21:58
x4

Generation XML diff on a fly for the scripts using XMLDIff with Visual Studio Code

Post by ChemODun »

Using XMLDIff with Visual Studio Code

Introduction

This document describes how to use XMLDiff with Visual Studio Code.

Prerequisites Steps
  1. Prepare a folders structure like the following:

    Code: Select all

        RootExtensionFolder
        │   XMLDiffAndPatch
        │   │   XMLDiff.exe
        │   RunXMLDiff.bat
        │   aiscripts
        │   aiscripts.modified
        │   aiscripts.original
        │   md
        │   md.modified
        │   md.original
        
  2. You can skip creation *.original folders, if you will use the extracted files folder.
  3. Copy the RunXMLDiff.bat file to the root of your project.
  4. Copy the XMLDiff.exe file to the XMLDiffAndPatch folder in the root of your project.
  5. Install the VSCode extension Run on Save by emeraldwalk
  6. Open the settings of the extension by pressing Ctrl + , and search for runOnSave.commands
  7. Add the following configuration to the settings:
    1. In case of not to use the "Extracted" files folder, i.e. with *.original folders:

      Code: Select all

          "commands": [
              {
                  "match": "\\.modified\\\\.+?\\.xml$",
                  "cmd": "${workspaceFolder}\\RunXMLDiff.bat ${file} ${workspaceFolder}\\XMLDiffAndPatch\\XMLDiff.exe
              }
          ]
          
    2. With the extracted folder, which is outside the project folder:

      Code: Select all

          "commands": [
              {
                  "match": "\\.modified\\\\.+?\\.xml$",
                  "cmd": "${workspaceFolder}\\RunXMLDiff.bat ${file} ${workspaceFolder}\\XMLDiffAndPatch\\XMLDiff.exe" ${workspaceFolder}\\..\\extracted"
              }
          ]
          
      Take in account that with the extracted folder you still can use the *.original folders for original files. It will be used first, if they exist, otherwise the script will use the extracted folder.
      /
      • Save the settings and close the settings tab.
      Now you can modify the *.modified files and the RunXMLDiff.bat will be executed automatically on save.

      You can check an output in the OUTPUT tab in the VSCode, selecting the Run on Save item in the dropdown list.

      Description of the RunXMLDiff.bat

      RunXMLDiff.bat is a batch script that compares a modified XML file with its original version using the XMLDiff.exe utility. The script takes the paths to the modified file, the XMLDiff utility, and optionally the path to the original files as input. It then constructs the appropriate paths, checks for the existence of necessary files and directories, and runs the XMLDiff utility to generate a diff file.

      Usage

      Code: Select all

      RunXMLDiff.bat <ModifiedFilePath> <XMLDiffPath> [OriginalFilesPath]
      
      Parameters
      • <ModifiedFilePath>: The full path to the modified XML file. This parameter is mandatory.
      • <XMLDiffPath>: The full path to the XMLDiff.exe utility. This parameter is mandatory.
      • [OriginalFilesPath]: The full path to the directory containing the original XML files. This parameter is optional.
      Example

      Code: Select all

      RunXMLDiff.bat "C:\path\to\md.modified\file.xml" "C:\path\to\XMLDiff.exe" "C:\path\to\original\files"
      
      How It Works
      1. Extract Type and Filename: The script extracts the type (e.g., aiscript or md) and filename from the modified file path.
      2. Determine Target File Path: The script constructs the target file path by removing .modified from the modified file path.
      3. Determine Original File Path:
      4. If the OriginalFilesPath is provided, the original file path is constructed as OriginalFilesPath\type\filename.
      5. If the OriginalFilesPath is not provided, the original file path is constructed by replacing .modified with .original in the modified file path.
      6. Check Existence:
      7. The script checks for the existence of XMLDiff.exe, the modified file, the original file, and the target file directory.
      8. Run XMLDiff: If all checks pass, the script runs XMLDiff.exe with the appropriate parameters to generate the diff file.

        Error Handling

        The script will output error messages and exit with a non-zero status code if any of the following conditions are met:
        • XMLDiff.exe is not found at the specified path.
        • The modified file is not found at the specified path.
        • The original file is not found at the constructed path.
        • The target file directory does not exist.
        Output

        If the script runs successfully, it will output a message indicating that the XMLDiff operation completed successfully and provide the path to the generated diff file.

        Notes
        • Ensure that the XMLDiff.exe utility is accessible and has the necessary permissions to execute.
        • The script assumes that the modified file path follows the convention where the last folder in the path is of the form type.modified.
        • If you have some beautification plugins for XML use the alternative save (Ctrl + K S, i.e. "Save without Formatting), which will not involve them, to get the diff as small as possible.
        Demo

        Small demo video

        License

        This script is provided "as-is" without any warranty. Use it at your own risk.
Multiply entropy by absolute zero

Freedom in space

Return to “X4: Foundations - Scripts and Modding”