Skip to content

Commit

Permalink
Refactor to reduce conditional code.
Browse files Browse the repository at this point in the history
Move most of the conditional compilation code to helper/extension methods.
  • Loading branch information
Gwindalmir committed Sep 4, 2021
1 parent ec7e8c4 commit 7752ed2
Show file tree
Hide file tree
Showing 9 changed files with 593 additions and 415 deletions.
1 change: 1 addition & 0 deletions SEWorkshopTool/SpaceGame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Phoenix.WorkshopTool;
using Phoenix.WorkshopTool.Extensions;
using Sandbox;
using Sandbox.Engine.Networking;
using Sandbox.Game;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using VRage;
using VRage.Game;
using VRage.GameServices;
#if SE
using Sandbox.Engine.Networking;
using VRage.Utils;
#if SE
using Error = VRage.Game.MyDefinitionErrors.Error;
using MyDebug = Phoenix.WorkshopTool.Extensions.MyDebug;
#else
using Error = VRage.Scripting.MyScriptCompiler.Message;
using VRage.Logging;
#endif

namespace Phoenix.WorkshopTool
namespace Phoenix.WorkshopTool.Extensions
{
#if SE
static class MyDebug
Expand Down Expand Up @@ -74,7 +80,7 @@ public static class MySteamHelper
}
}

public static class LoggingHelper
public static class LoggingExtensions
{
/// <summary>
/// Logs an exception.
Expand Down Expand Up @@ -114,9 +120,19 @@ public static void WriteLineWarning(this MyLog log, string msg)
}
}

public static class WorkshopIdHelper
public static class WorkshopIdExtensions
{
#if SE
public static ulong GetId(this VRage.Game.WorkshopId id)
{
return id.Id;
}

public static ulong[] GetIds(this VRage.Game.WorkshopId[] ids)
{
return ids.Select(i => i.Id).ToArray();
}

public static string AsString(this VRage.Game.WorkshopId[] ids)
{
var result = new StringBuilder();
Expand All @@ -131,15 +147,51 @@ public static string AsString(this VRage.Game.WorkshopId[] ids)

return result.ToString();
}

public static WorkshopId[] ToWorkshopIds(this IEnumerable<ulong> ids)
{
return ids.Select(id => new VRage.Game.WorkshopId(id, MyGameService.GetDefaultUGC().ServiceName)).ToArray();
}

public static WorkshopId ToWorkshopId(this ulong id)
{
return new VRage.Game.WorkshopId(id, MyGameService.GetDefaultUGC().ServiceName);
}
#else
public static string AsString(this ulong id)
// This is just a wrapper so the same method call can be used for either game.
public static ulong GetId(this ulong id)
{
return id;
}

public static ulong[] GetIds(this ulong[] ids)
{
return ids;
}

public static string AsString(this ulong[] id)
{
return id[0].ToString();
}

public static ulong[] ToWorkshopIds(this MyWorkshopItem[] items)
{
return id.ToString();
return items?.Select(i => i.Id)?.ToArray();
}

public static ulong[] ToWorkshopIds(this IEnumerable<ulong> ids)
{
return ids.ToArray();
}

public static ulong ToWorkshopId(this ulong id)
{
return id;
}
#endif
}

public static class ConsoleHelper
public static class ConsoleExtensions
{
public static bool IsInteractive(this TextWriter stream)
{
Expand Down Expand Up @@ -168,4 +220,74 @@ public static string GetChangelog(this Release release)
return release.Body.Substring(0, end).Trim();
}
}

public static class MyDefinitionErrorExtensions
{
public static string GetErrorText(this Error error)
{
#if SE
return error.Message;
#else
return error.Text;
#endif
}
}

public static class MyWorkshopExtensions
{
internal static void AddHiddenTags(this List<MyWorkshop.Category> categories, WorkshopType? type = null)
{
switch (type)
{
case WorkshopType.Mod:
#if SE
categories.Add(new MyWorkshop.Category() { Id = "campaign" });
categories.Add(new MyWorkshop.Category() { Id = "font" });
categories.Add(new MyWorkshop.Category() { Id = "noscripts" });
#endif
break;
case WorkshopType.Blueprint:
MyWorkshop.BlueprintCategories.ForEach(c => categories.Add(c));
#if SE
categories.Add(new MyWorkshop.Category() { Id = "large_grid" });
categories.Add(new MyWorkshop.Category() { Id = "small_grid" });
categories.Add(new MyWorkshop.Category() { Id = "safe" }); // Mod.io only?
#endif
break;
case WorkshopType.Scenario:
break;
case WorkshopType.World:
break;
case WorkshopType.IngameScript:
break;
case null:
#if SE
// 'obsolete' tag is always available, as is 'No Mods' and 'experimental'
categories.Add(new MyWorkshop.Category() { Id = "obsolete" });
categories.Add(new MyWorkshop.Category() { Id = "no mods" });
categories.Add(new MyWorkshop.Category() { Id = "experimental" });
#endif
break;
default:
MyDebug.FailRelease("Invalid category.");
break;
}
}
}

public static class MyDLCExtensions
{
public static uint TryGetDLC(this string dlc)
{
#if SE
Sandbox.Game.MyDLCs.MyDLC dlcvalue;
if (Sandbox.Game.MyDLCs.TryGetDLC(dlc, out dlcvalue))
return dlcvalue.AppId;
else
return 0;
#else
return 0;
#endif
}
}
}
41 changes: 41 additions & 0 deletions WorkshopToolCommon/Extensions/GameServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Steamworks;
using System;
using System.Collections.Generic;
using System.Text;
using VRage.GameServices;

namespace Phoenix.WorkshopTool.Extensions
{
static class GameServiceExtensions
{
public static bool GetRemoteStorageQuota(this IMyGameService service, out ulong totalBytes, out ulong availableBytes)
{
return SteamRemoteStorage.GetQuota(out totalBytes, out availableBytes);
}

public static int GetRemoteStorageFileCount(this IMyGameService service)
{
return SteamRemoteStorage.GetFileCount();
}

public static string GetRemoteStorageFileNameAndSize(this IMyGameService service, int fileIndex, out int fileSizeInBytes)
{
return SteamRemoteStorage.GetFileNameAndSize(fileIndex, out fileSizeInBytes);
}

public static bool IsRemoteStorageFilePersisted(this IMyGameService service, string file)
{
return SteamRemoteStorage.FilePersisted(file);
}

public static bool RemoteStorageFileForget(this IMyGameService service, string file)
{
return SteamRemoteStorage.FileForget(file);
}

public static bool DeleteFromCloud(this IMyGameService service, string fileName)
{
return SteamRemoteStorage.FileDelete(fileName);
}
}
}

0 comments on commit 7752ed2

Please sign in to comment.