Skip to content

Commit

Permalink
- Version number validation
Browse files Browse the repository at this point in the history
  • Loading branch information
agilbert1412 committed Feb 27, 2023
1 parent 5dc39c3 commit 0584c9c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
21 changes: 16 additions & 5 deletions StardewArchipelago/Archipelago/ArchipelagoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ArchipelagoClient
private ChatForwarder _chatForwarder;
private GiftHandler _giftHandler;
private ArchipelagoConnectionInfo _connectionInfo;
private IManifest _modManifest;

private Action _itemReceivedFunction;

Expand All @@ -34,13 +35,14 @@ public class ArchipelagoClient
public Dictionary<string, ScoutedLocation> ScoutedLocations { get; set; }
// public Random MultiRandom { get; set; }

public ArchipelagoClient(IMonitor console, IModHelper modHelper, Harmony harmony, Action itemReceivedFunction)
public ArchipelagoClient(IMonitor console, IModHelper modHelper, Harmony harmony, Action itemReceivedFunction, IManifest manifest)
{
_console = console;
_modHelper = modHelper;
_harmony = harmony;
_itemReceivedFunction = itemReceivedFunction;
IsConnected = false;
_modManifest = manifest;
ScoutedLocations = new Dictionary<string, ScoutedLocation>();
}

Expand All @@ -52,6 +54,15 @@ public void Connect(ArchipelagoConnectionInfo connectionInfo, GiftHandler giftHa
if (!success)
{
DisconnectPermanently();
return;
}

var majorVersion = _modManifest.Version.MajorVersion.ToString();
if (!SlotData.MultiworldVersion.StartsWith(majorVersion))
{
errorMessage = $"This Multiworld has been created for StardewArchipelago version {SlotData.MultiworldVersion},\nbut this is StardewArchipelago version {_modManifest.Version}.\nPlease update to a compatible mod version.";
DisconnectPermanently();
return;
}
}

Expand All @@ -62,7 +73,7 @@ private bool TryConnect(ArchipelagoConnectionInfo connectionInfo, out string err
{
InitSession(connectionInfo);
var itemsHandling = ItemsHandlingFlags.AllItems;
var minimumVersion = new Version(0, 3, 7);
var minimumVersion = new Version(0, 3, 9);
var tags = connectionInfo.DeathLink ? new[] { "AP", "DeathLink" } : new[] { "AP" };
result = _session.TryConnectAndLogin(GAME_NAME, _connectionInfo.SlotName, itemsHandling, minimumVersion, tags, null, _connectionInfo.Password);
}
Expand Down Expand Up @@ -99,14 +110,14 @@ private bool TryConnect(ArchipelagoConnectionInfo connectionInfo, out string err
_console.Log(loginMessage, LogLevel.Info);

// Must go AFTER a successful connection attempt
InitializeAfterConnection(loginSuccess, connectionInfo.SlotName);
InitializeSlotData(connectionInfo.SlotName, loginSuccess.SlotData);
InitializeAfterConnection();
connectionInfo.DeathLink = SlotData.DeathLink;
return true;
}

private void InitializeAfterConnection(LoginSuccessful loginSuccess, string slotName)
private void InitializeAfterConnection()
{
InitializeSlotData(slotName, loginSuccess.SlotData);
_session.Items.ItemReceived += OnItemReceived;
_session.MessageLog.OnMessageReceived += OnMessageReceived;
_session.Socket.ErrorReceived += SessionErrorReceived;
Expand Down
3 changes: 3 additions & 0 deletions StardewArchipelago/Archipelago/SlotData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SlotData
private const string SEED_KEY = "seed";
private const string MODIFIED_BUNDLES_KEY = "modified_bundles";
private const string MODIFIED_ENTRANCES_KEY = "randomized_entrances";
private const string MULTIWORLD_VERSION_KEY = "client_version";

private Dictionary<string, object> _slotDataFields;
private IMonitor _console;
Expand All @@ -58,6 +59,7 @@ public class SlotData
public double GiftTax { get; private set; }
public bool DeathLink { get; private set; }
public string Seed { get; private set; }
public string MultiworldVersion { get; private set; }
private Dictionary<string, string> ModifiedBundles { get; set; }
public Dictionary<string, string> ModifiedEntrances { get; set; }

Expand Down Expand Up @@ -87,6 +89,7 @@ public SlotData(string slotName, Dictionary<string, object> slotDataFields, IMon
GiftTax = GetSlotSetting(GIFT_TAX_KEY, 30) / 100.0;
DeathLink = GetSlotSetting(DEATH_LINK_KEY, false);
Seed = GetSlotSetting(SEED_KEY, "");
MultiworldVersion = GetSlotSetting(MULTIWORLD_VERSION_KEY, "");
var newBundleStringData = GetSlotSetting(MODIFIED_BUNDLES_KEY, "");
ModifiedBundles = JsonConvert.DeserializeObject<Dictionary<string, string>>(newBundleStringData);
var newEntrancesStringData = GetSlotSetting(MODIFIED_ENTRANCES_KEY, "");
Expand Down
2 changes: 1 addition & 1 deletion StardewArchipelago/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override void Entry(IModHelper helper)
_helper = helper;
_harmony = new Harmony(this.ModManifest.UniqueID);

_archipelago = new ArchipelagoClient(Monitor, _helper, _harmony, OnItemReceived);
_archipelago = new ArchipelagoClient(Monitor, _helper, _harmony, OnItemReceived, ModManifest);

_helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
_helper.Events.GameLoop.SaveCreating += this.OnSaveCreating;
Expand Down
2 changes: 1 addition & 1 deletion StardewArchipelago/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Name": "StardewArchipelago",
"Author": "Kaito Kid",
"Version": "2.2.1",
"Version": "2.2.2",
"Description": "Stardew Valley Randomizer for Archipelago",
"UniqueID": "KaitoKid.StardewArchipelago",
"EntryDll": "StardewArchipelago.dll",
Expand Down

0 comments on commit 0584c9c

Please sign in to comment.