Skip to content

Commit

Permalink
zip
Browse files Browse the repository at this point in the history
  • Loading branch information
Rynchodon committed Nov 2, 2016
1 parent a99b1db commit 041cc1c
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Extender/Loader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
Expand Down
4 changes: 2 additions & 2 deletions Extender/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("0.4.0.0")]
[assembly: AssemblyFileVersion("0.4.0.0")]
51 changes: 39 additions & 12 deletions Injector/ArmsUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand All @@ -10,15 +11,14 @@ namespace Rynchodon
{
public static class ArmsUpdater
{
public const string ArmsDll = "ARMS.dll", ArmsReleaseNotes = "ARMS - Release Notes.txt";
public const string ArmsZip = "ARMS.zip", ArmsDll = "ARMS.dll", ArmsReleaseNotes = "ARMS - Release Notes.txt", User_Agent = "ARMS-Updater";

public static void UpdateArms()
{
const string stable = "-stable", unstable = "-unstable";
const string userAgent = "ARMS-Updater";

Release[] allReleases;
try { allReleases = GitHubClient.GetReleases(userAgent); }
try { allReleases = GitHubClient.GetReleases("ARMS", User_Agent); }
catch (WebException ex)
{
WriteLine("Failed to connect to github:\n" + ex);
Expand All @@ -39,11 +39,15 @@ public static void UpdateArms()

Release.Asset asset = null;
foreach (Release.Asset a in bestRelease.assets)
if (a.name == ArmsDll)
{
if (a.name == ArmsZip)
{
asset = a;
break;
}
if (a.name == ArmsDll)
asset = a;
}

if (asset == null)
{
Expand All @@ -57,14 +61,7 @@ public static void UpdateArms()
return;
}

WriteLine("Downloading update: " + bestRelease.BestName);
HttpWebRequest request = WebRequest.CreateHttp(asset.browser_download_url);
request.UserAgent = userAgent;
WebResponse response = request.GetResponse();

FileStream file = File.Create("ARMS.dll");
response.GetResponseStream().CopyTo(file);
file.Close();
DownloadAsset(bestRelease, asset);

File.WriteAllText(ArmsReleaseNotes, bestRelease.body);
WriteLine("ARMS has been updated");
Expand All @@ -80,6 +77,36 @@ private static bool NeedsUpdate(Release rel)
return rel.Version.CompareTo(currentVersion) > 0;
}

private static void DownloadAsset(Release bestRelease, Release.Asset asset)
{
WriteLine("Downloading update: " + bestRelease.BestName);
HttpWebRequest request = WebRequest.CreateHttp(asset.browser_download_url);
request.UserAgent = User_Agent;

WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

if (asset.name == ArmsZip)
{
FileStream zipFile = File.Create(ArmsZip);
responseStream.CopyTo(zipFile);
zipFile.Dispose();
ZipFile.ExtractToDirectory(ArmsZip, ".");
File.Delete(ArmsZip);
}
else if (asset.name == ArmsDll)
{
FileStream dllFile = File.Create(ArmsDll);
responseStream.CopyTo(dllFile);
dllFile.Dispose();
}
else
throw new Exception("Unknown asset: " + asset.name);

responseStream.Dispose();
response.Dispose();
}

private static void WriteLine(string line, bool skipMemeberName = false, [CallerMemberName] string memberName = null)
{
if (!skipMemeberName)
Expand Down
2 changes: 2 additions & 0 deletions Injector/DllInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ private static void Run()
Console.WriteLine(currentVersion.ToString());
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine();

reader.Dispose();
}

process = WaitForGameStart(isDedicatedServer);
Expand Down
56 changes: 19 additions & 37 deletions Injector/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;

namespace Rynchodon
{
public class GitHubClient
{

public static void CreateRelease(string userAgent, string oAuthToken, Release release, params string[] assetsPaths)
public static void CreateRelease(string repo, string userAgent, string oAuthToken, Release release, params string[] assetsPaths)
{
HttpWebRequest request = WebRequest.CreateHttp(@"https://api.github.com/repos/Rynchodon/ARMS/releases");
HttpWebRequest request = WebRequest.CreateHttp(@"https://api.github.com/repos/Rynchodon/" + repo + "/releases");
request.UserAgent = userAgent;
request.Method = "POST";
request.Headers.Add("Authorization", "token " + oAuthToken);
Expand All @@ -28,47 +27,30 @@ public static void CreateRelease(string userAgent, string oAuthToken, Release re
foreach (string asset in assetsPaths)
{
string fileName = Path.GetFileName(asset);
Console.Write("Posting asset " + fileName + ": ");
request = WebRequest.CreateHttp(@"https://uploads.github.com/repos/Rynchodon/" + repo + "/releases/" + release.id + "/assets?name=" + fileName);
request.UserAgent = userAgent;
request.Method = "POST";
request.ContentType = "application/" + Path.GetExtension(fileName);
request.Headers.Add("Authorization", "token " + oAuthToken);

using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.UserAgent, userAgent);
client.Headers.Add(HttpRequestHeader.ContentType, "application/dll");
client.Headers.Add(HttpRequestHeader.Authorization, "token " + oAuthToken);
Stream upStream = request.GetRequestStream();
FileStream fileRead = new FileStream(asset, FileMode.Open);

int cursorLeft = Console.CursorLeft, cursorTop = Console.CursorTop;
long lastPercent = -1L;
object locker = new object();
UploadProgressChangedEventHandler handler = (sender, e) => {
long percent = e.BytesSent * 100L / e.TotalBytesToSend;
lock (locker)
{
if (percent == lastPercent)
return;
lastPercent = percent;
Console.SetCursorPosition(cursorLeft, cursorTop);
if (percent < 10)
Console.Write(' ');
if (percent < 100)
Console.Write(' ');
Console.Write(percent);
Console.Write('%');
}
};
fileRead.CopyTo(upStream);
Console.WriteLine("Posting: " + fileName);
request.GetResponse().Dispose();

client.UploadProgressChanged += handler;
Task uploadTask = client.UploadFileTaskAsync(@"https://uploads.github.com/repos/Rynchodon/ARMS/releases/" + release.id + "/assets?name=" + fileName, asset);
uploadTask.Wait();
uploadTask.Dispose();
}

Console.WriteLine();
fileRead.Dispose();
upStream.Dispose();
}

Console.WriteLine();
Console.WriteLine("Release successful");
}

public static Release[] GetReleases(string userAgent)
public static Release[] GetReleases(string repo, string userAgent)
{
HttpWebRequest request = WebRequest.CreateHttp(@"https://api.github.com/repos/Rynchodon/ARMS/releases");
HttpWebRequest request = WebRequest.CreateHttp(@"https://api.github.com/repos/Rynchodon/" + repo + "/releases");
request.UserAgent = userAgent;
using (WebResponse response = request.GetResponse())
{
Expand Down
4 changes: 3 additions & 1 deletion Injector/Injector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
Expand All @@ -62,6 +62,8 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
4 changes: 2 additions & 2 deletions Injector/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("0.4.0.0")]
[assembly: AssemblyFileVersion("0.4.0.0")]
47 changes: 26 additions & 21 deletions Injector/Release.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,46 @@
namespace Rynchodon
{
[DataContract]
public class Release : IComparable<Release>
public class CreateRelease
{
#pragma warning disable CS0649
[DataContract]
public class Asset
[DataMember]
public string tag_name, body;
[DataMember]
public bool draft, prerelease;

public CreateRelease() { }

public CreateRelease(CreateRelease copy)
{
[DataMember]
public string name, browser_download_url;
this.tag_name = copy.tag_name;
this.body = copy.body;
this.draft = copy.draft;
this.prerelease = copy.prerelease;
}

public void WriteCreateJson(Stream writeTo)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CreateRelease));
serializer.WriteObject(writeTo, new CreateRelease(this));
}
}

[DataContract]
public class Release : CreateRelease, IComparable<Release>
{
[DataContract]
private class Create
public class Asset
{
[DataMember]
public string tag_name, body;
[DataMember]
public bool draft, prerelease;
public string name, browser_download_url;
}

[DataMember]
public string tag_name, name, body;
public string name;
[DataMember]
public long id;
[DataMember]
public bool draft, prerelease;
[DataMember]
public Asset[] assets;
#pragma warning restore CS0649

private Version value_version;

Expand All @@ -56,12 +68,5 @@ public int CompareTo(Release other)
return this.prerelease ? int.MinValue : int.MaxValue;
return this.Version.CompareTo(other.Version);
}

public void WriteCreateJson(Stream writeTo)
{
Create c = new Create() { tag_name = tag_name, body = body, draft = draft, prerelease = prerelease };
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Create));
serializer.WriteObject(writeTo, c);
}
}
}
8 changes: 4 additions & 4 deletions Injector/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public Version(string version)

