Skip to content

Commit

Permalink
feat(sqlite): add active feature table
Browse files Browse the repository at this point in the history
closes #249
  • Loading branch information
DorielRivalet committed Jan 28, 2024
1 parent 6934803 commit 3410975
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
9 changes: 9 additions & 0 deletions MHFZ_Overlay/Models/Addresses/AddressModelHGE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,4 +1769,13 @@ public AddressModelHGE(Mem m)

/// <inheritdoc/>
public override decimal PlayerPositionInQuestZ() => (decimal)this.M.ReadFloat("mhfo-hd.dll+21B3538", string.Empty, false);

/// <inheritdoc/>
public override int ActiveFeature1() => this.M.Read2Byte("mhfo-hd.dll+1C2AB0C");

/// <inheritdoc/>
public override int ActiveFeature2() => this.M.Read2Byte("mhfo-hd.dll+1C2AB14");

/// <inheritdoc/>
public override int ActiveFeature3() => this.M.Read2Byte("mhfo-hd.dll+E41A2A8");
}
9 changes: 9 additions & 0 deletions MHFZ_Overlay/Models/Addresses/AddressModelNotHGE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1689,4 +1689,13 @@ public AddressModelNotHGE(Mem m)
/// <inheritdoc/>
public override decimal PlayerPositionInQuestZ() => (decimal)this.M.ReadFloat("mhfo.dll+20BB548", string.Empty, false);

/// <inheritdoc/>
public override int ActiveFeature1() => this.M.Read2Byte("mhfo.dll+1BD2F50");

/// <inheritdoc/>
public override int ActiveFeature2() => this.M.Read2Byte("mhfo.dll+1BD2F58");

/// <inheritdoc/>
public override int ActiveFeature3() => this.M.Read2Byte("mhfo.dll+57E26E8");

}
28 changes: 27 additions & 1 deletion MHFZ_Overlay/Models/Structures/Bitfields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,30 @@ public enum EquipmentType : uint
HC = 8,
UNK1 = 16,
Ravi = HC | UNK1,
}
}

/// <summary>
/// Active features enabled.
/// </summary>
[Flags]
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ActiveFeature : uint
{
[DefaultValue(None)]
None = 0,
GreatSword = 1,
HeavyBowgun = 2,
Hammer = 4,
Lance = 8,
SwordAndShield = 16,
LightBowgun = 32,
DualSwords = 64,
LongSword = 128,
HuntingHorn = 256,
Gunlance = 512,
Bow = 1024,
Tonfa = 2048,
SwitchAxeF = 4096,
MagnetSpike = 8192,
All = GreatSword | HeavyBowgun | Hammer | Lance | SwordAndShield | LightBowgun | DualSwords | LongSword | HuntingHorn | Gunlance | Bow | Tonfa | SwitchAxeF | MagnetSpike,
}
53 changes: 53 additions & 0 deletions MHFZ_Overlay/Services/DatabaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ namespace MHFZ_Overlay.Services;
using MHFZ_Overlay.Models.Constant;
using MHFZ_Overlay.Models.Messengers;
using MHFZ_Overlay.Models.Structures;
using MHFZ_Overlay.ViewModels.Windows;
using MHFZ_Overlay.Views.Windows;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using Octokit;
using SharpCompress.Common;
using Wpf.Ui.Common;
using Wpf.Ui.Controls;
using Formatting = Newtonsoft.Json.Formatting;
Expand Down Expand Up @@ -1849,7 +1851,24 @@ FinalTimeValue ASC

Logger.Debug("Inserted into QuestsOverlayHash table");

sql = @"INSERT INTO QuestsActiveFeature (
ActiveFeature,
RunID
) VALUES (
@ActiveFeature,
@RunID
)";

using (var cmd = new SQLiteCommand(sql, conn))
{
var activeFeature = GetActiveFeature(model);

cmd.Parameters.AddWithValue("@ActiveFeature", activeFeature);
cmd.Parameters.AddWithValue("@RunID", runID);
cmd.ExecuteNonQuery();
}

