Skip to content

Commit

Permalink
[SDK-626] Feature/webgl helper update (#158)
Browse files Browse the repository at this point in the history
- updated auto setup functionality to move instead of copy folders
- added the moving of WebGLHelper to correct plugins folder
  • Loading branch information
HarrisonHough authored Nov 22, 2023
1 parent 4280c90 commit 7208ed3
Showing 1 changed file with 35 additions and 41 deletions.
76 changes: 35 additions & 41 deletions Samples~/WebGLSample/Editor/EditorWindows/SampleSetup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using ReadyPlayerMe.Core;
using ReadyPlayerMe.Core.Editor;
Expand All @@ -12,14 +13,15 @@ public static class SampleSetup
private const string TAG = nameof(SampleSetup);
private const string WINDOW_TITLE = "RPM WebGL Sample";
private const string DESCRIPTION =
"This sample includes a WebGL template that can be used for WebGL builds. To use the template it needs to be moved inside WebGLTemplates folder and set in the player settings. Would you like to move it automatically?";
"This sample includes a WebGL template and a WebGLHelper library that is used for WebGL builds. For these to work they need to be moved into specific directories. Would you like to move them automatically now?";
private const string CONFIRM_BUTTON_TEXT = "Ok";
private const string CANCEL_BUTTON_TEXT = "Cancel";

private const string RPM_WEBGL_SCREEN_SHOWN_KEY = "rpm-webgl-screen-shown";
private const string TEMPLATE_PATH = "/WebGLTemplates/RPMTemplate";
private const string FILE_NAME = "SampleSetup.cs";
private const string ROOT_PATH = "/Assets";
private const string TEMPLATE_PATH = "WebGLTemplates";
private const string FILE_NAME = "ReadyPlayerMe.Core.WebGLSample.asmdef";
private const string PLUGINS_FOLDER = "Plugins";
private const string WEBGL_HELPER_PATH = "WebGlHelper";

[InitializeOnLoadMethod]
private static void InitializeOnLoad()
Expand Down Expand Up @@ -53,59 +55,51 @@ private static void ShowWebGLScreen()

private static void OnConfirm()
{
var templatePaths = GetTemplatePaths();

if (templatePaths == null)
var samplesRootFolder = GetSampleRootFolder();
if (string.IsNullOrEmpty(samplesRootFolder))
{
Debug.LogWarning("Failed to set source and destination paths. No changes were done to project");
Debug.LogWarning("Failed to find WebGLSample. No changes were done to project");
return;
}
Copy(templatePaths[0], templatePaths[1]);
MoveFolder($"{samplesRootFolder}/{TEMPLATE_PATH}", $"{Application.dataPath}");
MoveFolder($"{samplesRootFolder}/{WEBGL_HELPER_PATH}", $"{Application.dataPath}/{PLUGINS_FOLDER}");
SetWebGLTemplate();
}

private static List<string> GetTemplatePaths()
private static string GetSampleRootFolder()
{
var res = Directory.GetFiles(Application.dataPath, FILE_NAME, SearchOption.AllDirectories);
if (res.Length == 0)
var results = Directory.GetFiles(Application.dataPath, FILE_NAME, SearchOption.AllDirectories);
if (results.Length == 0)
{
return null;
return String.Empty;
}
var path = res[0].Replace(FILE_NAME, "").Replace("\\", "/");
var sourcePath = path.Substring(0, path.IndexOf("/Editor/")) + TEMPLATE_PATH;
var destinationPath = path.Substring(0, path.IndexOf(ROOT_PATH)) + ROOT_PATH;
return new List<string>() { sourcePath, destinationPath };
var rootSamplePath = results[0].Replace(FILE_NAME, "").Replace("\\", "/");
return rootSamplePath.TrimEnd('/');
}

private static void Copy(string sourcePath, string destinationPath)
private static void MoveFolder(string sourcePath, string destinationPath)
{
foreach (string sourceFile in Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories))
{
if (sourceFile.EndsWith(".meta"))
{
continue;
}

var sourceFilePath = sourceFile.Replace("\\", "/");
// Extract the last part of the source path (e.g., "Plugin")
var sourceDirectoryName = new DirectoryInfo(sourcePath).Name;

if (File.Exists(sourceFilePath))
{
var destination = destinationPath + sourceFilePath.Substring(sourceFilePath.IndexOf(TEMPLATE_PATH)).Replace("\\", "/");
// Append the source directory name to the destination path
var newDestinationPath = Path.Combine(destinationPath, sourceDirectoryName);

if (!Directory.Exists(destination.Substring(0, destination.LastIndexOf("/"))))
{
Directory.CreateDirectory(destination.Substring(0, destination.LastIndexOf("/")));
}
// Check if the source directory exists
if (!Directory.Exists(sourcePath))
{
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourcePath);
}

File.Copy(sourceFilePath, destination, true);
}
else
{
Debug.LogError("Source file does not exist: " + sourceFilePath);
}
// If the destination directory doesn't exist, create it
if (!Directory.Exists(destinationPath))
{
Directory.CreateDirectory(destinationPath);
}

SDKLogger.Log(TAG, "Copied RPMTemplate to the WebGLTemplate folder in the root path of Assets");
// Move the entire source directory to the new destination
Directory.Move(sourcePath, newDestinationPath);
SDKLogger.Log(TAG, $"Moved folder and contents from {sourcePath} to {newDestinationPath}");
AssetDatabase.Refresh();
}

Expand Down

0 comments on commit 7208ed3

Please sign in to comment.