Skip to content

Commit ebac263

Browse files
committed
DRY
- Added a new shared lib for shared utils. - Moved FakeCallerAttributes into Utils.csproj - Moved PathCombine into Utils.csproj and adjusted it a little. Can't make it an extension method because static classes "Path" are not supported. - Moved CopyStream into Utils.csproj, removed Exception handling since that should be done by the client and made it an extension method.
1 parent 30cce28 commit ebac263

13 files changed

+177
-99
lines changed

PatchLoader.sln

+15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29911.84
5+
MinimumVisualStudioVersion = 10.0.40219.1
36
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchLoaderMod", "PatchLoaderMod\PatchLoaderMod.csproj", "{00C4FDF9-637B-4F00-BEC7-B8E9F0D06E4A}"
47
EndProject
58
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Patch.API", "Patch.API\Patch.API.csproj", "{C5C7F3DD-F203-4510-8525-7EE6FB0E6124}"
69
EndProject
710
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchLoader", "PatchLoader\PatchLoader.csproj", "{FD6438F0-AF7A-4196-8B28-F3B578489AA0}"
811
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "Utils\Utils.csproj", "{6C49A64B-A8DA-4EB3-AB71-A9D77193685A}"
13+
EndProject
914
Global
1015
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1116
Debug|Any CPU = Debug|Any CPU
@@ -24,5 +29,15 @@ Global
2429
{FD6438F0-AF7A-4196-8B28-F3B578489AA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
2530
{FD6438F0-AF7A-4196-8B28-F3B578489AA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
2631
{FD6438F0-AF7A-4196-8B28-F3B578489AA0}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{6C49A64B-A8DA-4EB3-AB71-A9D77193685A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{6C49A64B-A8DA-4EB3-AB71-A9D77193685A}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{6C49A64B-A8DA-4EB3-AB71-A9D77193685A}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{6C49A64B-A8DA-4EB3-AB71-A9D77193685A}.Release|Any CPU.Build.0 = Release|Any CPU
36+
EndGlobalSection
37+
GlobalSection(SolutionProperties) = preSolution
38+
HideSolutionNode = FALSE
39+
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {0BAE538F-5B8C-4865-9235-8CC1A2F671BB}
2742
EndGlobalSection
2843
EndGlobal

PatchLoader/PatchLoader.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
<Compile Include="PatchScanner.cs" />
4646
<Compile Include="Properties\AssemblyInfo.cs" />
4747
<Compile Include="Utils\ConfigManager.cs" />
48-
<Compile Include="Utils\FakeCallerAttributes.cs" />
4948
<Compile Include="Utils\Log.cs" />
5049
<Compile Include="Utils\Paths.cs" />
5150
</ItemGroup>
@@ -54,6 +53,11 @@
5453
<Project>{c5c7f3dd-f203-4510-8525-7ee6fb0e6124}</Project>
5554
<Name>Patch.API</Name>
5655
</ProjectReference>
56+
<ProjectReference Include="..\Utils\Utils.csproj">
57+
<Project>{6C49A64B-A8DA-4EB3-AB71-A9D77193685A}</Project>
58+
<Name>Utils</Name>
59+
<Private>True</Private>
60+
</ProjectReference>
5761
</ItemGroup>
5862
<ItemGroup>
5963
<PackageReference Include="Mono.Cecil">

PatchLoader/Utils/Paths.cs

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.IO;
3-
using System.Linq;
3+
using Utils;
44

55
namespace PatchLoader.Utils {
66
public static class Paths {
@@ -36,26 +36,13 @@ internal static void LoadVars() {
3636

3737
ManagedFolderPath = Environment.GetEnvironmentVariable("DOORSTOP_MANAGED_FOLDER_DIR");
3838

39-
ModsPath = PathCombine(WorkingPath, "Files", "Mods");
39+
ModsPath = PathExtensions.Combine(WorkingPath, "Files", "Mods");
4040

41-
AppDataModsPath = PathCombine(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)).FullName, "Colossal Order", "Cities_Skylines", "Addons", "Mods");
41+
AppDataModsPath = PathExtensions.Combine(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)).FullName, "Colossal Order", "Cities_Skylines", "Addons", "Mods");
4242
}
4343

4444
public static void LogPaths() {
4545
Log._Debug($"\nLoaderPath: {LoaderPath}\nWorkingPath: {WorkingPath}\nManagedFolderPath: {ManagedFolderPath}\nModsPath: {ModsPath}\nAppDataModsPath: {AppDataModsPath}");
4646
}
47-
48-
/// <summary>
49-
/// Non performant Path.Combine(params string[] paths) implementation for .NET 3.5 and lower, to be able to handle multiple values.
50-
/// Do not use it in the gameloop, or see the performance crumble, and the few fps you had left burn.
51-
/// </summary>
52-
/// <param name="paths">Paths to be combined.</param>
53-
/// <returns>Returns the full path.</returns>
54-
public static string PathCombine(params string[] paths)
55-
{
56-
return paths
57-
.Where(p => p != null)
58-
.Aggregate((a, b) => Path.Combine(a, b));
59-
}
6047
}
6148
}

PatchLoaderMod/DoorstopManager.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Reflection;
55
using System.Text;
6+
using Utils;
67

