Skip to content

Commit

Permalink
Addresses issue #33 and adds new auto load feature (#34)
Browse files Browse the repository at this point in the history
* Update to android 11 using All Files Access permission

* Removed special/forbidden chars from date

* Auto loads past game saves

* Fixed string name being printed instead of contents

* Fixed initial crash

* Update AndroidManifest.xml

* Fixed initial crash part 2

* Moved prompt for all files access

* Changed date format for better sorting in directories

* Added icon
  • Loading branch information
eco9898 authored Oct 9, 2021
1 parent d44efa5 commit 6205f77
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
2 changes: 1 addition & 1 deletion PKHeX.Android/PKHeX.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v10.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v11.0</TargetFrameworkVersion>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
<NuGetPackageImportStamp>
Expand Down
5 changes: 3 additions & 2 deletions PKHeX.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.ProjectPokemon.PKHeX" android:installLocation="auto">
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="29" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="3" android:versionName="1.1.1" package="com.ProjectPokemon.PKHeX" android:installLocation="auto">
<uses-sdk android:minSdkVersion="30" android:targetSdkVersion="30" />
<application android:label="PKHeX.Android"></application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
</manifest>
2 changes: 2 additions & 0 deletions PKHeX.Android/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@

// Add some common permissions, these can be removed if not needed
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
[assembly: UsesPermission(Android.Manifest.Permission.ReadExternalStorage)]
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
[assembly: UsesPermission(Android.Manifest.Permission.ManageExternalStorage)]
42 changes: 39 additions & 3 deletions PKHeX.Mobile/Logic/FileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ namespace PKHeX.Mobile.Logic
// todo: rename this class to not clash with PKHeX.Core
public static class FileUtil
{
private static string outputFolder = "/storage/emulated/0/PkHex/";
public static async Task<FileResult> PickFile()
{
var fileData = await FilePicker.PickAsync(PickOptions.Default).ConfigureAwait(false);
if (fileData == null)
return null; // user canceled file picking
Debug.WriteLine($"File name chosen: {fileData.FileName}");
Debug.WriteLine($"File path chosen: {fileData.FullPath}");
return fileData;
}

Expand Down Expand Up @@ -55,6 +57,26 @@ public static async Task<SaveFile> TryGetSaveFile()
}
}

public static SaveFile TryGetSaveFile(string filePath)
{
try
{
var data = File.ReadAllBytes(filePath);
var len = data.Length;
bool isPossibleSAV = SaveUtil.IsSizeValid(len);
if (!isPossibleSAV)
return null;
var sav = SaveUtil.GetVariantSAV(data);
sav?.Metadata.SetExtraInfo(filePath);
return sav;
}
catch
{
//Ignore errors as this is meant to be a background scanning function
return null;
}
}

public static async Task<bool> ExportSAV(SaveFile sav)
{
if (!sav.State.Exportable)
Expand All @@ -63,13 +85,26 @@ public static async Task<bool> ExportSAV(SaveFile sav)
return false;
}

//Create directory structure
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
}
String myDate = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss");
if (!Directory.Exists(outputFolder + myDate + "/"))
{
Directory.CreateDirectory(outputFolder + myDate + "/");
}

var data = sav.Write();
var path = sav.Metadata.FilePath;
var path = outputFolder + myDate + "/" + Path.GetFileName(sav.Metadata.FilePath);
sav?.Metadata.SetExtraInfo(path);
Debug.WriteLine($"File path moved: {sav.Metadata.FilePath}");
try
{
if (sav.State.Exportable)
await SaveBackup(sav).ConfigureAwait(false);

File.Create(path).Close();
await File.WriteAllBytesAsync(path, data).ConfigureAwait(false);
await UserDialogs.Instance.AlertAsync($"Exported save file to: {path}").ConfigureAwait(false);
return true;
Expand All @@ -78,7 +113,8 @@ public static async Task<bool> ExportSAV(SaveFile sav)
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
await UserDialogs.Instance.AlertAsync($"Failed: {ex}").ConfigureAwait(false);
await UserDialogs.Instance.AlertAsync($"Failed to access \"" + outputFolder + "\" please grant All File Access Special Permision").ConfigureAwait(false);
//await UserDialogs.Instance.AlertAsync($"Failed: {ex}").ConfigureAwait(false);
return false;
}
}
Expand Down
1 change: 1 addition & 0 deletions PKHeX.Mobile/PKHeX.Mobile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<LangVersion>latest</LangVersion>
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
32 changes: 32 additions & 0 deletions PKHeX.Mobile/Views/Loader.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,47 @@
using PKHeX.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.IO;

namespace PKHeX.Mobile.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Loader : ContentPage
{
private static string outputFolder = "/storage/emulated/0/PkHex/";
public Loader()
{
InitializeComponent();
initialLoad();
}

private async void initialLoad()
{
try
{
if (CV_Saves.SelectedItem == null)
{
if (Directory.Exists(outputFolder))
{
foreach (string filePath in Directory.EnumerateFiles(outputFolder, "*", SearchOption.AllDirectories))
{
var sav = Logic.FileUtil.TryGetSaveFile(filePath);
if (sav != null)
{
var match = VM.Saves.FirstOrDefault(z => z.LocatedAt(sav.Metadata.FilePath));
if (match != null)
CV_Saves.SelectedItem = match;
else
await LoadNewSaveFile(sav).ConfigureAwait(false);
}
}
}
}
} catch
{
//first time load, ignore error
return;
}
}

private async void TgrOpenTapped(object sender, EventArgs e)
Expand Down
Binary file added PKHeX.Mobile/icon.ico
Binary file not shown.

0 comments on commit 6205f77

Please sign in to comment.