Logger.Debug("Inserted into QuestsActiveFeature table");
// TODO more tables


Expand Down Expand Up @@ -3087,6 +3106,27 @@ public string GetOverlayHash()
return overlayHash;
}

/// <summary>
/// TODO needs real testing
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int GetActiveFeature(AddressModel model)
{
var activeFeatures = new[] { model.ActiveFeature1(), model.ActiveFeature2(), model.ActiveFeature3() };

foreach (var activeFeature in activeFeatures)
{
if (model.IsValidBitfield((uint)activeFeature, (uint)ActiveFeature.All))
{
return activeFeature;
}
}

Logger.Warn("Active feature not found: {0} {1} {2}", activeFeatures[0], activeFeatures[1], activeFeatures[2]);
return 0;
}

/// <summary>
/// Stores the overlay hash.
/// </summary>
Expand Down Expand Up @@ -5345,6 +5385,19 @@ FOREIGN KEY(RunID) REFERENCES Quests(RunID)
cmd.ExecuteNonQuery();
}

sql = @"CREATE TABLE IF NOT EXISTS QuestsActiveFeature(
QuestsActiveFeatureID INTEGER PRIMARY KEY AUTOINCREMENT,
ActiveFeature INTEGER NOT NULL DEFAULT 0,
RunID INTEGER NOT NULL,
FOREIGN KEY(RunID) REFERENCES Quests(RunID)
)";
using (var cmd = new SQLiteCommand(sql, conn))
{
cmd.ExecuteNonQuery();
}

// TODO extra tables

// a mh game but like a MUD. hunt in-game to get many kinds of points for this game. hunt and tame monsters. challenge other CPU players/monsters.
sql = @"CREATE TABLE IF NOT EXISTS GachaMaterial(
GachaMaterialID INTEGER PRIMARY KEY,
Expand Down
30 changes: 28 additions & 2 deletions MHFZ_Overlay/ViewModels/Windows/AddressModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,12 @@ 21747 or

public abstract decimal PlayerPositionInQuestZ();

public abstract int ActiveFeature1();

public abstract int ActiveFeature2();

public abstract int ActiveFeature3();


/// <TODO>
/// [] Not Done
Expand Down Expand Up @@ -1695,7 +1701,7 @@ public OverlayMode GetOverlayMode()
else if (s.TimerInfoShown && s.EnableInputLogging && s.EnableQuestLogging && s.OverlayModeWatermarkShown)
{
// TODO diva prayer gem
if (!HasRightsFlag((uint)this.Rights(), CourseRightsFirstByte.Support) && this.DivaSkillUsesLeft() == 0 && this.StyleRank1() != 15 && this.StyleRank2() != 15)
if (!HasBitfieldFlag((uint)this.Rights(), CourseRightsFirstByte.Support) && this.DivaSkillUsesLeft() == 0 && this.StyleRank1() != 15 && this.StyleRank2() != 15)
{
return OverlayMode.TimeAttack;
}
Expand All @@ -1714,7 +1720,7 @@ public OverlayMode GetOverlayMode()
}
}

public bool HasRightsFlag(uint value, CourseRightsFirstByte flag)
public bool HasBitfieldFlag(uint value, CourseRightsFirstByte flag)
{
if (value < 0x0100)
{
Expand All @@ -1737,6 +1743,26 @@ public bool HasRightsFlag(uint value, CourseRightsFirstByte flag)
return rights.HasFlag(flag);
}

public bool HasBitfieldFlag<T>(uint value, T flag, uint all) where T : Enum
{
// Validate
if (!IsValidBitfield(value, all))
{
return false;
}

// Convert
Enum convertedValue = (Enum)(object)value;

// Check if the flag is set
return convertedValue.HasFlag(flag);
}

public bool IsValidBitfield(uint value, uint all)
{
return (value & all) == value;
}

public bool CaravanOverride()
{
var s = (Settings)Application.Current.TryFindResource("Settings");
Expand Down

0 comments on commit 3410975

Please sign in to comment.