Add UTF8 format for created plugin files and format code

#1335
This commit is contained in:
Wojtek Figat
2023-09-11 22:32:37 +02:00
parent 99c2adc1f3
commit 985d6cc811
2 changed files with 45 additions and 44 deletions

View File

@@ -30,7 +30,7 @@ namespace FlaxEditor.Content
ShowFileExtension = true; ShowFileExtension = true;
} }
private static string FilterScriptName(string input) internal static string FilterScriptName(string input)
{ {
var length = input.Length; var length = input.Length;
var sb = new StringBuilder(length); var sb = new StringBuilder(length);

View File

@@ -6,6 +6,7 @@ using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using FlaxEditor.GUI; using FlaxEditor.GUI;
using FlaxEditor.GUI.ContextMenu; using FlaxEditor.GUI.ContextMenu;
@@ -13,7 +14,6 @@ using FlaxEditor.GUI.Tabs;
using FlaxEngine; using FlaxEngine;
using FlaxEngine.GUI; using FlaxEngine.GUI;
using FlaxEngine.Json; using FlaxEngine.Json;
using Debug = FlaxEngine.Debug;
namespace FlaxEditor.Windows namespace FlaxEditor.Windows
{ {
@@ -193,7 +193,7 @@ namespace FlaxEditor.Windows
Text = "Create Plugin Project", Text = "Create Plugin Project",
TooltipText = "Add new plugin project.", TooltipText = "Add new plugin project.",
AnchorPreset = AnchorPresets.TopLeft, AnchorPreset = AnchorPresets.TopLeft,
LocalLocation = new Float2(70,18), LocalLocation = new Float2(70, 18),
Size = new Float2(150, 25), Size = new Float2(150, 25),
Parent = vp, Parent = vp,
}; };
@@ -315,7 +315,7 @@ namespace FlaxEditor.Windows
{ {
if (Directory.Exists(Path.Combine(Globals.ProjectFolder, "Plugins", nameTextBox.Text)) && !string.IsNullOrEmpty(nameTextBox.Text)) if (Directory.Exists(Path.Combine(Globals.ProjectFolder, "Plugins", nameTextBox.Text)) && !string.IsNullOrEmpty(nameTextBox.Text))
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Warning, "Cannot create plugin due to name conflict."); Editor.LogWarning("Cannot create plugin due to name conflict.");
return; return;
} }
OnCloneButtonClicked(nameTextBox.Text, gitPathTextBox.Text); OnCloneButtonClicked(nameTextBox.Text, gitPathTextBox.Text);
@@ -346,7 +346,7 @@ namespace FlaxEditor.Windows
{ {
if (string.IsNullOrEmpty(gitPath)) if (string.IsNullOrEmpty(gitPath))
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Error, $"Failed to create plugin project due to no GIT path."); Editor.LogError($"Failed to create plugin project due to no GIT path.");
return; return;
} }
if (string.IsNullOrEmpty(pluginName)) if (string.IsNullOrEmpty(pluginName))
@@ -369,26 +369,28 @@ namespace FlaxEditor.Windows
Directory.CreateDirectory(clonePath); Directory.CreateDirectory(clonePath);
else else
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Error, $"Plugin Name is already used. Pick a different Name."); Editor.LogError($"Plugin Name is already used. Pick a different Name.");
return; return;
} }
try try
{ {
// Start git clone // Start git clone
var settings = new CreateProcessSettings(); var settings = new CreateProcessSettings
settings.FileName = "git"; {
settings.Arguments = $"clone {gitPath} \"{clonePath}\""; FileName = "git",
settings.ShellExecute = false; Arguments = $"clone {gitPath} \"{clonePath}\"",
settings.LogOutput = true; ShellExecute = false,
LogOutput = true,
};
Platform.CreateProcess(ref settings); Platform.CreateProcess(ref settings);
} }
catch (Exception e) catch (Exception e)
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Error, $"Failed Git process. {e}"); Editor.LogError($"Failed Git process. {e}");
return; return;
} }
Debug.Logger.LogHandler.LogWrite(LogType.Info, $"Plugin project has been cloned."); Editor.Log($"Plugin project has been cloned.");
// Find project config file. Could be different then what the user named the folder. // Find project config file. Could be different then what the user named the folder.
var files = Directory.GetFiles(clonePath); var files = Directory.GetFiles(clonePath);
@@ -403,7 +405,7 @@ namespace FlaxEditor.Windows
} }
if (string.IsNullOrEmpty(pluginProjectName)) if (string.IsNullOrEmpty(pluginProjectName))
Debug.Logger.LogHandler.LogWrite(LogType.Error, $"Failed to find plugin project file to add to Project config. Please add manually."); Editor.LogError($"Failed to find plugin project file to add to Project config. Please add manually.");
else else
{ {
await AddReferenceToProject(pluginName, pluginProjectName); await AddReferenceToProject(pluginName, pluginProjectName);
@@ -518,7 +520,7 @@ namespace FlaxEditor.Windows
{ {
if (Directory.Exists(Path.Combine(Globals.ProjectFolder, "Plugins", nameTextBox.Text))) if (Directory.Exists(Path.Combine(Globals.ProjectFolder, "Plugins", nameTextBox.Text)))
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Warning, "Cannot create plugin due to name conflict."); Editor.LogWarning("Cannot create plugin due to name conflict.");
return; return;
} }
OnCreateButtonClicked(nameTextBox.Text, versionTextBox.Text, companyTextBox.Text); OnCreateButtonClicked(nameTextBox.Text, versionTextBox.Text, companyTextBox.Text);
@@ -551,7 +553,7 @@ namespace FlaxEditor.Windows
{ {
if (string.IsNullOrEmpty(pluginName)) if (string.IsNullOrEmpty(pluginName))
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Error, $"Failed to create plugin project due to no plugin name."); Editor.LogError($"Failed to create plugin project due to no plugin name.");
return; return;
} }
@@ -564,20 +566,20 @@ namespace FlaxEditor.Windows
try try
{ {
// Download example plugin // Download example plugin
using (HttpClient client = new HttpClient()) using (var client = new HttpClient())
{ {
byte[] zipBytes = await client.GetByteArrayAsync(templateUrl); byte[] zipBytes = await client.GetByteArrayAsync(templateUrl);
await File.WriteAllBytesAsync(!File.Exists(localTemplatePath) ? Path.Combine(localTemplatePath) : Path.Combine(Editor.LocalCachePath, "TemplatePluginCache" , "TemplatePlugin1.zip"), zipBytes); await File.WriteAllBytesAsync(!File.Exists(localTemplatePath) ? Path.Combine(localTemplatePath) : Path.Combine(Editor.LocalCachePath, "TemplatePluginCache", "TemplatePlugin1.zip"), zipBytes);
Debug.Logger.LogHandler.LogWrite(LogType.Info, "Template for plugin project has downloaded"); Editor.Log("Template for plugin project has downloaded");
} }
} }
catch (Exception e) catch (Exception e)
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Error, $"Failed to download template project. Trying to use local file. {e}"); Editor.LogError($"Failed to download template project. Trying to use local file. {e}");
if (!File.Exists(localTemplatePath)) if (!File.Exists(localTemplatePath))
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Error, $"Failed to use local file. Does not exist."); Editor.LogError($"Failed to use local file. Does not exist.");
return; return;
} }
} }
@@ -630,15 +632,12 @@ namespace FlaxEditor.Windows
try try
{ {
await Task.Run(() => ZipFile.ExtractToDirectory(localTemplatePath, extractPath)); await Task.Run(() => ZipFile.ExtractToDirectory(localTemplatePath, extractPath));
Debug.Logger.LogHandler.LogWrite(LogType.Info, "Template for plugin project successfully moved to project."); Editor.Log("Template for plugin project successfully moved to project.");
} }
catch (IOException e) catch (IOException e)
{ {
Debug.Logger.LogHandler.LogWrite(LogType.Error, $"Failed to add plugin to project. {e}"); Editor.LogError($"Failed to add plugin to project. {e}");
} }
var oldpluginPath = Path.Combine(extractPath, "ExamplePlugin-master");
var newPluginPath = Path.Combine(extractPath , pluginName);
Directory.Move(oldpluginPath, newPluginPath);
// Format plugin name into a valid name for code (C#/C++ typename) // Format plugin name into a valid name for code (C#/C++ typename)
var pluginCodeName = Content.ScriptItem.FilterScriptName(pluginName); var pluginCodeName = Content.ScriptItem.FilterScriptName(pluginName);
@@ -646,6 +645,9 @@ namespace FlaxEditor.Windows
pluginCodeName = "MyPlugin"; pluginCodeName = "MyPlugin";
Editor.Log($"Using plugin code type name: {pluginCodeName}"); Editor.Log($"Using plugin code type name: {pluginCodeName}");
var oldPluginPath = Path.Combine(extractPath, "ExamplePlugin-master");
var newPluginPath = Path.Combine(extractPath, pluginName);
Directory.Move(oldPluginPath, newPluginPath);
var oldFlaxProjFile = Path.Combine(newPluginPath, "ExamplePlugin.flaxproj"); var oldFlaxProjFile = Path.Combine(newPluginPath, "ExamplePlugin.flaxproj");
var newFlaxProjFile = Path.Combine(newPluginPath, $"{pluginName}.flaxproj"); var newFlaxProjFile = Path.Combine(newPluginPath, $"{pluginName}.flaxproj");
@@ -667,7 +669,7 @@ namespace FlaxEditor.Windows
flaxPluginProjContents.Company = companyName; flaxPluginProjContents.Company = companyName;
flaxPluginProjContents.GameTarget = $"{pluginCodeName}Target"; flaxPluginProjContents.GameTarget = $"{pluginCodeName}Target";
flaxPluginProjContents.EditorTarget = $"{pluginCodeName}EditorTarget"; flaxPluginProjContents.EditorTarget = $"{pluginCodeName}EditorTarget";
await File.WriteAllTextAsync(newFlaxProjFile, JsonSerializer.Serialize(flaxPluginProjContents, typeof(ProjectInfo))); await File.WriteAllTextAsync(newFlaxProjFile, JsonSerializer.Serialize(flaxPluginProjContents, typeof(ProjectInfo)), Encoding.UTF8);
// Format game settings // Format game settings
var gameSettingsPath = Path.Combine(newPluginPath, "Content", "GameSettings.json"); var gameSettingsPath = Path.Combine(newPluginPath, "Content", "GameSettings.json");
@@ -709,11 +711,11 @@ namespace FlaxEditor.Windows
foreach (var file in targetFiles) foreach (var file in targetFiles)
{ {
var fileText = await File.ReadAllTextAsync(file); var fileText = await File.ReadAllTextAsync(file);
await File.WriteAllTextAsync(file, fileText.Replace("ExamplePlugin", pluginCodeName)); await File.WriteAllTextAsync(file, fileText.Replace("ExamplePlugin", pluginCodeName), Encoding.UTF8);
var newName = file.Replace("ExamplePlugin", pluginCodeName); var newName = file.Replace("ExamplePlugin", pluginCodeName);
File.Move(file, newName); File.Move(file, newName);
} }
Debug.Logger.LogHandler.LogWrite(LogType.Info, $"Plugin project {pluginName} has successfully been created."); Editor.Log($"Plugin project {pluginName} has successfully been created.");
await AddReferenceToProject(pluginName, pluginName); await AddReferenceToProject(pluginName, pluginName);
MessageBox.Show($"{pluginName} has been successfully created. Restart editor for changes to take effect.", "Plugin Project Created", MessageBoxButtons.OK); MessageBox.Show($"{pluginName} has been successfully created. Restart editor for changes to take effect.", "Plugin Project Created", MessageBoxButtons.OK);
@@ -738,11 +740,10 @@ namespace FlaxEditor.Windows
references.Add(newReference); references.Add(newReference);
} }
flaxProjContents.References = references.ToArray(); flaxProjContents.References = references.ToArray();
await File.WriteAllTextAsync(flaxProjPath, JsonSerializer.Serialize(flaxProjContents, typeof(ProjectInfo))); await File.WriteAllTextAsync(flaxProjPath, JsonSerializer.Serialize(flaxProjContents, typeof(ProjectInfo)), Encoding.UTF8);
} }
} }
private void OnPluginsChanged() private void OnPluginsChanged()
{ {
List<PluginEntry> toRemove = null; List<PluginEntry> toRemove = null;