Skip to content

Commit

Permalink
Merge pull request #56 from Haelle/linux-support
Browse files Browse the repository at this point in the history
fix: Fixed Linux systems not operating properly with the tileset exporter command
  • Loading branch information
Cammin authored Jun 10, 2024
2 parents f6d025a + d28f5e4 commit 0a61fb3
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ internal sealed class LDtkEditorCommandUpdater
{
public string ProjectPath;
public string ProjectName;

public string ExePath;
public string Arg;

public string Command;

public LDtkEditorCommandUpdater(string projectPath)
Expand All @@ -24,50 +24,51 @@ public LDtkEditorCommandUpdater(string projectPath)
ExePath = GetExecutablePath();
Arg = $"\\\"{ProjectName}\\\"";
Command += $"{ExePath} \"{ProjectName}\"";
#if UNITY_EDITOR_OSX

#if UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX
Command += $" $1";
#endif
}

private string GetExecutablePath()
{
string fromPath = LDtkPathUtility.AssetsPathToAbsolutePath(ProjectPath);


//if mac/Linux, we're launching a different file that's meant to launch the exe
#if UNITY_EDITOR_OSX
//if mac, we're launching a different file that's meant to launch the exe
string appPath = LDtkTilesetExporterUtil.PathToMacSh();
#elif UNITY_EDITOR_LINUX
string appPath = LDtkTilesetExporterUtil.PathToLinuxSh();
#else
string appPath = LDtkTilesetExporterUtil.PathToExe();
#endif

var commandContent = LDtkPathUtility.GetRelativePath(fromPath, appPath);
//backslashes break deserialization
commandContent = LDtkPathUtility.CleanPathSlashes(commandContent);

//Debug.Log($"fromPath {fromPath}");
//Debug.Log($"appPath {appPath}");
//Debug.Log($"relPath {relPath}");

return commandContent;
}

public void TryDrawFixButton(LdtkJson data)
{
//if it defined no tileset defs, it's fine
if (data.Defs.Tilesets.IsNullOrEmpty())
{
return;
}

if (!IsInstalled(out var installReason))
{
using (new EditorGUILayout.HorizontalScope())
{
EditorGUIUtility.SetIconSize(Vector2.one * 32);
EditorGUILayout.HelpBox($"The importer requires an app installed to the Library folder. This is a one time process.\nReason: {installReason}", MessageType.Error);

EditorGUIUtility.SetIconSize(new Vector2(16, 16));
GUIContent installContent = new GUIContent()
{
Expand All @@ -90,17 +91,17 @@ public void TryDrawFixButton(LdtkJson data)
LDtkEditorGUIUtility.DrawDivider();
return;
}

if (HasCustomCommand(data, out var reason))
{
return;
}

using (new EditorGUILayout.HorizontalScope())
{
EditorGUIUtility.SetIconSize(Vector2.one * 32);
EditorGUILayout.HelpBox($"This project needs a command that should run this after saving:\n{Command}\nReason: {reason}", MessageType.Error);

using (new EditorGUILayout.VerticalScope(GUILayout.Width(50)))
{
EditorGUIUtility.SetIconSize(new Vector2(16, 14));
Expand Down Expand Up @@ -143,7 +144,7 @@ public void TryDrawFixButton(LdtkJson data)

GUIUtility.ExitGUI();
}

EditorGUIUtility.SetIconSize(new Vector2(11, 13));
GUIContent copyContent = new GUIContent()
{
Expand All @@ -156,7 +157,7 @@ public void TryDrawFixButton(LdtkJson data)
}
}
}

EditorGUILayout.Space();
LDtkEditorGUIUtility.DrawDivider();
}
Expand All @@ -181,17 +182,17 @@ bool IsBlockedFromAddingCommand()
{
return false;
}
int result = EditorUtility.DisplayDialogComplex(ProjectName,
"Didn't add command.\n" +
"Close all LDtk processes, and try again.\n" +
"\n" + "Alternatively, you may add the command manually:\n" +
"- Copy the path to the clipboard\n" +
"- Go to LDtk's project settings\n" +
"- Create a new command\n" +
"- Set the timing to \"Run after saving\"\n" +
"- Paste the following path from your clipboard:\n" +
$"\"{Command}\"\n",

int result = EditorUtility.DisplayDialogComplex(ProjectName,
"Didn't add command.\n" +
"Close all LDtk processes, and try again.\n" +
"\n" + "Alternatively, you may add the command manually:\n" +
"- Copy the path to the clipboard\n" +
"- Go to LDtk's project settings\n" +
"- Create a new command\n" +
"- Set the timing to \"Run after saving\"\n" +
"- Paste the following path from your clipboard:\n" +
$"\"{Command}\"\n",
"Try Again", "Close", "Copy to Clipboard");
switch (result)
{
Expand All @@ -205,7 +206,7 @@ bool IsBlockedFromAddingCommand()
}
}
}

if (ModifyProjectWithCommand(ProjectPath, ExePath, Arg))
{
EditorUtility.DisplayDialog("Modified", $"Modified\n\"{ProjectName}\"\nwith the custom command.\nNow open the project and save!", "Ok");
Expand All @@ -223,11 +224,11 @@ public static bool ModifyProjectWithCommand(string projectPath, string exeRelPat
{
return false;
}

const string before = @"""customCommands"": [],";
const string after = @"""customCommands"": [{ ""command"": ""_PATH"", ""when"": ""AfterSave"" }],";
string insert = after.Replace("_PATH", $"{exeRelPath} {arg}");

string[] lines = File.ReadAllLines(projectPath);
bool found = false;
for (int i = 0; i < lines.Length; i++)
Expand Down Expand Up @@ -257,25 +258,25 @@ public bool IsInstalled(out string reason)
reason = $"The app's version ({old}) does not match the required one ({required})";
return false;
}

reason = null;
return true;
}

