Skip to content

Commit 0febfad

Browse files
committed
allow templates to be folders and .tgz files, fixes #197
1 parent 5080404 commit 0febfad

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

UnityLauncherPro/Tools.cs

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,10 +1705,27 @@ public static Project FastCreateProject(string version, string baseFolder, strin
17051705
// create folder
17061706
CreateEmptyProjectFolder(newPath, version);
17071707

1708-
// unzip template, if any
1708+
// unzip or copy template
17091709
if (templateZipPath != null)
17101710
{
1711-
TarLib.Tar.ExtractTarGz(templateZipPath, newPath);
1711+
//Console.WriteLine(templateZipPath);
1712+
1713+
if (File.Exists(templateZipPath))
1714+
{
1715+
TarLib.Tar.ExtractTarGz(templateZipPath, newPath);
1716+
}
1717+
else if (Directory.Exists(templateZipPath))
1718+
{
1719+
try
1720+
{
1721+
CopyDirectory(templateZipPath, newPath);
1722+
}
1723+
catch (Exception ex)
1724+
{
1725+
SetStatus("Error copying template folder: " + ex.Message);
1726+
}
1727+
}
1728+
17121729
}
17131730

17141731
// copy init file into project
@@ -1830,24 +1847,38 @@ public static Dictionary<string, string> ScanTemplates(string unityInstallPath)
18301847
var templateFolder = Path.Combine(unityPath, "Data/Resources/PackageManager/ProjectTemplates/");
18311848
if (Directory.Exists(templateFolder) == false) return items;
18321849

1850+
// get all files in template folder
18331851
var fileEntries = Directory.GetFiles(templateFolder).ToList();
18341852

18351853
// process found files
1836-
for (int i = fileEntries.Count - 1; i > -1; i--)
1854+
for (int i = 0; i < fileEntries.Count; i++)
18371855
{
1856+
//Console.WriteLine(fileEntries[i]);
18381857
// check if its tgz
1839-
if (fileEntries[i].IndexOf(".tgz") == -1)
1840-
{
1841-
fileEntries.RemoveAt(i);
1842-
}
1843-
else
1858+
if (fileEntries[i].ToLower().IndexOf(".tgz") > -1)
18441859
{
1845-
// cleanup name
1860+
// cleanup displayed name
18461861
var name = Path.GetFileName(fileEntries[i]).Replace("com.unity.template.", "").Replace(".tgz", "");
18471862
items.Add(name, fileEntries[i]);
18481863
}
18491864
}
18501865

1866+
// 2018.4 has folders instead of tgz files
1867+
// BUT do this for all versions, in case user has added custom template folders (that contain Assets/ folder)
1868+
var dirEntries = Directory.GetDirectories(templateFolder).ToList();
1869+
for (int i = 0; i < dirEntries.Count; i++)
1870+
{
1871+
//Console.WriteLine(dirEntries[i]);
1872+
// if it has com.unity.template. prefix, then its a template
1873+
// if that directory contains Assets/ folder, then its a template
1874+
if (Directory.Exists(Path.Combine(dirEntries[i], "Assets")) == true)
1875+
{
1876+
//Console.WriteLine(dirEntries[i]);
1877+
var name = Path.GetFileName(dirEntries[i]).Replace("com.unity.template.", "");
1878+
items.Add(name, dirEntries[i]);
1879+
}
1880+
}
1881+
18511882
return items;
18521883
}
18531884

@@ -2792,10 +2823,7 @@ public static string GetSafeFilePath(string subfolder, string fileName)
27922823
// 1) Preferred: under the app folder
27932824
string preferredDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, subfolder);
27942825
// 2) Fallback: in LocalAppData
2795-
string fallbackDir = Path.Combine(
2796-
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
2797-
"UnityLauncherPro",
2798-
subfolder);
2826+
string fallbackDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UnityLauncherPro", subfolder);
27992827

28002828
try
28012829
{
@@ -2818,9 +2846,26 @@ public static string GetSafeFilePath(string subfolder, string fileName)
28182846
return Path.Combine(fallbackDir, fileName);
28192847
}
28202848

2849+
// copy directory structure (for template folder)
2850+
public static void CopyDirectory(string sourceDir, string targetDir)
2851+
{
2852+
// Create the target directory if it doesn't exist
2853+
Directory.CreateDirectory(targetDir);
28212854

2855+
// Copy all files
2856+
foreach (var file in Directory.GetFiles(sourceDir))
2857+
{
2858+
var destFile = Path.Combine(targetDir, Path.GetFileName(file));
2859+
File.Copy(file, destFile, overwrite: false);
2860+
}
28222861

2823-
2862+
// Recursively copy subdirectories
2863+
foreach (var directory in Directory.GetDirectories(sourceDir))
2864+
{
2865+
var destDir = Path.Combine(targetDir, Path.GetFileName(directory));
2866+
CopyDirectory(directory, destDir);
2867+
}
2868+
}
28242869

28252870
} // class
28262871

0 commit comments

Comments
 (0)