Nerfed/Nerfed.Compiler/AssemblyDefinition.cs

64 lines
1.9 KiB
C#
Raw Normal View History

2024-07-23 22:31:19 +02:00
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Nerfed.Compiler;
public class AssemblyDefinition
{
public string Name { get; set; }
public string Guid { get; set; }
//public bool IsEditor { get; set; }
// Add platform stuff here..?
// Add dll's here..?
// Add dependencies here..?
public static bool Create(string assemblyDefinitionFilePath, string name, out AssemblyDefinition assemblyDefinition)
{
assemblyDefinition = null;
if (File.Exists(assemblyDefinitionFilePath))
{
Console.WriteLine($"ERROR: File already exists!");
return false;
}
// Create project file.
assemblyDefinition = new AssemblyDefinition
{
Name = name,
Guid = System.Guid.NewGuid().ToString("B").ToUpper(),
};
Save(assemblyDefinition, assemblyDefinitionFilePath);
return true;
}
public static bool Save(AssemblyDefinition assemblyDefinition, string assemblyDefinitionFilePath)
{
string jsonString = JsonSerializer.Serialize(assemblyDefinition, AssemblyDefinitionContext.Default.AssemblyDefinition);
File.WriteAllText(assemblyDefinitionFilePath, jsonString);
return true;
}
public static bool Open(string assemblyDefinitionFilePath, out AssemblyDefinition assemblyDefinition)
{
string jsonString = File.ReadAllText(assemblyDefinitionFilePath);
assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition);
if (assemblyDefinition == null)
{
Console.WriteLine($"ERROR: Could not open {typeof(AssemblyDefinition)}.");
return false;
}
return true;
}
}
[JsonSerializable(typeof(AssemblyDefinition))]
public partial class AssemblyDefinitionContext : JsonSerializerContext
{
}