Skip to content

Commit

Permalink
update ExctractTarGz, fixes #149, fix hidden beta regex parsing (feat…
Browse files Browse the repository at this point in the history
…ure not enabled yet)
  • Loading branch information
unitycoder committed Apr 25, 2024
1 parent c614492 commit 8056e1e
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 66 deletions.
173 changes: 116 additions & 57 deletions UnityLauncherPro/Libraries/ExtractTarGz.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// source https://gist.github.com/Su-s/438be493ae692318c73e30367cbc5c2a
// updated source https://gist.github.com/Matheos96/da8990030dfe3e27b0a48722042d9c0b

using System;
using System.IO;
Expand All @@ -16,7 +17,10 @@ public class Tar
/// <param name="outputDir">Output directory to write the files.</param>
public static void ExtractTarGz(string filename, string outputDir)
{
using (var stream = File.OpenRead(filename)) ExtractTarGz(stream, outputDir);
using (var stream = File.OpenRead(filename))
{
ExtractTarGz(stream, outputDir);
}
}

/// <summary>
Expand All @@ -26,9 +30,25 @@ public static void ExtractTarGz(string filename, string outputDir)
/// <param name="outputDir">Output directory to write the files.</param>
public static void ExtractTarGz(Stream stream, string outputDir)
{
using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
int read;
const int chunk = 4096;
var buffer = new byte[chunk];

// A GZipStream is not seekable, so copy it first to a MemoryStream
using (var gzipStream = new GZipStream(stream, CompressionMode.Decompress))
{
ExtractTar(gzip, outputDir);
using (var memStream = new MemoryStream())
{
//For .NET 6+
while ((read = gzipStream.Read(buffer, 0, buffer.Length)) > 0)
{
memStream.Write(buffer, 0, read);
}
memStream.Seek(0, SeekOrigin.Begin);

//ExtractTar(gzip, outputDir);
ExtractTar(memStream, outputDir);
}
}
}

Expand All @@ -40,7 +60,9 @@ public static void ExtractTarGz(Stream stream, string outputDir)
public static void ExtractTar(string filename, string outputDir)
{
using (var stream = File.OpenRead(filename))
{
ExtractTar(stream, outputDir);
}
}

/// <summary>
Expand All @@ -51,78 +73,115 @@ public static void ExtractTar(string filename, string outputDir)
public static void ExtractTar(Stream stream, string outputDir)
{
var buffer = new byte[100];
// store current position here
long pos = 0;
var longFileName = string.Empty;
while (true)
{
pos += stream.Read(buffer, 0, 100);
var name = Encoding.ASCII.GetString(buffer).Trim('\0');
if (String.IsNullOrWhiteSpace(name)) break;
FakeSeekForward(stream, 24);
pos += 24;
stream.Read(buffer, 0, 100);
string name = string.IsNullOrEmpty(longFileName) ? Encoding.ASCII.GetString(buffer).Trim('\0') : longFileName; //Use longFileName if we have one read

pos += stream.Read(buffer, 0, 12);
if (String.IsNullOrWhiteSpace(name)) break;
stream.Seek(24, SeekOrigin.Current);
stream.Read(buffer, 0, 12);
var size = Convert.ToInt64(Encoding.UTF8.GetString(buffer, 0, 12).Trim('\0').Trim(), 8);
FakeSeekForward(stream, 376);
pos += 376;
stream.Seek(20, SeekOrigin.Current); //Move head to typeTag byte
var typeTag = stream.ReadByte();
stream.Seek(355L, SeekOrigin.Current); //Move head to beginning of data (byte 512)

if (typeTag == 'L')
{
//If Type Tag is 'L' we have a filename that is longer than the 100 bytes reserved for it in the header.
//We read it here and save it temporarily as it will be the file name of the next block where the actual data is
var buf = new byte[size];
stream.Read(buf, 0, buf.Length);
longFileName = Encoding.ASCII.GetString(buf).Trim('\0');
}
else
{
longFileName = string.Empty; //Reset longFileName if current entry is not indicating one

var output = Path.Combine(outputDir, name);
var output = Path.Combine(outputDir, name);

// only include these folders
var include = (output.IndexOf("package/ProjectData~/Assets/") > -1);
include |= (output.IndexOf("package/ProjectData~/ProjectSettings/") > -1);
include |= (output.IndexOf("package/ProjectData~/Packages/") > -1);
// only include these folders
var include = (output.IndexOf("package/ProjectData~/Assets/") > -1);
include |= (output.IndexOf("package/ProjectData~/ProjectSettings/") > -1);
include |= (output.IndexOf("package/ProjectData~/Packages/") > -1);

// rename output path from "package/ProjectData~/Assets/" into "Assets/"
output = output.Replace("package/ProjectData~/", "");
// rename output path from "package/ProjectData~/Assets/" into "Assets/"
output = output.Replace("package/ProjectData~/", "");

if (include == true && !Directory.Exists(Path.GetDirectoryName(output))) Directory.CreateDirectory(Path.GetDirectoryName(output));
if (include == true && !Directory.Exists(Path.GetDirectoryName(output))) Directory.CreateDirectory(Path.GetDirectoryName(output));

if (!name.Equals("./", StringComparison.InvariantCulture))
{
if (include == true)
// not folder
//if (name.Equals("./", StringComparison.InvariantCulture) == false)
if (name.EndsWith("/") == false) //Directories are zero size and don't need anything written
{
//Console.WriteLine("output=" + output);
using (var str = File.Open(output, FileMode.OpenOrCreate, FileAccess.Write))
if (include == true)
{
//Console.WriteLine("output=" + output);
using (var str = File.Open(output, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var buf = new byte[size];
stream.Read(buf, 0, buf.Length);
// take only data from this folder
str.Write(buf, 0, buf.Length);
}
}
else
{
var buf = new byte[size];
pos += stream.Read(buf, 0, buf.Length);
// take only data from this folder
str.Write(buf, 0, buf.Length);
stream.Read(buf, 0, buf.Length);
}
}
else
{
//pos += size;
var buf = new byte[size];
pos += stream.Read(buf, 0, buf.Length);
//FakeSeekForward(stream, (int)size);
}
}

var offset = (int)(512 - (pos % 512));
//Move head to next 512 byte block
var pos = stream.Position;
var offset = 512 - (pos % 512);
if (offset == 512) offset = 0;
FakeSeekForward(stream, offset);
pos += offset;
}
}

private static void FakeSeekForward(Stream stream, int offset)
{
if (stream.CanSeek)
stream.Seek(offset, SeekOrigin.Current);
else
{
int bytesRead = 0;
var buffer = new byte[offset];
while (bytesRead < offset)
{
int read = stream.Read(buffer, bytesRead, offset - bytesRead);
if (read == 0)
throw new EndOfStreamException();
bytesRead += read;
}
}
}
}
}
} // class Tar
} // namespace TarLib


