This commit is contained in:
nub31
2026-02-13 00:27:59 +01:00
parent 21a095f691
commit ee485e4119
6 changed files with 347 additions and 399 deletions

View File

@@ -1,5 +1,6 @@
using System.IO.Compression;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Compiler;
@@ -32,7 +33,7 @@ public class NubLib
fileStream.CopyTo(entryStream);
}
public static NublibLoadResult Unpack(string nublibPath)
public static NubLibLoadResult Unpack(string nublibPath)
{
using var fs = new FileStream(nublibPath, FileMode.Open, FileAccess.Read);
using var zip = new ZipArchive(fs, ZipArchiveMode.Read);
@@ -55,8 +56,48 @@ public class NubLib
entryStream.CopyTo(tempFile);
}
return new NublibLoadResult(manifest, tempArchivePath);
return new NubLibLoadResult(manifest, tempArchivePath);
}
public record NublibLoadResult(Manifest Manifest, string ArchivePath);
public record NubLibLoadResult(Manifest Manifest, string ArchivePath);
}
public record Manifest(Dictionary<string, Manifest.Module> Modules)
{
public static Manifest Create(ModuleGraph moduleGraph)
{
var modules = new Dictionary<string, Module>();
foreach (var module in moduleGraph.GetModules())
{
var types = module.GetTypes().ToDictionary(x => x.Key, x => ConvertType(x.Value));
var identifiers = module.GetIdentifiers().ToDictionary(x => x.Key, x => new Module.IdentifierInfo(x.Value.Exported, x.Value.Type));
modules[module.Name] = new Module(types, identifiers);
}
return new Manifest(modules);
static Module.TypeInfo ConvertType(Compiler.Module.TypeInfo typeInfo)
{
return typeInfo switch
{
Compiler.Module.TypeInfoStruct s => new Module.TypeInfoStruct(s.Exported, s.Packed, s.Fields.Select(x => new Module.TypeInfoStruct.Field(x.Name, x.Type)).ToList()),
_ => throw new ArgumentOutOfRangeException(nameof(typeInfo))
};
}
}
public record Module(Dictionary<string, Module.TypeInfo> Types, Dictionary<string, Module.IdentifierInfo> Identifiers)
{
public record IdentifierInfo(bool Exported, NubType Type);
[JsonDerivedType(typeof(TypeInfoStruct), "struct")]
public abstract record TypeInfo(bool Exported);
public record TypeInfoStruct(bool Exported, bool Packed, IReadOnlyList<TypeInfoStruct.Field> Fields) : TypeInfo(Exported)
{
public record Field(string Name, NubType Type);
}
}
}