Skip to content

Commit 33c1d10

Browse files
authored
Merge pull request #17 from TorchAPI/patch-11
Fixes the problem with !entities refresh
2 parents 13900f5 + da1b36f commit 33c1d10

3 files changed

Lines changed: 82 additions & 6 deletions

File tree

‎Essentials/Commands/EntityModule.cs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Sandbox.ModAPI;
1313
using Torch.Commands;
1414
using Torch.Commands.Permissions;
15+
using Torch.Utils;
1516
using VRage.Collections;
1617
using VRage.Game.Entity;
1718
using VRage.Game.ModAPI;
@@ -25,6 +26,21 @@ namespace Essentials
2526
[Category("entities")]
2627
public class EntityModule : CommandModule
2728
{
29+
#pragma warning disable 649
30+
[ReflectedGetter(Name = "m_clientStates")]
31+
private static Func<MyReplicationServer, IDictionary> _clientStates;
32+
33+
private const string CLIENT_DATA_TYPE_NAME = "VRage.Network.MyReplicationServer+ClientData, VRage";
34+
[ReflectedGetter(TypeName = CLIENT_DATA_TYPE_NAME, Name = "Replicables")]
35+
private static Func<object, MyConcurrentDictionary<IMyReplicable, MyReplicableClientData>> _replicables;
36+
37+
[ReflectedMethod(Name = "RemoveForClient", OverrideTypeNames = new[] { null, null, CLIENT_DATA_TYPE_NAME, null })]
38+
private static Action<MyReplicationServer, IMyReplicable, Endpoint, object, bool> _removeForClient;
39+
40+
[ReflectedMethod(Name = "ForceReplicable")]
41+
private static Action<MyReplicationServer, IMyReplicable, Endpoint> _forceReplicable;
42+
#pragma warning restore 649
43+
2844
[Command("refresh", "Resyncs all entities for the player running the command.")]
2945
[Permission(MyPromoteLevel.None)]
3046
public void Refresh()
@@ -34,7 +50,7 @@ public void Refresh()
3450

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

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

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

5670
foreach (var replicable in replicableList)
5771
{
58-
removeForClientMethod.Invoke(replicationServer, new object[] {replicable, playerEndpoint, clientData, true});
59-
forceReplicableMethod.Invoke(replicationServer, new object[] {replicable, playerEndpoint});
72+
_removeForClient.Invoke(replicationServer, replicable, playerEndpoint, clientData, true);
73+
_forceReplicable.Invoke(replicationServer, replicable, playerEndpoint);
6074
}
6175

6276
Context.Respond($"Forced replication of {replicableList.Count} entities.");

‎Jenkins/release.ps1‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
param([string] $ApiBase, [string]$tagName, [string]$authinfo, [string[]] $assetPaths)
2+
Add-Type -AssemblyName "System.Web"
3+
4+
$headers = @{
5+
Authorization = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($authinfo))
6+
Accept = "application/vnd.github.v3+json"
7+
}
8+
try
9+
{
10+
Write-Output("Checking if release with tag " + $tagName + " already exists...")
11+
$release = Invoke-RestMethod -Uri ($ApiBase+"releases/tags/$tagName") -Method "GET" -Headers $headers
12+
Write-Output(" Using existing release " + $release.id + " at " + $release.html_url)
13+
} catch {
14+
Write-Output(" Doesn't exist")
15+
$rel_arg = @{
16+
tag_name=$tagName
17+
name="Generated $tagName"
18+
body=""
19+
draft=$TRUE
20+
prerelease=$tagName.Contains("alpha") -or $tagName.Contains("beta")
21+
}
22+
Write-Output("Creating new release " + $tagName + "...")
23+
$release = Invoke-RestMethod -Uri ($ApiBase+"releases") -Method "POST" -Headers $headers -Body (ConvertTo-Json($rel_arg))
24+
Write-Output(" Created new release " + $tagName + " at " + $release.html_url)
25+
}
26+
27+
$assetsApiBase = $release.assets_url
28+
Write-Output("Checking for existing assets...")
29+
$existingAssets = Invoke-RestMethod -Uri ($assetsApiBase) -Method "GET" -Headers $headers
30+
$assetLabels = ($assetPaths | ForEach-Object {[System.IO.Path]::GetFileName($_)})
31+
foreach ($asset in $existingAssets) {
32+
if ($assetLabels -contains $asset.name) {
33+
$uri = $asset.url
34+
Write-Output(" Deleting old asset " + $asset.name + " (id " + $asset.id + "); URI=" + $uri)
35+
$result = Invoke-RestMethod -Uri $uri -Method "DELETE" -Headers $headers
36+
}
37+
}
38+
Write-Output("Uploading assets...")
39+
$uploadUrl = $release.upload_url.Substring(0, $release.upload_url.LastIndexOf('{'))
40+
foreach ($asset in $assetPaths) {
41+
$assetName = [System.IO.Path]::GetFileName($asset)
42+
$assetType = [System.Web.MimeMapping]::GetMimeMapping($asset)
43+
$assetData = [System.IO.File]::ReadAllBytes($asset)
44+
$headerExtra = $headers + @{
45+
"Content-Type" = $assetType
46+
Name = $assetName
47+
}
48+
$uri = $uploadUrl + "?name=" + $assetName
49+
Write-Output(" Uploading " + $asset + " as " + $assetType + "; URI=" + $uri)
50+
$result = Invoke-RestMethod -Uri $uri -Method "POST" -Headers $headerExtra -Body $assetData
51+
Write-Output(" ID=" + $result.id + ", found at=" + $result.browser_download_url)
52+
}

‎Jenkinsfile‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ node {
6565
stage('Archive') {
6666
archiveArtifacts artifacts: "bin/x64/Release/Essentials.dll", caseSensitive: false, fingerprint: true, onlyIfSuccessful: true
6767
}
68+
69+
gitVersion = bat(returnStdout: true, script: "@git describe --tags").trim()
70+
gitSimpleVersion = bat(returnStdout: true, script: "@git describe --tags --abbrev=0").trim()
71+
if (gitVersion == gitSimpleVersion) {
72+
stage('Release') {
73+
withCredentials([usernamePassword(credentialsId: 'torch-github', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
74+
powershell "& ./Jenkins/release.ps1 \"https://api.github.com/repos/TorchAPI/Essentials/\" \"$gitSimpleVersion\" \"$USERNAME:$PASSWORD\" @(\"bin/x64/Release/Essentials.dll\")"
75+
}
76+
}
77+
}
6878
}
6979
else
7080
currentBuild.result = "FAIL"

0 commit comments

Comments
 (0)