/*
This software is available under 2 licenses-- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain.We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
18 changes: 9 additions & 9 deletions UnityLauncherPro/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public static void DownloadInBrowser(string url, string version, bool preferFull
{
string exeURL = ParseDownloadURLFromWebpage(version, preferFullInstaller, useHash);

Console.WriteLine("download exeURL= (" + exeURL + ")");
//Console.WriteLine("DownloadInBrowser exeURL= '" + exeURL + "'");

if (string.IsNullOrEmpty(exeURL) == false && exeURL.StartsWith("https") == true)
{
Expand Down Expand Up @@ -834,9 +834,10 @@ public static string ParseDownloadURLFromWebpage(string version, bool preferFull

//Console.WriteLine(url);


version = FetchUnityVersionNumberFromHTML(url);
//Console.WriteLine(url);
//Console.WriteLine(version);
//Console.WriteLine("got "+version);
if (string.IsNullOrEmpty(version))
{
SetStatus("Failed to get version (" + version + ") number from hash: " + hash);
Expand All @@ -849,7 +850,7 @@ public static string ParseDownloadURLFromWebpage(string version, bool preferFull
string[] lines = sourceHTML.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

// patch version download assistant finder
if (VersionIsPatch(version))
if (useHash==false && VersionIsPatch(version))
{
for (int i = 0; i < lines.Length; i++)
{
Expand All @@ -862,7 +863,7 @@ public static string ParseDownloadURLFromWebpage(string version, bool preferFull
}
}
}
else if (VersionIsArchived(version))
else if (useHash == false && VersionIsArchived(version))
{
// archived version download assistant finder
for (int i = 0; i < lines.Length; i++)
Expand Down Expand Up @@ -911,15 +912,15 @@ public static string ParseDownloadURLFromWebpage(string version, bool preferFull
}
else // hidden download page
{
string pattern = @"UnityDownloadAssistant(?:-\d+\.\d+\.\d+b\d+)?\.exe";
string pattern = @"UnityDownloadAssistant(?:-\d+\.\d+\.\d+[bf]\d*)?\.exe";
Match match = Regex.Match(lines[i], pattern);
if (match.Success)
{
// append base url
Regex regex = new Regex(@"(https://beta\.unity3d\.com/download/[a-zA-Z0-9]+/)");
Match match2 = regex.Match(url);

Console.WriteLine("source urö: " + url);
//Console.WriteLine("source url: " + url);

if (match2.Success)
{
Expand All @@ -938,7 +939,7 @@ public static string ParseDownloadURLFromWebpage(string version, bool preferFull
} // alpha or beta

// download full installer instead
if (preferFullInstaller)
if (useHash == false && preferFullInstaller)
{
exeURL = exeURL.Replace("UnityDownloadAssistant-" + version + ".exe", "Windows64EditorInstaller/UnitySetup64-" + version + ".exe");
// handle alpha/beta
Expand All @@ -962,8 +963,7 @@ private static string FetchUnityVersionNumberFromHTML(string url)

if (string.IsNullOrEmpty(sourceHTML)) return null;

string pattern = @"\d+\.\d+\.\d+b\d+";

string pattern = @"\d+\.\d+\.\d+[bf]\d+";
MatchCollection matches = Regex.Matches(sourceHTML, pattern);
if (matches.Count > 0)
{
Expand Down

0 comments on commit 8056e1e

Please sign in to comment.