Skip to content

Commit

Permalink
Apply code fixups
Browse files Browse the repository at this point in the history
I think a regex still isn't working
  • Loading branch information
heinermann committed Aug 3, 2022
1 parent b62ead0 commit 5cd4209
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 7 deletions.
93 changes: 90 additions & 3 deletions ValheimExportHelper/FixCodeFiles.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,105 @@
using AssetRipper.Core.Logging;
using AssetRipper.Library;
using AssetRipper.Library.Exporters;
using System.Text.RegularExpressions;

namespace ValheimExportHelper
{
class FixCodeFiles : IPostExporter
{
private Ripper? Ripper { get; set; }
private string? MonoScriptDir { get; set; }

public void DoPostExport(Ripper ripper)
{
Logger.Info(LogCategory.Plugin, "[ValheimExportHelper] Fixing code files");

// TODO: Delete folder Microsoft.CSharp
// TODO: Delete folder Mono.Posix
// TODO (regex replace in all Assets/MonoScript/**/*.cs files)
Ripper = ripper;
MonoScriptDir = Path.Join(ripper.Settings.AssetsPath, "MonoScript");

DeleteStandardLibraries();
WholeCodebaseFixes();
OneOffCodeFixes();
}

private void TryDelete(string filename)
{
if (Directory.Exists(filename))
{
Directory.Delete(filename, true);
}
else if (File.Exists(filename))
{
File.Delete(filename);
}
}

private void DeleteStandardLibraries()
{
// Script: Decompiled
TryDelete(Path.Join(MonoScriptDir, "Microsoft.CSharp"));
TryDelete(Path.Join(MonoScriptDir, "Mono.Posix"));

// Script: Dll Export Without Renaming
TryDelete(Path.Join(MonoScriptDir, "Microsoft.CSharp.dll"));
TryDelete(Path.Join(MonoScriptDir, "Mono.Posix.dll"));
}

// TODO (regex replace in all Assets/MonoScript/**/*.cs files)

private void FixupFile(string filename)
{
string file = File.ReadAllText(filename);
file = file.Replace("StructLayout(0", "StructLayout(LayoutKind.Sequential");
file = file.Replace("StructLayout(2", "StructLayout(LayoutKind.Explicit");
File.WriteAllText(filename, file);
}

private void WholeCodebaseFixes()
{
foreach (var file in Directory.EnumerateFiles(MonoScriptDir, "*.cs", SearchOption.AllDirectories))
{
FixupFile(file);
}
}

private void FixUtils()
{
string filename = Path.Join(MonoScriptDir, "assembly_utils", "Utils.cs");
if (!File.Exists(filename)) return;

string file = File.ReadAllText(filename);
file = file.Replace(" CompressionLevel.Fastest", " System.IO.Compression.CompressionLevel.Fastest");
File.WriteAllText(filename, file);
}

private void FixSteamworks()
{
string basePath = Path.Join(MonoScriptDir, "assembly_steamworks", "Steamworks");

{
string filename = Path.Join(basePath, "Callback.cs");
string file = File.ReadAllText(filename);

// Match the declaration of m_Func, capture the spacing for the brace, and match everything between it and its closing brace (non-greedy everything: [\s\S]+?)
file = Regex.Replace(file, @"event DispatchDelegate m_Func\n(\s+)\{[\s\S]+?^\1\}", "event DispatchDelegate m_Func;", RegexOptions.Multiline);
File.WriteAllText(filename, file);
}

{
string filename = Path.Join(basePath, "CallResult.cs");
string file = File.ReadAllText(filename);

// Match the declaration of m_Func, capture the spacing for the brace, and match everything between it and its closing brace (non-greedy everything: [\s\S]+?)
file = Regex.Replace(file, @"event APIDispatchDelegate m_Func\n(\s+)\{[\s\S]+?^\1\}", "event APIDispatchDelegate m_Func;", RegexOptions.Multiline);
File.WriteAllText(filename, file);
}
}

private void OneOffCodeFixes()
{
FixUtils();
FixSteamworks();
}
}
}
16 changes: 12 additions & 4 deletions ValheimExportHelper/ValheimExportHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<PackageProjectUrl>https://github.com/heinermann/ValheimExportHelper</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/heinermann/ValheimExportHelper.git</RepositoryUrl>
<PackageTags>AssetRipper</PackageTags>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,6 +20,10 @@
</ItemGroup>

<ItemGroup>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="Resources\Editor.cs" />
</ItemGroup>

Expand All @@ -25,19 +33,19 @@

<ItemGroup>
<Reference Include="AssetRipper.Fundamentals">
<HintPath>..\..\..\Downloads\AssetRipper-0.2.1.0\Bins\AssetRipperLibrary\ReleaseWindows\AssetRipper.Fundamentals.dll</HintPath>
<HintPath>..\..\Downloads\AssetRipper-0.2.1.0\Bins\AssetRipperLibrary\ReleaseWindows\AssetRipper.Fundamentals.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AssetRipperCommon">
<HintPath>..\..\..\Downloads\AssetRipper-0.2.1.0\Bins\AssetRipperLibrary\ReleaseWindows\AssetRipperCommon.dll</HintPath>
<HintPath>..\..\Downloads\AssetRipper-0.2.1.0\Bins\AssetRipperLibrary\ReleaseWindows\AssetRipperCommon.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AssetRipperCore">
<HintPath>..\..\..\Downloads\AssetRipper-0.2.1.0\Bins\AssetRipperLibrary\ReleaseWindows\AssetRipperCore.dll</HintPath>
<HintPath>..\..\Downloads\AssetRipper-0.2.1.0\Bins\AssetRipperLibrary\ReleaseWindows\AssetRipperCore.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="AssetRipperLibrary">
<HintPath>..\..\..\Downloads\AssetRipper-0.2.1.0\Bins\AssetRipperLibrary\ReleaseWindows\AssetRipperLibrary.dll</HintPath>
<HintPath>..\..\Downloads\AssetRipper-0.2.1.0\Bins\AssetRipperLibrary\ReleaseWindows\AssetRipperLibrary.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
Expand Down

0 comments on commit 5cd4209

Please sign in to comment.