reason = $"The app doesn't exist";
return false;
}

public bool HasCustomCommand(LdtkJson data, out string reason)
{
LdtkCustomCommand[] commands = data.CustomCommands;

if (commands == null)
{
reason = "customCommands was null. Old LDtk version?";
return false;
}

foreach (LdtkCustomCommand command in commands)
{
if (command.Command == Command)
Expand All @@ -285,13 +286,13 @@ public bool HasCustomCommand(LdtkJson data, out string reason)
reason = "The command exists, but the timing is not set to \"Run after saving\"";
return false;
}

//ensure that there is a 2nd arg.
string[] split = Regex.Matches(command.Command, @"[\""].+?[\""]|[^ ]+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();

if (split.Length != 2 || split[1] != $"\"{ProjectName}\"")
{
reason = $"The command exists, but doesn't have a single parameter of the project name." +
Expand Down
44 changes: 30 additions & 14 deletions Assets/LDtkUnity/Editor/Utility/LDtkTilesetExporterUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;

Expand All @@ -11,40 +12,49 @@ internal static class LDtkTilesetExporterUtil
private const string EXPORT_ZIP = "LDtkTilesetExporter.zip";
private const string EXPORT_APP = "ExportTilesetDefinition.exe";
private const string MAC_APP = "ExportTilesetDefinitionMac.sh";
private const string LINUX_APP = "ExportTilesetDefinitionLinux.sh";

//[MenuItem("UnzipToProject/Unzip")]
public static void UnzipToLibrary()
{
string pathToZip = PathToZip();
string destDir = PathToLibraryDir();

LDtkPathUtility.TryCreateDirectory(destDir);
if (Directory.Exists(destDir))
{
DirectoryInfo di = new DirectoryInfo(destDir);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
file.Delete();
}
}

Debug.Assert(File.Exists(pathToZip), "File.Exists(pathToZip)");
Debug.Assert(Directory.Exists(destDir), "Directory.Exists(destDir)");

ZipUtil.Extract(pathToZip, destDir);
#if UNITY_EDITOR_OSX

#if UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX
string pathToExe = PathToExe();

//if mac, we need to create a shell script to run the exe
//if mac/Linux, we need to create a shell script to run the exe
#if UNITY_EDITOR_OSX
string shPath = Path.Combine(destDir, MAC_APP);
string shContent = $"#!/bin/sh\n /Library/Frameworks/Mono.framework/Versions/Current/Commands/mono {pathToExe} $1";
#elif UNITY_EDITOR_LINUX
string shPath = Path.Combine(destDir, LINUX_APP);
var unityPath = Path.GetDirectoryName(EditorApplication.applicationPath);
var monoPath = Path.Combine(unityPath, "Data", "MonoBleedingEdge", "bin", "mono");
string shContent = $"#!/bin/sh\n {monoPath} {pathToExe} $1";
#endif

File.WriteAllText(shPath, shContent);
//on mac, the app needs some permission. Use "sudo chmod +x"

//on mac/Linux, the app needs some permission. Use "sudo chmod +x"
Process.Start("/bin/bash", $"-c \" chmod +x {shPath}\" ");
#endif

LDtkDebug.Log($"Extracted the tileset export app to \"{destDir}\"");
}

Expand All @@ -53,26 +63,26 @@ private static void CheckAppVersion()
{
Debug.Log($"app version up to date? {GetAppUpToDate()}");
}
//[MenuItem("UnzipToProject/LogPathToExe")]
public static void LogPathToExe()
{
Debug.Log(PathToExe());
}*/

public static string PathToLibraryDir()
{
string destDir = Application.dataPath;
destDir = Path.Combine(destDir, "..", "Library", "LDtkTilesetExporter");
destDir = Path.GetFullPath(destDir);
return destDir;
}

public static string PathToZip()
{
string packagePath = Path.Combine(LDtkInternalUtility.ASSETS, EXPORT_ZIP);
if (File.Exists(packagePath)) return packagePath;

string assetsPath = Path.Combine(LDtkInternalUtility.PACKAGES, EXPORT_ZIP);
if (File.Exists(assetsPath)) return assetsPath;

Expand All @@ -88,7 +98,13 @@ public static string PathToMacSh()
string exePath = Path.Combine(PathToLibraryDir(), MAC_APP);
return exePath;
}


public static string PathToLinuxSh()
{
string exePath = Path.Combine(PathToLibraryDir(), LINUX_APP);
return exePath;
}

public static bool GetAppUpToDate(out Version version, out Version requiredVersion)
{
FileVersionInfo info = FileVersionInfo.GetVersionInfo(PathToExe());
Expand Down
1 change: 1 addition & 0 deletions Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"com.unity.ide.visualstudio": "2.0.22",
"com.unity.test-framework": "1.4.4",
"com.unity.testtools.codecoverage": "1.2.5",
"com.unity.toolchain.linux-x86_64": "2.0.9",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
"com.unity.modules.animation": "1.0.0",
Expand Down
Loading

0 comments on commit 0a61fb3

Please sign in to comment.