Skip to content

Commit

Permalink
fix permissions bug (closes #8); update for compatibility with asf >4…
Browse files Browse the repository at this point in the history
….2.0.0 (closes #9)
  • Loading branch information
Rudokhvist committed Apr 5, 2020
1 parent 790a6a6 commit e50b99d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
36 changes: 21 additions & 15 deletions ASFAchievementManager/ASFAchievementManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public sealed class ASFAchievementManager : IBotSteamClient, IBotCommand {
public void OnLoaded() => ASF.ArchiLogger.LogGenericInfo("ASF Achievement Manager Plugin by Ryzhehvost, powered by ginger cats");

public async Task<string> OnBotCommand([NotNull] Bot bot, ulong steamID, [NotNull] string message, string[] args) {
if (!bot.HasPermission(steamID, BotConfig.EPermission.Master)) {
return null;
}

switch (args.Length) {
case 0:
Expand All @@ -38,17 +35,17 @@ public async Task<string> OnBotCommand([NotNull] Bot bot, ulong steamID, [NotNul
default:
switch (args[0].ToUpperInvariant()) {
case "ALIST" when args.Length > 2:
return await ResponseAchievementList(args[1], Utilities.GetArgsAsText(args, 2, ",")).ConfigureAwait(false);
return await ResponseAchievementList(steamID, args[1], Utilities.GetArgsAsText(args, 2, ",")).ConfigureAwait(false);
case "ALIST":
return await ResponseAchievementList(bot, args[1]).ConfigureAwait(false);
return await ResponseAchievementList(steamID ,bot, args[1]).ConfigureAwait(false);
case "ASET" when args.Length > 3:
return await ResponseAchievementSet(args[1], args[2], Utilities.GetArgsAsText(args, 3, ","), true).ConfigureAwait(false);
return await ResponseAchievementSet(steamID, args[1], args[2], Utilities.GetArgsAsText(args, 3, ","), true).ConfigureAwait(false);
case "ASET" when args.Length > 2:
return await ResponseAchievementSet(bot, args[1], Utilities.GetArgsAsText(args, 2, ","), true).ConfigureAwait(false);
return await ResponseAchievementSet(steamID, bot, args[1], Utilities.GetArgsAsText(args, 2, ","), true).ConfigureAwait(false);
case "ARESET" when args.Length > 3:
return await ResponseAchievementSet(args[1], args[2], Utilities.GetArgsAsText(args, 3, ","), false).ConfigureAwait(false);
return await ResponseAchievementSet(steamID, args[1], args[2], Utilities.GetArgsAsText(args, 3, ","), false).ConfigureAwait(false);
case "ARESET" when args.Length > 2:
return await ResponseAchievementSet(bot, args[1], Utilities.GetArgsAsText(args, 2, ","), false).ConfigureAwait(false);
return await ResponseAchievementSet(steamID, bot, args[1], Utilities.GetArgsAsText(args, 2, ","), false).ConfigureAwait(false);
default:
return null;
}
Expand All @@ -65,7 +62,12 @@ public IReadOnlyCollection<ClientMsgHandler> OnBotSteamHandlersInit([NotNull] Bo

//Responses

private static async Task<string> ResponseAchievementList(Bot bot, string appids) {
private static async Task<string> ResponseAchievementList(ulong steamID, Bot bot, string appids) {

if (!bot.HasPermission(steamID, BotConfig.EPermission.Master)) {
return null;
}

string[] gameIDs = appids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

if (gameIDs.Length == 0) {
Expand Down Expand Up @@ -97,23 +99,27 @@ private static async Task<string> ResponseAchievementList(Bot bot, string appids

}

private static async Task<string> ResponseAchievementList(string botNames, string appids) {
private static async Task<string> ResponseAchievementList(ulong steamID, string botNames, string appids) {

HashSet<Bot> bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0)) {
return Commands.FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
}

IList<string> results = await Utilities.InParallel(bots.Select(bot => ResponseAchievementList(bot, appids))).ConfigureAwait(false);
IList<string> results = await Utilities.InParallel(bots.Select(bot => ResponseAchievementList(steamID, bot, appids))).ConfigureAwait(false);

List<string> responses = new List<string>(results.Where(result => !string.IsNullOrEmpty(result)));

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}


private static async Task<string> ResponseAchievementSet(Bot bot, string appid, string AchievementNumbers, bool set = true) {
private static async Task<string> ResponseAchievementSet(ulong steamID, Bot bot, string appid, string AchievementNumbers, bool set = true) {
if (!bot.HasPermission(steamID, BotConfig.EPermission.Master)) {
return null;
}

if (string.IsNullOrEmpty(AchievementNumbers)) {
return bot.Commands.FormatBotResponse(string.Format(Strings.ErrorObjectIsNull, nameof(AchievementNumbers)));
}
Expand Down Expand Up @@ -144,15 +150,15 @@ private static async Task<string> ResponseAchievementSet(Bot bot, string appid,
return bot.Commands.FormatBotResponse(await Task.Run<string>(() => AchievementHandler.SetAchievements(bot, appId, achievements, set)).ConfigureAwait(false));
}

private static async Task<string> ResponseAchievementSet(string botNames, string appid, string AchievementNumbers, bool set = true) {
private static async Task<string> ResponseAchievementSet(ulong steamID, string botNames, string appid, string AchievementNumbers, bool set = true) {

HashSet<Bot> bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0)) {
return Commands.FormatStaticResponse(string.Format(Strings.BotNotFound, botNames));
}

IList<string> results = await Utilities.InParallel(bots.Select(bot => ResponseAchievementSet(bot, appid, AchievementNumbers, set))).ConfigureAwait(false);
IList<string> results = await Utilities.InParallel(bots.Select(bot => ResponseAchievementSet(steamID, bot, appid, AchievementNumbers, set))).ConfigureAwait(false);

List<string> responses = new List<string>(results.Where(result => !string.IsNullOrEmpty(result)));

Expand Down
2 changes: 1 addition & 1 deletion ASFAchievementManager/ASFAchievementManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Authors>Ryzhehvost</Authors>
<AssemblyVersion>0.1.1.0</AssemblyVersion>
<AssemblyVersion>0.1.2.0</AssemblyVersion>
<TargetFrameworks>netcoreapp3.1;net48</TargetFrameworks>
</PropertyGroup>

Expand Down
9 changes: 1 addition & 8 deletions ASFAchievementManager/AchievementHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ private void HandleGetUserStatsResponse(IPacketMsg packetMsg) {
return;
}
ClientMsgProtobuf<CMsgClientGetUserStatsResponse> response = new ClientMsgProtobuf<CMsgClientGetUserStatsResponse>(packetMsg);
//store data
if (!response.Body.game_idSpecified) {
ASF.ArchiLogger.LogNullError("response.Body.game_id");
}
if (!Responses.TryAdd(response.Body.game_id,new StoredResponse{
Success = response.Body.eresult == 1,
Response = response.Body
Expand All @@ -61,9 +57,6 @@ private void HandleStoreUserStatsResponse(IPacketMsg packetMsg) {
return;
}
ClientMsgProtobuf<CMsgClientStoreUserStatsResponse> response = new ClientMsgProtobuf<CMsgClientStoreUserStatsResponse>(packetMsg);
if (!response.Body.game_idSpecified) {
ASF.ArchiLogger.LogNullError("response.Body.game_id");
}
if (!Responses.TryAdd(response.Body.game_id, new StoredResponse {
Success = response.Body.eresult == 1,
Response = null //we don't care about this, just need to know that request was successful
Expand All @@ -82,7 +75,7 @@ private void HandleStoreUserStatsResponse(IPacketMsg packetMsg) {
private List<StatData> ParseResponse(CMsgClientGetUserStatsResponse Response) {
List<StatData> result = new List<StatData>();
KeyValue KeyValues = new KeyValue();
if (Response.schemaSpecified && Response.schema != null) {
if (Response.schema != null) {
using (MemoryStream ms = new MemoryStream(Response.schema)) {
if (!KeyValues.TryReadAsBinary(ms)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsInvalid, nameof(Response.schema)));
Expand Down

0 comments on commit e50b99d

Please sign in to comment.