Skip to content

Commit

Permalink
SE version tracking
Browse files Browse the repository at this point in the history
unstable releases are now stable when the version of SE it was compiled
with is stable
  • Loading branch information
Rynchodon committed Mar 23, 2017
1 parent 4c06e8e commit ec9c78f
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 37 deletions.
3 changes: 2 additions & 1 deletion Injector/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Rynchodon.Injector
class CommandLine
{

private enum OptionName : byte { help, author, repo, allBuilds, basedir, oAuthToken, publish, version }
private enum OptionName : byte { help, author, repo, allBuilds, basedir, oAuthToken, publish, version, seVersion }

private class Option
{
Expand Down Expand Up @@ -95,6 +95,7 @@ private static SortedDictionary<OptionName, Option> GetOptions()
opts.Add(OptionName.oAuthToken, new Option(new string[] { "--oAuthToken=" }, "personal access token used to log into GitHub, by default the value from the environment variable \"oAuthToken\" will be used", typeof(string)));
opts.Add(OptionName.publish, new Option(new string[] { "-p", "--publish" }, "publish the mod to GitHub"));
opts.Add(OptionName.version, new Option(new string[] { "-v=", "--version=" }, "the version of the mod, by default the version is the highest file version", typeof(string)));
opts.Add(OptionName.seVersion, new Option(new string[] { "--se=", "--sev=", "--seVersion" }, "the version of Space Engineers the mod was compiled against, by default get the version from SpaceEngineersGame.SE_VERSION", typeof(int)));

return opts;
}
Expand Down
3 changes: 2 additions & 1 deletion Injector/Injector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
<Import Project="$(SolutionDir)\global.config" />
<PropertyGroup>
<PostBuildEvent>copy ..\..\..\..\Loader\bin\x64\Release\LoadARMS.dll LoadARMS.dll
start cmd.exe /C ""$(ReferencePath)\LoadArms.exe" --author=Rynchodon --repo=Load-ARMS --allBuilds LoadARMS.exe LoadArms.dll "..\..\..\..\Load-ARMS Readme.txt" "..\..\..\..\Load-ARMS License.txt""</PostBuildEvent>

start cmd.exe /C ""$(ReferencePath)\LoadArms.exe" --author=Rynchodon --repo=Load-ARMS --allBuilds LoadARMS.exe LoadArms.dll "..\..\..\..\Load-ARMS Readme.txt" "..\..\..\..\Load-ARMS License.txt""</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PreBuildEvent>
Expand Down
2 changes: 1 addition & 1 deletion Injector/Properties/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// 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("0.5.3.0")]
[assembly: AssemblyVersion("0.5.4.0")]
33 changes: 33 additions & 0 deletions Loader/BuildTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Reflection;
using VRage.Game;

namespace Rynchodon.Loader
{
public static class BuildTest
{

/// <summary>
/// Check that the current Space Engineers build is stable.
/// </summary>
/// <returns>True iff the current Space Engineers build is stable.</returns>
public static bool IsStable()
{
FieldInfo field = typeof(MyFinalBuildConstants).GetField("IS_STABLE");
if (field == null)
throw new NullReferenceException("MyFinalBuildConstants does not have a field named IS_STABLE or it has unexpected binding");
return (bool)field.GetValue(null);
}

/// <summary>
/// Check that a version's build matches the current Space Engineers build.
/// </summary>
/// <param name="version">The version that may be compatible.</param>
/// <returns>True iff the version is compatible with the current Space Engineers build.</returns>
public static bool MatchesCurrent(Version version)
{
return IsStable() ? version.StableBuild : version.UnstableBuild;
}

}
}
37 changes: 28 additions & 9 deletions Loader/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using SpaceEngineers.Game;
using VRage.Game;

namespace Rynchodon.Loader
Expand Down Expand Up @@ -106,7 +108,7 @@ public bool PublishRelease(CreateRelease create, params string[] assetsPaths)
{
Logger.WriteLine("Failed to post asset(s)\n" + ex);
DeleteRelease(release);
throw ex;
throw;
}

if (!draft)
Expand Down Expand Up @@ -259,6 +261,8 @@ internal bool Update(ModInfo info, ModVersion current, string destinationDirecto
// already complained about it in depth
return false;

int seVersion = LoadArms.GetCurrentSEVersion();

Release mostRecent = null;
foreach (Release rel in releases)
{
Expand All @@ -267,9 +271,15 @@ internal bool Update(ModInfo info, ModVersion current, string destinationDirecto
if (rel.prerelease && !info.downloadPrerelease)
continue;

if (MyFinalBuildConstants.IS_STABLE ? rel.version.StableBuild : rel.version.UnstableBuild)
if (mostRecent == null || mostRecent.version.CompareTo(rel.version) < 0)
mostRecent = rel;
// skip if release was compiled with a newer version of SE
if (seVersion < rel.seVersion)
continue;

if (!BuildTest.MatchesCurrent(rel.version))
continue;

if (mostRecent == null || mostRecent.version.CompareTo(rel.version) < 0)
mostRecent = rel;
}

if (mostRecent == null)
Expand All @@ -278,26 +288,28 @@ internal bool Update(ModInfo info, ModVersion current, string destinationDirecto
return false;
}

Logger.WriteLine("Latest release version: " + mostRecent.version + ", Current version: " + current.version);

int relative = mostRecent.version.CompareTo(current.version);
if (relative == 0)
{
Logger.WriteLine("Up-to-date: " + current.version);
return false;
}
if (relative < 0)
if (relative < 0) // current version is newer than latest
{
if (current.locallyCompiled)
if (current.locallyCompiled && BuildTest.MatchesCurrent(current.version))
{
Logger.WriteLine("Locally compiled version: " + current.version);
Logger.WriteLine("Keeping locally compiled version: " + current.version);
return false;
}
// keep Load-ARMS from rolling back to a version that does not update itself
if (current.author == LoadArms.Rynchodon && current.repository == LoadArms.LoadArmsRepo && mostRecent.version.Major < 5)
if (current.author == LoadArms.Rynchodon && current.repository == LoadArms.LoadArmsRepo && mostRecent.version.Major < 1 && mostRecent.version.Minor < 5)
{
Logger.WriteLine("Cannot roll back to earlier version of LoadARMS");
return false;
}
Logger.WriteLine("Rolling back version: " + current.version);
Logger.WriteLine("Roll back version: " + current.version + " to " + mostRecent.version);
}

if (mostRecent.assets == null || mostRecent.assets.Length == 0)
Expand All @@ -306,6 +318,13 @@ internal bool Update(ModInfo info, ModVersion current, string destinationDirecto
return false;
}

// warn if a locally compiled version is going to be replaced by a downloaded version
if (current.locallyCompiled && MessageBox.Show("Mod: " + info.fullName + "\nLocally compiled version: " + current.version + "\nLatest release version: " + mostRecent.version + "\n\nOverwrite locally compiled mod?", "Warning", MessageBoxButtons.YesNo) == DialogResult.No)
{
Logger.WriteLine("Not overwriting locally compiled mod");
return false;
}

current.EraseAllFiles();

Logger.WriteLine("Downloading version: " + mostRecent.version);
Expand Down
24 changes: 21 additions & 3 deletions Loader/LoadArms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Sandbox;
using Sandbox.Engine.Platform;
using Sandbox.Graphics.GUI;
using SpaceEngineers.Game;
using VRage.Plugins;

namespace Rynchodon.Loader
Expand Down Expand Up @@ -104,7 +105,8 @@ public static void RunInSEProcess()
/// <param name="oAuthToken">Personal access token for GitHub, it may also be set as an environment variable.</param>
/// <param name="publish">If true, publish a release to GitHub</param>
/// <param name="versionString">The version of the release. If it is null, the highest version number from any file is used.</param>
public static void AddLocallyCompiledMod(IEnumerable<string> files, string author, string repo, bool allBuilds = false, string basedir = null, string oAuthToken = null, bool publish = false, string versionString = null)
/// <param name="seVersion">Version of Space Engineers the mod was compiled against.</param>
public static void AddLocallyCompiledMod(IEnumerable<string> files, string author, string repo, bool allBuilds = false, string basedir = null, string oAuthToken = null, bool publish = false, string versionString = null, int seVersion = 0)
{
if (_instance == null)
new LoadArms(false);
Expand All @@ -125,13 +127,28 @@ public static void AddLocallyCompiledMod(IEnumerable<string> files, string autho
}
}

if (seVersion < 1)
seVersion = GetCurrentSEVersion();

_instance.Load();
ModVersion modVersion = _instance.AddLocallyCompiled(name, version, files, basedir);
ModVersion modVersion = _instance.AddLocallyCompiled(name, version, seVersion, files, basedir);

if (publish && GitChecks.Check(basedir, _instance._config.PathToGit))
ReleaseCreater.Publish(modVersion, PathExtensions.Combine(_instance._directory, "mods", name.fullName), oAuthToken);
}

/// <summary>
/// Get the current Space Engineers version from SpaceEngineersGame.
/// </summary>
/// <returns>The current version of Space Engineers.</returns>
public static int GetCurrentSEVersion()
{
FieldInfo field = typeof(SpaceEngineersGame).GetField("SE_VERSION", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
if (field == null)
throw new NullReferenceException("SpaceEngineersGame does not have field SE_VERSION, or it has unexpected binding");
return (int)field.GetValue(null);
}

[DataContract]
private struct Config
{
Expand Down Expand Up @@ -469,7 +486,7 @@ private bool LoadPlugins(List<IPlugin> chainedPlugins, ModVersion mod, HashSet<M
return true;
}

private ModVersion AddLocallyCompiled(ModName name, Version version, IEnumerable<string> files, string baseDir)
private ModVersion AddLocallyCompiled(ModName name, Version version, int seVersion, IEnumerable<string> files, string baseDir)
{
int hashCode = name.GetHashCode();
ModVersion current;
Expand All @@ -479,6 +496,7 @@ private ModVersion AddLocallyCompiled(ModName name, Version version, IEnumerable
_data.ModsCurrentVersions.Add(hashCode, current);
}
current.version = version;
current.seVersion = seVersion;
Logger.WriteLine("mod: " + name.fullName + ", compiled version: " + current.version);
current.EraseAllFiles();

Expand Down
1 change: 1 addition & 0 deletions Loader/Loader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="..\Injector\Properties\VersionInfo.cs">
<Link>Properties\VersionInfo.cs</Link>
</Compile>
<Compile Include="BuildTest.cs" />
<Compile Include="DownloadProgress.cs" />
<Compile Include="GitChecks.cs" />
<Compile Include="GitHubClient.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Loader/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static void WriteLine(string line, [CallerFilePath] string callerPath =
_lineWriter(line, callerPath, memberName, lineNumber);
}

internal static void WriteLineToStream(string line, string callerPath = null, string memberName = null, int lineNumber = 0)
private static void WriteLineToStream(string line, string callerPath = null, string memberName = null, int lineNumber = 0)
{
if (logFile == null)
throw new NullReferenceException("logFile");
Expand Down
5 changes: 5 additions & 0 deletions Loader/ModInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -64,8 +65,12 @@ internal class ModInfo : ModName
[DataContract]
internal class ModVersion : ModName
{
/// <summary>The version of the mod.</summary>
[DataMember]
public Version version;
/// <summary>The version of Space Engineers the mod was compiled against.</summary>
[DataMember]
public int seVersion;
/// <summary>Allows version to be greater than latest release.</summary>
[DataMember]
public bool locallyCompiled;
Expand Down
36 changes: 23 additions & 13 deletions Loader/Release.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;

namespace Rynchodon.Loader
{
Expand Down Expand Up @@ -35,8 +36,6 @@ public class CreateRelease

[IgnoreDataMember]
private string _tag_name;
[IgnoreDataMember]
private Version _version;

/// <summary>Needs to be in a Version compatible format.</summary>
[DataMember]
Expand All @@ -45,25 +44,36 @@ public string tag_name
get { return _tag_name; }
set
{
Match match = Regex.Match(value, @"-SE(\d+)");
if (!match.Success)
seVersion = 0;
else
{
string group = match.Groups[1].Value;
seVersion = string.IsNullOrWhiteSpace(group) ? 0 : int.Parse(group);
}

_tag_name = value;
_version = new Version(value);
version = new Version(value);
}
}

/// <summary>Setting this value also sets tag_name.</summary>
[IgnoreDataMember]
public Version version
{
get { return _version; }
set
{
_version = value;
_tag_name = _version.ToString();
}
}
public Version version { get; private set; }

[IgnoreDataMember]
public int seVersion { get; private set; }

public CreateRelease() { }

public CreateRelease(Version modVersion, int seVersion, bool draft)
{
this.version = modVersion;
this.seVersion = seVersion;
this.tag_name = modVersion.ToString() + "-SE" + seVersion;
this.draft = draft;
}

/// <summary>
/// Special JSON writer for CreateRelease because GitHub won't take null for an answer.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions Loader/ReleaseCreater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private class Input
[DataMember]
public string ZipFileName = null;
[DataMember]
public CreateRelease Release = new CreateRelease();
public CreateRelease Release;
}

public static void Publish(ModVersion modVersion, string directory, string oAuthToken)
Expand All @@ -31,8 +31,8 @@ public static void Publish(ModVersion modVersion, string directory, string oAuth
throw new ArgumentException("Need oAuthToken");

Input input = new Input();
input.Release.version = modVersion.version;
input.Release.draft = true;
input.Release = new CreateRelease(modVersion.version, modVersion.seVersion, true);
string tag_name = input.Release.tag_name;

string zipTempFile = PathExtensions.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".zip");
Task compress = new Task(() => CompressFiles(zipTempFile, directory, modVersion.filePaths));
Expand Down Expand Up @@ -73,7 +73,7 @@ public static void Publish(ModVersion modVersion, string directory, string oAuth
}

// force release version to match mod version
input.Release.version = modVersion.version;
input.Release.tag_name = tag_name;

Console.WriteLine("Release created");
compress.Wait();
Expand Down
7 changes: 3 additions & 4 deletions Loader/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using VRage.Game;

namespace Rynchodon.Loader
{
Expand All @@ -27,15 +26,15 @@ public Version(FileVersionInfo info, bool allBuilds)
{
StableBuild = UnstableBuild = true;
}
else if ((bool)typeof(MyFinalBuildConstants).GetField("IS_STABLE").GetValue(null))
else if (BuildTest.IsStable())
{
StableBuild = true;
UnstableBuild = false;
}
else
{
StableBuild = false;
UnstableBuild = true;
// SE version will be checked, so stable will only get the release once it catches up
StableBuild = UnstableBuild = true;
}
}

Expand Down

0 comments on commit ec9c78f

Please sign in to comment.