|
@@ -10,9 +10,11 @@ namespace PixiEditor.Extensions.Runtime;
|
|
|
|
|
|
public class ExtensionLoader
|
|
|
{
|
|
|
- private readonly Dictionary<string, OfficialExtensionData> _officialExtensionsKeys = new Dictionary<string, OfficialExtensionData>();
|
|
|
+ private readonly Dictionary<string, OfficialExtensionData> _officialExtensionsKeys =
|
|
|
+ new Dictionary<string, OfficialExtensionData>();
|
|
|
+
|
|
|
public List<Extension> LoadedExtensions { get; } = new();
|
|
|
-
|
|
|
+
|
|
|
public string PackagesPath { get; }
|
|
|
public string UnpackedExtensionsPath { get; }
|
|
|
|
|
@@ -24,7 +26,7 @@ public class ExtensionLoader
|
|
|
UnpackedExtensionsPath = unpackedExtensionsPath;
|
|
|
ValidateExtensionFolder();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void AddOfficialExtension(string uniqueName, OfficialExtensionData data)
|
|
|
{
|
|
|
_officialExtensionsKeys.Add(uniqueName, data);
|
|
@@ -85,17 +87,23 @@ public class ExtensionLoader
|
|
|
{
|
|
|
var extZip = ZipFile.OpenRead(extension);
|
|
|
ExtensionMetadata metadata = ExtractMetadata(extZip);
|
|
|
- if(IsDifferentThanCached(metadata, extension))
|
|
|
+ bool isLoaded = LoadedExtensions.Any(x => x.Metadata.UniqueName == metadata.UniqueName);
|
|
|
+ if (isLoaded)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (IsDifferentThanCached(metadata, extension))
|
|
|
{
|
|
|
UnpackExtension(extZip, metadata);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
string extensionJson = Path.Combine(UnpackedExtensionsPath, metadata.UniqueName, "extension.json");
|
|
|
if (!File.Exists(extensionJson))
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return LoadExtensionFromCache(extensionJson);
|
|
|
}
|
|
|
|
|
@@ -124,7 +132,7 @@ public class ExtensionLoader
|
|
|
var serializer = new JsonSerializer();
|
|
|
return serializer.Deserialize<ExtensionMetadata>(jsonTextReader);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private bool IsDifferentThanCached(ExtensionMetadata metadata, string extension)
|
|
|
{
|
|
|
string extensionJson = Path.Combine(UnpackedExtensionsPath, metadata.UniqueName, "extension.json");
|
|
@@ -135,24 +143,24 @@ public class ExtensionLoader
|
|
|
|
|
|
string json = File.ReadAllText(extensionJson);
|
|
|
ExtensionMetadata? cachedMetadata = JsonConvert.DeserializeObject<ExtensionMetadata>(json);
|
|
|
-
|
|
|
- if(cachedMetadata is null)
|
|
|
+
|
|
|
+ if (cachedMetadata is null)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (metadata.UniqueName != cachedMetadata.UniqueName)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
bool isDifferent = metadata.Version != cachedMetadata.Version;
|
|
|
-
|
|
|
+
|
|
|
if (isDifferent)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return PackageWriteTimeIsBigger(Path.Combine(UnpackedExtensionsPath, metadata.UniqueName), extension);
|
|
|
}
|
|
|
|
|
@@ -249,7 +257,7 @@ public class ExtensionLoader
|
|
|
|
|
|
if (fixedUniqueName.StartsWith("pixieditor".Trim(), StringComparison.OrdinalIgnoreCase))
|
|
|
{
|
|
|
- if(!IsOfficialAssemblyLegit(fixedUniqueName, assembly))
|
|
|
+ if (!IsOfficialAssemblyLegit(fixedUniqueName, assembly))
|
|
|
{
|
|
|
throw new ForbiddenUniqueNameExtension();
|
|
|
}
|
|
@@ -319,7 +327,9 @@ public class ExtensionLoader
|
|
|
private bool PublicKeysMatch(byte[] assemblyPublicKey, string pathToPublicKey)
|
|
|
{
|
|
|
Assembly currentAssembly = Assembly.GetExecutingAssembly();
|
|
|
- using Stream? stream = currentAssembly.GetManifestResourceStream($"{currentAssembly.GetName().Name}.OfficialExtensions.{pathToPublicKey}");
|
|
|
+ using Stream? stream =
|
|
|
+ currentAssembly.GetManifestResourceStream(
|
|
|
+ $"{currentAssembly.GetName().Name}.OfficialExtensions.{pathToPublicKey}");
|
|
|
if (stream == null) return false;
|
|
|
|
|
|
using MemoryStream memoryStream = new MemoryStream();
|
|
@@ -330,8 +340,9 @@ public class ExtensionLoader
|
|
|
}
|
|
|
|
|
|
//TODO: uhh, other platforms dumbass?
|
|
|
- [DllImport("mscoree.dll", CharSet=CharSet.Unicode)]
|
|
|
- static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification, ref bool pfWasVerified);
|
|
|
+ [DllImport("mscoree.dll", CharSet = CharSet.Unicode)]
|
|
|
+ static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification,
|
|
|
+ ref bool pfWasVerified);
|
|
|
|
|
|
private Extension LoadExtensionEntry(ExtensionEntry entry, ExtensionMetadata metadata)
|
|
|
{
|
|
@@ -389,7 +400,7 @@ public class ExtensionLoader
|
|
|
{
|
|
|
Directory.CreateDirectory(PackagesPath);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (!Directory.Exists(UnpackedExtensionsPath))
|
|
|
{
|
|
|
Directory.CreateDirectory(UnpackedExtensionsPath);
|