78
namespace PatchLoaderMod
89
{
@@ -70,11 +71,8 @@ private void InstallWinHttpFile()
7071
using (Stream input = executingAssembly.GetManifestResourceStream(resourcePath))
7172
using (Stream output = File.Create("winhttp.dll"))
7273
{
73-
var success = FileTools.CopyStream(input, output);
74-
if (!success)
75-
{
76-
throw new Exception("FileTools.CopyStream() was not successful.");
77-
}
74+
Log._Debug("Copying stream.");
75+
input.CopyStream(output);
7876
}
7977
}
8078

PatchLoaderMod/PatchLoaderMod.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using PatchLoaderMod.Utils;
55
using System.Reflection;
66
using UnityEngine;
7+
using Utils;
78

89
namespace PatchLoaderMod
910
{
@@ -17,7 +18,7 @@ public class PatchLoaderMod : LoadingExtensionBase, IUserMod {
1718
public void OnEnabled() {
1819
_pluginInfo = PluginManager.instance.FindPluginInfo(Assembly.GetExecutingAssembly());
1920

20-
var expectedTargetAssemblyPath = FileTools.PathCombine(
21+
var expectedTargetAssemblyPath = PathExtensions.Combine(
2122
_pluginInfo.modPath,
2223
"PatchLoader",
2324
"PatchLoader.dll"

PatchLoaderMod/PatchLoaderMod.csproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@
5858
<Compile Include="PatchLoaderMod.cs" />
5959
<Compile Include="Properties\AssemblyInfo.cs" />
6060
<Compile Include="SettingsUi.cs" />
61-
<Compile Include="Utils\FakeCallerAttributes.cs" />
62-
<Compile Include="Utils\FileTools.cs" />
6361
<Compile Include="Utils\Log.cs" />
6462
</ItemGroup>
6563
<ItemGroup>
6664
<EmbeddedResource Include="Resources\winhttp.dll" />
6765
</ItemGroup>
66+
<ItemGroup>
67+
<ProjectReference Include="..\Utils\Utils.csproj">
68+
<Project>{6C49A64B-A8DA-4EB3-AB71-A9D77193685A}</Project>
69+
<Name>Utils</Name>
70+
</ProjectReference>
71+
</ItemGroup>
6872
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6973
<PropertyGroup>
7074
<PostBuildEvent>

PatchLoaderMod/Utils/FakeCallerAttributes.cs

-19
This file was deleted.

PatchLoaderMod/Utils/FileTools.cs

-50
This file was deleted.

PatchLoader/Utils/FakeCallerAttributes.cs Utils/FakeCallerAttributes.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
// Set of attributes that are used by compiler to generate info about caller
33

44
// ReSharper disable once CheckNamespace
5-
namespace System.Runtime.CompilerServices {
6-
5+
namespace System.Runtime.CompilerServices
6+
{
77
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
8-
public class CallerMemberNameAttribute : Attribute {
8+
public class CallerMemberNameAttribute : Attribute
9+
{
910
}
1011

1112
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
12-
public class CallerFilePathAttribute : Attribute {
13+
public class CallerFilePathAttribute : Attribute
14+
{
1315
}
1416

1517
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
16-
public class CallerLineNumberAttribute : Attribute {
18+
public class CallerLineNumberAttribute : Attribute
19+
{
1720
}
1821
}

Utils/PathExtensions.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace Utils
6+
{
7+
public static class PathExtensions
8+
{
9+
/// <summary>
10+
/// Non performant Path.Combine(params string[] paths) implementation for .NET 3.5 and lower, to be able to handle multiple values.
11+
/// Do not use it in the gameloop, or see the performance crumble, and the few fps you had left burn.
12+
/// </summary>
13+
/// <param name="paths">Paths to be combined.</param>
14+
/// <returns>Returns the full path.</returns>
15+
public static string Combine(params string[] paths)
16+
{
17+
if (paths is null)
18+
{
19+
throw new ArgumentNullException(nameof(paths));
20+
}
21+
22+
return paths
23+
.Where(p => p != null)
24+
.Aggregate((a, b) => Path.Combine(a, b));
25+
}
26+
}
27+
}

Utils/Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Utils")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Utils")]
13+
[assembly: AssemblyCopyright("Copyright © 2020")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("6c49a64b-a8da-4eb3-ab71-a9d77193685a")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Utils/StreamExtensions.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.IO;
2+
3+
namespace Utils
4+
{
5+
public static class StreamExtensions
6+
{
7+
private const int _defaultCopyBufferSize = 81920; //see https://referencesource.microsoft.com/#mscorlib/system/io/stream.cs,2a0f078c2e0c0aa8
8+
9+
/// <summary>
10+
/// Copies an input stream to an output stream.
11+
/// </summary>
12+
/// <param name="input">Input stream.</param>
13+
/// <param name="output">Output stream.</param>
14+
public static void CopyStream(this Stream input, Stream output)
15+
{
16+
byte[] buffer = new byte[_defaultCopyBufferSize];
17+
int len;
18+
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
19+
{
20+
output.Write(buffer, 0, len);
21+
}
22+
}
23+
}
24+
}

Utils/Utils.csproj

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{6C49A64B-A8DA-4EB3-AB71-A9D77193685A}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Utils</RootNamespace>
11+
<AssemblyName>Utils</AssemblyName>
12+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="System.Data" />
39+
<Reference Include="System.Xml" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="FakeCallerAttributes.cs" />
43+
<Compile Include="PathExtensions.cs" />
44+
<Compile Include="Properties\AssemblyInfo.cs" />
45+
<Compile Include="StreamExtensions.cs" />
46+
</ItemGroup>
47+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
48+
</Project>

0 commit comments

Comments
 (0)