PKHeX/Misc/Util.cs

385 lines
16 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2014-10-10 02:59:57 +00:00
using System.Globalization;
using System.Drawing;
2014-08-01 05:36:42 +00:00
using System.IO;
2014-10-10 02:59:57 +00:00
using System.Windows.Forms;
2014-10-11 07:22:22 +00:00
using System.Drawing.Imaging;
2014-12-11 06:50:40 +00:00
using System.Text.RegularExpressions;
namespace PKHeX
{
public partial class Util
{
2014-08-12 23:37:18 +00:00
// Image Layering/Blending Utility
2014-10-11 07:22:22 +00:00
internal static Bitmap LayerImage(Image baseLayer, Image overLayer, int x, int y, double trans)
{
Bitmap overlayImage = (Bitmap)overLayer;
Bitmap newImage = (Bitmap)baseLayer;
2014-10-11 07:22:22 +00:00
if (baseLayer == null) return overlayImage;
for (int i = 0; i < (overlayImage.Width * overlayImage.Height); i++)
{
Color newColor = overlayImage.GetPixel(i % (overlayImage.Width), i / (overlayImage.Width));
Color oldColor = newImage.GetPixel(i % (overlayImage.Width) + x, i / (overlayImage.Width) + y);
newColor = Color.FromArgb((int)((double)(newColor.A) * trans), newColor.R, newColor.G, newColor.B); // Apply transparency change
// if (newColor.A != 0) // If Pixel isn't transparent, we'll overwrite the color.
{
// if (newColor.A < 100)
newColor = AlphaBlend(newColor, oldColor);
newImage.SetPixel(
i % (overlayImage.Width) + x,
i / (overlayImage.Width) + y,
newColor);
}
}
return newImage;
}
2014-10-11 07:22:22 +00:00
internal static Bitmap ChangeOpacity(Image img, double trans)
{
if (img == null) return null;
Bitmap bmp = new Bitmap(img.Width, img.Height); // Determining Width and Height of Source Image
Graphics graphics = Graphics.FromImage(bmp);
ColorMatrix colormatrix = new ColorMatrix();
colormatrix.Matrix33 = (float)trans;
ImageAttributes imgAttribute = new ImageAttributes();
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
graphics.Dispose(); // Releasing all resource used by graphics
return bmp;
}
internal static Color AlphaBlend(Color ForeGround, Color BackGround)
{
if (ForeGround.A == 0)
return BackGround;
if (BackGround.A == 0)
return ForeGround;
if (ForeGround.A == 255)
return ForeGround;
int Alpha = Convert.ToInt32(ForeGround.A) + 1;
int B = Alpha * ForeGround.B + (255 - Alpha) * BackGround.B >> 8;
int G = Alpha * ForeGround.G + (255 - Alpha) * BackGround.G >> 8;
int R = Alpha * ForeGround.R + (255 - Alpha) * BackGround.R >> 8;
int A = ForeGround.A;
if (BackGround.A == 255)
A = 255;
if (A > 255)
A = 255;
if (R > 255)
R = 255;
if (G > 255)
G = 255;
if (B > 255)
B = 255;
return Color.FromArgb(Math.Abs(A), Math.Abs(R), Math.Abs(G), Math.Abs(B));
}
2014-12-11 06:50:40 +00:00
// Strings and Paths
internal static FileInfo GetNewestFile(DirectoryInfo directory)
{
return directory.GetFiles()
.Union(directory.GetDirectories().Select(d => GetNewestFile(d)))
.OrderByDescending(f => (f == null ? DateTime.MinValue : f.LastWriteTime))
.FirstOrDefault();
}
internal static string NormalizePath(string path)
{
return Path.GetFullPath(new Uri(path).LocalPath)
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
2014-12-11 06:50:40 +00:00
internal static string GetTempFolder()
2014-08-12 23:37:18 +00:00
{
2014-12-11 06:50:40 +00:00
return Path.Combine(Path.GetTempPath(), "3DSSE");
2014-08-12 23:37:18 +00:00
}
2014-12-11 06:50:40 +00:00
internal static string GetCacheFolder()
2014-08-12 23:37:18 +00:00
{
return Path.Combine(GetBackupLocation(), "cache");
}
internal static string GetRegistryValue(string key)
{
Microsoft.Win32.RegistryKey currentUser = Microsoft.Win32.Registry.CurrentUser;
Microsoft.Win32.RegistryKey key3 = currentUser.OpenSubKey(GetRegistryBase());
if (key3 == null)
return null;
2014-11-28 10:14:15 +00:00
2014-08-12 23:37:18 +00:00
string str = key3.GetValue(key) as string;
key3.Close();
currentUser.Close();
return str;
}
internal static string GetRegistryBase()
{
return @"SOFTWARE\CYBER Gadget\3DSSaveEditor";
}
2014-10-11 07:22:22 +00:00
internal static string GetBackupLocation()
2014-08-12 23:37:18 +00:00
{
string registryValue = GetRegistryValue("Location");
if (!string.IsNullOrEmpty(registryValue))
{
Directory.CreateDirectory(registryValue);
return registryValue;
}
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + Path.DirectorySeparatorChar + "3DSSaveBank";
Directory.CreateDirectory(path);
return path;
}
internal static string GetSDFLocation()
{
2014-11-28 21:18:44 +00:00
try
{
2014-12-11 06:50:40 +00:00
// Start by checking if the 3DS file path exists or not.
2014-11-28 21:18:44 +00:00
string path_SDF = null;
string[] DriveList = Environment.GetLogicalDrives();
2014-12-11 06:50:40 +00:00
for (int i = 1; i < DriveList.Length; i++) // Skip first drive (some users still have floppy drives and would chew up time!)
{
2014-11-29 03:49:08 +00:00
string potentialPath_SDF = NormalizePath(Path.Combine(DriveList[i], "filer" + Path.DirectorySeparatorChar + "UserSaveData"));
2014-11-28 21:18:44 +00:00
if (Directory.Exists(potentialPath_SDF))
2014-12-11 06:50:40 +00:00
{ path_SDF = potentialPath_SDF; break; }
2014-11-28 21:18:44 +00:00
}
if (path_SDF == null)
return null;
else
{
// 3DS data found in SD card reader. Let's get the title folder location!
string[] folders = Directory.GetDirectories(path_SDF, "*", System.IO.SearchOption.TopDirectoryOnly);
2014-11-28 21:18:44 +00:00
// Loop through all the folders in the Nintendo 3DS folder to see if any of them contain 'title'.
for (int i = folders.Length - 1; i >= 0; i--)
2014-11-28 21:18:44 +00:00
{
if (File.Exists(Path.Combine(folders[i], "000011c4" + Path.DirectorySeparatorChar + "main"))) return Path.Combine(folders[i], "000011c4"); // OR
if (File.Exists(Path.Combine(folders[i], "000011c5" + Path.DirectorySeparatorChar + "main"))) return Path.Combine(folders[i], "000011c5"); // AS
if (File.Exists(Path.Combine(folders[i], "0000055d" + Path.DirectorySeparatorChar + "main"))) return Path.Combine(folders[i], "0000055d"); // X
if (File.Exists(Path.Combine(folders[i], "0000055e" + Path.DirectorySeparatorChar + "main"))) return Path.Combine(folders[i], "0000055e"); // Y
2014-11-28 21:18:44 +00:00
}
2014-12-11 06:50:40 +00:00
return null; // Fallthrough
}
}
2014-11-28 21:18:44 +00:00
catch { return null; }
}
2014-10-11 07:22:22 +00:00
internal static string CleanFileName(string fileName)
{
return Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
}
2014-10-11 07:22:22 +00:00
internal static string TrimFromZero(string input)
{
int index = input.IndexOf('\0');
if (index < 0)
return input;
return input.Substring(0, index);
}
2014-10-11 07:22:22 +00:00
internal static string[] getStringList(string f, string l)
{
object txt = Properties.Resources.ResourceManager.GetObject("text_" + f + "_" + l); // Fetch File, \n to list.
List<string> rawlist = ((string)txt).Split(new char[] { '\n' }).ToList();
string[] stringdata = new string[rawlist.Count];
for (int i = 0; i < rawlist.Count; i++)
2014-11-28 17:56:29 +00:00
stringdata[i] = rawlist[i].Trim();
return stringdata;
}
2014-12-11 06:50:40 +00:00
// Randomization
2014-10-11 07:22:22 +00:00
internal static Random rand = new Random();
internal static uint rnd32()
{
2014-10-10 02:59:57 +00:00
return (uint)(rand.Next(1 << 30)) << 2 | (uint)(rand.Next(1 << 2));
}
2014-12-11 06:50:40 +00:00
// Data Retrieval
2014-10-11 07:22:22 +00:00
internal static int ToInt32(TextBox tb)
{
string value = tb.Text;
return ToInt32(value);
}
2014-10-11 07:22:22 +00:00
internal static uint ToUInt32(TextBox tb)
{
string value = tb.Text;
return ToUInt32(value);
}
2014-10-11 07:22:22 +00:00
internal static int ToInt32(MaskedTextBox tb)
2014-10-10 02:59:57 +00:00
{
string value = tb.Text;
return ToInt32(value);
2014-10-10 02:59:57 +00:00
}
2014-10-11 07:22:22 +00:00
internal static uint ToUInt32(MaskedTextBox tb)
2014-10-10 02:59:57 +00:00
{
string value = tb.Text;
return ToUInt32(value);
2014-10-10 02:59:57 +00:00
}
2014-10-11 07:22:22 +00:00
internal static int ToInt32(String value)
2014-10-10 02:59:57 +00:00
{
value = value.Replace(" ", "");
if (String.IsNullOrEmpty(value))
return 0;
2014-10-10 02:59:57 +00:00
try
{
value = value.TrimEnd(new char[]{'_'});
2014-11-28 10:14:15 +00:00
return Int32.Parse(value);
}
2014-11-28 10:14:15 +00:00
catch { return 0; }
2014-10-10 02:59:57 +00:00
}
2014-10-11 07:22:22 +00:00
internal static uint ToUInt32(String value)
2014-10-10 02:59:57 +00:00
{
value = value.Replace(" ", "");
2014-10-10 02:59:57 +00:00
if (String.IsNullOrEmpty(value))
return 0;
2014-10-10 02:59:57 +00:00
try
{
value = value.TrimEnd(new char[]{'_'});
return UInt32.Parse(value);
}
2014-11-28 10:14:15 +00:00
catch { return 0; }
}
2014-10-11 07:22:22 +00:00
internal static uint getHEXval(TextBox tb)
{
2014-10-10 02:59:57 +00:00
if (tb.Text == null)
return 0;
2014-10-11 07:22:22 +00:00
string str = getOnlyHex(tb.Text);
2014-10-10 02:59:57 +00:00
return UInt32.Parse(str, NumberStyles.HexNumber);
}
2014-10-11 07:22:22 +00:00
internal static int getIndex(ComboBox cb)
2014-10-10 02:59:57 +00:00
{
int val = 0;
2014-11-27 09:26:39 +00:00
if (cb.SelectedValue == null)
return 0;
try
2014-11-28 10:14:15 +00:00
{ val = Util.ToInt32(cb.SelectedValue.ToString()); }
2014-10-10 02:59:57 +00:00
catch
2014-11-28 10:14:15 +00:00
{ val = cb.SelectedIndex; if (val < 0) val = 0; }
2014-10-10 02:59:57 +00:00
return val;
}
2014-10-11 07:22:22 +00:00
internal static string getOnlyHex(string str)
2014-10-10 02:59:57 +00:00
{
2014-10-11 07:22:22 +00:00
if (str == null) return "0";
2014-10-10 02:59:57 +00:00
2014-10-11 07:22:22 +00:00
char c;
string s = "";
2014-10-11 07:22:22 +00:00
for (int i = 0; i < str.Length; i++)
2014-10-10 02:59:57 +00:00
{
2014-10-11 07:22:22 +00:00
c = str[i];
2014-10-10 02:59:57 +00:00
// filter for hex
2014-10-11 07:22:22 +00:00
if ((c < 0x0047 && c > 0x002F) || (c < 0x0067 && c > 0x0060))
s+= c;
2014-10-10 02:59:57 +00:00
else
System.Media.SystemSounds.Beep.Play();
}
2014-10-11 07:22:22 +00:00
if (s.Length == 0)
s = "0";
return s;
}
2014-12-11 06:50:40 +00:00
// Data Manipulation
internal static void Shuffle<T>(T[] array)
{
int n = array.Length;
for (int i = 0; i < n; i++)
{
int r = i + (int)(rand.NextDouble() * (n - i));
T t = array[r];
array[r] = array[i];
array[i] = t;
}
}
// Form Manipulation
internal static void TranslateInterface(string FORM_NAME, string lang, Control.ControlCollection Controls, MenuStrip menu = null)
2014-10-11 07:22:22 +00:00
{
2014-12-11 06:50:40 +00:00
// Fetch a File
// Check to see if a the translation file exists in the same folder as the executable
string externalLangPath = System.Windows.Forms.Application.StartupPath + Path.DirectorySeparatorChar + "lang_" + lang + ".txt";
string[] rawlist;
if (File.Exists(externalLangPath))
rawlist = File.ReadAllLines(externalLangPath);
else
{
object txt;
txt = Properties.Resources.ResourceManager.GetObject("lang_" + lang); // Fetch File, \n to list.
if (txt == null) return; // Translation file does not exist as a resource; abort this function and don't translate UI.
string[] stringSeparators = new string[] { "\r\n" }; // Resource files are notepad compatible
rawlist = ((string)txt).Split(stringSeparators, StringSplitOptions.None);
rawlist = rawlist.Select(i => i.Trim()).ToArray(); // Remove trailing spaces
}
2014-10-11 07:22:22 +00:00
2014-12-11 06:50:40 +00:00
string[] stringdata = new string[rawlist.Length];
int itemsToRename = 0;
int start = Array.IndexOf(rawlist, "! " + FORM_NAME);
if (start < 0) return; // No Translation loaded for this Form.
for (int i = start; i < rawlist.Length; i++)
2014-10-11 07:22:22 +00:00
{
2014-12-11 06:50:40 +00:00
// Find our starting point
if (rawlist[i] == "! " + FORM_NAME) // Start our data
{
// Copy our Control Names and Text to a new array for later processing.
for (int j = i + 1; j < rawlist.Length; j++)
{
if (rawlist[j].Length == 0)
continue; // Skip Over Empty Lines, errhandled
if (rawlist[j][0].ToString() != "-") // If line is not a comment line...
{
if (rawlist[j][0].ToString() == "!") // Stop if we have reached the end of translation
break; // exit inner loop
stringdata[itemsToRename] = rawlist[j]; // Add the entry to process later.
itemsToRename++;
}
}
break; // exit outer loop
}
2014-10-11 07:22:22 +00:00
}
2014-12-11 06:50:40 +00:00
// Now that we have our items to rename in: Control = Text format, let's execute the changes!
for (int i = 0; i < itemsToRename; i++)
{
string[] SplitString = Regex.Split(stringdata[i], " = ");
if (SplitString.Length < 2)
continue; // Error in Input, errhandled
string ctrl = SplitString[0]; // Control to change the text of...
string text = SplitString[1]; // Text to set Control.Text to...
Control[] controllist = Controls.Find(ctrl, true);
if (controllist.Length == 0) // If Control isn't found...
{
// Menu Items can't be found with Controls.Find as they aren't Controls
ToolStripDropDownItem TSI = (ToolStripDropDownItem)menu.Items[ctrl];
if (TSI != null)
{
// We'll rename the main and child in a row.
string[] ToolItems = Regex.Split(SplitString[1], " ; ");
TSI.Text = ToolItems[0]; // Set parent's text first
if (TSI.DropDownItems.Count != ToolItems.Length - 1)
continue; // Error in Input, errhandled
for (int ti = 1; ti <= TSI.DropDownItems.Count; ti++)
TSI.DropDownItems[ti - 1].Text = ToolItems[ti]; // Set child text
}
// If not found, it is not something to rename and is thus skipped.
}
else // Set the input control's text.
controllist[0].Text = text;
}
}
// Message Displays
internal static DialogResult Error(params string[] lines)
{
System.Media.SystemSounds.Exclamation.Play();
2014-12-11 07:00:25 +00:00
string msg = String.Join(Environment.NewLine + Environment.NewLine, lines);
2014-12-11 06:50:40 +00:00
return (DialogResult)MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
internal static DialogResult Alert(params string[] lines)
{
System.Media.SystemSounds.Asterisk.Play();
2014-12-11 07:00:25 +00:00
string msg = String.Join(Environment.NewLine + Environment.NewLine, lines);
2014-12-11 06:50:40 +00:00
return (DialogResult)MessageBox.Show(msg, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
internal static DialogResult Prompt(MessageBoxButtons btn, params string[] lines)
{
System.Media.SystemSounds.Question.Play();
2014-12-11 07:00:25 +00:00
string msg = String.Join(Environment.NewLine + Environment.NewLine, lines);
2014-12-11 06:50:40 +00:00
return (DialogResult)MessageBox.Show(msg, "Prompt", btn, MessageBoxIcon.Asterisk);
}
}
}