2014-07-28 22:28:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2014-10-10 02:59:57 +00:00
|
|
|
|
using System.Globalization;
|
2014-07-28 22:28:06 +00:00
|
|
|
|
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;
|
2014-07-28 22:28:06 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
public class Util
|
2014-07-28 22:28:06 +00:00
|
|
|
|
{
|
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)
|
2014-07-28 22:28:06 +00:00
|
|
|
|
{
|
|
|
|
|
Bitmap overlayImage = (Bitmap)overLayer;
|
|
|
|
|
Bitmap newImage = (Bitmap)baseLayer;
|
2014-10-11 07:22:22 +00:00
|
|
|
|
if (baseLayer == null) return overlayImage;
|
2014-07-28 22:28:06 +00:00
|
|
|
|
for (int i = 0; i < (overlayImage.Width * overlayImage.Height); i++)
|
|
|
|
|
{
|
|
|
|
|
Color newColor = overlayImage.GetPixel(i % (overlayImage.Width), i / (overlayImage.Width));
|
2014-08-03 05:15:47 +00:00
|
|
|
|
Color oldColor = newImage.GetPixel(i % (overlayImage.Width) + x, i / (overlayImage.Width) + y);
|
2015-03-11 01:44:51 +00:00
|
|
|
|
newColor = Color.FromArgb((int)(newColor.A * trans), newColor.R, newColor.G, newColor.B); // Apply transparency change
|
2014-08-03 05:15:47 +00:00
|
|
|
|
// if (newColor.A != 0) // If Pixel isn't transparent, we'll overwrite the color.
|
2014-07-28 22:28:06 +00:00
|
|
|
|
{
|
2014-08-03 05:15:47 +00:00
|
|
|
|
// if (newColor.A < 100)
|
|
|
|
|
newColor = AlphaBlend(newColor, oldColor);
|
2014-07-28 22:28:06 +00:00
|
|
|
|
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);
|
2015-03-11 01:44:51 +00:00
|
|
|
|
ColorMatrix colormatrix = new ColorMatrix {Matrix33 = (float) trans};
|
2014-10-11 07:22:22 +00:00
|
|
|
|
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)
|
2014-08-03 05:15:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (ForeGround.A == 0)
|
|
|
|
|
return BackGround;
|
|
|
|
|
if (BackGround.A == 0)
|
|
|
|
|
return ForeGround;
|
|
|
|
|
if (ForeGround.A == 255)
|
|
|
|
|
return ForeGround;
|
2014-12-23 06:39:22 +00:00
|
|
|
|
int Alpha = Convert.ToInt32(ForeGround.A);
|
2014-08-03 05:15:47 +00:00
|
|
|
|
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
|
2014-11-28 03:26:42 +00:00
|
|
|
|
internal static FileInfo GetNewestFile(DirectoryInfo directory)
|
|
|
|
|
{
|
|
|
|
|
return directory.GetFiles()
|
2015-03-11 01:44:51 +00:00
|
|
|
|
.Union(directory.GetDirectories().Select(GetNewestFile))
|
2014-11-28 03:26:42 +00:00
|
|
|
|
.OrderByDescending(f => (f == null ? DateTime.MinValue : f.LastWriteTime))
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
}
|
2014-11-28 23:46:09 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2015-01-27 06:05:04 +00:00
|
|
|
|
internal static string get3DSLocation()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string path_3DS = null;
|
|
|
|
|
string[] DriveList = Environment.GetLogicalDrives();
|
|
|
|
|
for (int i = 1; i < DriveList.Length; i++) // Skip first drive (some users still have floppy drives and would chew up time!)
|
|
|
|
|
{
|
2015-09-05 01:50:14 +00:00
|
|
|
|
string potentialPath = Path.Combine(DriveList[i], "Nintendo 3DS");
|
2015-03-12 04:44:12 +00:00
|
|
|
|
if (!Directory.Exists(potentialPath)) continue;
|
|
|
|
|
|
|
|
|
|
path_3DS = potentialPath; break;
|
2015-01-27 06:05:04 +00:00
|
|
|
|
}
|
|
|
|
|
return path_3DS;
|
|
|
|
|
}
|
2015-03-11 01:44:51 +00:00
|
|
|
|
catch { return null; }
|
2015-01-27 06:05:04 +00:00
|
|
|
|
}
|
2014-11-28 03:26:42 +00:00
|
|
|
|
internal static string GetSDFLocation()
|
|
|
|
|
{
|
2014-11-28 21:18:44 +00:00
|
|
|
|
try
|
2014-11-28 03:26:42 +00:00
|
|
|
|
{
|
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-28 03:26:42 +00:00
|
|
|
|
{
|
2014-11-29 03:49:08 +00:00
|
|
|
|
string potentialPath_SDF = NormalizePath(Path.Combine(DriveList[i], "filer" + Path.DirectorySeparatorChar + "UserSaveData"));
|
2015-03-12 04:44:12 +00:00
|
|
|
|
if (!Directory.Exists(potentialPath_SDF)) continue;
|
|
|
|
|
|
|
|
|
|
path_SDF = potentialPath_SDF; break;
|
2014-11-28 21:18:44 +00:00
|
|
|
|
}
|
|
|
|
|
if (path_SDF == null)
|
|
|
|
|
return null;
|
2015-03-11 01:44:51 +00:00
|
|
|
|
// 3DS data found in SD card reader. Let's get the title folder location!
|
|
|
|
|
string[] folders = Directory.GetDirectories(path_SDF, "*", SearchOption.TopDirectoryOnly);
|
|
|
|
|
Array.Sort(folders); // Don't need Modified Date, sort by path names just in case.
|
2014-11-28 03:26:42 +00:00
|
|
|
|
|
2015-03-11 01:44:51 +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--)
|
|
|
|
|
{
|
|
|
|
|
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 03:26:42 +00:00
|
|
|
|
}
|
2015-03-11 01:44:51 +00:00
|
|
|
|
return null; // Fallthrough
|
2014-11-28 03:26:42 +00:00
|
|
|
|
}
|
2014-11-28 21:18:44 +00:00
|
|
|
|
catch { return null; }
|
2014-11-28 03:26:42 +00:00
|
|
|
|
}
|
2014-10-11 07:22:22 +00:00
|
|
|
|
internal static string CleanFileName(string fileName)
|
2014-08-31 20:32:04 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
2014-08-31 20:32:04 +00:00
|
|
|
|
{
|
2015-09-18 23:33:37 +00:00
|
|
|
|
int index = input.IndexOf('\0');
|
|
|
|
|
return index < 0 ? input : input.Substring(0, index);
|
2014-08-31 20:32:04 +00:00
|
|
|
|
}
|
2014-10-11 07:22:22 +00:00
|
|
|
|
internal static string[] getStringList(string f, string l)
|
2014-08-31 20:32:04 +00:00
|
|
|
|
{
|
|
|
|
|
object txt = Properties.Resources.ResourceManager.GetObject("text_" + f + "_" + l); // Fetch File, \n to list.
|
2015-03-11 01:44:51 +00:00
|
|
|
|
List<string> rawlist = ((string)txt).Split(new[] { '\n' }).ToList();
|
2014-08-31 20:32:04 +00:00
|
|
|
|
|
|
|
|
|
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();
|
2014-08-31 20:32:04 +00:00
|
|
|
|
|
|
|
|
|
return stringdata;
|
|
|
|
|
}
|
2014-12-13 06:58:34 +00:00
|
|
|
|
internal static string[] getSimpleStringList(string f)
|
|
|
|
|
{
|
|
|
|
|
object txt = Properties.Resources.ResourceManager.GetObject(f); // Fetch File, \n to list.
|
2015-03-11 01:44:51 +00:00
|
|
|
|
List<string> rawlist = ((string)txt).Split(new[] { '\n' }).ToList();
|
2014-12-13 06:58:34 +00:00
|
|
|
|
|
|
|
|
|
string[] stringdata = new string[rawlist.Count];
|
|
|
|
|
for (int i = 0; i < rawlist.Count; i++)
|
|
|
|
|
stringdata[i] = rawlist[i].Trim();
|
2014-08-31 20:32:04 +00:00
|
|
|
|
|
2014-12-13 06:58:34 +00:00
|
|
|
|
return stringdata;
|
|
|
|
|
}
|
2015-02-03 05:51:08 +00:00
|
|
|
|
internal static string[] getNulledStringArray(string[] SimpleStringList)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
string[] newlist = new string[ToInt32(SimpleStringList[SimpleStringList.Length - 1].Split(',')[0]) + 1];
|
2015-02-03 05:51:08 +00:00
|
|
|
|
for (int i = 1; i < SimpleStringList.Length; i++)
|
2015-03-11 01:44:51 +00:00
|
|
|
|
newlist[ToInt32(SimpleStringList[i].Split(',')[0])] = SimpleStringList[i].Split(',')[1];
|
2015-02-03 05:51:08 +00:00
|
|
|
|
return newlist;
|
|
|
|
|
}
|
|
|
|
|
catch { return null; }
|
|
|
|
|
}
|
|
|
|
|
|
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-08-31 20:32:04 +00:00
|
|
|
|
{
|
2014-10-10 02:59:57 +00:00
|
|
|
|
return (uint)(rand.Next(1 << 30)) << 2 | (uint)(rand.Next(1 << 2));
|
|
|
|
|
}
|
2014-08-31 20:32:04 +00:00
|
|
|
|
|
2014-12-11 06:50:40 +00:00
|
|
|
|
// Data Retrieval
|
2014-10-11 07:22:22 +00:00
|
|
|
|
internal static int ToInt32(TextBox tb)
|
2015-01-03 01:07:29 +00:00
|
|
|
|
{
|
|
|
|
|
string value = tb.Text;
|
|
|
|
|
return ToInt32(value);
|
|
|
|
|
}
|
2014-10-11 07:22:22 +00:00
|
|
|
|
internal static uint ToUInt32(TextBox tb)
|
2015-01-03 01:07:29 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
2014-11-27 20:15:32 +00:00
|
|
|
|
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;
|
2014-11-27 20:15:32 +00:00
|
|
|
|
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
|
|
|
|
{
|
2014-11-28 09:44:43 +00:00
|
|
|
|
value = value.Replace(" ", "");
|
2015-01-03 01:07:29 +00:00
|
|
|
|
if (String.IsNullOrEmpty(value))
|
|
|
|
|
return 0;
|
2014-10-10 02:59:57 +00:00
|
|
|
|
try
|
2015-01-03 01:07:29 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
value = value.TrimEnd(new[] { '_' });
|
2015-01-03 01:07:29 +00:00
|
|
|
|
return Int32.Parse(value);
|
2014-11-27 20:15:32 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2014-11-28 09:44:43 +00:00
|
|
|
|
value = value.Replace(" ", "");
|
2014-10-10 02:59:57 +00:00
|
|
|
|
if (String.IsNullOrEmpty(value))
|
2015-01-03 01:07:29 +00:00
|
|
|
|
return 0;
|
2014-10-10 02:59:57 +00:00
|
|
|
|
try
|
2014-11-27 20:15:32 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
value = value.TrimEnd(new[] { '_' });
|
2014-11-27 20:15:32 +00:00
|
|
|
|
return UInt32.Parse(value);
|
|
|
|
|
}
|
2014-11-28 10:14:15 +00:00
|
|
|
|
catch { return 0; }
|
2014-08-31 20:32:04 +00:00
|
|
|
|
}
|
2014-10-11 07:22:22 +00:00
|
|
|
|
internal static uint getHEXval(TextBox tb)
|
2014-08-31 20:32:04 +00:00
|
|
|
|
{
|
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
|
|
|
|
{
|
2015-03-12 04:44:12 +00:00
|
|
|
|
int val;
|
|
|
|
|
if (cb.SelectedValue == null) return 0;
|
|
|
|
|
|
|
|
|
|
try { val = int.Parse(cb.SelectedValue.ToString()); }
|
|
|
|
|
catch { val = cb.SelectedIndex; if (val < 0) val = 0; }
|
2014-11-27 09:26:39 +00:00
|
|
|
|
|
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
|
|
|
|
string s = "";
|
2014-08-31 20:32:04 +00:00
|
|
|
|
|
2015-03-11 01:44:51 +00:00
|
|
|
|
foreach (char c in str)
|
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))
|
2015-01-03 01:07:29 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-23 06:39:22 +00:00
|
|
|
|
// Form Translation
|
2014-12-20 04:18:12 +00:00
|
|
|
|
internal static void TranslateInterface(Control form, string lang, MenuStrip menu = null)
|
2014-10-11 07:22:22 +00:00
|
|
|
|
{
|
2014-12-15 00:50:15 +00:00
|
|
|
|
string FORM_NAME = form.Name;
|
2014-12-20 04:18:12 +00:00
|
|
|
|
Control.ControlCollection Controls = form.Controls;
|
2014-12-15 00:50:15 +00:00
|
|
|
|
// debug(Controls);
|
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
|
2015-03-11 01:44:51 +00:00
|
|
|
|
string externalLangPath = Application.StartupPath + Path.DirectorySeparatorChar + "lang_" + lang + ".txt";
|
2014-12-11 06:50:40 +00:00
|
|
|
|
string[] rawlist;
|
|
|
|
|
if (File.Exists(externalLangPath))
|
|
|
|
|
rawlist = File.ReadAllLines(externalLangPath);
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
object txt = Properties.Resources.ResourceManager.GetObject("lang_" + lang);
|
2014-12-11 06:50:40 +00:00
|
|
|
|
if (txt == null) return; // Translation file does not exist as a resource; abort this function and don't translate UI.
|
2015-03-11 01:44:51 +00:00
|
|
|
|
rawlist = ((string)txt).Split(new[] { "\n" }, StringSplitOptions.None);
|
2014-12-11 06:50:40 +00:00
|
|
|
|
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;
|
2014-12-15 00:50:15 +00:00
|
|
|
|
for (int i = 0; i < rawlist.Length; i++)
|
2014-10-11 07:22:22 +00:00
|
|
|
|
{
|
2014-12-11 06:50:40 +00:00
|
|
|
|
// Find our starting point
|
2015-03-12 04:44:12 +00:00
|
|
|
|
if (!rawlist[i].Contains("! " + FORM_NAME)) continue;
|
|
|
|
|
|
|
|
|
|
// Allow renaming of the Window Title
|
|
|
|
|
string[] WindowName = Regex.Split(rawlist[i], " = ");
|
|
|
|
|
if (WindowName.Length > 1) form.Text = WindowName[1];
|
|
|
|
|
// Copy our Control Names and Text to a new array for later processing.
|
|
|
|
|
for (int j = i + 1; j < rawlist.Length; j++)
|
2014-12-11 06:50:40 +00:00
|
|
|
|
{
|
2015-03-12 04:44:12 +00:00
|
|
|
|
if (rawlist[j].Length == 0) continue; // Skip Over Empty Lines, errhandled
|
|
|
|
|
if (rawlist[j][0].ToString() == "-") continue; // Keep translating if line is a comment line
|
|
|
|
|
if (rawlist[j][0].ToString() == "!") // Stop if we have reached the end of translation
|
|
|
|
|
goto rename;
|
|
|
|
|
stringdata[itemsToRename] = rawlist[j]; // Add the entry to process later.
|
|
|
|
|
itemsToRename++;
|
2014-12-11 06:50:40 +00:00
|
|
|
|
}
|
2014-10-11 07:22:22 +00:00
|
|
|
|
}
|
2014-12-15 00:50:15 +00:00
|
|
|
|
return; // Not Found
|
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!
|
2014-12-15 00:50:15 +00:00
|
|
|
|
rename:
|
2014-12-11 06:50:40 +00:00
|
|
|
|
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...
|
2015-01-03 01:07:29 +00:00
|
|
|
|
try
|
2014-12-11 06:50:40 +00:00
|
|
|
|
{
|
2015-01-03 01:07:29 +00:00
|
|
|
|
// Menu Items can't be found with Controls.Find as they aren't Controls
|
|
|
|
|
ToolStripDropDownItem TSI = (ToolStripDropDownItem)menu.Items[ctrl];
|
2015-03-12 04:44:12 +00:00
|
|
|
|
if (TSI == null) continue;
|
|
|
|
|
|
|
|
|
|
// 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
|
2015-01-03 01:07:29 +00:00
|
|
|
|
// If not found, it is not something to rename and is thus skipped.
|
2014-12-11 06:50:40 +00:00
|
|
|
|
}
|
2015-01-03 01:07:29 +00:00
|
|
|
|
catch { }
|
2014-12-11 06:50:40 +00:00
|
|
|
|
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);
|
2015-03-11 01:44:51 +00:00
|
|
|
|
return MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2014-12-11 06:50:40 +00:00
|
|
|
|
}
|
|
|
|
|
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);
|
2015-03-11 01:44:51 +00:00
|
|
|
|
return MessageBox.Show(msg, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
2014-12-11 06:50:40 +00:00
|
|
|
|
}
|
|
|
|
|
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);
|
2015-03-11 01:44:51 +00:00
|
|
|
|
return MessageBox.Show(msg, "Prompt", btn, MessageBoxIcon.Asterisk);
|
2014-08-31 20:32:04 +00:00
|
|
|
|
}
|
2014-12-14 18:31:53 +00:00
|
|
|
|
|
|
|
|
|
// DataSource Providing
|
|
|
|
|
public class cbItem
|
|
|
|
|
{
|
|
|
|
|
public string Text { get; set; }
|
|
|
|
|
public object Value { get; set; }
|
|
|
|
|
}
|
|
|
|
|
internal static List<cbItem> getCBList(string textfile, string lang)
|
|
|
|
|
{
|
|
|
|
|
// Set up
|
2015-03-11 01:44:51 +00:00
|
|
|
|
string[] inputCSV = getSimpleStringList(textfile);
|
2014-12-14 18:31:53 +00:00
|
|
|
|
|
|
|
|
|
// Get Language we're fetching for
|
2015-03-11 01:44:51 +00:00
|
|
|
|
int index = Array.IndexOf(new[] { "ja", "en", "fr", "de", "it", "es", "ko", "zh", }, lang);
|
2014-12-14 18:31:53 +00:00
|
|
|
|
|
|
|
|
|
// Set up our Temporary Storage
|
|
|
|
|
string[] unsortedList = new string[inputCSV.Length - 1];
|
|
|
|
|
int[] indexes = new int[inputCSV.Length - 1];
|
|
|
|
|
|
|
|
|
|
// Gather our data from the input file
|
|
|
|
|
for (int i = 1; i < inputCSV.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
string[] countryData = inputCSV[i].Split(',');
|
|
|
|
|
indexes[i - 1] = Convert.ToInt32(countryData[0]);
|
|
|
|
|
unsortedList[i - 1] = countryData[index + 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort our input data
|
|
|
|
|
string[] sortedList = new string[inputCSV.Length - 1];
|
|
|
|
|
Array.Copy(unsortedList, sortedList, unsortedList.Length);
|
|
|
|
|
Array.Sort(sortedList);
|
|
|
|
|
|
|
|
|
|
// Arrange the input data based on original number
|
2015-03-11 01:44:51 +00:00
|
|
|
|
return sortedList.Select(s => new cbItem
|
2014-12-14 18:31:53 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
Text = s,
|
|
|
|
|
Value = indexes[Array.IndexOf(unsortedList, s)]
|
|
|
|
|
}).ToList();
|
2014-12-14 18:31:53 +00:00
|
|
|
|
}
|
|
|
|
|
internal static List<cbItem> getCBList(string[] inStrings, params int[][] allowed)
|
|
|
|
|
{
|
|
|
|
|
List<cbItem> cbList = new List<cbItem>();
|
|
|
|
|
if (allowed == null)
|
2015-03-11 01:44:51 +00:00
|
|
|
|
allowed = new[] { Enumerable.Range(0, inStrings.Length).ToArray() };
|
2014-12-14 18:31:53 +00:00
|
|
|
|
|
|
|
|
|
foreach (int[] list in allowed)
|
|
|
|
|
{
|
|
|
|
|
// Sort the Rest based on String Name
|
|
|
|
|
string[] unsortedChoices = new string[list.Length];
|
|
|
|
|
for (int i = 0; i < list.Length; i++)
|
|
|
|
|
unsortedChoices[i] = inStrings[list[i]];
|
|
|
|
|
|
|
|
|
|
string[] sortedChoices = new string[unsortedChoices.Length];
|
|
|
|
|
Array.Copy(unsortedChoices, sortedChoices, unsortedChoices.Length);
|
|
|
|
|
Array.Sort(sortedChoices);
|
|
|
|
|
|
|
|
|
|
// Add the rest of the items
|
2015-03-11 01:44:51 +00:00
|
|
|
|
cbList.AddRange(sortedChoices.Select(t => new cbItem
|
2014-12-14 18:31:53 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
Text = t,
|
|
|
|
|
Value = list[Array.IndexOf(unsortedChoices, t)]
|
|
|
|
|
}));
|
2014-12-14 18:31:53 +00:00
|
|
|
|
}
|
|
|
|
|
return cbList;
|
|
|
|
|
}
|
|
|
|
|
internal static List<cbItem> getOffsetCBList(List<cbItem> cbList, string[] inStrings, int offset, int[] allowed)
|
|
|
|
|
{
|
|
|
|
|
if (allowed == null)
|
|
|
|
|
allowed = Enumerable.Range(0, inStrings.Length).ToArray();
|
|
|
|
|
|
|
|
|
|
int[] list = (int[])allowed.Clone();
|
|
|
|
|
for (int i = 0; i < list.Length; i++)
|
|
|
|
|
list[i] -= offset;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Sort the Rest based on String Name
|
|
|
|
|
string[] unsortedChoices = new string[allowed.Length];
|
|
|
|
|
for (int i = 0; i < allowed.Length; i++)
|
|
|
|
|
unsortedChoices[i] = inStrings[list[i]];
|
|
|
|
|
|
|
|
|
|
string[] sortedChoices = new string[unsortedChoices.Length];
|
|
|
|
|
Array.Copy(unsortedChoices, sortedChoices, unsortedChoices.Length);
|
|
|
|
|
Array.Sort(sortedChoices);
|
|
|
|
|
|
|
|
|
|
// Add the rest of the items
|
2015-03-11 01:44:51 +00:00
|
|
|
|
cbList.AddRange(sortedChoices.Select(s => new cbItem
|
2014-12-14 18:31:53 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
Text = s, Value = allowed[Array.IndexOf(unsortedChoices, s)]
|
|
|
|
|
}));
|
2014-12-14 18:31:53 +00:00
|
|
|
|
}
|
|
|
|
|
return cbList;
|
|
|
|
|
}
|
2014-12-18 01:04:57 +00:00
|
|
|
|
internal static List<cbItem> getVariedCBList(List<cbItem> cbList, string[] inStrings, int[] stringNum, int[] stringVal)
|
|
|
|
|
{
|
|
|
|
|
// Set up
|
|
|
|
|
List<cbItem> newlist = new List<cbItem>();
|
|
|
|
|
|
|
|
|
|
for (int i = 4; i > 1; i--) // add 4,3,2
|
|
|
|
|
{
|
|
|
|
|
// First 3 Balls are always first
|
2015-03-11 01:44:51 +00:00
|
|
|
|
cbItem ncbi = new cbItem
|
|
|
|
|
{
|
|
|
|
|
Text = inStrings[i],
|
|
|
|
|
Value = i
|
|
|
|
|
};
|
2014-12-18 01:04:57 +00:00
|
|
|
|
newlist.Add(ncbi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort the Rest based on String Name
|
|
|
|
|
string[] ballnames = new string[stringNum.Length];
|
|
|
|
|
for (int i = 0; i < stringNum.Length; i++)
|
|
|
|
|
ballnames[i] = inStrings[stringNum[i]];
|
|
|
|
|
|
|
|
|
|
string[] sortedballs = new string[stringNum.Length];
|
|
|
|
|
Array.Copy(ballnames, sortedballs, ballnames.Length);
|
|
|
|
|
Array.Sort(sortedballs);
|
|
|
|
|
|
|
|
|
|
// Add the rest of the balls
|
2015-03-11 01:44:51 +00:00
|
|
|
|
newlist.AddRange(sortedballs.Select(t => new cbItem
|
2014-12-18 01:04:57 +00:00
|
|
|
|
{
|
2015-03-11 01:44:51 +00:00
|
|
|
|
Text = t,
|
|
|
|
|
Value = stringVal[Array.IndexOf(ballnames, t)]
|
|
|
|
|
}));
|
2014-12-18 01:04:57 +00:00
|
|
|
|
return newlist;
|
|
|
|
|
}
|
2014-12-14 18:31:53 +00:00
|
|
|
|
internal static List<cbItem> getUnsortedCBList(string textfile)
|
|
|
|
|
{
|
|
|
|
|
// Set up
|
|
|
|
|
List<cbItem> cbList = new List<cbItem>();
|
2015-03-11 01:44:51 +00:00
|
|
|
|
string[] inputCSV = getSimpleStringList(textfile);
|
2014-12-14 18:31:53 +00:00
|
|
|
|
|
|
|
|
|
// Gather our data from the input file
|
|
|
|
|
for (int i = 1; i < inputCSV.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
string[] inputData = inputCSV[i].Split(',');
|
2015-03-11 01:44:51 +00:00
|
|
|
|
cbItem ncbi = new cbItem
|
|
|
|
|
{
|
|
|
|
|
Text = inputData[1],
|
|
|
|
|
Value = Convert.ToInt32(inputData[0])
|
|
|
|
|
};
|
2014-12-14 18:31:53 +00:00
|
|
|
|
cbList.Add(ncbi);
|
|
|
|
|
}
|
|
|
|
|
return cbList;
|
|
|
|
|
}
|
2015-03-14 02:59:51 +00:00
|
|
|
|
|
|
|
|
|
// QR Utility
|
|
|
|
|
internal static byte[] getQRData()
|
|
|
|
|
{
|
|
|
|
|
// Fetch data from QR code...
|
|
|
|
|
string address;
|
|
|
|
|
try { address = Clipboard.GetText(); }
|
|
|
|
|
catch { Alert("No text (url) in clipboard."); return null; }
|
|
|
|
|
try { if (address.Length < 4 || address.Substring(0, 3) != "htt") { Alert("Clipboard text is not a valid URL:", address); return null; } }
|
|
|
|
|
catch { Alert("Clipboard text is not a valid URL:", address); return null; }
|
2015-07-26 17:02:30 +00:00
|
|
|
|
string webURL = "http://api.qrserver.com/v1/read-qr-code/?fileurl=" + System.Web.HttpUtility.UrlEncode(address);
|
2015-03-14 02:59:51 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(webURL);
|
|
|
|
|
System.Net.HttpWebResponse httpWebReponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
|
|
|
|
|
var reader = new StreamReader(httpWebReponse.GetResponseStream());
|
|
|
|
|
string data = reader.ReadToEnd();
|
|
|
|
|
if (data.Contains("could not find")) { Alert("Reader could not find QR data in the image."); return null; }
|
|
|
|
|
if (data.Contains("filetype not supported")) { Alert("Input URL is not valid. Double check that it is an image (jpg/png).", address); return null; }
|
|
|
|
|
// Quickly convert the json response to a data string
|
|
|
|
|
string pkstr = data.Substring(data.IndexOf("#", StringComparison.Ordinal) + 1); // Trim intro
|
|
|
|
|
pkstr = pkstr.Substring(0, pkstr.IndexOf("\",\"error\":null}]}]", StringComparison.Ordinal)); // Trim outro
|
|
|
|
|
if (pkstr.Contains("nQR-Code:")) pkstr = pkstr.Substring(0, pkstr.IndexOf("nQR-Code:", StringComparison.Ordinal)); // Remove multiple QR codes in same image
|
|
|
|
|
pkstr = pkstr.Replace("\\", ""); // Rectify response
|
|
|
|
|
|
|
|
|
|
try { return Convert.FromBase64String(pkstr); }
|
|
|
|
|
catch { Alert("QR string to Data failed.", pkstr); return null; }
|
|
|
|
|
}
|
|
|
|
|
catch { Alert("Unable to connect to the internet to decode QR code."); return null;}
|
|
|
|
|
}
|
|
|
|
|
internal static Image getQRImage(byte[] data, string server)
|
|
|
|
|
{
|
|
|
|
|
string qrdata = Convert.ToBase64String(data);
|
|
|
|
|
string message = server + qrdata;
|
2015-07-26 17:02:30 +00:00
|
|
|
|
string webURL = "http://chart.apis.google.com/chart?chs=365x365&cht=qr&chl=" + System.Web.HttpUtility.UrlEncode(message);
|
2015-03-14 02:59:51 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(webURL);
|
|
|
|
|
System.Net.HttpWebResponse httpWebReponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
|
|
|
|
|
Stream stream = httpWebReponse.GetResponseStream();
|
|
|
|
|
if (stream != null) return Image.FromStream(stream);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
if (Prompt(MessageBoxButtons.YesNo,
|
|
|
|
|
"Unable to connect to the internet to receive QR code.",
|
|
|
|
|
"Copy QR URL to Clipboard?")
|
|
|
|
|
!= DialogResult.Yes) return null;
|
|
|
|
|
try { Clipboard.SetText(webURL); }
|
|
|
|
|
catch { Alert("Failed to set text to Clipboard"); }
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-07-28 22:28:06 +00:00
|
|
|
|
}
|
2015-01-03 01:07:29 +00:00
|
|
|
|
}
|