Skip to content

Commit

Permalink
Merge pull request #18 from niezbop/feature/github_repository
Browse files Browse the repository at this point in the history
Add GitHub repository
  • Loading branch information
niezbop authored Nov 27, 2017
2 parents dd34f24 + 528d11d commit 5d49f8d
Show file tree
Hide file tree
Showing 19 changed files with 1,158 additions and 636 deletions.
9 changes: 7 additions & 2 deletions Assets/Plugins/Editor/Uplift/Common/StrictXmlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ public StrictXmlDeserializer() {
this.serializer = new XmlSerializer(typeof(T));
}

public T Deserialize(FileStream fileStream) {
public T Deserialize(Stream stream) {
return StrictDeserialize(
() => { return serializer.Deserialize(fileStream); }
() => { return serializer.Deserialize(stream); }
);
}

public T Deserialize(string xmlString)
{
return Deserialize(new MemoryStream (Convert.FromBase64String(xmlString)));
}

private T StrictDeserialize(Func<Object> block) {
errors = new List<string> ();

Expand Down
264 changes: 136 additions & 128 deletions Assets/Plugins/Editor/Uplift/Common/UnityPackageOpener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,177 +48,185 @@ public void Extract(string archivePath)
Path.GetDirectoryName(archivePath)
);
}

// From https://github.com/Unity-Technologies/dotpackage-assistant/blob/master/DotPackageAssistant.cs


/// <summary>
/// Extracts a package from archivePath, and unpacks it at destinationPath
/// </summary>
public void Extract(string archivePath, string destinationPath)
{
using (FileStream compressedFileStream = File.OpenRead(archivePath))
{
GZipStream gzipFileStream = new GZipStream(compressedFileStream, CompressionMode.Decompress);
BinaryReader reader = new BinaryReader(gzipFileStream);
int readBufferSize = 1024 * 1024 * 10;
int tarBlockSize = 512;
byte[] readBuffer = new byte[readBufferSize];
Regex hashPattern = new Regex(@"^([a-f\d]{20,})\/");
Extract(compressedFileStream, destinationPath);
}
}

// From https://github.com/Unity-Technologies/dotpackage-assistant/blob/master/DotPackageAssistant.cs

