Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions Essentials/Commands/EntityModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Sandbox.ModAPI;
using Torch.Commands;
using Torch.Commands.Permissions;
using Torch.Utils;
using VRage.Collections;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
Expand All @@ -25,6 +26,21 @@ namespace Essentials
[Category("entities")]
public class EntityModule : CommandModule
{
#pragma warning disable 649
[ReflectedGetter(Name = "m_clientStates")]
private static Func<MyReplicationServer, IDictionary> _clientStates;

private const string CLIENT_DATA_TYPE_NAME = "VRage.Network.MyReplicationServer+ClientData, VRage";
[ReflectedGetter(TypeName = CLIENT_DATA_TYPE_NAME, Name = "Replicables")]
private static Func<object, MyConcurrentDictionary<IMyReplicable, MyReplicableClientData>> _replicables;

[ReflectedMethod(Name = "RemoveForClient", OverrideTypeNames = new[] { null, null, CLIENT_DATA_TYPE_NAME, null })]
private static Action<MyReplicationServer, IMyReplicable, Endpoint, object, bool> _removeForClient;

[ReflectedMethod(Name = "ForceReplicable")]
private static Action<MyReplicationServer, IMyReplicable, Endpoint> _forceReplicable;
#pragma warning restore 649

[Command("refresh", "Resyncs all entities for the player running the command.")]
[Permission(MyPromoteLevel.None)]
public void Refresh()
Expand All @@ -34,7 +50,7 @@ public void Refresh()

var playerEndpoint = new Endpoint(Context.Player.SteamUserId, 0);
var replicationServer = (MyReplicationServer)MyMultiplayer.ReplicationLayer;
var clientDataDict = typeof(MyReplicationServer).GetField("m_clientStates", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(replicationServer) as IDictionary;
var clientDataDict = _clientStates.Invoke(replicationServer);
object clientData;
try
{
Expand All @@ -45,18 +61,16 @@ public void Refresh()
return;
}

var clientReplicables = clientData.GetType().GetField("Replicables").GetValue(clientData) as MyConcurrentDictionary<IMyReplicable, MyReplicableClientData>;
var removeForClientMethod = typeof(MyReplicationServer).GetMethod("RemoveForClient", BindingFlags.Instance | BindingFlags.NonPublic);
var forceReplicableMethod = typeof(MyReplicationServer).GetMethod("ForceReplicable", BindingFlags.Instance | BindingFlags.NonPublic, null, new[] {typeof(IMyReplicable), typeof(Endpoint)}, null);
var clientReplicables = _replicables.Invoke(clientData);

var replicableList = new List<IMyReplicable>(clientReplicables.Count);
foreach (var pair in clientReplicables)
replicableList.Add(pair.Key);

foreach (var replicable in replicableList)
{
removeForClientMethod.Invoke(replicationServer, new object[] {replicable, playerEndpoint, clientData, true});
forceReplicableMethod.Invoke(replicationServer, new object[] {replicable, playerEndpoint});
_removeForClient.Invoke(replicationServer, replicable, playerEndpoint, clientData, true);
_forceReplicable.Invoke(replicationServer, replicable, playerEndpoint);
}

Context.Respond($"Forced replication of {replicableList.Count} entities.");
Expand Down
52 changes: 52 additions & 0 deletions Jenkins/release.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
param([string] $ApiBase, [string]$tagName, [string]$authinfo, [string[]] $assetPaths)
Add-Type -AssemblyName "System.Web"

$headers = @{
Authorization = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($authinfo))
Accept = "application/vnd.github.v3+json"
}
try
{
Write-Output("Checking if release with tag " + $tagName + " already exists...")
$release = Invoke-RestMethod -Uri ($ApiBase+"releases/tags/$tagName") -Method "GET" -Headers $headers
Write-Output(" Using existing release " + $release.id + " at " + $release.html_url)
} catch {
Write-Output(" Doesn't exist")
$rel_arg = @{
tag_name=$tagName
name="Generated $tagName"
body=""
draft=$TRUE
prerelease=$tagName.Contains("alpha") -or $tagName.Contains("beta")
}
Write-Output("Creating new release " + $tagName + "...")
$release = Invoke-RestMethod -Uri ($ApiBase+"releases") -Method "POST" -Headers $headers -Body (ConvertTo-Json($rel_arg))
Write-Output(" Created new release " + $tagName + " at " + $release.html_url)
}

$assetsApiBase = $release.assets_url
Write-Output("Checking for existing assets...")
$existingAssets = Invoke-RestMethod -Uri ($assetsApiBase) -Method "GET" -Headers $headers
$assetLabels = ($assetPaths | ForEach-Object {[System.IO.Path]::GetFileName($_)})
foreach ($asset in $existingAssets) {
if ($assetLabels -contains $asset.name) {
$uri = $asset.url
Write-Output(" Deleting old asset " + $asset.name + " (id " + $asset.id + "); URI=" + $uri)
$result = Invoke-RestMethod -Uri $uri -Method "DELETE" -Headers $headers
}
}
Write-Output("Uploading assets...")
$uploadUrl = $release.upload_url.Substring(0, $release.upload_url.LastIndexOf('{'))
foreach ($asset in $assetPaths) {
$assetName = [System.IO.Path]::GetFileName($asset)
$assetType = [System.Web.MimeMapping]::GetMimeMapping($asset)
$assetData = [System.IO.File]::ReadAllBytes($asset)
$headerExtra = $headers + @{
"Content-Type" = $assetType
Name = $assetName
}
$uri = $uploadUrl + "?name=" + $assetName
Write-Output(" Uploading " + $asset + " as " + $assetType + "; URI=" + $uri)
$result = Invoke-RestMethod -Uri $uri -Method "POST" -Headers $headerExtra -Body $assetData
Write-Output(" ID=" + $result.id + ", found at=" + $result.browser_download_url)
}
10 changes: 10 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ node {
stage('Archive') {
archiveArtifacts artifacts: "bin/x64/Release/Essentials.dll", caseSensitive: false, fingerprint: true, onlyIfSuccessful: true
}

gitVersion = bat(returnStdout: true, script: "@git describe --tags").trim()
gitSimpleVersion = bat(returnStdout: true, script: "@git describe --tags --abbrev=0").trim()
if (gitVersion == gitSimpleVersion) {
stage('Release') {
withCredentials([usernamePassword(credentialsId: 'torch-github', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
powershell "& ./Jenkins/release.ps1 \"https://api.github.com/repos/TorchAPI/Essentials/\" \"$gitSimpleVersion\" \"$USERNAME:$PASSWORD\" @(\"bin/x64/Release/Essentials.dll\")"
}
}
}
}
else
currentBuild.result = "FAIL"
Expand Down