-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from mistzzt/fix-platform
Add Platform modification
- Loading branch information
Showing
8 changed files
with
116 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
TShock.Modifications.Platform/Callbacks/PlatformConstructorCallback.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} | ||
} |
9 changes: 4 additions & 5 deletions
9
...s.UnicodeInput/Properties/AssemblyInfo.cs → ...tions.Platform/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
28 changes: 0 additions & 28 deletions
28
TShock.Modifications.UnicodeInput/Callbacks/InitializeConsoleOutputCallback.cs
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
TShock.Modifications.UnicodeInput/UnicodeInputModification.cs
This file was deleted.
Oops, something went wrong.