74f32d14d4
commit 2ad9b5de40f128e8413f1ad5f568a4bc006d0971 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 20:11:41 2019 +0200 v0.8.9.3 commit 2d64da9d1e0d10f9ccb08d6e3fbc45d69fcda368 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 18:08:10 2019 +0200 Randomize the races and the heads in the net lobby. TODO: remember the previous heads and check that the next in line is unique (not exactly the same as the current). commit 09f74ab37692291ca070bac5dbc53eef395fb1cf Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 16:30:16 2019 +0200 AI characters can give treatment to characters who aren't unconscious, fixed AI characters attempting to treat dead characters. Closes #1083 commit a8fea2450cf4ef0b80a8fc809aee339bc39b28c6 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 16:28:23 2019 +0200 Fixed horizontal docking ports failing to find the hull prefab commit dd5a7a4f09195ecfeb82605e1e73ea22607694e7 Author: ezjamsen <ezjames.fi@gmail.com> Date: Wed Feb 6 16:02:52 2019 +0200 halved the hitpoints for baby moloch commit 92ae260558ece991560d83819168ef51d5267a66 Merge: 75fe087d7 8f451825e Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:44:08 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 75fe087d7263b46611fb0aad5c91c616e22ad41b Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:43:12 2019 +0200 Update the ai block in the moloch boss config file. commit caf11944294dfe66b5d302888baecbfddad7293a Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:42:42 2019 +0200 Remove the lowercase version of the moloch boss config file. commit b422feece1e620c427fa62d41c2b847b6b8837e9 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:41:40 2019 +0200 Fix molochbaby character name in the config file. commit 913762516dd17921c185de2cb912333f2e5230dd Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:41:22 2019 +0200 Include the baby moloch files in the project. commit 8f451825e95b415b1262f984d83828c379d36ea2 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 15:36:50 2019 +0200 Added molochbaby to the project file commit 2078307f255f432c19fcbbc0b690acf5655f3c99 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:33:08 2019 +0200 Disable creating humanoids in the wizard for now, because it can go wrong in so many ways. commit 55a931795ac5f60699033c4ff484a9a2d0ae9b7e Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:32:18 2019 +0200 Unfreeze when adjusting the joint scale, because the limbs are not otherwise updated to the joint positions. commit 2c92373ccbf331c545c830c9780f450b116f0359 Author: Iiro Enges <iiro@fakefish.fi> Date: Wed Feb 6 15:28:22 2019 +0200 Tinted baby moloch to differentiate from the adult version (for testing purposes) commit f05be351cdf8a6073702e988ed1861f665399048 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 15:26:52 2019 +0200 Fixed automatic temperature control setting turbine output above 100 if the power consumption is higher than what the reactor can generate. Caused "failed to write an event for the entity" errors in MP when the server tried to write the targetTurbineOutput using WriteRangedSingle with a maximum value of 100. Closes #1085 commit 7b737ebbea41fd5b7825a6c6373e578c1b1c91fd Author: ezjamsen <ezjames.fi@gmail.com> Date: Wed Feb 6 15:15:59 2019 +0200 Baby moloch, doo doo doo doo doo doo commit f5d13649915ba761bbc6fe78370c607905e2d5f8 Author: ezjamsen <ezjames.fi@gmail.com> Date: Wed Feb 6 15:15:04 2019 +0200 Small fixes and tweaks to gun performance dependent on skill of user. commit 4bf61738c6da08d771dedf1c96160cc76e0b4dd7 Author: ezjamsen <ezjames.fi@gmail.com> Date: Wed Feb 6 15:14:11 2019 +0200 New staggered swarm medium difficulty event commit 3e593f46850987ba074d9b26d59d08eeeddd1d70 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 15:05:43 2019 +0200 Removed incorrect item name/description instructions fron EnglishVanilla.xml. Correct instructions are at the beginning of the file. commit ce33a523e440a5b5037b84ca96bebf1f977e0aa8 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 15:00:48 2019 +0200 Attempt to fix "attempted to move pull joint extremely far" errors (#895). I think they were (at least for the most part) caused by characters switching to simple physics mode and then moving a significant distance, and when the character switched back to normal animation, the pull joints were still somewhere far away and lerping them to the position of the collider failed (because the lerped position triggers the error). commit c8212e3eff6d9272c9d38b833743b0f33b3611a2 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 13:54:15 2019 +0200 Fixed nullref exception when teleporting from a sub to ruins or vice versa. Closes #1037 commit 0d812394944eb05be4291be7b56593cc4def6d0f Author: Daniel Asteljoki <daniel.asteljoki@gmail.com> Date: Wed Feb 6 13:34:58 2019 +0200 Made regular ruin walls much more durable (500 -> 5000) commit c9a4d9d6f6c9c767ecd8cf34f917db9da852f4bf Author: Daniel Asteljoki <daniel.asteljoki@gmail.com> Date: Wed Feb 6 13:34:26 2019 +0200 Rebalanced Humpback battery grid, added toggles for using backup power commit 47617a6d54f933714dffeca5fa311776e9d0523c Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:17:57 2019 +0200 Remove the lowercase variant of moloch's config file from the source control. commit 640662835684464e4a66b106b1958e5a9fde8a0c Merge: 80e91f96b 7861d00a6 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:10:00 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 80e91f96bf5f1e9f89c5a6e4695292298a802b06 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:09:37 2019 +0200 Fix a null reference exception. commit 91b16aaa7aacc658cb36325ea85944802166ef53 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:09:26 2019 +0200 Instead of having a bad sight, make the Hammerhead deaf. Add "provocative" priority tag, remove "sonar". Make slightly less aggressive towards weaker. commit a383c136f6f6915163e98c472ec7ba7f575ea479 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:02:00 2019 +0200 Add "provocative" tag on Moloch and Mudraptor targeting priorities. Tweak moloch priorities a bit. commit 7861d00a609f2555a19f510d72591bd0330f607e Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 12:53:37 2019 +0200 Attempt to fix/diagnose "collection was modified; enumeration operation may not execute" exceptions when using the fixhulls command (see #1011). Haven't been able to reproduce this issue. commit 1d1321c60e8815b3beaffb81bfb9c5f5c2528297 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:52:29 2019 +0200 Add "provocative" tag that can be used as a red flag for the monsters. Add the tag on the diving scooter. Also add an ai target with sound ranges. commit 8530f4c9e4b1c444687cadbf615a9ade0440262b Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:50:43 2019 +0200 Propulsion can now make sound that attracts the enemies. The sound is on when the propulsion is active. commit 291f12676e5148da1a83d4dd72071381c1ae73e3 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:49:39 2019 +0200 If a target item is held by a character, attack the character instead. commit 2aeaa9eaa51f3ed3db71f7853efef37bf5998838 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:46:27 2019 +0200 Allow to define min and max sound ranges. commit 3bf5d2383cbcc33fa862a79c06756e6d9f20a753 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:42:57 2019 +0200 Adjust the ai debug info. commit 369355ffef340875335c0d81d9a2c9a4bfac898b Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 12:40:30 2019 +0200 ContextualTutorial Update cleanup commit 101387adc1308ad87e3134ab2960d3032f725735 Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 12:34:07 2019 +0200 Formatting commit cc44eff21d57d93e4b0a67249af2135e5782e3c3 Merge: 9a338a2d4 33d1cd1be Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 12:31:54 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 9a338a2d440ff12426171fe9c3121efb934b6803 Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 12:31:50 2019 +0200 Modified: ContextualTutorial dismissal logic to use buttons instead of the press any key logic commit 33d1cd1beb61862a91bce16ebeb5090a8969cf1b Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 12:07:21 2019 +0200 Fixed spectate button staying disabled if starting a round fails (due to a missing sub file for example). Closes #1066 commit 8935825146203725759463278e7776a42d84a6a0 Merge: 2cdc96214 9fd3de63b Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 11:45:06 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 2cdc96214e2285bfa83a566ed8bc2aed4156c31d Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 11:44:56 2019 +0200 Added: Saving of the download size to the Workshop.Item, allows displaying the file size for non-downloaded content in SteamWorshopScreen. Closes #1052 commit 9fd3de63bc08e06a924bf5ec8a8b2938a647282f Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 11:34:38 2019 +0200 Watchmen cannot be dragged or grabbed, fixed watchmen imploding continuously if they end up outside. Closes #1072 commit eb3fca43d77e79f04e0ae80daac53b7e2c1e37f7 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 11:14:06 2019 +0200 Fixed server not sending PowerContainer events until the charge of the PowerContainer has changed by more then 100% (i.e. never). Closes #1068 commit 5b3b71bab75489714f72ce55621f6da7d5ec8d94 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 10:59:09 2019 +0200 Prevent locations from being generated too close to each other. Closes #1048 commit b032b36a50b60432f565cab0178de749585562ad Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 10:48:43 2019 +0200 Fixed a couple of errors in the translation instructions in EnglishVanilla.xml commit a31b2f257ef863f2cdd1504a3a15356f95262225 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 09:34:11 2019 +0200 Show inventory icons in the sprite editor. commit fb7a69d6179824dde0a44d27ce5143a1a25d0147 Author: Roni Kovaniemi <roni.kovaniemi@gmail.com> Date: Wed Feb 6 02:18:44 2019 +0200 Improved background smoke texture commit 888e86a0e7d6e6a70feb2b87ea16cee17adcfd76 Author: Juan Pablo Arce <juanjp600@users.noreply.github.com> Date: Tue Feb 5 11:36:06 2019 -0300 whoops strikes again commit 67e94c6bb9e59b20bc1b9f3cb796780d1c2141f7 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Tue Feb 5 16:32:45 2019 +0200 Adjust the min and max values for the sight range and add a todo note. commit ad0becdf175a66c963b2cbea5259b435c250ffa6 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Tue Feb 5 16:31:47 2019 +0200 Refactor the sound range calculation. It was way off. commit adc0aa96be5426dbb845e4138acab87c44c935ba Author: itchyOwl <lauri.harkanen@gmail.com> Date: Tue Feb 5 16:31:18 2019 +0200 Improve the ai target debug info. commit 9230ba0865896d5740898993ab3389659a4cbb4a Author: Juan Pablo Arce <juanjp600@users.noreply.github.com> Date: Tue Feb 5 10:53:51 2019 -0300 Attempted audio buffer queue fix commit dfbc61628de37a60acbaa208934957a9d892eb99 Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 15:18:34 2019 +0200 Fixed: Rubber ducks not floating like a good duck should commit 8b8523dad99b5c1db271ac13e48975487a7c7c99 Merge: 509c67594 c0156c3d3 Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 14:34:59 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 509c6759484750d44a154a7f1703f92d0d6e1692 Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 14:34:53 2019 +0200 Fixed: Holdable items retaining their pushing ability even when the user is stunned or unconscious. Closes #1061 commit c0156c3d350b64309c8068f437ddd57765f0b911 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Tue Feb 5 13:46:32 2019 +0200 If no limb is selected, select the limb with the most critical affliction. Re-evaluate when new affliction is received. Sort afflictions so that dmg/sec is prioritized. Implements the feature request #1033. commit 4a056c6e9b78f3d6f43574b1b9ff5493d36565b0 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Tue Feb 5 13:25:57 2019 +0200 Fixed order messages not being visible in single player if the character issuing the order has no headset. Closes #1030 commit 357b14fb4f4b1a56a04d192c17a2ce8e0ab7405e Author: ezjamsen <ezjames.fi@gmail.com> Date: Tue Feb 5 12:52:40 2019 +0200 new load tips commit 1f158760e1ed79968f10647c0a2931d2c603281e Author: ezjamsen <ezjames.fi@gmail.com> Date: Tue Feb 5 12:52:20 2019 +0200 minor formatting tweak to xml commit c53438151043174e18fbfe979ac3b3b862d24255 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Tue Feb 5 12:48:47 2019 +0200 Stop Steam auth session of a client when they disconnect. Probably fixes servers reporting incorrect player counts (#1049)? commit 2073b170e4fdd5cedd998ff9d01f37842995194b Merge: 9914631c7 9b912a7bf Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 12:43:05 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 9914631c7eb30c02913cd4ed44f7b7827169ad83 Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 12:42:53 2019 +0200 Fixed: Fabricator allowing new items to be created when the output is not empty, resulting in wasted materials. commit 9b912a7bf16bd121bbe8b7eae20f4a253a9e1eba Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Tue Feb 5 12:33:00 2019 +0200 Fixed LOS effect not working on ruins when looking at them from inside a sub commit aeaf190c6d3b45d4d12654926acbe223dc4f5888 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Tue Feb 5 11:57:17 2019 +0200 Pressing the radio chat hotkey doesn't deselect the chatbox. Closes #1044
1055 lines
43 KiB
C#
1055 lines
43 KiB
C#
using Barotrauma.Networking;
|
|
using Facepunch.Steamworks;
|
|
using Microsoft.Xna.Framework;
|
|
using RestSharp.Extensions.MonoHttp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Barotrauma.Steam
|
|
{
|
|
class SteamManager
|
|
{
|
|
#if DEBUG
|
|
public static bool USE_STEAM
|
|
{
|
|
get { return GameMain.Config.UseSteam; }
|
|
}
|
|
#else
|
|
//cannot enable/disable steam in release builds
|
|
public const bool USE_STEAM = true;
|
|
#endif
|
|
|
|
public const uint AppID = 602960;
|
|
|
|
private Facepunch.Steamworks.Client client;
|
|
private Server server;
|
|
|
|
private Dictionary<string, int> tagCommonness = new Dictionary<string, int>()
|
|
{
|
|
{ "submarine", 10 },
|
|
{ "item", 10 },
|
|
{ "monster", 8 },
|
|
{ "art", 8 },
|
|
{ "mission", 8 },
|
|
{ "environment", 5 }
|
|
};
|
|
|
|
private List<string> popularTags = new List<string>();
|
|
public static IEnumerable<string> PopularTags
|
|
{
|
|
get
|
|
{
|
|
if (instance == null || !instance.isInitialized) { return Enumerable.Empty<string>(); }
|
|
return instance.popularTags;
|
|
}
|
|
}
|
|
|
|
private static SteamManager instance;
|
|
public static SteamManager Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null) instance = new SteamManager();
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
private bool isInitialized;
|
|
public static bool IsInitialized
|
|
{
|
|
get
|
|
{
|
|
return Instance.isInitialized;
|
|
}
|
|
}
|
|
|
|
public static void Initialize()
|
|
{
|
|
if (!USE_STEAM) return;
|
|
instance = new SteamManager();
|
|
}
|
|
|
|
private SteamManager()
|
|
{
|
|
try
|
|
{
|
|
client = new Facepunch.Steamworks.Client(AppID);
|
|
isInitialized = client.IsSubscribed && client.IsValid;
|
|
|
|
if (isInitialized)
|
|
{
|
|
DebugConsole.Log("Logged in as " + client.Username + " (SteamID " + client.SteamId + ")");
|
|
}
|
|
}
|
|
catch (DllNotFoundException e)
|
|
{
|
|
isInitialized = false;
|
|
#if CLIENT
|
|
new Barotrauma.GUIMessageBox(TextManager.Get("Error"), TextManager.Get("SteamDllNotFound"));
|
|
#else
|
|
DebugConsole.ThrowError("Initializing Steam client failed (steam_api64.dll not found).", e);
|
|
#endif
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
isInitialized = false;
|
|
#if CLIENT
|
|
new Barotrauma.GUIMessageBox(TextManager.Get("Error"), TextManager.Get("SteamClientInitFailed"));
|
|
#else
|
|
DebugConsole.ThrowError("Initializing Steam client failed.", e);
|
|
#endif
|
|
}
|
|
|
|
if (!isInitialized)
|
|
{
|
|
try
|
|
{
|
|
|
|
Facepunch.Steamworks.Client.Instance.Dispose();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (GameSettings.VerboseLogging) DebugConsole.ThrowError("Disposing Steam client failed.", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static ulong GetSteamID()
|
|
{
|
|
if (instance == null || !instance.isInitialized)
|
|
{
|
|
return 0;
|
|
}
|
|
return instance.client.SteamId;
|
|
}
|
|
|
|
public static ulong GetWorkshopItemIDFromUrl(string url)
|
|
{
|
|
try
|
|
{
|
|
Uri uri = new Uri(url);
|
|
string idStr = HttpUtility.ParseQueryString(uri.Query).Get("id");
|
|
if (ulong.TryParse(idStr, out ulong id))
|
|
{
|
|
return id;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugConsole.ThrowError("Failed to get Workshop item ID from the url \""+url+"\"!", e);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static bool UnlockAchievement(string achievementName)
|
|
{
|
|
if (instance == null || !instance.isInitialized)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
DebugConsole.Log("Unlocked achievement \"" + achievementName + "\"");
|
|
|
|
bool unlocked = instance.client.Achievements.Trigger(achievementName);
|
|
if (!unlocked)
|
|
{
|
|
//can be caused by an incorrect identifier, but also happens during normal gameplay:
|
|
//SteamAchievementManager tries to unlock achievements that may or may not exist
|
|
//(discovered[whateverbiomewasentered], kill[withwhateveritem], kill[somemonster] etc) so that we can add
|
|
//some types of new achievements without the need for client-side changes.
|
|
#if DEBUG
|
|
DebugConsole.NewMessage("Failed to unlock achievement \"" + achievementName + "\".");
|
|
#endif
|
|
}
|
|
|
|
return unlocked;
|
|
}
|
|
|
|
|
|
public static bool IncrementStat(string statName, int increment)
|
|
{
|
|
if (instance == null || !instance.isInitialized) { return false; }
|
|
DebugConsole.Log("Incremented stat \"" + statName + "\" by " + increment);
|
|
bool success = instance.client.Stats.Add(statName, increment);
|
|
if (!success)
|
|
{
|
|
#if DEBUG
|
|
DebugConsole.NewMessage("Failed to increment stat \"" + statName + "\".");
|
|
#endif
|
|
}
|
|
return success;
|
|
}
|
|
|
|
public static bool IncrementStat(string statName, float increment)
|
|
{
|
|
if (instance == null || !instance.isInitialized) { return false; }
|
|
DebugConsole.Log("Incremented stat \"" + statName + "\" by " + increment);
|
|
bool success = instance.client.Stats.Add(statName, increment);
|
|
if (!success)
|
|
{
|
|
#if DEBUG
|
|
DebugConsole.NewMessage("Failed to increment stat \"" + statName + "\".");
|
|
#endif
|
|
}
|
|
return success;
|
|
}
|
|
|
|
|
|
#region Connecting to servers
|
|
|
|
public static bool GetServers(Action<Networking.ServerInfo> onServerFound, Action<Networking.ServerInfo> onServerRulesReceived, Action onFinished)
|
|
{
|
|
if (instance == null || !instance.isInitialized)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var filter = new ServerList.Filter
|
|
{
|
|
{ "appid", AppID.ToString() },
|
|
{ "gamedir", "Barotrauma" },
|
|
{ "secure", "1" }
|
|
};
|
|
|
|
//include unresponsive servers in the server list
|
|
|
|
//the response is queried using the server's query port, not the game port,
|
|
//so it may be possible to play on the server even if it doesn't respond to server list queries
|
|
var query = instance.client.ServerList.Internet(filter);
|
|
query.OnUpdate += () => { UpdateServerQuery(query, onServerFound, onServerRulesReceived, includeUnresponsive: true); };
|
|
query.OnFinished = onFinished;
|
|
|
|
var localQuery = instance.client.ServerList.Local(filter);
|
|
localQuery.OnUpdate += () => { UpdateServerQuery(localQuery, onServerFound, onServerRulesReceived, includeUnresponsive: true); };
|
|
localQuery.OnFinished = onFinished;
|
|
|
|
return true;
|
|
}
|
|
|
|
private static void UpdateServerQuery(ServerList.Request query, Action<Networking.ServerInfo> onServerFound, Action<Networking.ServerInfo> onServerRulesReceived, bool includeUnresponsive)
|
|
{
|
|
IEnumerable<ServerList.Server> servers = includeUnresponsive ?
|
|
new List<ServerList.Server>(query.Responded).Concat(query.Unresponsive) :
|
|
query.Responded;
|
|
|
|
foreach (ServerList.Server s in servers)
|
|
{
|
|
if (!ValidateServerInfo(s)) { continue; }
|
|
|
|
bool responded = query.Responded.Contains(s);
|
|
if (responded)
|
|
{
|
|
DebugConsole.Log(s.Name + " responded to server query.");
|
|
}
|
|
else
|
|
{
|
|
DebugConsole.Log(s.Name + " did not respond to server query.");
|
|
}
|
|
var serverInfo = new ServerInfo()
|
|
{
|
|
ServerName = s.Name,
|
|
Port = s.ConnectionPort.ToString(),
|
|
IP = s.Address.ToString(),
|
|
PlayerCount = s.Players,
|
|
MaxPlayers = s.MaxPlayers,
|
|
HasPassword = s.Passworded,
|
|
RespondedToSteamQuery = responded
|
|
};
|
|
serverInfo.PingChecked = true;
|
|
serverInfo.Ping = s.Ping;
|
|
if (responded)
|
|
{
|
|
s.FetchRules();
|
|
}
|
|
s.OnReceivedRules += (bool rulesReceived) =>
|
|
{
|
|
if (!rulesReceived || s.Rules == null) { return; }
|
|
|
|
if (s.Rules.ContainsKey("message")) serverInfo.ServerMessage = s.Rules["message"];
|
|
if (s.Rules.ContainsKey("version")) serverInfo.GameVersion = s.Rules["version"];
|
|
|
|
if (s.Rules.ContainsKey("contentpackage")) serverInfo.ContentPackageNames.AddRange(s.Rules["contentpackage"].Split(','));
|
|
if (s.Rules.ContainsKey("contentpackagehash")) serverInfo.ContentPackageHashes.AddRange(s.Rules["contentpackagehash"].Split(','));
|
|
if (s.Rules.ContainsKey("contentpackageurl")) serverInfo.ContentPackageWorkshopUrls.AddRange(s.Rules["contentpackageurl"].Split(','));
|
|
|
|
if (s.Rules.ContainsKey("usingwhitelist")) serverInfo.UsingWhiteList = s.Rules["usingwhitelist"] == "True";
|
|
if (s.Rules.ContainsKey("modeselectionmode"))
|
|
{
|
|
if (Enum.TryParse(s.Rules["modeselectionmode"], out SelectionMode selectionMode)) serverInfo.ModeSelectionMode = selectionMode;
|
|
}
|
|
if (s.Rules.ContainsKey("subselectionmode"))
|
|
{
|
|
if (Enum.TryParse(s.Rules["subselectionmode"], out SelectionMode selectionMode)) serverInfo.SubSelectionMode = selectionMode;
|
|
}
|
|
if (s.Rules.ContainsKey("allowspectating")) serverInfo.AllowSpectating = s.Rules["allowspectating"] == "True";
|
|
if (s.Rules.ContainsKey("allowrespawn")) serverInfo.AllowRespawn = s.Rules["allowrespawn"] == "True";
|
|
if (s.Rules.ContainsKey("traitors"))
|
|
{
|
|
if (Enum.TryParse(s.Rules["traitors"], out YesNoMaybe traitorsEnabled)) serverInfo.TraitorsEnabled = traitorsEnabled;
|
|
}
|
|
|
|
if (serverInfo.ContentPackageNames.Count != serverInfo.ContentPackageHashes.Count ||
|
|
serverInfo.ContentPackageHashes.Count != serverInfo.ContentPackageWorkshopUrls.Count)
|
|
{
|
|
//invalid contentpackage info
|
|
serverInfo.ContentPackageNames.Clear();
|
|
serverInfo.ContentPackageHashes.Clear();
|
|
}
|
|
onServerRulesReceived(serverInfo);
|
|
};
|
|
|
|
onServerFound(serverInfo);
|
|
}
|
|
query.Responded.Clear();
|
|
}
|
|
|
|
private static bool ValidateServerInfo(ServerList.Server server)
|
|
{
|
|
if (string.IsNullOrEmpty(server.Name)) { return false; }
|
|
if (server.Address == null) { return false; }
|
|
|
|
return true;
|
|
}
|
|
|
|
public static Auth.Ticket GetAuthSessionTicket()
|
|
{
|
|
if (instance == null || !instance.isInitialized)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return instance.client.Auth.GetAuthSessionTicket();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Server
|
|
|
|
public static bool CreateServer(Networking.GameServer server, bool isPublic)
|
|
{
|
|
if (instance == null || !instance.isInitialized)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
ServerInit options = new ServerInit("Barotrauma", "Barotrauma")
|
|
{
|
|
GamePort = (ushort)server.Port,
|
|
QueryPort = (ushort)server.QueryPort
|
|
};
|
|
//options.QueryShareGamePort();
|
|
|
|
instance.server = new Server(AppID, options, isPublic);
|
|
if (!instance.server.IsValid)
|
|
{
|
|
instance.server.Dispose();
|
|
instance.server = null;
|
|
DebugConsole.ThrowError("Initializing Steam server failed.");
|
|
return false;
|
|
}
|
|
|
|
RefreshServerDetails(server);
|
|
|
|
instance.server.Auth.OnAuthChange = server.OnAuthChange;
|
|
Instance.server.LogOnAnonymous();
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool RefreshServerDetails(Networking.GameServer server)
|
|
{
|
|
if (instance == null || !instance.isInitialized)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// These server state variables may be changed at any time. Note that there is no lnoger a mechanism
|
|
// to send the player count. The player count is maintained by steam and you should use the player
|
|
// creation/authentication functions to maintain your player count.
|
|
instance.server.ServerName = server.Name;
|
|
instance.server.MaxPlayers = server.MaxPlayers;
|
|
instance.server.Passworded = server.HasPassword;
|
|
Instance.server.SetKey("message", GameMain.NetLobbyScreen.ServerMessageText);
|
|
Instance.server.SetKey("version", GameMain.Version.ToString());
|
|
Instance.server.SetKey("contentpackage", string.Join(",", GameMain.Config.SelectedContentPackages.Select(cp => cp.Name)));
|
|
Instance.server.SetKey("contentpackagehash", string.Join(",", GameMain.Config.SelectedContentPackages.Select(cp => cp.MD5hash.Hash)));
|
|
Instance.server.SetKey("contentpackageurl", string.Join(",", GameMain.Config.SelectedContentPackages.Select(cp => cp.SteamWorkshopUrl ?? "")));
|
|
Instance.server.SetKey("usingwhitelist", (server.WhiteList != null && server.WhiteList.Enabled).ToString());
|
|
Instance.server.SetKey("modeselectionmode", server.ModeSelectionMode.ToString());
|
|
Instance.server.SetKey("subselectionmode", server.SubSelectionMode.ToString());
|
|
Instance.server.SetKey("allowspectating", server.AllowSpectating.ToString());
|
|
Instance.server.SetKey("allowrespawn", server.AllowRespawn.ToString());
|
|
Instance.server.SetKey("traitors", server.TraitorsEnabled.ToString());
|
|
Instance.server.SetKey("gamestarted", server.GameStarted.ToString());
|
|
Instance.server.SetKey("gamemode", server.GameModeIdentifier);
|
|
|
|
#if SERVER
|
|
instance.server.DedicatedServer = true;
|
|
#endif
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool StartAuthSession(byte[] authTicketData, ulong clientSteamID)
|
|
{
|
|
if (instance == null || !instance.isInitialized || instance.server == null) return false;
|
|
|
|
DebugConsole.Log("SteamManager authenticating Steam client " + clientSteamID);
|
|
if (!instance.server.Auth.StartSession(authTicketData, clientSteamID))
|
|
{
|
|
DebugConsole.Log("Authentication failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static void StopAuthSession(ulong clientSteamID)
|
|
{
|
|
if (instance == null || !instance.isInitialized || instance.server == null) return;
|
|
|
|
DebugConsole.Log("SteamManager ending auth session with Steam client " + clientSteamID);
|
|
instance.server.Auth.EndSession(clientSteamID);
|
|
}
|
|
|
|
public static bool CloseServer()
|
|
{
|
|
if (instance == null || !instance.isInitialized || instance.server == null) return false;
|
|
|
|
instance.server.Dispose();
|
|
instance.server = null;
|
|
|
|
return true;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Workshop
|
|
|
|
public const string WorkshopItemStagingFolder = "NewWorkshopItem";
|
|
public const string WorkshopItemPreviewImageFolder = "Workshop";
|
|
public const string PreviewImageName = "PreviewImage.png";
|
|
private const string MetadataFileName = "filelist.xml";
|
|
private const string DefaultPreviewImagePath = "Content/DefaultWorkshopPreviewImage.png";
|
|
|
|
private Sprite defaultPreviewImage;
|
|
public Sprite DefaultPreviewImage
|
|
{
|
|
get
|
|
{
|
|
if (defaultPreviewImage == null)
|
|
{
|
|
defaultPreviewImage = new Sprite(DefaultPreviewImagePath, sourceRectangle: null);
|
|
}
|
|
return defaultPreviewImage;
|
|
}
|
|
}
|
|
|
|
public static void GetSubscribedWorkshopItems(Action<IList<Workshop.Item>> onItemsFound, List<string> requireTags = null)
|
|
{
|
|
if (instance == null || !instance.isInitialized) return;
|
|
|
|
var query = instance.client.Workshop.CreateQuery();
|
|
query.Order = Workshop.Order.RankedByTrend;
|
|
query.UserId = instance.client.SteamId;
|
|
query.UserQueryType = Workshop.UserQueryType.Subscribed;
|
|
query.UploaderAppId = AppID;
|
|
if (requireTags != null) query.RequireTags = requireTags;
|
|
query.Run();
|
|
query.OnResult += (Workshop.Query q) =>
|
|
{
|
|
onItemsFound?.Invoke(q.Items);
|
|
};
|
|
}
|
|
|
|
public static void GetPopularWorkshopItems(Action<IList<Workshop.Item>> onItemsFound, int amount, List<string> requireTags = null)
|
|
{
|
|
if (instance == null || !instance.isInitialized) return;
|
|
|
|
var query = instance.client.Workshop.CreateQuery();
|
|
query.Order = Workshop.Order.RankedByTrend;
|
|
query.RankedByTrendDays = 30;
|
|
query.UploaderAppId = AppID;
|
|
if (requireTags != null) query.RequireTags = requireTags;
|
|
query.Run();
|
|
query.OnResult += (Workshop.Query q) =>
|
|
{
|
|
//count the number of each unique tag
|
|
foreach (var item in q.Items)
|
|
{
|
|
foreach (string tag in item.Tags)
|
|
{
|
|
if (string.IsNullOrEmpty(tag)) { continue; }
|
|
string caseInvariantTag = tag.ToLowerInvariant();
|
|
if (!instance.tagCommonness.ContainsKey(caseInvariantTag))
|
|
{
|
|
instance.tagCommonness[caseInvariantTag] = 1;
|
|
}
|
|
else
|
|
{
|
|
instance.tagCommonness[caseInvariantTag]++;
|
|
}
|
|
}
|
|
}
|
|
//populate the popularTags list with tags sorted by commonness
|
|
instance.popularTags.Clear();
|
|
foreach (KeyValuePair<string, int> tagCommonness in instance.tagCommonness)
|
|
{
|
|
int i = 0;
|
|
while (i < instance.popularTags.Count &&
|
|
instance.tagCommonness[instance.popularTags[i]] > tagCommonness.Value)
|
|
{
|
|
i++;
|
|
}
|
|
instance.popularTags.Insert(i, tagCommonness.Key);
|
|
}
|
|
|
|
var nonSubscribedItems = q.Items.Where(it => !it.Subscribed && !it.Installed);
|
|
if (nonSubscribedItems.Count() > amount)
|
|
{
|
|
nonSubscribedItems = nonSubscribedItems.Take(amount);
|
|
}
|
|
onItemsFound?.Invoke(nonSubscribedItems.ToList());
|
|
};
|
|
}
|
|
|
|
public static void GetPublishedWorkshopItems(Action<IList<Workshop.Item>> onItemsFound, List<string> requireTags = null)
|
|
{
|
|
if (instance == null || !instance.isInitialized) return;
|
|
|
|
var query = instance.client.Workshop.CreateQuery();
|
|
query.Order = Workshop.Order.RankedByPublicationDate;
|
|
query.UserId = instance.client.SteamId;
|
|
query.UserQueryType = Workshop.UserQueryType.Published;
|
|
query.UploaderAppId = AppID;
|
|
if (requireTags != null) query.RequireTags = requireTags;
|
|
query.Run();
|
|
query.OnResult += (Workshop.Query q) =>
|
|
{
|
|
onItemsFound?.Invoke(q.Items);
|
|
};
|
|
}
|
|
|
|
public static void SubscribeToWorkshopItem(string itemUrl)
|
|
{
|
|
if (instance == null || !instance.isInitialized) return;
|
|
|
|
ulong id = GetWorkshopItemIDFromUrl(itemUrl);
|
|
if (id == 0) { return; }
|
|
|
|
var item = instance.client.Workshop.GetItem(id);
|
|
if (item == null)
|
|
{
|
|
DebugConsole.ThrowError("Failed to find a Steam Workshop item with the ID " + id + ".");
|
|
return;
|
|
}
|
|
|
|
item.Subscribe();
|
|
item.Download();
|
|
}
|
|
|
|
public static void SaveToWorkshop(Submarine sub)
|
|
{
|
|
if (instance == null || !instance.isInitialized) return;
|
|
|
|
Workshop.Editor item;
|
|
ContentPackage contentPackage;
|
|
try
|
|
{
|
|
CreateWorkshopItemStaging(
|
|
new List<ContentFile>() { new ContentFile(sub.FilePath, ContentType.None) },
|
|
out item, out contentPackage);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugConsole.ThrowError("Creating the workshop item failed.", e);
|
|
return;
|
|
}
|
|
|
|
item.Description = sub.Description;
|
|
item.Title = sub.Name;
|
|
item.Tags.Add("Submarine");
|
|
|
|
string subPreviewPath = Path.GetFullPath(Path.Combine(item.Folder, PreviewImageName));
|
|
#if CLIENT
|
|
try
|
|
{
|
|
using (Stream s = File.Create(subPreviewPath))
|
|
{
|
|
sub.PreviewImage.Texture.SaveAsPng(s, (int)sub.PreviewImage.size.X, (int)sub.PreviewImage.size.Y);
|
|
item.PreviewImage = subPreviewPath;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
DebugConsole.ThrowError("Saving submarine preview image failed.", e);
|
|
item.PreviewImage = null;
|
|
}
|
|
#endif
|
|
|
|
StartPublishItem(contentPackage, item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new folder, copies the specified files there and creates a metadata file with install instructions.
|
|
/// </summary>
|
|
public static void CreateWorkshopItemStaging(List<ContentFile> contentFiles, out Workshop.Editor itemEditor, out ContentPackage contentPackage)
|
|
{
|
|
var stagingFolder = new DirectoryInfo(WorkshopItemStagingFolder);
|
|
if (stagingFolder.Exists)
|
|
{
|
|
SaveUtil.ClearFolder(stagingFolder.FullName);
|
|
}
|
|
else
|
|
{
|
|
stagingFolder.Create();
|
|
}
|
|
Directory.CreateDirectory(Path.Combine(WorkshopItemStagingFolder, "Submarines"));
|
|
Directory.CreateDirectory(Path.Combine(WorkshopItemStagingFolder, "Mods"));
|
|
Directory.CreateDirectory(Path.Combine(WorkshopItemStagingFolder, "Mods", "ModName"));
|
|
|
|
itemEditor = instance.client.Workshop.CreateItem(Workshop.ItemType.Community);
|
|
itemEditor.Visibility = Workshop.Editor.VisibilityType.Public;
|
|
itemEditor.WorkshopUploadAppId = AppID;
|
|
itemEditor.Folder = stagingFolder.FullName;
|
|
|
|
string previewImagePath = Path.GetFullPath(Path.Combine(itemEditor.Folder, PreviewImageName));
|
|
File.Copy("Content/DefaultWorkshopPreviewImage.png", previewImagePath);
|
|
|
|
//copy content files to the staging folder
|
|
List<string> copiedFilePaths = new List<string>();
|
|
foreach (ContentFile file in contentFiles)
|
|
{
|
|
string relativePath = UpdaterUtil.GetRelativePath(Path.GetFullPath(file.Path), Environment.CurrentDirectory);
|
|
string destinationPath = Path.Combine(stagingFolder.FullName, relativePath);
|
|
//make sure the directory exists
|
|
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
|
|
File.Copy(file.Path, destinationPath);
|
|
copiedFilePaths.Add(destinationPath);
|
|
}
|
|
System.Diagnostics.Debug.Assert(copiedFilePaths.Count == contentFiles.Count);
|
|
|
|
//create a new content package and include the copied files in it
|
|
contentPackage = ContentPackage.CreatePackage("ContentPackage", Path.Combine(itemEditor.Folder, MetadataFileName), false);
|
|
for (int i = 0; i < copiedFilePaths.Count; i++)
|
|
{
|
|
contentPackage.AddFile(copiedFilePaths[i], contentFiles[i].Type);
|
|
}
|
|
|
|
contentPackage.Save(Path.Combine(stagingFolder.FullName, MetadataFileName));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a copy of the specified workshop item in the staging folder and an editor that can be used to edit and update the item
|
|
/// </summary>
|
|
public static void CreateWorkshopItemStaging(Workshop.Item existingItem, out Workshop.Editor itemEditor, out ContentPackage contentPackage)
|
|
{
|
|
if (!existingItem.Installed)
|
|
{
|
|
itemEditor = null;
|
|
contentPackage = null;
|
|
DebugConsole.ThrowError("Cannot edit the workshop item \"" + existingItem.Title + "\" because it has not been installed.");
|
|
return;
|
|
}
|
|
|
|
var stagingFolder = new DirectoryInfo(WorkshopItemStagingFolder);
|
|
if (stagingFolder.Exists)
|
|
{
|
|
SaveUtil.ClearFolder(stagingFolder.FullName);
|
|
}
|
|
else
|
|
{
|
|
stagingFolder.Create();
|
|
}
|
|
|
|
itemEditor = instance.client.Workshop.EditItem(existingItem.Id);
|
|
itemEditor.Visibility = Workshop.Editor.VisibilityType.Public;
|
|
itemEditor.Title = existingItem.Title;
|
|
itemEditor.Tags = existingItem.Tags.ToList();
|
|
itemEditor.Description = existingItem.Description;
|
|
itemEditor.WorkshopUploadAppId = AppID;
|
|
itemEditor.Folder = stagingFolder.FullName;
|
|
|
|
string previewImagePath = Path.GetFullPath(Path.Combine(itemEditor.Folder, PreviewImageName));
|
|
itemEditor.PreviewImage = previewImagePath;
|
|
|
|
using (WebClient client = new WebClient())
|
|
{
|
|
if (File.Exists(previewImagePath))
|
|
{
|
|
File.Delete(previewImagePath);
|
|
}
|
|
client.DownloadFile(new Uri(existingItem.PreviewImageUrl), previewImagePath);
|
|
}
|
|
|
|
ContentPackage tempContentPackage = new ContentPackage(Path.Combine(existingItem.Directory.FullName, MetadataFileName));
|
|
if (File.Exists(tempContentPackage.Path))
|
|
{
|
|
string newContentPackagePath = Path.Combine(WorkshopItemStagingFolder, MetadataFileName);
|
|
File.Copy(tempContentPackage.Path, newContentPackagePath, overwrite: true);
|
|
contentPackage = new ContentPackage(newContentPackagePath);
|
|
foreach (ContentFile contentFile in tempContentPackage.Files)
|
|
{
|
|
string sourceFile = Path.Combine(existingItem.Directory.FullName, contentFile.Path);
|
|
if (!File.Exists(sourceFile)) { continue; }
|
|
//make sure the destination directory exists
|
|
string destinationPath = Path.Combine(WorkshopItemStagingFolder, contentFile.Path);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
|
|
File.Copy(sourceFile, destinationPath, overwrite: true);
|
|
contentPackage.AddFile(contentFile.Path, contentFile.Type);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
contentPackage = ContentPackage.CreatePackage(existingItem.Title, Path.Combine(WorkshopItemStagingFolder, MetadataFileName), false);
|
|
contentPackage.Save(contentPackage.Path);
|
|
}
|
|
}
|
|
|
|
public static void StartPublishItem(ContentPackage contentPackage, Workshop.Editor item)
|
|
{
|
|
if (instance == null || !instance.isInitialized) return;
|
|
|
|
if (string.IsNullOrEmpty(item.Title))
|
|
{
|
|
DebugConsole.ThrowError("Cannot publish workshop item - title not set.");
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(item.Folder))
|
|
{
|
|
DebugConsole.ThrowError("Cannot publish workshop item \"" + item.Title + "\" - folder not set.");
|
|
return;
|
|
}
|
|
|
|
contentPackage.Name = item.Title;
|
|
contentPackage.GameVersion = GameMain.Version;
|
|
contentPackage.Save(Path.Combine(WorkshopItemStagingFolder, MetadataFileName));
|
|
|
|
if (File.Exists(PreviewImageName)) File.Delete(PreviewImageName);
|
|
//move the preview image out of the staging folder, it does not need to be included in the folder sent to Workshop
|
|
File.Move(Path.GetFullPath(Path.Combine(item.Folder, PreviewImageName)), PreviewImageName);
|
|
item.PreviewImage = Path.GetFullPath(PreviewImageName);
|
|
|
|
CoroutineManager.StartCoroutine(PublishItem(item));
|
|
}
|
|
|
|
private static IEnumerable<object> PublishItem(Workshop.Editor item)
|
|
{
|
|
if (instance == null || !instance.isInitialized)
|
|
{
|
|
yield return CoroutineStatus.Success;
|
|
}
|
|
|
|
item.Publish();
|
|
while (item.Publishing)
|
|
{
|
|
yield return CoroutineStatus.Running;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(item.Error))
|
|
{
|
|
DebugConsole.NewMessage("Published workshop item " + item.Title + " successfully.", Microsoft.Xna.Framework.Color.LightGreen);
|
|
}
|
|
else
|
|
{
|
|
DebugConsole.ThrowError("Publishing workshop item " + item.Title + " failed. " + item.Error);
|
|
}
|
|
|
|
SaveUtil.ClearFolder(WorkshopItemStagingFolder);
|
|
Directory.Delete(WorkshopItemStagingFolder);
|
|
File.Delete(PreviewImageName);
|
|
|
|
yield return CoroutineStatus.Success;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enables a workshop item by moving it to the game folder.
|
|
/// </summary>
|
|
public static bool EnableWorkShopItem(Workshop.Item item, bool allowFileOverwrite, out string errorMsg)
|
|
{
|
|
if (!item.Installed)
|
|
{
|
|
errorMsg = TextManager.Get("WorkshopErrorInstallRequiredToEnable").Replace("[itemname]", item.Title);
|
|
DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red);
|
|
return false;
|
|
}
|
|
|
|
ContentPackage contentPackage = new ContentPackage(Path.Combine(item.Directory.FullName, MetadataFileName));
|
|
string newContentPackagePath = GetWorkshopItemContentPackagePath(contentPackage);
|
|
|
|
var allPackageFiles = Directory.GetFiles(item.Directory.FullName, "*", SearchOption.AllDirectories);
|
|
List<string> nonContentFiles = new List<string>();
|
|
foreach (string file in allPackageFiles)
|
|
{
|
|
if (file == MetadataFileName) { continue; }
|
|
string relativePath = UpdaterUtil.GetRelativePath(file, item.Directory.FullName);
|
|
string fullPath = Path.GetFullPath(relativePath);
|
|
if (contentPackage.Files.Any(f => { string fp = Path.GetFullPath(f.Path); return fp == fullPath; })) { continue; }
|
|
if (ContentPackage.IsModFilePathAllowed(relativePath))
|
|
{
|
|
nonContentFiles.Add(relativePath);
|
|
}
|
|
}
|
|
|
|
if (!allowFileOverwrite)
|
|
{
|
|
if (File.Exists(newContentPackagePath))
|
|
{
|
|
errorMsg = TextManager.Get("WorkshopErrorOverwriteOnEnable")
|
|
.Replace("[itemname]", item.Title)
|
|
.Replace("[filename]", newContentPackagePath);
|
|
DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red);
|
|
return false;
|
|
}
|
|
|
|
foreach (ContentFile contentFile in contentPackage.Files)
|
|
{
|
|
string sourceFile = Path.Combine(item.Directory.FullName, contentFile.Path);
|
|
if (File.Exists(sourceFile) && File.Exists(contentFile.Path))
|
|
{
|
|
errorMsg = TextManager.Get("WorkshopErrorOverwriteOnEnable")
|
|
.Replace("[itemname]", item.Title)
|
|
.Replace("[filename]", contentFile.Path);
|
|
DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
//we only need to create a new content package for the item if it contains content with a type other than None or Submarine
|
|
//e.g. items that are just a sub file are just copied to the game folder
|
|
if (contentPackage.Files.Any(f => f.Type != ContentType.None && f.Type != ContentType.Submarine))
|
|
{
|
|
File.Copy(contentPackage.Path, newContentPackagePath);
|
|
}
|
|
|
|
foreach (ContentFile contentFile in contentPackage.Files)
|
|
{
|
|
string sourceFile = Path.Combine(item.Directory.FullName, contentFile.Path);
|
|
if (!File.Exists(sourceFile)) { continue; }
|
|
if (!ContentPackage.IsModFilePathAllowed(contentFile))
|
|
{
|
|
DebugConsole.ThrowError(TextManager.Get("WorkshopErrorIllegalPathOnEnable").Replace("[filename]", contentFile.Path));
|
|
continue;
|
|
}
|
|
|
|
//make sure the destination directory exists
|
|
Directory.CreateDirectory(Path.GetDirectoryName(contentFile.Path));
|
|
File.Copy(sourceFile, contentFile.Path, overwrite: true);
|
|
}
|
|
|
|
foreach (string nonContentFile in nonContentFiles)
|
|
{
|
|
string sourceFile = Path.Combine(item.Directory.FullName, nonContentFile);
|
|
if (!File.Exists(sourceFile)) { continue; }
|
|
if (!ContentPackage.IsModFilePathAllowed(nonContentFile))
|
|
{
|
|
DebugConsole.ThrowError(TextManager.Get("WorkshopErrorIllegalPathOnEnable").Replace("[filename]", nonContentFile));
|
|
continue;
|
|
}
|
|
Directory.CreateDirectory(Path.GetDirectoryName(nonContentFile));
|
|
File.Copy(sourceFile, nonContentFile, overwrite: true);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
errorMsg = TextManager.Get("WorkshopErrorEnableFailed").Replace("[itemname]", item.Title) + " " + e.Message;
|
|
DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red);
|
|
return false;
|
|
}
|
|
|
|
var newPackage = new ContentPackage(contentPackage.Path, newContentPackagePath)
|
|
{
|
|
SteamWorkshopUrl = item.Url
|
|
};
|
|
newPackage.Save(newContentPackagePath);
|
|
ContentPackage.List.Add(newPackage);
|
|
if (newPackage.CorePackage)
|
|
{
|
|
//if enabling a core package, disable all other core packages
|
|
GameMain.Config.SelectedContentPackages.RemoveWhere(cp => cp.CorePackage);
|
|
}
|
|
GameMain.Config.SelectedContentPackages.Add(newPackage);
|
|
GameMain.Config.Save();
|
|
|
|
if (item.Tags.Contains("Submarine") || newPackage.Files.Any(f => f.Type == ContentType.Submarine))
|
|
{
|
|
Submarine.RefreshSavedSubs();
|
|
}
|
|
|
|
errorMsg = "";
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disables a workshop item by removing the files from the game folder.
|
|
/// </summary>
|
|
public static bool DisableWorkShopItem(Workshop.Item item, out string errorMsg)
|
|
{
|
|
if (!item.Installed)
|
|
{
|
|
errorMsg = "Cannot disable workshop item \"" + item.Title + "\" because it has not been installed.";
|
|
DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red);
|
|
return false;
|
|
}
|
|
|
|
ContentPackage contentPackage = new ContentPackage(Path.Combine(item.Directory.FullName, MetadataFileName));
|
|
string installedContentPackagePath = GetWorkshopItemContentPackagePath(contentPackage);
|
|
|
|
var allPackageFiles = Directory.GetFiles(item.Directory.FullName, "*", SearchOption.AllDirectories);
|
|
List<string> nonContentFiles = new List<string>();
|
|
foreach (string file in allPackageFiles)
|
|
{
|
|
if (file == MetadataFileName) { continue; }
|
|
string relativePath = UpdaterUtil.GetRelativePath(file, item.Directory.FullName);
|
|
string fullPath = Path.GetFullPath(relativePath);
|
|
if (contentPackage.Files.Any(f => { string fp = Path.GetFullPath(f.Path); return fp == fullPath; })) { continue; }
|
|
if (ContentPackage.IsModFilePathAllowed(relativePath))
|
|
{
|
|
nonContentFiles.Add(relativePath);
|
|
}
|
|
}
|
|
if (File.Exists(installedContentPackagePath)) { File.Delete(installedContentPackagePath); }
|
|
|
|
bool wasSub = item.Tags.Contains("Submarine") || contentPackage.Files.Any(f => f.Type == ContentType.Submarine);
|
|
|
|
HashSet<string> directories = new HashSet<string>();
|
|
try
|
|
{
|
|
foreach (ContentFile contentFile in contentPackage.Files)
|
|
{
|
|
if (!ContentPackage.IsModFilePathAllowed(contentFile))
|
|
{
|
|
//Workshop items are not allowed to add or modify files in the Content or Data folders;
|
|
continue;
|
|
}
|
|
if (!File.Exists(contentFile.Path)) { continue; }
|
|
File.Delete(contentFile.Path);
|
|
directories.Add(Path.GetDirectoryName(contentFile.Path));
|
|
}
|
|
foreach (string nonContentFile in nonContentFiles)
|
|
{
|
|
if (!ContentPackage.IsModFilePathAllowed(nonContentFile))
|
|
{
|
|
//Workshop items are not allowed to add or modify files in the Content or Data folders;
|
|
continue;
|
|
}
|
|
if (!File.Exists(nonContentFile)) { continue; }
|
|
File.Delete(nonContentFile);
|
|
directories.Add(Path.GetDirectoryName(nonContentFile));
|
|
}
|
|
|
|
foreach (string directory in directories)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(directory) || !Directory.Exists(directory)) { continue; }
|
|
if (Directory.GetFiles(directory, "*", SearchOption.AllDirectories).Count() == 0)
|
|
{
|
|
Directory.Delete(directory, recursive: true);
|
|
}
|
|
}
|
|
|
|
ContentPackage.List.RemoveAll(cp => System.IO.Path.GetFullPath(cp.Path) == System.IO.Path.GetFullPath(installedContentPackagePath));
|
|
GameMain.Config.SelectedContentPackages.RemoveWhere(cp => !ContentPackage.List.Contains(cp));
|
|
GameMain.Config.Save();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
errorMsg = "Disabling the workshop item \"" + item.Title + "\" failed. "+e.Message;
|
|
DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red);
|
|
return false;
|
|
}
|
|
|
|
if (wasSub)
|
|
{
|
|
Submarine.RefreshSavedSubs();
|
|
}
|
|
|
|
errorMsg = "";
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Is the item compatible with this version of Barotrauma. Returns null if compatibility couldn't be determined (item not installed)
|
|
/// </summary>
|
|
public static bool? CheckWorkshopItemCompatibility(Workshop.Item item)
|
|
{
|
|
if (!item.Installed) { return null; }
|
|
|
|
string metaDataPath = Path.Combine(item.Directory.FullName, MetadataFileName);
|
|
if (!File.Exists(metaDataPath))
|
|
{
|
|
throw new FileNotFoundException("Metadata file for the Workshop item \"" + item.Title + "\" not found. The file may be corrupted.");
|
|
}
|
|
|
|
ContentPackage contentPackage = new ContentPackage(metaDataPath);
|
|
return contentPackage.IsCompatible();
|
|
}
|
|
|
|
public static bool CheckWorkshopItemEnabled(Workshop.Item item)
|
|
{
|
|
if (!item.Installed) return false;
|
|
|
|
string metaDataPath = Path.Combine(item.Directory.FullName, MetadataFileName);
|
|
if (!File.Exists(metaDataPath))
|
|
{
|
|
throw new FileNotFoundException("Metadata file for the Workshop item \"" + item.Title + "\" not found. The file may be corrupted.");
|
|
}
|
|
|
|
ContentPackage contentPackage = new ContentPackage(metaDataPath);
|
|
//make sure the contentpackage file is present
|
|
//(unless the package only contains submarine files, in which case we don't need a content package)
|
|
if (contentPackage.Files.Any(f => f.Type != ContentType.Submarine) &&
|
|
!File.Exists(GetWorkshopItemContentPackagePath(contentPackage)) &&
|
|
!ContentPackage.List.Any(cp => cp.Name == contentPackage.Name))
|
|
{
|
|
return false;
|
|
}
|
|
foreach (ContentFile contentFile in contentPackage.Files)
|
|
{
|
|
if (!File.Exists(contentFile.Path)) return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static string GetWorkshopItemContentPackagePath(ContentPackage contentPackage)
|
|
{
|
|
string fileName = contentPackage.Name + ".xml";
|
|
string invalidChars = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
|
|
foreach (char c in invalidChars) fileName = fileName.Replace(c.ToString(), "");
|
|
return Path.Combine("Data", "ContentPackages", fileName);
|
|
}
|
|
|
|
#endregion
|
|
|
|
public static void Update(float deltaTime)
|
|
{
|
|
if (instance == null || !instance.isInitialized) return;
|
|
|
|
instance.client?.Update();
|
|
instance.server?.Update();
|
|
|
|
SteamAchievementManager.Update(deltaTime);
|
|
}
|
|
|
|
public static void ShutDown()
|
|
{
|
|
if (instance == null || !instance.isInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
instance.client?.Dispose();
|
|
instance.client = null;
|
|
instance.server?.Dispose();
|
|
instance.server = null;
|
|
}
|
|
}
|
|
}
|