Skip to content

Commit

Permalink
improved the flow for adding the command
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed Aug 16, 2023
1 parent 6fd8a44 commit 0e1e333
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace LDtkUnity.Editor
{
internal sealed class LDtkEditorCommandUpdater
{
public string ProjectPath;
public string ProjectName;
public string RelPath;

public LDtkEditorCommandUpdater(string projectPath)
{
ProjectPath = projectPath;
RelPath = GetPath();
ProjectName = Path.GetFileNameWithoutExtension(projectPath);
}

private string GetPath()
Expand Down Expand Up @@ -98,17 +102,9 @@ public void TryDrawFixButton(LdtkJson data)
"To generate tileset files, you configure LDtk to run a custom export app through a custom command.\n" +
"(The app is included with this importer)\n" +
"\n" +
"To add the command, there's two options:\n" +
"To add the command:\n" +
"- Ensure LDtk is saved and closed first\n" +
"- Click the button below to modify the LDtk file which adds the command automatically\n" +
"\n" +
"Or:\n" +
"- Use the \"Copy\" button below the fix button to 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" +
$"\"{RelPath}\"\n" +
$"\n" +
"After adding the command, save the project. If a warning appears, select \"I understand the risk, allow user commands\".\n" +
"You only need to configure this once. Now with every project save, tileset definition files will be generated!\n" +
Expand All @@ -119,7 +115,7 @@ public void TryDrawFixButton(LdtkJson data)
switch (result)
{
case 0:
//todo get lines and insert the relpath as the command! but also check if the ldtk process is running just to ensure nothing is broke.
TryModifyProjectWithCommand();
break;
case 1:
//cancel
Expand Down Expand Up @@ -155,6 +151,58 @@ private void ToClipboard()
LDtkDebug.Log($"Copied to clipboard: \"{RelPath}\". Paste this as a new custom command in LDtk then save!");
}

public void TryModifyProjectWithCommand()
{
if (Process.GetProcessesByName("LDtk").Any())
{
if (EditorUtility.DisplayDialog(
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" +
$"\"{RelPath}\"\n",
"Copy to Clipboard", "Close"))
{
ToClipboard();
}

return;
}

ModifyProjectWithCommand(ProjectPath, RelPath);
Debug.Log($"Modified \"{ProjectName}\" with the custom command!");
}

public static void ModifyProjectWithCommand(string projectPath, string exeRelPath)
{
if (Path.GetExtension(projectPath) != ".ldtk")
{
return;
}

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

string[] lines = File.ReadAllLines(projectPath);
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (line.Contains(before))
{
lines[i] = line.Replace(before, insert);
break;
}
}
File.WriteAllLines(projectPath, lines);
}

public bool IsInstalled(out string reason)
{
if (File.Exists(LDtkTilesetExporterUtil.PathToExe()))
Expand Down
30 changes: 1 addition & 29 deletions Assets/Tools/SampleUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,8 @@ private static void CopyFilesRecursively(string sourcePath, string targetPath)

string dest = newPath.Replace(sourcePath, targetPath);
File.Copy(newPath, dest, true);
ModifyCommandToWork(dest);
LDtkEditorCommandUpdater.ModifyProjectWithCommand(dest, @"../../../Library/LDtkTilesetExporter/ExportTilesetDefinition.exe");
}
}

private static void ModifyCommandToWork(string newPath)
{
if (Path.GetExtension(newPath) != ".ldtk")
{
return;
}
Debug.Log(newPath);

const string before = @"""customCommands"": [],";
const string after = @"""customCommands"": [{ ""command"": ""../../../Library/LDtkTilesetExporter/ExportTilesetDefinition.exe"", ""when"": ""AfterSave"" }],";

string[] lines = File.ReadAllLines(newPath);
Debug.Log(lines.Length);

for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];

if (line.Contains(before))
{
Debug.Log("REPLACE");
lines[i] = line.Replace(before, after);
}
}

File.WriteAllLines(newPath, lines);
}
}
}

0 comments on commit 0e1e333

Please sign in to comment.