Skip to content

Commit

Permalink
Add notes screen
Browse files Browse the repository at this point in the history
  • Loading branch information
DarwinBaker committed Apr 17, 2021
1 parent 6600506 commit 52ee40e
Show file tree
Hide file tree
Showing 13 changed files with 2,260 additions and 94 deletions.
15 changes: 14 additions & 1 deletion AATool/AATool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Settings\NotesSettings.cs" />
<Compile Include="Trackers\AchievementTracker.cs" />
<Compile Include="Trackers\StatisticsTracker.cs" />
<Compile Include="Trackers\Tracker.cs" />
Expand All @@ -115,6 +116,7 @@
<Compile Include="UI\Controls\UIEnchantmentTable.cs" />
<Compile Include="UI\Controls\UIButton.cs" />
<Compile Include="Utilities\GameVersionDetector.cs" />
<Compile Include="Utilities\UpdateChecker.cs" />
<Compile Include="Winforms\Controls\CCreditsGroup.cs">
<SubType>UserControl</SubType>
</Compile>
Expand All @@ -133,6 +135,12 @@
<Compile Include="Winforms\Forms\FAbout.Designer.cs">
<DependentUpon>FAbout.cs</DependentUpon>
</Compile>
<Compile Include="Winforms\Forms\FNotes.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Winforms\Forms\FNotes.Designer.cs">
<DependentUpon>FNotes.cs</DependentUpon>
</Compile>
<Compile Include="Winforms\Forms\FPickFavorites.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -571,7 +579,9 @@
<Content Include="assets\graphics\sprites\achievements\spawnWither^48.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="assets\graphics\system\icon.ico" />
<Content Include="assets\graphics\system\icon.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="assets\graphics\sprites\advancements\adventure\adventure.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -2048,6 +2058,9 @@
<DependentUpon>FAbout.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Winforms\Forms\FNotes.resx">
<DependentUpon>FNotes.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Winforms\Forms\FPickFavorites.resx">
<DependentUpon>FPickFavorites.cs</DependentUpon>
<SubType>Designer</SubType>
Expand Down
60 changes: 34 additions & 26 deletions AATool/DataStructures/Saves/SaveJSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,54 @@ public bool TryRead()
{
//attempt to open stream
TryOpen(GetCurrentJSON(folderName));

//check if minecraft has saved changes to this file since last update
DateTime latestAccessDate = new FileInfo(currentFile).LastWriteTime;
if (CurrentAccessDate < latestAccessDate)

try
{
try
//check if minecraft has saved changes to this file since last update
DateTime latestAccessDate = new FileInfo(currentFile).LastWriteTime;
if (CurrentAccessDate < latestAccessDate)
{
if (IsOpen && stream.CanRead)
try
{
//reset stream to beginning, read all, and deserialize into dynamic JSON
stream.Position = 0;
json = JsonConvert.DeserializeObject(reader.ReadToEnd());
if (IsOpen && stream.CanRead)
{
//reset stream to beginning, read all, and deserialize into dynamic JSON
stream.Position = 0;
json = JsonConvert.DeserializeObject(reader.ReadToEnd());
}
}
catch
{
//something went wrong, probably file missing
if (IsOpen)
Close();
}
CurrentAccessDate = latestAccessDate;
return true;
}
catch
{
//something went wrong, probably file missing
if (IsOpen)
Close();
}
CurrentAccessDate = latestAccessDate;
return true;
}
catch (Exception) { }
return false;
}

