<?xml version="1.0" encoding="utf-8"?>
<Project>

  <!--
    ZipMod target: assembles a Thunderstore-ready zip under package/.
    Run explicitly with:  dotnet build -t:ZipMod -c Release
    It does NOT run automatically on every build.

    "Pack" is reserved by the .NET SDK for NuGet packaging, so this target
    uses the name "ZipMod" to avoid conflict.

    Per-project customisation properties (set in the project's PropertyGroup):
      PackHasChangelog     – set to 'true' to include CHANGELOG.md in the zip
      PackUsePluginsLayout – set to 'true' for the plugins/patchers folder layout
                             (used by Dustbin and LabOpt)

    Simple layout zips:
      <AssemblyName>.dll  icon.png  manifest.json  README.md  [CHANGELOG.md]
    plugins/patchers layout zips:
      plugins/  patchers/  icon.png  manifest.json  README.md  [CHANGELOG.md]
  -->
  <Target Name="ZipMod" DependsOnTargets="Build" Condition="'$(PackPreloaderTargetDir)' == ''">
    <PropertyGroup>
      <_PackageDir>$(MSBuildProjectDirectory)\package</_PackageDir>
      <_StagingDir>$(MSBuildProjectDirectory)\obj\pack-staging</_StagingDir>
      <_ZipPath>$(_PackageDir)\$(ProjectName)-$(Version).zip</_ZipPath>
    </PropertyGroup>

    <!-- Sync manifest.json version_number from .csproj $(Version) before packaging -->
    <Exec Condition="Exists('$(_PackageDir)\manifest.json')"
          Command="powershell.exe -NoProfile -ExecutionPolicy Bypass -Command &quot;$f='$(_PackageDir)\manifest.json'; $c=[IO.File]::ReadAllText($f); $c=$c -replace '(\u0022version_number\u0022\s*:\s*\u0022)[^\u0022]*(\u0022)','${1}$(Version)${2}'; [IO.File]::WriteAllText($f,$c,[Text.UTF8Encoding]::new($true))&quot;" />

    <!-- ── plugins/patchers layout: copy DLL into package/plugins/ first ── -->
    <MakeDir Directories="$(_PackageDir)\plugins"
             Condition="'$(PackUsePluginsLayout)' == 'true'" />
    <Copy SourceFiles="$(TargetPath)" DestinationFolder="$(_PackageDir)\plugins"
          Condition="'$(PackUsePluginsLayout)' == 'true'" />

    <!-- Collect loose root files (common to both layouts) -->
    <ItemGroup>
      <_PackRootFiles Include="$(_PackageDir)\icon.png" />
      <_PackRootFiles Include="$(_PackageDir)\manifest.json" />
      <_PackRootFiles Include="$(MSBuildProjectDirectory)\README.md" />
      <_PackRootFiles Include="$(MSBuildProjectDirectory)\CHANGELOG.md"
                      Condition="'$(PackHasChangelog)' == 'true'" />
      <!-- Simple layout: also include the DLL itself at the root -->
      <_PackRootFiles Include="$(TargetPath)"
                      Condition="'$(PackUsePluginsLayout)' != 'true'" />
    </ItemGroup>

    <!-- Prepare staging directory -->
    <RemoveDir Directories="$(_StagingDir)" />
    <MakeDir Directories="$(_StagingDir)" />

    <!-- Copy loose root files into staging root -->
    <Copy SourceFiles="@(_PackRootFiles)" DestinationFolder="$(_StagingDir)" />

    <!-- plugins/patchers layout: copy sub-folders into staging (preserve structure) -->
    <ItemGroup Condition="'$(PackUsePluginsLayout)' == 'true'">
      <_PluginFiles Include="$(_PackageDir)\plugins\**\*" />
      <_PatcherFiles Include="$(_PackageDir)\patchers\**\*" />
    </ItemGroup>
    <Copy SourceFiles="@(_PluginFiles)"
          DestinationFiles="@(_PluginFiles->'$(_StagingDir)\plugins\%(RecursiveDir)%(Filename)%(Extension)')"
          Condition="'$(PackUsePluginsLayout)' == 'true'" />
    <Copy SourceFiles="@(_PatcherFiles)"
          DestinationFiles="@(_PatcherFiles->'$(_StagingDir)\patchers\%(RecursiveDir)%(Filename)%(Extension)')"
          Condition="'$(PackUsePluginsLayout)' == 'true'" />

    <!-- Delete previous zip if present -->
    <Delete Files="$(_ZipPath)" Condition="Exists('$(_ZipPath)')" />

    <!-- Create zip using MSBuild ZipDirectory task – no shell, no quoting issues -->
    <ZipDirectory SourceDirectory="$(_StagingDir)" DestinationFile="$(_ZipPath)" />

    <!-- Clean up staging directory -->
    <RemoveDir Directories="$(_StagingDir)" />

    <Message Text="ZipMod: created $(_ZipPath)" Importance="high" />
  </Target>

  <!-- ── Preloader helper target ────────────────────────────────────────────
       Preloader projects (DustbinPreloader, LabOptPreloader) call this to
       copy their output DLL into the sibling main mod's patchers/ folder.
       Set PackPreloaderTargetDir in the preloader's csproj, e.g.:
         <PackPreloaderTargetDir>..\Dustbin\package\patchers</PackPreloaderTargetDir>
  ──────────────────────────────────────────────────────────────────────── -->
  <Target Name="CopyToParentPackage" DependsOnTargets="Build"
          Condition="'$(PackPreloaderTargetDir)' != ''">
    <MakeDir Directories="$(PackPreloaderTargetDir)" />
    <Copy SourceFiles="$(TargetPath)" DestinationFolder="$(PackPreloaderTargetDir)" />
    <Message Text="CopyToParentPackage: copied $(TargetFileName) to $(PackPreloaderTargetDir)" Importance="high" />
  </Target>
</Project>
