kianzarrin / LoadOrder

12 stars 6 forks source link

print file and line numbers #2

Closed kianzarrin closed 3 years ago

kianzarrin commented 4 years ago

[moved to wiki]

when debugging CS mods it would be useful to get file and line number in your exceptions stack trace like this:

Exception: test kian exception
  at NetworkDetective.Test.Factorial (Int32 n) [0x00025] in C:\Users\dell\source\repos\NetworkDetective\NetowrkDetective\NetworkDetectiveMod.cs:35 
  at NetworkDetective.Test.Factorial (Int32 n) [0x0000a] in C:\Users\dell\source\repos\NetworkDetective\NetowrkDetective\NetworkDetectiveMod.cs:32 
  at NetworkDetective.Test.Factorial (Int32 n) [0x0000a] in C:\Users\dell\source\repos\NetworkDetective\NetowrkDetective\NetworkDetectiveMod.cs:32 
  at NetworkDetective.Test.Factorial (Int32 n) [0x0000a] in C:\Users\dell\source\repos\NetworkDetective\NetowrkDetective\NetworkDetectiveMod.cs:32 
  at NetworkDetective.NetworkDetectiveMod.OnEnabled () [0x00001] in C:\Users\dell\source\repos\NetworkDetective\NetowrkDetective\NetworkDetectiveMod.cs:50 

Follow these steps to get file and line number (I am working to reduce these steps).

Use Nuget Package"Mono.Unofficial.pdb2mdb" to generate MDB file:

mono cannot use pdb files. So we need to convert it to mdb file. Not every version of pdb2mdb.exe works for us. So follow these steps to convert pdb to mdb

use the following Nuget package:

<ItemGroup>
    <PackageReference Include="Mono.Unofficial.pdb2mdb" Version="4.2.3.4" />
</ItemGroup>

generate pdb-only debug symbols ( optionally use the path map to display relative path instead of absolute path):

<PropertyGroup>
    <DebugType>pdbonly</DebugType>
    <PathMap>$(MSBuildProjectDirectory)\=$(ProjectName)\</PathMap>
</PropertyGroup>

convert pdb2mdb in your post build script:

  <Target Name="DeployToModDirectory" AfterTargets="Build">
    <Delete Files="$(TargetPath).mdb" />
    <Exec Command=' "$(PkgMono_Unofficial_pdb2mdb)\tools\pdb2mdb.exe\" "$(TargetPath)" ' />   
    <Copy SourceFiles="$(TargetPath).mdb" DestinationFolder="$(DeployDir)" />
    ...

Use LOM mod:

Load order tool V0.4.4+ ( github steam ) is able to launch the game in debug mode (uses debug mono and patches CO to load symbols). image

kianzarrin commented 4 years ago

TODO:

kianzarrin commented 4 years ago

The CO patch is:

+ Assembly assembly;
+ string symPath = dllPath + ".mdb";
+ if(File.Exists(symPath)) {
+   CODebugBase<InternalLogChannel>.Log(InternalLogChannel.Mods, "Loading " + dllPath + "\nSymbols " + symPath);
+   assembly = Assembly.Load(File.ReadAllBytes(dllPath), File.ReadAllBytes(symPath));
+ } else {
    CODebugBase<InternalLogChannel>.Log(InternalLogChannel.Mods, "Loading " + dllPath);
- Assembly assembly = Assembly.Load(File.ReadAllBytes(dllPath));
+   assembly = Assembly.Load(File.ReadAllBytes(dllPath));
+ }
kianzarrin commented 4 years ago

Example post-build scripts for pdb2mdb:

New SDK format

  <Target Name="DeployToModDirectory" AfterTargets="Build">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="Targets" />
    </GetAssemblyIdentity>
    <PropertyGroup>
      <DeployDir>$(LOCALAPPDATA)\Colossal Order\Cities_Skylines\Addons\Mods\$(ProjectName)\</DeployDir>
      <UnityPath>$(MSBuildExtensionsPath64)\..\Unity\</UnityPath>
      <UnityPath Condition="! Exists ('$(UnityPath)')">..\Unity\</UnityPath>
      <MonoDir>$(UnityPath)Editor\Data\MonoBleedingEdge\</MonoDir>
    </PropertyGroup>
    <ItemGroup>
      <AuxilaryFiles Include="$(TargetDir)**/*.*" Exclude="$(TargetPath);$(TargetDir)*.pdb" />
      <VersionNumber Include="@(Targets->'%(Version)')" />
    </ItemGroup>
    <Delete Files="$(TargetPath).mdb" />
    <Exec Command="&quot;$(MonoDir)bin\mono.exe&quot; &quot;$(MonoDir)lib\mono\4.5\pdb2mdb.exe&quot; &quot;$(TargetPath)&quot;" />
    <Copy SourceFiles="$(TargetPath).mdb" DestinationFolder="$(DeployDir)" />
    <Copy SourceFiles="@(AuxilaryFiles)" DestinationFolder="$(DeployDir)" />
    <!-- <Message Importance="high" Text="Deleting $(DeployDir)$(TargetFileName)" /> -->
    <Delete Files="$(DeployDir)$(TargetFileName)" />
    <Copy SourceFiles="$(TargetPath)" DestinationFolder="$(DeployDir)" />
    <Message Importance="high" Text="AssemblyVersion= @(VersionNumber)" />
  </Target>

Old format

mkdir "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\$(ProjectName)"
del "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\$(ProjectName)"\$(TargetFileName)"
xcopy /y "$(TargetPath)" "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\$(ProjectName)"
xcopy /y "$(TargetDir)MoveItIntegration.dll" "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\$(ProjectName)"

set "UNITY_PATH=$(MSBuildExtensionsPath64)\..\Unity"
IF not EXIST "%UNITY_PATH% " set "UNITY_PATH=$(SolutionDir)Unity"
set "MONODIR=%UNITY_PATH%\Editor\Data\MonoBleedingEdge\"
echo MONODIR is %MONODIR%
"%MONODIR%\bin\mono.exe" "%MONODIR%\lib\mono\4.5\pdb2mdb.exe" "$(TargetPath)"
IF EXIST "$(TargetPath).mdb" xcopy /y "$(TargetPath).mdb" "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\$(ProjectName)"
kianzarrin commented 2 years ago

krzychu gave me a new mono.dll that is malware free. I updated my LOM tool.