Skip to content

Commit

Permalink
setup the ui to unzip the app into the library folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed Aug 14, 2023
1 parent 3d30635 commit 76b3c55
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ public LDtkEditorCommandUpdater(string projectPath)
{
ProjectPath = projectPath;
RelPath = GetPath();
}

private string GetPath()
{
string fromPath = LDtkPathUtility.AssetsPathToAbsolutePath(ProjectPath);
string appPath = LDtkTilesetExporterUtil.PathToExe();

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


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

public void TryDrawFixButton(LdtkJson data)
Expand All @@ -33,6 +46,36 @@ public void TryDrawFixButton(LdtkJson data)
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()
{
text = "Install",
tooltip = "This will extract from a zip file in this package into the Library folder.",
image = EditorGUIUtility.IconContent("Add-Available").image
};

using (new EditorGUILayout.VerticalScope(GUILayout.Width(75)))
{
if (GUILayout.Button(installContent, GUILayout.ExpandHeight(true)))
{
LDtkTilesetExporterUtil.UnzipToLibrary();
AssetDatabase.Refresh();
}
}
}

EditorGUILayout.Space();
LDtkEditorGUIUtility.DrawDivider();
return;
}

using (new EditorGUILayout.HorizontalScope())
{
EditorGUIUtility.SetIconSize(Vector2.one * 32);
Expand Down Expand Up @@ -96,12 +139,11 @@ public void TryDrawFixButton(LdtkJson data)
{
ToClipboard();
}
if (GUILayout.Button("install", GUILayout.ExpandHeight(true)))
{

}
}
}

EditorGUILayout.Space();
LDtkEditorGUIUtility.DrawDivider();
}

private void ToClipboard()
Expand All @@ -110,6 +152,24 @@ private void ToClipboard()
LDtkDebug.Log($"Copied to clipboard: \"{RelPath}\". Paste this as a new custom command in LDtk then save!");
}

public bool IsInstalled(out string reason)
{
if (File.Exists(LDtkTilesetExporterUtil.PathToExe()))
{
if (!LDtkTilesetExporterUtil.GetAppUpToDate())
{
reason = $"The app's version does not match the required one";
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;
Expand All @@ -132,27 +192,5 @@ public bool HasCustomCommand(LdtkJson data, out string reason)
reason = $"A command to the above path doesn't exists";
return false;
}

public string GetPath()
{
string fromPath = LDtkPathUtility.AssetsPathToAbsolutePath(ProjectPath);
string appPath = LDtkTilesetExporterUtil.PathToExe();

string destPath = LDtkPathUtility.AssetsPathToAbsolutePath(appPath);

string dataPath = Application.dataPath;
Debug.Log(dataPath);

Debug.Log($"dataPath {dataPath}");
Debug.Log($"exportAppPath {appPath}");
Debug.Log($"destinationPath {destPath}");


var relPath = LDtkPathUtility.GetRelativePath(fromPath, destPath);

//backslashes break deserialization
relPath = LDtkPathUtility.CleanPathSlashes(relPath);
return relPath;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ internal static class LDtkImporterConsts
public const int LEVEL_VERSION = 8;
public const int TILESET_VERSION = 1;
public const string LDTK_JSON_VERSION = "1.3.0";
public const string EXPORT_APP_VERSION_REQUIRED = "1.0.0.0";

public const string PROJECT_EXT = "ldtk";
public const string LEVEL_EXT = "ldtkl";
Expand Down
49 changes: 31 additions & 18 deletions Assets/LDtkUnity/Editor/Utility/LDtkTilesetExporterUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.IO;
using System;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace LDtkUnity.Editor
{
Expand All @@ -9,47 +12,49 @@ internal static class LDtkTilesetExporterUtil
private const string EXPORT_ZIP = "LDtkTilesetExporter.zip";
private const string EXPORT_APP = "ExportTilesetDefinition.exe";

[MenuItem("UnzipToProject/Unzip")]
public static void UnzipToProject()
//[MenuItem("UnzipToProject/Unzip")]
public static void UnzipToLibrary()
{
string pathToZip = PathToZip();
EditorUtility.DisplayProgressBar("Install", pathToZip, 0);

string destDir = PathToLibraryDir();


LDtkPathUtility.TryCreateDirectory(destDir);
if (Directory.Exists(destDir))
{
//delete so it can overwrite
if (EditorUtility.DisplayDialog("delete", $"delete {destDir}", "ok", "cancel"))
DirectoryInfo di = new DirectoryInfo(destDir);
foreach (FileInfo file in di.GetFiles())
{
Directory.Delete(destDir);
file.Delete();
}
}

if (!Directory.Exists(destDir))
{
Directory.CreateDirectory(destDir);
}

Debug.Log($"pathToZip {pathToZip}");
Debug.Assert(File.Exists(pathToZip), "File.Exists(pathToZip)");

Debug.Log($"destDir {destDir}");
Debug.Assert(Directory.Exists(destDir), "Directory.Exists(destDir)");

ZipUtil.Extract(pathToZip, destDir);
Debug.Log($"Extracted the tileset export app to \"{destDir}\"");
}

[MenuItem("UnzipToProject/LogPathToExe")]
/*//[MenuItem("UnzipToProject/AppVersion")]
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);
//Debug.Assert(Directory.Exists(destDir), "path to project dir doesnt exist");
return destDir;
}

Expand All @@ -66,8 +71,16 @@ public static string PathToZip()
public static string PathToExe()
{
string exePath = Path.Combine(PathToLibraryDir(), EXPORT_APP);
Debug.Assert(File.Exists(exePath), "exe path doesnt exist");
return exePath;
}

public static bool GetAppUpToDate()
{
FileVersionInfo info = FileVersionInfo.GetVersionInfo(PathToExe());
Version version = new Version(info.FileVersion);
Version requiredVersion = new Version(LDtkImporterConsts.EXPORT_APP_VERSION_REQUIRED);
//Debug.Log($"app version {version}, required {requiredVersion}");
return version == requiredVersion;
}
}
}
4 changes: 2 additions & 2 deletions Assets/LDtkUnity/Samples~/Samples/AutoLayers_1_basic.ldtk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"iid": "a22d35f0-7820-11ed-b6fd-213e885f30da",
"jsonVersion": "1.3.3",
"appBuildId": 467696,
"appBuildId": 467698,
"nextUid": 108,
"identifierStyle": "Capitalize",
"toc": [],
Expand All @@ -36,7 +36,7 @@
"backupRelPath": null,
"levelNamePattern": "%world_Level_%idx",
"tutorialDesc": "IntGrid layers can paint tiles automatically using simple RULE SETS.\n\n - Press [SHIFT + R] to toggle auto-layer rendering\n - Click on the RULES button on the left to see/edit rules.",
"customCommands": [],
"customCommands": [{ "command": "../../../Library/LDtkTilesetExporter/ExportTilesetDefinition.exe", "when": "Manual" }],
"flags": ["UseMultilinesType"],
"defs": { "layers": [
{
Expand Down

0 comments on commit 76b3c55

Please sign in to comment.