2024-06-19 04:10:46 -04:00
|
|
|
|
using Microsoft.CodeAnalysis;
|
2024-06-19 21:11:42 -04:00
|
|
|
|
using Microsoft.Win32;
|
2024-06-19 04:10:46 -04:00
|
|
|
|
using Newtonsoft.Json;
|
2024-06-18 20:42:58 -04:00
|
|
|
|
using ReCodeIt.Models;
|
|
|
|
|
using ReCodeIt.Utils;
|
2024-06-19 21:11:42 -04:00
|
|
|
|
using ReCodeItLib.Utils;
|
2024-06-18 20:42:58 -04:00
|
|
|
|
|
|
|
|
|
namespace ReCodeIt.CrossCompiler;
|
|
|
|
|
|
|
|
|
|
public static class ProjectManager
|
|
|
|
|
{
|
|
|
|
|
public static CrossCompilerProjectModel ActiveProject { get; private set; }
|
|
|
|
|
private static CrossCompilerSettings Settings => DataProvider.Settings.CrossCompiler;
|
|
|
|
|
|
2024-06-19 04:10:46 -04:00
|
|
|
|
public static List<string> AllProjectSourceFiles { get; private set; } = [];
|
|
|
|
|
|
2024-06-18 20:42:58 -04:00
|
|
|
|
public static void CreateProject(
|
|
|
|
|
string OrigAssemblyPath,
|
|
|
|
|
string VSSolutionDirPath,
|
2024-06-20 13:58:39 -04:00
|
|
|
|
string DependencyPath,
|
2024-06-18 20:42:58 -04:00
|
|
|
|
string BuildPath)
|
|
|
|
|
{
|
|
|
|
|
Logger.Log("-----------------------------------------------", ConsoleColor.Yellow);
|
|
|
|
|
Logger.Log($"Generating Cross Compiler project", ConsoleColor.Yellow);
|
|
|
|
|
Logger.Log($"Original Assembly Path {OrigAssemblyPath}", ConsoleColor.Yellow);
|
2024-06-20 14:23:11 -04:00
|
|
|
|
Logger.Log($"Remapped Assembly Path: {DependencyPath}", ConsoleColor.Yellow);
|
2024-06-18 20:42:58 -04:00
|
|
|
|
Logger.Log($"Visual Studio Solution Directory: {VSSolutionDirPath}", ConsoleColor.Yellow);
|
|
|
|
|
Logger.Log($"Build Path: {BuildPath}", ConsoleColor.Yellow);
|
|
|
|
|
|
|
|
|
|
// Build the project model
|
|
|
|
|
ActiveProject = new CrossCompilerProjectModel
|
|
|
|
|
{
|
|
|
|
|
OriginalAssemblyPath = OrigAssemblyPath,
|
|
|
|
|
VisualStudioSolutionPath = VSSolutionDirPath,
|
2024-06-20 13:58:39 -04:00
|
|
|
|
VisualStudioDependencyPath = DependencyPath,
|
2024-06-18 20:42:58 -04:00
|
|
|
|
BuildDirectory = BuildPath,
|
|
|
|
|
OriginalAssemblyHash = HashUtil.GetFileHash(OrigAssemblyPath),
|
|
|
|
|
RemappedAssemblyHash = "",
|
|
|
|
|
ChangedTypes = [],
|
|
|
|
|
RemapModels = []
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Now save the project json inside the original solution directory
|
|
|
|
|
SaveCrossCompilerProjectModel(ActiveProject);
|
|
|
|
|
|
|
|
|
|
Logger.Log($"Found Solution: {ActiveProject.SolutionName}", ConsoleColor.Yellow);
|
|
|
|
|
Logger.Log($"Original Assembly Checksum: {ActiveProject.OriginalAssemblyHash}", ConsoleColor.Yellow);
|
2024-06-18 21:06:37 -04:00
|
|
|
|
Logger.Log($"Project Generated to: {DataProvider.Settings.CrossCompiler.LastLoadedProject}", ConsoleColor.Green);
|
2024-06-18 20:42:58 -04:00
|
|
|
|
Logger.Log("-----------------------------------------------", ConsoleColor.Yellow);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 14:44:21 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the provided project model to disk, used from the GUI
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
public static void SaveCrossCompilerProjectModel(CrossCompilerProjectModel model)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(model.VisualStudioSolutionDirectoryPath, "ReCodeItProj.json");
|
|
|
|
|
|
|
|
|
|
JsonSerializerSettings settings = new()
|
|
|
|
|
{
|
|
|
|
|
Formatting = Formatting.Indented
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var jsonText = JsonConvert.SerializeObject(model, settings);
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(path, jsonText);
|
|
|
|
|
|
|
|
|
|
DataProvider.Settings.CrossCompiler.LastLoadedProject = path;
|
|
|
|
|
|
|
|
|
|
RegistryHelper.SetRegistryValue("LastLoadedProject", path, RegistryValueKind.String);
|
|
|
|
|
DataProvider.SaveAppSettings();
|
|
|
|
|
|
|
|
|
|
Logger.Log($"Cross Compiler project json saved to {path}", ConsoleColor.Green);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The "LoadProject" method only loads the project file from disk, used for initiating the GUI
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path"></param>
|
|
|
|
|
/// <param name="cli"></param>
|
2024-06-19 21:11:42 -04:00
|
|
|
|
public static void LoadProject(string path, bool cli = false)
|
2024-06-18 20:42:58 -04:00
|
|
|
|
{
|
2024-06-19 21:11:42 -04:00
|
|
|
|
ActiveProject = LoadCrossCompilerProjModel(path, cli);
|
2024-06-20 14:44:21 -04:00
|
|
|
|
Logger.Log($"Found and Loaded ReCodeIt Project at {path}");
|
|
|
|
|
}
|
2024-06-20 13:58:39 -04:00
|
|
|
|
|
2024-06-20 14:44:21 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads the project model from disk
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path"></param>
|
|
|
|
|
/// <param name="cli"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-06-19 21:11:42 -04:00
|
|
|
|
private static CrossCompilerProjectModel LoadCrossCompilerProjModel(string path, bool cli = false)
|
2024-06-18 20:42:58 -04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
Logger.Log($"Error loading cache model from `{path}`", ConsoleColor.Red);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var jsonText = File.ReadAllText(path);
|
|
|
|
|
|
|
|
|
|
var model = JsonConvert.DeserializeObject<CrossCompilerProjectModel>(jsonText);
|
|
|
|
|
|
2024-06-19 21:11:42 -04:00
|
|
|
|
if (!cli)
|
|
|
|
|
{
|
|
|
|
|
DataProvider.Settings.CrossCompiler.LastLoadedProject = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RegistryHelper.SetRegistryValue("LastLoadedProject", path, RegistryValueKind.String);
|
2024-06-18 20:42:58 -04:00
|
|
|
|
DataProvider.SaveAppSettings();
|
|
|
|
|
|
2024-06-20 13:58:39 -04:00
|
|
|
|
Logger.Log($"Loaded Cross Compiler Project: {model?.VisualStudioSolutionDirectoryPath}");
|
2024-06-18 20:42:58 -04:00
|
|
|
|
|
|
|
|
|
return model!;
|
|
|
|
|
}
|
2024-06-19 04:10:46 -04:00
|
|
|
|
|
2024-06-20 14:44:21 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gathers all the projects source files
|
|
|
|
|
/// </summary>
|
2024-06-20 13:58:39 -04:00
|
|
|
|
private static void LoadProjectSourceFiles()
|
2024-06-19 04:10:46 -04:00
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(
|
|
|
|
|
DataProvider.ReCodeItProjectsPath,
|
|
|
|
|
ActiveProject.SolutionName);
|
|
|
|
|
|
|
|
|
|
// Find all the source files in the project, we dont want anything from the obj folder.
|
|
|
|
|
AllProjectSourceFiles = Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories)
|
|
|
|
|
.Where(file => !file.Contains(Path.DirectorySeparatorChar + "obj" + Path.DirectorySeparatorChar))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
Logger.Log($"Found {AllProjectSourceFiles.Count} source files in the project", ConsoleColor.Yellow);
|
|
|
|
|
}
|
2024-06-18 20:42:58 -04:00
|
|
|
|
}
|