Skip to content

Commit

Permalink
Changed: Cleaned up projects with C#10 features.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Feb 7, 2022
1 parent 34e7616 commit c369eb2
Show file tree
Hide file tree
Showing 15 changed files with 561 additions and 604 deletions.
100 changes: 47 additions & 53 deletions Reloaded.Universal.Monitor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,66 @@
using System;
using System.Diagnostics;
using System.IO;
using Reloaded.Mod.Interfaces;
using Reloaded.Mod.Interfaces.Internal;
using Reloaded.Universal.Redirector.Interfaces;

namespace Reloaded.Universal.Monitor
namespace Reloaded.Universal.Monitor;

public class Program : IMod
{
public class Program : IMod
{
private const string RedirectorId = "reloaded.universal.redirector";
private const string RedirectorId = "reloaded.universal.redirector";

private ILogger _logger;
private IModLoader _modLoader;
private WeakReference<IRedirectorController> _redirectorController;
private bool _printLoading = true;
private ILogger _logger;
private IModLoader _modLoader;
private WeakReference<IRedirectorController> _redirectorController;
private bool _printLoading = true;

public static void Main() {}
public void Start(IModLoaderV1 loader)
{
#if DEBUG
Debugger.Launch();
#endif
_modLoader = (IModLoader)loader;
_logger = (ILogger)_modLoader.GetLogger();
public static void Main() {}
public void Start(IModLoaderV1 loader)
{
_modLoader = (IModLoader)loader;
_logger = (ILogger)_modLoader.GetLogger();

// Auto-subscribe on loaded redirector.
_modLoader.ModLoaded += ModLoaded;
SetupEventFromRedirector();
}
// Auto-subscribe on loaded redirector.
_modLoader.ModLoaded += ModLoaded;
SetupEventFromRedirector();
}

private void SetupEventFromRedirector()
private void SetupEventFromRedirector()
{
_redirectorController = _modLoader.GetController<IRedirectorController>();
if (_redirectorController != null &&
_redirectorController.TryGetTarget(out var target))
{
_redirectorController = _modLoader.GetController<IRedirectorController>();
if (_redirectorController != null &&
_redirectorController.TryGetTarget(out var target))
{
target.Loading += TargetOnLoading;
}
target.Loading += TargetOnLoading;
}
}

private void ModLoaded(IModV1 mod, IModConfigV1 modConfig)
{
if (modConfig.ModId == RedirectorId)
SetupEventFromRedirector();
}
private void ModLoaded(IModV1 mod, IModConfigV1 modConfig)
{
if (modConfig.ModId == RedirectorId)
SetupEventFromRedirector();
}

private void TargetOnLoading(string path)
{
if (_printLoading && !path.Contains("usb#vid") && !path.Contains("hid#vid"))
_logger.PrintMessage($"RII File Monitor: {path}", _logger.TextColor);
}
private void TargetOnLoading(string path)
{
if (_printLoading && !path.Contains("usb#vid") && !path.Contains("hid#vid"))
_logger.PrintMessage($"RII File Monitor: {path}", _logger.TextColor);
}

/* Mod loader actions. */
public void Suspend() => _printLoading = false;
public void Resume() => _printLoading = true;
/* Mod loader actions. */
public void Suspend() => _printLoading = false;
public void Resume() => _printLoading = true;

public void Unload()
public void Unload()
{
if (_redirectorController != null &&
_redirectorController.TryGetTarget(out var target))
{
if (_redirectorController != null &&
_redirectorController.TryGetTarget(out var target))
{
target.Loading -= TargetOnLoading;
}
target.Loading -= TargetOnLoading;
}

public bool CanUnload() => true;
public bool CanSuspend() => true;
public Action Disposing { get; }
}
}

public bool CanUnload() => true;
public bool CanSuspend() => true;
public Action Disposing { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ApplicationIcon />
<OutputType>WinExe</OutputType>
<StartupObject />
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
140 changes: 68 additions & 72 deletions Reloaded.Universal.Redirector.Interfaces/IRedirectorController.cs
Original file line number Diff line number Diff line change
@@ -1,87 +1,83 @@
using System;
namespace Reloaded.Universal.Redirector.Interfaces;

namespace Reloaded.Universal.Redirector.Interfaces
public interface IRedirectorControllerV4
{
public interface IRedirectorControllerV4
{
/// <summary>
/// Temporarily disables the redirector.
/// </summary>
void Disable();

/// <summary>
/// Re-enables the redirector after a temporary disable.
/// </summary>
void Enable();
}

public interface IRedirectorControllerV3
{
/// <summary>
/// Adds a folder for file redirection, specifying what folder inside the game directory the folder should map to.
/// </summary>
/// <param name="folderPath">The full path of the folder to redirect files to.</param>
/// <param name="sourceFolder">Folder path relative to game's directory to redirect files from. Path should start with back or forward slash.</param>
/// <returns>True if the operation succeeds, else false. Operation fails if a folder is already redirected.</returns>
void AddRedirectFolder(string folderPath, string sourceFolder);
/// <summary>
/// Temporarily disables the redirector.
/// </summary>
void Disable();

/// <summary>
/// Removes a folder to be redirected.
/// The files inside the folder are mapped relative to the executable directory of the modified application's executable.
/// </summary>
/// <returns>True if the operation succeeds, else false.</returns>
void RemoveRedirectFolder(string folderPath, string sourceFolder);
}
/// <summary>
/// Re-enables the redirector after a temporary disable.
/// </summary>
void Enable();
}

public interface IRedirectorControllerV2
{
/// <summary>
/// Adds a folder for file redirection.
/// The files inside the folder are mapped relative to the executable directory of the modified application's executable.
/// </summary>
/// <param name="folderPath">The full path of the folder to redirect files to.</param>
/// <returns>True if the operation succeeds, else false. Operation fails if a folder is already redirected.</returns>
void AddRedirectFolder(string folderPath);
public interface IRedirectorControllerV3
{
/// <summary>
/// Adds a folder for file redirection, specifying what folder inside the game directory the folder should map to.
/// </summary>
/// <param name="folderPath">The full path of the folder to redirect files to.</param>
/// <param name="sourceFolder">Folder path relative to game's directory to redirect files from. Path should start with back or forward slash.</param>
/// <returns>True if the operation succeeds, else false. Operation fails if a folder is already redirected.</returns>
void AddRedirectFolder(string folderPath, string sourceFolder);

/// <summary>
/// Removes a folder to be redirected.
/// The files inside the folder are mapped relative to the executable directory of the modified application's executable.
/// </summary>
/// <returns>True if the operation succeeds, else false.</returns>
void RemoveRedirectFolder(string folderPath);
}
/// <summary>
/// Removes a folder to be redirected.
/// The files inside the folder are mapped relative to the executable directory of the modified application's executable.
/// </summary>
/// <returns>True if the operation succeeds, else false.</returns>
void RemoveRedirectFolder(string folderPath, string sourceFolder);
}

public interface IRedirectorController : IRedirectorControllerV2, IRedirectorControllerV3, IRedirectorControllerV4
{
Redirecting Redirecting { get; set; }
Loading Loading { get; set; }
public interface IRedirectorControllerV2
{
/// <summary>
/// Adds a folder for file redirection.
/// The files inside the folder are mapped relative to the executable directory of the modified application's executable.
/// </summary>
/// <param name="folderPath">The full path of the folder to redirect files to.</param>
/// <returns>True if the operation succeeds, else false. Operation fails if a folder is already redirected.</returns>
void AddRedirectFolder(string folderPath);

/// <summary>
/// Adds a file to be redirected.
/// </summary>
/// <param name="oldFilePath">The absolute path of the file to be replaced. Tip: Use Path.GetFullPath()</param>
/// <param name="newFilePath">The absolute path to the new file. Tip: Use Path.GetFullPath()</param>
void AddRedirect(string oldFilePath, string newFilePath);
/// <summary>
/// Removes a folder to be redirected.
/// The files inside the folder are mapped relative to the executable directory of the modified application's executable.
/// </summary>
/// <returns>True if the operation succeeds, else false.</returns>
void RemoveRedirectFolder(string folderPath);
}

/// <summary>
/// Removes a file from being redirected.
/// </summary>
/// <param name="oldFilePath">The absolute path of the file to no longer be replaced. Tip: Use Path.GetFullPath()</param>
void RemoveRedirect(string oldFilePath);
}
public interface IRedirectorController : IRedirectorControllerV2, IRedirectorControllerV3, IRedirectorControllerV4
{
Redirecting Redirecting { get; set; }
Loading Loading { get; set; }

/// <summary>
/// Called when a file is about to be redirected.
/// Adds a file to be redirected.
/// </summary>
/// <param name="oldPath">The path that was originally going to be opened.</param>
/// <param name="newPath">The new path of the file.</param>
public delegate void Redirecting(string oldPath, string newPath);
/// <param name="oldFilePath">The absolute path of the file to be replaced. Tip: Use Path.GetFullPath()</param>
/// <param name="newFilePath">The absolute path to the new file. Tip: Use Path.GetFullPath()</param>
void AddRedirect(string oldFilePath, string newFilePath);

/// <summary>
/// Called when a file with a specific file is going to be loaded.
/// Note: This is before redirection takes place, see <see cref="Redirecting"/> if you want to know redirected paths.
/// Removes a file from being redirected.
/// </summary>
/// <param name="path">The path to be loaded.</param>
public delegate void Loading(string path);

/// <param name="oldFilePath">The absolute path of the file to no longer be replaced. Tip: Use Path.GetFullPath()</param>
void RemoveRedirect(string oldFilePath);
}

/// <summary>
/// Called when a file is about to be redirected.
/// </summary>
/// <param name="oldPath">The path that was originally going to be opened.</param>
/// <param name="newPath">The new path of the file.</param>
public delegate void Redirecting(string oldPath, string newPath);

/// <summary>
/// Called when a file with a specific file is going to be loaded.
/// Note: This is before redirection takes place, see <see cref="Redirecting"/> if you want to know redirected paths.
/// </summary>
/// <param name="path">The path to be loaded.</param>
public delegate void Loading(string path);
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseExpression />
<Version>1.1.0</Version>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit c369eb2

Please sign in to comment.