private void TryOpen(string latestFile)
{
if (currentFile != latestFile)
try
{
//most recently accessed save changed. close old streams and open new ones
Close();
if (!File.Exists(latestFile))
return;
if (currentFile != latestFile)
{
//most recently accessed save changed. close old streams and open new ones
Close();
if (!File.Exists(latestFile))
return;

CurrentSaveName = Directory.GetParent(Directory.GetParent(latestFile).FullName).Name;
stream = new FileStream(latestFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
reader = new StreamReader(stream);
currentFile = latestFile;
CurrentSaveName = Directory.GetParent(Directory.GetParent(latestFile).FullName).Name;
stream = new FileStream(latestFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
reader = new StreamReader(stream);
currentFile = latestFile;
}
}
catch (Exception) { }
}

private void Close()
Expand Down
76 changes: 19 additions & 57 deletions AATool/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Main : Game
private Display display;
private Screen mainScreen;
private Dictionary<Type, Screen> altScreens;
private FNotes notesWindow;

private void AddScreen(Screen screen) => altScreens[screen.GetType()] = screen;

Expand Down Expand Up @@ -63,7 +64,7 @@ protected override void Initialize()
mainScreen.Form.BringToFront();

base.Initialize();
CheckForUpdatesAsync();
UpdateHelper.TryCheckForUpdatesAsync();
}

protected override void Update(GameTime gameTime)
Expand All @@ -83,6 +84,22 @@ protected override void Update(GameTime gameTime)
foreach (var screen in altScreens.Values)
screen.UpdateRecursive(time);

//update notes screen
if (NotesSettings.Instance.Enabled)
{
if (notesWindow == null || notesWindow.IsDisposed)
{
notesWindow = new FNotes();
notesWindow.Show();
}
else if (TrackerSettings.IsPostExplorationUpdate)
notesWindow.UpdateCurrentSave(AdvancementTracker.CurrentSaveName);
else
notesWindow.UpdateCurrentSave(AchievementTracker.CurrentSaveName);
}
else if (notesWindow != null && !notesWindow.IsDisposed)
notesWindow.Close();

TrackerSettings.Instance.Update();
MainSettings.Instance.Update();
OverlaySettings.Instance.Update();
Expand Down Expand Up @@ -121,7 +138,7 @@ public static void ForceQuit()
{
string message = "Error: One or more required assets failed to load. Would you like to repair your installation?";
if (System.Windows.Forms.MessageBox.Show(message, caption, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Yes)
RunUpdateAssistant(true);
UpdateHelper.RunUpdateAssistant(true);
}
else
{
Expand All @@ -131,60 +148,5 @@ public static void ForceQuit()
}
Environment.Exit(1);
}

public static async void CheckForUpdatesAsync(bool failSilently = true)
{
string html;
using (var client = new WebClient())
html = await client.DownloadStringTaskAsync("https://github.com/DarwinBaker/AATool/releases/latest/");

//get latest github release page
int startIndex = html.IndexOf("DarwinBaker/AATool/releases/download/");
int endIndex = html.IndexOf(".zip");
string latestLink = html.Substring(startIndex, endIndex - startIndex);
string patchNotes = html.Substring(html.IndexOf("<div class=\"markdown-body\">"));
patchNotes = Regex.Replace(patchNotes.Substring(0, patchNotes.IndexOf("<details")), @"<[^>]*>", string.Empty);

//get latest version number from download link
string latestVersion = latestLink.Substring(latestLink.LastIndexOf('_') + 1);
string[] latestSplit = latestVersion.Split('.');
string latestDecimal = string.Empty;
for (int i = 0; i < latestSplit.Length; i++)
latestDecimal += (i == 1 ? "." : string.Empty) + latestSplit[i];

//get current version number from this exe
string currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string[] currentSplit = currentVersion.Split('.');
string currentDecimal = string.Empty;
for (int i = 0; i < currentSplit.Length; i++)
currentDecimal += (i == 1 ? "." : string.Empty) + currentSplit[i];

//compare version numbers to determine if updates are available
if (!double.TryParse(latestDecimal, out var latestNumber))
return;
if (latestNumber <= double.Parse(currentDecimal))
{
//aready have the latest version
if (!failSilently)
System.Windows.Forms.MessageBox.Show("You already have the lastest version (" + currentVersion + ") of CTM's AATool.", "No Updates Available");
return;
}

using (var dialog = new FUpdate(latestVersion, patchNotes))
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
RunUpdateAssistant(false);
}

public static void RunUpdateAssistant(bool isError = false)
{
//start update executable with "return to AATool" flag
using (var process = new Process())
{
process.StartInfo.FileName = "AAUpdate.exe";
process.StartInfo.Arguments = "-r";
process.Start();
Environment.Exit(isError ? 1 : 0);
}
}
}
}
1 change: 1 addition & 0 deletions AATool/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class Paths
//constant settings paths
public const string DIR_SETTINGS = "settings/";
public const string DIR_FAVORITES = DIR_SETTINGS + "favorites";
public const string DIR_NOTES = "notes/";

//constant asset paths
public const string DIR_ASSETS = "assets/";
Expand Down
4 changes: 2 additions & 2 deletions AATool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.3.0")]
[assembly: AssemblyFileVersion("1.2.3.0")]
[assembly: AssemblyVersion("1.2.4.0")]
[assembly: AssemblyFileVersion("1.2.4.0")]
[assembly: NeutralResourcesLanguage("en")]
29 changes: 29 additions & 0 deletions AATool/Settings/NotesSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;

namespace AATool.Settings
{
//i don't love the way these classes work, i might change them later
public class NotesSettings : SettingsGroup
{
public static NotesSettings Instance = new NotesSettings();

public const string ENABLED = "enabled";
public const string ALWAYS_ON_TOP = "always_on_top";

public bool Enabled { get => Get<bool>(ENABLED); set => Set(ENABLED, value); }
public bool AlwaysOnTop { get => Get<bool>(ALWAYS_ON_TOP); set => Set(ALWAYS_ON_TOP, value); }

private NotesSettings()
{
FileName = "notes";
Load();
}

public override void ResetToDefaults()
{
Entries = new Dictionary<string, object>();
Set(ENABLED, false);
Set(ALWAYS_ON_TOP, true);
}
}
}
6 changes: 4 additions & 2 deletions AATool/UI/Screens/OverlayScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ public OverlayScreen(Main main) : base(main, GameWindow.Create(main, 1920, 380),
Form.MaximumSize = new System.Drawing.Size(5120, 512);
Form.ResizeBegin += OnResizeBegin;
Form.ResizeEnd += OnResizeEnd;
Form.FormClosed += OnFormClosed;
Form.FormClosing += OnFormClosing;

MoveTo(new Point(0, 0));
if (settings.Enabled)
Show();
}

private void OnFormClosed(object sender, FormClosedEventArgs e)
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Form.Hide();
settings.Enabled = false;
settings.Save();
}
Expand Down
Loading

0 comments on commit 52ee40e

Please sign in to comment.