<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
    <Import Project="environment.props"/>
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{E3FE707A-B402-4C69-8C3D-E9C0C709715D}</ProjectGuid>
        <OutputType>Library</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>PerfectPlacement</RootNamespace>
        <AssemblyName>PerfectPlacement</AssemblyName>
        <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
        <LangVersion>10</LangVersion>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    </PropertyGroup>
    <Choose>
        <When Condition="($(OS) == 'Unix' OR $(OS) == 'OSX') AND $(GamePath) == ''">
            <PropertyGroup>
                <GamePath Condition="!Exists('$(GamePath)')">$(HOME)/.steam/steam/steamapps/common/Valheim</GamePath>
                <GamePath Condition="!Exists('$(GamePath)')">$(HOME)/Library/Application Support/Steam/steamapps/common/Valheim/Contents/MacOS</GamePath>
                <CorlibPath Condition="!Exists('$(CorlibPath)')">$(GamePath)/unstripped_corelib</CorlibPath>
            </PropertyGroup>
        </When>
        <When Condition="($(OS) == 'Windows_NT') AND $(GamePath) == ''">
            <PropertyGroup>
                <GamePath Condition="!Exists('$(GamePath)')">$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 892970', 'InstallLocation', null, RegistryView.Registry64, RegistryView.Registry32))</GamePath>
                <_SteamLibraryPath>$([MSBuild]::GetRegistryValueFromView('HKEY_CURRENT_USER\SOFTWARE\Valve\Steam', 'SteamPath', null, RegistryView.Registry32))</_SteamLibraryPath>
                <GamePath Condition="!Exists('$(GamePath)') AND '$(_SteamLibraryPath)' != ''">$(_SteamLibraryPath)\steamapps\common\Valheim</GamePath>
                <GamePath Condition="!Exists('$(GamePath)') AND Exists('$(ValheimGamePath)')">$(ValheimGamePath)</GamePath>
                <GamePath Condition="!Exists('$(GamePath)')">C:\Program Files\Steam\steamapps\common\Valheim</GamePath>
                <GamePath Condition="!Exists('$(GamePath)')">C:\Program Files (x86)\Steam\steamapps\common\Valheim</GamePath>
                <GamePath Condition="!Exists('$(GamePath)')">D:\SteamLibrary\steamapps\common\Valheim</GamePath>
                <BepInExPath Condition="!Exists('$(BepInExPath)')AND Exists('$(GamePath)')">$(GamePath)\BepInEx</BepInExPath>
                <CorlibPath Condition="!Exists('$(CorlibPath)') AND Exists('$(GamePath)')">$(GamePath)\unstripped_corlib</CorlibPath>
                <PublicizedAssembliesPath Condition="!Exists('$(PublicizedAssembliesPath)') AND Exists('$(GamePath)')">$(GamePath)\valheim_Data\Managed\publicized_assemblies</PublicizedAssembliesPath>
                <AfterTargets Condition="'$(AfterTargets)' == ''">ILRepacker</AfterTargets>
            </PropertyGroup>
        </When>
    </Choose>
    <ItemGroup>
        <Reference Include="0Harmony">
            <HintPath>$(BepInExPath)\core\0Harmony.dll</HintPath>
        </Reference>
        <Reference Include="assembly_guiutils">
            <HintPath>$(PublicizedAssembliesPath)\assembly_guiutils_publicized.dll</HintPath>
        </Reference>
        <Reference Include="assembly_utils">
            <HintPath>$(PublicizedAssembliesPath)\assembly_utils_publicized.dll</HintPath>
        </Reference>
        <Reference Include="assembly_valheim">
            <HintPath>$(PublicizedAssembliesPath)\assembly_valheim_publicized.dll</HintPath>
        </Reference>
        <Reference Include="BepInEx">
            <HintPath>$(BepInExPath)\core\BepInEx.dll</HintPath>
        </Reference>
        <Reference Include="ServerSync">
            <HintPath>Libs\ServerSync.dll</HintPath>
        </Reference>
        <Reference Include="System"/>
        <Reference Include="System.Core"/>
        <Reference Include="System.Data"/>
        <Reference Include="System.Xml"/>
        <Reference Include="UnityEngine">
            <HintPath>$(CorlibPath)\UnityEngine.dll</HintPath>
        </Reference>
        <Reference Include="UnityEngine.CoreModule">
            <HintPath>$(CorlibPath)\UnityEngine.CoreModule.dll</HintPath>
        </Reference>
        <Reference Include="UnityEngine.InputLegacyModule">
            <HintPath>$(CorlibPath)\UnityEngine.InputLegacyModule.dll</HintPath>
        </Reference>
        <Reference Include="UnityEngine.PhysicsModule">
            <HintPath>$(CorlibPath)\UnityEngine.PhysicsModule.dll</HintPath>
        </Reference>
        <Reference Include="UnityEngine.UI">
            <HintPath>$(CorlibPath)\UnityEngine.UI.dll</HintPath>
        </Reference>
        <Reference Include="Unity.TextMeshPro">
            <HintPath>$(Gamepath)\valheim_Data\Managed\Unity.TextMeshPro.dll</HintPath>
        </Reference>
    </ItemGroup>
    <ItemGroup>
        <Compile Include="Patches\Util.cs"/>
        <Compile Include="Plugin.cs"/>
        <Compile Include="Patches\ABM.cs"/>
        <Compile Include="Patches\AEM.cs"/>
        <Compile Include="Patches\Compatibility\ModCompat.cs"/>
        <Compile Include="Patches\Compatibility\WardIsLove\CustomCheck.cs"/>
        <Compile Include="Patches\Compatibility\WardIsLove\WardIsLovePlugin.cs"/>
        <Compile Include="Patches\Compatibility\WardIsLove\WardMonoscript.cs"/>
        <Compile Include="Patches\Compatibility\WardIsLove\WardMonoscriptExt.cs"/>
        <Compile Include="Patches\Patches.cs"/>
        <Compile Include="Properties\AssemblyInfo.cs"/>
        <Compile Include="VersionHandshake.cs"/>
    </ItemGroup>
    <ItemGroup>
        <Content Include=".gitignore"/>
        <Content Include="environment.props"/>
        <Content Include="ILRepack.targets"/>
        <Content Include="Libs\ServerSync.dll"/>
        <Content Include="LICENSE.md"/>
        <Content Include="README.md"/>
        <Content Include="Thunderstore\CHANGELOG.md"/>
        <Content Include="Thunderstore\icon.png"/>
        <Content Include="Thunderstore\manifest.json"/>
        <Content Include="Thunderstore\README.md"/>
        <Content Include="UpdateManifest.ps1"/>
    </ItemGroup>
    <ItemGroup>
        <None Include="packages.config"/>
    </ItemGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
    <Import Project="packages\ILRepack.Lib.MSBuild.Task.2.0.18.2\build\ILRepack.Lib.MSBuild.Task.targets" Condition="Exists('packages\ILRepack.Lib.MSBuild.Task.2.0.18.2\build\ILRepack.Lib.MSBuild.Task.targets')"/>
    <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
        <PropertyGroup>
            <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
        </PropertyGroup>
        <Error Condition="!Exists('packages\ILRepack.Lib.MSBuild.Task.2.0.18.2\build\ILRepack.Lib.MSBuild.Task.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\ILRepack.Lib.MSBuild.Task.2.0.18.2\build\ILRepack.Lib.MSBuild.Task.targets'))"/>
    </Target>
    <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
         Other similar extension points exist, see Microsoft.Common.targets.
    <Target Name="BeforeBuild">
    </Target>
    <Target Name="AfterBuild">
    </Target>
    -->

    <UsingTask AssemblyFile="KeyManager.dll" TaskName="KeyManager.Seal" Condition="'$(AfterTargets)' == 'KeyManager'"/>
    <Target Name="KeyManager" AfterTargets="ILRepack" Condition="'$(AfterTargets)' == 'KeyManager'">
        <Seal DLL="$(TargetPath)"/>
    </Target>
    <Target Name="GetAssemblyVersion" AfterTargets="$(AfterTargets)">
        <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
            <Output TaskParameter="Assemblies" ItemName="Targets"/>
        </GetAssemblyIdentity>
        <ItemGroup>
            <AssemblyVersions Include="@(Targets->'%(Version)')"/>
        </ItemGroup>
        <Error Text="Failed to get Assembly Version." Condition="'%(AssemblyVersions.Identity)' == ''"/>
        <PropertyGroup>
            <AssemblyVersion>%(AssemblyVersions.Identity)</AssemblyVersion>
            <Major>$(AssemblyVersion.Split('.')[0])</Major>
            <Minor>$(AssemblyVersion.Split('.')[1])</Minor>
            <Patch>$(AssemblyVersion.Split('.')[2])</Patch>
            <PackageName>$(AssemblyName)_v$(Major).$(Minor).$(Patch)</PackageName>
            <PackageVersion>$(Major).$(Minor).$(Patch)</PackageVersion>
            <VersionString>$(Major).$(Minor).$(Patch)</VersionString>
            <PackageDir>$(ProjectDir)\Thunderstore\$(PackageName)</PackageDir>
        </PropertyGroup>
    </Target>
    <Target Name="CopyOutputDLL" AfterTargets="GetAssemblyVersion">
        <Error Text="DLL not found in target directory." Condition="!Exists('$(TargetDir)$(AssemblyName).dll')"/>
        <Copy SourceFiles="$(TargetDir)$(AssemblyName).dll" DestinationFolder="$(CopyOutputDLLPath)" OverwriteReadOnlyFiles="true"/>
        <Copy SourceFiles="$(TargetDir)$(AssemblyName).dll" DestinationFolder="$(CopyOutputDLLPath2)" OverwriteReadOnlyFiles="true" Condition="Exists('$(CopyOutputDLLPath2)')"/>
        <Copy SourceFiles="$(TargetDir)$(AssemblyName).dll" DestinationFolder="$(CopyOutputDLLPath3)" OverwriteReadOnlyFiles="true" Condition="Exists('$(CopyOutputDLLPath3)')"/>
    </Target>
    <Target Name="CopyReadMeToTSFolder" AfterTargets="CopyOutputDLL" Condition=" ('$(Configuration)|$(Platform)' == 'Release|AnyCPU') And ('$(OS)' == 'Windows_NT') ">
        <Warning Text="README.md not found in project directory. A default README.md file will be created for the Thunderstore package" Condition="!Exists('README.md')"/>
        <WriteLinesToFile File="README.md" Lines="Default Readme file from one of Azumatt's Templates. Replace this with actual content." Overwrite="false" Condition="!Exists('README.md')"/>
        <Copy SourceFiles="README.md" DestinationFolder="$(PackageDir)" OverwriteReadOnlyFiles="true"/>
        <Copy SourceFiles="README.md" DestinationFolder="$(ProjectDir)\Thunderstore" OverwriteReadOnlyFiles="true"/>
    </Target>
    <UsingTask TaskName="WriteManifestFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
            <ManifestFile ParameterType="System.String" Required="true"/>
            <AssemblyName ParameterType="System.String" Required="true"/>
        </ParameterGroup>
        <Task>
            <Code Type="Fragment" Language="cs">
                <![CDATA[
if (!System.IO.File.Exists(ManifestFile))
{
string content = @"{
  ""name"": """ + AssemblyName + @""",
  ""version_number"": ""1.0.0"",
  ""website_url"": ""https://discord.gg/pdHgy6Bsng"",
  ""description"": ""Your mod description goes here. Do not forget to replace the icon.png file with your own. Must be 256x256. Your Readme.md file will be copied from the one in your project folder."",
  ""dependencies"": [
    ""denikson-BepInExPack_Valheim-5.4.2105""
]
}";
System.IO.File.WriteAllText(ManifestFile, content);
}
]]>

            </Code>
        </Task>
    </UsingTask>
    <Target Name="UpdateManifestVersion" AfterTargets="CopyReadmeToTSFolder" Condition=" ('$(Configuration)|$(Platform)' == 'Release|AnyCPU') And ('$(OS)' == 'Windows_NT') ">
        <WriteManifestFile ManifestFile="$(ProjectDir)Thunderstore\manifest.json" AssemblyName="$(AssemblyName)"/>
        <ItemGroup>
            <ManifestFile Include="$(ProjectDir)Thunderstore\manifest.json"/>
        </ItemGroup>
        <Message Text="ManifestFile is @(ManifestFile)"/>
        <Message Text="Updating version number in Thunderstore\manifest.json" Condition="Exists(@(ManifestFile))"/>
        <Exec Command="powershell.exe -ExecutionPolicy Bypass -Command &quot;&amp; { &amp; '$(ProjectDir)UpdateManifest.ps1' -manifestFile '@(ManifestFile)' -versionString '$(PackageVersion)' }&quot;"/>
    </Target>
    <Target Name="MakeThunderstorePackage" AfterTargets="UpdateManifestVersion" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <Error Text="manifest.json not found in Thunderstore directory." Condition="!Exists('$(ProjectDir)\Thunderstore\manifest.json')"/>
        <Error Text="icon.png not found in Thunderstore directory. Please note that Thunderstore requires this image to be 256x256 in dimension" Condition="!Exists('$(ProjectDir)\Thunderstore\icon.png')"/>
        <ItemGroup>
            <PackageFiles Include="$(TargetDir)$(AssemblyName).dll" Condition="Exists('$(TargetDir)$(AssemblyName).dll')"/>
            <PackageFiles Include="$(ProjectDir)\Thunderstore\CHANGELOG.md" Condition="Exists('$(ProjectDir)\Thunderstore\CHANGELOG.md')"/>
            <PackageFiles Include="$(ProjectDir)\Thunderstore\manifest.json" Condition="Exists('$(ProjectDir)\Thunderstore\manifest.json')"/>
            <PackageFiles Include="$(ProjectDir)\Thunderstore\icon.png" Condition="Exists('$(ProjectDir)\Thunderstore\icon.png')"/>
        </ItemGroup>
        <MakeDir Directories="$(PackageDir)"/>
        <Copy SourceFiles="@(PackageFiles)" DestinationFolder="$(PackageDir)"/>
        <ZipDirectory SourceDirectory="$(PackageDir)" DestinationFile="Thunderstore\$(PackageName).zip" Overwrite="true"/>
        <RemoveDir Directories="$(PackageDir)"/>
    </Target>
<!--    <Target Name="MakeNexusPackage" AfterTargets="MakeThunderstorePackage" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <MakeDir Directories="$(ProjectDir)\Nexus"/>
        <ItemGroup>
            <NexusPackageFiles Include="$(TargetDir)$(AssemblyName).dll;"/>
        </ItemGroup>
        <MakeDir Directories="$(PackageDir)"/>
        <Copy SourceFiles="@(NexusPackageFiles)" DestinationFolder="$(PackageDir)"/>
        <ZipDirectory SourceDirectory="$(PackageDir)" DestinationFile="Nexus\$(PackageName).zip" Overwrite="true"/>
        <RemoveDir Directories="$(PackageDir)"/>
    </Target>-->

</Project>