public Version(FileVersionInfo version)
{
Major = Math.Min(version.FileMajorPart, version.ProductMajorPart);
Minor = Math.Min(version.FileMinorPart, version.ProductMinorPart);
Build = Math.Min(version.FileBuildPart, version.ProductBuildPart);
Revision = Math.Min(version.FilePrivatePart, version.ProductPrivatePart);
Major = Math.Max(version.FileMajorPart, version.ProductMajorPart);
Minor = Math.Max(version.FileMinorPart, version.ProductMinorPart);
Build = Math.Max(version.FileBuildPart, version.ProductBuildPart);
Revision = Math.Max(version.FilePrivatePart, version.ProductPrivatePart);
}

public int CompareTo(Version other)
Expand Down
10 changes: 9 additions & 1 deletion Readme.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
Downloads the latest ARMS scripts and loads them into Space Engineers. The scripts will run only alongside the models, which are distributed via steam.
The new process downloads the latest release of ARMS scripts and injects them into Space Engineers, bypassing the whitelist entirely.

Extend Whitelist will be removed when you run Load-ARMS.

The scripts will run only alongside the models, which are distributed via steam.

The scripts will run despite Space Engineer's error messages. The error messages will be fixed in the future.

Usage
=====
Unpack to ...\SpaceEngineers\Bin64, run LoadARMS.exe.
If the game is not running, LoadARMS will start it. If the game is running, ARMS will be loaded into the running game. You will need to reload any running world.
LoadARMS must be run every time you launch Space Engineers.

Dedicated Server
================
Unpack to ...\SpaceEngineers\DedicatedServer64, run LoadARMS.exe.
If the dedicated server is not running, LoadARMS will start the configurator. If the configurator is already running, LoadARMS will wait for the server to start. If the dedicated server is running, LoadARMS will load ARMS into the server.
LoadARMS must be run every time you launch Space Engineers.

0 comments on commit 041cc1c

Please sign in to comment.