Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional corepackage custom client-side files #14536

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Barotrauma.IO;

namespace Barotrauma
{
sealed class CustomFilesFile : OtherFile
{
public CustomFilesFile(ContentPackage contentPackage, ContentPath path) : base(contentPackage, path) { }

public static ContentPath MutateContentPath(ContentPath path)
{
if (File.Exists(path.FullPath)) { return path; }

string rawValueWithoutExtension()
=> Barotrauma.IO.Path.Combine(
Barotrauma.IO.Path.GetDirectoryName(path.RawValue ?? ""),
Barotrauma.IO.Path.GetFileNameWithoutExtension(path.RawValue ?? "")).CleanUpPath();

path = ContentPath.FromRaw(path.ContentPackage, rawValueWithoutExtension());
if (File.Exists(path.FullPath)) { return path; }

path = ContentPath.FromRaw(path.ContentPackage,
rawValueWithoutExtension() + ".zip");
return path;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using System.IO.Compression;

namespace Barotrauma
{
Expand Down Expand Up @@ -43,5 +44,48 @@ public CorePackage(XDocument doc, string path) : base(doc, path)
"Core package requires at least one of the following content types: " +
string.Join(", ", missingFileTypes.Select(t => t.Type.Name)));
}

public void TryToInstallCustomFiles()
{
#if CLIENT
string customFilesZip = Files.OfType<CustomFilesFile>().FirstOrDefault()?.Path?.FullPath;
if (!customFilesZip.IsNullOrEmpty())
{
// Convert these LocalizedStrings to normal strings so if the corepackage
// that's being switched over to changed one of these texts or one of them are missing it wont't sync.

var warningBox = new GUIMessageBox(headerText: TextManager.Get("Warning").ToString(),
text: TextManager.Get("ModCustomClientFilesAtYourOwnRisk").ToString(),
new LocalizedString[] { TextManager.Get("Yes").ToString(), TextManager.Get("No").ToString() });
warningBox.Buttons[0].OnClicked = (_, __) =>
{
warningBox.Close();
try
{
ZipFile.ExtractToDirectory(customFilesZip, AppDomain.CurrentDomain.BaseDirectory, true);
}
catch (Exception e)
{
DebugConsole.ThrowError($"An error occured while trying to install files: {e.Message}\n{e.StackTrace}");
return false;
}

var restartBox = new GUIMessageBox(TextManager.Get("restartrequiredlabel"), TextManager.Get("restartrequiredgeneric"));
restartBox.Buttons[0].OnClicked += (btn, userdata) =>
{
restartBox.Close();
return true;
};

return false;
};
warningBox.Buttons[1].OnClicked = (_, __) =>
{
warningBox.Close();
return false;
};
}
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ private static class BackupPackages
public static ImmutableArray<RegularPackage>? Regular;
}

public static void SetCore(CorePackage newCore) => SetCoreEnumerable(newCore).Consume();
public static void SetCore(CorePackage newCore)
{
newCore.TryToInstallCustomFiles();

SetCoreEnumerable(newCore).Consume();
}

public static IEnumerable<LoadProgress> SetCoreEnumerable(CorePackage newCore)
{
Expand Down