<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup Label="Target">
        <EnableWindowsTargeting>True</EnableWindowsTargeting>
    </PropertyGroup>

    <PropertyGroup Label="Feature">
        <Description>XIV Launcher addon framework</Description>
        <DalamudVersion>12.0.0.13</DalamudVersion>
        <AssemblyVersion>$(DalamudVersion)</AssemblyVersion>
        <Version>$(DalamudVersion)</Version>
        <FileVersion>$(DalamudVersion)</FileVersion>
        <PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
    </PropertyGroup>

    <PropertyGroup Label="Output">
        <OutputType>Library</OutputType>
        <OutputPath>..\bin\$(Configuration)\</OutputPath>
        <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
        <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
        <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
        <ProduceReferenceAssembly>false</ProduceReferenceAssembly>
    </PropertyGroup>

    <PropertyGroup Label="Documentation">
        <DocumentationFile>$(OutputPath)Dalamud.xml</DocumentationFile>
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
    </PropertyGroup>

    <PropertyGroup Label="Build">
        <UseWindowsForms>true</UseWindowsForms>
        <EnableDynamicLoading>true</EnableDynamicLoading>
        <DebugSymbols>true</DebugSymbols>
        <DebugType>portable</DebugType>
        <Deterministic>true</Deterministic>
        <Nullable>annotations</Nullable>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    </PropertyGroup>

    <PropertyGroup Label="Configuration">
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    </PropertyGroup>
    <PropertyGroup Label="Configuration" Condition="'$(Configuration)'=='Debug'">
        <DefineConstants>DEBUG;TRACE</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Label="Warnings">
        <NoWarn>IDE0002;IDE0003;IDE1006;IDE0044;CA1822;CS1591;CS1701;CS1702</NoWarn>
        <!-- IDE1002 - Simplify member access -->
        <!-- IDE1003 - This and me preferences -->
        <!-- IDE1006 - Naming violation -->
        <!-- IDE1006 - Add readonly modifier -->
        <!-- CA1822  - Can be marked as static -->
        <!-- CS1591  - Missing XML comment for publicly visible type or member -->
        <!-- CS1701  - Runtime policy may be needed -->
        <!-- CS1702  - Runtime policy may be needed -->
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="BitFaster.Caching" Version="2.4.1" />
        <PackageReference Include="CheapLoc" Version="1.1.8" />
        <PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.4" PrivateAssets="all" />
        <PackageReference Include="goatcorp.Reloaded.Hooks" Version="4.2.0-goatcorp4" />
        <PackageReference Include="goatcorp.Reloaded.Assembler" Version="1.0.14-goatcorp3" />
        <PackageReference Include="JetBrains.Annotations" Version="2024.2.0" />
        <PackageReference Include="Lumina" Version="$(LuminaVersion)" />
        <PackageReference Include="Lumina.Excel" Version="$(LuminaExcelVersion)" />
        <PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.0-preview.1.24081.5" />
        <PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.46-beta">
          <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="MinSharp" Version="1.0.4" />
        <PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" />
        <PackageReference Include="Serilog" Version="4.0.2" />
        <PackageReference Include="Serilog.Sinks.Async" Version="2.0.0" />
        <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
        <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
        <PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
        <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
        <PackageReference Include="System.Drawing.Common" Version="8.0.0" />
        <PackageReference Include="System.Reactive" Version="5.0.0" />
        <PackageReference Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
        <PackageReference Include="System.Resources.Extensions" Version="8.0.0" />
        <PackageReference Include="TerraFX.Interop.Windows" Version="10.0.22621.2" />
    </ItemGroup>
    <ItemGroup>
        <ProjectReference Include="..\Dalamud.Common\Dalamud.Common.csproj" />
        <ProjectReference Include="..\lib\FFXIVClientStructs\FFXIVClientStructs\FFXIVClientStructs.csproj" />
        <ProjectReference Include="..\lib\FFXIVClientStructs\InteropGenerator.Runtime\InteropGenerator.Runtime.csproj" />
        <ProjectReference Include="..\lib\ImGuiScene\deps\ImGui.NET\src\ImGui.NET-472\ImGui.NET-472.csproj" />
        <ProjectReference Include="..\lib\ImGuiScene\deps\SDL2-CS\SDL2-CS.csproj" />
        <ProjectReference Include="..\lib\ImGuiScene\ImGuiScene\ImGuiScene.csproj" />
    </ItemGroup>

    <ItemGroup>
        <AdditionalFiles Include="..\stylecop.json" />
    </ItemGroup>

    <ItemGroup>
      <None Remove="licenses.txt" />
      <Content Include="licenses.txt">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
    </ItemGroup>

    <ItemGroup>
      <EmbeddedResource Include="Interface\ImGuiSeStringRenderer\Internal\TextProcessing\DerivedGeneralCategory.txt" LogicalName="DerivedGeneralCategory.txt" />
      <EmbeddedResource Include="Interface\ImGuiSeStringRenderer\Internal\TextProcessing\EastAsianWidth.txt" LogicalName="EastAsianWidth.txt" />
      <EmbeddedResource Include="Interface\ImGuiSeStringRenderer\Internal\TextProcessing\emoji-data.txt" LogicalName="emoji-data.txt" />
      <EmbeddedResource Include="Interface\ImGuiSeStringRenderer\Internal\TextProcessing\LineBreak.txt" LogicalName="LineBreak.txt" />
    </ItemGroup>

    <Target Name="AddRuntimeDependenciesToContent" BeforeTargets="GetCopyToOutputDirectoryItems" DependsOnTargets="GenerateBuildDependencyFile;GenerateBuildRuntimeConfigurationFiles">
        <ItemGroup>
            <ContentWithTargetPath Include="$(ProjectDepsFilePath)" CopyToOutputDirectory="PreserveNewest" TargetPath="$(ProjectDepsFileName)" />
            <ContentWithTargetPath Include="$(ProjectRuntimeConfigFilePath)" CopyToOutputDirectory="PreserveNewest" TargetPath="$(ProjectRuntimeConfigFileName)" />
        </ItemGroup>
    </Target>

    <PropertyGroup>
        <!-- Needed temporarily for CI -->
        <TempVerFile>$(OutputPath)TEMP_gitver.txt</TempVerFile>
        <CommitHashFile>$(OutputPath)commit_hash.txt</CommitHashFile>
        <DalamudRevisionFile>$(OutputPath)revision.txt</DalamudRevisionFile>
    </PropertyGroup>

    <Target Name="GetVersionData" BeforeTargets="WriteVersionData" Condition="'$(SCMVersion)'=='' And '$(Configuration)'=='Release'">
        <Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))&quot; rev-list --count HEAD" ConsoleToMSBuild="true">
            <Output TaskParameter="ConsoleOutput" PropertyName="DalamudGitCommitCount" />
        </Exec>
        <Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))&quot; describe --match=NeVeRmAtCh --always --abbrev=40 --dirty" ConsoleToMSBuild="true">
            <Output TaskParameter="ConsoleOutput" PropertyName="DalamudGitCommitHash" />
        </Exec>
        <Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))&quot; describe --tags --always --dirty" ConsoleToMSBuild="true">
            <Output TaskParameter="ConsoleOutput" PropertyName="DalamudGitDescribeOutput" />
        </Exec>
        <Exec Command="git -C &quot;$(ProjectDir.Replace('\','\\'))\..\lib\FFXIVClientStructs&quot; describe --long --always --dirty" ConsoleToMSBuild="true">
            <Output TaskParameter="ConsoleOutput" PropertyName="ClientStructsGitDescribeOutput" />
        </Exec>

        <PropertyGroup>
            <CommitCount>$([System.Text.RegularExpressions.Regex]::Replace($(DalamudGitCommitCount), @"\t|\n|\r", ""))</CommitCount>
            <CommitHash>$([System.Text.RegularExpressions.Regex]::Replace($(DalamudGitCommitHash), @"\t|\n|\r", ""))</CommitHash>
            <SCMVersion>$([System.Text.RegularExpressions.Regex]::Replace($(DalamudGitDescribeOutput), @"\t|\n|\r", ""))</SCMVersion>
            <CommitHashClientStructs>$([System.Text.RegularExpressions.Regex]::Replace($(ClientStructsGitDescribeOutput), @"\t|\n|\r", ""))</CommitHashClientStructs>
        </PropertyGroup>

        <Exec Command="echo|set /P =&quot;$(CommitCount)&quot; &gt; $(DalamudRevisionFile)" IgnoreExitCode="true" />
        <Exec Command="echo|set /P =&quot;$(CommitHash)&quot; &gt; $(CommitHashFile)" IgnoreExitCode="true" />
        <Exec Command="echo|set /P =&quot;$(SCMVersion)&quot; &gt; $(TempVerFile)" IgnoreExitCode="true" />
    </Target>

    <Target Name="GenerateStubVersionData" BeforeTargets="WriteVersionData" Condition="'$(SCMVersion)'=='' And '$(Configuration)'!='Release'">
        <!-- stub out version since it takes a while. -->
        <PropertyGroup>
            <SCMVersion>Local build at $([System.DateTime]::Now.ToString(yyyy-MM-dd HH:mm:ss))</SCMVersion>
            <CommitHashClientStructs>???</CommitHashClientStructs>
        </PropertyGroup>
    </Target>

    <Target Name="WriteVersionData" BeforeTargets="CoreCompile">
        <!-- names the obj/.../CustomAssemblyInfo.cs file -->
        <PropertyGroup>
            <CustomAssemblyInfoFile>$(IntermediateOutputPath)CustomAssemblyInfo.cs</CustomAssemblyInfoFile>
        </PropertyGroup>
        <!-- includes the CustomAssemblyInfo for compilation into your project -->
        <ItemGroup>
            <Compile Include="$(CustomAssemblyInfoFile)" />
        </ItemGroup>
        <!-- defines the AssemblyMetadata attribute that will be written -->
        <ItemGroup>
            <AssemblyAttributes Include="AssemblyMetadata" Condition="'$(SCMVersion)' != ''">
                <_Parameter1>SCMVersion</_Parameter1>
                <_Parameter2>$(SCMVersion)</_Parameter2>
            </AssemblyAttributes>
            <AssemblyAttributes Include="AssemblyMetadata" Condition="'$(CommitCount)' != ''">
                <_Parameter1>GitCommitCount</_Parameter1>
                <_Parameter2>$(CommitCount)</_Parameter2>
            </AssemblyAttributes>
            <AssemblyAttributes Include="AssemblyMetadata" Condition="'$(CommitHashClientStructs)' != ''">
                <_Parameter1>GitHashClientStructs</_Parameter1>
                <_Parameter2>$(CommitHashClientStructs)</_Parameter2>
            </AssemblyAttributes>
            <AssemblyAttributes Include="AssemblyMetadata" Condition="'$(CommitHash)' != ''">
                <_Parameter1>GitHash</_Parameter1>
                <_Parameter2>$(CommitHash)</_Parameter2>
            </AssemblyAttributes>
        </ItemGroup>
        <!-- writes the attribute to the customAssemblyInfo file -->
        <WriteCodeFragment Language="C#" OutputFile="$(CustomAssemblyInfoFile)" AssemblyAttributes="@(AssemblyAttributes)" />
    </Target>

    <!-- Copy plugin .targets folder into distrib -->
    <Target Name="CopyPluginTargets" AfterTargets="Build">
        <Copy SourceFiles="$(ProjectDir)\..\targets\Dalamud.Plugin.targets;$(ProjectDir)\..\targets\Dalamud.Plugin.Bootstrap.targets" DestinationFolder="$(OutDir)\targets" />
    </Target>
</Project>
