Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Platform modification #134

Merged
merged 4 commits into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TShock.4.OTAPI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documents", "Documents", "{
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TShock.Modifications.SSC", "TShock.Modifications.SSC\TShock.Modifications.SSC.csproj", "{23EA4516-79FF-4DE4-AE92-3F3D69A906CA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TShock.Modifications.UnicodeInput", "TShock.Modifications.UnicodeInput\TShock.Modifications.UnicodeInput.csproj", "{AC441C8A-EA6B-416F-9961-FA944131AFB6}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TShock.Modifications.Platform", "TShock.Modifications.Platform\TShock.Modifications.Platform.csproj", "{AC441C8A-EA6B-416F-9961-FA944131AFB6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Runtime.InteropServices;

namespace Mintaka.Modifications.Platform.Callbacks
{
internal class PlatformConstructorCallback
{
internal static void ChangePlatform(ref ReLogic.OS.Platform platform)
{
Type type;

switch (Environment.OSVersion.Platform)
{
case PlatformID.Unix:
type = typeof(ReLogic.OS.Platform).Assembly.GetType(IsRunningOnMac() ? "ReLogic.OS.OsxPlatform" : "ReLogic.OS.LinuxPlatform");
break;
case PlatformID.Win32NT:
type = typeof(ReLogic.OS.Platform).Assembly.GetType("ReLogic.OS.WindowsPlatform");
break;
default:
throw new NotSupportedException();
}

platform = (ReLogic.OS.Platform) Activator.CreateInstance(type);
}

[DllImport("libc")]
private static extern int uname(IntPtr buf);

private static bool IsRunningOnMac()
{
var buf = IntPtr.Zero;
try
{
buf = Marshal.AllocHGlobal(8192);
// This is a hacktastic way of getting sysname from uname ()
if (uname(buf) == 0)
{
string os = Marshal.PtrToStringAnsi(buf);
if (os == "Darwin")
{
return true;
}
}
}
catch
{
// ignored
}
finally
{
if (buf != IntPtr.Zero)
{
Marshal.FreeHGlobal(buf);
}
}
return false;
}
}
}
47 changes: 47 additions & 0 deletions TShock.Modifications.Platform/PlatformModification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil.Cil;
using OTAPI.Patcher.Engine.Modification;

namespace Mintaka.Modifications.Platform
{
/// <summary>
/// This modification will make OTAPI initialized with right platform rather than Windows platform.
/// This should fix different world path OTAPI used on macOS
/// In addition, the console input will also be fixed.
/// </summary>
public class PlatformModification : ModificationBase
{
public override IEnumerable<string> AssemblyTargets => new[]
{
"OTAPI, Version=1.3.5.3, Culture=neutral, PublicKeyToken=null"
};

public override string Description => "Enforcing OTAPI to load right platform...";

public override void Run()
{
ReLogic.OS.Platform temp = null;
var changePlatformMethodDefinition = SourceDefinition.MainModule.Import(Method(() => Callbacks.PlatformConstructorCallback.ChangePlatform(ref temp)));

var field = Field(() => ReLogic.OS.Platform.Current);
var cctor = Type<ReLogic.OS.Platform>().Methods.Single(m => m.Name == ".cctor");
var instructions = cctor.Body.Instructions;

if (instructions?.Count != 3 || instructions[1].OpCode != OpCodes.Stsfld || instructions[1].Operand != field)
{
throw new NotSupportedException("Could not patch Platform..cctor()");
}

var processor = cctor.Body.GetILProcessor();

processor.Body.Instructions.Clear();

processor.Append(Instruction.Create(OpCodes.Ldsflda, field));
processor.Append(Instruction.Create(OpCodes.Call, changePlatformMethodDefinition));
processor.Append(Instruction.Create(OpCodes.Ret));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Mintaka.Modifications.UnicodeInput")]
[assembly: AssemblyDescription("Modifies the TerrariaServer console I/O to make it compatible with macOS/Linux")]
[assembly: AssemblyTitle("Mintaka.Modifications.Platform")]
[assembly: AssemblyDescription("Enforcing OTAPI to load right platform")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Mintaka.Modifications.UnicodeInput")]
[assembly: AssemblyCopyright("Copyright © Tyler Watson <tyler@tw.id.au> 2017")]
[assembly: AssemblyProduct("Mintaka.Modifications.Platform")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TShock.Modifications.UnicodeInput</RootNamespace>
<AssemblyName>TShock.Modifications.UnicodeInput</AssemblyName>
<RootNamespace>Mintaka.Modifications.Platform</RootNamespace>
<AssemblyName>TShock.Modifications.Platform</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<LangVersion>6</LangVersion>
Expand Down Expand Up @@ -64,9 +64,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Callbacks\InitializeConsoleOutputCallback.cs" />
<Compile Include="Callbacks\PlatformConstructorCallback.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnicodeInputModification.cs" />
<Compile Include="PlatformModification.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

This file was deleted.

42 changes: 0 additions & 42 deletions TShock.Modifications.UnicodeInput/UnicodeInputModification.cs

This file was deleted.