byte[] rawAsset = null;
byte[] rawMeta = null;
string path = null;
/// <summary>
/// Extracts a package from the input stream, and unpacks it at destinationPath
/// </summary>
public void Extract(Stream stream, string destinationPath)
{
GZipStream gzipFileStream = new GZipStream(stream, CompressionMode.Decompress);
BinaryReader reader = new BinaryReader(gzipFileStream);
int readBufferSize = 1024 * 1024 * 10;
int tarBlockSize = 512;
byte[] readBuffer = new byte[readBufferSize];
Regex hashPattern = new Regex(@"^([a-f\d]{20,})\/");

while (true)
byte[] rawAsset = null;
byte[] rawMeta = null;
string path = null;

while (true)
{
byte[] headerBuffer = reader.ReadBytes(tarBlockSize); //We want the header, but the header is padded to a blocksize
if (headerBuffer.All(x => x == 0))
{
byte[] headerBuffer = reader.ReadBytes(tarBlockSize); //We want the header, but the header is padded to a blocksize
if (headerBuffer.All(x => x == 0))
{
//Reached end of stream
break;
}
GCHandle handle = GCHandle.Alloc(headerBuffer, GCHandleType.Pinned);
TarHeader header;
header = (TarHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(TarHeader));
handle.Free();
//Reached end of stream
break;
}
GCHandle handle = GCHandle.Alloc(headerBuffer, GCHandleType.Pinned);
TarHeader header;
header = (TarHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(TarHeader));
handle.Free();

string filename;
string filename;
unsafe
{
filename = Marshal.PtrToStringAnsi((IntPtr)header.filename, 100);
}
filename = filename.Trim();
filename = filename.TrimEnd(new char[] { (char)0 });

string ustar;
unsafe
{
ustar = Marshal.PtrToStringAnsi((IntPtr)header.ustar, 6);
}
string prefix = string.Empty;
if (ustar.Equals("ustar"))
{
unsafe
{
filename = Marshal.PtrToStringAnsi((IntPtr)header.filename, 100);
prefix = Marshal.PtrToStringAnsi((IntPtr)header.prefix, 155);
}
filename = filename.Trim();
filename = filename.TrimEnd(new char[] { (char)0 });
}
prefix = prefix.Trim();
prefix = prefix.TrimEnd(new char[] { (char)0 });

string ustar;
unsafe
string fullname = prefix + filename;
Match hashMatch = hashPattern.Match(fullname);

bool extractPathName = false;
bool extractRawMeta = false;
bool extractRawAsset = false;

if (hashMatch.Success)
{
if (fullname.EndsWith("/asset.meta"))
{
ustar = Marshal.PtrToStringAnsi((IntPtr)header.ustar, 6);
extractRawMeta = true;
}
string prefix = string.Empty;
if (ustar.Equals("ustar"))
if (fullname.EndsWith("/asset"))
{
unsafe
{
prefix = Marshal.PtrToStringAnsi((IntPtr)header.prefix, 155);
}
extractRawAsset = true;
}
prefix = prefix.Trim();
prefix = prefix.TrimEnd(new char[] { (char)0 });

string fullname = prefix + filename;
Match hashMatch = hashPattern.Match(fullname);
if (fullname.EndsWith("/pathname"))
{
extractPathName = true;
}
}

bool extractPathName = false;
bool extractRawMeta = false;
bool extractRawAsset = false;
string rawFilesize;
unsafe
{
rawFilesize = Marshal.PtrToStringAnsi((IntPtr)header.filesize, 12);
}
string filesize = rawFilesize.Trim();
filesize = filesize.TrimEnd(new char[] { (char)0 });

if (hashMatch.Success)
//Convert the octal string to a decimal number
try
{
int filesizeInt = Convert.ToInt32(filesize, 8);
int toRead = filesizeInt;
int toWrite = filesizeInt;
int modulus = filesizeInt % tarBlockSize;
if (modulus > 0)
toRead += (tarBlockSize - modulus); //Read the file and assume it's also 512 byte padded
while (toRead > 0)
{
if (fullname.EndsWith("/asset.meta"))
int readThisTime = Math.Min(readBufferSize, toRead);
int writeThisTime = Math.Min(readBufferSize, toWrite);
readBuffer = reader.ReadBytes(readThisTime);
if (extractPathName)
{
extractRawMeta = true;
if (toRead > readThisTime)
throw new Exception("Assumed a pathname would fit in a single read!");
string pathnameFileContents = Encoding.UTF8.GetString(readBuffer, 0, filesizeInt);
path = FormatPath(pathnameFileContents.Split(new char[] { '\n' })[0]);
Debug.Log(path);
}
if (fullname.EndsWith("/asset"))
else if(extractRawMeta)
{
extractRawAsset = true;
if(rawMeta == null) rawMeta = new byte[0];
int rawLength = rawMeta.Length;
Array.Resize<byte>(ref rawMeta, rawLength + writeThisTime);
Array.Copy(readBuffer, 0, rawMeta, rawLength, writeThisTime);
}
if (fullname.EndsWith("/pathname"))
else if(extractRawAsset)
{
extractPathName = true;
if(rawAsset == null) rawAsset = new byte[0];
int rawLength = rawAsset.Length;
Array.Resize<byte>(ref rawAsset, rawLength + writeThisTime);
Array.Copy(readBuffer, 0, rawAsset, rawLength, writeThisTime);
}
toRead -= readThisTime;
toWrite -= writeThisTime;
}

string rawFilesize;
unsafe
}
catch (Exception ex)
{
Debug.Log(String.Format("Caught Exception converting octal string to int: {0}", ex.Message));
foreach (byte fsChar in filesize)
{
rawFilesize = Marshal.PtrToStringAnsi((IntPtr)header.filesize, 12);
Debug.Log(fsChar);
}
string filesize = rawFilesize.Trim();
filesize = filesize.TrimEnd(new char[] { (char)0 });
throw;
}

// Path has been read, write to file system
if(path != null)
{
string target = Path.Combine(destinationPath, path);
Uplift.Common.FileSystemUtil.EnsureParentExists(target);

//Convert the octal string to a decimal number
try
// Asset or not? (ie directory)
if(rawAsset == null)
{
int filesizeInt = Convert.ToInt32(filesize, 8);
int toRead = filesizeInt;
int toWrite = filesizeInt;
int modulus = filesizeInt % tarBlockSize;
if (modulus > 0)
toRead += (tarBlockSize - modulus); //Read the file and assume it's also 512 byte padded
while (toRead > 0)
{
int readThisTime = Math.Min(readBufferSize, toRead);
int writeThisTime = Math.Min(readBufferSize, toWrite);
readBuffer = reader.ReadBytes(readThisTime);
if (extractPathName)
{
if (toRead > readThisTime)
throw new Exception("Assumed a pathname would fit in a single read!");
string pathnameFileContents = Encoding.UTF8.GetString(readBuffer, 0, filesizeInt);
path = FormatPath(pathnameFileContents.Split(new char[] { '\n' })[0]);
Debug.Log(path);
}
else if(extractRawMeta)
{
if(rawMeta == null) rawMeta = new byte[0];
int rawLength = rawMeta.Length;
Array.Resize<byte>(ref rawMeta, rawLength + writeThisTime);
Array.Copy(readBuffer, 0, rawMeta, rawLength, writeThisTime);
}
else if(extractRawAsset)
{
if(rawAsset == null) rawAsset = new byte[0];
int rawLength = rawAsset.Length;
Array.Resize<byte>(ref rawAsset, rawLength + writeThisTime);
Array.Copy(readBuffer, 0, rawAsset, rawLength, writeThisTime);
}
toRead -= readThisTime;
toWrite -= writeThisTime;
}
Directory.CreateDirectory(target);
}
catch (Exception ex)
else
{
Debug.Log(String.Format("Caught Exception converting octal string to int: {0}", ex.Message));
foreach (byte fsChar in filesize)
using(var tw = new StreamWriter(target, false, new UTF8Encoding(false)))
{
Debug.Log(fsChar);
tw.BaseStream.Write(rawAsset, 0, rawAsset.Length);
}
throw;
rawAsset = null;
}

// Path has been read, write to file system
if(path != null)
// Create meta
if(rawMeta != null)
{
string target = Path.Combine(destinationPath, path);
Uplift.Common.FileSystemUtil.EnsureParentExists(target);

// Asset or not? (ie directory)
if(rawAsset == null)
using(var tw = new StreamWriter(target + ".meta", false, new UTF8Encoding(false)))
{
Directory.CreateDirectory(target);
tw.BaseStream.Write(rawMeta, 0, rawMeta.Length);
}
else
{
using(var tw = new StreamWriter(target, false, new UTF8Encoding(false)))
{
tw.BaseStream.Write(rawAsset, 0, rawAsset.Length);
}
rawAsset = null;
}

// Create meta
if(rawMeta != null)
{
using(var tw = new StreamWriter(target + ".meta", false, new UTF8Encoding(false)))
{
tw.BaseStream.Write(rawMeta, 0, rawMeta.Length);
}
rawMeta = null;
}

path = null;
rawMeta = null;
}

path = null;
}
}
}
Expand Down
32 changes: 25 additions & 7 deletions Assets/Plugins/Editor/Uplift/Packages/PackageList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,34 @@ public void RefreshPackages()

public void LoadPackages(Repository repository)
{
PackageRepo pr;
foreach (Upset package in repository.ListPackages())
using(LogAggregator la = LogAggregator.InUnity(
"Packages successfully loaded from {0}",
"Packages successfully loaded from {0}, but warning were raised",
"Error(s) occured while loading packages from {0}",
repository.ToString()
))
{
pr = new PackageRepo
Upset[] packages;
try
{
Package = package,
Repository = repository
};
Packages.Add(pr);
packages = repository.ListPackages();
}
catch(Exception e)
{
Debug.LogException(e);
return;
}

PackageRepo pr;
foreach (Upset package in packages)
{
pr = new PackageRepo
{
Package = package,
Repository = repository
};
Packages.Add(pr);
}
}
}

Expand Down
Loading

0 comments on commit 5d49f8d

Please sign in to comment.